xref: /freebsd/sys/dev/cardbus/cardbus.c (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
1 /*-
2  * Copyright (c) 2003 M. Warner Losh.  All Rights Reserved.
3  * Copyright (c) 2000,2001 Jonathan Chen.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.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 SYSCTL_NODE(_hw, OID_AUTO, cardbus, CTLFLAG_RD, 0, "CardBus parameters");
58 
59 int    cardbus_debug = 0;
60 TUNABLE_INT("hw.cardbus.debug", &cardbus_debug);
61 SYSCTL_INT(_hw_cardbus, OID_AUTO, debug, CTLFLAG_RW,
62     &cardbus_debug, 0,
63   "CardBus debug");
64 
65 int    cardbus_cis_debug = 0;
66 TUNABLE_INT("hw.cardbus.cis_debug", &cardbus_cis_debug);
67 SYSCTL_INT(_hw_cardbus, OID_AUTO, cis_debug, CTLFLAG_RW,
68     &cardbus_cis_debug, 0,
69   "CardBus CIS debug");
70 
71 #define	DPRINTF(a) if (cardbus_debug) printf a
72 #define	DEVPRINTF(x) if (cardbus_debug) device_printf x
73 
74 static int	cardbus_attach(device_t cbdev);
75 static int	cardbus_attach_card(device_t cbdev);
76 static int	cardbus_detach(device_t cbdev);
77 static int	cardbus_detach_card(device_t cbdev);
78 static void	cardbus_device_setup_regs(device_t brdev, int b, int s, int f,
79 		    pcicfgregs *cfg);
80 static void	cardbus_driver_added(device_t cbdev, driver_t *driver);
81 static int	cardbus_probe(device_t cbdev);
82 static int	cardbus_read_ivar(device_t cbdev, device_t child, int which,
83 		    uintptr_t *result);
84 static void	cardbus_release_all_resources(device_t cbdev,
85 		    struct cardbus_devinfo *dinfo);
86 static int	cardbus_write_ivar(device_t cbdev, device_t child, int which,
87 		    uintptr_t value);
88 
89 /************************************************************************/
90 /* Probe/Attach								*/
91 /************************************************************************/
92 
93 static int
94 cardbus_probe(device_t cbdev)
95 {
96 	device_set_desc(cbdev, "CardBus bus");
97 	return (0);
98 }
99 
100 static int
101 cardbus_attach(device_t cbdev)
102 {
103 	struct cardbus_softc *sc = device_get_softc(cbdev);
104 
105 	sc->sc_dev = cbdev;
106 	cardbus_device_create(sc);
107 	return (0);
108 }
109 
110 static int
111 cardbus_detach(device_t cbdev)
112 {
113 	struct cardbus_softc *sc = device_get_softc(cbdev);
114 
115 	cardbus_detach_card(cbdev);
116 	cardbus_device_destroy(sc);
117 	return (0);
118 }
119 
120 static int
121 cardbus_suspend(device_t self)
122 {
123 
124 	cardbus_detach_card(self);
125 	return (0);
126 }
127 
128 static int
129 cardbus_resume(device_t self)
130 {
131 
132 	return (0);
133 }
134 
135 /************************************************************************/
136 /* Attach/Detach card							*/
137 /************************************************************************/
138 
139 static void
140 cardbus_device_setup_regs(device_t brdev, int b, int s, int f, pcicfgregs *cfg)
141 {
142 	PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_INTLINE,
143 	    pci_get_irq(device_get_parent(brdev)), 1);
144 	cfg->intline = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_INTLINE, 1);
145 
146 	PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_CACHELNSZ, 0x08, 1);
147 	cfg->cachelnsz = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_CACHELNSZ, 1);
148 
149 	PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_LATTIMER, 0xa8, 1);
150 	cfg->lattimer = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_LATTIMER, 1);
151 
152 	PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_MINGNT, 0x14, 1);
153 	cfg->mingnt = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_MINGNT, 1);
154 
155 	PCIB_WRITE_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 0x14, 1);
156 	cfg->maxlat = PCIB_READ_CONFIG(brdev, b, s, f, PCIR_MAXLAT, 1);
157 }
158 
159 static int
160 cardbus_attach_card(device_t cbdev)
161 {
162 	device_t brdev = device_get_parent(cbdev);
163 	device_t child;
164 	int cardattached = 0;
165 	int bus, slot, func;
166 	int cardbusfunchigh = 0;
167 
168 	cardbus_detach_card(cbdev); /* detach existing cards */
169 	POWER_ENABLE_SOCKET(brdev, cbdev);
170 	bus = pcib_get_bus(cbdev);
171 	slot = 0;
172 	/* For each function, set it up and try to attach a driver to it */
173 	for (func = 0; func <= cardbusfunchigh; func++) {
174 		struct cardbus_devinfo *dinfo;
175 
176 		dinfo = (struct cardbus_devinfo *)
177 		    pci_read_device(brdev, bus, slot, func,
178 			sizeof(struct cardbus_devinfo));
179 		if (dinfo == NULL)
180 			continue;
181 		if (dinfo->pci.cfg.mfdev)
182 			cardbusfunchigh = PCI_FUNCMAX;
183 
184 		cardbus_device_setup_regs(brdev, bus, slot, func,
185 		    &dinfo->pci.cfg);
186 		child = device_add_child(cbdev, NULL, -1);
187 		if (child == NULL) {
188 			DEVPRINTF((cbdev, "Cannot add child!\n"));
189 			pci_freecfg((struct pci_devinfo *)dinfo);
190 			continue;
191 		}
192 		dinfo->pci.cfg.dev = child;
193 		resource_list_init(&dinfo->pci.resources);
194 		device_set_ivars(child, dinfo);
195 		if (cardbus_do_cis(cbdev, child) != 0) {
196 			DEVPRINTF((cbdev, "Can't parse cis\n"));
197 			pci_freecfg((struct pci_devinfo *)dinfo);
198 			continue;
199 		}
200 		pci_cfg_save(dinfo->pci.cfg.dev, &dinfo->pci, 0);
201 		pci_cfg_restore(dinfo->pci.cfg.dev, &dinfo->pci);
202 		pci_add_resources(cbdev, child, 1, dinfo->mprefetchable);
203 		pci_print_verbose(&dinfo->pci);
204 		if (device_probe_and_attach(child) == 0)
205 			cardattached++;
206 		else
207 			pci_cfg_save(dinfo->pci.cfg.dev, &dinfo->pci, 1);
208 	}
209 	if (cardattached > 0)
210 		return (0);
211 	POWER_DISABLE_SOCKET(brdev, cbdev);
212 	return (ENOENT);
213 }
214 
215 static int
216 cardbus_detach_card(device_t cbdev)
217 {
218 	int numdevs;
219 	device_t *devlist;
220 	int tmp;
221 	int err = 0;
222 
223 	device_get_children(cbdev, &devlist, &numdevs);
224 
225 	if (numdevs == 0) {
226 		free(devlist, M_TEMP);
227 		return (ENOENT);
228 	}
229 
230 	for (tmp = 0; tmp < numdevs; tmp++) {
231 		struct cardbus_devinfo *dinfo = device_get_ivars(devlist[tmp]);
232 		int status = device_get_state(devlist[tmp]);
233 
234 		if (dinfo->pci.cfg.dev != devlist[tmp])
235 			device_printf(cbdev, "devinfo dev mismatch\n");
236 		if (status == DS_ATTACHED || status == DS_BUSY)
237 			device_detach(devlist[tmp]);
238 		cardbus_release_all_resources(cbdev, dinfo);
239 		device_delete_child(cbdev, devlist[tmp]);
240 		pci_freecfg((struct pci_devinfo *)dinfo);
241 	}
242 	POWER_DISABLE_SOCKET(device_get_parent(cbdev), cbdev);
243 	free(devlist, M_TEMP);
244 	return (err);
245 }
246 
247 static void
248 cardbus_driver_added(device_t cbdev, driver_t *driver)
249 {
250 	int numdevs;
251 	device_t *devlist;
252 	device_t dev;
253 	int i;
254 	struct cardbus_devinfo *dinfo;
255 
256 	DEVICE_IDENTIFY(driver, cbdev);
257 	device_get_children(cbdev, &devlist, &numdevs);
258 	/*
259 	 * If there are no drivers attached, but there are children,
260 	 * then power the card up.
261 	 */
262 	for (i = 0; i < numdevs; i++) {
263 		dev = devlist[i];
264 		if (device_get_state(dev) != DS_NOTPRESENT)
265 		    break;
266 	}
267 	if (i > 0 && i == numdevs)
268 		POWER_ENABLE_SOCKET(device_get_parent(cbdev), cbdev);
269 	for (i = 0; i < numdevs; i++) {
270 		dev = devlist[i];
271 		if (device_get_state(dev) != DS_NOTPRESENT)
272 			continue;
273 		dinfo = device_get_ivars(dev);
274 		pci_print_verbose(&dinfo->pci);
275 		pci_cfg_restore(dinfo->pci.cfg.dev, &dinfo->pci);
276 		if (device_probe_and_attach(dev) != 0)
277 			pci_cfg_save(dev, &dinfo->pci, 1);
278 	}
279 	free(devlist, M_TEMP);
280 }
281 
282 static void
283 cardbus_release_all_resources(device_t cbdev, struct cardbus_devinfo *dinfo)
284 {
285 	struct resource_list_entry *rle;
286 
287 	/* Free all allocated resources */
288 	STAILQ_FOREACH(rle, &dinfo->pci.resources, link) {
289 		if (rle->res) {
290 			BUS_RELEASE_RESOURCE(device_get_parent(cbdev),
291 			    cbdev, rle->type, rle->rid, rle->res);
292 			rle->res = NULL;
293 			/*
294 			 * zero out config so the card won't acknowledge
295 			 * access to the space anymore. XXX doesn't handle
296 			 * 64-bit bars.
297 			 */
298 			pci_write_config(dinfo->pci.cfg.dev, rle->rid, 0, 4);
299 		}
300 	}
301 	resource_list_free(&dinfo->pci.resources);
302 }
303 
304 /************************************************************************/
305 /* Other Bus Methods							*/
306 /************************************************************************/
307 
308 static int
309 cardbus_read_ivar(device_t cbdev, device_t child, int which, uintptr_t *result)
310 {
311 	struct cardbus_devinfo *dinfo;
312 	pcicfgregs *cfg;
313 
314 	dinfo = device_get_ivars(child);
315 	cfg = &dinfo->pci.cfg;
316 
317 	switch (which) {
318 	case PCI_IVAR_ETHADDR:
319 		/*
320 		 * The generic accessor doesn't deal with failure, so
321 		 * we set the return value, then return an error.
322 		 */
323 		if (dinfo->fepresent & (1 << PCCARD_TPLFE_TYPE_LAN_NID)) {
324 			*((uint8_t **) result) = dinfo->funce.lan.nid;
325 			break;
326 		}
327 		*((uint8_t **) result) = NULL;
328 		return (EINVAL);
329 	default:
330 		return (pci_read_ivar(cbdev, child, which, result));
331 	}
332 	return 0;
333 }
334 
335 static int
336 cardbus_write_ivar(device_t cbdev, device_t child, int which, uintptr_t value)
337 {
338 	return(pci_write_ivar(cbdev, child, which, value));
339 }
340 
341 static device_method_t cardbus_methods[] = {
342 	/* Device interface */
343 	DEVMETHOD(device_probe,		cardbus_probe),
344 	DEVMETHOD(device_attach,	cardbus_attach),
345 	DEVMETHOD(device_detach,	cardbus_detach),
346 	DEVMETHOD(device_suspend,	cardbus_suspend),
347 	DEVMETHOD(device_resume,	cardbus_resume),
348 
349 	/* Bus interface */
350 	DEVMETHOD(bus_read_ivar,	cardbus_read_ivar),
351 	DEVMETHOD(bus_write_ivar,	cardbus_write_ivar),
352 	DEVMETHOD(bus_driver_added,	cardbus_driver_added),
353 
354 	/* Card Interface */
355 	DEVMETHOD(card_attach_card,	cardbus_attach_card),
356 	DEVMETHOD(card_detach_card,	cardbus_detach_card),
357 
358 	{0,0}
359 };
360 
361 DEFINE_CLASS_1(cardbus, cardbus_driver, cardbus_methods,
362     sizeof(struct cardbus_softc), pci_driver);
363 
364 static devclass_t cardbus_devclass;
365 
366 DRIVER_MODULE(cardbus, cbb, cardbus_driver, cardbus_devclass, 0, 0);
367 MODULE_VERSION(cardbus, 1);
368