blob: a4dae21ef9a38ac922aad97f4067637bce1ba46e [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>
Arend van Spriel5b435de2011-10-05 13:19:03 +020020#include <linux/pci.h>
21#include <linux/pci_ids.h>
22#include <linux/sched.h>
23#include <linux/completion.h>
Franky Lin354b75b2013-06-18 13:29:29 +020024#include <linux/scatterlist.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020025#include <linux/mmc/sdio.h>
Arend van Spriele2dc9ee2013-12-12 11:58:50 +010026#include <linux/mmc/core.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020027#include <linux/mmc/sdio_func.h>
Arend van Spriele2dc9ee2013-12-12 11:58:50 +010028#include <linux/mmc/sdio_ids.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020029#include <linux/mmc/card.h>
Arend van Spriele2dc9ee2013-12-12 11:58:50 +010030#include <linux/mmc/host.h>
31#include <linux/platform_device.h>
Hante Meuleman668761a2013-04-12 10:55:55 +020032#include <linux/platform_data/brcmfmac-sdio.h>
Arend van Spriele2dc9ee2013-12-12 11:58:50 +010033#include <linux/suspend.h>
34#include <linux/errno.h>
35#include <linux/module.h>
36#include <net/cfg80211.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020037
38#include <defs.h>
39#include <brcm_hw_ids.h>
40#include <brcmu_utils.h>
41#include <brcmu_wifi.h>
42#include <soc.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020043#include "dhd_bus.h"
44#include "dhd_dbg.h"
45#include "sdio_host.h"
Arend van Spriele2dc9ee2013-12-12 11:58:50 +010046#include "sdio_chip.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020047
48#define SDIOH_API_ACCESS_RETRY_LIMIT 2
49
Arend van Spriele49b06b2013-12-12 11:58:51 +010050#define SDIO_VENDOR_ID_BROADCOM 0x02d0
51
52#define DMA_ALIGN_MASK 0x03
53
54#define SDIO_FUNC1_BLOCKSIZE 64
55#define SDIO_FUNC2_BLOCKSIZE 512
Arend van Spriel71370eb2013-12-12 11:58:53 +010056/* Maximum milliseconds to wait for F2 to come up */
57#define SDIO_WAIT_F2RDY 3000
58
Hante Meuleman668761a2013-04-12 10:55:55 +020059
Arend van Spriela39be272013-12-12 11:58:58 +010060static irqreturn_t brcmf_sdiod_oob_irqhandler(int irq, void *dev_id)
Franky Linba89bf12012-04-27 18:56:59 -070061{
Hante Meuleman5b3c1832012-11-14 18:46:20 -080062 struct brcmf_bus *bus_if = dev_get_drvdata(dev_id);
63 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linba89bf12012-04-27 18:56:59 -070064
Hante Meuleman668761a2013-04-12 10:55:55 +020065 brcmf_dbg(INTR, "OOB intr triggered\n");
Franky Linba89bf12012-04-27 18:56:59 -070066
Hante Meuleman668761a2013-04-12 10:55:55 +020067 /* out-of-band interrupt is level-triggered which won't
Franky Linba89bf12012-04-27 18:56:59 -070068 * be cleared until dpc
69 */
70 if (sdiodev->irq_en) {
71 disable_irq_nosync(irq);
72 sdiodev->irq_en = false;
73 }
74
Arend van Spriel82d7f3c2013-12-12 11:59:04 +010075 brcmf_sdio_isr(sdiodev->bus);
Franky Linba89bf12012-04-27 18:56:59 -070076
77 return IRQ_HANDLED;
78}
79
Arend van Spriela39be272013-12-12 11:58:58 +010080static void brcmf_sdiod_ib_irqhandler(struct sdio_func *func)
Arend van Spriel5b435de2011-10-05 13:19:03 +020081{
Hante Meuleman5b3c1832012-11-14 18:46:20 -080082 struct brcmf_bus *bus_if = dev_get_drvdata(&func->dev);
83 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Arend van Spriel5b435de2011-10-05 13:19:03 +020084
Hante Meuleman668761a2013-04-12 10:55:55 +020085 brcmf_dbg(INTR, "IB intr triggered\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +020086
Arend van Spriel82d7f3c2013-12-12 11:59:04 +010087 brcmf_sdio_isr(sdiodev->bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +020088}
89
Franky Linfbf59102011-12-16 18:36:53 -080090/* dummy handler for SDIO function 2 interrupt */
Arend van Spriela39be272013-12-12 11:58:58 +010091static void brcmf_sdiod_dummy_irqhandler(struct sdio_func *func)
Franky Linfbf59102011-12-16 18:36:53 -080092{
93}
94
Arend van Spriela39be272013-12-12 11:58:58 +010095static bool brcmf_sdiod_pm_resume_error(struct brcmf_sdio_dev *sdiodev)
Arend van Spriele49b06b2013-12-12 11:58:51 +010096{
97 bool is_err = false;
98#ifdef CONFIG_PM_SLEEP
99 is_err = atomic_read(&sdiodev->suspend);
100#endif
101 return is_err;
102}
103
Arend van Spriela39be272013-12-12 11:58:58 +0100104static void brcmf_sdiod_pm_resume_wait(struct brcmf_sdio_dev *sdiodev,
105 wait_queue_head_t *wq)
Arend van Spriele49b06b2013-12-12 11:58:51 +0100106{
107#ifdef CONFIG_PM_SLEEP
108 int retry = 0;
109 while (atomic_read(&sdiodev->suspend) && retry++ != 30)
110 wait_event_timeout(*wq, false, HZ/100);
111#endif
112}
113
Arend van Spriela39be272013-12-12 11:58:58 +0100114int brcmf_sdiod_intr_register(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200115{
Hante Meuleman668761a2013-04-12 10:55:55 +0200116 int ret = 0;
117 u8 data;
118 unsigned long flags;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200119
Hante Meuleman668761a2013-04-12 10:55:55 +0200120 if ((sdiodev->pdata) && (sdiodev->pdata->oob_irq_supported)) {
121 brcmf_dbg(SDIO, "Enter, register OOB IRQ %d\n",
122 sdiodev->pdata->oob_irq_nr);
123 ret = request_irq(sdiodev->pdata->oob_irq_nr,
Arend van Spriela39be272013-12-12 11:58:58 +0100124 brcmf_sdiod_oob_irqhandler,
Hante Meuleman668761a2013-04-12 10:55:55 +0200125 sdiodev->pdata->oob_irq_flags,
126 "brcmf_oob_intr",
127 &sdiodev->func[1]->dev);
128 if (ret != 0) {
129 brcmf_err("request_irq failed %d\n", ret);
130 return ret;
131 }
132 sdiodev->oob_irq_requested = true;
133 spin_lock_init(&sdiodev->irq_en_lock);
134 spin_lock_irqsave(&sdiodev->irq_en_lock, flags);
135 sdiodev->irq_en = true;
136 spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags);
137
138 ret = enable_irq_wake(sdiodev->pdata->oob_irq_nr);
139 if (ret != 0) {
140 brcmf_err("enable_irq_wake failed %d\n", ret);
141 return ret;
142 }
143 sdiodev->irq_wake = true;
144
145 sdio_claim_host(sdiodev->func[1]);
146
147 /* must configure SDIO_CCCR_IENx to enable irq */
Arend van Spriela39be272013-12-12 11:58:58 +0100148 data = brcmf_sdiod_regrb(sdiodev, SDIO_CCCR_IENx, &ret);
Hante Meuleman668761a2013-04-12 10:55:55 +0200149 data |= 1 << SDIO_FUNC_1 | 1 << SDIO_FUNC_2 | 1;
Arend van Spriela39be272013-12-12 11:58:58 +0100150 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_IENx, data, &ret);
Hante Meuleman668761a2013-04-12 10:55:55 +0200151
152 /* redirect, configure and enable io for interrupt signal */
153 data = SDIO_SEPINT_MASK | SDIO_SEPINT_OE;
154 if (sdiodev->pdata->oob_irq_flags & IRQF_TRIGGER_HIGH)
155 data |= SDIO_SEPINT_ACT_HI;
Arend van Spriela39be272013-12-12 11:58:58 +0100156 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, data, &ret);
Hante Meuleman668761a2013-04-12 10:55:55 +0200157
158 sdio_release_host(sdiodev->func[1]);
159 } else {
160 brcmf_dbg(SDIO, "Entering\n");
161 sdio_claim_host(sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +0100162 sdio_claim_irq(sdiodev->func[1], brcmf_sdiod_ib_irqhandler);
163 sdio_claim_irq(sdiodev->func[2], brcmf_sdiod_dummy_irqhandler);
Hante Meuleman668761a2013-04-12 10:55:55 +0200164 sdio_release_host(sdiodev->func[1]);
165 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200166
167 return 0;
168}
169
Arend van Spriela39be272013-12-12 11:58:58 +0100170int brcmf_sdiod_intr_unregister(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200171{
Arend van Sprielc3203372013-04-03 12:40:44 +0200172 brcmf_dbg(SDIO, "Entering\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200173
Hante Meuleman668761a2013-04-12 10:55:55 +0200174 if ((sdiodev->pdata) && (sdiodev->pdata->oob_irq_supported)) {
175 sdio_claim_host(sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +0100176 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, 0, NULL);
177 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_IENx, 0, NULL);
Hante Meuleman668761a2013-04-12 10:55:55 +0200178 sdio_release_host(sdiodev->func[1]);
179
180 if (sdiodev->oob_irq_requested) {
181 sdiodev->oob_irq_requested = false;
182 if (sdiodev->irq_wake) {
183 disable_irq_wake(sdiodev->pdata->oob_irq_nr);
184 sdiodev->irq_wake = false;
185 }
186 free_irq(sdiodev->pdata->oob_irq_nr,
187 &sdiodev->func[1]->dev);
188 sdiodev->irq_en = false;
189 }
190 } else {
191 sdio_claim_host(sdiodev->func[1]);
192 sdio_release_irq(sdiodev->func[2]);
193 sdio_release_irq(sdiodev->func[1]);
194 sdio_release_host(sdiodev->func[1]);
195 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200196
197 return 0;
198}
199
Arend van Spriela39be272013-12-12 11:58:58 +0100200static inline int brcmf_sdiod_f0_write_byte(struct brcmf_sdio_dev *sdiodev,
Arend van Spriele49b06b2013-12-12 11:58:51 +0100201 uint regaddr, u8 *byte)
202{
203 struct sdio_func *sdfunc = sdiodev->func[0];
204 int err_ret;
205
206 /*
207 * Can only directly write to some F0 registers.
208 * Handle F2 enable/disable and Abort command
209 * as a special case.
210 */
Arend van Spriel71370eb2013-12-12 11:58:53 +0100211 if ((regaddr == SDIO_CCCR_ABORT) ||
212 (regaddr == SDIO_CCCR_IENx)) {
Arend van Spriele49b06b2013-12-12 11:58:51 +0100213 sdfunc = kmemdup(sdiodev->func[0], sizeof(struct sdio_func),
214 GFP_KERNEL);
215 if (!sdfunc)
216 return -ENOMEM;
217 sdfunc->num = 0;
218 sdio_writeb(sdfunc, *byte, regaddr, &err_ret);
219 kfree(sdfunc);
220 } else if (regaddr < 0xF0) {
221 brcmf_err("F0 Wr:0x%02x: write disallowed\n", regaddr);
222 err_ret = -EPERM;
223 } else {
224 sdio_f0_writeb(sdfunc, *byte, regaddr, &err_ret);
225 }
226
227 return err_ret;
228}
229
Arend van Spriela39be272013-12-12 11:58:58 +0100230static int brcmf_sdiod_request_byte(struct brcmf_sdio_dev *sdiodev, uint rw,
Arend van Spriele49b06b2013-12-12 11:58:51 +0100231 uint func, uint regaddr, u8 *byte)
232{
233 int err_ret;
234
235 brcmf_dbg(SDIO, "rw=%d, func=%d, addr=0x%05x\n", rw, func, regaddr);
236
Arend van Spriela39be272013-12-12 11:58:58 +0100237 brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_byte_wait);
238 if (brcmf_sdiod_pm_resume_error(sdiodev))
Arend van Spriele49b06b2013-12-12 11:58:51 +0100239 return -EIO;
240
241 if (rw && func == 0) {
242 /* handle F0 separately */
Arend van Spriela39be272013-12-12 11:58:58 +0100243 err_ret = brcmf_sdiod_f0_write_byte(sdiodev, regaddr, byte);
Arend van Spriele49b06b2013-12-12 11:58:51 +0100244 } else {
245 if (rw) /* CMD52 Write */
246 sdio_writeb(sdiodev->func[func], *byte, regaddr,
247 &err_ret);
248 else if (func == 0) {
249 *byte = sdio_f0_readb(sdiodev->func[func], regaddr,
250 &err_ret);
251 } else {
252 *byte = sdio_readb(sdiodev->func[func], regaddr,
253 &err_ret);
254 }
255 }
256
257 if (err_ret) {
258 /*
259 * SleepCSR register access can fail when
260 * waking up the device so reduce this noise
261 * in the logs.
262 */
263 if (regaddr != SBSDIO_FUNC1_SLEEPCSR)
264 brcmf_err("Failed to %s byte F%d:@0x%05x=%02x, Err: %d\n",
265 rw ? "write" : "read", func, regaddr, *byte,
266 err_ret);
267 else
268 brcmf_dbg(SDIO, "Failed to %s byte F%d:@0x%05x=%02x, Err: %d\n",
269 rw ? "write" : "read", func, regaddr, *byte,
270 err_ret);
271 }
272 return err_ret;
273}
274
Arend van Spriela39be272013-12-12 11:58:58 +0100275static int brcmf_sdiod_request_word(struct brcmf_sdio_dev *sdiodev, uint rw,
Arend van Spriele49b06b2013-12-12 11:58:51 +0100276 uint func, uint addr, u32 *word,
277 uint nbytes)
278{
279 int err_ret = -EIO;
280
281 if (func == 0) {
282 brcmf_err("Only CMD52 allowed to F0\n");
283 return -EINVAL;
284 }
285
286 brcmf_dbg(SDIO, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n",
287 rw, func, addr, nbytes);
288
Arend van Spriela39be272013-12-12 11:58:58 +0100289 brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_word_wait);
290 if (brcmf_sdiod_pm_resume_error(sdiodev))
Arend van Spriele49b06b2013-12-12 11:58:51 +0100291 return -EIO;
292
293 if (rw) { /* CMD52 Write */
294 if (nbytes == 4)
295 sdio_writel(sdiodev->func[func], *word, addr,
296 &err_ret);
297 else if (nbytes == 2)
298 sdio_writew(sdiodev->func[func], (*word & 0xFFFF),
299 addr, &err_ret);
300 else
301 brcmf_err("Invalid nbytes: %d\n", nbytes);
302 } else { /* CMD52 Read */
303 if (nbytes == 4)
304 *word = sdio_readl(sdiodev->func[func], addr, &err_ret);
305 else if (nbytes == 2)
306 *word = sdio_readw(sdiodev->func[func], addr,
307 &err_ret) & 0xFFFF;
308 else
309 brcmf_err("Invalid nbytes: %d\n", nbytes);
310 }
311
312 if (err_ret)
313 brcmf_err("Failed to %s word, Err: 0x%08x\n",
314 rw ? "write" : "read", err_ret);
315
316 return err_ret;
317}
318
Franky Lin356bae62013-06-26 14:20:17 +0200319static int
Arend van Spriela39be272013-12-12 11:58:58 +0100320brcmf_sdiod_set_sbaddr_window(struct brcmf_sdio_dev *sdiodev, u32 address)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200321{
Franky Lin7d9cfc22012-05-04 18:27:30 -0700322 int err = 0, i;
323 u8 addr[3];
324 s32 retry;
325
326 addr[0] = (address >> 8) & SBSDIO_SBADDRLOW_MASK;
327 addr[1] = (address >> 16) & SBSDIO_SBADDRMID_MASK;
328 addr[2] = (address >> 24) & SBSDIO_SBADDRHIGH_MASK;
329
330 for (i = 0; i < 3; i++) {
331 retry = 0;
332 do {
333 if (retry)
334 usleep_range(1000, 2000);
Arend van Spriela39be272013-12-12 11:58:58 +0100335 err = brcmf_sdiod_request_byte(sdiodev, SDIOH_WRITE,
Franky Lin7d9cfc22012-05-04 18:27:30 -0700336 SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW + i,
337 &addr[i]);
338 } while (err != 0 && retry++ < SDIOH_API_ACCESS_RETRY_LIMIT);
339
340 if (err) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100341 brcmf_err("failed at addr:0x%0x\n",
Franky Lin7d9cfc22012-05-04 18:27:30 -0700342 SBSDIO_FUNC1_SBADDRLOW + i);
343 break;
344 }
345 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200346
347 return err;
348}
349
Franky Lin356bae62013-06-26 14:20:17 +0200350static int
Arend van Spriela39be272013-12-12 11:58:58 +0100351brcmf_sdiod_addrprep(struct brcmf_sdio_dev *sdiodev, uint width, u32 *addr)
Franky Lin356bae62013-06-26 14:20:17 +0200352{
353 uint bar0 = *addr & ~SBSDIO_SB_OFT_ADDR_MASK;
354 int err = 0;
355
356 if (bar0 != sdiodev->sbwad) {
Arend van Spriela39be272013-12-12 11:58:58 +0100357 err = brcmf_sdiod_set_sbaddr_window(sdiodev, bar0);
Franky Lin356bae62013-06-26 14:20:17 +0200358 if (err)
359 return err;
360
361 sdiodev->sbwad = bar0;
362 }
363
364 *addr &= SBSDIO_SB_OFT_ADDR_MASK;
365
366 if (width == 4)
367 *addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
368
369 return 0;
370}
371
Arend van Spriela39be272013-12-12 11:58:58 +0100372static int brcmf_sdiod_regrw_helper(struct brcmf_sdio_dev *sdiodev, u32 addr,
373 void *data, bool write)
Franky Line9b8d912012-05-04 18:27:31 -0700374{
375 u8 func_num, reg_size;
Franky Line9b8d912012-05-04 18:27:31 -0700376 s32 retry = 0;
377 int ret;
378
379 /*
380 * figure out how to read the register based on address range
Franky Lind8b3fc52012-05-04 18:27:38 -0700381 * 0x00 ~ 0x7FF: function 0 CCCR and FBR
Franky Line9b8d912012-05-04 18:27:31 -0700382 * 0x10000 ~ 0x1FFFF: function 1 miscellaneous registers
383 * The rest: function 1 silicon backplane core registers
384 */
Franky Lind8b3fc52012-05-04 18:27:38 -0700385 if ((addr & ~REG_F0_REG_MASK) == 0) {
Franky Line9b8d912012-05-04 18:27:31 -0700386 func_num = SDIO_FUNC_0;
387 reg_size = 1;
388 } else if ((addr & ~REG_F1_MISC_MASK) == 0) {
389 func_num = SDIO_FUNC_1;
390 reg_size = 1;
391 } else {
392 func_num = SDIO_FUNC_1;
393 reg_size = 4;
394
Arend van Spriela39be272013-12-12 11:58:58 +0100395 ret = brcmf_sdiod_addrprep(sdiodev, reg_size, &addr);
Arend van Sprielfcaac2b2013-10-15 15:44:53 +0200396 if (ret)
397 goto done;
Franky Line9b8d912012-05-04 18:27:31 -0700398 }
399
400 do {
401 if (!write)
402 memset(data, 0, reg_size);
403 if (retry) /* wait for 1 ms till bus get settled down */
404 usleep_range(1000, 2000);
405 if (reg_size == 1)
Arend van Spriela39be272013-12-12 11:58:58 +0100406 ret = brcmf_sdiod_request_byte(sdiodev, write,
Franky Line9b8d912012-05-04 18:27:31 -0700407 func_num, addr, data);
408 else
Arend van Spriela39be272013-12-12 11:58:58 +0100409 ret = brcmf_sdiod_request_word(sdiodev, write,
Franky Line9b8d912012-05-04 18:27:31 -0700410 func_num, addr, data, 4);
411 } while (ret != 0 && retry++ < SDIOH_API_ACCESS_RETRY_LIMIT);
412
Arend van Sprielfcaac2b2013-10-15 15:44:53 +0200413done:
Franky Lin5c15c232012-05-04 18:27:37 -0700414 if (ret != 0)
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100415 brcmf_err("failed with %d\n", ret);
Franky Line9b8d912012-05-04 18:27:31 -0700416
417 return ret;
418}
419
Arend van Spriela39be272013-12-12 11:58:58 +0100420u8 brcmf_sdiod_regrb(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret)
Franky Line9b8d912012-05-04 18:27:31 -0700421{
422 u8 data;
423 int retval;
424
Arend van Sprielc3203372013-04-03 12:40:44 +0200425 brcmf_dbg(SDIO, "addr:0x%08x\n", addr);
Arend van Spriela39be272013-12-12 11:58:58 +0100426 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, &data, false);
Arend van Sprielc3203372013-04-03 12:40:44 +0200427 brcmf_dbg(SDIO, "data:0x%02x\n", data);
Franky Line9b8d912012-05-04 18:27:31 -0700428
429 if (ret)
430 *ret = retval;
431
432 return data;
433}
434
Arend van Spriela39be272013-12-12 11:58:58 +0100435u32 brcmf_sdiod_regrl(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret)
Franky Line9b8d912012-05-04 18:27:31 -0700436{
437 u32 data;
438 int retval;
439
Arend van Sprielc3203372013-04-03 12:40:44 +0200440 brcmf_dbg(SDIO, "addr:0x%08x\n", addr);
Arend van Spriela39be272013-12-12 11:58:58 +0100441 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, &data, false);
Arend van Sprielc3203372013-04-03 12:40:44 +0200442 brcmf_dbg(SDIO, "data:0x%08x\n", data);
Franky Line9b8d912012-05-04 18:27:31 -0700443
444 if (ret)
445 *ret = retval;
446
447 return data;
448}
449
Arend van Spriela39be272013-12-12 11:58:58 +0100450void brcmf_sdiod_regwb(struct brcmf_sdio_dev *sdiodev, u32 addr,
Franky Line9b8d912012-05-04 18:27:31 -0700451 u8 data, int *ret)
452{
453 int retval;
454
Arend van Sprielc3203372013-04-03 12:40:44 +0200455 brcmf_dbg(SDIO, "addr:0x%08x, data:0x%02x\n", addr, data);
Arend van Spriela39be272013-12-12 11:58:58 +0100456 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, &data, true);
Franky Line9b8d912012-05-04 18:27:31 -0700457
458 if (ret)
459 *ret = retval;
460}
461
Arend van Spriela39be272013-12-12 11:58:58 +0100462void brcmf_sdiod_regwl(struct brcmf_sdio_dev *sdiodev, u32 addr,
Franky Line9b8d912012-05-04 18:27:31 -0700463 u32 data, int *ret)
464{
465 int retval;
466
Arend van Sprielc3203372013-04-03 12:40:44 +0200467 brcmf_dbg(SDIO, "addr:0x%08x, data:0x%08x\n", addr, data);
Arend van Spriela39be272013-12-12 11:58:58 +0100468 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, &data, true);
Franky Line9b8d912012-05-04 18:27:31 -0700469
470 if (ret)
471 *ret = retval;
472}
473
Arend van Spriela39be272013-12-12 11:58:58 +0100474static int brcmf_sdiod_buffrw(struct brcmf_sdio_dev *sdiodev, uint fn,
Arend van Spriel4aef2672013-10-15 15:44:52 +0200475 bool write, u32 addr, struct sk_buff *pkt)
476{
477 unsigned int req_sz;
478
Arend van Spriela39be272013-12-12 11:58:58 +0100479 brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_buffer_wait);
480 if (brcmf_sdiod_pm_resume_error(sdiodev))
Arend van Spriel4aef2672013-10-15 15:44:52 +0200481 return -EIO;
482
483 /* Single skb use the standard mmc interface */
484 req_sz = pkt->len + 3;
485 req_sz &= (uint)~3;
486
487 if (write)
488 return sdio_memcpy_toio(sdiodev->func[fn], addr,
489 ((u8 *)(pkt->data)),
490 req_sz);
491 else if (fn == 1)
492 return sdio_memcpy_fromio(sdiodev->func[fn],
493 ((u8 *)(pkt->data)),
494 addr, req_sz);
495 else
496 /* function 2 read is FIFO operation */
497 return sdio_readsb(sdiodev->func[fn],
498 ((u8 *)(pkt->data)), addr,
499 req_sz);
500}
501
Franky Lin78b3f1c2013-06-18 13:29:28 +0200502/**
Arend van Spriela39be272013-12-12 11:58:58 +0100503 * brcmf_sdiod_sglist_rw - SDIO interface function for block data access
Franky Lin78b3f1c2013-06-18 13:29:28 +0200504 * @sdiodev: brcmfmac sdio device
505 * @fn: SDIO function number
506 * @write: direction flag
507 * @addr: dongle memory address as source/destination
508 * @pkt: skb pointer
509 *
510 * This function takes the respbonsibility as the interface function to MMC
511 * stack for block data access. It assumes that the skb passed down by the
512 * caller has already been padded and aligned.
513 */
Arend van Spriela39be272013-12-12 11:58:58 +0100514static int brcmf_sdiod_sglist_rw(struct brcmf_sdio_dev *sdiodev, uint fn,
515 bool write, u32 addr,
516 struct sk_buff_head *pktlist)
Franky Lin78b3f1c2013-06-18 13:29:28 +0200517{
Franky Lin354b75b2013-06-18 13:29:29 +0200518 unsigned int req_sz, func_blk_sz, sg_cnt, sg_data_sz, pkt_offset;
Arend van Spriel71201492013-10-15 15:44:49 +0200519 unsigned int max_req_sz, orig_offset, dst_offset;
Arend van Spriel3f782742013-10-15 15:44:48 +0200520 unsigned short max_seg_cnt, seg_sz;
Franky Lin3b81a682013-06-26 14:20:18 +0200521 unsigned char *pkt_data, *orig_data, *dst_data;
522 struct sk_buff *pkt_next = NULL, *local_pkt_next;
523 struct sk_buff_head local_list, *target_list;
Franky Lin354b75b2013-06-18 13:29:29 +0200524 struct mmc_request mmc_req;
525 struct mmc_command mmc_cmd;
526 struct mmc_data mmc_dat;
527 struct sg_table st;
528 struct scatterlist *sgl;
Franky Lin354b75b2013-06-18 13:29:29 +0200529 int ret = 0;
530
531 if (!pktlist->qlen)
532 return -EINVAL;
Franky Lin78b3f1c2013-06-18 13:29:28 +0200533
Arend van Spriela39be272013-12-12 11:58:58 +0100534 brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_buffer_wait);
535 if (brcmf_sdiod_pm_resume_error(sdiodev))
Franky Lin78b3f1c2013-06-18 13:29:28 +0200536 return -EIO;
537
Franky Lin3b81a682013-06-26 14:20:18 +0200538 target_list = pktlist;
539 /* for host with broken sg support, prepare a page aligned list */
540 __skb_queue_head_init(&local_list);
541 if (sdiodev->pdata && sdiodev->pdata->broken_sg_support && !write) {
542 req_sz = 0;
543 skb_queue_walk(pktlist, pkt_next)
544 req_sz += pkt_next->len;
545 req_sz = ALIGN(req_sz, sdiodev->func[fn]->cur_blksize);
546 while (req_sz > PAGE_SIZE) {
547 pkt_next = brcmu_pkt_buf_get_skb(PAGE_SIZE);
548 if (pkt_next == NULL) {
549 ret = -ENOMEM;
550 goto exit;
551 }
552 __skb_queue_tail(&local_list, pkt_next);
553 req_sz -= PAGE_SIZE;
554 }
555 pkt_next = brcmu_pkt_buf_get_skb(req_sz);
556 if (pkt_next == NULL) {
557 ret = -ENOMEM;
558 goto exit;
559 }
560 __skb_queue_tail(&local_list, pkt_next);
561 target_list = &local_list;
562 }
563
Franky Lin354b75b2013-06-18 13:29:29 +0200564 func_blk_sz = sdiodev->func[fn]->cur_blksize;
Arend van Spriel71201492013-10-15 15:44:49 +0200565 max_req_sz = sdiodev->max_request_size;
566 max_seg_cnt = min_t(unsigned short, sdiodev->max_segment_count,
567 target_list->qlen);
Franky Lin3b81a682013-06-26 14:20:18 +0200568 seg_sz = target_list->qlen;
Franky Lin354b75b2013-06-18 13:29:29 +0200569 pkt_offset = 0;
Franky Lin3b81a682013-06-26 14:20:18 +0200570 pkt_next = target_list->next;
Franky Lin354b75b2013-06-18 13:29:29 +0200571
Arend van Spriel3f782742013-10-15 15:44:48 +0200572 if (sg_alloc_table(&st, max_seg_cnt, GFP_KERNEL)) {
Franky Lin3b81a682013-06-26 14:20:18 +0200573 ret = -ENOMEM;
574 goto exit;
575 }
Franky Lin354b75b2013-06-18 13:29:29 +0200576
Arend van Spriel7f9a8dc2013-10-15 15:44:50 +0200577 memset(&mmc_req, 0, sizeof(struct mmc_request));
578 memset(&mmc_cmd, 0, sizeof(struct mmc_command));
579 memset(&mmc_dat, 0, sizeof(struct mmc_data));
580
581 mmc_dat.sg = st.sgl;
582 mmc_dat.blksz = func_blk_sz;
583 mmc_dat.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
584 mmc_cmd.opcode = SD_IO_RW_EXTENDED;
585 mmc_cmd.arg = write ? 1<<31 : 0; /* write flag */
586 mmc_cmd.arg |= (fn & 0x7) << 28; /* SDIO func num */
587 mmc_cmd.arg |= 1<<27; /* block mode */
588 /* for function 1 the addr will be incremented */
589 mmc_cmd.arg |= (fn == 1) ? 1<<26 : 0;
590 mmc_cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
591 mmc_req.cmd = &mmc_cmd;
592 mmc_req.data = &mmc_dat;
593
Franky Lin354b75b2013-06-18 13:29:29 +0200594 while (seg_sz) {
595 req_sz = 0;
596 sg_cnt = 0;
Franky Lin354b75b2013-06-18 13:29:29 +0200597 sgl = st.sgl;
598 /* prep sg table */
Franky Lin3b81a682013-06-26 14:20:18 +0200599 while (pkt_next != (struct sk_buff *)target_list) {
Franky Lin354b75b2013-06-18 13:29:29 +0200600 pkt_data = pkt_next->data + pkt_offset;
601 sg_data_sz = pkt_next->len - pkt_offset;
Arend van Spriel71201492013-10-15 15:44:49 +0200602 if (sg_data_sz > sdiodev->max_segment_size)
603 sg_data_sz = sdiodev->max_segment_size;
Franky Lin354b75b2013-06-18 13:29:29 +0200604 if (sg_data_sz > max_req_sz - req_sz)
605 sg_data_sz = max_req_sz - req_sz;
606
607 sg_set_buf(sgl, pkt_data, sg_data_sz);
608
609 sg_cnt++;
610 sgl = sg_next(sgl);
611 req_sz += sg_data_sz;
612 pkt_offset += sg_data_sz;
613 if (pkt_offset == pkt_next->len) {
614 pkt_offset = 0;
615 pkt_next = pkt_next->next;
616 }
617
Arend van Spriel3f782742013-10-15 15:44:48 +0200618 if (req_sz >= max_req_sz || sg_cnt >= max_seg_cnt)
Franky Lin354b75b2013-06-18 13:29:29 +0200619 break;
620 }
621 seg_sz -= sg_cnt;
622
623 if (req_sz % func_blk_sz != 0) {
624 brcmf_err("sg request length %u is not %u aligned\n",
625 req_sz, func_blk_sz);
Franky Lin3b81a682013-06-26 14:20:18 +0200626 ret = -ENOTBLK;
627 goto exit;
Franky Lin354b75b2013-06-18 13:29:29 +0200628 }
Arend van Spriel7f9a8dc2013-10-15 15:44:50 +0200629
Franky Lin354b75b2013-06-18 13:29:29 +0200630 mmc_dat.sg_len = sg_cnt;
Franky Lin354b75b2013-06-18 13:29:29 +0200631 mmc_dat.blocks = req_sz / func_blk_sz;
Franky Lin354b75b2013-06-18 13:29:29 +0200632 mmc_cmd.arg |= (addr & 0x1FFFF) << 9; /* address */
633 mmc_cmd.arg |= mmc_dat.blocks & 0x1FF; /* block count */
Arend van Spriel7f9a8dc2013-10-15 15:44:50 +0200634 /* incrementing addr for function 1 */
Franky Lin354b75b2013-06-18 13:29:29 +0200635 if (fn == 1)
636 addr += req_sz;
637
638 mmc_set_data_timeout(&mmc_dat, sdiodev->func[fn]->card);
Arend van Spriel71201492013-10-15 15:44:49 +0200639 mmc_wait_for_req(sdiodev->func[fn]->card->host, &mmc_req);
Franky Lin354b75b2013-06-18 13:29:29 +0200640
641 ret = mmc_cmd.error ? mmc_cmd.error : mmc_dat.error;
642 if (ret != 0) {
643 brcmf_err("CMD53 sg block %s failed %d\n",
644 write ? "write" : "read", ret);
645 ret = -EIO;
646 break;
647 }
648 }
649
Franky Lin3b81a682013-06-26 14:20:18 +0200650 if (sdiodev->pdata && sdiodev->pdata->broken_sg_support && !write) {
651 local_pkt_next = local_list.next;
652 orig_offset = 0;
653 skb_queue_walk(pktlist, pkt_next) {
654 dst_offset = 0;
655 do {
656 req_sz = local_pkt_next->len - orig_offset;
657 req_sz = min_t(uint, pkt_next->len - dst_offset,
658 req_sz);
659 orig_data = local_pkt_next->data + orig_offset;
660 dst_data = pkt_next->data + dst_offset;
661 memcpy(dst_data, orig_data, req_sz);
662 orig_offset += req_sz;
663 dst_offset += req_sz;
664 if (orig_offset == local_pkt_next->len) {
665 orig_offset = 0;
666 local_pkt_next = local_pkt_next->next;
667 }
668 if (dst_offset == pkt_next->len)
669 break;
670 } while (!skb_queue_empty(&local_list));
671 }
672 }
673
674exit:
Franky Lin354b75b2013-06-18 13:29:29 +0200675 sg_free_table(&st);
Franky Lin3b81a682013-06-26 14:20:18 +0200676 while ((pkt_next = __skb_dequeue(&local_list)) != NULL)
677 brcmu_pkt_buf_free_skb(pkt_next);
Franky Lin354b75b2013-06-18 13:29:29 +0200678
679 return ret;
Franky Lin78b3f1c2013-06-18 13:29:28 +0200680}
681
Arend van Spriela7cdd822013-12-12 11:58:59 +0100682int brcmf_sdiod_recv_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes)
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800683{
684 struct sk_buff *mypkt;
685 int err;
686
687 mypkt = brcmu_pkt_buf_get_skb(nbytes);
688 if (!mypkt) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100689 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n",
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800690 nbytes);
691 return -EIO;
692 }
693
Arend van Spriela7cdd822013-12-12 11:58:59 +0100694 err = brcmf_sdiod_recv_pkt(sdiodev, mypkt);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800695 if (!err)
696 memcpy(buf, mypkt->data, nbytes);
697
698 brcmu_pkt_buf_free_skb(mypkt);
699 return err;
700}
701
Arend van Spriela7cdd822013-12-12 11:58:59 +0100702int brcmf_sdiod_recv_pkt(struct brcmf_sdio_dev *sdiodev, struct sk_buff *pkt)
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800703{
Arend van Spriela7cdd822013-12-12 11:58:59 +0100704 u32 addr = sdiodev->sbwad;
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800705 int err = 0;
706
Arend van Spriela7cdd822013-12-12 11:58:59 +0100707 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pkt->len);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800708
Arend van Spriela7cdd822013-12-12 11:58:59 +0100709 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800710 if (err)
Franky Lin7057fd02012-09-13 21:12:02 +0200711 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200712
Arend van Spriela7cdd822013-12-12 11:58:59 +0100713 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr, pkt);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800714
Franky Lin7057fd02012-09-13 21:12:02 +0200715done:
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800716 return err;
717}
718
Arend van Spriela7cdd822013-12-12 11:58:59 +0100719int brcmf_sdiod_recv_chain(struct brcmf_sdio_dev *sdiodev,
720 struct sk_buff_head *pktq, uint totlen)
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800721{
Arend van Spriela413e392013-10-15 15:44:57 +0200722 struct sk_buff *glom_skb;
723 struct sk_buff *skb;
Arend van Spriela7cdd822013-12-12 11:58:59 +0100724 u32 addr = sdiodev->sbwad;
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800725 int err = 0;
726
Arend van Spriela7cdd822013-12-12 11:58:59 +0100727 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n",
728 addr, pktq->qlen);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800729
Arend van Spriela7cdd822013-12-12 11:58:59 +0100730 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800731 if (err)
Franky Lin7057fd02012-09-13 21:12:02 +0200732 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200733
Arend van Spriela413e392013-10-15 15:44:57 +0200734 if (pktq->qlen == 1)
Arend van Spriela7cdd822013-12-12 11:58:59 +0100735 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr,
736 pktq->next);
Arend van Spriela413e392013-10-15 15:44:57 +0200737 else if (!sdiodev->sg_support) {
738 glom_skb = brcmu_pkt_buf_get_skb(totlen);
739 if (!glom_skb)
740 return -ENOMEM;
Arend van Spriela7cdd822013-12-12 11:58:59 +0100741 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr,
742 glom_skb);
Arend van Spriela413e392013-10-15 15:44:57 +0200743 if (err)
744 goto done;
745
746 skb_queue_walk(pktq, skb) {
747 memcpy(skb->data, glom_skb->data, skb->len);
748 skb_pull(glom_skb, skb->len);
749 }
750 } else
Arend van Spriela7cdd822013-12-12 11:58:59 +0100751 err = brcmf_sdiod_sglist_rw(sdiodev, SDIO_FUNC_2, false, addr,
752 pktq);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200753
Franky Lin7057fd02012-09-13 21:12:02 +0200754done:
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800755 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200756}
757
Arend van Spriela7cdd822013-12-12 11:58:59 +0100758int brcmf_sdiod_send_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes)
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800759{
760 struct sk_buff *mypkt;
Arend van Spriela7cdd822013-12-12 11:58:59 +0100761 u32 addr = sdiodev->sbwad;
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800762 int err;
763
764 mypkt = brcmu_pkt_buf_get_skb(nbytes);
765 if (!mypkt) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100766 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n",
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800767 nbytes);
768 return -EIO;
769 }
770
771 memcpy(mypkt->data, buf, nbytes);
Arend van Spriel4aef2672013-10-15 15:44:52 +0200772
Arend van Spriela7cdd822013-12-12 11:58:59 +0100773 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
Arend van Spriel4aef2672013-10-15 15:44:52 +0200774
Arend van Sprielfcaac2b2013-10-15 15:44:53 +0200775 if (!err)
Arend van Spriela7cdd822013-12-12 11:58:59 +0100776 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, true, addr,
777 mypkt);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800778
779 brcmu_pkt_buf_free_skb(mypkt);
780 return err;
781
782}
783
Arend van Spriela7cdd822013-12-12 11:58:59 +0100784int brcmf_sdiod_send_pkt(struct brcmf_sdio_dev *sdiodev,
785 struct sk_buff_head *pktq)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200786{
Arend van Sprielc8cce1f2013-10-15 15:44:55 +0200787 struct sk_buff *skb;
Arend van Spriela7cdd822013-12-12 11:58:59 +0100788 u32 addr = sdiodev->sbwad;
Arend van Sprielfcaac2b2013-10-15 15:44:53 +0200789 int err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200790
Arend van Spriela7cdd822013-12-12 11:58:59 +0100791 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pktq->qlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200792
Arend van Spriela7cdd822013-12-12 11:58:59 +0100793 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
Arend van Sprielfcaac2b2013-10-15 15:44:53 +0200794 if (err)
795 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200796
Arend van Sprielc8cce1f2013-10-15 15:44:55 +0200797 if (pktq->qlen == 1 || !sdiodev->sg_support)
798 skb_queue_walk(pktq, skb) {
Arend van Spriela7cdd822013-12-12 11:58:59 +0100799 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, true,
800 addr, skb);
Arend van Sprielc8cce1f2013-10-15 15:44:55 +0200801 if (err)
802 break;
803 }
804 else
Arend van Spriela7cdd822013-12-12 11:58:59 +0100805 err = brcmf_sdiod_sglist_rw(sdiodev, SDIO_FUNC_2, true, addr,
806 pktq);
Arend van Sprielc8cce1f2013-10-15 15:44:55 +0200807
808 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200809}
810
Franky Linba540b02013-04-11 13:28:47 +0200811int
Arend van Spriela39be272013-12-12 11:58:58 +0100812brcmf_sdiod_ramrw(struct brcmf_sdio_dev *sdiodev, bool write, u32 address,
813 u8 *data, uint size)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200814{
Franky Linba540b02013-04-11 13:28:47 +0200815 int bcmerror = 0;
816 struct sk_buff *pkt;
817 u32 sdaddr;
818 uint dsize;
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800819
Franky Linba540b02013-04-11 13:28:47 +0200820 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
821 pkt = dev_alloc_skb(dsize);
822 if (!pkt) {
823 brcmf_err("dev_alloc_skb failed: len %d\n", dsize);
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800824 return -EIO;
825 }
Franky Linba540b02013-04-11 13:28:47 +0200826 pkt->priority = 0;
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800827
Franky Linba540b02013-04-11 13:28:47 +0200828 /* Determine initial transfer parameters */
829 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
830 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
831 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
832 else
833 dsize = size;
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800834
Franky Linba540b02013-04-11 13:28:47 +0200835 sdio_claim_host(sdiodev->func[1]);
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800836
Franky Linba540b02013-04-11 13:28:47 +0200837 /* Do the transfer(s) */
838 while (size) {
839 /* Set the backplane window to include the start address */
Arend van Spriela39be272013-12-12 11:58:58 +0100840 bcmerror = brcmf_sdiod_set_sbaddr_window(sdiodev, address);
Franky Linba540b02013-04-11 13:28:47 +0200841 if (bcmerror)
842 break;
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800843
Franky Linba540b02013-04-11 13:28:47 +0200844 brcmf_dbg(SDIO, "%s %d bytes at offset 0x%08x in window 0x%08x\n",
845 write ? "write" : "read", dsize,
846 sdaddr, address & SBSDIO_SBWINDOW_MASK);
847
848 sdaddr &= SBSDIO_SB_OFT_ADDR_MASK;
849 sdaddr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
850
851 skb_put(pkt, dsize);
852 if (write)
853 memcpy(pkt->data, data, dsize);
Arend van Spriela39be272013-12-12 11:58:58 +0100854 bcmerror = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_1, write,
855 sdaddr, pkt);
Franky Linba540b02013-04-11 13:28:47 +0200856 if (bcmerror) {
857 brcmf_err("membytes transfer failed\n");
858 break;
859 }
860 if (!write)
861 memcpy(data, pkt->data, dsize);
862 skb_trim(pkt, dsize);
863
864 /* Adjust for next transfer (if any) */
865 size -= dsize;
866 if (size) {
867 data += dsize;
868 address += dsize;
869 sdaddr = 0;
870 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
871 }
872 }
873
874 dev_kfree_skb(pkt);
875
876 /* Return the window to backplane enumeration space for core access */
Arend van Spriela39be272013-12-12 11:58:58 +0100877 if (brcmf_sdiod_set_sbaddr_window(sdiodev, sdiodev->sbwad))
Franky Linba540b02013-04-11 13:28:47 +0200878 brcmf_err("FAILED to set window back to 0x%x\n",
879 sdiodev->sbwad);
880
881 sdio_release_host(sdiodev->func[1]);
882
883 return bcmerror;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200884}
885
Arend van Spriela39be272013-12-12 11:58:58 +0100886int brcmf_sdiod_abort(struct brcmf_sdio_dev *sdiodev, uint fn)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200887{
888 char t_func = (char)fn;
Arend van Sprielc3203372013-04-03 12:40:44 +0200889 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200890
891 /* issue abort cmd52 command through F0 */
Arend van Spriela39be272013-12-12 11:58:58 +0100892 brcmf_sdiod_request_byte(sdiodev, SDIOH_WRITE, SDIO_FUNC_0,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200893 SDIO_CCCR_ABORT, &t_func);
894
Arend van Sprielc3203372013-04-03 12:40:44 +0200895 brcmf_dbg(SDIO, "Exit\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200896 return 0;
897}
898
Arend van Spriela39be272013-12-12 11:58:58 +0100899static int brcmf_sdiod_remove(struct brcmf_sdio_dev *sdiodev)
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100900{
Arend van Spriela39be272013-12-12 11:58:58 +0100901 sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100902
Arend van Spriela39be272013-12-12 11:58:58 +0100903 if (sdiodev->bus) {
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100904 brcmf_sdio_disconnect(sdiodev->bus);
Arend van Spriela39be272013-12-12 11:58:58 +0100905 sdiodev->bus = NULL;
906 }
907
908 /* Disable Function 2 */
909 sdio_claim_host(sdiodev->func[2]);
910 sdio_disable_func(sdiodev->func[2]);
911 sdio_release_host(sdiodev->func[2]);
912
913 /* Disable Function 1 */
914 sdio_claim_host(sdiodev->func[1]);
915 sdio_disable_func(sdiodev->func[1]);
916 sdio_release_host(sdiodev->func[1]);
917
918 sdiodev->sbwad = 0;
919
920 return 0;
921}
922
923static int brcmf_sdiod_probe(struct brcmf_sdio_dev *sdiodev)
924{
925 struct sdio_func *func;
926 struct mmc_host *host;
927 uint max_blocks;
928 int ret = 0;
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100929
930 sdiodev->num_funcs = 2;
931
932 sdio_claim_host(sdiodev->func[1]);
933
Arend van Spriela39be272013-12-12 11:58:58 +0100934 ret = sdio_set_block_size(sdiodev->func[1], SDIO_FUNC1_BLOCKSIZE);
935 if (ret) {
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100936 brcmf_err("Failed to set F1 blocksize\n");
Arend van Spriela39be272013-12-12 11:58:58 +0100937 sdio_release_host(sdiodev->func[1]);
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100938 goto out;
939 }
Arend van Spriela39be272013-12-12 11:58:58 +0100940 ret = sdio_set_block_size(sdiodev->func[2], SDIO_FUNC2_BLOCKSIZE);
941 if (ret) {
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100942 brcmf_err("Failed to set F2 blocksize\n");
Arend van Spriela39be272013-12-12 11:58:58 +0100943 sdio_release_host(sdiodev->func[1]);
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100944 goto out;
945 }
946
Arend van Spriel71370eb2013-12-12 11:58:53 +0100947 /* increase F2 timeout */
948 sdiodev->func[2]->enable_timeout = SDIO_WAIT_F2RDY;
949
Arend van Spriel463c30b2013-12-12 11:58:52 +0100950 /* Enable Function 1 */
Arend van Spriela39be272013-12-12 11:58:58 +0100951 ret = sdio_enable_func(sdiodev->func[1]);
952 sdio_release_host(sdiodev->func[1]);
953 if (ret) {
954 brcmf_err("Failed to enable F1: err=%d\n", ret);
Arend van Spriel463c30b2013-12-12 11:58:52 +0100955 goto out;
956 }
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100957
958 /*
Arend van Spriela39be272013-12-12 11:58:58 +0100959 * determine host related variables after brcmf_sdiod_probe()
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100960 * as func->cur_blksize is properly set and F2 init has been
961 * completed successfully.
962 */
963 func = sdiodev->func[2];
964 host = func->card->host;
965 sdiodev->sg_support = host->max_segs > 1;
966 max_blocks = min_t(uint, host->max_blk_count, 511u);
967 sdiodev->max_request_size = min_t(uint, host->max_req_size,
968 max_blocks * func->cur_blksize);
969 sdiodev->max_segment_count = min_t(uint, host->max_segs,
970 SG_MAX_SINGLE_ALLOC);
971 sdiodev->max_segment_size = host->max_seg_size;
Arend van Spriele49b06b2013-12-12 11:58:51 +0100972
Arend van Spriele49b06b2013-12-12 11:58:51 +0100973 /* try to attach to the target device */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100974 sdiodev->bus = brcmf_sdio_probe(sdiodev);
Arend van Spriele49b06b2013-12-12 11:58:51 +0100975 if (!sdiodev->bus) {
Arend van Spriele49b06b2013-12-12 11:58:51 +0100976 ret = -ENODEV;
977 goto out;
978 }
979
980out:
981 if (ret)
Arend van Spriela39be272013-12-12 11:58:58 +0100982 brcmf_sdiod_remove(sdiodev);
Arend van Spriele49b06b2013-12-12 11:58:51 +0100983
984 return ret;
985}
986
987/* devices we support, null terminated */
988static const struct sdio_device_id brcmf_sdmmc_ids[] = {
989 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43143)},
990 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43241)},
991 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4329)},
992 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4330)},
993 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4334)},
994 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM,
995 SDIO_DEVICE_ID_BROADCOM_4335_4339)},
996 { /* end: all zeroes */ },
997};
998MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids);
999
1000static struct brcmfmac_sdio_platform_data *brcmfmac_sdio_pdata;
1001
1002
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001003static int brcmf_ops_sdio_probe(struct sdio_func *func,
1004 const struct sdio_device_id *id)
1005{
1006 int err;
1007 struct brcmf_sdio_dev *sdiodev;
1008 struct brcmf_bus *bus_if;
1009
1010 brcmf_dbg(SDIO, "Enter\n");
1011 brcmf_dbg(SDIO, "Class=%x\n", func->class);
1012 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor);
1013 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device);
1014 brcmf_dbg(SDIO, "Function#: %d\n", func->num);
1015
1016 /* Consume func num 1 but dont do anything with it. */
1017 if (func->num == 1)
1018 return 0;
1019
1020 /* Ignore anything but func 2 */
1021 if (func->num != 2)
1022 return -ENODEV;
1023
1024 bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL);
1025 if (!bus_if)
1026 return -ENOMEM;
1027 sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL);
1028 if (!sdiodev) {
1029 kfree(bus_if);
1030 return -ENOMEM;
1031 }
1032
1033 sdiodev->func[0] = func->card->sdio_func[0];
1034 sdiodev->func[1] = func->card->sdio_func[0];
1035 sdiodev->func[2] = func;
1036
1037 sdiodev->bus_if = bus_if;
1038 bus_if->bus_priv.sdio = sdiodev;
Hante Meuleman943258b2013-12-12 11:59:02 +01001039 bus_if->proto_type = BRCMF_PROTO_BCDC;
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001040 dev_set_drvdata(&func->dev, bus_if);
1041 dev_set_drvdata(&sdiodev->func[1]->dev, bus_if);
1042 sdiodev->dev = &sdiodev->func[1]->dev;
1043 sdiodev->pdata = brcmfmac_sdio_pdata;
1044
1045 atomic_set(&sdiodev->suspend, false);
1046 init_waitqueue_head(&sdiodev->request_byte_wait);
1047 init_waitqueue_head(&sdiodev->request_word_wait);
1048 init_waitqueue_head(&sdiodev->request_buffer_wait);
1049
Arend van Spriela39be272013-12-12 11:58:58 +01001050 brcmf_dbg(SDIO, "F2 found, calling brcmf_sdiod_probe...\n");
1051 err = brcmf_sdiod_probe(sdiodev);
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001052 if (err) {
1053 brcmf_err("F2 error, probe failed %d...\n", err);
1054 goto fail;
1055 }
1056
1057 brcmf_dbg(SDIO, "F2 init completed...\n");
1058 return 0;
1059
1060fail:
1061 dev_set_drvdata(&func->dev, NULL);
1062 dev_set_drvdata(&sdiodev->func[1]->dev, NULL);
1063 kfree(sdiodev);
1064 kfree(bus_if);
1065 return err;
1066}
1067
1068static void brcmf_ops_sdio_remove(struct sdio_func *func)
1069{
1070 struct brcmf_bus *bus_if;
1071 struct brcmf_sdio_dev *sdiodev;
1072
1073 brcmf_dbg(SDIO, "Enter\n");
1074 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor);
1075 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device);
1076 brcmf_dbg(SDIO, "Function: %d\n", func->num);
1077
1078 if (func->num != 1 && func->num != 2)
1079 return;
1080
1081 bus_if = dev_get_drvdata(&func->dev);
1082 if (bus_if) {
1083 sdiodev = bus_if->bus_priv.sdio;
Arend van Spriela39be272013-12-12 11:58:58 +01001084 brcmf_sdiod_remove(sdiodev);
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001085
1086 dev_set_drvdata(&sdiodev->func[1]->dev, NULL);
1087 dev_set_drvdata(&sdiodev->func[2]->dev, NULL);
1088
1089 kfree(bus_if);
1090 kfree(sdiodev);
1091 }
1092
1093 brcmf_dbg(SDIO, "Exit\n");
1094}
1095
1096#ifdef CONFIG_PM_SLEEP
Arend van Spriela39be272013-12-12 11:58:58 +01001097static int brcmf_ops_sdio_suspend(struct device *dev)
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001098{
1099 mmc_pm_flag_t sdio_flags;
1100 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1101 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
1102 int ret = 0;
1103
1104 brcmf_dbg(SDIO, "\n");
1105
1106 atomic_set(&sdiodev->suspend, true);
1107
1108 sdio_flags = sdio_get_host_pm_caps(sdiodev->func[1]);
1109 if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
1110 brcmf_err("Host can't keep power while suspended\n");
1111 return -EINVAL;
1112 }
1113
1114 ret = sdio_set_host_pm_flags(sdiodev->func[1], MMC_PM_KEEP_POWER);
1115 if (ret) {
1116 brcmf_err("Failed to set pm_flags\n");
1117 return ret;
1118 }
1119
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001120 brcmf_sdio_wd_timer(sdiodev->bus, 0);
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001121
1122 return ret;
1123}
1124
Arend van Spriela39be272013-12-12 11:58:58 +01001125static int brcmf_ops_sdio_resume(struct device *dev)
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001126{
1127 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1128 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
1129
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001130 brcmf_sdio_wd_timer(sdiodev->bus, BRCMF_WD_POLL_MS);
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001131 atomic_set(&sdiodev->suspend, false);
1132 return 0;
1133}
1134
1135static const struct dev_pm_ops brcmf_sdio_pm_ops = {
Arend van Spriela39be272013-12-12 11:58:58 +01001136 .suspend = brcmf_ops_sdio_suspend,
1137 .resume = brcmf_ops_sdio_resume,
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001138};
1139#endif /* CONFIG_PM_SLEEP */
1140
1141static struct sdio_driver brcmf_sdmmc_driver = {
1142 .probe = brcmf_ops_sdio_probe,
1143 .remove = brcmf_ops_sdio_remove,
1144 .name = BRCMFMAC_SDIO_PDATA_NAME,
1145 .id_table = brcmf_sdmmc_ids,
1146#ifdef CONFIG_PM_SLEEP
1147 .drv = {
1148 .pm = &brcmf_sdio_pm_ops,
1149 },
1150#endif /* CONFIG_PM_SLEEP */
1151};
1152
1153static int brcmf_sdio_pd_probe(struct platform_device *pdev)
1154{
1155 brcmf_dbg(SDIO, "Enter\n");
1156
1157 brcmfmac_sdio_pdata = dev_get_platdata(&pdev->dev);
1158
1159 if (brcmfmac_sdio_pdata->power_on)
1160 brcmfmac_sdio_pdata->power_on();
1161
1162 return 0;
1163}
1164
1165static int brcmf_sdio_pd_remove(struct platform_device *pdev)
1166{
1167 brcmf_dbg(SDIO, "Enter\n");
1168
1169 if (brcmfmac_sdio_pdata->power_off)
1170 brcmfmac_sdio_pdata->power_off();
1171
1172 sdio_unregister_driver(&brcmf_sdmmc_driver);
1173
1174 return 0;
1175}
1176
1177static struct platform_driver brcmf_sdio_pd = {
1178 .remove = brcmf_sdio_pd_remove,
1179 .driver = {
1180 .name = BRCMFMAC_SDIO_PDATA_NAME,
1181 .owner = THIS_MODULE,
1182 }
1183};
1184
1185void brcmf_sdio_register(void)
1186{
1187 int ret;
1188
1189 ret = sdio_register_driver(&brcmf_sdmmc_driver);
1190 if (ret)
1191 brcmf_err("sdio_register_driver failed: %d\n", ret);
1192}
1193
1194void brcmf_sdio_exit(void)
1195{
1196 brcmf_dbg(SDIO, "Enter\n");
1197
1198 if (brcmfmac_sdio_pdata)
1199 platform_driver_unregister(&brcmf_sdio_pd);
1200 else
1201 sdio_unregister_driver(&brcmf_sdmmc_driver);
1202}
1203
1204void __init brcmf_sdio_init(void)
1205{
1206 int ret;
1207
1208 brcmf_dbg(SDIO, "Enter\n");
1209
1210 ret = platform_driver_probe(&brcmf_sdio_pd, brcmf_sdio_pd_probe);
1211 if (ret == -ENODEV)
1212 brcmf_dbg(SDIO, "No platform data available.\n");
1213}