2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/types.h>
18 #include <linux/netdevice.h>
19 #include <linux/mmc/sdio.h>
20 #include <linux/mmc/core.h>
21 #include <linux/mmc/sdio_func.h>
22 #include <linux/mmc/sdio_ids.h>
23 #include <linux/mmc/card.h>
24 #include <linux/suspend.h>
25 #include <linux/errno.h>
26 #include <linux/sched.h> /* request_irq() */
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/platform_data/brcmfmac-sdio.h>
30 #include <net/cfg80211.h>
33 #include <brcm_hw_ids.h>
34 #include <brcmu_utils.h>
35 #include <brcmu_wifi.h>
36 #include "sdio_host.h"
40 #define SDIO_VENDOR_ID_BROADCOM 0x02d0
42 #define DMA_ALIGN_MASK 0x03
44 #define SDIO_DEVICE_ID_BROADCOM_43143 43143
45 #define SDIO_DEVICE_ID_BROADCOM_43241 0x4324
46 #define SDIO_DEVICE_ID_BROADCOM_4329 0x4329
47 #define SDIO_DEVICE_ID_BROADCOM_4330 0x4330
48 #define SDIO_DEVICE_ID_BROADCOM_4334 0x4334
49 #define SDIO_DEVICE_ID_BROADCOM_4335 0x4335
51 #define SDIO_FUNC1_BLOCKSIZE 64
52 #define SDIO_FUNC2_BLOCKSIZE 512
54 /* devices we support, null terminated */
55 static const struct sdio_device_id brcmf_sdmmc_ids
[] = {
56 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM
, SDIO_DEVICE_ID_BROADCOM_43143
)},
57 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM
, SDIO_DEVICE_ID_BROADCOM_43241
)},
58 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM
, SDIO_DEVICE_ID_BROADCOM_4329
)},
59 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM
, SDIO_DEVICE_ID_BROADCOM_4330
)},
60 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM
, SDIO_DEVICE_ID_BROADCOM_4334
)},
61 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM
, SDIO_DEVICE_ID_BROADCOM_4335
)},
62 { /* end: all zeroes */ },
64 MODULE_DEVICE_TABLE(sdio
, brcmf_sdmmc_ids
);
66 static struct brcmfmac_sdio_platform_data
*brcmfmac_sdio_pdata
;
70 brcmf_pm_resume_error(struct brcmf_sdio_dev
*sdiodev
)
73 #ifdef CONFIG_PM_SLEEP
74 is_err
= atomic_read(&sdiodev
->suspend
);
80 brcmf_pm_resume_wait(struct brcmf_sdio_dev
*sdiodev
, wait_queue_head_t
*wq
)
82 #ifdef CONFIG_PM_SLEEP
84 while (atomic_read(&sdiodev
->suspend
) && retry
++ != 30)
85 wait_event_timeout(*wq
, false, HZ
/100);
89 static inline int brcmf_sdioh_f0_write_byte(struct brcmf_sdio_dev
*sdiodev
,
90 uint regaddr
, u8
*byte
)
92 struct sdio_func
*sdfunc
= sdiodev
->func
[0];
96 * Can only directly write to some F0 registers.
97 * Handle F2 enable/disable and Abort command
100 if (regaddr
== SDIO_CCCR_IOEx
) {
101 sdfunc
= sdiodev
->func
[2];
103 if (*byte
& SDIO_FUNC_ENABLE_2
) {
104 /* Enable Function 2 */
105 err_ret
= sdio_enable_func(sdfunc
);
107 brcmf_err("enable F2 failed:%d\n",
110 /* Disable Function 2 */
111 err_ret
= sdio_disable_func(sdfunc
);
113 brcmf_err("Disable F2 failed:%d\n",
117 } else if ((regaddr
== SDIO_CCCR_ABORT
) ||
118 (regaddr
== SDIO_CCCR_IENx
)) {
119 sdfunc
= kmemdup(sdiodev
->func
[0], sizeof(struct sdio_func
),
124 sdio_writeb(sdfunc
, *byte
, regaddr
, &err_ret
);
126 } else if (regaddr
< 0xF0) {
127 brcmf_err("F0 Wr:0x%02x: write disallowed\n", regaddr
);
130 sdio_f0_writeb(sdfunc
, *byte
, regaddr
, &err_ret
);
136 int brcmf_sdioh_request_byte(struct brcmf_sdio_dev
*sdiodev
, uint rw
, uint func
,
137 uint regaddr
, u8
*byte
)
141 brcmf_dbg(SDIO
, "rw=%d, func=%d, addr=0x%05x\n", rw
, func
, regaddr
);
143 brcmf_pm_resume_wait(sdiodev
, &sdiodev
->request_byte_wait
);
144 if (brcmf_pm_resume_error(sdiodev
))
147 if (rw
&& func
== 0) {
148 /* handle F0 separately */
149 err_ret
= brcmf_sdioh_f0_write_byte(sdiodev
, regaddr
, byte
);
151 if (rw
) /* CMD52 Write */
152 sdio_writeb(sdiodev
->func
[func
], *byte
, regaddr
,
154 else if (func
== 0) {
155 *byte
= sdio_f0_readb(sdiodev
->func
[func
], regaddr
,
158 *byte
= sdio_readb(sdiodev
->func
[func
], regaddr
,
164 brcmf_err("Failed to %s byte F%d:@0x%05x=%02x, Err: %d\n",
165 rw
? "write" : "read", func
, regaddr
, *byte
, err_ret
);
170 int brcmf_sdioh_request_word(struct brcmf_sdio_dev
*sdiodev
,
171 uint rw
, uint func
, uint addr
, u32
*word
,
177 brcmf_err("Only CMD52 allowed to F0\n");
181 brcmf_dbg(SDIO
, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n",
182 rw
, func
, addr
, nbytes
);
184 brcmf_pm_resume_wait(sdiodev
, &sdiodev
->request_word_wait
);
185 if (brcmf_pm_resume_error(sdiodev
))
188 if (rw
) { /* CMD52 Write */
190 sdio_writel(sdiodev
->func
[func
], *word
, addr
,
192 else if (nbytes
== 2)
193 sdio_writew(sdiodev
->func
[func
], (*word
& 0xFFFF),
196 brcmf_err("Invalid nbytes: %d\n", nbytes
);
197 } else { /* CMD52 Read */
199 *word
= sdio_readl(sdiodev
->func
[func
], addr
, &err_ret
);
200 else if (nbytes
== 2)
201 *word
= sdio_readw(sdiodev
->func
[func
], addr
,
204 brcmf_err("Invalid nbytes: %d\n", nbytes
);
208 brcmf_err("Failed to %s word, Err: 0x%08x\n",
209 rw
? "write" : "read", err_ret
);
214 static int brcmf_sdioh_get_cisaddr(struct brcmf_sdio_dev
*sdiodev
, u32 regaddr
)
216 /* read 24 bits and return valid 17 bit addr */
218 u32 scratch
, regdata
;
220 u8
*ptr
= (u8
*)&scratch_le
;
222 for (i
= 0; i
< 3; i
++) {
223 regdata
= brcmf_sdio_regrl(sdiodev
, regaddr
, &ret
);
225 brcmf_err("Can't read!\n");
227 *ptr
++ = (u8
) regdata
;
231 /* Only the lower 17-bits are valid */
232 scratch
= le32_to_cpu(scratch_le
);
233 scratch
&= 0x0001FFFF;
237 static int brcmf_sdioh_enablefuncs(struct brcmf_sdio_dev
*sdiodev
)
243 brcmf_dbg(SDIO
, "\n");
245 /* Get the Card's common CIS address */
246 sdiodev
->func_cis_ptr
[0] = brcmf_sdioh_get_cisaddr(sdiodev
,
248 brcmf_dbg(SDIO
, "Card's Common CIS Ptr = 0x%x\n",
249 sdiodev
->func_cis_ptr
[0]);
251 /* Get the Card's function CIS (for each function) */
252 for (fbraddr
= SDIO_FBR_BASE(1), func
= 1;
253 func
<= sdiodev
->num_funcs
; func
++, fbraddr
+= SDIOD_FBR_SIZE
) {
254 sdiodev
->func_cis_ptr
[func
] =
255 brcmf_sdioh_get_cisaddr(sdiodev
, SDIO_FBR_CIS
+ fbraddr
);
256 brcmf_dbg(SDIO
, "Function %d CIS Ptr = 0x%x\n",
257 func
, sdiodev
->func_cis_ptr
[func
]);
260 /* Enable Function 1 */
261 err_ret
= sdio_enable_func(sdiodev
->func
[1]);
263 brcmf_err("Failed to enable F1 Err: 0x%08x\n", err_ret
);
269 * Public entry points & extern's
271 int brcmf_sdioh_attach(struct brcmf_sdio_dev
*sdiodev
)
275 brcmf_dbg(SDIO
, "\n");
277 sdiodev
->num_funcs
= 2;
279 sdio_claim_host(sdiodev
->func
[1]);
281 err_ret
= sdio_set_block_size(sdiodev
->func
[1], SDIO_FUNC1_BLOCKSIZE
);
283 brcmf_err("Failed to set F1 blocksize\n");
287 err_ret
= sdio_set_block_size(sdiodev
->func
[2], SDIO_FUNC2_BLOCKSIZE
);
289 brcmf_err("Failed to set F2 blocksize\n");
293 brcmf_sdioh_enablefuncs(sdiodev
);
296 sdio_release_host(sdiodev
->func
[1]);
297 brcmf_dbg(SDIO
, "Done\n");
301 void brcmf_sdioh_detach(struct brcmf_sdio_dev
*sdiodev
)
303 brcmf_dbg(SDIO
, "\n");
305 /* Disable Function 2 */
306 sdio_claim_host(sdiodev
->func
[2]);
307 sdio_disable_func(sdiodev
->func
[2]);
308 sdio_release_host(sdiodev
->func
[2]);
310 /* Disable Function 1 */
311 sdio_claim_host(sdiodev
->func
[1]);
312 sdio_disable_func(sdiodev
->func
[1]);
313 sdio_release_host(sdiodev
->func
[1]);
317 static int brcmf_ops_sdio_probe(struct sdio_func
*func
,
318 const struct sdio_device_id
*id
)
321 struct brcmf_sdio_dev
*sdiodev
;
322 struct brcmf_bus
*bus_if
;
324 brcmf_dbg(SDIO
, "Enter\n");
325 brcmf_dbg(SDIO
, "Class=%x\n", func
->class);
326 brcmf_dbg(SDIO
, "sdio vendor ID: 0x%04x\n", func
->vendor
);
327 brcmf_dbg(SDIO
, "sdio device ID: 0x%04x\n", func
->device
);
328 brcmf_dbg(SDIO
, "Function#: %d\n", func
->num
);
330 /* Consume func num 1 but dont do anything with it. */
334 /* Ignore anything but func 2 */
338 bus_if
= kzalloc(sizeof(struct brcmf_bus
), GFP_KERNEL
);
341 sdiodev
= kzalloc(sizeof(struct brcmf_sdio_dev
), GFP_KERNEL
);
347 sdiodev
->func
[0] = func
->card
->sdio_func
[0];
348 sdiodev
->func
[1] = func
->card
->sdio_func
[0];
349 sdiodev
->func
[2] = func
;
351 sdiodev
->bus_if
= bus_if
;
352 bus_if
->bus_priv
.sdio
= sdiodev
;
353 dev_set_drvdata(&func
->dev
, bus_if
);
354 dev_set_drvdata(&sdiodev
->func
[1]->dev
, bus_if
);
355 sdiodev
->dev
= &sdiodev
->func
[1]->dev
;
356 sdiodev
->pdata
= brcmfmac_sdio_pdata
;
358 atomic_set(&sdiodev
->suspend
, false);
359 init_waitqueue_head(&sdiodev
->request_byte_wait
);
360 init_waitqueue_head(&sdiodev
->request_word_wait
);
361 init_waitqueue_head(&sdiodev
->request_buffer_wait
);
363 brcmf_dbg(SDIO
, "F2 found, calling brcmf_sdio_probe...\n");
364 err
= brcmf_sdio_probe(sdiodev
);
366 brcmf_err("F2 error, probe failed %d...\n", err
);
369 brcmf_dbg(SDIO
, "F2 init completed...\n");
373 dev_set_drvdata(&func
->dev
, NULL
);
374 dev_set_drvdata(&sdiodev
->func
[1]->dev
, NULL
);
380 static void brcmf_ops_sdio_remove(struct sdio_func
*func
)
382 struct brcmf_bus
*bus_if
;
383 struct brcmf_sdio_dev
*sdiodev
;
385 brcmf_dbg(SDIO
, "Enter\n");
386 brcmf_dbg(SDIO
, "sdio vendor ID: 0x%04x\n", func
->vendor
);
387 brcmf_dbg(SDIO
, "sdio device ID: 0x%04x\n", func
->device
);
388 brcmf_dbg(SDIO
, "Function: %d\n", func
->num
);
390 if (func
->num
!= 1 && func
->num
!= 2)
393 bus_if
= dev_get_drvdata(&func
->dev
);
395 sdiodev
= bus_if
->bus_priv
.sdio
;
396 brcmf_sdio_remove(sdiodev
);
398 dev_set_drvdata(&sdiodev
->func
[1]->dev
, NULL
);
399 dev_set_drvdata(&sdiodev
->func
[2]->dev
, NULL
);
405 brcmf_dbg(SDIO
, "Exit\n");
408 #ifdef CONFIG_PM_SLEEP
409 static int brcmf_sdio_suspend(struct device
*dev
)
411 mmc_pm_flag_t sdio_flags
;
412 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
413 struct brcmf_sdio_dev
*sdiodev
= bus_if
->bus_priv
.sdio
;
416 brcmf_dbg(SDIO
, "\n");
418 atomic_set(&sdiodev
->suspend
, true);
420 sdio_flags
= sdio_get_host_pm_caps(sdiodev
->func
[1]);
421 if (!(sdio_flags
& MMC_PM_KEEP_POWER
)) {
422 brcmf_err("Host can't keep power while suspended\n");
426 ret
= sdio_set_host_pm_flags(sdiodev
->func
[1], MMC_PM_KEEP_POWER
);
428 brcmf_err("Failed to set pm_flags\n");
432 brcmf_sdio_wdtmr_enable(sdiodev
, false);
437 static int brcmf_sdio_resume(struct device
*dev
)
439 struct brcmf_bus
*bus_if
= dev_get_drvdata(dev
);
440 struct brcmf_sdio_dev
*sdiodev
= bus_if
->bus_priv
.sdio
;
442 brcmf_sdio_wdtmr_enable(sdiodev
, true);
443 atomic_set(&sdiodev
->suspend
, false);
447 static const struct dev_pm_ops brcmf_sdio_pm_ops
= {
448 .suspend
= brcmf_sdio_suspend
,
449 .resume
= brcmf_sdio_resume
,
451 #endif /* CONFIG_PM_SLEEP */
453 static struct sdio_driver brcmf_sdmmc_driver
= {
454 .probe
= brcmf_ops_sdio_probe
,
455 .remove
= brcmf_ops_sdio_remove
,
456 .name
= BRCMFMAC_SDIO_PDATA_NAME
,
457 .id_table
= brcmf_sdmmc_ids
,
458 #ifdef CONFIG_PM_SLEEP
460 .pm
= &brcmf_sdio_pm_ops
,
462 #endif /* CONFIG_PM_SLEEP */
465 static int brcmf_sdio_pd_probe(struct platform_device
*pdev
)
467 brcmf_dbg(SDIO
, "Enter\n");
469 brcmfmac_sdio_pdata
= pdev
->dev
.platform_data
;
471 if (brcmfmac_sdio_pdata
->power_on
)
472 brcmfmac_sdio_pdata
->power_on();
477 static int brcmf_sdio_pd_remove(struct platform_device
*pdev
)
479 brcmf_dbg(SDIO
, "Enter\n");
481 if (brcmfmac_sdio_pdata
->power_off
)
482 brcmfmac_sdio_pdata
->power_off();
484 sdio_unregister_driver(&brcmf_sdmmc_driver
);
489 static struct platform_driver brcmf_sdio_pd
= {
490 .remove
= brcmf_sdio_pd_remove
,
492 .name
= BRCMFMAC_SDIO_PDATA_NAME
,
493 .owner
= THIS_MODULE
,
497 void brcmf_sdio_register(void)
501 ret
= sdio_register_driver(&brcmf_sdmmc_driver
);
503 brcmf_err("sdio_register_driver failed: %d\n", ret
);
506 void brcmf_sdio_exit(void)
508 brcmf_dbg(SDIO
, "Enter\n");
510 if (brcmfmac_sdio_pdata
)
511 platform_driver_unregister(&brcmf_sdio_pd
);
513 sdio_unregister_driver(&brcmf_sdmmc_driver
);
516 void __init
brcmf_sdio_init(void)
520 brcmf_dbg(SDIO
, "Enter\n");
522 ret
= platform_driver_probe(&brcmf_sdio_pd
, brcmf_sdio_pd_probe
);
524 brcmf_dbg(SDIO
, "No platform data available.\n");