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