staging: brcm80211: brcmfmac/*: fix remaining 'assignment in if condition'
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / brcm80211 / brcmfmac / bcmsdh_linux.c
blobf81823ebeb10f8c87db63cb285bacb58b33c24a2
1 /*
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.
17 /**
18 * @file bcmsdh_linux.c
21 #define __UNDEF_NO_VERSION__
23 #include <typedefs.h>
24 #include <linuxver.h>
26 #include <linux/pci.h>
27 #include <linux/completion.h>
29 #include <osl.h>
30 #include <pcicfg.h>
31 #include <bcmdefs.h>
32 #include <bcmdevs.h>
34 #if defined(OOB_INTR_ONLY)
35 #include <linux/irq.h>
36 extern void dhdsdio_isr(void *args);
37 #include <bcmutils.h>
38 #include <dngl_stats.h>
39 #include <dhd.h>
40 #endif /* defined(OOB_INTR_ONLY) */
41 #if defined(CONFIG_MACH_SANDGATE2G) || defined(CONFIG_MACH_LOGICPD_PXA270)
42 #if !defined(BCMPLATFORM_BUS)
43 #define BCMPLATFORM_BUS
44 #endif /* !defined(BCMPLATFORM_BUS) */
46 #include <linux/platform_device.h>
47 #endif /* CONFIG_MACH_SANDGATE2G */
49 /**
50 * SDIO Host Controller info
52 typedef struct bcmsdh_hc bcmsdh_hc_t;
54 struct bcmsdh_hc {
55 bcmsdh_hc_t *next;
56 #ifdef BCMPLATFORM_BUS
57 struct device *dev; /* platform device handle */
58 #else
59 struct pci_dev *dev; /* pci device handle */
60 #endif /* BCMPLATFORM_BUS */
61 osl_t *osh;
62 void *regs; /* SDIO Host Controller address */
63 bcmsdh_info_t *sdh; /* SDIO Host Controller handle */
64 void *ch;
65 unsigned int oob_irq;
66 unsigned long oob_flags; /* OOB Host specifiction
67 as edge and etc */
68 bool oob_irq_registered;
69 #if defined(OOB_INTR_ONLY)
70 spinlock_t irq_lock;
71 #endif
73 static bcmsdh_hc_t *sdhcinfo = NULL;
75 /* driver info, initialized when bcmsdh_register is called */
76 static bcmsdh_driver_t drvinfo = { NULL, NULL };
78 /* debugging macros */
79 #define SDLX_MSG(x)
81 /**
82 * Checks to see if vendor and device IDs match a supported SDIO Host Controller.
84 bool bcmsdh_chipmatch(uint16 vendor, uint16 device)
86 /* Add other vendors and devices as required */
88 #ifdef BCMSDIOH_STD
89 /* Check for Arasan host controller */
90 if (vendor == VENDOR_SI_IMAGE)
91 return TRUE;
93 /* Check for BRCM 27XX Standard host controller */
94 if (device == BCM27XX_SDIOH_ID && vendor == VENDOR_BROADCOM)
95 return TRUE;
97 /* Check for BRCM Standard host controller */
98 if (device == SDIOH_FPGA_ID && vendor == VENDOR_BROADCOM)
99 return TRUE;
101 /* Check for TI PCIxx21 Standard host controller */
102 if (device == PCIXX21_SDIOH_ID && vendor == VENDOR_TI)
103 return TRUE;
105 if (device == PCIXX21_SDIOH0_ID && vendor == VENDOR_TI)
106 return TRUE;
108 /* Ricoh R5C822 Standard SDIO Host */
109 if (device == R5C822_SDIOH_ID && vendor == VENDOR_RICOH)
110 return TRUE;
112 /* JMicron Standard SDIO Host */
113 if (device == JMICRON_SDIOH_ID && vendor == VENDOR_JMICRON)
114 return TRUE;
115 #endif /* BCMSDIOH_STD */
116 #ifdef BCMSDIOH_SPI
117 /* This is the PciSpiHost. */
118 if (device == SPIH_FPGA_ID && vendor == VENDOR_BROADCOM) {
119 printf("Found PCI SPI Host Controller\n");
120 return TRUE;
122 #endif /* BCMSDIOH_SPI */
124 return FALSE;
127 #if defined(BCMPLATFORM_BUS)
128 #if defined(BCMLXSDMMC)
129 /* forward declarations */
130 int bcmsdh_probe(struct device *dev);
131 EXPORT_SYMBOL(bcmsdh_probe);
133 int bcmsdh_remove(struct device *dev);
134 EXPORT_SYMBOL(bcmsdh_remove);
136 #else
137 /* forward declarations */
138 static int __devinit bcmsdh_probe(struct device *dev);
139 static int __devexit bcmsdh_remove(struct device *dev);
140 #endif /* BCMLXSDMMC */
142 #ifndef BCMLXSDMMC
143 static struct device_driver bcmsdh_driver = {
144 .name = "pxa2xx-mci",
145 .bus = &platform_bus_type,
146 .probe = bcmsdh_probe,
147 .remove = bcmsdh_remove,
148 .suspend = NULL,
149 .resume = NULL,
151 #endif /* BCMLXSDMMC */
153 #ifndef BCMLXSDMMC
154 static
155 #endif /* BCMLXSDMMC */
156 int bcmsdh_probe(struct device *dev)
158 osl_t *osh = NULL;
159 bcmsdh_hc_t *sdhc = NULL;
160 unsigned long regs = 0;
161 bcmsdh_info_t *sdh = NULL;
162 #if !defined(BCMLXSDMMC) && defined(BCMPLATFORM_BUS)
163 struct platform_device *pdev;
164 struct resource *r;
165 #endif /* BCMLXSDMMC */
166 int irq = 0;
167 uint32 vendevid;
168 unsigned long irq_flags = 0;
170 #if !defined(BCMLXSDMMC) && defined(BCMPLATFORM_BUS)
171 pdev = to_platform_device(dev);
172 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
173 irq = platform_get_irq(pdev, 0);
174 if (!r || irq == NO_IRQ)
175 return -ENXIO;
176 #endif /* BCMLXSDMMC */
178 #if defined(OOB_INTR_ONLY)
179 #ifdef HW_OOB
180 irq_flags =
181 IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL |
182 IORESOURCE_IRQ_SHAREABLE;
183 #else
184 irq_flags = IRQF_TRIGGER_FALLING;
185 #endif /* HW_OOB */
186 irq = dhd_customer_oob_irq_map(&irq_flags);
187 if (irq < 0) {
188 SDLX_MSG(("%s: Host irq is not defined\n", __func__));
189 return 1;
191 #endif /* defined(OOB_INTR_ONLY) */
192 /* allocate SDIO Host Controller state info */
193 osh = osl_attach(dev, PCI_BUS, FALSE);
194 if (!osh) {
195 SDLX_MSG(("%s: osl_attach failed\n", __func__));
196 goto err;
198 sdhc = MALLOC(osh, sizeof(bcmsdh_hc_t));
199 if (!sdhc) {
200 SDLX_MSG(("%s: out of memory, allocated %d bytes\n",
201 __func__, MALLOCED(osh)));
202 goto err;
204 bzero(sdhc, sizeof(bcmsdh_hc_t));
205 sdhc->osh = osh;
207 sdhc->dev = (void *)dev;
209 #ifdef BCMLXSDMMC
210 sdh = bcmsdh_attach(osh, (void *)0, (void **)&regs, irq);
211 if (!sdh) {
212 SDLX_MSG(("%s: bcmsdh_attach failed\n", __func__));
213 goto err;
215 #else
216 sdh = bcmsdh_attach(osh, (void *)r->start, (void **)&regs, irq);
217 if (!sdh) {
218 SDLX_MSG(("%s: bcmsdh_attach failed\n", __func__));
219 goto err;
221 #endif /* BCMLXSDMMC */
222 sdhc->sdh = sdh;
223 sdhc->oob_irq = irq;
224 sdhc->oob_flags = irq_flags;
225 sdhc->oob_irq_registered = FALSE; /* to make sure.. */
226 #if defined(OOB_INTR_ONLY)
227 spin_lock_init(&sdhc->irq_lock);
228 #endif
230 /* chain SDIO Host Controller info together */
231 sdhc->next = sdhcinfo;
232 sdhcinfo = sdhc;
233 /* Read the vendor/device ID from the CIS */
234 vendevid = bcmsdh_query_device(sdh);
236 /* try to attach to the target device */
237 sdhc->ch = drvinfo.attach((vendevid >> 16), (vendevid & 0xFFFF),
238 0, 0, 0, 0, (void *)regs, NULL, sdh);
239 if (!sdhc->ch) {
240 SDLX_MSG(("%s: device attach failed\n", __func__));
241 goto err;
244 return 0;
246 /* error handling */
247 err:
248 if (sdhc) {
249 if (sdhc->sdh)
250 bcmsdh_detach(sdhc->osh, sdhc->sdh);
251 MFREE(osh, sdhc, sizeof(bcmsdh_hc_t));
253 if (osh)
254 osl_detach(osh);
255 return -ENODEV;
258 #ifndef BCMLXSDMMC
259 static
260 #endif /* BCMLXSDMMC */
261 int bcmsdh_remove(struct device *dev)
263 bcmsdh_hc_t *sdhc, *prev;
264 osl_t *osh;
266 sdhc = sdhcinfo;
267 drvinfo.detach(sdhc->ch);
268 bcmsdh_detach(sdhc->osh, sdhc->sdh);
269 /* find the SDIO Host Controller state for this pdev
270 and take it out from the list */
271 for (sdhc = sdhcinfo, prev = NULL; sdhc; sdhc = sdhc->next) {
272 if (sdhc->dev == (void *)dev) {
273 if (prev)
274 prev->next = sdhc->next;
275 else
276 sdhcinfo = NULL;
277 break;
279 prev = sdhc;
281 if (!sdhc) {
282 SDLX_MSG(("%s: failed\n", __func__));
283 return 0;
286 /* release SDIO Host Controller info */
287 osh = sdhc->osh;
288 MFREE(osh, sdhc, sizeof(bcmsdh_hc_t));
289 osl_detach(osh);
291 #if !defined(BCMLXSDMMC)
292 dev_set_drvdata(dev, NULL);
293 #endif /* !defined(BCMLXSDMMC) */
295 return 0;
298 #else /* BCMPLATFORM_BUS */
300 #if !defined(BCMLXSDMMC)
301 /* forward declarations for PCI probe and remove functions. */
302 static int __devinit bcmsdh_pci_probe(struct pci_dev *pdev,
303 const struct pci_device_id *ent);
304 static void __devexit bcmsdh_pci_remove(struct pci_dev *pdev);
307 * pci id table
309 static struct pci_device_id bcmsdh_pci_devid[] __devinitdata = {
311 .vendor = PCI_ANY_ID,
312 .device = PCI_ANY_ID,
313 .subvendor = PCI_ANY_ID,
314 .subdevice = PCI_ANY_ID,
315 .class = 0,
316 .class_mask = 0,
317 .driver_data = 0,
319 {0,}
322 MODULE_DEVICE_TABLE(pci, bcmsdh_pci_devid);
325 * SDIO Host Controller pci driver info
327 static struct pci_driver bcmsdh_pci_driver = {
328 .node = {},
329 .name = "bcmsdh",
330 .id_table = bcmsdh_pci_devid,
331 .probe = bcmsdh_pci_probe,
332 .remove = bcmsdh_pci_remove,
333 .suspend = NULL,
334 .resume = NULL,
337 extern uint sd_pci_slot; /* Force detection to a particular PCI */
338 /* slot only . Allows for having multiple */
339 /* WL devices at once in a PC */
340 /* Only one instance of dhd will be */
341 /* usable at a time */
342 /* Upper word is bus number, */
343 /* lower word is slot number */
344 /* Default value of 0xFFFFffff turns this */
345 /* off */
346 module_param(sd_pci_slot, uint, 0);
349 * Detect supported SDIO Host Controller and attach if found.
351 * Determine if the device described by pdev is a supported SDIO Host
352 * Controller. If so, attach to it and attach to the target device.
354 static int __devinit
355 bcmsdh_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
357 osl_t *osh = NULL;
358 bcmsdh_hc_t *sdhc = NULL;
359 unsigned long regs;
360 bcmsdh_info_t *sdh = NULL;
361 int rc;
363 if (sd_pci_slot != 0xFFFFffff) {
364 if (pdev->bus->number != (sd_pci_slot >> 16) ||
365 PCI_SLOT(pdev->devfn) != (sd_pci_slot & 0xffff)) {
366 SDLX_MSG(("%s: %s: bus %X, slot %X, vend %X, dev %X\n",
367 __func__,
368 bcmsdh_chipmatch(pdev->vendor, pdev->device) ?
369 "Found compatible SDIOHC" :
370 "Probing unknown device",
371 pdev->bus->number, PCI_SLOT(pdev->devfn),
372 pdev->vendor, pdev->device));
373 return -ENODEV;
375 SDLX_MSG(("%s: %s: bus %X, slot %X, vendor %X, device %X "
376 "(good PCI location)\n", __func__,
377 bcmsdh_chipmatch(pdev->vendor, pdev->device) ?
378 "Using compatible SDIOHC" : "WARNING, forced use "
379 "of unkown device",
380 pdev->bus->number, PCI_SLOT(pdev->devfn), pdev->vendor,
381 pdev->device));
384 if ((pdev->vendor == VENDOR_TI)
385 && ((pdev->device == PCIXX21_FLASHMEDIA_ID)
386 || (pdev->device == PCIXX21_FLASHMEDIA0_ID))) {
387 uint32 config_reg;
389 SDLX_MSG(("%s: Disabling TI FlashMedia Controller.\n",
390 __func__));
391 osh = osl_attach(pdev, PCI_BUS, FALSE);
392 if (!osh) {
393 SDLX_MSG(("%s: osl_attach failed\n", __func__));
394 goto err;
397 config_reg = OSL_PCI_READ_CONFIG(osh, 0x4c, 4);
400 * Set MMC_SD_DIS bit in FlashMedia Controller.
401 * Disbling the SD/MMC Controller in the FlashMedia Controller
402 * allows the Standard SD Host Controller to take over control
403 * of the SD Slot.
405 config_reg |= 0x02;
406 OSL_PCI_WRITE_CONFIG(osh, 0x4c, 4, config_reg);
407 osl_detach(osh);
409 /* match this pci device with what we support */
410 /* we can't solely rely on this to believe it is
411 our SDIO Host Controller! */
412 if (!bcmsdh_chipmatch(pdev->vendor, pdev->device))
413 return -ENODEV;
415 /* this is a pci device we might support */
416 SDLX_MSG(("%s: Found possible SDIO Host Controller: "
417 "bus %d slot %d func %d irq %d\n", __func__,
418 pdev->bus->number, PCI_SLOT(pdev->devfn),
419 PCI_FUNC(pdev->devfn), pdev->irq));
421 /* use bcmsdh_query_device() to get the vendor ID of the target device
422 * so it will eventually appear in the Broadcom string on the console
425 /* allocate SDIO Host Controller state info */
426 osh = osl_attach(pdev, PCI_BUS, FALSE);
427 if (!osh) {
428 SDLX_MSG(("%s: osl_attach failed\n", __func__));
429 goto err;
431 sdhc = MALLOC(osh, sizeof(bcmsdh_hc_t));
432 if (!sdhc) {
433 SDLX_MSG(("%s: out of memory, allocated %d bytes\n",
434 __func__, MALLOCED(osh)));
435 goto err;
437 bzero(sdhc, sizeof(bcmsdh_hc_t));
438 sdhc->osh = osh;
440 sdhc->dev = pdev;
442 /* map to address where host can access */
443 pci_set_master(pdev);
444 rc = pci_enable_device(pdev);
445 if (rc) {
446 SDLX_MSG(("%s: Cannot enable PCI device\n", __func__));
447 goto err;
449 sdh = bcmsdh_attach(osh, (void *)(uintptr) pci_resource_start(pdev, 0),
450 (void **)&regs, pdev->irq);
451 if (!sdh) {
452 SDLX_MSG(("%s: bcmsdh_attach failed\n", __func__));
453 goto err;
456 sdhc->sdh = sdh;
458 /* try to attach to the target device */
459 sdhc->ch = drvinfo.attach(VENDOR_BROADCOM, /* pdev->vendor, */
460 bcmsdh_query_device(sdh) & 0xFFFF, 0, 0, 0, 0,
461 (void *)regs, NULL, sdh);
462 if (!sdhc->ch) {
463 SDLX_MSG(("%s: device attach failed\n", __func__));
464 goto err;
467 /* chain SDIO Host Controller info together */
468 sdhc->next = sdhcinfo;
469 sdhcinfo = sdhc;
471 return 0;
473 /* error handling */
474 err:
475 if (sdhc->sdh)
476 bcmsdh_detach(sdhc->osh, sdhc->sdh);
477 if (sdhc)
478 MFREE(osh, sdhc, sizeof(bcmsdh_hc_t));
479 if (osh)
480 osl_detach(osh);
481 return -ENODEV;
485 * Detach from target devices and SDIO Host Controller
487 static void __devexit bcmsdh_pci_remove(struct pci_dev *pdev)
489 bcmsdh_hc_t *sdhc, *prev;
490 osl_t *osh;
492 /* find the SDIO Host Controller state for this
493 pdev and take it out from the list */
494 for (sdhc = sdhcinfo, prev = NULL; sdhc; sdhc = sdhc->next) {
495 if (sdhc->dev == pdev) {
496 if (prev)
497 prev->next = sdhc->next;
498 else
499 sdhcinfo = NULL;
500 break;
502 prev = sdhc;
504 if (!sdhc)
505 return;
507 drvinfo.detach(sdhc->ch);
509 bcmsdh_detach(sdhc->osh, sdhc->sdh);
511 /* release SDIO Host Controller info */
512 osh = sdhc->osh;
513 MFREE(osh, sdhc, sizeof(bcmsdh_hc_t));
514 osl_detach(osh);
516 #endif /* BCMLXSDMMC */
517 #endif /* BCMPLATFORM_BUS */
519 extern int sdio_function_init(void);
521 int bcmsdh_register(bcmsdh_driver_t *driver)
523 int error = 0;
525 drvinfo = *driver;
527 #if defined(BCMPLATFORM_BUS)
528 #if defined(BCMLXSDMMC)
529 SDLX_MSG(("Linux Kernel SDIO/MMC Driver\n"));
530 error = sdio_function_init();
531 #else
532 SDLX_MSG(("Intel PXA270 SDIO Driver\n"));
533 error = driver_register(&bcmsdh_driver);
534 #endif /* defined(BCMLXSDMMC) */
535 return error;
536 #endif /* defined(BCMPLATFORM_BUS) */
538 #if !defined(BCMPLATFORM_BUS) && !defined(BCMLXSDMMC)
539 error = pci_register_driver(&bcmsdh_pci_driver);
540 if (!error)
541 return 0;
543 SDLX_MSG(("%s: pci_module_init failed 0x%x\n", __func__, error));
544 #endif /* BCMPLATFORM_BUS */
546 return error;
549 extern void sdio_function_cleanup(void);
551 void bcmsdh_unregister(void)
553 #if defined(BCMPLATFORM_BUS) && !defined(BCMLXSDMMC)
554 driver_unregister(&bcmsdh_driver);
555 #endif
556 #if defined(BCMLXSDMMC)
557 sdio_function_cleanup();
558 #endif /* BCMLXSDMMC */
559 #if !defined(BCMPLATFORM_BUS) && !defined(BCMLXSDMMC)
560 pci_unregister_driver(&bcmsdh_pci_driver);
561 #endif /* BCMPLATFORM_BUS */
564 #if defined(OOB_INTR_ONLY)
565 void bcmsdh_oob_intr_set(bool enable)
567 static bool curstate = 1;
568 unsigned long flags;
570 spin_lock_irqsave(&sdhcinfo->irq_lock, flags);
571 if (curstate != enable) {
572 if (enable)
573 enable_irq(sdhcinfo->oob_irq);
574 else
575 disable_irq_nosync(sdhcinfo->oob_irq);
576 curstate = enable;
578 spin_unlock_irqrestore(&sdhcinfo->irq_lock, flags);
581 static irqreturn_t wlan_oob_irq(int irq, void *dev_id)
583 dhd_pub_t *dhdp;
585 dhdp = (dhd_pub_t *) dev_get_drvdata(sdhcinfo->dev);
587 bcmsdh_oob_intr_set(0);
589 if (dhdp == NULL) {
590 SDLX_MSG(("Out of band GPIO interrupt fired way too early\n"));
591 return IRQ_HANDLED;
594 WAKE_LOCK_TIMEOUT(dhdp, WAKE_LOCK_TMOUT, 25);
596 dhdsdio_isr((void *)dhdp->bus);
598 return IRQ_HANDLED;
601 int bcmsdh_register_oob_intr(void *dhdp)
603 int error = 0;
605 SDLX_MSG(("%s Enter\n", __func__));
607 sdhcinfo->oob_flags =
608 IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL |
609 IORESOURCE_IRQ_SHAREABLE;
610 dev_set_drvdata(sdhcinfo->dev, dhdp);
612 if (!sdhcinfo->oob_irq_registered) {
613 SDLX_MSG(("%s IRQ=%d Type=%X\n", __func__,
614 (int)sdhcinfo->oob_irq, (int)sdhcinfo->oob_flags));
615 /* Refer to customer Host IRQ docs about
616 proper irqflags definition */
617 error =
618 request_irq(sdhcinfo->oob_irq, wlan_oob_irq,
619 sdhcinfo->oob_flags, "bcmsdh_sdmmc", NULL);
620 if (error)
621 return -ENODEV;
623 set_irq_wake(sdhcinfo->oob_irq, 1);
624 sdhcinfo->oob_irq_registered = TRUE;
627 return 0;
630 void bcmsdh_unregister_oob_intr(void)
632 SDLX_MSG(("%s: Enter\n", __func__));
634 set_irq_wake(sdhcinfo->oob_irq, 0);
635 disable_irq(sdhcinfo->oob_irq); /* just in case.. */
636 free_irq(sdhcinfo->oob_irq, NULL);
637 sdhcinfo->oob_irq_registered = FALSE;
639 #endif /* defined(OOB_INTR_ONLY) */
640 /* Module parameters specific to each host-controller driver */
642 extern uint sd_msglevel; /* Debug message level */
643 module_param(sd_msglevel, uint, 0);
645 extern uint sd_power; /* 0 = SD Power OFF,
646 1 = SD Power ON. */
647 module_param(sd_power, uint, 0);
649 extern uint sd_clock; /* SD Clock Control, 0 = SD Clock OFF,
650 1 = SD Clock ON */
651 module_param(sd_clock, uint, 0);
653 extern uint sd_divisor; /* Divisor (-1 means external clock) */
654 module_param(sd_divisor, uint, 0);
656 extern uint sd_sdmode; /* Default is SD4, 0=SPI, 1=SD1, 2=SD4 */
657 module_param(sd_sdmode, uint, 0);
659 extern uint sd_hiok; /* Ok to use hi-speed mode */
660 module_param(sd_hiok, uint, 0);
662 extern uint sd_f2_blocksize;
663 module_param(sd_f2_blocksize, int, 0);