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