2 * SDIO access interface for drivers - linux specific (pci only)
4 * Copyright (C) 1999-2011, Broadcom Corporation
6 * Unless you and Broadcom execute a separate written software license
7 * agreement governing use of this software, this software is licensed to you
8 * under the terms of the GNU General Public License version 2 (the "GPL"),
9 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10 * following added to such license:
12 * As a special exception, the copyright holders of this software give you
13 * permission to link this software with independent modules, and to copy and
14 * distribute the resulting executable under terms of your choice, provided that
15 * you also meet, for each linked independent module, the terms and conditions of
16 * the license of that module. An independent module is a module which is not
17 * derived from this software. The special exception does not apply to any
18 * modifications of the software.
20 * Notwithstanding the above, under no circumstances may you combine this
21 * software in any way with any other Broadcom software provided under a license
22 * other than the GPL, without Broadcom's express prior written consent.
24 * $Id: bcmsdh_linux.c 294990 2011-11-09 00:13:10Z $
28 * @file bcmsdh_linux.c
31 #define __UNDEF_NO_VERSION__
36 #include <linux/pci.h>
37 #include <linux/completion.h>
44 #if defined(OOB_INTR_ONLY)
45 #include <linux/irq.h>
46 extern void dhdsdio_isr(void * args);
48 #include <dngl_stats.h>
50 #endif /* defined(OOB_INTR_ONLY) */
52 #if defined(CONFIG_PM_SLEEP) && defined(CUSTOMER_HW_SLP)
53 /*SLP_wakelock_alternative_code*/
54 struct device *pm_dev;
55 #endif /* CONFIG_PM_SLEEP && CUSTOMER_HW_SLP */
57 * SDIO Host Controller info
59 typedef struct bcmsdh_hc bcmsdh_hc_t;
63 #ifdef BCMPLATFORM_BUS
64 struct device *dev; /* platform device handle */
66 struct pci_dev *dev; /* pci device handle */
67 #endif /* BCMPLATFORM_BUS */
69 void *regs; /* SDIO Host Controller address */
70 bcmsdh_info_t *sdh; /* SDIO Host Controller handle */
73 unsigned long oob_flags; /* OOB Host specifiction as edge and etc */
74 bool oob_irq_registered;
75 bool oob_irq_enable_flag;
76 #if defined(OOB_INTR_ONLY)
80 static bcmsdh_hc_t *sdhcinfo = NULL;
82 /* driver info, initialized when bcmsdh_register is called */
83 static bcmsdh_driver_t drvinfo = {NULL, NULL};
85 /* debugging macros */
89 * Checks to see if vendor and device IDs match a supported SDIO Host Controller.
92 bcmsdh_chipmatch(uint16 vendor, uint16 device)
94 /* Add other vendors and devices as required */
97 /* Check for Arasan host controller */
98 if (vendor == VENDOR_SI_IMAGE) {
101 /* Check for BRCM 27XX Standard host controller */
102 if (device == BCM27XX_SDIOH_ID && vendor == VENDOR_BROADCOM) {
105 /* Check for BRCM Standard host controller */
106 if (device == SDIOH_FPGA_ID && vendor == VENDOR_BROADCOM) {
109 /* Check for TI PCIxx21 Standard host controller */
110 if (device == PCIXX21_SDIOH_ID && vendor == VENDOR_TI) {
113 if (device == PCIXX21_SDIOH0_ID && vendor == VENDOR_TI) {
116 /* Ricoh R5C822 Standard SDIO Host */
117 if (device == R5C822_SDIOH_ID && vendor == VENDOR_RICOH) {
120 /* JMicron Standard SDIO Host */
121 if (device == JMICRON_SDIOH_ID && vendor == VENDOR_JMICRON) {
125 #endif /* BCMSDIOH_STD */
127 /* This is the PciSpiHost. */
128 if (device == SPIH_FPGA_ID && vendor == VENDOR_BROADCOM) {
129 printf("Found PCI SPI Host Controller\n");
133 #endif /* BCMSDIOH_SPI */
138 #if defined(BCMPLATFORM_BUS)
139 #if defined(BCMLXSDMMC)
140 /* forward declarations */
141 int bcmsdh_probe(struct device *dev);
142 int bcmsdh_remove(struct device *dev);
144 EXPORT_SYMBOL(bcmsdh_probe);
145 EXPORT_SYMBOL(bcmsdh_remove);
148 /* forward declarations */
149 static int __devinit bcmsdh_probe(struct device *dev);
150 static int __devexit bcmsdh_remove(struct device *dev);
151 #endif /* BCMLXSDMMC */
155 #endif /* BCMLXSDMMC */
156 int bcmsdh_probe(struct device *dev)
159 bcmsdh_hc_t *sdhc = NULL;
161 bcmsdh_info_t *sdh = NULL;
162 #if !defined(BCMLXSDMMC) && defined(BCMPLATFORM_BUS)
163 struct platform_device *pdev;
165 #endif /* BCMLXSDMMC */
168 unsigned long irq_flags = 0;
169 #if defined(CONFIG_PM_SLEEP) && defined(CUSTOMER_HW_SLP)
171 #endif /* CONFIG_PM_SLEEP && CUSTOMER_HW_SLP */
173 #if !defined(BCMLXSDMMC) && defined(BCMPLATFORM_BUS)
174 pdev = to_platform_device(dev);
175 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
176 irq = platform_get_irq(pdev, 0);
177 if (!r || irq == NO_IRQ)
179 #endif /* BCMLXSDMMC */
181 #if defined(OOB_INTR_ONLY)
184 IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL | IORESOURCE_IRQ_SHAREABLE;
186 irq_flags = IRQF_TRIGGER_FALLING;
189 /* Get customer specific OOB IRQ parametres: IRQ number as IRQ type */
190 irq = dhd_customer_oob_irq_map(&irq_flags);
192 /* Do not disable this IRQ during suspend */
193 irq_flags |= IRQF_NO_SUSPEND;
196 SDLX_MSG(("%s: Host irq is not defined\n", __FUNCTION__));
199 #endif /* defined(OOB_INTR_ONLY) */
200 /* allocate SDIO Host Controller state info */
201 if (!(osh = osl_attach(dev, PCI_BUS, FALSE))) {
202 SDLX_MSG(("%s: osl_attach failed\n", __FUNCTION__));
205 if (!(sdhc = MALLOC(osh, sizeof(bcmsdh_hc_t)))) {
206 SDLX_MSG(("%s: out of memory, allocated %d bytes\n",
211 bzero(sdhc, sizeof(bcmsdh_hc_t));
214 sdhc->dev = (void *)dev;
217 if (!(sdh = bcmsdh_attach(osh, (void *)0,
218 (void **)®s, irq))) {
219 SDLX_MSG(("%s: bcmsdh_attach failed\n", __FUNCTION__));
223 if (!(sdh = bcmsdh_attach(osh, (void *)r->start,
224 (void **)®s, irq))) {
225 SDLX_MSG(("%s: bcmsdh_attach failed\n", __FUNCTION__));
228 #endif /* BCMLXSDMMC */
231 sdhc->oob_flags = irq_flags;
232 sdhc->oob_irq_registered = FALSE; /* to make sure.. */
233 sdhc->oob_irq_enable_flag = FALSE;
234 #if defined(OOB_INTR_ONLY)
235 spin_lock_init(&sdhc->irq_lock);
238 /* chain SDIO Host Controller info together */
239 sdhc->next = sdhcinfo;
242 #if defined(CONFIG_PM_SLEEP) && defined(CUSTOMER_HW_SLP)
243 /*SLP_wakelock_alternative_code*/
245 #ifdef CONFIG_SLP_WAKEUP_COUNT
246 device_init_wakeup_setirq(pm_dev, sdhc->oob_irq);
248 ret = device_init_wakeup(pm_dev, 1);
249 printf("%s : device_init_wakeup(pm_dev) enable, ret = %d\n", __func__, ret);
251 #endif /* CONFIG_PM_SLEEP && CUSTOMER_HW_SLP */
252 /* Read the vendor/device ID from the CIS */
253 vendevid = bcmsdh_query_device(sdh);
254 /* try to attach to the target device */
255 if (!(sdhc->ch = drvinfo.attach((vendevid >> 16),
256 (vendevid & 0xFFFF), 0, 0, 0, 0,
257 (void *)regs, NULL, sdh))) {
258 SDLX_MSG(("%s: device attach failed\n", __FUNCTION__));
268 bcmsdh_detach(sdhc->osh, sdhc->sdh);
269 MFREE(osh, sdhc, sizeof(bcmsdh_hc_t));
278 #endif /* BCMLXSDMMC */
279 int bcmsdh_remove(struct device *dev)
281 bcmsdh_hc_t *sdhc, *prev;
285 #if defined(CONFIG_PM_SLEEP) && defined(CUSTOMER_HW_SLP)
286 /*SLP_wakelock_alternative_code*/
287 device_init_wakeup(pm_dev, 0);
288 printf("%s : device_init_wakeup(pm_dev) disable\n", __func__);
289 #endif /* CONFIG_PM_SLEEP && CUSTOMER_HW_SLP */
290 drvinfo.detach(sdhc->ch);
291 bcmsdh_detach(sdhc->osh, sdhc->sdh);
293 /* find the SDIO Host Controller state for this pdev and take it out from the list */
294 for (sdhc = sdhcinfo, prev = NULL; sdhc; sdhc = sdhc->next) {
295 if (sdhc->dev == (void *)dev) {
297 prev->next = sdhc->next;
305 SDLX_MSG(("%s: failed\n", __FUNCTION__));
309 /* release SDIO Host Controller info */
311 MFREE(osh, sdhc, sizeof(bcmsdh_hc_t));
314 #if !defined(BCMLXSDMMC) || defined(OOB_INTR_ONLY)
315 dev_set_drvdata(dev, NULL);
316 #endif /* !defined(BCMLXSDMMC) || defined(OOB_INTR_ONLY) */
321 #else /* BCMPLATFORM_BUS */
323 #if !defined(BCMLXSDMMC)
324 /* forward declarations for PCI probe and remove functions. */
325 static int __devinit bcmsdh_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
326 static void __devexit bcmsdh_pci_remove(struct pci_dev *pdev);
331 static struct pci_device_id bcmsdh_pci_devid[] __devinitdata = {
332 { vendor: PCI_ANY_ID,
334 subvendor: PCI_ANY_ID,
335 subdevice: PCI_ANY_ID,
342 MODULE_DEVICE_TABLE(pci, bcmsdh_pci_devid);
345 * SDIO Host Controller pci driver info
347 static struct pci_driver bcmsdh_pci_driver = {
350 id_table: bcmsdh_pci_devid,
351 probe: bcmsdh_pci_probe,
352 remove: bcmsdh_pci_remove,
353 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
361 extern uint sd_pci_slot; /* Force detection to a particular PCI */
362 /* slot only . Allows for having multiple */
363 /* WL devices at once in a PC */
364 /* Only one instance of dhd will be */
365 /* usable at a time */
366 /* Upper word is bus number, */
367 /* lower word is slot number */
368 /* Default value of 0xffffffff turns this */
370 module_param(sd_pci_slot, uint, 0);
374 * Detect supported SDIO Host Controller and attach if found.
376 * Determine if the device described by pdev is a supported SDIO Host
377 * Controller. If so, attach to it and attach to the target device.
380 bcmsdh_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
383 bcmsdh_hc_t *sdhc = NULL;
385 bcmsdh_info_t *sdh = NULL;
388 if (sd_pci_slot != 0xFFFFffff) {
389 if (pdev->bus->number != (sd_pci_slot>>16) ||
390 PCI_SLOT(pdev->devfn) != (sd_pci_slot&0xffff)) {
391 SDLX_MSG(("%s: %s: bus %X, slot %X, vend %X, dev %X\n",
393 bcmsdh_chipmatch(pdev->vendor, pdev->device)
394 ?"Found compatible SDIOHC"
395 :"Probing unknown device",
396 pdev->bus->number, PCI_SLOT(pdev->devfn), pdev->vendor,
400 SDLX_MSG(("%s: %s: bus %X, slot %X, vendor %X, device %X (good PCI location)\n",
402 bcmsdh_chipmatch(pdev->vendor, pdev->device)
403 ?"Using compatible SDIOHC"
404 :"WARNING, forced use of unkown device",
405 pdev->bus->number, PCI_SLOT(pdev->devfn), pdev->vendor, pdev->device));
408 if ((pdev->vendor == VENDOR_TI) && ((pdev->device == PCIXX21_FLASHMEDIA_ID) ||
409 (pdev->device == PCIXX21_FLASHMEDIA0_ID))) {
412 SDLX_MSG(("%s: Disabling TI FlashMedia Controller.\n", __FUNCTION__));
413 if (!(osh = osl_attach(pdev, PCI_BUS, FALSE))) {
414 SDLX_MSG(("%s: osl_attach failed\n", __FUNCTION__));
418 config_reg = OSL_PCI_READ_CONFIG(osh, 0x4c, 4);
421 * Set MMC_SD_DIS bit in FlashMedia Controller.
422 * Disbling the SD/MMC Controller in the FlashMedia Controller
423 * allows the Standard SD Host Controller to take over control
427 OSL_PCI_WRITE_CONFIG(osh, 0x4c, 4, config_reg);
430 /* match this pci device with what we support */
431 /* we can't solely rely on this to believe it is our SDIO Host Controller! */
432 if (!bcmsdh_chipmatch(pdev->vendor, pdev->device)) {
436 /* this is a pci device we might support */
437 SDLX_MSG(("%s: Found possible SDIO Host Controller: bus %d slot %d func %d irq %d\n",
439 pdev->bus->number, PCI_SLOT(pdev->devfn),
440 PCI_FUNC(pdev->devfn), pdev->irq));
442 /* use bcmsdh_query_device() to get the vendor ID of the target device so
443 * it will eventually appear in the Broadcom string on the console
446 /* allocate SDIO Host Controller state info */
447 if (!(osh = osl_attach(pdev, PCI_BUS, FALSE))) {
448 SDLX_MSG(("%s: osl_attach failed\n", __FUNCTION__));
451 if (!(sdhc = MALLOC(osh, sizeof(bcmsdh_hc_t)))) {
452 SDLX_MSG(("%s: out of memory, allocated %d bytes\n",
457 bzero(sdhc, sizeof(bcmsdh_hc_t));
462 /* map to address where host can access */
463 pci_set_master(pdev);
464 rc = pci_enable_device(pdev);
466 SDLX_MSG(("%s: Cannot enable PCI device\n", __FUNCTION__));
469 if (!(sdh = bcmsdh_attach(osh, (void *)(uintptr)pci_resource_start(pdev, 0),
470 (void **)®s, pdev->irq))) {
471 SDLX_MSG(("%s: bcmsdh_attach failed\n", __FUNCTION__));
477 /* try to attach to the target device */
478 if (!(sdhc->ch = drvinfo.attach(VENDOR_BROADCOM, /* pdev->vendor, */
479 bcmsdh_query_device(sdh) & 0xFFFF, 0, 0, 0, 0,
480 (void *)regs, NULL, sdh))) {
481 SDLX_MSG(("%s: device attach failed\n", __FUNCTION__));
485 /* chain SDIO Host Controller info together */
486 sdhc->next = sdhcinfo;
495 bcmsdh_detach(sdhc->osh, sdhc->sdh);
496 MFREE(osh, sdhc, sizeof(bcmsdh_hc_t));
505 * Detach from target devices and SDIO Host Controller
507 static void __devexit
508 bcmsdh_pci_remove(struct pci_dev *pdev)
510 bcmsdh_hc_t *sdhc, *prev;
513 /* find the SDIO Host Controller state for this pdev and take it out from the list */
514 for (sdhc = sdhcinfo, prev = NULL; sdhc; sdhc = sdhc->next) {
515 if (sdhc->dev == pdev) {
517 prev->next = sdhc->next;
527 drvinfo.detach(sdhc->ch);
529 bcmsdh_detach(sdhc->osh, sdhc->sdh);
531 /* release SDIO Host Controller info */
533 MFREE(osh, sdhc, sizeof(bcmsdh_hc_t));
536 #endif /* BCMLXSDMMC */
537 #endif /* BCMPLATFORM_BUS */
539 extern int sdio_function_init(void);
541 extern int sdio_func_reg_notify(void* semaphore);
542 extern void sdio_func_unreg_notify(void);
544 #if defined(BCMLXSDMMC)
545 int bcmsdh_reg_sdio_notify(void* semaphore)
548 return sdio_func_reg_notify(semaphore);
552 void bcmsdh_unreg_sdio_notify(void)
554 sdio_func_unreg_notify();
557 #endif /* defined(BCMLXSDMMC) */
560 bcmsdh_register(bcmsdh_driver_t *driver)
566 #if defined(BCMPLATFORM_BUS)
567 SDLX_MSG(("Linux Kernel SDIO/MMC Driver\n"));
568 error = sdio_function_init();
570 #endif /* defined(BCMPLATFORM_BUS) */
572 #if !defined(BCMPLATFORM_BUS) && !defined(BCMLXSDMMC)
573 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
574 if (!(error = pci_module_init(&bcmsdh_pci_driver)))
577 if (!(error = pci_register_driver(&bcmsdh_pci_driver)))
581 SDLX_MSG(("%s: pci_module_init failed 0x%x\n", __FUNCTION__, error));
582 #endif /* BCMPLATFORM_BUS */
587 extern void sdio_function_cleanup(void);
590 bcmsdh_unregister(void)
592 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
593 if (bcmsdh_pci_driver.node.next)
596 #if defined(BCMLXSDMMC)
597 sdio_function_cleanup();
598 #endif /* BCMLXSDMMC */
600 #if !defined(BCMPLATFORM_BUS) && !defined(BCMLXSDMMC)
601 pci_unregister_driver(&bcmsdh_pci_driver);
602 #endif /* BCMPLATFORM_BUS */
605 #if defined(OOB_INTR_ONLY)
606 void bcmsdh_oob_intr_set(bool enable)
608 static bool curstate = 1;
611 spin_lock_irqsave(&sdhcinfo->irq_lock, flags);
612 if (curstate != enable) {
614 enable_irq(sdhcinfo->oob_irq);
616 disable_irq_nosync(sdhcinfo->oob_irq);
619 spin_unlock_irqrestore(&sdhcinfo->irq_lock, flags);
622 static irqreturn_t wlan_oob_irq(int irq, void *dev_id)
626 dhdp = (dhd_pub_t *)dev_get_drvdata(sdhcinfo->dev);
628 bcmsdh_oob_intr_set(0);
631 SDLX_MSG(("Out of band GPIO interrupt fired way too early\n"));
635 dhdsdio_isr((void *)dhdp->bus);
640 int bcmsdh_register_oob_intr(void * dhdp)
644 SDLX_MSG(("%s Enter \n", __FUNCTION__));
646 /* IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL | IORESOURCE_IRQ_SHAREABLE; */
648 dev_set_drvdata(sdhcinfo->dev, dhdp);
650 if (!sdhcinfo->oob_irq_registered) {
651 SDLX_MSG(("%s IRQ=%d Type=%X \n", __FUNCTION__,
652 (int)sdhcinfo->oob_irq, (int)sdhcinfo->oob_flags));
653 /* Refer to customer Host IRQ docs about proper irqflags definition */
654 error = request_irq(sdhcinfo->oob_irq, wlan_oob_irq, sdhcinfo->oob_flags,
655 "bcmsdh_sdmmc", NULL);
659 enable_irq_wake(sdhcinfo->oob_irq);
660 sdhcinfo->oob_irq_registered = TRUE;
661 sdhcinfo->oob_irq_enable_flag = TRUE;
667 void bcmsdh_set_irq(int flag)
669 if (sdhcinfo->oob_irq_registered && sdhcinfo->oob_irq_enable_flag != flag) {
670 SDLX_MSG(("%s Flag = %d", __FUNCTION__, flag));
671 sdhcinfo->oob_irq_enable_flag = flag;
673 enable_irq(sdhcinfo->oob_irq);
674 enable_irq_wake(sdhcinfo->oob_irq);
676 disable_irq_wake(sdhcinfo->oob_irq);
677 disable_irq(sdhcinfo->oob_irq);
682 void bcmsdh_unregister_oob_intr(void)
684 SDLX_MSG(("%s: Enter\n", __FUNCTION__));
686 if (sdhcinfo->oob_irq_registered == TRUE) {
687 bcmsdh_set_irq(FALSE);
688 free_irq(sdhcinfo->oob_irq, NULL);
689 sdhcinfo->oob_irq_registered = FALSE;
692 #endif /* defined(OOB_INTR_ONLY) */
694 #if defined(BCMLXSDMMC)
695 void *bcmsdh_get_drvdata(void)
699 return dev_get_drvdata(sdhcinfo->dev);
701 #endif /* defined(OOB_INTR_ONLY) */
703 /* Module parameters specific to each host-controller driver */
705 extern uint sd_msglevel; /* Debug message level */
706 module_param(sd_msglevel, uint, 0);
708 extern uint sd_power; /* 0 = SD Power OFF, 1 = SD Power ON. */
709 module_param(sd_power, uint, 0);
711 extern uint sd_clock; /* SD Clock Control, 0 = SD Clock OFF, 1 = SD Clock ON */
712 module_param(sd_clock, uint, 0);
714 extern uint sd_divisor; /* Divisor (-1 means external clock) */
715 module_param(sd_divisor, uint, 0);
717 extern uint sd_sdmode; /* Default is SD4, 0=SPI, 1=SD1, 2=SD4 */
718 module_param(sd_sdmode, uint, 0);
720 extern uint sd_hiok; /* Ok to use hi-speed mode */
721 module_param(sd_hiok, uint, 0);
723 extern uint sd_f2_blocksize;
724 module_param(sd_f2_blocksize, int, 0);
727 extern int sd_uhsimode;
728 module_param(sd_uhsimode, int, 0);
732 EXPORT_SYMBOL(bcmsdh_attach);
733 EXPORT_SYMBOL(bcmsdh_detach);
734 EXPORT_SYMBOL(bcmsdh_intr_query);
735 EXPORT_SYMBOL(bcmsdh_intr_enable);
736 EXPORT_SYMBOL(bcmsdh_intr_disable);
737 EXPORT_SYMBOL(bcmsdh_intr_reg);
738 EXPORT_SYMBOL(bcmsdh_intr_dereg);
740 #if defined(DHD_DEBUG)
741 EXPORT_SYMBOL(bcmsdh_intr_pending);
744 EXPORT_SYMBOL(bcmsdh_devremove_reg);
745 EXPORT_SYMBOL(bcmsdh_cfg_read);
746 EXPORT_SYMBOL(bcmsdh_cfg_write);
747 EXPORT_SYMBOL(bcmsdh_cis_read);
748 EXPORT_SYMBOL(bcmsdh_reg_read);
749 EXPORT_SYMBOL(bcmsdh_reg_write);
750 EXPORT_SYMBOL(bcmsdh_regfail);
751 EXPORT_SYMBOL(bcmsdh_send_buf);
752 EXPORT_SYMBOL(bcmsdh_recv_buf);
754 EXPORT_SYMBOL(bcmsdh_rwdata);
755 EXPORT_SYMBOL(bcmsdh_abort);
756 EXPORT_SYMBOL(bcmsdh_query_device);
757 EXPORT_SYMBOL(bcmsdh_query_iofnum);
758 EXPORT_SYMBOL(bcmsdh_iovar_op);
759 EXPORT_SYMBOL(bcmsdh_register);
760 EXPORT_SYMBOL(bcmsdh_unregister);
761 EXPORT_SYMBOL(bcmsdh_chipmatch);
762 EXPORT_SYMBOL(bcmsdh_reset);
763 EXPORT_SYMBOL(bcmsdh_waitlockfree);
765 EXPORT_SYMBOL(bcmsdh_get_dstatus);
766 EXPORT_SYMBOL(bcmsdh_cfg_read_word);
767 EXPORT_SYMBOL(bcmsdh_cfg_write_word);
768 EXPORT_SYMBOL(bcmsdh_cur_sbwad);
769 EXPORT_SYMBOL(bcmsdh_chipinfo);
771 #endif /* BCMSDH_MODULE */