cardbus.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * cardbus.c -- 16-bit PCMCIA core support
  4. *
  5. * The initial developer of the original code is David A. Hinds
  6. * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  7. * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
  8. *
  9. * (C) 1999 David A. Hinds
  10. */
  11. /*
  12. * Cardbus handling has been re-written to be more of a PCI bridge thing,
  13. * and the PCI code basically does all the resource handling.
  14. *
  15. * Linus, Jan 2000
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/pci.h>
  20. #include <pcmcia/ss.h>
  21. #include <pcmcia/cistpl.h>
  22. #include "cs_internal.h"
  23. static void cardbus_config_irq_and_cls(struct pci_bus *bus, int irq)
  24. {
  25. struct pci_dev *dev;
  26. list_for_each_entry(dev, &bus->devices, bus_list) {
  27. u8 irq_pin;
  28. /*
  29. * Since there is only one interrupt available to
  30. * CardBus devices, all devices downstream of this
  31. * device must be using this IRQ.
  32. */
  33. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq_pin);
  34. if (irq_pin) {
  35. dev->irq = irq;
  36. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
  37. }
  38. /*
  39. * Some controllers transfer very slowly with 0 CLS.
  40. * Configure it. This may fail as CLS configuration
  41. * is mandatory only for MWI.
  42. */
  43. pci_set_cacheline_size(dev);
  44. if (dev->subordinate)
  45. cardbus_config_irq_and_cls(dev->subordinate, irq);
  46. }
  47. }
  48. /**
  49. * cb_alloc() - add CardBus device
  50. * @s: the pcmcia_socket where the CardBus device is located
  51. *
  52. * cb_alloc() allocates the kernel data structures for a Cardbus device
  53. * and handles the lowest level PCI device setup issues.
  54. */
  55. int __ref cb_alloc(struct pcmcia_socket *s)
  56. {
  57. struct pci_bus *bus = s->cb_dev->subordinate;
  58. struct pci_dev *dev;
  59. unsigned int max, pass;
  60. pci_lock_rescan_remove();
  61. s->functions = pci_scan_slot(bus, PCI_DEVFN(0, 0));
  62. pci_fixup_cardbus(bus);
  63. max = bus->busn_res.start;
  64. for (pass = 0; pass < 2; pass++)
  65. for_each_pci_bridge(dev, bus)
  66. max = pci_scan_bridge(bus, dev, max, pass);
  67. /*
  68. * Size all resources below the CardBus controller.
  69. */
  70. pci_bus_size_bridges(bus);
  71. pci_bus_assign_resources(bus);
  72. cardbus_config_irq_and_cls(bus, s->pci_irq);
  73. /* socket specific tune function */
  74. if (s->tune_bridge)
  75. s->tune_bridge(s, bus);
  76. pci_bus_add_devices(bus);
  77. pci_unlock_rescan_remove();
  78. return 0;
  79. }
  80. /**
  81. * cb_free() - remove CardBus device
  82. * @s: the pcmcia_socket where the CardBus device was located
  83. *
  84. * cb_free() handles the lowest level PCI device cleanup.
  85. */
  86. void cb_free(struct pcmcia_socket *s)
  87. {
  88. struct pci_dev *bridge, *dev, *tmp;
  89. struct pci_bus *bus;
  90. bridge = s->cb_dev;
  91. if (!bridge)
  92. return;
  93. bus = bridge->subordinate;
  94. if (!bus)
  95. return;
  96. pci_lock_rescan_remove();
  97. list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list)
  98. pci_stop_and_remove_bus_device(dev);
  99. pci_unlock_rescan_remove();
  100. }