blob: 45c8252c8edcd5c5251d4c6b723cc293c5259899 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * cardbus.c -- 16-bit PCMCIA core support
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kernel.h>
Dominik Brodowski57197b92010-01-02 17:27:33 +010021#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <pcmcia/ss.h>
Ben Dooks (Codethink)990a1b52019-10-17 12:40:59 +010025#include <pcmcia/cistpl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Ben Dooks (Codethink)990a1b52019-10-17 12:40:59 +010027#include "cs_internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Tejun Heo15ea76d2009-09-22 17:34:48 +090029static void cardbus_config_irq_and_cls(struct pci_bus *bus, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 struct pci_dev *dev;
32
33 list_for_each_entry(dev, &bus->devices, bus_list) {
34 u8 irq_pin;
35
Tejun Heo15ea76d2009-09-22 17:34:48 +090036 /*
37 * Since there is only one interrupt available to
38 * CardBus devices, all devices downstream of this
39 * device must be using this IRQ.
40 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq_pin);
42 if (irq_pin) {
43 dev->irq = irq;
44 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
45 }
46
Tejun Heo15ea76d2009-09-22 17:34:48 +090047 /*
48 * Some controllers transfer very slowly with 0 CLS.
49 * Configure it. This may fail as CLS configuration
50 * is mandatory only for MWI.
51 */
52 pci_set_cacheline_size(dev);
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (dev->subordinate)
Tejun Heo15ea76d2009-09-22 17:34:48 +090055 cardbus_config_irq_and_cls(dev->subordinate, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 }
57}
58
Dominik Brodowski57197b92010-01-02 17:27:33 +010059/**
60 * cb_alloc() - add CardBus device
61 * @s: the pcmcia_socket where the CardBus device is located
62 *
63 * cb_alloc() allocates the kernel data structures for a Cardbus device
64 * and handles the lowest level PCI device setup issues.
65 */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +010066int __ref cb_alloc(struct pcmcia_socket *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 struct pci_bus *bus = s->cb_dev->subordinate;
69 struct pci_dev *dev;
70 unsigned int max, pass;
71
Rafael J. Wysocki5ef68e82014-01-10 15:25:34 +010072 pci_lock_rescan_remove();
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 s->functions = pci_scan_slot(bus, PCI_DEVFN(0, 0));
Dominik Brodowski6e83ee02010-03-02 08:57:33 +010075 pci_fixup_cardbus(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Yinghai Lub918c622012-05-17 18:51:11 -070077 max = bus->busn_res.start;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 for (pass = 0; pass < 2; pass++)
Andy Shevchenko24a0c652017-10-20 15:38:54 -050079 for_each_pci_bridge(dev, bus)
80 max = pci_scan_bridge(bus, dev, max, pass);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 /*
83 * Size all resources below the CardBus controller.
84 */
85 pci_bus_size_bridges(bus);
86 pci_bus_assign_resources(bus);
Tejun Heo15ea76d2009-09-22 17:34:48 +090087 cardbus_config_irq_and_cls(bus, s->pci_irq);
Daniel Ritz8c3520d2005-08-21 22:29:26 -070088
89 /* socket specific tune function */
90 if (s->tune_bridge)
91 s->tune_bridge(s, bus);
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 pci_bus_add_devices(bus);
94
Rafael J. Wysocki5ef68e82014-01-10 15:25:34 +010095 pci_unlock_rescan_remove();
Dominik Brodowski4c89e882008-08-03 10:07:45 +020096 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Dominik Brodowski57197b92010-01-02 17:27:33 +010099/**
100 * cb_free() - remove CardBus device
101 * @s: the pcmcia_socket where the CardBus device was located
102 *
103 * cb_free() handles the lowest level PCI device cleanup.
104 */
Dominik Brodowski9fea84f2009-12-07 22:11:45 +0100105void cb_free(struct pcmcia_socket *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Bjorn Helgaas0a140572012-08-17 10:49:04 -0600107 struct pci_dev *bridge, *dev, *tmp;
108 struct pci_bus *bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Bjorn Helgaas0a140572012-08-17 10:49:04 -0600110 bridge = s->cb_dev;
111 if (!bridge)
112 return;
113
114 bus = bridge->subordinate;
115 if (!bus)
116 return;
117
Rafael J. Wysocki5ef68e82014-01-10 15:25:34 +0100118 pci_lock_rescan_remove();
119
Bjorn Helgaas0a140572012-08-17 10:49:04 -0600120 list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list)
121 pci_stop_and_remove_bus_device(dev);
Rafael J. Wysocki5ef68e82014-01-10 15:25:34 +0100122
123 pci_unlock_rescan_remove();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}