blob: 07e7d2520257846f0278891ffe74a368a0b0b851 [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"
46
47#define SDIOH_API_ACCESS_RETRY_LIMIT 2
48
Arend van Spriele49b06b2013-12-12 11:58:51 +010049#define DMA_ALIGN_MASK 0x03
50
51#define SDIO_FUNC1_BLOCKSIZE 64
52#define SDIO_FUNC2_BLOCKSIZE 512
Arend van Spriel71370eb2013-12-12 11:58:53 +010053/* Maximum milliseconds to wait for F2 to come up */
54#define SDIO_WAIT_F2RDY 3000
55
Hante Meuleman668761a2013-04-12 10:55:55 +020056
Arend van Spriela39be272013-12-12 11:58:58 +010057static irqreturn_t brcmf_sdiod_oob_irqhandler(int irq, void *dev_id)
Franky Linba89bf12012-04-27 18:56:59 -070058{
Hante Meuleman5b3c1832012-11-14 18:46:20 -080059 struct brcmf_bus *bus_if = dev_get_drvdata(dev_id);
60 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linba89bf12012-04-27 18:56:59 -070061
Hante Meuleman668761a2013-04-12 10:55:55 +020062 brcmf_dbg(INTR, "OOB intr triggered\n");
Franky Linba89bf12012-04-27 18:56:59 -070063
Hante Meuleman668761a2013-04-12 10:55:55 +020064 /* out-of-band interrupt is level-triggered which won't
Franky Linba89bf12012-04-27 18:56:59 -070065 * be cleared until dpc
66 */
67 if (sdiodev->irq_en) {
68 disable_irq_nosync(irq);
69 sdiodev->irq_en = false;
70 }
71
Arend van Spriel82d7f3c2013-12-12 11:59:04 +010072 brcmf_sdio_isr(sdiodev->bus);
Franky Linba89bf12012-04-27 18:56:59 -070073
74 return IRQ_HANDLED;
75}
76
Arend van Spriela39be272013-12-12 11:58:58 +010077static void brcmf_sdiod_ib_irqhandler(struct sdio_func *func)
Arend van Spriel5b435de2011-10-05 13:19:03 +020078{
Hante Meuleman5b3c1832012-11-14 18:46:20 -080079 struct brcmf_bus *bus_if = dev_get_drvdata(&func->dev);
80 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Arend van Spriel5b435de2011-10-05 13:19:03 +020081
Hante Meuleman668761a2013-04-12 10:55:55 +020082 brcmf_dbg(INTR, "IB intr triggered\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +020083
Arend van Spriel82d7f3c2013-12-12 11:59:04 +010084 brcmf_sdio_isr(sdiodev->bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +020085}
86
Franky Linfbf59102011-12-16 18:36:53 -080087/* dummy handler for SDIO function 2 interrupt */
Arend van Spriela39be272013-12-12 11:58:58 +010088static void brcmf_sdiod_dummy_irqhandler(struct sdio_func *func)
Franky Linfbf59102011-12-16 18:36:53 -080089{
90}
91
Arend van Spriela39be272013-12-12 11:58:58 +010092static bool brcmf_sdiod_pm_resume_error(struct brcmf_sdio_dev *sdiodev)
Arend van Spriele49b06b2013-12-12 11:58:51 +010093{
94 bool is_err = false;
95#ifdef CONFIG_PM_SLEEP
96 is_err = atomic_read(&sdiodev->suspend);
97#endif
98 return is_err;
99}
100
Arend van Spriela39be272013-12-12 11:58:58 +0100101static void brcmf_sdiod_pm_resume_wait(struct brcmf_sdio_dev *sdiodev,
102 wait_queue_head_t *wq)
Arend van Spriele49b06b2013-12-12 11:58:51 +0100103{
104#ifdef CONFIG_PM_SLEEP
105 int retry = 0;
106 while (atomic_read(&sdiodev->suspend) && retry++ != 30)
107 wait_event_timeout(*wq, false, HZ/100);
108#endif
109}
110
Arend van Spriela39be272013-12-12 11:58:58 +0100111int brcmf_sdiod_intr_register(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200112{
Hante Meuleman668761a2013-04-12 10:55:55 +0200113 int ret = 0;
114 u8 data;
115 unsigned long flags;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200116
Hante Meuleman668761a2013-04-12 10:55:55 +0200117 if ((sdiodev->pdata) && (sdiodev->pdata->oob_irq_supported)) {
118 brcmf_dbg(SDIO, "Enter, register OOB IRQ %d\n",
119 sdiodev->pdata->oob_irq_nr);
120 ret = request_irq(sdiodev->pdata->oob_irq_nr,
Arend van Spriela39be272013-12-12 11:58:58 +0100121 brcmf_sdiod_oob_irqhandler,
Hante Meuleman668761a2013-04-12 10:55:55 +0200122 sdiodev->pdata->oob_irq_flags,
123 "brcmf_oob_intr",
124 &sdiodev->func[1]->dev);
125 if (ret != 0) {
126 brcmf_err("request_irq failed %d\n", ret);
127 return ret;
128 }
129 sdiodev->oob_irq_requested = true;
130 spin_lock_init(&sdiodev->irq_en_lock);
131 spin_lock_irqsave(&sdiodev->irq_en_lock, flags);
132 sdiodev->irq_en = true;
133 spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags);
134
135 ret = enable_irq_wake(sdiodev->pdata->oob_irq_nr);
136 if (ret != 0) {
137 brcmf_err("enable_irq_wake failed %d\n", ret);
138 return ret;
139 }
140 sdiodev->irq_wake = true;
141
142 sdio_claim_host(sdiodev->func[1]);
143
144 /* must configure SDIO_CCCR_IENx to enable irq */
Arend van Spriela39be272013-12-12 11:58:58 +0100145 data = brcmf_sdiod_regrb(sdiodev, SDIO_CCCR_IENx, &ret);
Hante Meuleman668761a2013-04-12 10:55:55 +0200146 data |= 1 << SDIO_FUNC_1 | 1 << SDIO_FUNC_2 | 1;
Arend van Spriela39be272013-12-12 11:58:58 +0100147 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_IENx, data, &ret);
Hante Meuleman668761a2013-04-12 10:55:55 +0200148
149 /* redirect, configure and enable io for interrupt signal */
150 data = SDIO_SEPINT_MASK | SDIO_SEPINT_OE;
151 if (sdiodev->pdata->oob_irq_flags & IRQF_TRIGGER_HIGH)
152 data |= SDIO_SEPINT_ACT_HI;
Arend van Spriela39be272013-12-12 11:58:58 +0100153 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, data, &ret);
Hante Meuleman668761a2013-04-12 10:55:55 +0200154
155 sdio_release_host(sdiodev->func[1]);
156 } else {
157 brcmf_dbg(SDIO, "Entering\n");
158 sdio_claim_host(sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +0100159 sdio_claim_irq(sdiodev->func[1], brcmf_sdiod_ib_irqhandler);
160 sdio_claim_irq(sdiodev->func[2], brcmf_sdiod_dummy_irqhandler);
Hante Meuleman668761a2013-04-12 10:55:55 +0200161 sdio_release_host(sdiodev->func[1]);
162 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200163
164 return 0;
165}
166
Arend van Spriela39be272013-12-12 11:58:58 +0100167int brcmf_sdiod_intr_unregister(struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200168{
Arend van Sprielc3203372013-04-03 12:40:44 +0200169 brcmf_dbg(SDIO, "Entering\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200170
Hante Meuleman668761a2013-04-12 10:55:55 +0200171 if ((sdiodev->pdata) && (sdiodev->pdata->oob_irq_supported)) {
172 sdio_claim_host(sdiodev->func[1]);
Arend van Spriela39be272013-12-12 11:58:58 +0100173 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, 0, NULL);
174 brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_IENx, 0, NULL);
Hante Meuleman668761a2013-04-12 10:55:55 +0200175 sdio_release_host(sdiodev->func[1]);
176
177 if (sdiodev->oob_irq_requested) {
178 sdiodev->oob_irq_requested = false;
179 if (sdiodev->irq_wake) {
180 disable_irq_wake(sdiodev->pdata->oob_irq_nr);
181 sdiodev->irq_wake = false;
182 }
183 free_irq(sdiodev->pdata->oob_irq_nr,
184 &sdiodev->func[1]->dev);
185 sdiodev->irq_en = false;
186 }
187 } else {
188 sdio_claim_host(sdiodev->func[1]);
189 sdio_release_irq(sdiodev->func[2]);
190 sdio_release_irq(sdiodev->func[1]);
191 sdio_release_host(sdiodev->func[1]);
192 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200193
194 return 0;
195}
196
Arend van Spriel36c4e7e2013-12-12 11:59:06 +0100197static inline int brcmf_sdiod_f0_writeb(struct sdio_func *func,
198 uint regaddr, u8 byte)
Arend van Spriele49b06b2013-12-12 11:58:51 +0100199{
Arend van Spriele49b06b2013-12-12 11:58:51 +0100200 int err_ret;
201
202 /*
203 * Can only directly write to some F0 registers.
Arend van Spriel36c4e7e2013-12-12 11:59:06 +0100204 * Handle CCCR_IENx and CCCR_ABORT command
Arend van Spriele49b06b2013-12-12 11:58:51 +0100205 * as a special case.
206 */
Arend van Spriel71370eb2013-12-12 11:58:53 +0100207 if ((regaddr == SDIO_CCCR_ABORT) ||
Arend van Spriel36c4e7e2013-12-12 11:59:06 +0100208 (regaddr == SDIO_CCCR_IENx))
209 sdio_writeb(func, byte, regaddr, &err_ret);
210 else
211 sdio_f0_writeb(func, byte, regaddr, &err_ret);
Arend van Spriele49b06b2013-12-12 11:58:51 +0100212
213 return err_ret;
214}
215
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100216static int brcmf_sdiod_request_data(struct brcmf_sdio_dev *sdiodev, u8 fn,
217 u32 addr, u8 regsz, void *data, bool write)
Arend van Spriele49b06b2013-12-12 11:58:51 +0100218{
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100219 struct sdio_func *func;
220 int ret;
Arend van Spriele49b06b2013-12-12 11:58:51 +0100221
222 brcmf_dbg(SDIO, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n",
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100223 write, fn, addr, regsz);
Arend van Spriele49b06b2013-12-12 11:58:51 +0100224
Arend van Spriela39be272013-12-12 11:58:58 +0100225 brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_word_wait);
226 if (brcmf_sdiod_pm_resume_error(sdiodev))
Arend van Spriele49b06b2013-12-12 11:58:51 +0100227 return -EIO;
228
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100229 /* only allow byte access on F0 */
230 if (WARN_ON(regsz > 1 && !fn))
231 return -EINVAL;
232 func = sdiodev->func[fn];
233
234 switch (regsz) {
235 case sizeof(u8):
236 if (write) {
237 if (fn)
238 sdio_writeb(func, *(u8 *)data, addr, &ret);
239 else
240 ret = brcmf_sdiod_f0_writeb(func, addr,
241 *(u8 *)data);
242 } else {
243 if (fn)
244 *(u8 *)data = sdio_readb(func, addr, &ret);
245 else
246 *(u8 *)data = sdio_f0_readb(func, addr, &ret);
247 }
248 break;
249 case sizeof(u16):
250 if (write)
251 sdio_writew(func, *(u16 *)data, addr, &ret);
Arend van Spriele49b06b2013-12-12 11:58:51 +0100252 else
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100253 *(u16 *)data = sdio_readw(func, addr, &ret);
254 break;
255 case sizeof(u32):
256 if (write)
257 sdio_writel(func, *(u32 *)data, addr, &ret);
Arend van Spriele49b06b2013-12-12 11:58:51 +0100258 else
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100259 *(u32 *)data = sdio_readl(func, addr, &ret);
260 break;
261 default:
262 brcmf_err("invalid size: %d\n", regsz);
263 break;
Arend van Spriele49b06b2013-12-12 11:58:51 +0100264 }
265
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100266 if (ret) {
267 /*
268 * SleepCSR register access can fail when
269 * waking up the device so reduce this noise
270 * in the logs.
271 */
272 if (addr != SBSDIO_FUNC1_SLEEPCSR)
273 brcmf_err("failed to %s data F%d@0x%05x, err: %d\n",
274 write ? "write" : "read", fn, addr, ret);
275 else
276 brcmf_dbg(SDIO, "failed to %s data F%d@0x%05x, err: %d\n",
277 write ? "write" : "read", fn, addr, ret);
278 }
279 return ret;
280}
Arend van Spriele49b06b2013-12-12 11:58:51 +0100281
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100282static int brcmf_sdiod_regrw_helper(struct brcmf_sdio_dev *sdiodev, u32 addr,
283 u8 regsz, void *data, bool write)
284{
285 u8 func_num;
286 s32 retry = 0;
287 int ret;
288
Arend van Sprielbb350712014-01-13 22:20:29 +0100289 if (sdiodev->bus_if->state == BRCMF_BUS_NOMEDIUM)
290 return -ENOMEDIUM;
291
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100292 /*
293 * figure out how to read the register based on address range
294 * 0x00 ~ 0x7FF: function 0 CCCR and FBR
295 * 0x10000 ~ 0x1FFFF: function 1 miscellaneous registers
296 * The rest: function 1 silicon backplane core registers
297 */
298 if ((addr & ~REG_F0_REG_MASK) == 0)
299 func_num = SDIO_FUNC_0;
300 else
301 func_num = SDIO_FUNC_1;
302
303 do {
304 if (!write)
305 memset(data, 0, regsz);
306 /* for retry wait for 1 ms till bus get settled down */
307 if (retry)
308 usleep_range(1000, 2000);
309 ret = brcmf_sdiod_request_data(sdiodev, func_num, addr, regsz,
310 data, write);
Arend van Sprielbb350712014-01-13 22:20:29 +0100311 } while (ret != 0 && ret != -ENOMEDIUM &&
312 retry++ < SDIOH_API_ACCESS_RETRY_LIMIT);
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100313
Arend van Sprielbb350712014-01-13 22:20:29 +0100314 if (ret == -ENOMEDIUM)
315 brcmf_bus_change_state(sdiodev->bus_if, BRCMF_BUS_NOMEDIUM);
316 else if (ret != 0)
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100317 brcmf_err("failed with %d\n", ret);
318
319 return ret;
Arend van Spriele49b06b2013-12-12 11:58:51 +0100320}
321
Franky Lin356bae62013-06-26 14:20:17 +0200322static int
Arend van Spriela39be272013-12-12 11:58:58 +0100323brcmf_sdiod_set_sbaddr_window(struct brcmf_sdio_dev *sdiodev, u32 address)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200324{
Franky Lin7d9cfc22012-05-04 18:27:30 -0700325 int err = 0, i;
326 u8 addr[3];
Franky Lin7d9cfc22012-05-04 18:27:30 -0700327
Arend van Sprielbb350712014-01-13 22:20:29 +0100328 if (sdiodev->bus_if->state == BRCMF_BUS_NOMEDIUM)
329 return -ENOMEDIUM;
330
Franky Lin7d9cfc22012-05-04 18:27:30 -0700331 addr[0] = (address >> 8) & SBSDIO_SBADDRLOW_MASK;
332 addr[1] = (address >> 16) & SBSDIO_SBADDRMID_MASK;
333 addr[2] = (address >> 24) & SBSDIO_SBADDRHIGH_MASK;
334
335 for (i = 0; i < 3; i++) {
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100336 err = brcmf_sdiod_regrw_helper(sdiodev,
337 SBSDIO_FUNC1_SBADDRLOW + i,
338 sizeof(u8), &addr[i], true);
Franky Lin7d9cfc22012-05-04 18:27:30 -0700339 if (err) {
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100340 brcmf_err("failed at addr: 0x%0x\n",
Franky Lin7d9cfc22012-05-04 18:27:30 -0700341 SBSDIO_FUNC1_SBADDRLOW + i);
342 break;
343 }
344 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200345
346 return err;
347}
348
Franky Lin356bae62013-06-26 14:20:17 +0200349static int
Arend van Spriela39be272013-12-12 11:58:58 +0100350brcmf_sdiod_addrprep(struct brcmf_sdio_dev *sdiodev, uint width, u32 *addr)
Franky Lin356bae62013-06-26 14:20:17 +0200351{
352 uint bar0 = *addr & ~SBSDIO_SB_OFT_ADDR_MASK;
353 int err = 0;
354
355 if (bar0 != sdiodev->sbwad) {
Arend van Spriela39be272013-12-12 11:58:58 +0100356 err = brcmf_sdiod_set_sbaddr_window(sdiodev, bar0);
Franky Lin356bae62013-06-26 14:20:17 +0200357 if (err)
358 return err;
359
360 sdiodev->sbwad = bar0;
361 }
362
363 *addr &= SBSDIO_SB_OFT_ADDR_MASK;
364
365 if (width == 4)
366 *addr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
367
368 return 0;
369}
370
Arend van Spriela39be272013-12-12 11:58:58 +0100371u8 brcmf_sdiod_regrb(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret)
Franky Line9b8d912012-05-04 18:27:31 -0700372{
373 u8 data;
374 int retval;
375
Arend van Sprielc3203372013-04-03 12:40:44 +0200376 brcmf_dbg(SDIO, "addr:0x%08x\n", addr);
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100377 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data,
378 false);
Arend van Sprielc3203372013-04-03 12:40:44 +0200379 brcmf_dbg(SDIO, "data:0x%02x\n", data);
Franky Line9b8d912012-05-04 18:27:31 -0700380
381 if (ret)
382 *ret = retval;
383
384 return data;
385}
386
Arend van Spriela39be272013-12-12 11:58:58 +0100387u32 brcmf_sdiod_regrl(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret)
Franky Line9b8d912012-05-04 18:27:31 -0700388{
389 u32 data;
390 int retval;
391
Arend van Sprielc3203372013-04-03 12:40:44 +0200392 brcmf_dbg(SDIO, "addr:0x%08x\n", addr);
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100393 retval = brcmf_sdiod_addrprep(sdiodev, sizeof(data), &addr);
394 if (retval)
395 goto done;
396 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data,
397 false);
Arend van Sprielc3203372013-04-03 12:40:44 +0200398 brcmf_dbg(SDIO, "data:0x%08x\n", data);
Franky Line9b8d912012-05-04 18:27:31 -0700399
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100400done:
Franky Line9b8d912012-05-04 18:27:31 -0700401 if (ret)
402 *ret = retval;
403
404 return data;
405}
406
Arend van Spriela39be272013-12-12 11:58:58 +0100407void brcmf_sdiod_regwb(struct brcmf_sdio_dev *sdiodev, u32 addr,
Franky Line9b8d912012-05-04 18:27:31 -0700408 u8 data, int *ret)
409{
410 int retval;
411
Arend van Sprielc3203372013-04-03 12:40:44 +0200412 brcmf_dbg(SDIO, "addr:0x%08x, data:0x%02x\n", addr, data);
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100413 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data,
414 true);
Franky Line9b8d912012-05-04 18:27:31 -0700415 if (ret)
416 *ret = retval;
417}
418
Arend van Spriela39be272013-12-12 11:58:58 +0100419void brcmf_sdiod_regwl(struct brcmf_sdio_dev *sdiodev, u32 addr,
Franky Line9b8d912012-05-04 18:27:31 -0700420 u32 data, int *ret)
421{
422 int retval;
423
Arend van Sprielc3203372013-04-03 12:40:44 +0200424 brcmf_dbg(SDIO, "addr:0x%08x, data:0x%08x\n", addr, data);
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100425 retval = brcmf_sdiod_addrprep(sdiodev, sizeof(data), &addr);
426 if (retval)
427 goto done;
428 retval = brcmf_sdiod_regrw_helper(sdiodev, addr, sizeof(data), &data,
429 true);
Franky Line9b8d912012-05-04 18:27:31 -0700430
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100431done:
Franky Line9b8d912012-05-04 18:27:31 -0700432 if (ret)
433 *ret = retval;
434}
435
Arend van Spriela39be272013-12-12 11:58:58 +0100436static int brcmf_sdiod_buffrw(struct brcmf_sdio_dev *sdiodev, uint fn,
Arend van Spriel4aef2672013-10-15 15:44:52 +0200437 bool write, u32 addr, struct sk_buff *pkt)
438{
439 unsigned int req_sz;
Arend van Sprielbb350712014-01-13 22:20:29 +0100440 int err;
Arend van Spriel4aef2672013-10-15 15:44:52 +0200441
Arend van Spriela39be272013-12-12 11:58:58 +0100442 brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_buffer_wait);
443 if (brcmf_sdiod_pm_resume_error(sdiodev))
Arend van Spriel4aef2672013-10-15 15:44:52 +0200444 return -EIO;
445
446 /* Single skb use the standard mmc interface */
447 req_sz = pkt->len + 3;
448 req_sz &= (uint)~3;
449
450 if (write)
Arend van Sprielbb350712014-01-13 22:20:29 +0100451 err = sdio_memcpy_toio(sdiodev->func[fn], addr,
452 ((u8 *)(pkt->data)), req_sz);
Arend van Spriel4aef2672013-10-15 15:44:52 +0200453 else if (fn == 1)
Arend van Sprielbb350712014-01-13 22:20:29 +0100454 err = sdio_memcpy_fromio(sdiodev->func[fn], ((u8 *)(pkt->data)),
455 addr, req_sz);
Arend van Spriel4aef2672013-10-15 15:44:52 +0200456 else
457 /* function 2 read is FIFO operation */
Arend van Sprielbb350712014-01-13 22:20:29 +0100458 err = sdio_readsb(sdiodev->func[fn], ((u8 *)(pkt->data)), addr,
459 req_sz);
460 if (err == -ENOMEDIUM)
461 brcmf_bus_change_state(sdiodev->bus_if, BRCMF_BUS_NOMEDIUM);
462 return err;
Arend van Spriel4aef2672013-10-15 15:44:52 +0200463}
464
Franky Lin78b3f1c2013-06-18 13:29:28 +0200465/**
Arend van Spriela39be272013-12-12 11:58:58 +0100466 * brcmf_sdiod_sglist_rw - SDIO interface function for block data access
Franky Lin78b3f1c2013-06-18 13:29:28 +0200467 * @sdiodev: brcmfmac sdio device
468 * @fn: SDIO function number
469 * @write: direction flag
470 * @addr: dongle memory address as source/destination
471 * @pkt: skb pointer
472 *
473 * This function takes the respbonsibility as the interface function to MMC
474 * stack for block data access. It assumes that the skb passed down by the
475 * caller has already been padded and aligned.
476 */
Arend van Spriela39be272013-12-12 11:58:58 +0100477static int brcmf_sdiod_sglist_rw(struct brcmf_sdio_dev *sdiodev, uint fn,
478 bool write, u32 addr,
479 struct sk_buff_head *pktlist)
Franky Lin78b3f1c2013-06-18 13:29:28 +0200480{
Franky Lin354b75b2013-06-18 13:29:29 +0200481 unsigned int req_sz, func_blk_sz, sg_cnt, sg_data_sz, pkt_offset;
Arend van Spriel71201492013-10-15 15:44:49 +0200482 unsigned int max_req_sz, orig_offset, dst_offset;
Arend van Spriel3f782742013-10-15 15:44:48 +0200483 unsigned short max_seg_cnt, seg_sz;
Franky Lin3b81a682013-06-26 14:20:18 +0200484 unsigned char *pkt_data, *orig_data, *dst_data;
485 struct sk_buff *pkt_next = NULL, *local_pkt_next;
486 struct sk_buff_head local_list, *target_list;
Franky Lin354b75b2013-06-18 13:29:29 +0200487 struct mmc_request mmc_req;
488 struct mmc_command mmc_cmd;
489 struct mmc_data mmc_dat;
490 struct sg_table st;
491 struct scatterlist *sgl;
Franky Lin354b75b2013-06-18 13:29:29 +0200492 int ret = 0;
493
494 if (!pktlist->qlen)
495 return -EINVAL;
Franky Lin78b3f1c2013-06-18 13:29:28 +0200496
Arend van Spriela39be272013-12-12 11:58:58 +0100497 brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_buffer_wait);
498 if (brcmf_sdiod_pm_resume_error(sdiodev))
Franky Lin78b3f1c2013-06-18 13:29:28 +0200499 return -EIO;
500
Franky Lin3b81a682013-06-26 14:20:18 +0200501 target_list = pktlist;
502 /* for host with broken sg support, prepare a page aligned list */
503 __skb_queue_head_init(&local_list);
504 if (sdiodev->pdata && sdiodev->pdata->broken_sg_support && !write) {
505 req_sz = 0;
506 skb_queue_walk(pktlist, pkt_next)
507 req_sz += pkt_next->len;
508 req_sz = ALIGN(req_sz, sdiodev->func[fn]->cur_blksize);
509 while (req_sz > PAGE_SIZE) {
510 pkt_next = brcmu_pkt_buf_get_skb(PAGE_SIZE);
511 if (pkt_next == NULL) {
512 ret = -ENOMEM;
513 goto exit;
514 }
515 __skb_queue_tail(&local_list, pkt_next);
516 req_sz -= PAGE_SIZE;
517 }
518 pkt_next = brcmu_pkt_buf_get_skb(req_sz);
519 if (pkt_next == NULL) {
520 ret = -ENOMEM;
521 goto exit;
522 }
523 __skb_queue_tail(&local_list, pkt_next);
524 target_list = &local_list;
525 }
526
Franky Lin354b75b2013-06-18 13:29:29 +0200527 func_blk_sz = sdiodev->func[fn]->cur_blksize;
Arend van Spriel71201492013-10-15 15:44:49 +0200528 max_req_sz = sdiodev->max_request_size;
529 max_seg_cnt = min_t(unsigned short, sdiodev->max_segment_count,
530 target_list->qlen);
Franky Lin3b81a682013-06-26 14:20:18 +0200531 seg_sz = target_list->qlen;
Franky Lin354b75b2013-06-18 13:29:29 +0200532 pkt_offset = 0;
Franky Lin3b81a682013-06-26 14:20:18 +0200533 pkt_next = target_list->next;
Franky Lin354b75b2013-06-18 13:29:29 +0200534
Arend van Spriel3f782742013-10-15 15:44:48 +0200535 if (sg_alloc_table(&st, max_seg_cnt, GFP_KERNEL)) {
Franky Lin3b81a682013-06-26 14:20:18 +0200536 ret = -ENOMEM;
537 goto exit;
538 }
Franky Lin354b75b2013-06-18 13:29:29 +0200539
Arend van Spriel7f9a8dc2013-10-15 15:44:50 +0200540 memset(&mmc_req, 0, sizeof(struct mmc_request));
541 memset(&mmc_cmd, 0, sizeof(struct mmc_command));
542 memset(&mmc_dat, 0, sizeof(struct mmc_data));
543
544 mmc_dat.sg = st.sgl;
545 mmc_dat.blksz = func_blk_sz;
546 mmc_dat.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
547 mmc_cmd.opcode = SD_IO_RW_EXTENDED;
548 mmc_cmd.arg = write ? 1<<31 : 0; /* write flag */
549 mmc_cmd.arg |= (fn & 0x7) << 28; /* SDIO func num */
550 mmc_cmd.arg |= 1<<27; /* block mode */
551 /* for function 1 the addr will be incremented */
552 mmc_cmd.arg |= (fn == 1) ? 1<<26 : 0;
553 mmc_cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC;
554 mmc_req.cmd = &mmc_cmd;
555 mmc_req.data = &mmc_dat;
556
Franky Lin354b75b2013-06-18 13:29:29 +0200557 while (seg_sz) {
558 req_sz = 0;
559 sg_cnt = 0;
Franky Lin354b75b2013-06-18 13:29:29 +0200560 sgl = st.sgl;
561 /* prep sg table */
Franky Lin3b81a682013-06-26 14:20:18 +0200562 while (pkt_next != (struct sk_buff *)target_list) {
Franky Lin354b75b2013-06-18 13:29:29 +0200563 pkt_data = pkt_next->data + pkt_offset;
564 sg_data_sz = pkt_next->len - pkt_offset;
Arend van Spriel71201492013-10-15 15:44:49 +0200565 if (sg_data_sz > sdiodev->max_segment_size)
566 sg_data_sz = sdiodev->max_segment_size;
Franky Lin354b75b2013-06-18 13:29:29 +0200567 if (sg_data_sz > max_req_sz - req_sz)
568 sg_data_sz = max_req_sz - req_sz;
569
570 sg_set_buf(sgl, pkt_data, sg_data_sz);
571
572 sg_cnt++;
573 sgl = sg_next(sgl);
574 req_sz += sg_data_sz;
575 pkt_offset += sg_data_sz;
576 if (pkt_offset == pkt_next->len) {
577 pkt_offset = 0;
578 pkt_next = pkt_next->next;
579 }
580
Arend van Spriel3f782742013-10-15 15:44:48 +0200581 if (req_sz >= max_req_sz || sg_cnt >= max_seg_cnt)
Franky Lin354b75b2013-06-18 13:29:29 +0200582 break;
583 }
584 seg_sz -= sg_cnt;
585
586 if (req_sz % func_blk_sz != 0) {
587 brcmf_err("sg request length %u is not %u aligned\n",
588 req_sz, func_blk_sz);
Franky Lin3b81a682013-06-26 14:20:18 +0200589 ret = -ENOTBLK;
590 goto exit;
Franky Lin354b75b2013-06-18 13:29:29 +0200591 }
Arend van Spriel7f9a8dc2013-10-15 15:44:50 +0200592
Franky Lin354b75b2013-06-18 13:29:29 +0200593 mmc_dat.sg_len = sg_cnt;
Franky Lin354b75b2013-06-18 13:29:29 +0200594 mmc_dat.blocks = req_sz / func_blk_sz;
Franky Lin354b75b2013-06-18 13:29:29 +0200595 mmc_cmd.arg |= (addr & 0x1FFFF) << 9; /* address */
596 mmc_cmd.arg |= mmc_dat.blocks & 0x1FF; /* block count */
Arend van Spriel7f9a8dc2013-10-15 15:44:50 +0200597 /* incrementing addr for function 1 */
Franky Lin354b75b2013-06-18 13:29:29 +0200598 if (fn == 1)
599 addr += req_sz;
600
601 mmc_set_data_timeout(&mmc_dat, sdiodev->func[fn]->card);
Arend van Spriel71201492013-10-15 15:44:49 +0200602 mmc_wait_for_req(sdiodev->func[fn]->card->host, &mmc_req);
Franky Lin354b75b2013-06-18 13:29:29 +0200603
604 ret = mmc_cmd.error ? mmc_cmd.error : mmc_dat.error;
Arend van Sprielbb350712014-01-13 22:20:29 +0100605 if (ret == -ENOMEDIUM) {
606 brcmf_bus_change_state(sdiodev->bus_if,
607 BRCMF_BUS_NOMEDIUM);
608 break;
609 } else if (ret != 0) {
Franky Lin354b75b2013-06-18 13:29:29 +0200610 brcmf_err("CMD53 sg block %s failed %d\n",
611 write ? "write" : "read", ret);
612 ret = -EIO;
613 break;
614 }
615 }
616
Franky Lin3b81a682013-06-26 14:20:18 +0200617 if (sdiodev->pdata && sdiodev->pdata->broken_sg_support && !write) {
618 local_pkt_next = local_list.next;
619 orig_offset = 0;
620 skb_queue_walk(pktlist, pkt_next) {
621 dst_offset = 0;
622 do {
623 req_sz = local_pkt_next->len - orig_offset;
624 req_sz = min_t(uint, pkt_next->len - dst_offset,
625 req_sz);
626 orig_data = local_pkt_next->data + orig_offset;
627 dst_data = pkt_next->data + dst_offset;
628 memcpy(dst_data, orig_data, req_sz);
629 orig_offset += req_sz;
630 dst_offset += req_sz;
631 if (orig_offset == local_pkt_next->len) {
632 orig_offset = 0;
633 local_pkt_next = local_pkt_next->next;
634 }
635 if (dst_offset == pkt_next->len)
636 break;
637 } while (!skb_queue_empty(&local_list));
638 }
639 }
640
641exit:
Franky Lin354b75b2013-06-18 13:29:29 +0200642 sg_free_table(&st);
Franky Lin3b81a682013-06-26 14:20:18 +0200643 while ((pkt_next = __skb_dequeue(&local_list)) != NULL)
644 brcmu_pkt_buf_free_skb(pkt_next);
Franky Lin354b75b2013-06-18 13:29:29 +0200645
646 return ret;
Franky Lin78b3f1c2013-06-18 13:29:28 +0200647}
648
Arend van Spriela7cdd822013-12-12 11:58:59 +0100649int brcmf_sdiod_recv_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes)
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800650{
651 struct sk_buff *mypkt;
652 int err;
653
654 mypkt = brcmu_pkt_buf_get_skb(nbytes);
655 if (!mypkt) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100656 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n",
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800657 nbytes);
658 return -EIO;
659 }
660
Arend van Spriela7cdd822013-12-12 11:58:59 +0100661 err = brcmf_sdiod_recv_pkt(sdiodev, mypkt);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800662 if (!err)
663 memcpy(buf, mypkt->data, nbytes);
664
665 brcmu_pkt_buf_free_skb(mypkt);
666 return err;
667}
668
Arend van Spriela7cdd822013-12-12 11:58:59 +0100669int brcmf_sdiod_recv_pkt(struct brcmf_sdio_dev *sdiodev, struct sk_buff *pkt)
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800670{
Arend van Spriela7cdd822013-12-12 11:58:59 +0100671 u32 addr = sdiodev->sbwad;
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800672 int err = 0;
673
Arend van Spriela7cdd822013-12-12 11:58:59 +0100674 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pkt->len);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800675
Arend van Spriela7cdd822013-12-12 11:58:59 +0100676 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800677 if (err)
Franky Lin7057fd02012-09-13 21:12:02 +0200678 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200679
Arend van Spriela7cdd822013-12-12 11:58:59 +0100680 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr, pkt);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800681
Franky Lin7057fd02012-09-13 21:12:02 +0200682done:
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800683 return err;
684}
685
Arend van Spriela7cdd822013-12-12 11:58:59 +0100686int brcmf_sdiod_recv_chain(struct brcmf_sdio_dev *sdiodev,
687 struct sk_buff_head *pktq, uint totlen)
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800688{
Arend van Spriela413e392013-10-15 15:44:57 +0200689 struct sk_buff *glom_skb;
690 struct sk_buff *skb;
Arend van Spriela7cdd822013-12-12 11:58:59 +0100691 u32 addr = sdiodev->sbwad;
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800692 int err = 0;
693
Arend van Spriela7cdd822013-12-12 11:58:59 +0100694 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n",
695 addr, pktq->qlen);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800696
Arend van Spriela7cdd822013-12-12 11:58:59 +0100697 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800698 if (err)
Franky Lin7057fd02012-09-13 21:12:02 +0200699 goto done;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200700
Arend van Spriela413e392013-10-15 15:44:57 +0200701 if (pktq->qlen == 1)
Arend van Spriela7cdd822013-12-12 11:58:59 +0100702 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr,
703 pktq->next);
Arend van Spriela413e392013-10-15 15:44:57 +0200704 else if (!sdiodev->sg_support) {
705 glom_skb = brcmu_pkt_buf_get_skb(totlen);
706 if (!glom_skb)
707 return -ENOMEM;
Arend van Spriela7cdd822013-12-12 11:58:59 +0100708 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr,
709 glom_skb);
Arend van Spriela413e392013-10-15 15:44:57 +0200710 if (err)
711 goto done;
712
713 skb_queue_walk(pktq, skb) {
714 memcpy(skb->data, glom_skb->data, skb->len);
715 skb_pull(glom_skb, skb->len);
716 }
717 } else
Arend van Spriela7cdd822013-12-12 11:58:59 +0100718 err = brcmf_sdiod_sglist_rw(sdiodev, SDIO_FUNC_2, false, addr,
719 pktq);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200720
Franky Lin7057fd02012-09-13 21:12:02 +0200721done:
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800722 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200723}
724
Arend van Spriela7cdd822013-12-12 11:58:59 +0100725int brcmf_sdiod_send_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes)
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800726{
727 struct sk_buff *mypkt;
Arend van Spriela7cdd822013-12-12 11:58:59 +0100728 u32 addr = sdiodev->sbwad;
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800729 int err;
730
731 mypkt = brcmu_pkt_buf_get_skb(nbytes);
732 if (!mypkt) {
Arend van Spriel5e8149f2012-12-07 10:49:57 +0100733 brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n",
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800734 nbytes);
735 return -EIO;
736 }
737
738 memcpy(mypkt->data, buf, nbytes);
Arend van Spriel4aef2672013-10-15 15:44:52 +0200739
Arend van Spriela7cdd822013-12-12 11:58:59 +0100740 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
Arend van Spriel4aef2672013-10-15 15:44:52 +0200741
Arend van Sprielfcaac2b2013-10-15 15:44:53 +0200742 if (!err)
Arend van Spriela7cdd822013-12-12 11:58:59 +0100743 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, true, addr,
744 mypkt);
Arend van Spriel5adfeb62011-11-22 17:21:38 -0800745
746 brcmu_pkt_buf_free_skb(mypkt);
747 return err;
748
749}
750
Arend van Spriela7cdd822013-12-12 11:58:59 +0100751int brcmf_sdiod_send_pkt(struct brcmf_sdio_dev *sdiodev,
752 struct sk_buff_head *pktq)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200753{
Arend van Sprielc8cce1f2013-10-15 15:44:55 +0200754 struct sk_buff *skb;
Arend van Spriela7cdd822013-12-12 11:58:59 +0100755 u32 addr = sdiodev->sbwad;
Arend van Sprielfcaac2b2013-10-15 15:44:53 +0200756 int err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200757
Arend van Spriela7cdd822013-12-12 11:58:59 +0100758 brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pktq->qlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200759
Arend van Spriela7cdd822013-12-12 11:58:59 +0100760 err = brcmf_sdiod_addrprep(sdiodev, 4, &addr);
Arend van Sprielfcaac2b2013-10-15 15:44:53 +0200761 if (err)
762 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200763
Arend van Sprielc8cce1f2013-10-15 15:44:55 +0200764 if (pktq->qlen == 1 || !sdiodev->sg_support)
765 skb_queue_walk(pktq, skb) {
Arend van Spriela7cdd822013-12-12 11:58:59 +0100766 err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, true,
767 addr, skb);
Arend van Sprielc8cce1f2013-10-15 15:44:55 +0200768 if (err)
769 break;
770 }
771 else
Arend van Spriela7cdd822013-12-12 11:58:59 +0100772 err = brcmf_sdiod_sglist_rw(sdiodev, SDIO_FUNC_2, true, addr,
773 pktq);
Arend van Sprielc8cce1f2013-10-15 15:44:55 +0200774
775 return err;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200776}
777
Franky Linba540b02013-04-11 13:28:47 +0200778int
Arend van Spriela39be272013-12-12 11:58:58 +0100779brcmf_sdiod_ramrw(struct brcmf_sdio_dev *sdiodev, bool write, u32 address,
780 u8 *data, uint size)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200781{
Franky Linba540b02013-04-11 13:28:47 +0200782 int bcmerror = 0;
783 struct sk_buff *pkt;
784 u32 sdaddr;
785 uint dsize;
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800786
Franky Linba540b02013-04-11 13:28:47 +0200787 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
788 pkt = dev_alloc_skb(dsize);
789 if (!pkt) {
790 brcmf_err("dev_alloc_skb failed: len %d\n", dsize);
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800791 return -EIO;
792 }
Franky Linba540b02013-04-11 13:28:47 +0200793 pkt->priority = 0;
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800794
Franky Linba540b02013-04-11 13:28:47 +0200795 /* Determine initial transfer parameters */
796 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
797 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
798 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
799 else
800 dsize = size;
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800801
Franky Linba540b02013-04-11 13:28:47 +0200802 sdio_claim_host(sdiodev->func[1]);
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800803
Franky Linba540b02013-04-11 13:28:47 +0200804 /* Do the transfer(s) */
805 while (size) {
806 /* Set the backplane window to include the start address */
Arend van Spriela39be272013-12-12 11:58:58 +0100807 bcmerror = brcmf_sdiod_set_sbaddr_window(sdiodev, address);
Franky Linba540b02013-04-11 13:28:47 +0200808 if (bcmerror)
809 break;
Arend van Spriel4c6e8692011-11-22 17:21:40 -0800810
Franky Linba540b02013-04-11 13:28:47 +0200811 brcmf_dbg(SDIO, "%s %d bytes at offset 0x%08x in window 0x%08x\n",
812 write ? "write" : "read", dsize,
813 sdaddr, address & SBSDIO_SBWINDOW_MASK);
814
815 sdaddr &= SBSDIO_SB_OFT_ADDR_MASK;
816 sdaddr |= SBSDIO_SB_ACCESS_2_4B_FLAG;
817
818 skb_put(pkt, dsize);
819 if (write)
820 memcpy(pkt->data, data, dsize);
Arend van Spriela39be272013-12-12 11:58:58 +0100821 bcmerror = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_1, write,
822 sdaddr, pkt);
Franky Linba540b02013-04-11 13:28:47 +0200823 if (bcmerror) {
824 brcmf_err("membytes transfer failed\n");
825 break;
826 }
827 if (!write)
828 memcpy(data, pkt->data, dsize);
Hante Meuleman79c868e2014-01-29 15:32:16 +0100829 skb_trim(pkt, 0);
Franky Linba540b02013-04-11 13:28:47 +0200830
831 /* Adjust for next transfer (if any) */
832 size -= dsize;
833 if (size) {
834 data += dsize;
835 address += dsize;
836 sdaddr = 0;
837 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
838 }
839 }
840
841 dev_kfree_skb(pkt);
842
843 /* Return the window to backplane enumeration space for core access */
Arend van Spriela39be272013-12-12 11:58:58 +0100844 if (brcmf_sdiod_set_sbaddr_window(sdiodev, sdiodev->sbwad))
Franky Linba540b02013-04-11 13:28:47 +0200845 brcmf_err("FAILED to set window back to 0x%x\n",
846 sdiodev->sbwad);
847
848 sdio_release_host(sdiodev->func[1]);
849
850 return bcmerror;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200851}
852
Arend van Spriela39be272013-12-12 11:58:58 +0100853int brcmf_sdiod_abort(struct brcmf_sdio_dev *sdiodev, uint fn)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200854{
855 char t_func = (char)fn;
Arend van Sprielc3203372013-04-03 12:40:44 +0200856 brcmf_dbg(SDIO, "Enter\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200857
858 /* issue abort cmd52 command through F0 */
Arend van Spriel71c60cf2014-01-06 12:40:36 +0100859 brcmf_sdiod_request_data(sdiodev, SDIO_FUNC_0, SDIO_CCCR_ABORT,
860 sizeof(t_func), &t_func, true);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200861
Arend van Sprielc3203372013-04-03 12:40:44 +0200862 brcmf_dbg(SDIO, "Exit\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +0200863 return 0;
864}
865
Arend van Spriela39be272013-12-12 11:58:58 +0100866static int brcmf_sdiod_remove(struct brcmf_sdio_dev *sdiodev)
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100867{
Arend van Spriela39be272013-12-12 11:58:58 +0100868 if (sdiodev->bus) {
Arend van Spriel9fbe2a62013-12-12 11:59:05 +0100869 brcmf_sdio_remove(sdiodev->bus);
Arend van Spriela39be272013-12-12 11:58:58 +0100870 sdiodev->bus = NULL;
871 }
872
873 /* Disable Function 2 */
874 sdio_claim_host(sdiodev->func[2]);
875 sdio_disable_func(sdiodev->func[2]);
876 sdio_release_host(sdiodev->func[2]);
877
878 /* Disable Function 1 */
879 sdio_claim_host(sdiodev->func[1]);
880 sdio_disable_func(sdiodev->func[1]);
881 sdio_release_host(sdiodev->func[1]);
882
883 sdiodev->sbwad = 0;
884
885 return 0;
886}
887
888static int brcmf_sdiod_probe(struct brcmf_sdio_dev *sdiodev)
889{
890 struct sdio_func *func;
891 struct mmc_host *host;
892 uint max_blocks;
893 int ret = 0;
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100894
895 sdiodev->num_funcs = 2;
896
897 sdio_claim_host(sdiodev->func[1]);
898
Arend van Spriela39be272013-12-12 11:58:58 +0100899 ret = sdio_set_block_size(sdiodev->func[1], SDIO_FUNC1_BLOCKSIZE);
900 if (ret) {
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100901 brcmf_err("Failed to set F1 blocksize\n");
Arend van Spriela39be272013-12-12 11:58:58 +0100902 sdio_release_host(sdiodev->func[1]);
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100903 goto out;
904 }
Arend van Spriela39be272013-12-12 11:58:58 +0100905 ret = sdio_set_block_size(sdiodev->func[2], SDIO_FUNC2_BLOCKSIZE);
906 if (ret) {
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100907 brcmf_err("Failed to set F2 blocksize\n");
Arend van Spriela39be272013-12-12 11:58:58 +0100908 sdio_release_host(sdiodev->func[1]);
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100909 goto out;
910 }
911
Arend van Spriel71370eb2013-12-12 11:58:53 +0100912 /* increase F2 timeout */
913 sdiodev->func[2]->enable_timeout = SDIO_WAIT_F2RDY;
914
Arend van Spriel463c30b2013-12-12 11:58:52 +0100915 /* Enable Function 1 */
Arend van Spriela39be272013-12-12 11:58:58 +0100916 ret = sdio_enable_func(sdiodev->func[1]);
917 sdio_release_host(sdiodev->func[1]);
918 if (ret) {
919 brcmf_err("Failed to enable F1: err=%d\n", ret);
Arend van Spriel463c30b2013-12-12 11:58:52 +0100920 goto out;
921 }
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100922
923 /*
Arend van Spriela39be272013-12-12 11:58:58 +0100924 * determine host related variables after brcmf_sdiod_probe()
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100925 * as func->cur_blksize is properly set and F2 init has been
926 * completed successfully.
927 */
928 func = sdiodev->func[2];
929 host = func->card->host;
930 sdiodev->sg_support = host->max_segs > 1;
931 max_blocks = min_t(uint, host->max_blk_count, 511u);
932 sdiodev->max_request_size = min_t(uint, host->max_req_size,
933 max_blocks * func->cur_blksize);
934 sdiodev->max_segment_count = min_t(uint, host->max_segs,
935 SG_MAX_SINGLE_ALLOC);
936 sdiodev->max_segment_size = host->max_seg_size;
Arend van Spriele49b06b2013-12-12 11:58:51 +0100937
Arend van Spriele49b06b2013-12-12 11:58:51 +0100938 /* try to attach to the target device */
Arend van Spriel82d7f3c2013-12-12 11:59:04 +0100939 sdiodev->bus = brcmf_sdio_probe(sdiodev);
Arend van Spriele49b06b2013-12-12 11:58:51 +0100940 if (!sdiodev->bus) {
Arend van Spriele49b06b2013-12-12 11:58:51 +0100941 ret = -ENODEV;
942 goto out;
943 }
944
945out:
946 if (ret)
Arend van Spriela39be272013-12-12 11:58:58 +0100947 brcmf_sdiod_remove(sdiodev);
Arend van Spriele49b06b2013-12-12 11:58:51 +0100948
949 return ret;
950}
951
952/* devices we support, null terminated */
953static const struct sdio_device_id brcmf_sdmmc_ids[] = {
954 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43143)},
955 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43241)},
956 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4329)},
957 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4330)},
958 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4334)},
Arend van Spriel11e69c32014-01-08 10:49:33 +0100959 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43362)},
Arend van Spriele49b06b2013-12-12 11:58:51 +0100960 {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM,
961 SDIO_DEVICE_ID_BROADCOM_4335_4339)},
962 { /* end: all zeroes */ },
963};
964MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids);
965
966static struct brcmfmac_sdio_platform_data *brcmfmac_sdio_pdata;
967
968
Arend van Spriele2dc9ee2013-12-12 11:58:50 +0100969static int brcmf_ops_sdio_probe(struct sdio_func *func,
970 const struct sdio_device_id *id)
971{
972 int err;
973 struct brcmf_sdio_dev *sdiodev;
974 struct brcmf_bus *bus_if;
975
976 brcmf_dbg(SDIO, "Enter\n");
977 brcmf_dbg(SDIO, "Class=%x\n", func->class);
978 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor);
979 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device);
980 brcmf_dbg(SDIO, "Function#: %d\n", func->num);
981
982 /* Consume func num 1 but dont do anything with it. */
983 if (func->num == 1)
984 return 0;
985
986 /* Ignore anything but func 2 */
987 if (func->num != 2)
988 return -ENODEV;
989
990 bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL);
991 if (!bus_if)
992 return -ENOMEM;
993 sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL);
994 if (!sdiodev) {
995 kfree(bus_if);
996 return -ENOMEM;
997 }
998
Arend van Spriel36c4e7e2013-12-12 11:59:06 +0100999 /* store refs to functions used. mmc_card does
1000 * not hold the F0 function pointer.
1001 */
1002 sdiodev->func[0] = kmemdup(func, sizeof(*func), GFP_KERNEL);
1003 sdiodev->func[0]->num = 0;
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001004 sdiodev->func[1] = func->card->sdio_func[0];
1005 sdiodev->func[2] = func;
1006
1007 sdiodev->bus_if = bus_if;
1008 bus_if->bus_priv.sdio = sdiodev;
Hante Meuleman943258b2013-12-12 11:59:02 +01001009 bus_if->proto_type = BRCMF_PROTO_BCDC;
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001010 dev_set_drvdata(&func->dev, bus_if);
1011 dev_set_drvdata(&sdiodev->func[1]->dev, bus_if);
1012 sdiodev->dev = &sdiodev->func[1]->dev;
1013 sdiodev->pdata = brcmfmac_sdio_pdata;
1014
1015 atomic_set(&sdiodev->suspend, false);
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001016 init_waitqueue_head(&sdiodev->request_word_wait);
1017 init_waitqueue_head(&sdiodev->request_buffer_wait);
1018
Arend van Spriela39be272013-12-12 11:58:58 +01001019 brcmf_dbg(SDIO, "F2 found, calling brcmf_sdiod_probe...\n");
1020 err = brcmf_sdiod_probe(sdiodev);
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001021 if (err) {
1022 brcmf_err("F2 error, probe failed %d...\n", err);
1023 goto fail;
1024 }
1025
1026 brcmf_dbg(SDIO, "F2 init completed...\n");
1027 return 0;
1028
1029fail:
1030 dev_set_drvdata(&func->dev, NULL);
1031 dev_set_drvdata(&sdiodev->func[1]->dev, NULL);
Arend van Spriel36c4e7e2013-12-12 11:59:06 +01001032 kfree(sdiodev->func[0]);
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001033 kfree(sdiodev);
1034 kfree(bus_if);
1035 return err;
1036}
1037
1038static void brcmf_ops_sdio_remove(struct sdio_func *func)
1039{
1040 struct brcmf_bus *bus_if;
1041 struct brcmf_sdio_dev *sdiodev;
1042
1043 brcmf_dbg(SDIO, "Enter\n");
1044 brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor);
1045 brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device);
1046 brcmf_dbg(SDIO, "Function: %d\n", func->num);
1047
1048 if (func->num != 1 && func->num != 2)
1049 return;
1050
1051 bus_if = dev_get_drvdata(&func->dev);
1052 if (bus_if) {
1053 sdiodev = bus_if->bus_priv.sdio;
Arend van Spriela39be272013-12-12 11:58:58 +01001054 brcmf_sdiod_remove(sdiodev);
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001055
1056 dev_set_drvdata(&sdiodev->func[1]->dev, NULL);
1057 dev_set_drvdata(&sdiodev->func[2]->dev, NULL);
1058
1059 kfree(bus_if);
Arend van Spriel36c4e7e2013-12-12 11:59:06 +01001060 kfree(sdiodev->func[0]);
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001061 kfree(sdiodev);
1062 }
1063
1064 brcmf_dbg(SDIO, "Exit\n");
1065}
1066
1067#ifdef CONFIG_PM_SLEEP
Arend van Spriela39be272013-12-12 11:58:58 +01001068static int brcmf_ops_sdio_suspend(struct device *dev)
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001069{
1070 mmc_pm_flag_t sdio_flags;
1071 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1072 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
1073 int ret = 0;
1074
1075 brcmf_dbg(SDIO, "\n");
1076
1077 atomic_set(&sdiodev->suspend, true);
1078
1079 sdio_flags = sdio_get_host_pm_caps(sdiodev->func[1]);
1080 if (!(sdio_flags & MMC_PM_KEEP_POWER)) {
1081 brcmf_err("Host can't keep power while suspended\n");
1082 return -EINVAL;
1083 }
1084
1085 ret = sdio_set_host_pm_flags(sdiodev->func[1], MMC_PM_KEEP_POWER);
1086 if (ret) {
1087 brcmf_err("Failed to set pm_flags\n");
1088 return ret;
1089 }
1090
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001091 brcmf_sdio_wd_timer(sdiodev->bus, 0);
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001092
1093 return ret;
1094}
1095
Arend van Spriela39be272013-12-12 11:58:58 +01001096static int brcmf_ops_sdio_resume(struct device *dev)
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001097{
1098 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
1099 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
1100
Arend van Spriel82d7f3c2013-12-12 11:59:04 +01001101 brcmf_sdio_wd_timer(sdiodev->bus, BRCMF_WD_POLL_MS);
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001102 atomic_set(&sdiodev->suspend, false);
1103 return 0;
1104}
1105
1106static const struct dev_pm_ops brcmf_sdio_pm_ops = {
Arend van Spriela39be272013-12-12 11:58:58 +01001107 .suspend = brcmf_ops_sdio_suspend,
1108 .resume = brcmf_ops_sdio_resume,
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001109};
1110#endif /* CONFIG_PM_SLEEP */
1111
1112static struct sdio_driver brcmf_sdmmc_driver = {
1113 .probe = brcmf_ops_sdio_probe,
1114 .remove = brcmf_ops_sdio_remove,
1115 .name = BRCMFMAC_SDIO_PDATA_NAME,
1116 .id_table = brcmf_sdmmc_ids,
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001117 .drv = {
Franky Lin0b0acd82014-01-29 15:32:12 +01001118 .owner = THIS_MODULE,
1119#ifdef CONFIG_PM_SLEEP
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001120 .pm = &brcmf_sdio_pm_ops,
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001121#endif /* CONFIG_PM_SLEEP */
Franky Lin0b0acd82014-01-29 15:32:12 +01001122 },
Arend van Spriele2dc9ee2013-12-12 11:58:50 +01001123};
1124
1125static int brcmf_sdio_pd_probe(struct platform_device *pdev)
1126{
1127 brcmf_dbg(SDIO, "Enter\n");
1128
1129 brcmfmac_sdio_pdata = dev_get_platdata(&pdev->dev);
1130
1131 if (brcmfmac_sdio_pdata->power_on)
1132 brcmfmac_sdio_pdata->power_on();
1133
1134 return 0;
1135}
1136
1137static int brcmf_sdio_pd_remove(struct platform_device *pdev)
1138{
1139 brcmf_dbg(SDIO, "Enter\n");
1140
1141 if (brcmfmac_sdio_pdata->power_off)
1142 brcmfmac_sdio_pdata->power_off();
1143
1144 sdio_unregister_driver(&brcmf_sdmmc_driver);
1145
1146 return 0;
1147}
1148
1149static struct platform_driver brcmf_sdio_pd = {
1150 .remove = brcmf_sdio_pd_remove,
1151 .driver = {
1152 .name = BRCMFMAC_SDIO_PDATA_NAME,
1153 .owner = THIS_MODULE,
1154 }
1155};
1156
1157void brcmf_sdio_register(void)
1158{
1159 int ret;
1160
1161 ret = sdio_register_driver(&brcmf_sdmmc_driver);
1162 if (ret)
1163 brcmf_err("sdio_register_driver failed: %d\n", ret);
1164}
1165
1166void brcmf_sdio_exit(void)
1167{
1168 brcmf_dbg(SDIO, "Enter\n");
1169
1170 if (brcmfmac_sdio_pdata)
1171 platform_driver_unregister(&brcmf_sdio_pd);
1172 else
1173 sdio_unregister_driver(&brcmf_sdmmc_driver);
1174}
1175
1176void __init brcmf_sdio_init(void)
1177{
1178 int ret;
1179
1180 brcmf_dbg(SDIO, "Enter\n");
1181
1182 ret = platform_driver_probe(&brcmf_sdio_pd, brcmf_sdio_pd_probe);
1183 if (ret == -ENODEV)
1184 brcmf_dbg(SDIO, "No platform data available.\n");
1185}