| Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * cardbus.c -- 16-bit PCMCIA core support |
| 4 | * |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | /* |
| 13 | * Cardbus handling has been re-written to be more of a PCI bridge thing, |
| 14 | * and the PCI code basically does all the resource handling. |
| 15 | * |
| 16 | * Linus, Jan 2000 |
| 17 | */ |
| 18 | |
| 19 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/kernel.h> |
| Dominik Brodowski | 57197b9 | 2010-01-02 17:27:33 +0100 | [diff] [blame] | 21 | #include <linux/module.h> |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/pci.h> |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <pcmcia/ss.h> |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| Tejun Heo | 15ea76d | 2009-09-22 17:34:48 +0900 | [diff] [blame] | 27 | static void cardbus_config_irq_and_cls(struct pci_bus *bus, int irq) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
| 29 | struct pci_dev *dev; |
| 30 | |
| 31 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 32 | u8 irq_pin; |
| 33 | |
| Tejun Heo | 15ea76d | 2009-09-22 17:34:48 +0900 | [diff] [blame] | 34 | /* |
| 35 | * Since there is only one interrupt available to |
| 36 | * CardBus devices, all devices downstream of this |
| 37 | * device must be using this IRQ. |
| 38 | */ |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq_pin); |
| 40 | if (irq_pin) { |
| 41 | dev->irq = irq; |
| 42 | pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq); |
| 43 | } |
| 44 | |
| Tejun Heo | 15ea76d | 2009-09-22 17:34:48 +0900 | [diff] [blame] | 45 | /* |
| 46 | * Some controllers transfer very slowly with 0 CLS. |
| 47 | * Configure it. This may fail as CLS configuration |
| 48 | * is mandatory only for MWI. |
| 49 | */ |
| 50 | pci_set_cacheline_size(dev); |
| 51 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | if (dev->subordinate) |
| Tejun Heo | 15ea76d | 2009-09-22 17:34:48 +0900 | [diff] [blame] | 53 | cardbus_config_irq_and_cls(dev->subordinate, irq); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| Dominik Brodowski | 57197b9 | 2010-01-02 17:27:33 +0100 | [diff] [blame] | 57 | /** |
| 58 | * cb_alloc() - add CardBus device |
| 59 | * @s: the pcmcia_socket where the CardBus device is located |
| 60 | * |
| 61 | * cb_alloc() allocates the kernel data structures for a Cardbus device |
| 62 | * and handles the lowest level PCI device setup issues. |
| 63 | */ |
| Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 64 | int __ref cb_alloc(struct pcmcia_socket *s) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
| 66 | struct pci_bus *bus = s->cb_dev->subordinate; |
| 67 | struct pci_dev *dev; |
| 68 | unsigned int max, pass; |
| 69 | |
| Rafael J. Wysocki | 5ef68e8 | 2014-01-10 15:25:34 +0100 | [diff] [blame] | 70 | pci_lock_rescan_remove(); |
| 71 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | s->functions = pci_scan_slot(bus, PCI_DEVFN(0, 0)); |
| Dominik Brodowski | 6e83ee0 | 2010-03-02 08:57:33 +0100 | [diff] [blame] | 73 | pci_fixup_cardbus(bus); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| Yinghai Lu | b918c62 | 2012-05-17 18:51:11 -0700 | [diff] [blame] | 75 | max = bus->busn_res.start; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | for (pass = 0; pass < 2; pass++) |
| Andy Shevchenko | 24a0c65 | 2017-10-20 15:38:54 -0500 | [diff] [blame] | 77 | for_each_pci_bridge(dev, bus) |
| 78 | max = pci_scan_bridge(bus, dev, max, pass); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * Size all resources below the CardBus controller. |
| 82 | */ |
| 83 | pci_bus_size_bridges(bus); |
| 84 | pci_bus_assign_resources(bus); |
| Tejun Heo | 15ea76d | 2009-09-22 17:34:48 +0900 | [diff] [blame] | 85 | cardbus_config_irq_and_cls(bus, s->pci_irq); |
| Daniel Ritz | 8c3520d | 2005-08-21 22:29:26 -0700 | [diff] [blame] | 86 | |
| 87 | /* socket specific tune function */ |
| 88 | if (s->tune_bridge) |
| 89 | s->tune_bridge(s, bus); |
| 90 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | pci_bus_add_devices(bus); |
| 92 | |
| Rafael J. Wysocki | 5ef68e8 | 2014-01-10 15:25:34 +0100 | [diff] [blame] | 93 | pci_unlock_rescan_remove(); |
| Dominik Brodowski | 4c89e88 | 2008-08-03 10:07:45 +0200 | [diff] [blame] | 94 | return 0; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| Dominik Brodowski | 57197b9 | 2010-01-02 17:27:33 +0100 | [diff] [blame] | 97 | /** |
| 98 | * cb_free() - remove CardBus device |
| 99 | * @s: the pcmcia_socket where the CardBus device was located |
| 100 | * |
| 101 | * cb_free() handles the lowest level PCI device cleanup. |
| 102 | */ |
| Dominik Brodowski | 9fea84f | 2009-12-07 22:11:45 +0100 | [diff] [blame] | 103 | void cb_free(struct pcmcia_socket *s) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
| Bjorn Helgaas | 0a14057 | 2012-08-17 10:49:04 -0600 | [diff] [blame] | 105 | struct pci_dev *bridge, *dev, *tmp; |
| 106 | struct pci_bus *bus; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| Bjorn Helgaas | 0a14057 | 2012-08-17 10:49:04 -0600 | [diff] [blame] | 108 | bridge = s->cb_dev; |
| 109 | if (!bridge) |
| 110 | return; |
| 111 | |
| 112 | bus = bridge->subordinate; |
| 113 | if (!bus) |
| 114 | return; |
| 115 | |
| Rafael J. Wysocki | 5ef68e8 | 2014-01-10 15:25:34 +0100 | [diff] [blame] | 116 | pci_lock_rescan_remove(); |
| 117 | |
| Bjorn Helgaas | 0a14057 | 2012-08-17 10:49:04 -0600 | [diff] [blame] | 118 | list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) |
| 119 | pci_stop_and_remove_bus_device(dev); |
| Rafael J. Wysocki | 5ef68e8 | 2014-01-10 15:25:34 +0100 | [diff] [blame] | 120 | |
| 121 | pci_unlock_rescan_remove(); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |