Merge branch 'sh-latest' of git://git.kernel.org/pub/scm/linux/kernel/git/lethal...
[pandora-kernel.git] / drivers / staging / brcm80211 / brcmfmac / bcmsdh_linux.c
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
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.
7  *
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.
15  */
16
17 /**
18  * @file bcmsdh_linux.c
19  */
20
21 #define __UNDEF_NO_VERSION__
22
23 #include <linux/netdevice.h>
24 #include <linux/pci.h>
25 #include <linux/completion.h>
26
27 #include <pcicfg.h>
28 #include <bcmdefs.h>
29 #include <bcmdevs.h>
30 #include <bcmutils.h>
31
32 #if defined(OOB_INTR_ONLY)
33 #include <linux/irq.h>
34 extern void dhdsdio_isr(void *args);
35 #include <dngl_stats.h>
36 #include <dhd.h>
37 #endif                          /* defined(OOB_INTR_ONLY) */
38 #if defined(CONFIG_MACH_SANDGATE2G) || defined(CONFIG_MACH_LOGICPD_PXA270)
39 #if !defined(BCMPLATFORM_BUS)
40 #define BCMPLATFORM_BUS
41 #endif                          /* !defined(BCMPLATFORM_BUS) */
42
43 #include <linux/platform_device.h>
44 #endif                          /* CONFIG_MACH_SANDGATE2G */
45
46 #include "dngl_stats.h"
47 #include "dhd.h"
48
49 /**
50  * SDIO Host Controller info
51  */
52 typedef struct bcmsdh_hc bcmsdh_hc_t;
53
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         void *regs;             /* SDIO Host Controller address */
62         bcmsdh_info_t *sdh;     /* SDIO Host Controller handle */
63         void *ch;
64         unsigned int oob_irq;
65         unsigned long oob_flags;        /* OOB Host specifiction
66                                         as edge and etc */
67         bool oob_irq_registered;
68 #if defined(OOB_INTR_ONLY)
69         spinlock_t irq_lock;
70 #endif
71 };
72 static bcmsdh_hc_t *sdhcinfo;
73
74 /* driver info, initialized when bcmsdh_register is called */
75 static bcmsdh_driver_t drvinfo = { NULL, NULL };
76
77 /* debugging macros */
78 #define SDLX_MSG(x)
79
80 /**
81  * Checks to see if vendor and device IDs match a supported SDIO Host Controller.
82  */
83 bool bcmsdh_chipmatch(u16 vendor, u16 device)
84 {
85         /* Add other vendors and devices as required */
86
87 #ifdef BCMSDIOH_STD
88         /* Check for Arasan host controller */
89         if (vendor == VENDOR_SI_IMAGE)
90                 return true;
91
92         /* Check for BRCM 27XX Standard host controller */
93         if (device == BCM27XX_SDIOH_ID && vendor == PCI_VENDOR_ID_BROADCOM)
94                 return true;
95
96         /* Check for BRCM Standard host controller */
97         if (device == SDIOH_FPGA_ID && vendor == PCI_VENDOR_ID_BROADCOM)
98                 return true;
99
100         /* Check for TI PCIxx21 Standard host controller */
101         if (device == PCIXX21_SDIOH_ID && vendor == VENDOR_TI)
102                 return true;
103
104         if (device == PCIXX21_SDIOH0_ID && vendor == VENDOR_TI)
105                 return true;
106
107         /* Ricoh R5C822 Standard SDIO Host */
108         if (device == R5C822_SDIOH_ID && vendor == VENDOR_RICOH)
109                 return true;
110
111         /* JMicron Standard SDIO Host */
112         if (device == JMICRON_SDIOH_ID && vendor == VENDOR_JMICRON)
113                 return true;
114 #endif                          /* BCMSDIOH_STD */
115 #ifdef BCMSDIOH_SPI
116         /* This is the PciSpiHost. */
117         if (device == SPIH_FPGA_ID && vendor == PCI_VENDOR_ID_BROADCOM) {
118                 return true;
119         }
120 #endif                          /* BCMSDIOH_SPI */
121
122         return false;
123 }
124
125 #if defined(BCMPLATFORM_BUS)
126 #if defined(BCMLXSDMMC)
127 /* forward declarations */
128 int bcmsdh_probe(struct device *dev);
129 EXPORT_SYMBOL(bcmsdh_probe);
130
131 int bcmsdh_remove(struct device *dev);
132 EXPORT_SYMBOL(bcmsdh_remove);
133
134 #else
135 /* forward declarations */
136 static int __devinit bcmsdh_probe(struct device *dev);
137 static int __devexit bcmsdh_remove(struct device *dev);
138 #endif                          /* BCMLXSDMMC */
139
140 #ifndef BCMLXSDMMC
141 static
142 #endif                          /* BCMLXSDMMC */
143 int bcmsdh_probe(struct device *dev)
144 {
145         bcmsdh_hc_t *sdhc = NULL;
146         unsigned long regs = 0;
147         bcmsdh_info_t *sdh = NULL;
148 #if !defined(BCMLXSDMMC) && defined(BCMPLATFORM_BUS)
149         struct platform_device *pdev;
150         struct resource *r;
151 #endif                          /* BCMLXSDMMC */
152         int irq = 0;
153         u32 vendevid;
154         unsigned long irq_flags = 0;
155
156 #if !defined(BCMLXSDMMC) && defined(BCMPLATFORM_BUS)
157         pdev = to_platform_device(dev);
158         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
159         irq = platform_get_irq(pdev, 0);
160         if (!r || irq == NO_IRQ)
161                 return -ENXIO;
162 #endif                          /* BCMLXSDMMC */
163
164 #if defined(OOB_INTR_ONLY)
165 #ifdef HW_OOB
166         irq_flags =
167             IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL |
168             IORESOURCE_IRQ_SHAREABLE;
169 #else
170         irq_flags = IRQF_TRIGGER_FALLING;
171 #endif                          /* HW_OOB */
172         irq = dhd_customer_oob_irq_map(&irq_flags);
173         if (irq < 0) {
174                 SDLX_MSG(("%s: Host irq is not defined\n", __func__));
175                 return 1;
176         }
177 #endif                          /* defined(OOB_INTR_ONLY) */
178         /* allocate SDIO Host Controller state info */
179         sdhc = kzalloc(sizeof(bcmsdh_hc_t), GFP_ATOMIC);
180         if (!sdhc) {
181                 SDLX_MSG(("%s: out of memory\n", __func__));
182                 goto err;
183         }
184         sdhc->dev = (void *)dev;
185
186 #ifdef BCMLXSDMMC
187         sdh = bcmsdh_attach((void *)0, (void **)&regs, irq);
188         if (!sdh) {
189                 SDLX_MSG(("%s: bcmsdh_attach failed\n", __func__));
190                 goto err;
191         }
192 #else
193         sdh = bcmsdh_attach((void *)r->start, (void **)&regs, irq);
194         if (!sdh) {
195                 SDLX_MSG(("%s: bcmsdh_attach failed\n", __func__));
196                 goto err;
197         }
198 #endif                          /* BCMLXSDMMC */
199         sdhc->sdh = sdh;
200         sdhc->oob_irq = irq;
201         sdhc->oob_flags = irq_flags;
202         sdhc->oob_irq_registered = false;       /* to make sure.. */
203 #if defined(OOB_INTR_ONLY)
204         spin_lock_init(&sdhc->irq_lock);
205 #endif
206
207         /* chain SDIO Host Controller info together */
208         sdhc->next = sdhcinfo;
209         sdhcinfo = sdhc;
210         /* Read the vendor/device ID from the CIS */
211         vendevid = bcmsdh_query_device(sdh);
212
213         /* try to attach to the target device */
214         sdhc->ch = drvinfo.attach((vendevid >> 16), (vendevid & 0xFFFF),
215                                   0, 0, 0, 0, (void *)regs, sdh);
216         if (!sdhc->ch) {
217                 SDLX_MSG(("%s: device attach failed\n", __func__));
218                 goto err;
219         }
220
221         return 0;
222
223         /* error handling */
224 err:
225         if (sdhc) {
226                 if (sdhc->sdh)
227                         bcmsdh_detach(sdhc->sdh);
228                 kfree(sdhc);
229         }
230
231         return -ENODEV;
232 }
233
234 #ifndef BCMLXSDMMC
235 static
236 #endif                          /* BCMLXSDMMC */
237 int bcmsdh_remove(struct device *dev)
238 {
239         bcmsdh_hc_t *sdhc, *prev;
240
241         sdhc = sdhcinfo;
242         drvinfo.detach(sdhc->ch);
243         bcmsdh_detach(sdhc->sdh);
244         /* find the SDIO Host Controller state for this pdev
245                  and take it out from the list */
246         for (sdhc = sdhcinfo, prev = NULL; sdhc; sdhc = sdhc->next) {
247                 if (sdhc->dev == (void *)dev) {
248                         if (prev)
249                                 prev->next = sdhc->next;
250                         else
251                                 sdhcinfo = NULL;
252                         break;
253                 }
254                 prev = sdhc;
255         }
256         if (!sdhc) {
257                 SDLX_MSG(("%s: failed\n", __func__));
258                 return 0;
259         }
260
261         /* release SDIO Host Controller info */
262         kfree(sdhc);
263
264 #if !defined(BCMLXSDMMC)
265         dev_set_drvdata(dev, NULL);
266 #endif                          /* !defined(BCMLXSDMMC) */
267
268         return 0;
269 }
270 #endif                          /* BCMPLATFORM_BUS */
271
272 extern int sdio_function_init(void);
273
274 int bcmsdh_register(bcmsdh_driver_t *driver)
275 {
276         drvinfo = *driver;
277
278         SDLX_MSG(("Linux Kernel SDIO/MMC Driver\n"));
279         return sdio_function_init();
280 }
281
282 extern void sdio_function_cleanup(void);
283
284 void bcmsdh_unregister(void)
285 {
286         sdio_function_cleanup();
287 }
288
289 #if defined(OOB_INTR_ONLY)
290 void bcmsdh_oob_intr_set(bool enable)
291 {
292         static bool curstate = 1;
293         unsigned long flags;
294
295         spin_lock_irqsave(&sdhcinfo->irq_lock, flags);
296         if (curstate != enable) {
297                 if (enable)
298                         enable_irq(sdhcinfo->oob_irq);
299                 else
300                         disable_irq_nosync(sdhcinfo->oob_irq);
301                 curstate = enable;
302         }
303         spin_unlock_irqrestore(&sdhcinfo->irq_lock, flags);
304 }
305
306 static irqreturn_t wlan_oob_irq(int irq, void *dev_id)
307 {
308         dhd_pub_t *dhdp;
309
310         dhdp = (dhd_pub_t *) dev_get_drvdata(sdhcinfo->dev);
311
312         bcmsdh_oob_intr_set(0);
313
314         if (dhdp == NULL) {
315                 SDLX_MSG(("Out of band GPIO interrupt fired way too early\n"));
316                 return IRQ_HANDLED;
317         }
318
319         dhdsdio_isr((void *)dhdp->bus);
320
321         return IRQ_HANDLED;
322 }
323
324 int bcmsdh_register_oob_intr(void *dhdp)
325 {
326         int error = 0;
327
328         SDLX_MSG(("%s Enter\n", __func__));
329
330         sdhcinfo->oob_flags =
331             IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL |
332             IORESOURCE_IRQ_SHAREABLE;
333         dev_set_drvdata(sdhcinfo->dev, dhdp);
334
335         if (!sdhcinfo->oob_irq_registered) {
336                 SDLX_MSG(("%s IRQ=%d Type=%X\n", __func__,
337                           (int)sdhcinfo->oob_irq, (int)sdhcinfo->oob_flags));
338                 /* Refer to customer Host IRQ docs about
339                          proper irqflags definition */
340                 error =
341                     request_irq(sdhcinfo->oob_irq, wlan_oob_irq,
342                                 sdhcinfo->oob_flags, "bcmsdh_sdmmc", NULL);
343                 if (error)
344                         return -ENODEV;
345
346                 irq_set_irq_wake(sdhcinfo->oob_irq, 1);
347                 sdhcinfo->oob_irq_registered = true;
348         }
349
350         return 0;
351 }
352
353 void bcmsdh_unregister_oob_intr(void)
354 {
355         SDLX_MSG(("%s: Enter\n", __func__));
356
357         irq_set_irq_wake(sdhcinfo->oob_irq, 0);
358         disable_irq(sdhcinfo->oob_irq); /* just in case.. */
359         free_irq(sdhcinfo->oob_irq, NULL);
360         sdhcinfo->oob_irq_registered = false;
361 }
362 #endif                          /* defined(OOB_INTR_ONLY) */
363 /* Module parameters specific to each host-controller driver */
364
365 extern uint sd_msglevel;        /* Debug message level */
366 module_param(sd_msglevel, uint, 0);
367
368 extern uint sd_power;           /* 0 = SD Power OFF,
369                                          1 = SD Power ON. */
370 module_param(sd_power, uint, 0);
371
372 extern uint sd_clock;           /* SD Clock Control, 0 = SD Clock OFF,
373                                  1 = SD Clock ON */
374 module_param(sd_clock, uint, 0);
375
376 extern uint sd_divisor;         /* Divisor (-1 means external clock) */
377 module_param(sd_divisor, uint, 0);
378
379 extern uint sd_sdmode;          /* Default is SD4, 0=SPI, 1=SD1, 2=SD4 */
380 module_param(sd_sdmode, uint, 0);
381
382 extern uint sd_hiok;            /* Ok to use hi-speed mode */
383 module_param(sd_hiok, uint, 0);
384
385 extern uint sd_f2_blocksize;
386 module_param(sd_f2_blocksize, int, 0);