• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * BCMSDH Function Driver for the native SDIO/MMC driver in the Linux Kernel
3  *
4  * Copyright (C) 1999-2013, 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 404103 2013-05-23 20:07: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/sdio_func.h>
38 #include <linux/mmc/sdio_ids.h>
39 
40 #if !defined(SDIO_VENDOR_ID_BROADCOM)
41 #define SDIO_VENDOR_ID_BROADCOM		0x02d0
42 #endif /* !defined(SDIO_VENDOR_ID_BROADCOM) */
43 
44 #define SDIO_DEVICE_ID_BROADCOM_DEFAULT	0x0000
45 
46 #if !defined(SDIO_DEVICE_ID_BROADCOM_4325_SDGWB)
47 #define SDIO_DEVICE_ID_BROADCOM_4325_SDGWB	0x0492	/* BCM94325SDGWB */
48 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4325_SDGWB) */
49 #if !defined(SDIO_DEVICE_ID_BROADCOM_4325)
50 #define SDIO_DEVICE_ID_BROADCOM_4325	0x0493
51 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4325) */
52 #if !defined(SDIO_DEVICE_ID_BROADCOM_4329)
53 #define SDIO_DEVICE_ID_BROADCOM_4329	0x4329
54 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4329) */
55 #if !defined(SDIO_DEVICE_ID_BROADCOM_4319)
56 #define SDIO_DEVICE_ID_BROADCOM_4319	0x4319
57 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4319) */
58 #if !defined(SDIO_DEVICE_ID_BROADCOM_4330)
59 #define SDIO_DEVICE_ID_BROADCOM_4330	0x4330
60 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4330) */
61 #if !defined(SDIO_DEVICE_ID_BROADCOM_4334)
62 #define SDIO_DEVICE_ID_BROADCOM_4334    0x4334
63 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4334) */
64 #if !defined(SDIO_DEVICE_ID_BROADCOM_4324)
65 #define SDIO_DEVICE_ID_BROADCOM_4324    0x4324
66 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_4324) */
67 #if !defined(SDIO_DEVICE_ID_BROADCOM_43239)
68 #define SDIO_DEVICE_ID_BROADCOM_43239    43239
69 #endif /* !defined(SDIO_DEVICE_ID_BROADCOM_43239) */
70 
71 
72 #include <bcmsdh_sdmmc.h>
73 
74 #include <dhd_dbg.h>
75 
76 #ifdef WL_CFG80211
77 extern void wl_cfg80211_set_parent_dev(void *dev);
78 #endif
79 
80 extern void sdioh_sdmmc_devintr_off(sdioh_info_t *sd);
81 extern void sdioh_sdmmc_devintr_on(sdioh_info_t *sd);
82 extern int dhd_os_check_wakelock(void *dhdp);
83 extern int dhd_os_check_if_up(void *dhdp);
84 extern void *bcmsdh_get_drvdata(void);
85 
86 int sdio_function_init(void);
87 void sdio_function_cleanup(void);
88 
89 #define DESCRIPTION "bcmsdh_sdmmc Driver"
90 #define AUTHOR "Broadcom Corporation"
91 
92 /* module param defaults */
93 static int clockoverride = 0;
94 
95 module_param(clockoverride, int, 0644);
96 MODULE_PARM_DESC(clockoverride, "SDIO card clock override");
97 
98 PBCMSDH_SDMMC_INSTANCE gInstance;
99 
100 /* Maximum number of bcmsdh_sdmmc devices supported by driver */
101 #define BCMSDH_SDMMC_MAX_DEVICES 1
102 
103 extern int bcmsdh_probe(struct device *dev);
104 extern int bcmsdh_remove(struct device *dev);
105 extern volatile bool dhd_mmc_suspend;
106 
bcmsdh_sdmmc_probe(struct sdio_func * func,const struct sdio_device_id * id)107 static int bcmsdh_sdmmc_probe(struct sdio_func *func,
108                               const struct sdio_device_id *id)
109 {
110 	int ret = 0;
111 	static struct sdio_func sdio_func_0;
112 
113 	if (!gInstance)
114 		return -EINVAL;
115 
116 	if (func) {
117 		sd_trace(("bcmsdh_sdmmc: %s Enter\n", __FUNCTION__));
118 		sd_trace(("sdio_bcmsdh: func->class=%x\n", func->class));
119 		sd_trace(("sdio_vendor: 0x%04x\n", func->vendor));
120 		sd_trace(("sdio_device: 0x%04x\n", func->device));
121 		sd_trace(("Function#: 0x%04x\n", func->num));
122 
123 		if (func->num == 1) {
124 			sdio_func_0.num = 0;
125 			sdio_func_0.card = func->card;
126 			gInstance->func[0] = &sdio_func_0;
127 			if(func->device == 0x4) { /* 4318 */
128 				gInstance->func[2] = NULL;
129 				sd_trace(("NIC found, calling bcmsdh_probe...\n"));
130 				ret = bcmsdh_probe(&func->dev);
131 			}
132 		}
133 
134 		gInstance->func[func->num] = func;
135 
136 		if (func->num == 2) {
137 	#ifdef WL_CFG80211
138 			wl_cfg80211_set_parent_dev(&func->dev);
139 	#endif
140 			sd_trace(("F2 found, calling bcmsdh_probe...\n"));
141 			ret = bcmsdh_probe(&func->dev);
142 			if (ret < 0)
143 				gInstance->func[2] = NULL;
144 		}
145 	} else {
146 		ret = -ENODEV;
147 	}
148 
149 	return ret;
150 }
151 
bcmsdh_sdmmc_remove(struct sdio_func * func)152 static void bcmsdh_sdmmc_remove(struct sdio_func *func)
153 {
154 	if (func) {
155 		sd_trace(("bcmsdh_sdmmc: %s Enter\n", __FUNCTION__));
156 		sd_info(("sdio_bcmsdh: func->class=%x\n", func->class));
157 		sd_info(("sdio_vendor: 0x%04x\n", func->vendor));
158 		sd_info(("sdio_device: 0x%04x\n", func->device));
159 		sd_info(("Function#: 0x%04x\n", func->num));
160 
161 		if (gInstance->func[2]) {
162 			sd_trace(("F2 found, calling bcmsdh_remove...\n"));
163 			bcmsdh_remove(&func->dev);
164 			gInstance->func[2] = NULL;
165 		}
166 		if (func->num == 1) {
167 			sdio_claim_host(func);
168 			sdio_disable_func(func);
169 			sdio_release_host(func);
170 			gInstance->func[1] = NULL;
171 		}
172 	}
173 }
174 
175 /* devices we support, null terminated */
176 static const struct sdio_device_id bcmsdh_sdmmc_ids[] = {
177 	{ SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_DEFAULT) },
178 	{ SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4325_SDGWB) },
179 	{ SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4325) },
180 	{ SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4329) },
181 	{ SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4319) },
182 	{ SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4330) },
183 	{ SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4334) },
184 	{ SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4324) },
185 	{ SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43239) },
186 	{ SDIO_DEVICE_CLASS(SDIO_CLASS_NONE)		},
187 	{ /* end: all zeroes */				},
188 };
189 
190 MODULE_DEVICE_TABLE(sdio, bcmsdh_sdmmc_ids);
191 
192 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM)
bcmsdh_sdmmc_suspend(struct device * pdev)193 static int bcmsdh_sdmmc_suspend(struct device *pdev)
194 {
195 	struct sdio_func *func = dev_to_sdio_func(pdev);
196 	mmc_pm_flag_t sdio_flags;
197 	int ret;
198 
199 	if (func->num != 2)
200 		return 0;
201 
202 	sd_trace(("%s Enter\n", __FUNCTION__));
203 	if (dhd_os_check_wakelock(bcmsdh_get_drvdata()))
204 		return -EBUSY;
205 	sdio_flags = sdio_get_host_pm_caps(func);
206 
207 	if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
208 		sd_err(("%s: can't keep power while host is suspended\n", __FUNCTION__));
209 		return  -EINVAL;
210 	}
211 
212 	/* keep power while host suspended */
213 	ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
214 	if (ret) {
215 		sd_err(("%s: error while trying to keep power\n", __FUNCTION__));
216 		return ret;
217 	}
218 #if defined(OOB_INTR_ONLY)
219 	bcmsdh_oob_intr_set(0);
220 #endif
221 	dhd_mmc_suspend = TRUE;
222 	smp_mb();
223 
224 	return 0;
225 }
226 
bcmsdh_sdmmc_resume(struct device * pdev)227 static int bcmsdh_sdmmc_resume(struct device *pdev)
228 {
229 #if defined(OOB_INTR_ONLY)
230 	struct sdio_func *func = dev_to_sdio_func(pdev);
231 #endif
232 	sd_trace(("%s Enter\n", __FUNCTION__));
233 	dhd_mmc_suspend = FALSE;
234 #if defined(OOB_INTR_ONLY)
235 	if ((func->num == 2) && dhd_os_check_if_up(bcmsdh_get_drvdata()))
236 		bcmsdh_oob_intr_set(1);
237 #endif
238 
239 	smp_mb();
240 	return 0;
241 }
242 
243 static const struct dev_pm_ops bcmsdh_sdmmc_pm_ops = {
244 	.suspend	= bcmsdh_sdmmc_suspend,
245 	.resume		= bcmsdh_sdmmc_resume,
246 };
247 #endif  /* (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM) */
248 
249 #if defined(BCMLXSDMMC)
250 static struct semaphore *notify_semaphore = NULL;
251 
dummy_probe(struct sdio_func * func,const struct sdio_device_id * id)252 static int dummy_probe(struct sdio_func *func,
253                               const struct sdio_device_id *id)
254 {
255 	if (notify_semaphore)
256 		up(notify_semaphore);
257 	return 0;
258 }
259 
dummy_remove(struct sdio_func * func)260 static void dummy_remove(struct sdio_func *func)
261 {
262 }
263 
264 static struct sdio_driver dummy_sdmmc_driver = {
265 	.probe		= dummy_probe,
266 	.remove		= dummy_remove,
267 	.name		= "dummy_sdmmc",
268 	.id_table	= bcmsdh_sdmmc_ids,
269 	};
270 
sdio_func_reg_notify(void * semaphore)271 int sdio_func_reg_notify(void* semaphore)
272 {
273 	notify_semaphore = semaphore;
274 	return sdio_register_driver(&dummy_sdmmc_driver);
275 }
276 
sdio_func_unreg_notify(void)277 void sdio_func_unreg_notify(void)
278 {
279 	sdio_unregister_driver(&dummy_sdmmc_driver);
280 }
281 
282 #endif /* defined(BCMLXSDMMC) */
283 
284 static struct sdio_driver bcmsdh_sdmmc_driver = {
285 	.probe		= bcmsdh_sdmmc_probe,
286 	.remove		= bcmsdh_sdmmc_remove,
287 	.name		= "bcmsdh_sdmmc",
288 	.id_table	= bcmsdh_sdmmc_ids,
289 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM)
290 	.drv = {
291 	.pm	= &bcmsdh_sdmmc_pm_ops,
292 	},
293 #endif /* (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 39)) && defined(CONFIG_PM) */
294 	};
295 
296 struct sdos_info {
297 	sdioh_info_t *sd;
298 	spinlock_t lock;
299 };
300 
301 
302 int
sdioh_sdmmc_osinit(sdioh_info_t * sd)303 sdioh_sdmmc_osinit(sdioh_info_t *sd)
304 {
305 	struct sdos_info *sdos;
306 
307 	if (!sd)
308 		return BCME_BADARG;
309 
310 	sdos = (struct sdos_info*)MALLOC(sd->osh, sizeof(struct sdos_info));
311 	sd->sdos_info = (void*)sdos;
312 	if (sdos == NULL)
313 		return BCME_NOMEM;
314 
315 	sdos->sd = sd;
316 	spin_lock_init(&sdos->lock);
317 	return BCME_OK;
318 }
319 
320 void
sdioh_sdmmc_osfree(sdioh_info_t * sd)321 sdioh_sdmmc_osfree(sdioh_info_t *sd)
322 {
323 	struct sdos_info *sdos;
324 	ASSERT(sd && sd->sdos_info);
325 
326 	sdos = (struct sdos_info *)sd->sdos_info;
327 	MFREE(sd->osh, sdos, sizeof(struct sdos_info));
328 }
329 
330 /* Interrupt enable/disable */
331 SDIOH_API_RC
sdioh_interrupt_set(sdioh_info_t * sd,bool enable)332 sdioh_interrupt_set(sdioh_info_t *sd, bool enable)
333 {
334 	ulong flags;
335 	struct sdos_info *sdos;
336 
337 	if (!sd)
338 		return BCME_BADARG;
339 
340 	sd_trace(("%s: %s\n", __FUNCTION__, enable ? "Enabling" : "Disabling"));
341 
342 	sdos = (struct sdos_info *)sd->sdos_info;
343 	ASSERT(sdos);
344 
345 #if !defined(OOB_INTR_ONLY)
346 	if (enable && !(sd->intr_handler && sd->intr_handler_arg)) {
347 		sd_err(("%s: no handler registered, will not enable\n", __FUNCTION__));
348 		return SDIOH_API_RC_FAIL;
349 	}
350 #endif /* !defined(OOB_INTR_ONLY) */
351 
352 	/* Ensure atomicity for enable/disable calls */
353 	spin_lock_irqsave(&sdos->lock, flags);
354 
355 	sd->client_intr_enabled = enable;
356 	if (enable) {
357 		sdioh_sdmmc_devintr_on(sd);
358 	} else {
359 		sdioh_sdmmc_devintr_off(sd);
360 	}
361 
362 	spin_unlock_irqrestore(&sdos->lock, flags);
363 
364 	return SDIOH_API_RC_SUCCESS;
365 }
366 
367 
368 #ifdef BCMSDH_MODULE
369 static int __init
bcmsdh_module_init(void)370 bcmsdh_module_init(void)
371 {
372 	int error = 0;
373 	error = sdio_function_init();
374 	return error;
375 }
376 
377 static void __exit
bcmsdh_module_cleanup(void)378 bcmsdh_module_cleanup(void)
379 {
380 	sdio_function_cleanup();
381 }
382 
383 module_init(bcmsdh_module_init);
384 module_exit(bcmsdh_module_cleanup);
385 
386 MODULE_LICENSE("GPL v2");
387 MODULE_DESCRIPTION(DESCRIPTION);
388 MODULE_AUTHOR(AUTHOR);
389 
390 #endif /* BCMSDH_MODULE */
391 /*
392  * module init
393 */
sdio_function_init(void)394 int sdio_function_init(void)
395 {
396 	int error = 0;
397 	sd_trace(("bcmsdh_sdmmc: %s Enter\n", __FUNCTION__));
398 
399 	gInstance = kzalloc(sizeof(BCMSDH_SDMMC_INSTANCE), GFP_KERNEL);
400 	if (!gInstance)
401 		return -ENOMEM;
402 
403 	error = sdio_register_driver(&bcmsdh_sdmmc_driver);
404 	if (error) {
405 		kfree(gInstance);
406 		gInstance = NULL;
407 	}
408 
409 	return error;
410 }
411 
412 /*
413  * module cleanup
414 */
415 extern int bcmsdh_remove(struct device *dev);
sdio_function_cleanup(void)416 void sdio_function_cleanup(void)
417 {
418 	sd_trace(("%s Enter\n", __FUNCTION__));
419 
420 
421 	sdio_unregister_driver(&bcmsdh_sdmmc_driver);
422 
423 	if (gInstance) {
424 		kfree(gInstance);
425 		gInstance = NULL;
426 	}
427 }
428