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