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 ret = device_init_wakeup(pm_dev, 1);
246 printf("%s : device_init_wakeup(pm_dev) enable, ret = %d\n", __func__, ret);
247 #endif /* CONFIG_PM_SLEEP && CUSTOMER_HW_SLP */
248 /* Read the vendor/device ID from the CIS */
249 vendevid = bcmsdh_query_device(sdh);
250 /* try to attach to the target device */
251 if (!(sdhc->ch = drvinfo.attach((vendevid >> 16),
252 (vendevid & 0xFFFF), 0, 0, 0, 0,
253 (void *)regs, NULL, sdh))) {
254 SDLX_MSG(("%s: device attach failed\n", __FUNCTION__));
264 bcmsdh_detach(sdhc->osh, sdhc->sdh);
265 MFREE(osh, sdhc, sizeof(bcmsdh_hc_t));
274 #endif /* BCMLXSDMMC */
275 int bcmsdh_remove(struct device *dev)
277 bcmsdh_hc_t *sdhc, *prev;
281 #if defined(CONFIG_PM_SLEEP) && defined(CUSTOMER_HW_SLP)
282 /*SLP_wakelock_alternative_code*/
283 device_init_wakeup(pm_dev, 0);
284 printf("%s : device_init_wakeup(pm_dev) disable\n", __func__);
285 #endif /* CONFIG_PM_SLEEP && CUSTOMER_HW_SLP */
286 drvinfo.detach(sdhc->ch);
287 bcmsdh_detach(sdhc->osh, sdhc->sdh);
289 /* find the SDIO Host Controller state for this pdev and take it out from the list */
290 for (sdhc = sdhcinfo, prev = NULL; sdhc; sdhc = sdhc->next) {
291 if (sdhc->dev == (void *)dev) {
293 prev->next = sdhc->next;
301 SDLX_MSG(("%s: failed\n", __FUNCTION__));
305 /* release SDIO Host Controller info */
307 MFREE(osh, sdhc, sizeof(bcmsdh_hc_t));
310 #if !defined(BCMLXSDMMC) || defined(OOB_INTR_ONLY)
311 dev_set_drvdata(dev, NULL);
312 #endif /* !defined(BCMLXSDMMC) || defined(OOB_INTR_ONLY) */
317 #else /* BCMPLATFORM_BUS */
319 #if !defined(BCMLXSDMMC)
320 /* forward declarations for PCI probe and remove functions. */
321 static int __devinit bcmsdh_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
322 static void __devexit bcmsdh_pci_remove(struct pci_dev *pdev);
327 static struct pci_device_id bcmsdh_pci_devid[] __devinitdata = {
328 { vendor: PCI_ANY_ID,
330 subvendor: PCI_ANY_ID,
331 subdevice: PCI_ANY_ID,
338 MODULE_DEVICE_TABLE(pci, bcmsdh_pci_devid);
341 * SDIO Host Controller pci driver info
343 static struct pci_driver bcmsdh_pci_driver = {
346 id_table: bcmsdh_pci_devid,
347 probe: bcmsdh_pci_probe,
348 remove: bcmsdh_pci_remove,
349 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
357 extern uint sd_pci_slot; /* Force detection to a particular PCI */
358 /* slot only . Allows for having multiple */
359 /* WL devices at once in a PC */
360 /* Only one instance of dhd will be */
361 /* usable at a time */
362 /* Upper word is bus number, */
363 /* lower word is slot number */
364 /* Default value of 0xffffffff turns this */
366 module_param(sd_pci_slot, uint, 0);
370 * Detect supported SDIO Host Controller and attach if found.
372 * Determine if the device described by pdev is a supported SDIO Host
373 * Controller. If so, attach to it and attach to the target device.
376 bcmsdh_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
379 bcmsdh_hc_t *sdhc = NULL;
381 bcmsdh_info_t *sdh = NULL;
384 if (sd_pci_slot != 0xFFFFffff) {
385 if (pdev->bus->number != (sd_pci_slot>>16) ||
386 PCI_SLOT(pdev->devfn) != (sd_pci_slot&0xffff)) {
387 SDLX_MSG(("%s: %s: bus %X, slot %X, vend %X, dev %X\n",
389 bcmsdh_chipmatch(pdev->vendor, pdev->device)
390 ?"Found compatible SDIOHC"
391 :"Probing unknown device",
392 pdev->bus->number, PCI_SLOT(pdev->devfn), pdev->vendor,
396 SDLX_MSG(("%s: %s: bus %X, slot %X, vendor %X, device %X (good PCI location)\n",
398 bcmsdh_chipmatch(pdev->vendor, pdev->device)
399 ?"Using compatible SDIOHC"
400 :"WARNING, forced use of unkown device",
401 pdev->bus->number, PCI_SLOT(pdev->devfn), pdev->vendor, pdev->device));
404 if ((pdev->vendor == VENDOR_TI) && ((pdev->device == PCIXX21_FLASHMEDIA_ID) ||
405 (pdev->device == PCIXX21_FLASHMEDIA0_ID))) {
408 SDLX_MSG(("%s: Disabling TI FlashMedia Controller.\n", __FUNCTION__));
409 if (!(osh = osl_attach(pdev, PCI_BUS, FALSE))) {
410 SDLX_MSG(("%s: osl_attach failed\n", __FUNCTION__));
414 config_reg = OSL_PCI_READ_CONFIG(osh, 0x4c, 4);
417 * Set MMC_SD_DIS bit in FlashMedia Controller.
418 * Disbling the SD/MMC Controller in the FlashMedia Controller
419 * allows the Standard SD Host Controller to take over control
423 OSL_PCI_WRITE_CONFIG(osh, 0x4c, 4, config_reg);
426 /* match this pci device with what we support */
427 /* we can't solely rely on this to believe it is our SDIO Host Controller! */
428 if (!bcmsdh_chipmatch(pdev->vendor, pdev->device)) {
432 /* this is a pci device we might support */
433 SDLX_MSG(("%s: Found possible SDIO Host Controller: bus %d slot %d func %d irq %d\n",
435 pdev->bus->number, PCI_SLOT(pdev->devfn),
436 PCI_FUNC(pdev->devfn), pdev->irq));
438 /* use bcmsdh_query_device() to get the vendor ID of the target device so
439 * it will eventually appear in the Broadcom string on the console
442 /* allocate SDIO Host Controller state info */
443 if (!(osh = osl_attach(pdev, PCI_BUS, FALSE))) {
444 SDLX_MSG(("%s: osl_attach failed\n", __FUNCTION__));
447 if (!(sdhc = MALLOC(osh, sizeof(bcmsdh_hc_t)))) {
448 SDLX_MSG(("%s: out of memory, allocated %d bytes\n",
453 bzero(sdhc, sizeof(bcmsdh_hc_t));
458 /* map to address where host can access */
459 pci_set_master(pdev);
460 rc = pci_enable_device(pdev);
462 SDLX_MSG(("%s: Cannot enable PCI device\n", __FUNCTION__));
465 if (!(sdh = bcmsdh_attach(osh, (void *)(uintptr)pci_resource_start(pdev, 0),
466 (void **)®s, pdev->irq))) {
467 SDLX_MSG(("%s: bcmsdh_attach failed\n", __FUNCTION__));
473 /* try to attach to the target device */
474 if (!(sdhc->ch = drvinfo.attach(VENDOR_BROADCOM, /* pdev->vendor, */
475 bcmsdh_query_device(sdh) & 0xFFFF, 0, 0, 0, 0,
476 (void *)regs, NULL, sdh))) {
477 SDLX_MSG(("%s: device attach failed\n", __FUNCTION__));
481 /* chain SDIO Host Controller info together */
482 sdhc->next = sdhcinfo;
491 bcmsdh_detach(sdhc->osh, sdhc->sdh);
492 MFREE(osh, sdhc, sizeof(bcmsdh_hc_t));
501 * Detach from target devices and SDIO Host Controller
503 static void __devexit
504 bcmsdh_pci_remove(struct pci_dev *pdev)
506 bcmsdh_hc_t *sdhc, *prev;
509 /* find the SDIO Host Controller state for this pdev and take it out from the list */
510 for (sdhc = sdhcinfo, prev = NULL; sdhc; sdhc = sdhc->next) {
511 if (sdhc->dev == pdev) {
513 prev->next = sdhc->next;
523 drvinfo.detach(sdhc->ch);
525 bcmsdh_detach(sdhc->osh, sdhc->sdh);
527 /* release SDIO Host Controller info */
529 MFREE(osh, sdhc, sizeof(bcmsdh_hc_t));
532 #endif /* BCMLXSDMMC */
533 #endif /* BCMPLATFORM_BUS */
535 extern int sdio_function_init(void);
537 extern int sdio_func_reg_notify(void* semaphore);
538 extern void sdio_func_unreg_notify(void);
540 #if defined(BCMLXSDMMC)
541 int bcmsdh_reg_sdio_notify(void* semaphore)
544 return sdio_func_reg_notify(semaphore);
548 void bcmsdh_unreg_sdio_notify(void)
550 sdio_func_unreg_notify();
553 #endif /* defined(BCMLXSDMMC) */
556 bcmsdh_register(bcmsdh_driver_t *driver)
562 #if defined(BCMPLATFORM_BUS)
563 SDLX_MSG(("Linux Kernel SDIO/MMC Driver\n"));
564 error = sdio_function_init();
566 #endif /* defined(BCMPLATFORM_BUS) */
568 #if !defined(BCMPLATFORM_BUS) && !defined(BCMLXSDMMC)
569 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
570 if (!(error = pci_module_init(&bcmsdh_pci_driver)))
573 if (!(error = pci_register_driver(&bcmsdh_pci_driver)))
577 SDLX_MSG(("%s: pci_module_init failed 0x%x\n", __FUNCTION__, error));
578 #endif /* BCMPLATFORM_BUS */
583 extern void sdio_function_cleanup(void);
586 bcmsdh_unregister(void)
588 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
589 if (bcmsdh_pci_driver.node.next)
592 #if defined(BCMLXSDMMC)
593 sdio_function_cleanup();
594 #endif /* BCMLXSDMMC */
596 #if !defined(BCMPLATFORM_BUS) && !defined(BCMLXSDMMC)
597 pci_unregister_driver(&bcmsdh_pci_driver);
598 #endif /* BCMPLATFORM_BUS */
601 #if defined(OOB_INTR_ONLY)
602 void bcmsdh_oob_intr_set(bool enable)
604 static bool curstate = 1;
607 spin_lock_irqsave(&sdhcinfo->irq_lock, flags);
608 if (curstate != enable) {
610 enable_irq(sdhcinfo->oob_irq);
612 disable_irq_nosync(sdhcinfo->oob_irq);
615 spin_unlock_irqrestore(&sdhcinfo->irq_lock, flags);
618 static irqreturn_t wlan_oob_irq(int irq, void *dev_id)
622 dhdp = (dhd_pub_t *)dev_get_drvdata(sdhcinfo->dev);
624 bcmsdh_oob_intr_set(0);
627 SDLX_MSG(("Out of band GPIO interrupt fired way too early\n"));
631 dhdsdio_isr((void *)dhdp->bus);
636 int bcmsdh_register_oob_intr(void * dhdp)
640 SDLX_MSG(("%s Enter \n", __FUNCTION__));
642 /* IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL | IORESOURCE_IRQ_SHAREABLE; */
644 dev_set_drvdata(sdhcinfo->dev, dhdp);
646 if (!sdhcinfo->oob_irq_registered) {
647 SDLX_MSG(("%s IRQ=%d Type=%X \n", __FUNCTION__,
648 (int)sdhcinfo->oob_irq, (int)sdhcinfo->oob_flags));
649 /* Refer to customer Host IRQ docs about proper irqflags definition */
650 error = request_irq(sdhcinfo->oob_irq, wlan_oob_irq, sdhcinfo->oob_flags,
651 "bcmsdh_sdmmc", NULL);
655 enable_irq_wake(sdhcinfo->oob_irq);
656 sdhcinfo->oob_irq_registered = TRUE;
657 sdhcinfo->oob_irq_enable_flag = TRUE;
663 void bcmsdh_set_irq(int flag)
665 if (sdhcinfo->oob_irq_registered && sdhcinfo->oob_irq_enable_flag != flag) {
666 SDLX_MSG(("%s Flag = %d", __FUNCTION__, flag));
667 sdhcinfo->oob_irq_enable_flag = flag;
669 enable_irq(sdhcinfo->oob_irq);
670 enable_irq_wake(sdhcinfo->oob_irq);
672 disable_irq_wake(sdhcinfo->oob_irq);
673 disable_irq(sdhcinfo->oob_irq);
678 void bcmsdh_unregister_oob_intr(void)
680 SDLX_MSG(("%s: Enter\n", __FUNCTION__));
682 if (sdhcinfo->oob_irq_registered == TRUE) {
683 bcmsdh_set_irq(FALSE);
684 free_irq(sdhcinfo->oob_irq, NULL);
685 sdhcinfo->oob_irq_registered = FALSE;
688 #endif /* defined(OOB_INTR_ONLY) */
690 #if defined(BCMLXSDMMC)
691 void *bcmsdh_get_drvdata(void)
695 return dev_get_drvdata(sdhcinfo->dev);
697 #endif /* defined(OOB_INTR_ONLY) */
699 /* Module parameters specific to each host-controller driver */
701 extern uint sd_msglevel; /* Debug message level */
702 module_param(sd_msglevel, uint, 0);
704 extern uint sd_power; /* 0 = SD Power OFF, 1 = SD Power ON. */
705 module_param(sd_power, uint, 0);
707 extern uint sd_clock; /* SD Clock Control, 0 = SD Clock OFF, 1 = SD Clock ON */
708 module_param(sd_clock, uint, 0);
710 extern uint sd_divisor; /* Divisor (-1 means external clock) */
711 module_param(sd_divisor, uint, 0);
713 extern uint sd_sdmode; /* Default is SD4, 0=SPI, 1=SD1, 2=SD4 */
714 module_param(sd_sdmode, uint, 0);
716 extern uint sd_hiok; /* Ok to use hi-speed mode */
717 module_param(sd_hiok, uint, 0);
719 extern uint sd_f2_blocksize;
720 module_param(sd_f2_blocksize, int, 0);
723 extern int sd_uhsimode;
724 module_param(sd_uhsimode, int, 0);
728 EXPORT_SYMBOL(bcmsdh_attach);
729 EXPORT_SYMBOL(bcmsdh_detach);
730 EXPORT_SYMBOL(bcmsdh_intr_query);
731 EXPORT_SYMBOL(bcmsdh_intr_enable);
732 EXPORT_SYMBOL(bcmsdh_intr_disable);
733 EXPORT_SYMBOL(bcmsdh_intr_reg);
734 EXPORT_SYMBOL(bcmsdh_intr_dereg);
736 #if defined(DHD_DEBUG)
737 EXPORT_SYMBOL(bcmsdh_intr_pending);
740 EXPORT_SYMBOL(bcmsdh_devremove_reg);
741 EXPORT_SYMBOL(bcmsdh_cfg_read);
742 EXPORT_SYMBOL(bcmsdh_cfg_write);
743 EXPORT_SYMBOL(bcmsdh_cis_read);
744 EXPORT_SYMBOL(bcmsdh_reg_read);
745 EXPORT_SYMBOL(bcmsdh_reg_write);
746 EXPORT_SYMBOL(bcmsdh_regfail);
747 EXPORT_SYMBOL(bcmsdh_send_buf);
748 EXPORT_SYMBOL(bcmsdh_recv_buf);
750 EXPORT_SYMBOL(bcmsdh_rwdata);
751 EXPORT_SYMBOL(bcmsdh_abort);
752 EXPORT_SYMBOL(bcmsdh_query_device);
753 EXPORT_SYMBOL(bcmsdh_query_iofnum);
754 EXPORT_SYMBOL(bcmsdh_iovar_op);
755 EXPORT_SYMBOL(bcmsdh_register);
756 EXPORT_SYMBOL(bcmsdh_unregister);
757 EXPORT_SYMBOL(bcmsdh_chipmatch);
758 EXPORT_SYMBOL(bcmsdh_reset);
759 EXPORT_SYMBOL(bcmsdh_waitlockfree);
761 EXPORT_SYMBOL(bcmsdh_get_dstatus);
762 EXPORT_SYMBOL(bcmsdh_cfg_read_word);
763 EXPORT_SYMBOL(bcmsdh_cfg_write_word);
764 EXPORT_SYMBOL(bcmsdh_cur_sbwad);
765 EXPORT_SYMBOL(bcmsdh_chipinfo);
767 #endif /* BCMSDH_MODULE */