x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / net / wireless / brcm80211 / brcmfmac / bcmsdh_sdmmc.c
blobc3462b75bd080d4f7e9d72dd979f86bd5c894319
1 /*
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>
32 #include <defs.h>
33 #include <brcm_hw_ids.h>
34 #include <brcmu_utils.h>
35 #include <brcmu_wifi.h>
36 #include "sdio_host.h"
37 #include "dhd_dbg.h"
38 #include "dhd_bus.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;
69 bool
70 brcmf_pm_resume_error(struct brcmf_sdio_dev *sdiodev)
72 bool is_err = false;
73 #ifdef CONFIG_PM_SLEEP
74 is_err = atomic_read(&sdiodev->suspend);
75 #endif
76 return is_err;
79 void
80 brcmf_pm_resume_wait(struct brcmf_sdio_dev *sdiodev, wait_queue_head_t *wq)
82 #ifdef CONFIG_PM_SLEEP
83 int retry = 0;
84 while (atomic_read(&sdiodev->suspend) && retry++ != 30)
85 wait_event_timeout(*wq, false, HZ/100);
86 #endif
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];
93 int err_ret;
96 * Can only directly write to some F0 registers.
97 * Handle F2 enable/disable and Abort command
98 * as a special case.
100 if (regaddr == SDIO_CCCR_IOEx) {
101 sdfunc = sdiodev->func[2];
102 if (sdfunc) {
103 if (*byte & SDIO_FUNC_ENABLE_2) {
104 /* Enable Function 2 */
105 err_ret = sdio_enable_func(sdfunc);
106 if (err_ret)
107 brcmf_err("enable F2 failed:%d\n",
108 err_ret);
109 } else {
110 /* Disable Function 2 */
111 err_ret = sdio_disable_func(sdfunc);
112 if (err_ret)
113 brcmf_err("Disable F2 failed:%d\n",
114 err_ret);
117 } else if ((regaddr == SDIO_CCCR_ABORT) ||
118 (regaddr == SDIO_CCCR_IENx)) {
119 sdfunc = kmemdup(sdiodev->func[0], sizeof(struct sdio_func),
120 GFP_KERNEL);
121 if (!sdfunc)
122 return -ENOMEM;
123 sdfunc->num = 0;
124 sdio_writeb(sdfunc, *byte, regaddr, &err_ret);
125 kfree(sdfunc);
126 } else if (regaddr < 0xF0) {
127 brcmf_err("F0 Wr:0x%02x: write disallowed\n", regaddr);
128 err_ret = -EPERM;
129 } else {
130 sdio_f0_writeb(sdfunc, *byte, regaddr, &err_ret);
133 return err_ret;
136 int brcmf_sdioh_request_byte(struct brcmf_sdio_dev *sdiodev, uint rw, uint func,
137 uint regaddr, u8 *byte)
139 int err_ret;
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))
145 return -EIO;
147 if (rw && func == 0) {
148 /* handle F0 separately */
149 err_ret = brcmf_sdioh_f0_write_byte(sdiodev, regaddr, byte);
150 } else {
151 if (rw) /* CMD52 Write */
152 sdio_writeb(sdiodev->func[func], *byte, regaddr,
153 &err_ret);
154 else if (func == 0) {
155 *byte = sdio_f0_readb(sdiodev->func[func], regaddr,
156 &err_ret);
157 } else {
158 *byte = sdio_readb(sdiodev->func[func], regaddr,
159 &err_ret);
163 if (err_ret)
164 brcmf_err("Failed to %s byte F%d:@0x%05x=%02x, Err: %d\n",
165 rw ? "write" : "read", func, regaddr, *byte, err_ret);
167 return err_ret;
170 int brcmf_sdioh_request_word(struct brcmf_sdio_dev *sdiodev,
171 uint rw, uint func, uint addr, u32 *word,
172 uint nbytes)
174 int err_ret = -EIO;
176 if (func == 0) {
177 brcmf_err("Only CMD52 allowed to F0\n");
178 return -EINVAL;
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))
186 return -EIO;
188 if (rw) { /* CMD52 Write */
189 if (nbytes == 4)
190 sdio_writel(sdiodev->func[func], *word, addr,
191 &err_ret);
192 else if (nbytes == 2)
193 sdio_writew(sdiodev->func[func], (*word & 0xFFFF),
194 addr, &err_ret);
195 else
196 brcmf_err("Invalid nbytes: %d\n", nbytes);
197 } else { /* CMD52 Read */
198 if (nbytes == 4)
199 *word = sdio_readl(sdiodev->func[func], addr, &err_ret);
200 else if (nbytes == 2)
201 *word = sdio_readw(sdiodev->func[func], addr,
202 &err_ret) & 0xFFFF;
203 else
204 brcmf_err("Invalid nbytes: %d\n", nbytes);
207 if (err_ret)
208 brcmf_err("Failed to %s word, Err: 0x%08x\n",
209 rw ? "write" : "read", err_ret);
211 return 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 */
217 int i, ret;
218 u32 scratch, regdata;
219 __le32 scratch_le;
220 u8 *ptr = (u8 *)&scratch_le;
222 for (i = 0; i < 3; i++) {
223 regdata = brcmf_sdio_regrl(sdiodev, regaddr, &ret);
224 if (ret != 0)
225 brcmf_err("Can't read!\n");
227 *ptr++ = (u8) regdata;
228 regaddr++;
231 /* Only the lower 17-bits are valid */
232 scratch = le32_to_cpu(scratch_le);
233 scratch &= 0x0001FFFF;
234 return scratch;
237 static int brcmf_sdioh_enablefuncs(struct brcmf_sdio_dev *sdiodev)
239 int err_ret;
240 u32 fbraddr;
241 u8 func;
243 brcmf_dbg(SDIO, "\n");
245 /* Get the Card's common CIS address */
246 sdiodev->func_cis_ptr[0] = brcmf_sdioh_get_cisaddr(sdiodev,
247 SDIO_CCCR_CIS);
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]);
262 if (err_ret)
263 brcmf_err("Failed to enable F1 Err: 0x%08x\n", err_ret);
265 return false;
269 * Public entry points & extern's
271 int brcmf_sdioh_attach(struct brcmf_sdio_dev *sdiodev)
273 int err_ret = 0;
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);
282 if (err_ret) {
283 brcmf_err("Failed to set F1 blocksize\n");
284 goto out;
287 err_ret = sdio_set_block_size(sdiodev->func[2], SDIO_FUNC2_BLOCKSIZE);
288 if (err_ret) {
289 brcmf_err("Failed to set F2 blocksize\n");
290 goto out;
293 brcmf_sdioh_enablefuncs(sdiodev);
295 out:
296 sdio_release_host(sdiodev->func[1]);
297 brcmf_dbg(SDIO, "Done\n");
298 return err_ret;
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)
320 int err;
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. */
331 if (func->num == 1)
332 return 0;
334 /* Ignore anything but func 2 */
335 if (func->num != 2)
336 return -ENODEV;
338 bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL);
339 if (!bus_if)
340 return -ENOMEM;
341 sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL);
342 if (!sdiodev) {
343 kfree(bus_if);
344 return -ENOMEM;
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);
365 if (err) {
366 brcmf_err("F2 error, probe failed %d...\n", err);
367 goto fail;
369 brcmf_dbg(SDIO, "F2 init completed...\n");
370 return 0;
372 fail:
373 dev_set_drvdata(&func->dev, NULL);
374 dev_set_drvdata(&sdiodev->func[1]->dev, NULL);
375 kfree(sdiodev);
376 kfree(bus_if);
377 return err;
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)
391 return;
393 bus_if = dev_get_drvdata(&func->dev);
394 if (bus_if) {
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);
401 kfree(bus_if);
402 kfree(sdiodev);
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;
414 int ret = 0;
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");
423 return -EINVAL;
426 ret = sdio_set_host_pm_flags(sdiodev->func[1], MMC_PM_KEEP_POWER);
427 if (ret) {
428 brcmf_err("Failed to set pm_flags\n");
429 return ret;
432 brcmf_sdio_wdtmr_enable(sdiodev, false);
434 return ret;
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);
444 return 0;
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
459 .drv = {
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();
474 return 0;
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);
486 return 0;
489 static struct platform_driver brcmf_sdio_pd = {
490 .remove = brcmf_sdio_pd_remove,
491 .driver = {
492 .name = BRCMFMAC_SDIO_PDATA_NAME,
493 .owner = THIS_MODULE,
497 void brcmf_sdio_register(void)
499 int ret;
501 ret = sdio_register_driver(&brcmf_sdmmc_driver);
502 if (ret)
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);
512 else
513 sdio_unregister_driver(&brcmf_sdmmc_driver);
516 void __init brcmf_sdio_init(void)
518 int ret;
520 brcmf_dbg(SDIO, "Enter\n");
522 ret = platform_driver_probe(&brcmf_sdio_pd, brcmf_sdio_pd_probe);
523 if (ret == -ENODEV)
524 brcmf_dbg(SDIO, "No platform data available.\n");