2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * @file bcmsdh_linux.c
21 #define __UNDEF_NO_VERSION__
25 #include <linux/pci.h>
26 #include <linux/completion.h>
33 #if defined(OOB_INTR_ONLY)
34 #include <linux/irq.h>
35 extern void dhdsdio_isr(void *args);
37 #include <dngl_stats.h>
39 #endif /* defined(OOB_INTR_ONLY) */
40 #if defined(CONFIG_MACH_SANDGATE2G) || defined(CONFIG_MACH_LOGICPD_PXA270)
41 #if !defined(BCMPLATFORM_BUS)
42 #define BCMPLATFORM_BUS
43 #endif /* !defined(BCMPLATFORM_BUS) */
45 #include <linux/platform_device.h>
46 #endif /* CONFIG_MACH_SANDGATE2G */
49 * SDIO Host Controller info
51 typedef struct bcmsdh_hc bcmsdh_hc_t;
55 #ifdef BCMPLATFORM_BUS
56 struct device *dev; /* platform device handle */
58 struct pci_dev *dev; /* pci device handle */
59 #endif /* BCMPLATFORM_BUS */
61 void *regs; /* SDIO Host Controller address */
62 bcmsdh_info_t *sdh; /* SDIO Host Controller handle */
65 unsigned long oob_flags; /* OOB Host specifiction
67 bool oob_irq_registered;
68 #if defined(OOB_INTR_ONLY)
72 static bcmsdh_hc_t *sdhcinfo;
74 /* driver info, initialized when bcmsdh_register is called */
75 static bcmsdh_driver_t drvinfo = { NULL, NULL };
77 /* debugging macros */
81 * Checks to see if vendor and device IDs match a supported SDIO Host Controller.
83 bool bcmsdh_chipmatch(u16 vendor, u16 device)
85 /* Add other vendors and devices as required */
88 /* Check for Arasan host controller */
89 if (vendor == VENDOR_SI_IMAGE)
92 /* Check for BRCM 27XX Standard host controller */
93 if (device == BCM27XX_SDIOH_ID && vendor == VENDOR_BROADCOM)
96 /* Check for BRCM Standard host controller */
97 if (device == SDIOH_FPGA_ID && vendor == VENDOR_BROADCOM)
100 /* Check for TI PCIxx21 Standard host controller */
101 if (device == PCIXX21_SDIOH_ID && vendor == VENDOR_TI)
104 if (device == PCIXX21_SDIOH0_ID && vendor == VENDOR_TI)
107 /* Ricoh R5C822 Standard SDIO Host */
108 if (device == R5C822_SDIOH_ID && vendor == VENDOR_RICOH)
111 /* JMicron Standard SDIO Host */
112 if (device == JMICRON_SDIOH_ID && vendor == VENDOR_JMICRON)
114 #endif /* BCMSDIOH_STD */
116 /* This is the PciSpiHost. */
117 if (device == SPIH_FPGA_ID && vendor == VENDOR_BROADCOM) {
118 printf("Found PCI SPI Host Controller\n");
121 #endif /* BCMSDIOH_SPI */
126 #if defined(BCMPLATFORM_BUS)
127 #if defined(BCMLXSDMMC)
128 /* forward declarations */
129 int bcmsdh_probe(struct device *dev);
130 EXPORT_SYMBOL(bcmsdh_probe);
132 int bcmsdh_remove(struct device *dev);
133 EXPORT_SYMBOL(bcmsdh_remove);
136 /* forward declarations */
137 static int __devinit bcmsdh_probe(struct device *dev);
138 static int __devexit bcmsdh_remove(struct device *dev);
139 #endif /* BCMLXSDMMC */
142 static struct device_driver bcmsdh_driver = {
143 .name = "pxa2xx-mci",
144 .bus = &platform_bus_type,
145 .probe = bcmsdh_probe,
146 .remove = bcmsdh_remove,
150 #endif /* BCMLXSDMMC */
154 #endif /* BCMLXSDMMC */
155 int bcmsdh_probe(struct device *dev)
158 bcmsdh_hc_t *sdhc = NULL;
159 unsigned long regs = 0;
160 bcmsdh_info_t *sdh = NULL;
161 #if !defined(BCMLXSDMMC) && defined(BCMPLATFORM_BUS)
162 struct platform_device *pdev;
164 #endif /* BCMLXSDMMC */
167 unsigned long irq_flags = 0;
169 #if !defined(BCMLXSDMMC) && defined(BCMPLATFORM_BUS)
170 pdev = to_platform_device(dev);
171 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
172 irq = platform_get_irq(pdev, 0);
173 if (!r || irq == NO_IRQ)
175 #endif /* BCMLXSDMMC */
177 #if defined(OOB_INTR_ONLY)
180 IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL |
181 IORESOURCE_IRQ_SHAREABLE;
183 irq_flags = IRQF_TRIGGER_FALLING;
185 irq = dhd_customer_oob_irq_map(&irq_flags);
187 SDLX_MSG(("%s: Host irq is not defined\n", __func__));
190 #endif /* defined(OOB_INTR_ONLY) */
191 /* allocate SDIO Host Controller state info */
192 osh = osl_attach(dev, PCI_BUS, false);
194 SDLX_MSG(("%s: osl_attach failed\n", __func__));
197 sdhc = kzalloc(sizeof(bcmsdh_hc_t), GFP_ATOMIC);
199 SDLX_MSG(("%s: out of memory\n", __func__));
204 sdhc->dev = (void *)dev;
207 sdh = bcmsdh_attach(osh, (void *)0, (void **)®s, irq);
209 SDLX_MSG(("%s: bcmsdh_attach failed\n", __func__));
213 sdh = bcmsdh_attach(osh, (void *)r->start, (void **)®s, irq);
215 SDLX_MSG(("%s: bcmsdh_attach failed\n", __func__));
218 #endif /* BCMLXSDMMC */
221 sdhc->oob_flags = irq_flags;
222 sdhc->oob_irq_registered = false; /* to make sure.. */
223 #if defined(OOB_INTR_ONLY)
224 spin_lock_init(&sdhc->irq_lock);
227 /* chain SDIO Host Controller info together */
228 sdhc->next = sdhcinfo;
230 /* Read the vendor/device ID from the CIS */
231 vendevid = bcmsdh_query_device(sdh);
233 /* try to attach to the target device */
234 sdhc->ch = drvinfo.attach((vendevid >> 16), (vendevid & 0xFFFF),
235 0, 0, 0, 0, (void *)regs, NULL, sdh);
237 SDLX_MSG(("%s: device attach failed\n", __func__));
247 bcmsdh_detach(sdhc->osh, sdhc->sdh);
257 #endif /* BCMLXSDMMC */
258 int bcmsdh_remove(struct device *dev)
260 bcmsdh_hc_t *sdhc, *prev;
264 drvinfo.detach(sdhc->ch);
265 bcmsdh_detach(sdhc->osh, sdhc->sdh);
266 /* find the SDIO Host Controller state for this pdev
267 and take it out from the list */
268 for (sdhc = sdhcinfo, prev = NULL; sdhc; sdhc = sdhc->next) {
269 if (sdhc->dev == (void *)dev) {
271 prev->next = sdhc->next;
279 SDLX_MSG(("%s: failed\n", __func__));
283 /* release SDIO Host Controller info */
288 #if !defined(BCMLXSDMMC)
289 dev_set_drvdata(dev, NULL);
290 #endif /* !defined(BCMLXSDMMC) */
295 #else /* BCMPLATFORM_BUS */
297 #if !defined(BCMLXSDMMC)
298 /* forward declarations for PCI probe and remove functions. */
299 static int __devinit bcmsdh_pci_probe(struct pci_dev *pdev,
300 const struct pci_device_id *ent);
301 static void __devexit bcmsdh_pci_remove(struct pci_dev *pdev);
306 static struct pci_device_id bcmsdh_pci_devid[] __devinitdata = {
308 .vendor = PCI_ANY_ID,
309 .device = PCI_ANY_ID,
310 .subvendor = PCI_ANY_ID,
311 .subdevice = PCI_ANY_ID,
319 MODULE_DEVICE_TABLE(pci, bcmsdh_pci_devid);
322 * SDIO Host Controller pci driver info
324 static struct pci_driver bcmsdh_pci_driver = {
327 .id_table = bcmsdh_pci_devid,
328 .probe = bcmsdh_pci_probe,
329 .remove = bcmsdh_pci_remove,
334 extern uint sd_pci_slot; /* Force detection to a particular PCI */
335 /* slot only . Allows for having multiple */
336 /* WL devices at once in a PC */
337 /* Only one instance of dhd will be */
338 /* usable at a time */
339 /* Upper word is bus number, */
340 /* lower word is slot number */
341 /* Default value of 0xFFFFffff turns this */
343 module_param(sd_pci_slot, uint, 0);
346 * Detect supported SDIO Host Controller and attach if found.
348 * Determine if the device described by pdev is a supported SDIO Host
349 * Controller. If so, attach to it and attach to the target device.
352 bcmsdh_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
355 bcmsdh_hc_t *sdhc = NULL;
357 bcmsdh_info_t *sdh = NULL;
360 if (sd_pci_slot != 0xFFFFffff) {
361 if (pdev->bus->number != (sd_pci_slot >> 16) ||
362 PCI_SLOT(pdev->devfn) != (sd_pci_slot & 0xffff)) {
363 SDLX_MSG(("%s: %s: bus %X, slot %X, vend %X, dev %X\n",
365 bcmsdh_chipmatch(pdev->vendor, pdev->device) ?
366 "Found compatible SDIOHC" :
367 "Probing unknown device",
368 pdev->bus->number, PCI_SLOT(pdev->devfn),
369 pdev->vendor, pdev->device));
372 SDLX_MSG(("%s: %s: bus %X, slot %X, vendor %X, device %X "
373 "(good PCI location)\n", __func__,
374 bcmsdh_chipmatch(pdev->vendor, pdev->device) ?
375 "Using compatible SDIOHC" : "WARNING, forced use "
377 pdev->bus->number, PCI_SLOT(pdev->devfn), pdev->vendor,
381 if ((pdev->vendor == VENDOR_TI)
382 && ((pdev->device == PCIXX21_FLASHMEDIA_ID)
383 || (pdev->device == PCIXX21_FLASHMEDIA0_ID))) {
386 SDLX_MSG(("%s: Disabling TI FlashMedia Controller.\n",
388 osh = osl_attach(pdev, PCI_BUS, false);
390 SDLX_MSG(("%s: osl_attach failed\n", __func__));
394 config_reg = OSL_PCI_READ_CONFIG(osh, 0x4c, 4);
397 * Set MMC_SD_DIS bit in FlashMedia Controller.
398 * Disbling the SD/MMC Controller in the FlashMedia Controller
399 * allows the Standard SD Host Controller to take over control
403 OSL_PCI_WRITE_CONFIG(osh, 0x4c, 4, config_reg);
406 /* match this pci device with what we support */
407 /* we can't solely rely on this to believe it is
408 our SDIO Host Controller! */
409 if (!bcmsdh_chipmatch(pdev->vendor, pdev->device))
412 /* this is a pci device we might support */
413 SDLX_MSG(("%s: Found possible SDIO Host Controller: "
414 "bus %d slot %d func %d irq %d\n", __func__,
415 pdev->bus->number, PCI_SLOT(pdev->devfn),
416 PCI_FUNC(pdev->devfn), pdev->irq));
418 /* use bcmsdh_query_device() to get the vendor ID of the target device
419 * so it will eventually appear in the Broadcom string on the console
422 /* allocate SDIO Host Controller state info */
423 osh = osl_attach(pdev, PCI_BUS, false);
425 SDLX_MSG(("%s: osl_attach failed\n", __func__));
428 sdhc = kzalloc(sizeof(bcmsdh_hc_t), GFP_ATOMIC);
430 SDLX_MSG(("%s: out of memory\n", __func__));
437 /* map to address where host can access */
438 pci_set_master(pdev);
439 rc = pci_enable_device(pdev);
441 SDLX_MSG(("%s: Cannot enable PCI device\n", __func__));
444 sdh = bcmsdh_attach(osh, (void *)(unsigned long)pci_resource_start(pdev, 0),
445 (void **)®s, pdev->irq);
447 SDLX_MSG(("%s: bcmsdh_attach failed\n", __func__));
453 /* try to attach to the target device */
454 sdhc->ch = drvinfo.attach(VENDOR_BROADCOM, /* pdev->vendor, */
455 bcmsdh_query_device(sdh) & 0xFFFF, 0, 0, 0, 0,
456 (void *)regs, NULL, sdh);
458 SDLX_MSG(("%s: device attach failed\n", __func__));
462 /* chain SDIO Host Controller info together */
463 sdhc->next = sdhcinfo;
471 bcmsdh_detach(sdhc->osh, sdhc->sdh);
480 * Detach from target devices and SDIO Host Controller
482 static void __devexit bcmsdh_pci_remove(struct pci_dev *pdev)
484 bcmsdh_hc_t *sdhc, *prev;
487 /* find the SDIO Host Controller state for this
488 pdev and take it out from the list */
489 for (sdhc = sdhcinfo, prev = NULL; sdhc; sdhc = sdhc->next) {
490 if (sdhc->dev == pdev) {
492 prev->next = sdhc->next;
502 drvinfo.detach(sdhc->ch);
504 bcmsdh_detach(sdhc->osh, sdhc->sdh);
506 /* release SDIO Host Controller info */
511 #endif /* BCMLXSDMMC */
512 #endif /* BCMPLATFORM_BUS */
514 extern int sdio_function_init(void);
516 int bcmsdh_register(bcmsdh_driver_t *driver)
522 #if defined(BCMPLATFORM_BUS)
523 #if defined(BCMLXSDMMC)
524 SDLX_MSG(("Linux Kernel SDIO/MMC Driver\n"));
525 error = sdio_function_init();
527 SDLX_MSG(("Intel PXA270 SDIO Driver\n"));
528 error = driver_register(&bcmsdh_driver);
529 #endif /* defined(BCMLXSDMMC) */
531 #endif /* defined(BCMPLATFORM_BUS) */
533 #if !defined(BCMPLATFORM_BUS) && !defined(BCMLXSDMMC)
534 error = pci_register_driver(&bcmsdh_pci_driver);
538 SDLX_MSG(("%s: pci_register_driver failed 0x%x\n", __func__, error));
539 #endif /* BCMPLATFORM_BUS */
544 extern void sdio_function_cleanup(void);
546 void bcmsdh_unregister(void)
548 #if defined(BCMPLATFORM_BUS) && !defined(BCMLXSDMMC)
549 driver_unregister(&bcmsdh_driver);
551 #if defined(BCMLXSDMMC)
552 sdio_function_cleanup();
553 #endif /* BCMLXSDMMC */
554 #if !defined(BCMPLATFORM_BUS) && !defined(BCMLXSDMMC)
555 pci_unregister_driver(&bcmsdh_pci_driver);
556 #endif /* BCMPLATFORM_BUS */
559 #if defined(OOB_INTR_ONLY)
560 void bcmsdh_oob_intr_set(bool enable)
562 static bool curstate = 1;
565 spin_lock_irqsave(&sdhcinfo->irq_lock, flags);
566 if (curstate != enable) {
568 enable_irq(sdhcinfo->oob_irq);
570 disable_irq_nosync(sdhcinfo->oob_irq);
573 spin_unlock_irqrestore(&sdhcinfo->irq_lock, flags);
576 static irqreturn_t wlan_oob_irq(int irq, void *dev_id)
580 dhdp = (dhd_pub_t *) dev_get_drvdata(sdhcinfo->dev);
582 bcmsdh_oob_intr_set(0);
585 SDLX_MSG(("Out of band GPIO interrupt fired way too early\n"));
589 WAKE_LOCK_TIMEOUT(dhdp, WAKE_LOCK_TMOUT, 25);
591 dhdsdio_isr((void *)dhdp->bus);
596 int bcmsdh_register_oob_intr(void *dhdp)
600 SDLX_MSG(("%s Enter\n", __func__));
602 sdhcinfo->oob_flags =
603 IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL |
604 IORESOURCE_IRQ_SHAREABLE;
605 dev_set_drvdata(sdhcinfo->dev, dhdp);
607 if (!sdhcinfo->oob_irq_registered) {
608 SDLX_MSG(("%s IRQ=%d Type=%X\n", __func__,
609 (int)sdhcinfo->oob_irq, (int)sdhcinfo->oob_flags));
610 /* Refer to customer Host IRQ docs about
611 proper irqflags definition */
613 request_irq(sdhcinfo->oob_irq, wlan_oob_irq,
614 sdhcinfo->oob_flags, "bcmsdh_sdmmc", NULL);
618 set_irq_wake(sdhcinfo->oob_irq, 1);
619 sdhcinfo->oob_irq_registered = true;
625 void bcmsdh_unregister_oob_intr(void)
627 SDLX_MSG(("%s: Enter\n", __func__));
629 set_irq_wake(sdhcinfo->oob_irq, 0);
630 disable_irq(sdhcinfo->oob_irq); /* just in case.. */
631 free_irq(sdhcinfo->oob_irq, NULL);
632 sdhcinfo->oob_irq_registered = false;
634 #endif /* defined(OOB_INTR_ONLY) */
635 /* Module parameters specific to each host-controller driver */
637 extern uint sd_msglevel; /* Debug message level */
638 module_param(sd_msglevel, uint, 0);
640 extern uint sd_power; /* 0 = SD Power OFF,
642 module_param(sd_power, uint, 0);
644 extern uint sd_clock; /* SD Clock Control, 0 = SD Clock OFF,
646 module_param(sd_clock, uint, 0);
648 extern uint sd_divisor; /* Divisor (-1 means external clock) */
649 module_param(sd_divisor, uint, 0);
651 extern uint sd_sdmode; /* Default is SD4, 0=SPI, 1=SD1, 2=SD4 */
652 module_param(sd_sdmode, uint, 0);
654 extern uint sd_hiok; /* Ok to use hi-speed mode */
655 module_param(sd_hiok, uint, 0);
657 extern uint sd_f2_blocksize;
658 module_param(sd_f2_blocksize, int, 0);