xref: /freebsd/sys/dev/cardbus/cardbus.c (revision de9468837c92cab304c658480bd32dbe4e022d01)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2000,2001 Jonathan Chen.  All rights reserved.
5  * Copyright (c) 2003-2008 M. Warner Losh <imp@FreeBSD.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/eventhandler.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
34 #include <sys/kernel.h>
35 #include <sys/sysctl.h>
36 
37 #include <sys/bus.h>
38 #include <machine/bus.h>
39 #include <sys/rman.h>
40 #include <machine/resource.h>
41 
42 #include <sys/pciio.h>
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/pci_private.h>
46 
47 #include <dev/cardbus/cardbusreg.h>
48 #include <dev/cardbus/cardbusvar.h>
49 #include <dev/cardbus/cardbus_cis.h>
50 #include <dev/pccard/pccard_cis.h>
51 #include <dev/pccard/pccardvar.h>
52 
53 #include "power_if.h"
54 #include "pcib_if.h"
55 
56 /* sysctl vars */
57 static SYSCTL_NODE(_hw, OID_AUTO, cardbus, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
58     "CardBus parameters");
59 
60 int    cardbus_debug = 0;
61 SYSCTL_INT(_hw_cardbus, OID_AUTO, debug, CTLFLAG_RWTUN,
62     &cardbus_debug, 0, "CardBus debug");
63 
64 int    cardbus_cis_debug = 0;
65 SYSCTL_INT(_hw_cardbus, OID_AUTO, cis_debug, CTLFLAG_RWTUN,
66     &cardbus_cis_debug, 0, "CardBus CIS debug");
67 
68 #define	DPRINTF(a) if (cardbus_debug) printf a
69 #define	DEVPRINTF(x) if (cardbus_debug) device_printf x
70 
71 static int	cardbus_attach(device_t cbdev);
72 static int	cardbus_attach_card(device_t cbdev);
73 static int	cardbus_detach(device_t cbdev);
74 static int	cardbus_detach_card(device_t cbdev);
75 static void	cardbus_device_setup_regs(pcicfgregs *cfg);
76 static void	cardbus_driver_added(device_t cbdev, driver_t *driver);
77 static int	cardbus_probe(device_t cbdev);
78 static int	cardbus_read_ivar(device_t cbdev, device_t child, int which,
79 		    uintptr_t *result);
80 
81 /************************************************************************/
82 /* Probe/Attach								*/
83 /************************************************************************/
84 
85 static int
86 cardbus_probe(device_t cbdev)
87 {
88 	device_set_desc(cbdev, "CardBus bus");
89 	return (0);
90 }
91 
92 static int
93 cardbus_attach(device_t cbdev)
94 {
95 	struct cardbus_softc *sc;
96 	int rid;
97 
98 	sc = device_get_softc(cbdev);
99 	sc->sc_dev = cbdev;
100 	rid = 0;
101 	sc->sc_bus = bus_alloc_resource(cbdev, PCI_RES_BUS, &rid,
102 	    pcib_get_bus(cbdev), pcib_get_bus(cbdev), 1, 0);
103 	if (sc->sc_bus == NULL) {
104 		device_printf(cbdev, "failed to allocate bus number\n");
105 		return (ENXIO);
106 	}
107 	return (0);
108 }
109 
110 static int
111 cardbus_detach(device_t cbdev)
112 {
113 	struct cardbus_softc *sc;
114 
115 	cardbus_detach_card(cbdev);
116 	sc = device_get_softc(cbdev);
117 	device_printf(cbdev, "Freeing up the allocatd bus\n");
118 	(void)bus_release_resource(cbdev, PCI_RES_BUS, 0, sc->sc_bus);
119 	return (0);
120 }
121 
122 static int
123 cardbus_suspend(device_t self)
124 {
125 
126 	cardbus_detach_card(self);
127 	return (0);
128 }
129 
130 static int
131 cardbus_resume(device_t self)
132 {
133 
134 	return (0);
135 }
136 
137 /************************************************************************/
138 /* Attach/Detach card							*/
139 /************************************************************************/
140 
141 static void
142 cardbus_device_setup_regs(pcicfgregs *cfg)
143 {
144 	device_t dev = cfg->dev;
145 	int i;
146 
147 	/*
148 	 * Some cards power up with garbage in their BARs.  This
149 	 * code clears all that junk out.
150 	 */
151 	for (i = 0; i < PCIR_MAX_BAR_0; i++)
152 		pci_write_config(dev, PCIR_BAR(i), 0, 4);
153 
154 	cfg->intline =
155 	    pci_get_irq(device_get_parent(device_get_parent(dev)));
156 	pci_write_config(dev, PCIR_INTLINE, cfg->intline, 1);
157 	pci_write_config(dev, PCIR_CACHELNSZ, 0x08, 1);
158 	pci_write_config(dev, PCIR_LATTIMER, 0xa8, 1);
159 	pci_write_config(dev, PCIR_MINGNT, 0x14, 1);
160 	pci_write_config(dev, PCIR_MAXLAT, 0x14, 1);
161 }
162 
163 static struct pci_devinfo *
164 cardbus_alloc_devinfo(device_t dev)
165 {
166 	struct cardbus_devinfo *dinfo;
167 
168 	dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
169 	return (&dinfo->pci);
170 }
171 
172 static int
173 cardbus_attach_card(device_t cbdev)
174 {
175 	device_t brdev = device_get_parent(cbdev);
176 	device_t child;
177 	int bus, domain, slot, func;
178 	int cardattached = 0;
179 	int cardbusfunchigh = 0;
180 	struct cardbus_softc *sc;
181 
182 	sc = device_get_softc(cbdev);
183 	cardbus_detach_card(cbdev); /* detach existing cards */
184 	POWER_DISABLE_SOCKET(brdev, cbdev); /* Turn the socket off first */
185 	POWER_ENABLE_SOCKET(brdev, cbdev);
186 	domain = pcib_get_domain(cbdev);
187 	bus = pcib_get_bus(cbdev);
188 	slot = 0;
189 	bus_topo_lock();
190 	/* For each function, set it up and try to attach a driver to it */
191 	for (func = 0; func <= cardbusfunchigh; func++) {
192 		struct cardbus_devinfo *dinfo;
193 
194 		dinfo = (struct cardbus_devinfo *)
195 		    pci_read_device(brdev, cbdev, domain, bus, slot, func);
196 		if (dinfo == NULL)
197 			continue;
198 		if (dinfo->pci.cfg.mfdev)
199 			cardbusfunchigh = PCI_FUNCMAX;
200 
201 		child = device_add_child(cbdev, NULL, DEVICE_UNIT_ANY);
202 		if (child == NULL) {
203 			DEVPRINTF((cbdev, "Cannot add child!\n"));
204 			pci_freecfg((struct pci_devinfo *)dinfo);
205 			continue;
206 		}
207 		dinfo->pci.cfg.dev = child;
208 		resource_list_init(&dinfo->pci.resources);
209 		device_set_ivars(child, dinfo);
210 		cardbus_device_create(sc, dinfo, cbdev, child);
211 		if (cardbus_do_cis(cbdev, child) != 0)
212 			DEVPRINTF((cbdev, "Warning: Bogus CIS ignored\n"));
213 		pci_cfg_save(dinfo->pci.cfg.dev, &dinfo->pci, 0);
214 		pci_cfg_restore(dinfo->pci.cfg.dev, &dinfo->pci);
215 		cardbus_device_setup_regs(&dinfo->pci.cfg);
216 		pci_add_resources(cbdev, child, 1, dinfo->mprefetchable);
217 		pci_print_verbose(&dinfo->pci);
218 		if (device_probe_and_attach(child) == 0)
219 			cardattached++;
220 		else
221 			pci_cfg_save(dinfo->pci.cfg.dev, &dinfo->pci, 1);
222 	}
223 	bus_topo_unlock();
224 	if (cardattached > 0)
225 		return (0);
226 /*	POWER_DISABLE_SOCKET(brdev, cbdev); */
227 	return (ENOENT);
228 }
229 
230 static void
231 cardbus_child_deleted(device_t cbdev, device_t child)
232 {
233 	struct cardbus_devinfo *dinfo = device_get_ivars(child);
234 
235 	if (dinfo->pci.cfg.dev != child)
236 		device_printf(cbdev, "devinfo dev mismatch\n");
237 	cardbus_device_destroy(dinfo);
238 	pci_child_deleted(cbdev, child);
239 }
240 
241 static int
242 cardbus_detach_card(device_t cbdev)
243 {
244 	int err = 0;
245 
246 	bus_topo_lock();
247 	err = bus_generic_detach(cbdev);
248 	if (err == 0)
249 		err = device_delete_children(cbdev);
250 	bus_topo_unlock();
251 	if (err)
252 		return (err);
253 
254 	POWER_DISABLE_SOCKET(device_get_parent(cbdev), cbdev);
255 	return (err);
256 }
257 
258 static void
259 cardbus_driver_added(device_t cbdev, driver_t *driver)
260 {
261 	int numdevs;
262 	device_t *devlist;
263 	device_t dev;
264 	int i;
265 	struct cardbus_devinfo *dinfo;
266 
267 	DEVICE_IDENTIFY(driver, cbdev);
268 	if (device_get_children(cbdev, &devlist, &numdevs) != 0)
269 		return;
270 
271 	/*
272 	 * If there are no drivers attached, but there are children,
273 	 * then power the card up.
274 	 */
275 	for (i = 0; i < numdevs; i++) {
276 		dev = devlist[i];
277 		if (device_get_state(dev) != DS_NOTPRESENT)
278 		    break;
279 	}
280 	if (i > 0 && i == numdevs)
281 		POWER_ENABLE_SOCKET(device_get_parent(cbdev), cbdev);
282 	for (i = 0; i < numdevs; i++) {
283 		dev = devlist[i];
284 		if (device_get_state(dev) != DS_NOTPRESENT)
285 			continue;
286 		dinfo = device_get_ivars(dev);
287 		pci_print_verbose(&dinfo->pci);
288 		if (bootverbose)
289 			printf("pci%d:%d:%d:%d: reprobing on driver added\n",
290 			    dinfo->pci.cfg.domain, dinfo->pci.cfg.bus,
291 			    dinfo->pci.cfg.slot, dinfo->pci.cfg.func);
292 		pci_cfg_restore(dinfo->pci.cfg.dev, &dinfo->pci);
293 		if (device_probe_and_attach(dev) != 0)
294 			pci_cfg_save(dev, &dinfo->pci, 1);
295 	}
296 	free(devlist, M_TEMP);
297 }
298 
299 /************************************************************************/
300 /* Other Bus Methods							*/
301 /************************************************************************/
302 
303 static int
304 cardbus_read_ivar(device_t cbdev, device_t child, int which, uintptr_t *result)
305 {
306 	struct cardbus_devinfo *dinfo;
307 
308 	dinfo = device_get_ivars(child);
309 
310 	switch (which) {
311 	case PCI_IVAR_ETHADDR:
312 		/*
313 		 * The generic accessor doesn't deal with failure, so
314 		 * we set the return value, then return an error.
315 		 */
316 		if (dinfo->fepresent & (1 << PCCARD_TPLFE_TYPE_LAN_NID)) {
317 			*((uint8_t **) result) = dinfo->funce.lan.nid;
318 			break;
319 		}
320 		*((uint8_t **) result) = NULL;
321 		return (EINVAL);
322 	default:
323 		return (pci_read_ivar(cbdev, child, which, result));
324 	}
325 	return 0;
326 }
327 
328 static device_method_t cardbus_methods[] = {
329 	/* Device interface */
330 	DEVMETHOD(device_probe,		cardbus_probe),
331 	DEVMETHOD(device_attach,	cardbus_attach),
332 	DEVMETHOD(device_detach,	cardbus_detach),
333 	DEVMETHOD(device_suspend,	cardbus_suspend),
334 	DEVMETHOD(device_resume,	cardbus_resume),
335 
336 	/* Bus interface */
337 	DEVMETHOD(bus_child_deleted,	cardbus_child_deleted),
338 	DEVMETHOD(bus_get_dma_tag,	bus_generic_get_dma_tag),
339 	DEVMETHOD(bus_read_ivar,	cardbus_read_ivar),
340 	DEVMETHOD(bus_driver_added,	cardbus_driver_added),
341 	DEVMETHOD(bus_rescan,		bus_null_rescan),
342 
343 	/* Card Interface */
344 	DEVMETHOD(card_attach_card,	cardbus_attach_card),
345 	DEVMETHOD(card_detach_card,	cardbus_detach_card),
346 
347 	/* PCI interface */
348 	DEVMETHOD(pci_alloc_devinfo,	cardbus_alloc_devinfo),
349 	{0,0}
350 };
351 
352 DEFINE_CLASS_1(cardbus, cardbus_driver, cardbus_methods,
353     sizeof(struct cardbus_softc), pci_driver);
354 
355 DRIVER_MODULE(cardbus, cbb, cardbus_driver, 0, 0);
356 MODULE_VERSION(cardbus, 1);
357