Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
Joe Perches | 02f7719 | 2012-01-15 00:38:44 -0800 | [diff] [blame] | 18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 19 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 20 | #include <linux/types.h> |
| 21 | #include <linux/netdevice.h> |
Paul Gortmaker | ee40fa0 | 2011-05-27 16:14:23 -0400 | [diff] [blame] | 22 | #include <linux/export.h> |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 23 | #include <linux/pci.h> |
| 24 | #include <linux/pci_ids.h> |
| 25 | #include <linux/sched.h> |
| 26 | #include <linux/completion.h> |
| 27 | #include <linux/mmc/sdio.h> |
| 28 | #include <linux/mmc/sdio_func.h> |
| 29 | #include <linux/mmc/card.h> |
| 30 | |
| 31 | #include <defs.h> |
| 32 | #include <brcm_hw_ids.h> |
| 33 | #include <brcmu_utils.h> |
| 34 | #include <brcmu_wifi.h> |
| 35 | #include <soc.h> |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 36 | #include "dhd_bus.h" |
| 37 | #include "dhd_dbg.h" |
| 38 | #include "sdio_host.h" |
| 39 | |
| 40 | #define SDIOH_API_ACCESS_RETRY_LIMIT 2 |
| 41 | |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 42 | #ifdef CONFIG_BRCMFMAC_SDIO_OOB |
| 43 | static irqreturn_t brcmf_sdio_irqhandler(int irq, void *dev_id) |
| 44 | { |
| 45 | struct brcmf_sdio_dev *sdiodev = dev_get_drvdata(dev_id); |
| 46 | |
| 47 | brcmf_dbg(INTR, "oob intr triggered\n"); |
| 48 | |
| 49 | /* |
| 50 | * out-of-band interrupt is level-triggered which won't |
| 51 | * be cleared until dpc |
| 52 | */ |
| 53 | if (sdiodev->irq_en) { |
| 54 | disable_irq_nosync(irq); |
| 55 | sdiodev->irq_en = false; |
| 56 | } |
| 57 | |
| 58 | brcmf_sdbrcm_isr(sdiodev->bus); |
| 59 | |
| 60 | return IRQ_HANDLED; |
| 61 | } |
| 62 | |
| 63 | int brcmf_sdio_intr_register(struct brcmf_sdio_dev *sdiodev) |
| 64 | { |
| 65 | int ret = 0; |
| 66 | u8 data; |
| 67 | unsigned long flags; |
| 68 | |
| 69 | brcmf_dbg(TRACE, "Entering\n"); |
| 70 | |
| 71 | brcmf_dbg(ERROR, "requesting irq %d\n", sdiodev->irq); |
| 72 | ret = request_irq(sdiodev->irq, brcmf_sdio_irqhandler, |
| 73 | sdiodev->irq_flags, "brcmf_oob_intr", |
| 74 | &sdiodev->func[1]->card->dev); |
| 75 | if (ret != 0) |
| 76 | return ret; |
| 77 | spin_lock_init(&sdiodev->irq_en_lock); |
| 78 | spin_lock_irqsave(&sdiodev->irq_en_lock, flags); |
| 79 | sdiodev->irq_en = true; |
| 80 | spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags); |
| 81 | |
| 82 | ret = enable_irq_wake(sdiodev->irq); |
| 83 | if (ret != 0) |
| 84 | return ret; |
| 85 | sdiodev->irq_wake = true; |
| 86 | |
| 87 | /* must configure SDIO_CCCR_IENx to enable irq */ |
| 88 | data = brcmf_sdcard_cfg_read(sdiodev, SDIO_FUNC_0, |
| 89 | SDIO_CCCR_IENx, &ret); |
| 90 | data |= 1 << SDIO_FUNC_1 | 1 << SDIO_FUNC_2 | 1; |
| 91 | brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_0, SDIO_CCCR_IENx, |
| 92 | data, &ret); |
| 93 | |
| 94 | /* redirect, configure ane enable io for interrupt signal */ |
| 95 | data = SDIO_SEPINT_MASK | SDIO_SEPINT_OE; |
| 96 | if (sdiodev->irq_flags | IRQF_TRIGGER_HIGH) |
| 97 | data |= SDIO_SEPINT_ACT_HI; |
| 98 | brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_0, SDIO_CCCR_BRCM_SEPINT, |
| 99 | data, &ret); |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | int brcmf_sdio_intr_unregister(struct brcmf_sdio_dev *sdiodev) |
| 105 | { |
| 106 | brcmf_dbg(TRACE, "Entering\n"); |
| 107 | |
| 108 | brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_0, SDIO_CCCR_BRCM_SEPINT, |
| 109 | 0, NULL); |
| 110 | brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_0, SDIO_CCCR_IENx, 0, NULL); |
| 111 | |
| 112 | if (sdiodev->irq_wake) { |
| 113 | disable_irq_wake(sdiodev->irq); |
| 114 | sdiodev->irq_wake = false; |
| 115 | } |
| 116 | free_irq(sdiodev->irq, &sdiodev->func[1]->card->dev); |
| 117 | sdiodev->irq_en = false; |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | #else /* CONFIG_BRCMFMAC_SDIO_OOB */ |
| 122 | static void brcmf_sdio_irqhandler(struct sdio_func *func) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 123 | { |
Franky Lin | d76d1c8 | 2011-12-08 15:06:37 -0800 | [diff] [blame] | 124 | struct brcmf_sdio_dev *sdiodev = dev_get_drvdata(&func->card->dev); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 125 | |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 126 | brcmf_dbg(INTR, "ib intr triggered\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 127 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 128 | brcmf_sdbrcm_isr(sdiodev->bus); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Franky Lin | fbf5910 | 2011-12-16 18:36:53 -0800 | [diff] [blame] | 131 | /* dummy handler for SDIO function 2 interrupt */ |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 132 | static void brcmf_sdio_dummy_irqhandler(struct sdio_func *func) |
Franky Lin | fbf5910 | 2011-12-16 18:36:53 -0800 | [diff] [blame] | 133 | { |
| 134 | } |
| 135 | |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 136 | int brcmf_sdio_intr_register(struct brcmf_sdio_dev *sdiodev) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 137 | { |
| 138 | brcmf_dbg(TRACE, "Entering\n"); |
| 139 | |
| 140 | sdio_claim_host(sdiodev->func[1]); |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 141 | sdio_claim_irq(sdiodev->func[1], brcmf_sdio_irqhandler); |
| 142 | sdio_claim_irq(sdiodev->func[2], brcmf_sdio_dummy_irqhandler); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 143 | sdio_release_host(sdiodev->func[1]); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 148 | int brcmf_sdio_intr_unregister(struct brcmf_sdio_dev *sdiodev) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 149 | { |
| 150 | brcmf_dbg(TRACE, "Entering\n"); |
| 151 | |
| 152 | sdio_claim_host(sdiodev->func[1]); |
Franky Lin | fbf5910 | 2011-12-16 18:36:53 -0800 | [diff] [blame] | 153 | sdio_release_irq(sdiodev->func[2]); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 154 | sdio_release_irq(sdiodev->func[1]); |
| 155 | sdio_release_host(sdiodev->func[1]); |
| 156 | |
| 157 | return 0; |
| 158 | } |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 159 | #endif /* CONFIG_BRCMFMAC_SDIO_OOB */ |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 160 | |
| 161 | u8 brcmf_sdcard_cfg_read(struct brcmf_sdio_dev *sdiodev, uint fnc_num, u32 addr, |
| 162 | int *err) |
| 163 | { |
| 164 | int status; |
| 165 | s32 retry = 0; |
| 166 | u8 data = 0; |
| 167 | |
| 168 | do { |
| 169 | if (retry) /* wait for 1 ms till bus get settled down */ |
| 170 | udelay(1000); |
| 171 | status = brcmf_sdioh_request_byte(sdiodev, SDIOH_READ, fnc_num, |
| 172 | addr, (u8 *) &data); |
| 173 | } while (status != 0 |
| 174 | && (retry++ < SDIOH_API_ACCESS_RETRY_LIMIT)); |
| 175 | if (err) |
| 176 | *err = status; |
| 177 | |
| 178 | brcmf_dbg(INFO, "fun = %d, addr = 0x%x, u8data = 0x%x\n", |
| 179 | fnc_num, addr, data); |
| 180 | |
| 181 | return data; |
| 182 | } |
| 183 | |
| 184 | void |
| 185 | brcmf_sdcard_cfg_write(struct brcmf_sdio_dev *sdiodev, uint fnc_num, u32 addr, |
| 186 | u8 data, int *err) |
| 187 | { |
| 188 | int status; |
| 189 | s32 retry = 0; |
| 190 | |
| 191 | do { |
| 192 | if (retry) /* wait for 1 ms till bus get settled down */ |
| 193 | udelay(1000); |
| 194 | status = brcmf_sdioh_request_byte(sdiodev, SDIOH_WRITE, fnc_num, |
| 195 | addr, (u8 *) &data); |
| 196 | } while (status != 0 |
| 197 | && (retry++ < SDIOH_API_ACCESS_RETRY_LIMIT)); |
| 198 | if (err) |
| 199 | *err = status; |
| 200 | |
| 201 | brcmf_dbg(INFO, "fun = %d, addr = 0x%x, u8data = 0x%x\n", |
| 202 | fnc_num, addr, data); |
| 203 | } |
| 204 | |
| 205 | int |
| 206 | brcmf_sdcard_set_sbaddr_window(struct brcmf_sdio_dev *sdiodev, u32 address) |
| 207 | { |
| 208 | int err = 0; |
| 209 | brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW, |
| 210 | (address >> 8) & SBSDIO_SBADDRLOW_MASK, &err); |
| 211 | if (!err) |
| 212 | brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_1, |
| 213 | SBSDIO_FUNC1_SBADDRMID, |
| 214 | (address >> 16) & SBSDIO_SBADDRMID_MASK, |
| 215 | &err); |
| 216 | if (!err) |
| 217 | brcmf_sdcard_cfg_write(sdiodev, SDIO_FUNC_1, |
| 218 | SBSDIO_FUNC1_SBADDRHIGH, |
| 219 | (address >> 24) & SBSDIO_SBADDRHIGH_MASK, |
| 220 | &err); |
| 221 | |
| 222 | return err; |
| 223 | } |
| 224 | |
| 225 | u32 brcmf_sdcard_reg_read(struct brcmf_sdio_dev *sdiodev, u32 addr, uint size) |
| 226 | { |
| 227 | int status; |
| 228 | u32 word = 0; |
| 229 | uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK; |
| 230 | |
| 231 | brcmf_dbg(INFO, "fun = 1, addr = 0x%x\n", addr); |
| 232 | |
| 233 | if (bar0 != sdiodev->sbwad) { |
| 234 | if (brcmf_sdcard_set_sbaddr_window(sdiodev, bar0)) |
| 235 | return 0xFFFFFFFF; |
| 236 | |
| 237 | sdiodev->sbwad = bar0; |
| 238 | } |
| 239 | |
| 240 | addr &= SBSDIO_SB_OFT_ADDR_MASK; |
| 241 | if (size == 4) |
| 242 | addr |= SBSDIO_SB_ACCESS_2_4B_FLAG; |
| 243 | |
| 244 | status = brcmf_sdioh_request_word(sdiodev, SDIOH_READ, SDIO_FUNC_1, |
| 245 | addr, &word, size); |
| 246 | |
| 247 | sdiodev->regfail = (status != 0); |
| 248 | |
| 249 | brcmf_dbg(INFO, "u32data = 0x%x\n", word); |
| 250 | |
| 251 | /* if ok, return appropriately masked word */ |
| 252 | if (status == 0) { |
| 253 | switch (size) { |
| 254 | case sizeof(u8): |
| 255 | return word & 0xff; |
| 256 | case sizeof(u16): |
| 257 | return word & 0xffff; |
| 258 | case sizeof(u32): |
| 259 | return word; |
| 260 | default: |
| 261 | sdiodev->regfail = true; |
| 262 | |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /* otherwise, bad sdio access or invalid size */ |
| 267 | brcmf_dbg(ERROR, "error reading addr 0x%04x size %d\n", addr, size); |
| 268 | return 0xFFFFFFFF; |
| 269 | } |
| 270 | |
| 271 | u32 brcmf_sdcard_reg_write(struct brcmf_sdio_dev *sdiodev, u32 addr, uint size, |
| 272 | u32 data) |
| 273 | { |
| 274 | int status; |
| 275 | uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK; |
| 276 | int err = 0; |
| 277 | |
| 278 | brcmf_dbg(INFO, "fun = 1, addr = 0x%x, uint%ddata = 0x%x\n", |
| 279 | addr, size * 8, data); |
| 280 | |
| 281 | if (bar0 != sdiodev->sbwad) { |
| 282 | err = brcmf_sdcard_set_sbaddr_window(sdiodev, bar0); |
| 283 | if (err) |
| 284 | return err; |
| 285 | |
| 286 | sdiodev->sbwad = bar0; |
| 287 | } |
| 288 | |
| 289 | addr &= SBSDIO_SB_OFT_ADDR_MASK; |
| 290 | if (size == 4) |
| 291 | addr |= SBSDIO_SB_ACCESS_2_4B_FLAG; |
| 292 | status = |
| 293 | brcmf_sdioh_request_word(sdiodev, SDIOH_WRITE, SDIO_FUNC_1, |
| 294 | addr, &data, size); |
| 295 | sdiodev->regfail = (status != 0); |
| 296 | |
| 297 | if (status == 0) |
| 298 | return 0; |
| 299 | |
| 300 | brcmf_dbg(ERROR, "error writing 0x%08x to addr 0x%04x size %d\n", |
| 301 | data, addr, size); |
| 302 | return 0xFFFFFFFF; |
| 303 | } |
| 304 | |
| 305 | bool brcmf_sdcard_regfail(struct brcmf_sdio_dev *sdiodev) |
| 306 | { |
| 307 | return sdiodev->regfail; |
| 308 | } |
| 309 | |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 310 | static int brcmf_sdcard_recv_prepare(struct brcmf_sdio_dev *sdiodev, uint fn, |
| 311 | uint flags, uint width, u32 *addr) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 312 | { |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 313 | uint bar0 = *addr & ~SBSDIO_SB_OFT_ADDR_MASK; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 314 | int err = 0; |
| 315 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 316 | /* Async not implemented yet */ |
| 317 | if (flags & SDIO_REQ_ASYNC) |
| 318 | return -ENOTSUPP; |
| 319 | |
| 320 | if (bar0 != sdiodev->sbwad) { |
| 321 | err = brcmf_sdcard_set_sbaddr_window(sdiodev, bar0); |
| 322 | if (err) |
| 323 | return err; |
| 324 | |
| 325 | sdiodev->sbwad = bar0; |
| 326 | } |
| 327 | |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 328 | *addr &= SBSDIO_SB_OFT_ADDR_MASK; |
| 329 | |
| 330 | if (width == 4) |
| 331 | *addr |= SBSDIO_SB_ACCESS_2_4B_FLAG; |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | int |
| 337 | brcmf_sdcard_recv_buf(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn, |
| 338 | uint flags, u8 *buf, uint nbytes) |
| 339 | { |
| 340 | struct sk_buff *mypkt; |
| 341 | int err; |
| 342 | |
| 343 | mypkt = brcmu_pkt_buf_get_skb(nbytes); |
| 344 | if (!mypkt) { |
| 345 | brcmf_dbg(ERROR, "brcmu_pkt_buf_get_skb failed: len %d\n", |
| 346 | nbytes); |
| 347 | return -EIO; |
| 348 | } |
| 349 | |
| 350 | err = brcmf_sdcard_recv_pkt(sdiodev, addr, fn, flags, mypkt); |
| 351 | if (!err) |
| 352 | memcpy(buf, mypkt->data, nbytes); |
| 353 | |
| 354 | brcmu_pkt_buf_free_skb(mypkt); |
| 355 | return err; |
| 356 | } |
| 357 | |
| 358 | int |
| 359 | brcmf_sdcard_recv_pkt(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn, |
| 360 | uint flags, struct sk_buff *pkt) |
| 361 | { |
| 362 | uint incr_fix; |
| 363 | uint width; |
| 364 | int err = 0; |
| 365 | |
| 366 | brcmf_dbg(INFO, "fun = %d, addr = 0x%x, size = %d\n", |
| 367 | fn, addr, pkt->len); |
| 368 | |
| 369 | width = (flags & SDIO_REQ_4BYTE) ? 4 : 2; |
| 370 | err = brcmf_sdcard_recv_prepare(sdiodev, fn, flags, width, &addr); |
| 371 | if (err) |
| 372 | return err; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 373 | |
| 374 | incr_fix = (flags & SDIO_REQ_FIXED) ? SDIOH_DATA_FIX : SDIOH_DATA_INC; |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 375 | err = brcmf_sdioh_request_buffer(sdiodev, incr_fix, SDIOH_READ, |
Arend van Spriel | 4c6e869 | 2011-11-22 17:21:40 -0800 | [diff] [blame] | 376 | fn, addr, pkt); |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 377 | |
| 378 | return err; |
| 379 | } |
| 380 | |
| 381 | int brcmf_sdcard_recv_chain(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn, |
| 382 | uint flags, struct sk_buff_head *pktq) |
| 383 | { |
| 384 | uint incr_fix; |
| 385 | uint width; |
| 386 | int err = 0; |
| 387 | |
| 388 | brcmf_dbg(INFO, "fun = %d, addr = 0x%x, size = %d\n", |
| 389 | fn, addr, pktq->qlen); |
| 390 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 391 | width = (flags & SDIO_REQ_4BYTE) ? 4 : 2; |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 392 | err = brcmf_sdcard_recv_prepare(sdiodev, fn, flags, width, &addr); |
| 393 | if (err) |
| 394 | return err; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 395 | |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 396 | incr_fix = (flags & SDIO_REQ_FIXED) ? SDIOH_DATA_FIX : SDIOH_DATA_INC; |
| 397 | err = brcmf_sdioh_request_chain(sdiodev, incr_fix, SDIOH_READ, fn, addr, |
| 398 | pktq); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 399 | |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 400 | return err; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | int |
| 404 | brcmf_sdcard_send_buf(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn, |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 405 | uint flags, u8 *buf, uint nbytes) |
| 406 | { |
| 407 | struct sk_buff *mypkt; |
| 408 | int err; |
| 409 | |
| 410 | mypkt = brcmu_pkt_buf_get_skb(nbytes); |
| 411 | if (!mypkt) { |
| 412 | brcmf_dbg(ERROR, "brcmu_pkt_buf_get_skb failed: len %d\n", |
| 413 | nbytes); |
| 414 | return -EIO; |
| 415 | } |
| 416 | |
| 417 | memcpy(mypkt->data, buf, nbytes); |
| 418 | err = brcmf_sdcard_send_pkt(sdiodev, addr, fn, flags, mypkt); |
| 419 | |
| 420 | brcmu_pkt_buf_free_skb(mypkt); |
| 421 | return err; |
| 422 | |
| 423 | } |
| 424 | |
| 425 | int |
| 426 | brcmf_sdcard_send_pkt(struct brcmf_sdio_dev *sdiodev, u32 addr, uint fn, |
| 427 | uint flags, struct sk_buff *pkt) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 428 | { |
| 429 | uint incr_fix; |
| 430 | uint width; |
| 431 | uint bar0 = addr & ~SBSDIO_SB_OFT_ADDR_MASK; |
| 432 | int err = 0; |
| 433 | |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 434 | brcmf_dbg(INFO, "fun = %d, addr = 0x%x, size = %d\n", |
| 435 | fn, addr, pkt->len); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 436 | |
| 437 | /* Async not implemented yet */ |
| 438 | if (flags & SDIO_REQ_ASYNC) |
| 439 | return -ENOTSUPP; |
| 440 | |
| 441 | if (bar0 != sdiodev->sbwad) { |
| 442 | err = brcmf_sdcard_set_sbaddr_window(sdiodev, bar0); |
| 443 | if (err) |
| 444 | return err; |
| 445 | |
| 446 | sdiodev->sbwad = bar0; |
| 447 | } |
| 448 | |
| 449 | addr &= SBSDIO_SB_OFT_ADDR_MASK; |
| 450 | |
| 451 | incr_fix = (flags & SDIO_REQ_FIXED) ? SDIOH_DATA_FIX : SDIOH_DATA_INC; |
| 452 | width = (flags & SDIO_REQ_4BYTE) ? 4 : 2; |
| 453 | if (width == 4) |
| 454 | addr |= SBSDIO_SB_ACCESS_2_4B_FLAG; |
| 455 | |
| 456 | return brcmf_sdioh_request_buffer(sdiodev, incr_fix, SDIOH_WRITE, fn, |
Arend van Spriel | 4c6e869 | 2011-11-22 17:21:40 -0800 | [diff] [blame] | 457 | addr, pkt); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | int brcmf_sdcard_rwdata(struct brcmf_sdio_dev *sdiodev, uint rw, u32 addr, |
| 461 | u8 *buf, uint nbytes) |
| 462 | { |
Arend van Spriel | 4c6e869 | 2011-11-22 17:21:40 -0800 | [diff] [blame] | 463 | struct sk_buff *mypkt; |
| 464 | bool write = rw ? SDIOH_WRITE : SDIOH_READ; |
| 465 | int err; |
| 466 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 467 | addr &= SBSDIO_SB_OFT_ADDR_MASK; |
| 468 | addr |= SBSDIO_SB_ACCESS_2_4B_FLAG; |
| 469 | |
Arend van Spriel | 4c6e869 | 2011-11-22 17:21:40 -0800 | [diff] [blame] | 470 | mypkt = brcmu_pkt_buf_get_skb(nbytes); |
| 471 | if (!mypkt) { |
| 472 | brcmf_dbg(ERROR, "brcmu_pkt_buf_get_skb failed: len %d\n", |
| 473 | nbytes); |
| 474 | return -EIO; |
| 475 | } |
| 476 | |
| 477 | /* For a write, copy the buffer data into the packet. */ |
| 478 | if (write) |
| 479 | memcpy(mypkt->data, buf, nbytes); |
| 480 | |
| 481 | err = brcmf_sdioh_request_buffer(sdiodev, SDIOH_DATA_INC, write, |
| 482 | SDIO_FUNC_1, addr, mypkt); |
| 483 | |
| 484 | /* For a read, copy the packet data back to the buffer. */ |
| 485 | if (!err && !write) |
| 486 | memcpy(buf, mypkt->data, nbytes); |
| 487 | |
| 488 | brcmu_pkt_buf_free_skb(mypkt); |
| 489 | return err; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | int brcmf_sdcard_abort(struct brcmf_sdio_dev *sdiodev, uint fn) |
| 493 | { |
| 494 | char t_func = (char)fn; |
| 495 | brcmf_dbg(TRACE, "Enter\n"); |
| 496 | |
| 497 | /* issue abort cmd52 command through F0 */ |
| 498 | brcmf_sdioh_request_byte(sdiodev, SDIOH_WRITE, SDIO_FUNC_0, |
| 499 | SDIO_CCCR_ABORT, &t_func); |
| 500 | |
| 501 | brcmf_dbg(TRACE, "Exit\n"); |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | int brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev) |
| 506 | { |
| 507 | u32 regs = 0; |
| 508 | int ret = 0; |
| 509 | |
| 510 | ret = brcmf_sdioh_attach(sdiodev); |
| 511 | if (ret) |
| 512 | goto out; |
| 513 | |
| 514 | regs = SI_ENUM_BASE; |
| 515 | |
| 516 | /* Report the BAR, to fix if needed */ |
| 517 | sdiodev->sbwad = SI_ENUM_BASE; |
| 518 | |
| 519 | /* try to attach to the target device */ |
Franky Lin | 4175b88 | 2011-11-22 17:21:49 -0800 | [diff] [blame] | 520 | sdiodev->bus = brcmf_sdbrcm_probe(regs, sdiodev); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 521 | if (!sdiodev->bus) { |
| 522 | brcmf_dbg(ERROR, "device attach failed\n"); |
| 523 | ret = -ENODEV; |
| 524 | goto out; |
| 525 | } |
| 526 | |
| 527 | out: |
| 528 | if (ret) |
| 529 | brcmf_sdio_remove(sdiodev); |
| 530 | |
| 531 | return ret; |
| 532 | } |
| 533 | EXPORT_SYMBOL(brcmf_sdio_probe); |
| 534 | |
| 535 | int brcmf_sdio_remove(struct brcmf_sdio_dev *sdiodev) |
| 536 | { |
| 537 | if (sdiodev->bus) { |
| 538 | brcmf_sdbrcm_disconnect(sdiodev->bus); |
| 539 | sdiodev->bus = NULL; |
| 540 | } |
| 541 | |
| 542 | brcmf_sdioh_detach(sdiodev); |
| 543 | |
| 544 | sdiodev->sbwad = 0; |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | EXPORT_SYMBOL(brcmf_sdio_remove); |
| 549 | |
| 550 | void brcmf_sdio_wdtmr_enable(struct brcmf_sdio_dev *sdiodev, bool enable) |
| 551 | { |
| 552 | if (enable) |
| 553 | brcmf_sdbrcm_wd_timer(sdiodev->bus, BRCMF_WD_POLL_MS); |
| 554 | else |
| 555 | brcmf_sdbrcm_wd_timer(sdiodev->bus, 0); |
| 556 | } |