2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 /* ****************** SDIO CARD Interface Functions **************************/
18 #include <linux/types.h>
19 #include <linux/netdevice.h>
20 #include <linux/pci.h>
21 #include <linux/pci_ids.h>
22 #include <linux/sched.h>
23 #include <linux/completion.h>
24 #include <linux/mmc/sdio.h>
25 #include <linux/mmc/sdio_func.h>
26 #include <linux/mmc/card.h>
29 #include <brcm_hw_ids.h>
30 #include <brcmu_utils.h>
31 #include <brcmu_wifi.h>
36 #include "sdio_host.h"
38 #define SDIOH_API_ACCESS_RETRY_LIMIT 2
40 static void brcmf_sdioh_irqhandler(struct sdio_func
*func
)
42 struct brcmf_sdio_dev
*sdiodev
= dev_get_drvdata(&func
->card
->dev
);
44 brcmf_dbg(TRACE
, "***IRQHandler\n");
46 sdio_release_host(func
);
48 brcmf_sdbrcm_isr(sdiodev
->bus
);
50 sdio_claim_host(func
);
53 int brcmf_sdcard_intr_reg(struct brcmf_sdio_dev
*sdiodev
)
55 brcmf_dbg(TRACE
, "Entering\n");
57 sdio_claim_host(sdiodev
->func
[1]);
58 sdio_claim_irq(sdiodev
->func
[1], brcmf_sdioh_irqhandler
);
59 sdio_release_host(sdiodev
->func
[1]);
64 int brcmf_sdcard_intr_dereg(struct brcmf_sdio_dev
*sdiodev
)
66 brcmf_dbg(TRACE
, "Entering\n");
68 sdio_claim_host(sdiodev
->func
[1]);
69 sdio_release_irq(sdiodev
->func
[1]);
70 sdio_release_host(sdiodev
->func
[1]);
75 u8
brcmf_sdcard_cfg_read(struct brcmf_sdio_dev
*sdiodev
, uint fnc_num
, u32 addr
,
83 if (retry
) /* wait for 1 ms till bus get settled down */
85 status
= brcmf_sdioh_request_byte(sdiodev
, SDIOH_READ
, fnc_num
,
88 && (retry
++ < SDIOH_API_ACCESS_RETRY_LIMIT
));
92 brcmf_dbg(INFO
, "fun = %d, addr = 0x%x, u8data = 0x%x\n",
99 brcmf_sdcard_cfg_write(struct brcmf_sdio_dev
*sdiodev
, uint fnc_num
, u32 addr
,
106 if (retry
) /* wait for 1 ms till bus get settled down */
108 status
= brcmf_sdioh_request_byte(sdiodev
, SDIOH_WRITE
, fnc_num
,
111 && (retry
++ < SDIOH_API_ACCESS_RETRY_LIMIT
));
115 brcmf_dbg(INFO
, "fun = %d, addr = 0x%x, u8data = 0x%x\n",
116 fnc_num
, addr
, data
);
120 brcmf_sdcard_set_sbaddr_window(struct brcmf_sdio_dev
*sdiodev
, u32 address
)
123 brcmf_sdcard_cfg_write(sdiodev
, SDIO_FUNC_1
, SBSDIO_FUNC1_SBADDRLOW
,
124 (address
>> 8) & SBSDIO_SBADDRLOW_MASK
, &err
);
126 brcmf_sdcard_cfg_write(sdiodev
, SDIO_FUNC_1
,
127 SBSDIO_FUNC1_SBADDRMID
,
128 (address
>> 16) & SBSDIO_SBADDRMID_MASK
,
131 brcmf_sdcard_cfg_write(sdiodev
, SDIO_FUNC_1
,
132 SBSDIO_FUNC1_SBADDRHIGH
,
133 (address
>> 24) & SBSDIO_SBADDRHIGH_MASK
,
139 u32
brcmf_sdcard_reg_read(struct brcmf_sdio_dev
*sdiodev
, u32 addr
, uint size
)
143 uint bar0
= addr
& ~SBSDIO_SB_OFT_ADDR_MASK
;
145 brcmf_dbg(INFO
, "fun = 1, addr = 0x%x\n", addr
);
147 if (bar0
!= sdiodev
->sbwad
) {
148 if (brcmf_sdcard_set_sbaddr_window(sdiodev
, bar0
))
151 sdiodev
->sbwad
= bar0
;
154 addr
&= SBSDIO_SB_OFT_ADDR_MASK
;
156 addr
|= SBSDIO_SB_ACCESS_2_4B_FLAG
;
158 status
= brcmf_sdioh_request_word(sdiodev
, SDIOH_READ
, SDIO_FUNC_1
,
161 sdiodev
->regfail
= (status
!= 0);
163 brcmf_dbg(INFO
, "u32data = 0x%x\n", word
);
165 /* if ok, return appropriately masked word */
171 return word
& 0xffff;
175 sdiodev
->regfail
= true;
180 /* otherwise, bad sdio access or invalid size */
181 brcmf_dbg(ERROR
, "error reading addr 0x%04x size %d\n", addr
, size
);
185 u32
brcmf_sdcard_reg_write(struct brcmf_sdio_dev
*sdiodev
, u32 addr
, uint size
,
189 uint bar0
= addr
& ~SBSDIO_SB_OFT_ADDR_MASK
;
192 brcmf_dbg(INFO
, "fun = 1, addr = 0x%x, uint%ddata = 0x%x\n",
193 addr
, size
* 8, data
);
195 if (bar0
!= sdiodev
->sbwad
) {
196 err
= brcmf_sdcard_set_sbaddr_window(sdiodev
, bar0
);
200 sdiodev
->sbwad
= bar0
;
203 addr
&= SBSDIO_SB_OFT_ADDR_MASK
;
205 addr
|= SBSDIO_SB_ACCESS_2_4B_FLAG
;
207 brcmf_sdioh_request_word(sdiodev
, SDIOH_WRITE
, SDIO_FUNC_1
,
209 sdiodev
->regfail
= (status
!= 0);
214 brcmf_dbg(ERROR
, "error writing 0x%08x to addr 0x%04x size %d\n",
219 bool brcmf_sdcard_regfail(struct brcmf_sdio_dev
*sdiodev
)
221 return sdiodev
->regfail
;
225 brcmf_sdcard_recv_buf(struct brcmf_sdio_dev
*sdiodev
, u32 addr
, uint fn
,
227 u8
*buf
, uint nbytes
, struct sk_buff
*pkt
)
232 uint bar0
= addr
& ~SBSDIO_SB_OFT_ADDR_MASK
;
235 brcmf_dbg(INFO
, "fun = %d, addr = 0x%x, size = %d\n", fn
, addr
, nbytes
);
237 /* Async not implemented yet */
238 if (flags
& SDIO_REQ_ASYNC
)
241 if (bar0
!= sdiodev
->sbwad
) {
242 err
= brcmf_sdcard_set_sbaddr_window(sdiodev
, bar0
);
246 sdiodev
->sbwad
= bar0
;
249 addr
&= SBSDIO_SB_OFT_ADDR_MASK
;
251 incr_fix
= (flags
& SDIO_REQ_FIXED
) ? SDIOH_DATA_FIX
: SDIOH_DATA_INC
;
252 width
= (flags
& SDIO_REQ_4BYTE
) ? 4 : 2;
254 addr
|= SBSDIO_SB_ACCESS_2_4B_FLAG
;
256 status
= brcmf_sdioh_request_buffer(sdiodev
, incr_fix
, SDIOH_READ
,
257 fn
, addr
, width
, nbytes
, buf
, pkt
);
263 brcmf_sdcard_send_buf(struct brcmf_sdio_dev
*sdiodev
, u32 addr
, uint fn
,
264 uint flags
, u8
*buf
, uint nbytes
, struct sk_buff
*pkt
)
268 uint bar0
= addr
& ~SBSDIO_SB_OFT_ADDR_MASK
;
271 brcmf_dbg(INFO
, "fun = %d, addr = 0x%x, size = %d\n", fn
, addr
, nbytes
);
273 /* Async not implemented yet */
274 if (flags
& SDIO_REQ_ASYNC
)
277 if (bar0
!= sdiodev
->sbwad
) {
278 err
= brcmf_sdcard_set_sbaddr_window(sdiodev
, bar0
);
282 sdiodev
->sbwad
= bar0
;
285 addr
&= SBSDIO_SB_OFT_ADDR_MASK
;
287 incr_fix
= (flags
& SDIO_REQ_FIXED
) ? SDIOH_DATA_FIX
: SDIOH_DATA_INC
;
288 width
= (flags
& SDIO_REQ_4BYTE
) ? 4 : 2;
290 addr
|= SBSDIO_SB_ACCESS_2_4B_FLAG
;
292 return brcmf_sdioh_request_buffer(sdiodev
, incr_fix
, SDIOH_WRITE
, fn
,
293 addr
, width
, nbytes
, buf
, pkt
);
296 int brcmf_sdcard_rwdata(struct brcmf_sdio_dev
*sdiodev
, uint rw
, u32 addr
,
297 u8
*buf
, uint nbytes
)
299 addr
&= SBSDIO_SB_OFT_ADDR_MASK
;
300 addr
|= SBSDIO_SB_ACCESS_2_4B_FLAG
;
302 return brcmf_sdioh_request_buffer(sdiodev
, SDIOH_DATA_INC
,
303 (rw
? SDIOH_WRITE
: SDIOH_READ
), SDIO_FUNC_1
,
304 addr
, 4, nbytes
, buf
, NULL
);
307 int brcmf_sdcard_abort(struct brcmf_sdio_dev
*sdiodev
, uint fn
)
309 char t_func
= (char)fn
;
310 brcmf_dbg(TRACE
, "Enter\n");
312 /* issue abort cmd52 command through F0 */
313 brcmf_sdioh_request_byte(sdiodev
, SDIOH_WRITE
, SDIO_FUNC_0
,
314 SDIO_CCCR_ABORT
, &t_func
);
316 brcmf_dbg(TRACE
, "Exit\n");
320 int brcmf_sdio_probe(struct brcmf_sdio_dev
*sdiodev
)
325 ret
= brcmf_sdioh_attach(sdiodev
);
331 /* Report the BAR, to fix if needed */
332 sdiodev
->sbwad
= SI_ENUM_BASE
;
334 /* try to attach to the target device */
335 sdiodev
->bus
= brcmf_sdbrcm_probe(0, 0, 0, 0, regs
, sdiodev
);
337 brcmf_dbg(ERROR
, "device attach failed\n");
344 brcmf_sdio_remove(sdiodev
);
348 EXPORT_SYMBOL(brcmf_sdio_probe
);
350 int brcmf_sdio_remove(struct brcmf_sdio_dev
*sdiodev
)
353 brcmf_sdbrcm_disconnect(sdiodev
->bus
);
357 brcmf_sdioh_detach(sdiodev
);
363 EXPORT_SYMBOL(brcmf_sdio_remove
);
365 int brcmf_sdio_register(void)
367 return brcmf_sdio_function_init();
370 void brcmf_sdio_unregister(void)
372 brcmf_sdio_function_cleanup();
375 void brcmf_sdio_wdtmr_enable(struct brcmf_sdio_dev
*sdiodev
, bool enable
)
378 brcmf_sdbrcm_wd_timer(sdiodev
->bus
, BRCMF_WD_POLL_MS
);
380 brcmf_sdbrcm_wd_timer(sdiodev
->bus
, 0);