xref: /openbmc/linux/drivers/pcmcia/cardbus.c (revision f66501dc)
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 /*
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 
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 
24 #include <pcmcia/ss.h>
25 
26 
27 static void cardbus_config_irq_and_cls(struct pci_bus *bus, int irq)
28 {
29 	struct pci_dev *dev;
30 
31 	list_for_each_entry(dev, &bus->devices, bus_list) {
32 		u8 irq_pin;
33 
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 		 */
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 
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 
52 		if (dev->subordinate)
53 			cardbus_config_irq_and_cls(dev->subordinate, irq);
54 	}
55 }
56 
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  */
64 int __ref cb_alloc(struct pcmcia_socket *s)
65 {
66 	struct pci_bus *bus = s->cb_dev->subordinate;
67 	struct pci_dev *dev;
68 	unsigned int max, pass;
69 
70 	pci_lock_rescan_remove();
71 
72 	s->functions = pci_scan_slot(bus, PCI_DEVFN(0, 0));
73 	pci_fixup_cardbus(bus);
74 
75 	max = bus->busn_res.start;
76 	for (pass = 0; pass < 2; pass++)
77 		for_each_pci_bridge(dev, bus)
78 			max = pci_scan_bridge(bus, dev, max, pass);
79 
80 	/*
81 	 * Size all resources below the CardBus controller.
82 	 */
83 	pci_bus_size_bridges(bus);
84 	pci_bus_assign_resources(bus);
85 	cardbus_config_irq_and_cls(bus, s->pci_irq);
86 
87 	/* socket specific tune function */
88 	if (s->tune_bridge)
89 		s->tune_bridge(s, bus);
90 
91 	pci_bus_add_devices(bus);
92 
93 	pci_unlock_rescan_remove();
94 	return 0;
95 }
96 
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  */
103 void cb_free(struct pcmcia_socket *s)
104 {
105 	struct pci_dev *bridge, *dev, *tmp;
106 	struct pci_bus *bus;
107 
108 	bridge = s->cb_dev;
109 	if (!bridge)
110 		return;
111 
112 	bus = bridge->subordinate;
113 	if (!bus)
114 		return;
115 
116 	pci_lock_rescan_remove();
117 
118 	list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list)
119 		pci_stop_and_remove_bus_device(dev);
120 
121 	pci_unlock_rescan_remove();
122 }
123