]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/cardbus/cardbus.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ trunk r321545,
[FreeBSD/FreeBSD.git] / sys / dev / cardbus / cardbus.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2003-2008 M. Warner Losh.  All Rights Reserved.
5  * Copyright (c) 2000,2001 Jonathan Chen.  All rights reserved.
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/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
38
39 #include <sys/bus.h>
40 #include <machine/bus.h>
41 #include <sys/rman.h>
42 #include <machine/resource.h>
43
44 #include <sys/pciio.h>
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/pci_private.h>
48
49 #include <dev/cardbus/cardbusreg.h>
50 #include <dev/cardbus/cardbusvar.h>
51 #include <dev/cardbus/cardbus_cis.h>
52 #include <dev/pccard/pccard_cis.h>
53 #include <dev/pccard/pccardvar.h>
54
55 #include "power_if.h"
56 #include "pcib_if.h"
57
58 /* sysctl vars */
59 static SYSCTL_NODE(_hw, OID_AUTO, cardbus, CTLFLAG_RD, 0, "CardBus parameters");
60
61 int    cardbus_debug = 0;
62 SYSCTL_INT(_hw_cardbus, OID_AUTO, debug, CTLFLAG_RWTUN,
63     &cardbus_debug, 0, "CardBus debug");
64
65 int    cardbus_cis_debug = 0;
66 SYSCTL_INT(_hw_cardbus, OID_AUTO, cis_debug, CTLFLAG_RWTUN,
67     &cardbus_cis_debug, 0, "CardBus CIS debug");
68
69 #define DPRINTF(a) if (cardbus_debug) printf a
70 #define DEVPRINTF(x) if (cardbus_debug) device_printf x
71
72 static int      cardbus_attach(device_t cbdev);
73 static int      cardbus_attach_card(device_t cbdev);
74 static int      cardbus_detach(device_t cbdev);
75 static int      cardbus_detach_card(device_t cbdev);
76 static void     cardbus_device_setup_regs(pcicfgregs *cfg);
77 static void     cardbus_driver_added(device_t cbdev, driver_t *driver);
78 static int      cardbus_probe(device_t cbdev);
79 static int      cardbus_read_ivar(device_t cbdev, device_t child, int which,
80                     uintptr_t *result);
81
82 /************************************************************************/
83 /* Probe/Attach                                                         */
84 /************************************************************************/
85
86 static int
87 cardbus_probe(device_t cbdev)
88 {
89         device_set_desc(cbdev, "CardBus bus");
90         return (0);
91 }
92
93 static int
94 cardbus_attach(device_t cbdev)
95 {
96         struct cardbus_softc *sc;
97 #ifdef PCI_RES_BUS
98         int rid;
99 #endif
100
101         sc = device_get_softc(cbdev);
102         sc->sc_dev = cbdev;
103 #ifdef PCI_RES_BUS
104         rid = 0;
105         sc->sc_bus = bus_alloc_resource(cbdev, PCI_RES_BUS, &rid,
106             pcib_get_bus(cbdev), pcib_get_bus(cbdev), 1, 0);
107         if (sc->sc_bus == NULL) {
108                 device_printf(cbdev, "failed to allocate bus number\n");
109                 return (ENXIO);
110         }
111 #else
112         device_printf(cbdev, "Your bus numbers may be AFU\n");
113 #endif
114         return (0);
115 }
116
117 static int
118 cardbus_detach(device_t cbdev)
119 {
120 #ifdef PCI_RES_BUS
121         struct cardbus_softc *sc;
122 #endif
123
124         cardbus_detach_card(cbdev);
125 #ifdef PCI_RES_BUS
126         sc = device_get_softc(cbdev);
127         device_printf(cbdev, "Freeing up the allocatd bus\n");
128         (void)bus_release_resource(cbdev, PCI_RES_BUS, 0, sc->sc_bus);
129 #endif
130         return (0);
131 }
132
133 static int
134 cardbus_suspend(device_t self)
135 {
136
137         cardbus_detach_card(self);
138         return (0);
139 }
140
141 static int
142 cardbus_resume(device_t self)
143 {
144
145         return (0);
146 }
147
148 /************************************************************************/
149 /* Attach/Detach card                                                   */
150 /************************************************************************/
151
152 static void
153 cardbus_device_setup_regs(pcicfgregs *cfg)
154 {
155         device_t dev = cfg->dev;
156         int i;
157
158         /*
159          * Some cards power up with garbage in their BARs.  This
160          * code clears all that junk out.
161          */
162         for (i = 0; i < PCIR_MAX_BAR_0; i++)
163                 pci_write_config(dev, PCIR_BAR(i), 0, 4);
164
165         cfg->intline =
166             pci_get_irq(device_get_parent(device_get_parent(dev)));
167         pci_write_config(dev, PCIR_INTLINE, cfg->intline, 1);
168         pci_write_config(dev, PCIR_CACHELNSZ, 0x08, 1);
169         pci_write_config(dev, PCIR_LATTIMER, 0xa8, 1);
170         pci_write_config(dev, PCIR_MINGNT, 0x14, 1);
171         pci_write_config(dev, PCIR_MAXLAT, 0x14, 1);
172 }
173
174 static struct pci_devinfo *
175 cardbus_alloc_devinfo(device_t dev)
176 {
177         struct cardbus_devinfo *dinfo;
178
179         dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
180         return (&dinfo->pci);
181 }
182
183 static int
184 cardbus_attach_card(device_t cbdev)
185 {
186         device_t brdev = device_get_parent(cbdev);
187         device_t child;
188         int bus, domain, slot, func;
189         int cardattached = 0;
190         int cardbusfunchigh = 0;
191         struct cardbus_softc *sc;
192
193         sc = device_get_softc(cbdev);
194         cardbus_detach_card(cbdev); /* detach existing cards */
195         POWER_DISABLE_SOCKET(brdev, cbdev); /* Turn the socket off first */
196         POWER_ENABLE_SOCKET(brdev, cbdev);
197         domain = pcib_get_domain(cbdev);
198         bus = pcib_get_bus(cbdev);
199         slot = 0;
200         /* For each function, set it up and try to attach a driver to it */
201         for (func = 0; func <= cardbusfunchigh; func++) {
202                 struct cardbus_devinfo *dinfo;
203
204                 dinfo = (struct cardbus_devinfo *)
205                     pci_read_device(brdev, cbdev, domain, bus, slot, func);
206                 if (dinfo == NULL)
207                         continue;
208                 if (dinfo->pci.cfg.mfdev)
209                         cardbusfunchigh = PCI_FUNCMAX;
210
211                 child = device_add_child(cbdev, NULL, -1);
212                 if (child == NULL) {
213                         DEVPRINTF((cbdev, "Cannot add child!\n"));
214                         pci_freecfg((struct pci_devinfo *)dinfo);
215                         continue;
216                 }
217                 dinfo->pci.cfg.dev = child;
218                 resource_list_init(&dinfo->pci.resources);
219                 device_set_ivars(child, dinfo);
220                 cardbus_device_create(sc, dinfo, cbdev, child);
221                 if (cardbus_do_cis(cbdev, child) != 0)
222                         DEVPRINTF((cbdev, "Warning: Bogus CIS ignored\n"));
223                 pci_cfg_save(dinfo->pci.cfg.dev, &dinfo->pci, 0);
224                 pci_cfg_restore(dinfo->pci.cfg.dev, &dinfo->pci);
225                 cardbus_device_setup_regs(&dinfo->pci.cfg);
226                 pci_add_resources(cbdev, child, 1, dinfo->mprefetchable);
227                 pci_print_verbose(&dinfo->pci);
228                 if (device_probe_and_attach(child) == 0)
229                         cardattached++;
230                 else
231                         pci_cfg_save(dinfo->pci.cfg.dev, &dinfo->pci, 1);
232         }
233         if (cardattached > 0)
234                 return (0);
235 /*      POWER_DISABLE_SOCKET(brdev, cbdev); */
236         return (ENOENT);
237 }
238
239 static void
240 cardbus_child_deleted(device_t cbdev, device_t child)
241 {
242         struct cardbus_devinfo *dinfo = device_get_ivars(child);
243
244         if (dinfo->pci.cfg.dev != child)
245                 device_printf(cbdev, "devinfo dev mismatch\n");
246         cardbus_device_destroy(dinfo);
247         pci_child_deleted(cbdev, child);
248 }
249
250 static int
251 cardbus_detach_card(device_t cbdev)
252 {
253         int err = 0;
254
255         err = bus_generic_detach(cbdev);
256         if (err)
257                 return (err);
258         err = device_delete_children(cbdev);
259         if (err)
260                 return (err);
261
262         POWER_DISABLE_SOCKET(device_get_parent(cbdev), cbdev);
263         return (err);
264 }
265
266 static void
267 cardbus_driver_added(device_t cbdev, driver_t *driver)
268 {
269         int numdevs;
270         device_t *devlist;
271         device_t dev;
272         int i;
273         struct cardbus_devinfo *dinfo;
274
275         DEVICE_IDENTIFY(driver, cbdev);
276         if (device_get_children(cbdev, &devlist, &numdevs) != 0)
277                 return;
278
279         /*
280          * If there are no drivers attached, but there are children,
281          * then power the card up.
282          */
283         for (i = 0; i < numdevs; i++) {
284                 dev = devlist[i];
285                 if (device_get_state(dev) != DS_NOTPRESENT)
286                     break;
287         }
288         if (i > 0 && i == numdevs)
289                 POWER_ENABLE_SOCKET(device_get_parent(cbdev), cbdev);
290         for (i = 0; i < numdevs; i++) {
291                 dev = devlist[i];
292                 if (device_get_state(dev) != DS_NOTPRESENT)
293                         continue;
294                 dinfo = device_get_ivars(dev);
295                 pci_print_verbose(&dinfo->pci);
296                 if (bootverbose)
297                         printf("pci%d:%d:%d:%d: reprobing on driver added\n",
298                             dinfo->pci.cfg.domain, dinfo->pci.cfg.bus,
299                             dinfo->pci.cfg.slot, dinfo->pci.cfg.func);
300                 pci_cfg_restore(dinfo->pci.cfg.dev, &dinfo->pci);
301                 if (device_probe_and_attach(dev) != 0)
302                         pci_cfg_save(dev, &dinfo->pci, 1);
303         }
304         free(devlist, M_TEMP);
305 }
306
307 /************************************************************************/
308 /* Other Bus Methods                                                    */
309 /************************************************************************/
310
311 static int
312 cardbus_read_ivar(device_t cbdev, device_t child, int which, uintptr_t *result)
313 {
314         struct cardbus_devinfo *dinfo;
315         pcicfgregs *cfg;
316
317         dinfo = device_get_ivars(child);
318         cfg = &dinfo->pci.cfg;
319
320         switch (which) {
321         case PCI_IVAR_ETHADDR:
322                 /*
323                  * The generic accessor doesn't deal with failure, so
324                  * we set the return value, then return an error.
325                  */
326                 if (dinfo->fepresent & (1 << PCCARD_TPLFE_TYPE_LAN_NID)) {
327                         *((uint8_t **) result) = dinfo->funce.lan.nid;
328                         break;
329                 }
330                 *((uint8_t **) result) = NULL;
331                 return (EINVAL);
332         default:
333                 return (pci_read_ivar(cbdev, child, which, result));
334         }
335         return 0;
336 }
337
338 static device_method_t cardbus_methods[] = {
339         /* Device interface */
340         DEVMETHOD(device_probe,         cardbus_probe),
341         DEVMETHOD(device_attach,        cardbus_attach),
342         DEVMETHOD(device_detach,        cardbus_detach),
343         DEVMETHOD(device_suspend,       cardbus_suspend),
344         DEVMETHOD(device_resume,        cardbus_resume),
345
346         /* Bus interface */
347         DEVMETHOD(bus_child_deleted,    cardbus_child_deleted),
348         DEVMETHOD(bus_get_dma_tag,      bus_generic_get_dma_tag),
349         DEVMETHOD(bus_read_ivar,        cardbus_read_ivar),
350         DEVMETHOD(bus_driver_added,     cardbus_driver_added),
351         DEVMETHOD(bus_rescan,           bus_null_rescan),
352
353         /* Card Interface */
354         DEVMETHOD(card_attach_card,     cardbus_attach_card),
355         DEVMETHOD(card_detach_card,     cardbus_detach_card),
356
357         /* PCI interface */
358         DEVMETHOD(pci_alloc_devinfo,    cardbus_alloc_devinfo),
359
360         {0,0}
361 };
362
363 DEFINE_CLASS_1(cardbus, cardbus_driver, cardbus_methods,
364     sizeof(struct cardbus_softc), pci_driver);
365
366 static devclass_t cardbus_devclass;
367
368 DRIVER_MODULE(cardbus, cbb, cardbus_driver, cardbus_devclass, 0, 0);
369 MODULE_VERSION(cardbus, 1);