blob: 70cd0e9cd52bf113bfc9063649a8ea0a0f282ee5 [file] [log] [blame]
Arend van Spriel5b435de2011-10-05 13:19:03 +02001/*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
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.
7 *
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.
15 */
16/* ****************** SDIO CARD Interface Functions **************************/
17
18#include <linux/types.h>
19#include <linux/netdevice.h>
Paul Gortmakeree40fa02011-05-27 16:14:23 -040020#include <linux/export.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020021#include <linux/pci.h>
22#include <linux/pci_ids.h>
23#include <linux/sched.h>
24#include <linux/completion.h>
Franky Lin354b75b2013-06-18 13:29:29 +020025#include <linux/scatterlist.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020026#include <linux/mmc/sdio.h>
27#include <linux/mmc/sdio_func.h>
28#include <linux/mmc/card.h>
Franky Lin354b75b2013-06-18 13:29:29 +020029#include <linux/mmc/host.h>
Hante Meuleman668761a2013-04-12 10:55:55 +020030#include <linux/platform_data/brcmfmac-sdio.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020031
32#include <defs.h>
33#include <brcm_hw_ids.h>
34#include <brcmu_utils.h>
35#include <brcmu_wifi.h>
36#include <soc.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020037#include "dhd_bus.h"
38#include "dhd_dbg.h"
39#include "sdio_host.h"
40
41#define SDIOH_API_ACCESS_RETRY_LIMIT 2
42
Hante Meuleman668761a2013-04-12 10:55:55 +020043
44static irqreturn_t brcmf_sdio_oob_irqhandler(int irq, void *dev_id)
Franky Linba89bf12012-04-27 18:56:59 -070045{
Hante Meuleman5b3c1832012-11-14 18:46:20 -080046 struct brcmf_bus *bus_if = dev_get_drvdata(dev_id);
47 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linba89bf12012-04-27 18:56:59 -070048
Hante Meuleman668761a2013-04-12 10:55:55 +020049 brcmf_dbg(INTR, "OOB intr triggered\n");
Franky Linba89bf12012-04-27 18:56:59 -070050
Hante Meuleman668761a2013-04-12 10:55:55 +020051 /* out-of-band interrupt is level-triggered which won't
Franky Linba89bf12012-04-27 18:56:59 -070052 * be cleared until dpc
53 */
54 if (sdiodev->irq_en) {
55 disable_irq_nosync(irq);
56 sdiodev->irq_en = false;
57 }
58
59 brcmf_sdbrcm_isr(sdiodev->bus);
60
61 return IRQ_HANDLED;
62}
63
Hante Meuleman668761a2013-04-12 10:55:55 +020064static void brcmf_sdio_ib_irqhandler(struct sdio_func *func)
Arend van Spriel5b435de2011-10-05 13:19:03 +020065{
Hante Meuleman5b3c1832012-11-14 18:46:20 -080066 struct brcmf_bus *bus_if = dev_get_drvdata(&func->dev);
67 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Arend van Spriel5b435de2011-10-05 13:19:03 +020068
Hante Meuleman668761a2013-04-12 10:55:55 +020069 brcmf_dbg(INTR, "IB intr triggered\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +020070
Arend van Spriel5b435de2011-10-05 13:19:03 +020071 brcmf_sdbrcm_isr(sdiodev->bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +020072}
73
Franky Linfbf59102011-12-16 18:36:53 -080074/* dummy handler for SDIO function 2 interrupt */
Franky Linba89bf12012-04-27 18:56:59 -070075static void brcmf_sdio_dummy_irqhandler(struct sdio_func *func)
Franky Linfbf59102011-12-16 18:36:53 -080076{
77}
78
Franky Linba89bf12012-04-27 18:56:59 -070079int brcmf_sdio_intr_register(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +020080{
Hante Meuleman668761a2013-04-12 10:55:55 +020081 int ret = 0;
82 u8 data;
83 unsigned long flags;
Arend van Spriel5b435de2011-10-05 13:19:03 +020084
Hante Meuleman668761a2013-04-12 10:55:55 +020085 if ((sdiodev->pdata) && (sdiodev->pdata->oob_irq_supported)) {
86 brcmf_dbg(SDIO, "Enter, register OOB IRQ %d\n",
87 sdiodev->pdata->oob_irq_nr);
88 ret = request_irq(sdiodev->pdata->oob_irq_nr,
89 brcmf_sdio_oob_irqhandler,
90 sdiodev->pdata->oob_irq_flags,
91 "brcmf_oob_intr",
92 &sdiodev->func[1]->dev);
93 if (ret != 0) {
94 brcmf_err("request_irq failed %d\n", ret);
95 return ret;
96 }
97 sdiodev->oob_irq_requested = true;
98 spin_lock_init(&sdiodev->irq_en_lock);
99 spin_lock_irqsave(&sdiodev->irq_en_lock, flags);
100 sdiodev->irq_en = true;
101 spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags);
102
103 ret = enable_irq_wake(sdiodev->pdata->oob_irq_nr);
104 if (ret != 0) {
105 brcmf_err("enable_irq_wake failed %d\n", ret);
106 return ret;
107 }
108 sdiodev->irq_wake = true;
109
110 sdio_claim_host(sdiodev->func[1]);
111
112 /* must configure SDIO_CCCR_IENx to enable irq */
113 data = brcmf_sdio_regrb(sdiodev, SDIO_CCCR_IENx, &ret);
114 data |= 1 << SDIO_FUNC_1 | 1 << SDIO_FUNC_2 | 1;
115 brcmf_sdio_regwb(sdiodev, SDIO_CCCR_IENx, data, &ret);
116
117 /* redirect, configure and enable io for interrupt signal */
118 data = SDIO_SEPINT_MASK | SDIO_SEPINT_OE;
119 if (sdiodev->pdata->oob_irq_flags & IRQF_TRIGGER_HIGH)
120 data |= SDIO_SEPINT_ACT_HI;
121 brcmf_sdio_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, data, &ret);
122
123 sdio_release_host(sdiodev->func[1]);
124 } else {
125 brcmf_dbg(SDIO, "Entering\n");
126 sdio_claim_host(sdiodev->func[1]);
127 sdio_claim_irq(sdiodev->func[1], brcmf_sdio_ib_irqhandler);
128 sdio_claim_irq(sdiodev->func[2], brcmf_sdio_dummy_irqhandler);
129 sdio_release_host(sdiodev->func[1]);
130 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200131
132 return 0;
133}
134
Franky Linba89bf12012-04-27 18:56:59 -0700135int brcmf_sdio_intr_unregister(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200136{
Arend van Sprielc3203372013-04-03 12:40:44 +0200137 brcmf_dbg(SDIO, "Entering\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200138
Hante Meuleman668761a2013-04-12 10:55:55 +0200139 if ((sdiodev->pdata) && (sdiodev->pdata->oob_irq_supported)) {
140 sdio_claim_host(sdiodev->func[1]);
141 brcmf_sdio_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, 0, NULL);
142 brcmf_sdio_regwb(sdiodev, SDIO_CCCR_IENx, 0, NULL);
143 sdio_release_host(sdiodev->func[1]);
144
145 if (sdiodev->oob_irq_requested) {
146 sdiodev->oob_irq_requested = false;
147 if (sdiodev->irq_wake) {
148 disable_irq_wake(sdiodev->pdata->oob_irq_nr);
149 sdiodev->irq_wake = false;
150 }
151 free_irq(sdiodev->pdata->oob_irq_nr,
152 &sdiodev->func[1]->dev);
153 sdiodev->irq_en = false;
154 }
155 } else {
156 sdio_claim_host(sdiodev->func[1]);
157 sdio_release_irq(sdiodev->func[2]);
158 sdio_release_irq(sdiodev->func[1]);
159 sdio_release_host(sdiodev->func[1]);
160 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200161
162 return 0;
163}
164
Franky Lin356bae62013-06-26 14:20:17 +0200165static int
Arend van Spriel5b435de2011-10-05 13:19:03 +0200166brcmf_sdcard_set_sbaddr_window(struct brcmf_sdio_dev *sdiodev, u32 address)
167{
Franky Lin7d9cfc22012-05-04 18:27:30 -0700168 int err = 0, i;
169 u8 addr[3];
170 s32 retry;
171
172 addr[0] = (address >> 8) & SBSDIO_SBADDRLOW_MASK;
173 addr[1] = (address >> 16) & SBSDIO_SBADDRMID_MASK;
174 addr[2] = (address >> 24) & SBSDIO_SBADDRHIGH_MASK;
175
176 for (i = 0; i < 3; i++) {
177 retry = 0;
178 do {
179 if (retry)
180 usleep_range(1000, 2000);
181 err = brcmf_sdioh_request_byte(sdiodev, SDIOH_WRITE,
182 SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW + i,
183 &addr[i]);
184 } while (err != 0 && retry++ < SDIOH_API_ACCESS_RETRY_LIMIT);
185
186 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100187 brcmf_err("failed at addr:0x%0x\n",
Franky Lin7d9cfc22012-05-04 18:27:30 -0700188 SBSDIO_FUNC1_SBADDRLOW + i);
189 break;
190 }
191 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200192
193 return err;
194}
195
Franky Lin356bae62013-06-26 14:20:17 +0200196static int
197brcmf_sdio_addrprep(struct brcmf_sdio_dev *sdiodev, uint width, u32 *addr)
198{
199 uint bar0 = *addr & ~SBSDIO_SB_OFT_ADDR_MASK;
200 int err = 0;
201
202 if (bar0 != sdiodev->sbwad) {
203 err = brcmf_sdcard_set_sbaddr_window(sdiodev, bar0);
204 if (err)
205 return err;
206
207 sdiodev->sbwad = bar0;
208 }
209
210 *addr &= SBSDIO_SB_OFT_ADDR_MASK;
211
212 if (width == 4)
213 *addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
214
215 return 0;
216}
217
Franky Lin45316032012-09-13 21:12:03 +0200218int
Franky Line9b8d912012-05-04 18:27:31 -0700219brcmf_sdio_regrw_helper(struct brcmf_sdio_dev *sdiodev, u32 addr,
220 void *data, bool write)
221{
222 u8 func_num, reg_size;
Franky Line9b8d912012-05-04 18:27:31 -0700223 s32 retry = 0;
224 int ret;
225
226 /*
227 * figure out how to read the register based on address range
Franky Lind8b3fc52012-05-04 18:27:38 -0700228 * 0x00 ~ 0x7FF: function 0 CCCR and FBR
Franky Line9b8d912012-05-04 18:27:31 -0700229 * 0x10000 ~ 0x1FFFF: function 1 miscellaneous registers
230 * The rest: function 1 silicon backplane core registers
231 */
Franky Lind8b3fc52012-05-04 18:27:38 -0700232 if ((addr & ~REG_F0_REG_MASK) == 0) {
Franky Line9b8d912012-05-04 18:27:31 -0700233 func_num = SDIO_FUNC_0;
234 reg_size = 1;
235 } else if ((addr & ~REG_F1_MISC_MASK) == 0) {
236 func_num = SDIO_FUNC_1;
237 reg_size = 1;
238 } else {
239 func_num = SDIO_FUNC_1;
240 reg_size = 4;
241
Franky Lin356bae62013-06-26 14:20:17 +0200242 brcmf_sdio_addrprep(sdiodev, reg_size, &addr);
Franky Line9b8d912012-05-04 18:27:31 -0700243 }
244
245 do {
246 if (!write)
247 memset(data, 0, reg_size);
248 if (retry) /* wait for 1 ms till bus get settled down */
249 usleep_range(1000, 2000);
250 if (reg_size == 1)
251 ret = brcmf_sdioh_request_byte(sdiodev, write,
252 func_num, addr, data);
253 else
254 ret = brcmf_sdioh_request_word(sdiodev, write,
255 func_num, addr, data, 4);
256 } while (ret != 0 && retry++ < SDIOH_API_ACCESS_RETRY_LIMIT);
257
Franky Lin5c15c232012-05-04 18:27:37 -0700258 if (ret != 0)
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100259 brcmf_err("failed with %d\n", ret);
Franky Line9b8d912012-05-04 18:27:31 -0700260
261 return ret;
262}
263
264u8 brcmf_sdio_regrb(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret)
265{
266 u8 data;
267 int retval;
268
Arend van Sprielc3203372013-04-03 12:40:44 +0200269 brcmf_dbg(SDIO, "addr:0x%08x\n", addr);
Franky Line9b8d912012-05-04 18:27:31 -0700270 retval = brcmf_sdio_regrw_helper(sdiodev, addr, &data, false);
Arend van Sprielc3203372013-04-03 12:40:44 +0200271 brcmf_dbg(SDIO, "data:0x%02x\n", data);
Franky Line9b8d912012-05-04 18:27:31 -0700272
273 if (ret)
274 *ret = retval;
275
276 return data;
277}
278
279u32 brcmf_sdio_regrl(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret)
280{
281 u32 data;
282 int retval;
283
Arend van Sprielc3203372013-04-03 12:40:44 +0200284 brcmf_dbg(SDIO, "addr:0x%08x\n", addr);
Franky Line9b8d912012-05-04 18:27:31 -0700285 retval = brcmf_sdio_regrw_helper(sdiodev, addr, &data, false);
Arend van Sprielc3203372013-04-03 12:40:44 +0200286 brcmf_dbg(SDIO, "data:0x%08x\n", data);
Franky Line9b8d912012-05-04 18:27:31 -0700287
288 if (ret)
289 *ret = retval;
290
291 return data;
292}
293
294void brcmf_sdio_regwb(struct brcmf_sdio_dev *sdiodev, u32 addr,
295 u8 data, int *ret)
296{
297 int retval;
298
Arend van Sprielc3203372013-04-03 12:40:44 +0200299 brcmf_dbg(SDIO, "addr:0x%08x, data:0x%02x\n", addr, data);
Franky Line9b8d912012-05-04 18:27:31 -0700300 retval = brcmf_sdio_regrw_helper(sdiodev, addr, &data, true);
301
302 if (ret)
303 *ret = retval;
304}
305
306void brcmf_sdio_regwl(struct brcmf_sdio_dev *sdiodev, u32 addr,
307 u32 data, int *ret)
308{
309 int retval;
310
Arend van Sprielc3203372013-04-03 12:40:44 +0200311 brcmf_dbg(SDIO, "addr:0x%08x, data:0x%08x\n", addr, data);
Franky Line9b8d912012-05-04 18:27:31 -0700312 retval = brcmf_sdio_regrw_helper(sdiodev, addr, &data, true);
313
314 if (ret)
315 *ret = retval;
316}
317
Franky Lin78b3f1c2013-06-18 13:29:28 +0200318/**
319 * brcmf_sdio_buffrw - SDIO interface function for block data access
320 * @sdiodev: brcmfmac sdio device
321 * @fn: SDIO function number
322 * @write: direction flag
323 * @addr: dongle memory address as source/destination
324 * @pkt: skb pointer
325 *
326 * This function takes the respbonsibility as the interface function to MMC
327 * stack for block data access. It assumes that the skb passed down by the
328 * caller has already been padded and aligned.
329 */
330static int brcmf_sdio_buffrw(struct brcmf_sdio_dev *sdiodev, uint fn,
Franky Lin354b75b2013-06-18 13:29:29 +0200331 bool write, u32 addr, struct sk_buff_head *pktlist)
Franky Lin78b3f1c2013-06-18 13:29:28 +0200332{
Franky Lin354b75b2013-06-18 13:29:29 +0200333 unsigned int req_sz, func_blk_sz, sg_cnt, sg_data_sz, pkt_offset;
334 unsigned int max_blks, max_req_sz;
335 unsigned short max_seg_sz, seg_sz;
336 unsigned char *pkt_data;
337 struct sk_buff *pkt_next = NULL;
338 struct mmc_request mmc_req;
339 struct mmc_command mmc_cmd;
340 struct mmc_data mmc_dat;
341 struct sg_table st;
342 struct scatterlist *sgl;
343 struct mmc_host *host;
344 int ret = 0;
345
346 if (!pktlist->qlen)
347 return -EINVAL;
Franky Lin78b3f1c2013-06-18 13:29:28 +0200348
349 brcmf_pm_resume_wait(sdiodev, &sdiodev->request_buffer_wait);
350 if (brcmf_pm_resume_error(sdiodev))
351 return -EIO;
352
353 /* Single skb use the standard mmc interface */
Franky Lin354b75b2013-06-18 13:29:29 +0200354 if (pktlist->qlen == 1) {
355 pkt_next = pktlist->next;
356 req_sz = pkt_next->len + 3;
357 req_sz &= (uint)~3;
Franky Lin78b3f1c2013-06-18 13:29:28 +0200358
359 if (write)
360 return sdio_memcpy_toio(sdiodev->func[fn], addr,
Franky Lin354b75b2013-06-18 13:29:29 +0200361 ((u8 *)(pkt_next->data)),
362 req_sz);
Franky Lin78b3f1c2013-06-18 13:29:28 +0200363 else if (fn == 1)
364 return sdio_memcpy_fromio(sdiodev->func[fn],
Franky Lin354b75b2013-06-18 13:29:29 +0200365 ((u8 *)(pkt_next->data)),
366 addr, req_sz);
Franky Lin78b3f1c2013-06-18 13:29:28 +0200367 else
368 /* function 2 read is FIFO operation */
369 return sdio_readsb(sdiodev->func[fn],
Franky Lin354b75b2013-06-18 13:29:29 +0200370 ((u8 *)(pkt_next->data)), addr,
371 req_sz);
Franky Lin78b3f1c2013-06-18 13:29:28 +0200372 }
373
Franky Lin354b75b2013-06-18 13:29:29 +0200374 host = sdiodev->func[fn]->card->host;
375 func_blk_sz = sdiodev->func[fn]->cur_blksize;
376 /* Blocks per command is limited by host count, host transfer
377 * size and the maximum for IO_RW_EXTENDED of 511 blocks.
378 */
379 max_blks = min_t(unsigned int, host->max_blk_count, 511u);
380 max_req_sz = min_t(unsigned int, host->max_req_size,
381 max_blks * func_blk_sz);
382 max_seg_sz = min_t(unsigned short, host->max_segs, SG_MAX_SINGLE_ALLOC);
383 max_seg_sz = min_t(unsigned short, max_seg_sz, pktlist->qlen);
384 seg_sz = pktlist->qlen;
385 pkt_offset = 0;
386 pkt_next = pktlist->next;
387
388 if (sg_alloc_table(&st, max_seg_sz, GFP_KERNEL))
389 return -ENOMEM;
390
391 while (seg_sz) {
392 req_sz = 0;
393 sg_cnt = 0;
394 memset(&mmc_req, 0, sizeof(struct mmc_request));
395 memset(&mmc_cmd, 0, sizeof(struct mmc_command));
396 memset(&mmc_dat, 0, sizeof(struct mmc_data));
397 sgl = st.sgl;
398 /* prep sg table */
399 while (pkt_next != (struct sk_buff *)pktlist) {
400 pkt_data = pkt_next->data + pkt_offset;
401 sg_data_sz = pkt_next->len - pkt_offset;
402 if (sg_data_sz > host->max_seg_size)
403 sg_data_sz = host->max_seg_size;
404 if (sg_data_sz > max_req_sz - req_sz)
405 sg_data_sz = max_req_sz - req_sz;
406
407 sg_set_buf(sgl, pkt_data, sg_data_sz);
408
409 sg_cnt++;
410 sgl = sg_next(sgl);
411 req_sz += sg_data_sz;
412 pkt_offset += sg_data_sz;
413 if (pkt_offset == pkt_next->len) {
414 pkt_offset = 0;
415 pkt_next = pkt_next->next;
416 }
417
418 if (req_sz >= max_req_sz || sg_cnt >= max_seg_sz)
419 break;
420 }
421 seg_sz -= sg_cnt;
422
423 if (req_sz % func_blk_sz != 0) {
424 brcmf_err("sg request length %u is not %u aligned\n",
425 req_sz, func_blk_sz);
426 sg_free_table(&st);
427 return -ENOTBLK;
428 }
429 mmc_dat.sg = st.sgl;
430 mmc_dat.sg_len = sg_cnt;
431 mmc_dat.blksz = func_blk_sz;
432 mmc_dat.blocks = req_sz / func_blk_sz;
433 mmc_dat.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
434 mmc_cmd.opcode = SD_IO_RW_EXTENDED;
435 mmc_cmd.arg = write ? 1<<31 : 0; /* write flag */
436 mmc_cmd.arg |= (fn & 0x7) << 28; /* SDIO func num */
437 mmc_cmd.arg |= 1<<27; /* block mode */
438 /* incrementing addr for function 1 */
439 mmc_cmd.arg |= (fn == 1) ? 1<<26 : 0;
440 mmc_cmd.arg |= (addr & 0x1FFFF) << 9; /* address */
441 mmc_cmd.arg |= mmc_dat.blocks & 0x1FF; /* block count */
442 mmc_cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
443 mmc_req.cmd = &mmc_cmd;
444 mmc_req.data = &mmc_dat;
445 if (fn == 1)
446 addr += req_sz;
447
448 mmc_set_data_timeout(&mmc_dat, sdiodev->func[fn]->card);
449 mmc_wait_for_req(host, &mmc_req);
450
451 ret = mmc_cmd.error ? mmc_cmd.error : mmc_dat.error;
452 if (ret != 0) {
453 brcmf_err("CMD53 sg block %s failed %d\n",
454 write ? "write" : "read", ret);
455 ret = -EIO;
456 break;
457 }
458 }
459
460 sg_free_table(&st);
461
462 return ret;
Franky Lin78b3f1c2013-06-18 13:29:28 +0200463}
464
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800465int
466brcmf_sdcard_recv_buf(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn,
467 uint flags, u8 *buf, uint nbytes)
468{
469 struct sk_buff *mypkt;
470 int err;
471
472 mypkt = brcmu_pkt_buf_get_skb(nbytes);
473 if (!mypkt) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100474 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n",
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800475 nbytes);
476 return -EIO;
477 }
478
479 err = brcmf_sdcard_recv_pkt(sdiodev, addr, fn, flags, mypkt);
480 if (!err)
481 memcpy(buf, mypkt->data, nbytes);
482
483 brcmu_pkt_buf_free_skb(mypkt);
484 return err;
485}
486
487int
488brcmf_sdcard_recv_pkt(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn,
489 uint flags, struct sk_buff *pkt)
490{
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800491 uint width;
492 int err = 0;
Franky Lin354b75b2013-06-18 13:29:29 +0200493 struct sk_buff_head pkt_list;
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800494
Arend van Sprielc3203372013-04-03 12:40:44 +0200495 brcmf_dbg(SDIO, "fun = %d, addr = 0x%x, size = %d\n",
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800496 fn, addr, pkt->len);
497
498 width = (flags & SDIO_REQ_4BYTE) ? 4 : 2;
Franky Lin356bae62013-06-26 14:20:17 +0200499 err = brcmf_sdio_addrprep(sdiodev, width, &addr);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800500 if (err)
Franky Lin7057fd02012-09-13 21:12:02 +0200501 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200502
Franky Lin354b75b2013-06-18 13:29:29 +0200503 skb_queue_head_init(&pkt_list);
504 skb_queue_tail(&pkt_list, pkt);
505 err = brcmf_sdio_buffrw(sdiodev, fn, false, addr, &pkt_list);
506 skb_dequeue_tail(&pkt_list);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800507
Franky Lin7057fd02012-09-13 21:12:02 +0200508done:
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800509 return err;
510}
511
512int brcmf_sdcard_recv_chain(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn,
513 uint flags, struct sk_buff_head *pktq)
514{
515 uint incr_fix;
516 uint width;
517 int err = 0;
518
Arend van Sprielc3203372013-04-03 12:40:44 +0200519 brcmf_dbg(SDIO, "fun = %d, addr = 0x%x, size = %d\n",
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800520 fn, addr, pktq->qlen);
521
Arend van Spriel5b435de2011-10-05 13:19:03 +0200522 width = (flags & SDIO_REQ_4BYTE) ? 4 : 2;
Franky Lin356bae62013-06-26 14:20:17 +0200523 err = brcmf_sdio_addrprep(sdiodev, width, &addr);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800524 if (err)
Franky Lin7057fd02012-09-13 21:12:02 +0200525 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200526
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800527 incr_fix = (flags & SDIO_REQ_FIXED) ? SDIOH_DATA_FIX : SDIOH_DATA_INC;
Franky Lin354b75b2013-06-18 13:29:29 +0200528 err = brcmf_sdio_buffrw(sdiodev, fn, false, addr, pktq);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200529
Franky Lin7057fd02012-09-13 21:12:02 +0200530done:
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800531 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200532}
533
534int
535brcmf_sdcard_send_buf(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn,
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800536 uint flags, u8 *buf, uint nbytes)
537{
538 struct sk_buff *mypkt;
539 int err;
540
541 mypkt = brcmu_pkt_buf_get_skb(nbytes);
542 if (!mypkt) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100543 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n",
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800544 nbytes);
545 return -EIO;
546 }
547
548 memcpy(mypkt->data, buf, nbytes);
549 err = brcmf_sdcard_send_pkt(sdiodev, addr, fn, flags, mypkt);
550
551 brcmu_pkt_buf_free_skb(mypkt);
552 return err;
553
554}
555
556int
557brcmf_sdcard_send_pkt(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn,
558 uint flags, struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200559{
Arend van Spriel5b435de2011-10-05 13:19:03 +0200560 uint width;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200561 int err = 0;
Franky Lin354b75b2013-06-18 13:29:29 +0200562 struct sk_buff_head pkt_list;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200563
Arend van Sprielc3203372013-04-03 12:40:44 +0200564 brcmf_dbg(SDIO, "fun = %d, addr = 0x%x, size = %d\n",
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800565 fn, addr, pkt->len);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200566
Arend van Spriel5b435de2011-10-05 13:19:03 +0200567 width = (flags & SDIO_REQ_4BYTE) ? 4 : 2;
Franky Lin356bae62013-06-26 14:20:17 +0200568 brcmf_sdio_addrprep(sdiodev, width, &addr);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200569
Franky Lin354b75b2013-06-18 13:29:29 +0200570 skb_queue_head_init(&pkt_list);
571 skb_queue_tail(&pkt_list, pkt);
572 err = brcmf_sdio_buffrw(sdiodev, fn, true, addr, &pkt_list);
573 skb_dequeue_tail(&pkt_list);
Franky Lin7057fd02012-09-13 21:12:02 +0200574
Franky Lin7057fd02012-09-13 21:12:02 +0200575 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200576}
577
Franky Linba540b02013-04-11 13:28:47 +0200578int
579brcmf_sdio_ramrw(struct brcmf_sdio_dev *sdiodev, bool write, u32 address,
580 u8 *data, uint size)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200581{
Franky Linba540b02013-04-11 13:28:47 +0200582 int bcmerror = 0;
583 struct sk_buff *pkt;
584 u32 sdaddr;
585 uint dsize;
Franky Lin354b75b2013-06-18 13:29:29 +0200586 struct sk_buff_head pkt_list;
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800587
Franky Linba540b02013-04-11 13:28:47 +0200588 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
589 pkt = dev_alloc_skb(dsize);
590 if (!pkt) {
591 brcmf_err("dev_alloc_skb failed: len %d\n", dsize);
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800592 return -EIO;
593 }
Franky Linba540b02013-04-11 13:28:47 +0200594 pkt->priority = 0;
Franky Lin354b75b2013-06-18 13:29:29 +0200595 skb_queue_head_init(&pkt_list);
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800596
Franky Linba540b02013-04-11 13:28:47 +0200597 /* Determine initial transfer parameters */
598 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
599 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
600 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
601 else
602 dsize = size;
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800603
Franky Linba540b02013-04-11 13:28:47 +0200604 sdio_claim_host(sdiodev->func[1]);
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800605
Franky Linba540b02013-04-11 13:28:47 +0200606 /* Do the transfer(s) */
607 while (size) {
608 /* Set the backplane window to include the start address */
609 bcmerror = brcmf_sdcard_set_sbaddr_window(sdiodev, address);
610 if (bcmerror)
611 break;
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800612
Franky Linba540b02013-04-11 13:28:47 +0200613 brcmf_dbg(SDIO, "%s %d bytes at offset 0x%08x in window 0x%08x\n",
614 write ? "write" : "read", dsize,
615 sdaddr, address & SBSDIO_SBWINDOW_MASK);
616
617 sdaddr &= SBSDIO_SB_OFT_ADDR_MASK;
618 sdaddr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
619
620 skb_put(pkt, dsize);
621 if (write)
622 memcpy(pkt->data, data, dsize);
Franky Lin354b75b2013-06-18 13:29:29 +0200623 skb_queue_tail(&pkt_list, pkt);
Franky Lin78b3f1c2013-06-18 13:29:28 +0200624 bcmerror = brcmf_sdio_buffrw(sdiodev, SDIO_FUNC_1, write,
Franky Lin354b75b2013-06-18 13:29:29 +0200625 sdaddr, &pkt_list);
626 skb_dequeue_tail(&pkt_list);
Franky Linba540b02013-04-11 13:28:47 +0200627 if (bcmerror) {
628 brcmf_err("membytes transfer failed\n");
629 break;
630 }
631 if (!write)
632 memcpy(data, pkt->data, dsize);
633 skb_trim(pkt, dsize);
634
635 /* Adjust for next transfer (if any) */
636 size -= dsize;
637 if (size) {
638 data += dsize;
639 address += dsize;
640 sdaddr = 0;
641 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
642 }
643 }
644
645 dev_kfree_skb(pkt);
646
647 /* Return the window to backplane enumeration space for core access */
648 if (brcmf_sdcard_set_sbaddr_window(sdiodev, sdiodev->sbwad))
649 brcmf_err("FAILED to set window back to 0x%x\n",
650 sdiodev->sbwad);
651
652 sdio_release_host(sdiodev->func[1]);
653
654 return bcmerror;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200655}
656
657int brcmf_sdcard_abort(struct brcmf_sdio_dev *sdiodev, uint fn)
658{
659 char t_func = (char)fn;
Arend van Sprielc3203372013-04-03 12:40:44 +0200660 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200661
662 /* issue abort cmd52 command through F0 */
663 brcmf_sdioh_request_byte(sdiodev, SDIOH_WRITE, SDIO_FUNC_0,
664 SDIO_CCCR_ABORT, &t_func);
665
Arend van Sprielc3203372013-04-03 12:40:44 +0200666 brcmf_dbg(SDIO, "Exit\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200667 return 0;
668}
669
670int brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
671{
672 u32 regs = 0;
673 int ret = 0;
674
675 ret = brcmf_sdioh_attach(sdiodev);
676 if (ret)
677 goto out;
678
679 regs = SI_ENUM_BASE;
680
Arend van Spriel5b435de2011-10-05 13:19:03 +0200681 /* try to attach to the target device */
Franky Lin4175b882011-11-22 17:21:49 -0800682 sdiodev->bus = brcmf_sdbrcm_probe(regs, sdiodev);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200683 if (!sdiodev->bus) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100684 brcmf_err("device attach failed\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200685 ret = -ENODEV;
686 goto out;
687 }
688
689out:
690 if (ret)
691 brcmf_sdio_remove(sdiodev);
692
693 return ret;
694}
695EXPORT_SYMBOL(brcmf_sdio_probe);
696
697int brcmf_sdio_remove(struct brcmf_sdio_dev *sdiodev)
698{
Hante Meuleman2def5c12012-11-14 18:46:19 -0800699 sdiodev->bus_if->state = BRCMF_BUS_DOWN;
700
Arend van Spriel5b435de2011-10-05 13:19:03 +0200701 if (sdiodev->bus) {
702 brcmf_sdbrcm_disconnect(sdiodev->bus);
703 sdiodev->bus = NULL;
704 }
705
706 brcmf_sdioh_detach(sdiodev);
707
708 sdiodev->sbwad = 0;
709
710 return 0;
711}
712EXPORT_SYMBOL(brcmf_sdio_remove);
713
714void brcmf_sdio_wdtmr_enable(struct brcmf_sdio_dev *sdiodev, bool enable)
715{
716 if (enable)
717 brcmf_sdbrcm_wd_timer(sdiodev->bus, BRCMF_WD_POLL_MS);
718 else
719 brcmf_sdbrcm_wd_timer(sdiodev->bus, 0);
720}