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