bcmdhd wifi:update driver 1.201.59.5
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / rkwifi / bcmdhd / bcmsdh_sdmmc_linux.c
1 /*
2  * BCMSDH Function Driver for the native SDIO/MMC driver in the Linux Kernel
3  *
4  * Copyright (C) 1999-2014, Broadcom Corporation
5  * 
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:
11  * 
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.
19  * 
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.
23  *
24  * $Id: bcmsdh_sdmmc_linux.c 434777 2013-11-07 09:30:27Z $
25  */
26
27 #include <typedefs.h>
28 #include <bcmutils.h>
29 #include <sdio.h>       /* SDIO Device and Protocol Specs */
30 #include <bcmsdbus.h>   /* bcmsdh to/from specific controller APIs */
31 #include <sdiovar.h>    /* to get msglevel bit values */
32
33 #include <linux/sched.h>        /* request_irq() */
34
35 #include <linux/mmc/core.h>
36 #include <linux/mmc/card.h>
37 #include <linux/mmc/host.h>
38 #include <linux/mmc/sdio_func.h>
39 #include <linux/mmc/sdio_ids.h>
40 #include <dhd_linux.h>
41 #include <bcmsdh_sdmmc.h>
42 #include <dhd_dbg.h>
43
44 #if !defined(SDIO_VENDOR_ID_BROADCOM)
45 #define SDIO_VENDOR_ID_BROADCOM         0x02d0
46 #endif /* !defined(SDIO_VENDOR_ID_BROADCOM) */
47
48 #define SDIO_DEVICE_ID_BROADCOM_DEFAULT 0x0000
49
50 #if !defined(SDIO_DEVICE_ID_BROADCOM_4325_SDGWB)
51 #define SDIO_DEVICE_ID_BROADCOM_4325_SDGWB      0x0492  /* BCM94325SDGWB */
52 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4325_SDGWB) */
53 #if !defined(SDIO_DEVICE_ID_BROADCOM_4325)
54 #define SDIO_DEVICE_ID_BROADCOM_4325    0x0493
55 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4325) */
56 #if !defined(SDIO_DEVICE_ID_BROADCOM_4329)
57 #define SDIO_DEVICE_ID_BROADCOM_4329    0x4329
58 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4329) */
59 #if !defined(SDIO_DEVICE_ID_BROADCOM_4319)
60 #define SDIO_DEVICE_ID_BROADCOM_4319    0x4319
61 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4319) */
62 #if !defined(SDIO_DEVICE_ID_BROADCOM_4330)
63 #define SDIO_DEVICE_ID_BROADCOM_4330    0x4330
64 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4330) */
65 #if !defined(SDIO_DEVICE_ID_BROADCOM_4334)
66 #define SDIO_DEVICE_ID_BROADCOM_4334    0x4334
67 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4334) */
68 #if !defined(SDIO_DEVICE_ID_BROADCOM_4324)
69 #define SDIO_DEVICE_ID_BROADCOM_4324    0x4324
70 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4324) */
71 #if !defined(SDIO_DEVICE_ID_BROADCOM_43239)
72 #define SDIO_DEVICE_ID_BROADCOM_43239    43239
73 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_43239) */
74
75 extern void wl_cfg80211_set_parent_dev(void *dev);
76 extern void sdioh_sdmmc_devintr_off(sdioh_info_t *sd);
77 extern void sdioh_sdmmc_devintr_on(sdioh_info_t *sd);
78 extern void* bcmsdh_probe(osl_t *osh, void *dev, void *sdioh, void *adapter_info, uint bus_type,
79         uint bus_num, uint slot_num);
80 extern int bcmsdh_remove(bcmsdh_info_t *bcmsdh);
81
82 int sdio_function_init(void);
83 void sdio_function_cleanup(void);
84
85 #define DESCRIPTION "bcmsdh_sdmmc Driver"
86 #define AUTHOR "Broadcom Corporation"
87
88 /* module param defaults */
89 static int clockoverride = 0;
90
91 module_param(clockoverride, int, 0644);
92 MODULE_PARM_DESC(clockoverride, "SDIO card clock override");
93
94 #ifdef GLOBAL_SDMMC_INSTANCE
95 PBCMSDH_SDMMC_INSTANCE gInstance;
96 #endif
97
98 /* Maximum number of bcmsdh_sdmmc devices supported by driver */
99 #define BCMSDH_SDMMC_MAX_DEVICES 1
100
101 extern volatile bool dhd_mmc_suspend;
102
103 static int sdioh_probe(struct sdio_func *func)
104 {
105         int host_idx = func->card->host->index;
106         uint32 rca = func->card->rca;
107         wifi_adapter_info_t *adapter;
108         osl_t *osh = NULL;
109         sdioh_info_t *sdioh = NULL;
110
111         sd_err(("bus num (host idx)=%d, slot num (rca)=%d\n", host_idx, rca));
112         adapter = dhd_wifi_platform_get_adapter(SDIO_BUS, host_idx, rca);
113         if (adapter  != NULL)
114                 sd_err(("found adapter info '%s'\n", adapter->name));
115         else
116                 sd_err(("can't find adapter info for this chip\n"));
117
118 #ifdef WL_CFG80211
119         wl_cfg80211_set_parent_dev(&func->dev);
120 #endif
121
122          /* allocate SDIO Host Controller state info */
123          osh = osl_attach(&func->dev, SDIO_BUS, TRUE);
124          if (osh == NULL) {
125                  sd_err(("%s: osl_attach failed\n", __FUNCTION__));
126                  goto fail;
127          }
128          osl_static_mem_init(osh, adapter);
129          sdioh = sdioh_attach(osh, func);
130          if (sdioh == NULL) {
131                  sd_err(("%s: sdioh_attach failed\n", __FUNCTION__));
132                  goto fail;
133          }
134          sdioh->bcmsdh = bcmsdh_probe(osh, &func->dev, sdioh, adapter, SDIO_BUS, host_idx, rca);
135          if (sdioh->bcmsdh == NULL) {
136                  sd_err(("%s: bcmsdh_probe failed\n", __FUNCTION__));
137                  goto fail;
138          }
139
140         sdio_set_drvdata(func, sdioh);
141         return 0;
142
143 fail:
144         if (sdioh != NULL)
145                 sdioh_detach(osh, sdioh);
146         if (osh != NULL)
147                 osl_detach(osh);
148         return -ENOMEM;
149 }
150
151 static void sdioh_remove(struct sdio_func *func)
152 {
153         sdioh_info_t *sdioh;
154         osl_t *osh;
155
156         sdioh = sdio_get_drvdata(func);
157         if (sdioh == NULL) {
158                 sd_err(("%s: error, no sdioh handler found\n", __FUNCTION__));
159                 return;
160         }
161         sd_err(("%s: Enter\n", __FUNCTION__));
162
163         osh = sdioh->osh;
164         bcmsdh_remove(sdioh->bcmsdh);
165         sdioh_detach(osh, sdioh);
166         osl_detach(osh);
167 }
168
169 static int bcmsdh_sdmmc_probe(struct sdio_func *func,
170                               const struct sdio_device_id *id)
171 {
172         int ret = 0;
173
174         if (func == NULL)
175                 return -EINVAL;
176
177         sd_err(("bcmsdh_sdmmc: %s Enter\n", __FUNCTION__));
178         sd_info(("sdio_bcmsdh: func->class=%x\n", func->class));
179         sd_info(("sdio_vendor: 0x%04x\n", func->vendor));
180         sd_info(("sdio_device: 0x%04x\n", func->device));
181         sd_info(("Function#: 0x%04x\n", func->num));
182
183 #ifdef GLOBAL_SDMMC_INSTANCE
184         gInstance->func[func->num] = func;
185 #endif
186
187         /* 4318 doesn't have function 2 */
188         if ((func->num == 2) || (func->num == 1 && func->device == 0x4))
189                 ret = sdioh_probe(func);
190
191         return ret;
192 }
193
194 static void bcmsdh_sdmmc_remove(struct sdio_func *func)
195 {
196         if (func == NULL) {
197                 sd_err(("%s is called with NULL SDIO function pointer\n", __FUNCTION__));
198                 return;
199         }
200
201         sd_trace(("bcmsdh_sdmmc: %s Enter\n", __FUNCTION__));
202         sd_info(("sdio_bcmsdh: func->class=%x\n", func->class));
203         sd_info(("sdio_vendor: 0x%04x\n", func->vendor));
204         sd_info(("sdio_device: 0x%04x\n", func->device));
205         sd_info(("Function#: 0x%04x\n", func->num));
206
207         if ((func->num == 2) || (func->num == 1 && func->device == 0x4))
208                 sdioh_remove(func);
209 }
210
211 /* devices we support, null terminated */
212 static const struct sdio_device_id bcmsdh_sdmmc_ids[] = {
213         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_DEFAULT) },
214         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4325_SDGWB) },
215         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4325) },
216         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4329) },
217         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4319) },
218         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4330) },
219         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4334) },
220         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4324) },
221         { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43239) },
222         { SDIO_DEVICE_CLASS(SDIO_CLASS_NONE)            },
223         { /* end: all zeroes */                         },
224 };
225
226 MODULE_DEVICE_TABLE(sdio, bcmsdh_sdmmc_ids);
227
228 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM)
229 static int bcmsdh_sdmmc_suspend(struct device *pdev)
230 {
231         int err;
232         sdioh_info_t *sdioh;
233         struct sdio_func *func = dev_to_sdio_func(pdev);
234         mmc_pm_flag_t sdio_flags;
235
236         printf("%s Enter func->num=%d\n", __FUNCTION__, func->num);
237         if (func->num != 2)
238                 return 0;
239
240         sdioh = sdio_get_drvdata(func);
241         err = bcmsdh_suspend(sdioh->bcmsdh);
242         if (err) {
243                 printf("%s bcmsdh_suspend err=%d\n", __FUNCTION__, err);
244                 return err;
245         }
246
247         sdio_flags = sdio_get_host_pm_caps(func);
248         if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
249                 sd_err(("%s: can't keep power while host is suspended\n", __FUNCTION__));
250                 return  -EINVAL;
251         }
252
253         /* keep power while host suspended */
254         err = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
255         if (err) {
256                 sd_err(("%s: error while trying to keep power\n", __FUNCTION__));
257                 return err;
258         }
259 #if defined(OOB_INTR_ONLY)
260         bcmsdh_oob_intr_set(sdioh->bcmsdh, FALSE);
261 #endif
262         dhd_mmc_suspend = TRUE;
263         smp_mb();
264
265         printf("%s Exit\n", __FUNCTION__);
266         return 0;
267 }
268
269 static int bcmsdh_sdmmc_resume(struct device *pdev)
270 {
271 #if defined(OOB_INTR_ONLY)
272         sdioh_info_t *sdioh;
273 #endif
274         struct sdio_func *func = dev_to_sdio_func(pdev);
275
276         printf("%s Enter func->num=%d\n", __FUNCTION__, func->num);
277         if (func->num != 2)
278                 return 0;
279
280         dhd_mmc_suspend = FALSE;
281 #if defined(OOB_INTR_ONLY)
282         sdioh = sdio_get_drvdata(func);
283         bcmsdh_resume(sdioh->bcmsdh);
284 #endif
285
286         smp_mb();
287         printf("%s Exit\n", __FUNCTION__);
288         return 0;
289 }
290
291 static const struct dev_pm_ops bcmsdh_sdmmc_pm_ops = {
292         .suspend        = bcmsdh_sdmmc_suspend,
293         .resume         = bcmsdh_sdmmc_resume,
294 };
295 #endif  /* (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM) */
296
297 #if defined(BCMLXSDMMC)
298 static struct semaphore *notify_semaphore = NULL;
299
300 static int dummy_probe(struct sdio_func *func,
301                               const struct sdio_device_id *id)
302 {
303         if (func && (func->num != 2)) {
304                 return 0;
305         }
306
307         if (notify_semaphore)
308                 up(notify_semaphore);
309         return 0;
310 }
311
312 static void dummy_remove(struct sdio_func *func)
313 {
314 }
315
316 static struct sdio_driver dummy_sdmmc_driver = {
317         .probe          = dummy_probe,
318         .remove         = dummy_remove,
319         .name           = "dummy_sdmmc",
320         .id_table       = bcmsdh_sdmmc_ids,
321         };
322
323 int sdio_func_reg_notify(void* semaphore)
324 {
325         notify_semaphore = semaphore;
326         return sdio_register_driver(&dummy_sdmmc_driver);
327 }
328
329 void sdio_func_unreg_notify(void)
330 {
331         OSL_SLEEP(15);
332         sdio_unregister_driver(&dummy_sdmmc_driver);
333 }
334
335 #endif /* defined(BCMLXSDMMC) */
336
337 static struct sdio_driver bcmsdh_sdmmc_driver = {
338         .probe          = bcmsdh_sdmmc_probe,
339         .remove         = bcmsdh_sdmmc_remove,
340         .name           = "bcmsdh_sdmmc",
341         .id_table       = bcmsdh_sdmmc_ids,
342 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM)
343         .drv = {
344         .pm     = &bcmsdh_sdmmc_pm_ops,
345         },
346 #endif /* (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM) */
347 };
348
349 struct sdos_info {
350         sdioh_info_t *sd;
351         spinlock_t lock;
352 };
353
354 /* Interrupt enable/disable */
355 SDIOH_API_RC
356 sdioh_interrupt_set(sdioh_info_t *sd, bool enable)
357 {
358         if (!sd)
359                 return BCME_BADARG;
360
361         sd_trace(("%s: %s\n", __FUNCTION__, enable ? "Enabling" : "Disabling"));
362         return SDIOH_API_RC_SUCCESS;
363 }
364
365 #ifdef BCMSDH_MODULE
366 static int __init
367 bcmsdh_module_init(void)
368 {
369         int error = 0;
370         error = sdio_function_init();
371         return error;
372 }
373
374 static void __exit
375 bcmsdh_module_cleanup(void)
376 {
377         sdio_function_cleanup();
378 }
379
380 module_init(bcmsdh_module_init);
381 module_exit(bcmsdh_module_cleanup);
382
383 MODULE_LICENSE("GPL v2");
384 MODULE_DESCRIPTION(DESCRIPTION);
385 MODULE_AUTHOR(AUTHOR);
386
387 #endif /* BCMSDH_MODULE */
388 /*
389  * module init
390 */
391 int bcmsdh_register_client_driver(void)
392 {
393 #ifdef GLOBAL_SDMMC_INSTANCE
394         gInstance = kzalloc(sizeof(BCMSDH_SDMMC_INSTANCE), GFP_KERNEL);
395         if (!gInstance)
396                 return -ENOMEM;
397 #endif
398
399         return sdio_register_driver(&bcmsdh_sdmmc_driver);
400 }
401
402 /*
403  * module cleanup
404 */
405 void bcmsdh_unregister_client_driver(void)
406 {
407         sdio_unregister_driver(&bcmsdh_sdmmc_driver);
408 #ifdef GLOBAL_SDMMC_INSTANCE
409         if (gInstance)
410                 kfree(gInstance);
411 #endif
412 }