spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / net / wireless / brcm80211 / brcmfmac / bcmsdh_sdmmc.c
blob9b8c0ed833d40f523ffbb9ea9f3388ad610daa71
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.
16 #include <linux/types.h>
17 #include <linux/netdevice.h>
18 #include <linux/mmc/sdio.h>
19 #include <linux/mmc/core.h>
20 #include <linux/mmc/sdio_func.h>
21 #include <linux/mmc/sdio_ids.h>
22 #include <linux/mmc/card.h>
23 #include <linux/suspend.h>
24 #include <linux/errno.h>
25 #include <linux/sched.h> /* request_irq() */
26 #include <linux/module.h>
27 #include <net/cfg80211.h>
29 #include <defs.h>
30 #include <brcm_hw_ids.h>
31 #include <brcmu_utils.h>
32 #include <brcmu_wifi.h>
33 #include "sdio_host.h"
34 #include "dhd_dbg.h"
35 #include "dhd_bus.h"
37 #define SDIO_VENDOR_ID_BROADCOM 0x02d0
39 #define DMA_ALIGN_MASK 0x03
41 #define SDIO_DEVICE_ID_BROADCOM_4329 0x4329
42 #define SDIO_DEVICE_ID_BROADCOM_4330 0x4330
44 #define SDIO_FUNC1_BLOCKSIZE 64
45 #define SDIO_FUNC2_BLOCKSIZE 512
47 /* devices we support, null terminated */
48 static const struct sdio_device_id brcmf_sdmmc_ids[] = {
49 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4329)},
50 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4330)},
51 { /* end: all zeroes */ },
53 MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids);
55 static bool
56 brcmf_pm_resume_error(struct brcmf_sdio_dev *sdiodev)
58 bool is_err = false;
59 #ifdef CONFIG_PM_SLEEP
60 is_err = atomic_read(&sdiodev->suspend);
61 #endif
62 return is_err;
65 static void
66 brcmf_pm_resume_wait(struct brcmf_sdio_dev *sdiodev, wait_queue_head_t *wq)
68 #ifdef CONFIG_PM_SLEEP
69 int retry = 0;
70 while (atomic_read(&sdiodev->suspend) && retry++ != 30)
71 wait_event_timeout(*wq, false, HZ/100);
72 #endif
75 static inline int brcmf_sdioh_f0_write_byte(struct brcmf_sdio_dev *sdiodev,
76 uint regaddr, u8 *byte)
78 struct sdio_func *sdfunc = sdiodev->func[0];
79 int err_ret;
82 * Can only directly write to some F0 registers.
83 * Handle F2 enable/disable and Abort command
84 * as a special case.
86 if (regaddr == SDIO_CCCR_IOEx) {
87 sdfunc = sdiodev->func[2];
88 if (sdfunc) {
89 sdio_claim_host(sdfunc);
90 if (*byte & SDIO_FUNC_ENABLE_2) {
91 /* Enable Function 2 */
92 err_ret = sdio_enable_func(sdfunc);
93 if (err_ret)
94 brcmf_dbg(ERROR,
95 "enable F2 failed:%d\n",
96 err_ret);
97 } else {
98 /* Disable Function 2 */
99 err_ret = sdio_disable_func(sdfunc);
100 if (err_ret)
101 brcmf_dbg(ERROR,
102 "Disable F2 failed:%d\n",
103 err_ret);
105 sdio_release_host(sdfunc);
107 } else if (regaddr == SDIO_CCCR_ABORT) {
108 sdio_claim_host(sdfunc);
109 sdio_writeb(sdfunc, *byte, regaddr, &err_ret);
110 sdio_release_host(sdfunc);
111 } else if (regaddr < 0xF0) {
112 brcmf_dbg(ERROR, "F0 Wr:0x%02x: write disallowed\n", regaddr);
113 err_ret = -EPERM;
114 } else {
115 sdio_claim_host(sdfunc);
116 sdio_f0_writeb(sdfunc, *byte, regaddr, &err_ret);
117 sdio_release_host(sdfunc);
120 return err_ret;
123 int brcmf_sdioh_request_byte(struct brcmf_sdio_dev *sdiodev, uint rw, uint func,
124 uint regaddr, u8 *byte)
126 int err_ret;
128 brcmf_dbg(INFO, "rw=%d, func=%d, addr=0x%05x\n", rw, func, regaddr);
130 brcmf_pm_resume_wait(sdiodev, &sdiodev->request_byte_wait);
131 if (brcmf_pm_resume_error(sdiodev))
132 return -EIO;
134 if (rw && func == 0) {
135 /* handle F0 separately */
136 err_ret = brcmf_sdioh_f0_write_byte(sdiodev, regaddr, byte);
137 } else {
138 sdio_claim_host(sdiodev->func[func]);
139 if (rw) /* CMD52 Write */
140 sdio_writeb(sdiodev->func[func], *byte, regaddr,
141 &err_ret);
142 else if (func == 0) {
143 *byte = sdio_f0_readb(sdiodev->func[func], regaddr,
144 &err_ret);
145 } else {
146 *byte = sdio_readb(sdiodev->func[func], regaddr,
147 &err_ret);
149 sdio_release_host(sdiodev->func[func]);
152 if (err_ret)
153 brcmf_dbg(ERROR, "Failed to %s byte F%d:@0x%05x=%02x, Err: %d\n",
154 rw ? "write" : "read", func, regaddr, *byte, err_ret);
156 return err_ret;
159 int brcmf_sdioh_request_word(struct brcmf_sdio_dev *sdiodev,
160 uint rw, uint func, uint addr, u32 *word,
161 uint nbytes)
163 int err_ret = -EIO;
165 if (func == 0) {
166 brcmf_dbg(ERROR, "Only CMD52 allowed to F0\n");
167 return -EINVAL;
170 brcmf_dbg(INFO, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n",
171 rw, func, addr, nbytes);
173 brcmf_pm_resume_wait(sdiodev, &sdiodev->request_word_wait);
174 if (brcmf_pm_resume_error(sdiodev))
175 return -EIO;
176 /* Claim host controller */
177 sdio_claim_host(sdiodev->func[func]);
179 if (rw) { /* CMD52 Write */
180 if (nbytes == 4)
181 sdio_writel(sdiodev->func[func], *word, addr,
182 &err_ret);
183 else if (nbytes == 2)
184 sdio_writew(sdiodev->func[func], (*word & 0xFFFF),
185 addr, &err_ret);
186 else
187 brcmf_dbg(ERROR, "Invalid nbytes: %d\n", nbytes);
188 } else { /* CMD52 Read */
189 if (nbytes == 4)
190 *word = sdio_readl(sdiodev->func[func], addr, &err_ret);
191 else if (nbytes == 2)
192 *word = sdio_readw(sdiodev->func[func], addr,
193 &err_ret) & 0xFFFF;
194 else
195 brcmf_dbg(ERROR, "Invalid nbytes: %d\n", nbytes);
198 /* Release host controller */
199 sdio_release_host(sdiodev->func[func]);
201 if (err_ret)
202 brcmf_dbg(ERROR, "Failed to %s word, Err: 0x%08x\n",
203 rw ? "write" : "read", err_ret);
205 return err_ret;
208 /* precondition: host controller is claimed */
209 static int
210 brcmf_sdioh_request_data(struct brcmf_sdio_dev *sdiodev, uint write, bool fifo,
211 uint func, uint addr, struct sk_buff *pkt, uint pktlen)
213 int err_ret = 0;
215 if ((write) && (!fifo)) {
216 err_ret = sdio_memcpy_toio(sdiodev->func[func], addr,
217 ((u8 *) (pkt->data)), pktlen);
218 } else if (write) {
219 err_ret = sdio_memcpy_toio(sdiodev->func[func], addr,
220 ((u8 *) (pkt->data)), pktlen);
221 } else if (fifo) {
222 err_ret = sdio_readsb(sdiodev->func[func],
223 ((u8 *) (pkt->data)), addr, pktlen);
224 } else {
225 err_ret = sdio_memcpy_fromio(sdiodev->func[func],
226 ((u8 *) (pkt->data)),
227 addr, pktlen);
230 return err_ret;
234 * This function takes a queue of packets. The packets on the queue
235 * are assumed to be properly aligned by the caller.
238 brcmf_sdioh_request_chain(struct brcmf_sdio_dev *sdiodev, uint fix_inc,
239 uint write, uint func, uint addr,
240 struct sk_buff_head *pktq)
242 bool fifo = (fix_inc == SDIOH_DATA_FIX);
243 u32 SGCount = 0;
244 int err_ret = 0;
246 struct sk_buff *pkt;
248 brcmf_dbg(TRACE, "Enter\n");
250 brcmf_pm_resume_wait(sdiodev, &sdiodev->request_chain_wait);
251 if (brcmf_pm_resume_error(sdiodev))
252 return -EIO;
254 /* Claim host controller */
255 sdio_claim_host(sdiodev->func[func]);
257 skb_queue_walk(pktq, pkt) {
258 uint pkt_len = pkt->len;
259 pkt_len += 3;
260 pkt_len &= 0xFFFFFFFC;
262 err_ret = brcmf_sdioh_request_data(sdiodev, write, fifo, func,
263 addr, pkt, pkt_len);
264 if (err_ret) {
265 brcmf_dbg(ERROR, "%s FAILED %p[%d], addr=0x%05x, pkt_len=%d, ERR=0x%08x\n",
266 write ? "TX" : "RX", pkt, SGCount, addr,
267 pkt_len, err_ret);
268 } else {
269 brcmf_dbg(TRACE, "%s xfr'd %p[%d], addr=0x%05x, len=%d\n",
270 write ? "TX" : "RX", pkt, SGCount, addr,
271 pkt_len);
273 if (!fifo)
274 addr += pkt_len;
276 SGCount++;
279 /* Release host controller */
280 sdio_release_host(sdiodev->func[func]);
282 brcmf_dbg(TRACE, "Exit\n");
283 return err_ret;
287 * This function takes a single DMA-able packet.
289 int brcmf_sdioh_request_buffer(struct brcmf_sdio_dev *sdiodev,
290 uint fix_inc, uint write, uint func, uint addr,
291 struct sk_buff *pkt)
293 int status;
294 uint pkt_len = pkt->len;
295 bool fifo = (fix_inc == SDIOH_DATA_FIX);
297 brcmf_dbg(TRACE, "Enter\n");
299 if (pkt == NULL)
300 return -EINVAL;
302 brcmf_pm_resume_wait(sdiodev, &sdiodev->request_buffer_wait);
303 if (brcmf_pm_resume_error(sdiodev))
304 return -EIO;
306 /* Claim host controller */
307 sdio_claim_host(sdiodev->func[func]);
309 pkt_len += 3;
310 pkt_len &= (uint)~3;
312 status = brcmf_sdioh_request_data(sdiodev, write, fifo, func,
313 addr, pkt, pkt_len);
314 if (status) {
315 brcmf_dbg(ERROR, "%s FAILED %p, addr=0x%05x, pkt_len=%d, ERR=0x%08x\n",
316 write ? "TX" : "RX", pkt, addr, pkt_len, status);
317 } else {
318 brcmf_dbg(TRACE, "%s xfr'd %p, addr=0x%05x, len=%d\n",
319 write ? "TX" : "RX", pkt, addr, pkt_len);
322 /* Release host controller */
323 sdio_release_host(sdiodev->func[func]);
325 return status;
328 /* Read client card reg */
329 static int
330 brcmf_sdioh_card_regread(struct brcmf_sdio_dev *sdiodev, int func, u32 regaddr,
331 int regsize, u32 *data)
334 if ((func == 0) || (regsize == 1)) {
335 u8 temp = 0;
337 brcmf_sdioh_request_byte(sdiodev, SDIOH_READ, func, regaddr,
338 &temp);
339 *data = temp;
340 *data &= 0xff;
341 brcmf_dbg(DATA, "byte read data=0x%02x\n", *data);
342 } else {
343 brcmf_sdioh_request_word(sdiodev, SDIOH_READ, func, regaddr,
344 data, regsize);
345 if (regsize == 2)
346 *data &= 0xffff;
348 brcmf_dbg(DATA, "word read data=0x%08x\n", *data);
351 return SUCCESS;
354 static int brcmf_sdioh_get_cisaddr(struct brcmf_sdio_dev *sdiodev, u32 regaddr)
356 /* read 24 bits and return valid 17 bit addr */
357 int i;
358 u32 scratch, regdata;
359 __le32 scratch_le;
360 u8 *ptr = (u8 *)&scratch_le;
362 for (i = 0; i < 3; i++) {
363 if ((brcmf_sdioh_card_regread(sdiodev, 0, regaddr, 1,
364 &regdata)) != SUCCESS)
365 brcmf_dbg(ERROR, "Can't read!\n");
367 *ptr++ = (u8) regdata;
368 regaddr++;
371 /* Only the lower 17-bits are valid */
372 scratch = le32_to_cpu(scratch_le);
373 scratch &= 0x0001FFFF;
374 return scratch;
377 static int brcmf_sdioh_enablefuncs(struct brcmf_sdio_dev *sdiodev)
379 int err_ret;
380 u32 fbraddr;
381 u8 func;
383 brcmf_dbg(TRACE, "\n");
385 /* Get the Card's common CIS address */
386 sdiodev->func_cis_ptr[0] = brcmf_sdioh_get_cisaddr(sdiodev,
387 SDIO_CCCR_CIS);
388 brcmf_dbg(INFO, "Card's Common CIS Ptr = 0x%x\n",
389 sdiodev->func_cis_ptr[0]);
391 /* Get the Card's function CIS (for each function) */
392 for (fbraddr = SDIO_FBR_BASE(1), func = 1;
393 func <= sdiodev->num_funcs; func++, fbraddr += SDIOD_FBR_SIZE) {
394 sdiodev->func_cis_ptr[func] =
395 brcmf_sdioh_get_cisaddr(sdiodev, SDIO_FBR_CIS + fbraddr);
396 brcmf_dbg(INFO, "Function %d CIS Ptr = 0x%x\n",
397 func, sdiodev->func_cis_ptr[func]);
400 /* Enable Function 1 */
401 sdio_claim_host(sdiodev->func[1]);
402 err_ret = sdio_enable_func(sdiodev->func[1]);
403 sdio_release_host(sdiodev->func[1]);
404 if (err_ret)
405 brcmf_dbg(ERROR, "Failed to enable F1 Err: 0x%08x\n", err_ret);
407 return false;
411 * Public entry points & extern's
413 int brcmf_sdioh_attach(struct brcmf_sdio_dev *sdiodev)
415 int err_ret = 0;
417 brcmf_dbg(TRACE, "\n");
419 sdiodev->num_funcs = 2;
421 sdio_claim_host(sdiodev->func[1]);
422 err_ret = sdio_set_block_size(sdiodev->func[1], SDIO_FUNC1_BLOCKSIZE);
423 sdio_release_host(sdiodev->func[1]);
424 if (err_ret) {
425 brcmf_dbg(ERROR, "Failed to set F1 blocksize\n");
426 goto out;
429 sdio_claim_host(sdiodev->func[2]);
430 err_ret = sdio_set_block_size(sdiodev->func[2], SDIO_FUNC2_BLOCKSIZE);
431 sdio_release_host(sdiodev->func[2]);
432 if (err_ret) {
433 brcmf_dbg(ERROR, "Failed to set F2 blocksize\n");
434 goto out;
437 brcmf_sdioh_enablefuncs(sdiodev);
439 out:
440 brcmf_dbg(TRACE, "Done\n");
441 return err_ret;
444 void brcmf_sdioh_detach(struct brcmf_sdio_dev *sdiodev)
446 brcmf_dbg(TRACE, "\n");
448 /* Disable Function 2 */
449 sdio_claim_host(sdiodev->func[2]);
450 sdio_disable_func(sdiodev->func[2]);
451 sdio_release_host(sdiodev->func[2]);
453 /* Disable Function 1 */
454 sdio_claim_host(sdiodev->func[1]);
455 sdio_disable_func(sdiodev->func[1]);
456 sdio_release_host(sdiodev->func[1]);
460 static int brcmf_ops_sdio_probe(struct sdio_func *func,
461 const struct sdio_device_id *id)
463 int ret = 0;
464 struct brcmf_sdio_dev *sdiodev;
465 struct brcmf_bus *bus_if;
466 brcmf_dbg(TRACE, "Enter\n");
467 brcmf_dbg(TRACE, "func->class=%x\n", func->class);
468 brcmf_dbg(TRACE, "sdio_vendor: 0x%04x\n", func->vendor);
469 brcmf_dbg(TRACE, "sdio_device: 0x%04x\n", func->device);
470 brcmf_dbg(TRACE, "Function#: 0x%04x\n", func->num);
472 if (func->num == 1) {
473 if (dev_get_drvdata(&func->card->dev)) {
474 brcmf_dbg(ERROR, "card private drvdata occupied\n");
475 return -ENXIO;
477 bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL);
478 if (!bus_if)
479 return -ENOMEM;
480 sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL);
481 if (!sdiodev) {
482 kfree(bus_if);
483 return -ENOMEM;
485 sdiodev->func[0] = func->card->sdio_func[0];
486 sdiodev->func[1] = func;
487 sdiodev->bus_if = bus_if;
488 bus_if->bus_priv = sdiodev;
489 bus_if->type = SDIO_BUS;
490 bus_if->align = BRCMF_SDALIGN;
491 dev_set_drvdata(&func->card->dev, sdiodev);
493 atomic_set(&sdiodev->suspend, false);
494 init_waitqueue_head(&sdiodev->request_byte_wait);
495 init_waitqueue_head(&sdiodev->request_word_wait);
496 init_waitqueue_head(&sdiodev->request_chain_wait);
497 init_waitqueue_head(&sdiodev->request_buffer_wait);
500 if (func->num == 2) {
501 sdiodev = dev_get_drvdata(&func->card->dev);
502 if ((!sdiodev) || (sdiodev->func[1]->card != func->card))
503 return -ENODEV;
504 sdiodev->func[2] = func;
506 bus_if = sdiodev->bus_if;
507 sdiodev->dev = &func->dev;
508 dev_set_drvdata(&func->dev, bus_if);
510 brcmf_dbg(TRACE, "F2 found, calling brcmf_sdio_probe...\n");
511 ret = brcmf_sdio_probe(sdiodev);
514 return ret;
517 static void brcmf_ops_sdio_remove(struct sdio_func *func)
519 struct brcmf_bus *bus_if;
520 struct brcmf_sdio_dev *sdiodev;
521 brcmf_dbg(TRACE, "Enter\n");
522 brcmf_dbg(INFO, "func->class=%x\n", func->class);
523 brcmf_dbg(INFO, "sdio_vendor: 0x%04x\n", func->vendor);
524 brcmf_dbg(INFO, "sdio_device: 0x%04x\n", func->device);
525 brcmf_dbg(INFO, "Function#: 0x%04x\n", func->num);
527 if (func->num == 2) {
528 bus_if = dev_get_drvdata(&func->dev);
529 sdiodev = bus_if->bus_priv;
530 brcmf_dbg(TRACE, "F2 found, calling brcmf_sdio_remove...\n");
531 brcmf_sdio_remove(sdiodev);
532 dev_set_drvdata(&func->card->dev, NULL);
533 dev_set_drvdata(&func->dev, NULL);
534 kfree(bus_if);
535 kfree(sdiodev);
539 #ifdef CONFIG_PM_SLEEP
540 static int brcmf_sdio_suspend(struct device *dev)
542 mmc_pm_flag_t sdio_flags;
543 struct sdio_func *func = dev_to_sdio_func(dev);
544 struct brcmf_sdio_dev *sdiodev = dev_get_drvdata(&func->card->dev);
545 int ret = 0;
547 brcmf_dbg(TRACE, "\n");
549 atomic_set(&sdiodev->suspend, true);
551 sdio_flags = sdio_get_host_pm_caps(sdiodev->func[1]);
552 if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
553 brcmf_dbg(ERROR, "Host can't keep power while suspended\n");
554 return -EINVAL;
557 ret = sdio_set_host_pm_flags(sdiodev->func[1], MMC_PM_KEEP_POWER);
558 if (ret) {
559 brcmf_dbg(ERROR, "Failed to set pm_flags\n");
560 return ret;
563 brcmf_sdio_wdtmr_enable(sdiodev, false);
565 return ret;
568 static int brcmf_sdio_resume(struct device *dev)
570 struct sdio_func *func = dev_to_sdio_func(dev);
571 struct brcmf_sdio_dev *sdiodev = dev_get_drvdata(&func->card->dev);
573 brcmf_sdio_wdtmr_enable(sdiodev, true);
574 atomic_set(&sdiodev->suspend, false);
575 return 0;
578 static const struct dev_pm_ops brcmf_sdio_pm_ops = {
579 .suspend = brcmf_sdio_suspend,
580 .resume = brcmf_sdio_resume,
582 #endif /* CONFIG_PM_SLEEP */
584 static struct sdio_driver brcmf_sdmmc_driver = {
585 .probe = brcmf_ops_sdio_probe,
586 .remove = brcmf_ops_sdio_remove,
587 .name = "brcmfmac",
588 .id_table = brcmf_sdmmc_ids,
589 #ifdef CONFIG_PM_SLEEP
590 .drv = {
591 .pm = &brcmf_sdio_pm_ops,
593 #endif /* CONFIG_PM_SLEEP */
596 static void __exit brcmf_sdio_exit(void)
598 brcmf_dbg(TRACE, "Enter\n");
600 sdio_unregister_driver(&brcmf_sdmmc_driver);
603 static int __init brcmf_sdio_init(void)
605 int ret;
607 brcmf_dbg(TRACE, "Enter\n");
609 ret = sdio_register_driver(&brcmf_sdmmc_driver);
611 if (ret)
612 brcmf_dbg(ERROR, "sdio_register_driver failed: %d\n", ret);
614 return ret;
617 module_init(brcmf_sdio_init);
618 module_exit(brcmf_sdio_exit);