1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * BCMSDH Function Driver for the native SDIO/MMC driver in the Linux Kernel
4 *
5 * Copyright (C) 1999-2019, Broadcom.
6 *
7 * Unless you and Broadcom execute a separate written software license
8 * agreement governing use of this software, this software is licensed to you
9 * under the terms of the GNU General Public License version 2 (the "GPL"),
10 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
11 * following added to such license:
12 *
13 * As a special exception, the copyright holders of this software give you
14 * permission to link this software with independent modules, and to copy and
15 * distribute the resulting executable under terms of your choice, provided that
16 * you also meet, for each linked independent module, the terms and conditions of
17 * the license of that module. An independent module is a module which is not
18 * derived from this software. The special exception does not apply to any
19 * modifications of the software.
20 *
21 * Notwithstanding the above, under no circumstances may you combine this
22 * software in any way with any other Broadcom software provided under a license
23 * other than the GPL, without Broadcom's express prior written consent.
24 *
25 *
26 * <<Broadcom-WL-IPTag/Proprietary,Open:>>
27 *
28 * $Id: bcmsdh_sdmmc_linux.c 825481 2019-06-14 10:06:03Z $
29 */
30
31 #include <typedefs.h>
32 #include <bcmutils.h>
33 #include <sdio.h> /* SDIO Device and Protocol Specs */
34 #include <bcmsdbus.h> /* bcmsdh to/from specific controller APIs */
35 #include <sdiovar.h> /* to get msglevel bit values */
36
37 #include <linux/sched.h> /* request_irq() */
38
39 #include <linux/mmc/core.h>
40 #include <linux/mmc/card.h>
41 #include <linux/mmc/host.h>
42 #include <linux/mmc/sdio_func.h>
43 #include <linux/mmc/sdio_ids.h>
44 #include <dhd_linux.h>
45 #include <bcmsdh_sdmmc.h>
46 #include <dhd_dbg.h>
47 #include <bcmdevs.h>
48
49 #if !defined(SDIO_VENDOR_ID_BROADCOM)
50 #define SDIO_VENDOR_ID_BROADCOM 0x02d0
51 #endif /* !defined(SDIO_VENDOR_ID_BROADCOM) */
52
53 #define SDIO_DEVICE_ID_BROADCOM_DEFAULT 0x0000
54
55 extern void wl_cfg80211_set_parent_dev(void *dev);
56 extern void sdioh_sdmmc_devintr_off(sdioh_info_t *sd);
57 extern void sdioh_sdmmc_devintr_on(sdioh_info_t *sd);
58 extern void* bcmsdh_probe(osl_t *osh, void *dev, void *sdioh, void *adapter_info, uint bus_type,
59 uint bus_num, 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->sdio_func = func;
96 } else
97 sd_err(("can't find adapter info for this chip\n"));
98
99 #ifdef WL_CFG80211
100 wl_cfg80211_set_parent_dev(&func->dev);
101 #endif // endif
102
103 /* allocate SDIO Host Controller state info */
104 osh = osl_attach(&func->dev, SDIO_BUS, TRUE);
105 if (osh == NULL) {
106 sd_err(("%s: osl_attach failed\n", __FUNCTION__));
107 goto fail;
108 }
109 osl_static_mem_init(osh, adapter);
110 sdioh = sdioh_attach(osh, func);
111 if (sdioh == NULL) {
112 sd_err(("%s: sdioh_attach failed\n", __FUNCTION__));
113 goto fail;
114 }
115 sdioh->bcmsdh = bcmsdh_probe(osh, &func->dev, sdioh, adapter, SDIO_BUS, host_idx, rca);
116 if (sdioh->bcmsdh == NULL) {
117 sd_err(("%s: bcmsdh_probe failed\n", __FUNCTION__));
118 goto fail;
119 }
120
121 sdio_set_drvdata(func, sdioh);
122 return 0;
123
124 fail:
125 if (sdioh != NULL)
126 sdioh_detach(osh, sdioh);
127 if (osh != NULL)
128 osl_detach(osh);
129 return -ENOMEM;
130 }
131
sdioh_remove(struct sdio_func * func)132 static void sdioh_remove(struct sdio_func *func)
133 {
134 sdioh_info_t *sdioh;
135 osl_t *osh;
136
137 sdioh = sdio_get_drvdata(func);
138 if (sdioh == NULL) {
139 sd_err(("%s: error, no sdioh handler found\n", __FUNCTION__));
140 return;
141 }
142 sd_err(("%s: Enter\n", __FUNCTION__));
143
144 osh = sdioh->osh;
145 bcmsdh_remove(sdioh->bcmsdh);
146 sdioh_detach(osh, sdioh);
147 osl_detach(osh);
148 }
149
bcmsdh_sdmmc_probe(struct sdio_func * func,const struct sdio_device_id * id)150 static int bcmsdh_sdmmc_probe(struct sdio_func *func,
151 const struct sdio_device_id *id)
152 {
153 int ret = 0;
154
155 if (func == NULL)
156 return -EINVAL;
157
158 sd_err(("%s: Enter num=%d\n", __FUNCTION__, func->num));
159 sd_info(("sdio_bcmsdh: func->class=%x\n", func->class));
160 sd_info(("sdio_vendor: 0x%04x\n", func->vendor));
161 sd_info(("sdio_device: 0x%04x\n", func->device));
162 sd_info(("Function#: 0x%04x\n", func->num));
163
164 #ifdef GLOBAL_SDMMC_INSTANCE
165 gInstance->func[func->num] = func;
166 #endif
167
168 /* 4318 doesn't have function 2 */
169 if ((func->num == 2) || (func->num == 1 && func->device == 0x4))
170 ret = sdioh_probe(func);
171
172 return ret;
173 }
174
bcmsdh_sdmmc_remove(struct sdio_func * func)175 static void bcmsdh_sdmmc_remove(struct sdio_func *func)
176 {
177 if (func == NULL) {
178 sd_err(("%s is called with NULL SDIO function pointer\n", __FUNCTION__));
179 return;
180 }
181
182 sd_trace(("bcmsdh_sdmmc: %s Enter\n", __FUNCTION__));
183 sd_info(("sdio_bcmsdh: func->class=%x\n", func->class));
184 sd_info(("sdio_vendor: 0x%04x\n", func->vendor));
185 sd_info(("sdio_device: 0x%04x\n", func->device));
186 sd_info(("Function#: 0x%04x\n", func->num));
187
188 if ((func->num == 2) || (func->num == 1 && func->device == 0x4))
189 sdioh_remove(func);
190 }
191
192 /* devices we support, null terminated */
193 static const struct sdio_device_id bcmsdh_sdmmc_ids[] = {
194 { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_DEFAULT) },
195 { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, BCM4362_CHIP_ID) },
196 { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, BCM43751_CHIP_ID) },
197 { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, BCM43752_CHIP_ID) },
198 { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, BCM43012_CHIP_ID) },
199 { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, BCM43014_CHIP_ID) },
200 { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, BCM43014_D11N_ID) },
201 { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, BCM43014_D11N2G_ID) },
202 { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, BCM43014_D11N5G_ID) },
203 /* { SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_ANY_ID) }, */
204 { SDIO_DEVICE_CLASS(SDIO_CLASS_NONE) },
205 /* end: all zeroes */
206 { 0, 0, 0, 0},
207 };
208
209 MODULE_DEVICE_TABLE(sdio, bcmsdh_sdmmc_ids);
210
211 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM)
bcmsdh_sdmmc_suspend(struct device * pdev)212 static int bcmsdh_sdmmc_suspend(struct device *pdev)
213 {
214 int err;
215 sdioh_info_t *sdioh;
216 struct sdio_func *func = dev_to_sdio_func(pdev);
217 mmc_pm_flag_t sdio_flags;
218
219 printf("%s Enter func->num=%d\n", __FUNCTION__, func->num);
220 if (func->num != 2)
221 return 0;
222
223 dhd_mmc_suspend = TRUE;
224 sdioh = sdio_get_drvdata(func);
225 err = bcmsdh_suspend(sdioh->bcmsdh);
226 if (err) {
227 printf("%s bcmsdh_suspend err=%d\n", __FUNCTION__, err);
228 dhd_mmc_suspend = FALSE;
229 return err;
230 }
231
232 sdio_flags = sdio_get_host_pm_caps(func);
233 if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
234 sd_err(("%s: can't keep power while host is suspended\n", __FUNCTION__));
235 dhd_mmc_suspend = FALSE;
236 return -EINVAL;
237 }
238
239 /* keep power while host suspended */
240 err = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
241 if (err) {
242 sd_err(("%s: error while trying to keep power\n", __FUNCTION__));
243 dhd_mmc_suspend = FALSE;
244 return err;
245 }
246 smp_mb();
247
248 printf("%s Exit\n", __FUNCTION__);
249 return 0;
250 }
251
bcmsdh_sdmmc_resume(struct device * pdev)252 static int bcmsdh_sdmmc_resume(struct device *pdev)
253 {
254 sdioh_info_t *sdioh;
255 struct sdio_func *func = dev_to_sdio_func(pdev);
256
257 printf("%s Enter func->num=%d\n", __FUNCTION__, func->num);
258 if (func->num != 2)
259 return 0;
260
261 dhd_mmc_suspend = FALSE;
262 sdioh = sdio_get_drvdata(func);
263 bcmsdh_resume(sdioh->bcmsdh);
264
265 smp_mb();
266 printf("%s Exit\n", __FUNCTION__);
267 return 0;
268 }
269
270 static const struct dev_pm_ops bcmsdh_sdmmc_pm_ops = {
271 .suspend = bcmsdh_sdmmc_suspend,
272 .resume = bcmsdh_sdmmc_resume,
273 };
274 #endif /* (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM) */
275
276 #if defined(BCMLXSDMMC)
277 static struct semaphore *notify_semaphore = NULL;
278
dummy_probe(struct sdio_func * func,const struct sdio_device_id * id)279 static int dummy_probe(struct sdio_func *func,
280 const struct sdio_device_id *id)
281 {
282 if (func && (func->num != 2)) {
283 return 0;
284 }
285
286 if (notify_semaphore)
287 up(notify_semaphore);
288 return 0;
289 }
290
dummy_remove(struct sdio_func * func)291 static void dummy_remove(struct sdio_func *func)
292 {
293 }
294
295 static struct sdio_driver dummy_sdmmc_driver = {
296 .probe = dummy_probe,
297 .remove = dummy_remove,
298 .name = "dummy_sdmmc",
299 .id_table = bcmsdh_sdmmc_ids,
300 };
301
sdio_func_reg_notify(void * semaphore)302 int sdio_func_reg_notify(void* semaphore)
303 {
304 notify_semaphore = semaphore;
305 return sdio_register_driver(&dummy_sdmmc_driver);
306 }
307
sdio_func_unreg_notify(void)308 void sdio_func_unreg_notify(void)
309 {
310 OSL_SLEEP(15);
311 sdio_unregister_driver(&dummy_sdmmc_driver);
312 }
313
314 #endif /* defined(BCMLXSDMMC) */
315
316 static struct sdio_driver bcmsdh_sdmmc_driver = {
317 .probe = bcmsdh_sdmmc_probe,
318 .remove = bcmsdh_sdmmc_remove,
319 .name = "bcmsdh_sdmmc",
320 .id_table = bcmsdh_sdmmc_ids,
321 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM)
322 .drv = {
323 .pm = &bcmsdh_sdmmc_pm_ops,
324 },
325 #endif /* (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM) */
326 };
327
328 struct sdos_info {
329 sdioh_info_t *sd;
330 spinlock_t lock;
331 };
332
333 /* Interrupt enable/disable */
334 SDIOH_API_RC
sdioh_interrupt_set(sdioh_info_t * sd,bool enable)335 sdioh_interrupt_set(sdioh_info_t *sd, bool enable)
336 {
337 if (!sd)
338 return BCME_BADARG;
339
340 sd_trace(("%s: %s\n", __FUNCTION__, enable ? "Enabling" : "Disabling"));
341 return SDIOH_API_RC_SUCCESS;
342 }
343
344 #ifdef BCMSDH_MODULE
345 static int __init
bcmsdh_module_init(void)346 bcmsdh_module_init(void)
347 {
348 int error = 0;
349 error = sdio_function_init();
350 return error;
351 }
352
353 static void __exit
bcmsdh_module_cleanup(void)354 bcmsdh_module_cleanup(void)
355 {
356 sdio_function_cleanup();
357 }
358
359 module_init(bcmsdh_module_init);
360 module_exit(bcmsdh_module_cleanup);
361
362 MODULE_LICENSE("GPL v2");
363 MODULE_DESCRIPTION(DESCRIPTION);
364 MODULE_AUTHOR(AUTHOR);
365
366 #endif /* BCMSDH_MODULE */
367 /*
368 * module init
369 */
bcmsdh_register_client_driver(void)370 int bcmsdh_register_client_driver(void)
371 {
372 return sdio_register_driver(&bcmsdh_sdmmc_driver);
373 }
374
375 /*
376 * module cleanup
377 */
bcmsdh_unregister_client_driver(void)378 void bcmsdh_unregister_client_driver(void)
379 {
380 sdio_unregister_driver(&bcmsdh_sdmmc_driver);
381 }
382