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/export.h>
21 #include <linux/pci.h>
22 #include <linux/pci_ids.h>
23 #include <linux/sched.h>
24 #include <linux/completion.h>
25 #include <linux/mmc/sdio.h>
26 #include <linux/mmc/sdio_func.h>
27 #include <linux/mmc/card.h>
30 #include <brcm_hw_ids.h>
31 #include <brcmu_utils.h>
32 #include <brcmu_wifi.h>
37 #include "sdio_host.h"
39 #define SDIOH_API_ACCESS_RETRY_LIMIT 2
41 static void brcmf_sdioh_irqhandler(struct sdio_func
*func
)
43 struct brcmf_sdio_dev
*sdiodev
= dev_get_drvdata(&func
->card
->dev
);
45 brcmf_dbg(TRACE
, "***IRQHandler\n");
47 sdio_release_host(func
);
49 brcmf_sdbrcm_isr(sdiodev
->bus
);
51 sdio_claim_host(func
);
54 int brcmf_sdcard_intr_reg(struct brcmf_sdio_dev
*sdiodev
)
56 brcmf_dbg(TRACE
, "Entering\n");
58 sdio_claim_host(sdiodev
->func
[1]);
59 sdio_claim_irq(sdiodev
->func
[1], brcmf_sdioh_irqhandler
);
60 sdio_release_host(sdiodev
->func
[1]);
65 int brcmf_sdcard_intr_dereg(struct brcmf_sdio_dev
*sdiodev
)
67 brcmf_dbg(TRACE
, "Entering\n");
69 sdio_claim_host(sdiodev
->func
[1]);
70 sdio_release_irq(sdiodev
->func
[1]);
71 sdio_release_host(sdiodev
->func
[1]);
76 u8
brcmf_sdcard_cfg_read(struct brcmf_sdio_dev
*sdiodev
, uint fnc_num
, u32 addr
,
84 if (retry
) /* wait for 1 ms till bus get settled down */
86 status
= brcmf_sdioh_request_byte(sdiodev
, SDIOH_READ
, fnc_num
,
89 && (retry
++ < SDIOH_API_ACCESS_RETRY_LIMIT
));
93 brcmf_dbg(INFO
, "fun = %d, addr = 0x%x, u8data = 0x%x\n",
100 brcmf_sdcard_cfg_write(struct brcmf_sdio_dev
*sdiodev
, uint fnc_num
, u32 addr
,
107 if (retry
) /* wait for 1 ms till bus get settled down */
109 status
= brcmf_sdioh_request_byte(sdiodev
, SDIOH_WRITE
, fnc_num
,
112 && (retry
++ < SDIOH_API_ACCESS_RETRY_LIMIT
));
116 brcmf_dbg(INFO
, "fun = %d, addr = 0x%x, u8data = 0x%x\n",
117 fnc_num
, addr
, data
);
121 brcmf_sdcard_set_sbaddr_window(struct brcmf_sdio_dev
*sdiodev
, u32 address
)
124 brcmf_sdcard_cfg_write(sdiodev
, SDIO_FUNC_1
, SBSDIO_FUNC1_SBADDRLOW
,
125 (address
>> 8) & SBSDIO_SBADDRLOW_MASK
, &err
);
127 brcmf_sdcard_cfg_write(sdiodev
, SDIO_FUNC_1
,
128 SBSDIO_FUNC1_SBADDRMID
,
129 (address
>> 16) & SBSDIO_SBADDRMID_MASK
,
132 brcmf_sdcard_cfg_write(sdiodev
, SDIO_FUNC_1
,
133 SBSDIO_FUNC1_SBADDRHIGH
,
134 (address
>> 24) & SBSDIO_SBADDRHIGH_MASK
,
140 u32
brcmf_sdcard_reg_read(struct brcmf_sdio_dev
*sdiodev
, u32 addr
, uint size
)
144 uint bar0
= addr
& ~SBSDIO_SB_OFT_ADDR_MASK
;
146 brcmf_dbg(INFO
, "fun = 1, addr = 0x%x\n", addr
);
148 if (bar0
!= sdiodev
->sbwad
) {
149 if (brcmf_sdcard_set_sbaddr_window(sdiodev
, bar0
))
152 sdiodev
->sbwad
= bar0
;
155 addr
&= SBSDIO_SB_OFT_ADDR_MASK
;
157 addr
|= SBSDIO_SB_ACCESS_2_4B_FLAG
;
159 status
= brcmf_sdioh_request_word(sdiodev
, SDIOH_READ
, SDIO_FUNC_1
,
162 sdiodev
->regfail
= (status
!= 0);
164 brcmf_dbg(INFO
, "u32data = 0x%x\n", word
);
166 /* if ok, return appropriately masked word */
172 return word
& 0xffff;
176 sdiodev
->regfail
= true;
181 /* otherwise, bad sdio access or invalid size */
182 brcmf_dbg(ERROR
, "error reading addr 0x%04x size %d\n", addr
, size
);
186 u32
brcmf_sdcard_reg_write(struct brcmf_sdio_dev
*sdiodev
, u32 addr
, uint size
,
190 uint bar0
= addr
& ~SBSDIO_SB_OFT_ADDR_MASK
;
193 brcmf_dbg(INFO
, "fun = 1, addr = 0x%x, uint%ddata = 0x%x\n",
194 addr
, size
* 8, data
);
196 if (bar0
!= sdiodev
->sbwad
) {
197 err
= brcmf_sdcard_set_sbaddr_window(sdiodev
, bar0
);
201 sdiodev
->sbwad
= bar0
;
204 addr
&= SBSDIO_SB_OFT_ADDR_MASK
;
206 addr
|= SBSDIO_SB_ACCESS_2_4B_FLAG
;
208 brcmf_sdioh_request_word(sdiodev
, SDIOH_WRITE
, SDIO_FUNC_1
,
210 sdiodev
->regfail
= (status
!= 0);
215 brcmf_dbg(ERROR
, "error writing 0x%08x to addr 0x%04x size %d\n",
220 bool brcmf_sdcard_regfail(struct brcmf_sdio_dev
*sdiodev
)
222 return sdiodev
->regfail
;
225 static int brcmf_sdcard_recv_prepare(struct brcmf_sdio_dev
*sdiodev
, uint fn
,
226 uint flags
, uint width
, u32
*addr
)
228 uint bar0
= *addr
& ~SBSDIO_SB_OFT_ADDR_MASK
;
231 /* Async not implemented yet */
232 if (flags
& SDIO_REQ_ASYNC
)
235 if (bar0
!= sdiodev
->sbwad
) {
236 err
= brcmf_sdcard_set_sbaddr_window(sdiodev
, bar0
);
240 sdiodev
->sbwad
= bar0
;
243 *addr
&= SBSDIO_SB_OFT_ADDR_MASK
;
246 *addr
|= SBSDIO_SB_ACCESS_2_4B_FLAG
;
252 brcmf_sdcard_recv_buf(struct brcmf_sdio_dev
*sdiodev
, u32 addr
, uint fn
,
253 uint flags
, u8
*buf
, uint nbytes
)
255 struct sk_buff
*mypkt
;
258 mypkt
= brcmu_pkt_buf_get_skb(nbytes
);
260 brcmf_dbg(ERROR
, "brcmu_pkt_buf_get_skb failed: len %d\n",
265 err
= brcmf_sdcard_recv_pkt(sdiodev
, addr
, fn
, flags
, mypkt
);
267 memcpy(buf
, mypkt
->data
, nbytes
);
269 brcmu_pkt_buf_free_skb(mypkt
);
274 brcmf_sdcard_recv_pkt(struct brcmf_sdio_dev
*sdiodev
, u32 addr
, uint fn
,
275 uint flags
, struct sk_buff
*pkt
)
281 brcmf_dbg(INFO
, "fun = %d, addr = 0x%x, size = %d\n",
284 width
= (flags
& SDIO_REQ_4BYTE
) ? 4 : 2;
285 err
= brcmf_sdcard_recv_prepare(sdiodev
, fn
, flags
, width
, &addr
);
289 incr_fix
= (flags
& SDIO_REQ_FIXED
) ? SDIOH_DATA_FIX
: SDIOH_DATA_INC
;
290 err
= brcmf_sdioh_request_buffer(sdiodev
, incr_fix
, SDIOH_READ
,
296 int brcmf_sdcard_recv_chain(struct brcmf_sdio_dev
*sdiodev
, u32 addr
, uint fn
,
297 uint flags
, struct sk_buff_head
*pktq
)
303 brcmf_dbg(INFO
, "fun = %d, addr = 0x%x, size = %d\n",
304 fn
, addr
, pktq
->qlen
);
306 width
= (flags
& SDIO_REQ_4BYTE
) ? 4 : 2;
307 err
= brcmf_sdcard_recv_prepare(sdiodev
, fn
, flags
, width
, &addr
);
311 incr_fix
= (flags
& SDIO_REQ_FIXED
) ? SDIOH_DATA_FIX
: SDIOH_DATA_INC
;
312 err
= brcmf_sdioh_request_chain(sdiodev
, incr_fix
, SDIOH_READ
, fn
, addr
,
319 brcmf_sdcard_send_buf(struct brcmf_sdio_dev
*sdiodev
, u32 addr
, uint fn
,
320 uint flags
, u8
*buf
, uint nbytes
)
322 struct sk_buff
*mypkt
;
325 mypkt
= brcmu_pkt_buf_get_skb(nbytes
);
327 brcmf_dbg(ERROR
, "brcmu_pkt_buf_get_skb failed: len %d\n",
332 memcpy(mypkt
->data
, buf
, nbytes
);
333 err
= brcmf_sdcard_send_pkt(sdiodev
, addr
, fn
, flags
, mypkt
);
335 brcmu_pkt_buf_free_skb(mypkt
);
341 brcmf_sdcard_send_pkt(struct brcmf_sdio_dev
*sdiodev
, u32 addr
, uint fn
,
342 uint flags
, struct sk_buff
*pkt
)
346 uint bar0
= addr
& ~SBSDIO_SB_OFT_ADDR_MASK
;
349 brcmf_dbg(INFO
, "fun = %d, addr = 0x%x, size = %d\n",
352 /* Async not implemented yet */
353 if (flags
& SDIO_REQ_ASYNC
)
356 if (bar0
!= sdiodev
->sbwad
) {
357 err
= brcmf_sdcard_set_sbaddr_window(sdiodev
, bar0
);
361 sdiodev
->sbwad
= bar0
;
364 addr
&= SBSDIO_SB_OFT_ADDR_MASK
;
366 incr_fix
= (flags
& SDIO_REQ_FIXED
) ? SDIOH_DATA_FIX
: SDIOH_DATA_INC
;
367 width
= (flags
& SDIO_REQ_4BYTE
) ? 4 : 2;
369 addr
|= SBSDIO_SB_ACCESS_2_4B_FLAG
;
371 return brcmf_sdioh_request_buffer(sdiodev
, incr_fix
, SDIOH_WRITE
, fn
,
375 int brcmf_sdcard_rwdata(struct brcmf_sdio_dev
*sdiodev
, uint rw
, u32 addr
,
376 u8
*buf
, uint nbytes
)
378 struct sk_buff
*mypkt
;
379 bool write
= rw
? SDIOH_WRITE
: SDIOH_READ
;
382 addr
&= SBSDIO_SB_OFT_ADDR_MASK
;
383 addr
|= SBSDIO_SB_ACCESS_2_4B_FLAG
;
385 mypkt
= brcmu_pkt_buf_get_skb(nbytes
);
387 brcmf_dbg(ERROR
, "brcmu_pkt_buf_get_skb failed: len %d\n",
392 /* For a write, copy the buffer data into the packet. */
394 memcpy(mypkt
->data
, buf
, nbytes
);
396 err
= brcmf_sdioh_request_buffer(sdiodev
, SDIOH_DATA_INC
, write
,
397 SDIO_FUNC_1
, addr
, mypkt
);
399 /* For a read, copy the packet data back to the buffer. */
401 memcpy(buf
, mypkt
->data
, nbytes
);
403 brcmu_pkt_buf_free_skb(mypkt
);
407 int brcmf_sdcard_abort(struct brcmf_sdio_dev
*sdiodev
, uint fn
)
409 char t_func
= (char)fn
;
410 brcmf_dbg(TRACE
, "Enter\n");
412 /* issue abort cmd52 command through F0 */
413 brcmf_sdioh_request_byte(sdiodev
, SDIOH_WRITE
, SDIO_FUNC_0
,
414 SDIO_CCCR_ABORT
, &t_func
);
416 brcmf_dbg(TRACE
, "Exit\n");
420 int brcmf_sdio_probe(struct brcmf_sdio_dev
*sdiodev
)
425 ret
= brcmf_sdioh_attach(sdiodev
);
431 /* Report the BAR, to fix if needed */
432 sdiodev
->sbwad
= SI_ENUM_BASE
;
434 /* try to attach to the target device */
435 sdiodev
->bus
= brcmf_sdbrcm_probe(0, 0, 0, 0, regs
, sdiodev
);
437 brcmf_dbg(ERROR
, "device attach failed\n");
444 brcmf_sdio_remove(sdiodev
);
448 EXPORT_SYMBOL(brcmf_sdio_probe
);
450 int brcmf_sdio_remove(struct brcmf_sdio_dev
*sdiodev
)
453 brcmf_sdbrcm_disconnect(sdiodev
->bus
);
457 brcmf_sdioh_detach(sdiodev
);
463 EXPORT_SYMBOL(brcmf_sdio_remove
);
465 void brcmf_sdio_wdtmr_enable(struct brcmf_sdio_dev
*sdiodev
, bool enable
)
468 brcmf_sdbrcm_wd_timer(sdiodev
->bus
, BRCMF_WD_POLL_MS
);
470 brcmf_sdbrcm_wd_timer(sdiodev
->bus
, 0);