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 | #include <linux/types.h> |
| 17 | #include <linux/netdevice.h> |
| 18 | #include <linux/mmc/sdio.h> |
| 19 | #include <linux/mmc/core.h> |
| 20 | #include <linux/mmc/sdio_func.h> |
| 21 | #include <linux/mmc/sdio_ids.h> |
| 22 | #include <linux/mmc/card.h> |
| 23 | #include <linux/suspend.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/sched.h> /* request_irq() */ |
Stephen Rothwell | b7a57e7 | 2011-10-12 21:35:07 +0200 | [diff] [blame^] | 26 | #include <linux/module.h> |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 27 | #include <net/cfg80211.h> |
| 28 | |
| 29 | #include <defs.h> |
| 30 | #include <brcm_hw_ids.h> |
| 31 | #include <brcmu_utils.h> |
| 32 | #include <brcmu_wifi.h> |
| 33 | #include "sdio_host.h" |
| 34 | #include "dhd.h" |
| 35 | #include "dhd_dbg.h" |
| 36 | #include "wl_cfg80211.h" |
| 37 | |
| 38 | #define SDIO_VENDOR_ID_BROADCOM 0x02d0 |
| 39 | |
| 40 | #define DMA_ALIGN_MASK 0x03 |
| 41 | |
| 42 | #define SDIO_DEVICE_ID_BROADCOM_4329 0x4329 |
| 43 | |
| 44 | #define SDIO_FUNC1_BLOCKSIZE 64 |
| 45 | #define SDIO_FUNC2_BLOCKSIZE 512 |
| 46 | |
| 47 | /* devices we support, null terminated */ |
| 48 | static const struct sdio_device_id brcmf_sdmmc_ids[] = { |
| 49 | {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4329)}, |
| 50 | { /* end: all zeroes */ }, |
| 51 | }; |
| 52 | MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids); |
| 53 | |
| 54 | static bool |
| 55 | brcmf_pm_resume_error(struct brcmf_sdio_dev *sdiodev) |
| 56 | { |
| 57 | bool is_err = false; |
| 58 | #ifdef CONFIG_PM_SLEEP |
| 59 | is_err = atomic_read(&sdiodev->suspend); |
| 60 | #endif |
| 61 | return is_err; |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | brcmf_pm_resume_wait(struct brcmf_sdio_dev *sdiodev, wait_queue_head_t *wq) |
| 66 | { |
| 67 | #ifdef CONFIG_PM_SLEEP |
| 68 | int retry = 0; |
| 69 | while (atomic_read(&sdiodev->suspend) && retry++ != 30) |
| 70 | wait_event_timeout(*wq, false, HZ/100); |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | static inline int brcmf_sdioh_f0_write_byte(struct brcmf_sdio_dev *sdiodev, |
| 75 | uint regaddr, u8 *byte) |
| 76 | { |
| 77 | struct sdio_func *sdfunc = sdiodev->func[0]; |
| 78 | int err_ret; |
| 79 | |
| 80 | /* |
| 81 | * Can only directly write to some F0 registers. |
| 82 | * Handle F2 enable/disable and Abort command |
| 83 | * as a special case. |
| 84 | */ |
| 85 | if (regaddr == SDIO_CCCR_IOEx) { |
| 86 | sdfunc = sdiodev->func[2]; |
| 87 | if (sdfunc) { |
| 88 | sdio_claim_host(sdfunc); |
| 89 | if (*byte & SDIO_FUNC_ENABLE_2) { |
| 90 | /* Enable Function 2 */ |
| 91 | err_ret = sdio_enable_func(sdfunc); |
| 92 | if (err_ret) |
| 93 | brcmf_dbg(ERROR, |
| 94 | "enable F2 failed:%d\n", |
| 95 | err_ret); |
| 96 | } else { |
| 97 | /* Disable Function 2 */ |
| 98 | err_ret = sdio_disable_func(sdfunc); |
| 99 | if (err_ret) |
| 100 | brcmf_dbg(ERROR, |
| 101 | "Disable F2 failed:%d\n", |
| 102 | err_ret); |
| 103 | } |
| 104 | sdio_release_host(sdfunc); |
| 105 | } |
| 106 | } else if (regaddr == SDIO_CCCR_ABORT) { |
| 107 | sdio_claim_host(sdfunc); |
| 108 | sdio_writeb(sdfunc, *byte, regaddr, &err_ret); |
| 109 | sdio_release_host(sdfunc); |
| 110 | } else if (regaddr < 0xF0) { |
| 111 | brcmf_dbg(ERROR, "F0 Wr:0x%02x: write disallowed\n", regaddr); |
| 112 | err_ret = -EPERM; |
| 113 | } else { |
| 114 | sdio_claim_host(sdfunc); |
| 115 | sdio_f0_writeb(sdfunc, *byte, regaddr, &err_ret); |
| 116 | sdio_release_host(sdfunc); |
| 117 | } |
| 118 | |
| 119 | return err_ret; |
| 120 | } |
| 121 | |
| 122 | int brcmf_sdioh_request_byte(struct brcmf_sdio_dev *sdiodev, uint rw, uint func, |
| 123 | uint regaddr, u8 *byte) |
| 124 | { |
| 125 | int err_ret; |
| 126 | |
| 127 | brcmf_dbg(INFO, "rw=%d, func=%d, addr=0x%05x\n", rw, func, regaddr); |
| 128 | |
| 129 | brcmf_pm_resume_wait(sdiodev, &sdiodev->request_byte_wait); |
| 130 | if (brcmf_pm_resume_error(sdiodev)) |
| 131 | return -EIO; |
| 132 | |
| 133 | if (rw && func == 0) { |
| 134 | /* handle F0 separately */ |
| 135 | err_ret = brcmf_sdioh_f0_write_byte(sdiodev, regaddr, byte); |
| 136 | } else { |
| 137 | sdio_claim_host(sdiodev->func[func]); |
| 138 | if (rw) /* CMD52 Write */ |
| 139 | sdio_writeb(sdiodev->func[func], *byte, regaddr, |
| 140 | &err_ret); |
| 141 | else if (func == 0) { |
| 142 | *byte = sdio_f0_readb(sdiodev->func[func], regaddr, |
| 143 | &err_ret); |
| 144 | } else { |
| 145 | *byte = sdio_readb(sdiodev->func[func], regaddr, |
| 146 | &err_ret); |
| 147 | } |
| 148 | sdio_release_host(sdiodev->func[func]); |
| 149 | } |
| 150 | |
| 151 | if (err_ret) |
| 152 | brcmf_dbg(ERROR, "Failed to %s byte F%d:@0x%05x=%02x, Err: %d\n", |
| 153 | rw ? "write" : "read", func, regaddr, *byte, err_ret); |
| 154 | |
| 155 | return err_ret; |
| 156 | } |
| 157 | |
| 158 | int brcmf_sdioh_request_word(struct brcmf_sdio_dev *sdiodev, |
| 159 | uint rw, uint func, uint addr, u32 *word, |
| 160 | uint nbytes) |
| 161 | { |
| 162 | int err_ret = -EIO; |
| 163 | |
| 164 | if (func == 0) { |
| 165 | brcmf_dbg(ERROR, "Only CMD52 allowed to F0\n"); |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | |
| 169 | brcmf_dbg(INFO, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n", |
| 170 | rw, func, addr, nbytes); |
| 171 | |
| 172 | brcmf_pm_resume_wait(sdiodev, &sdiodev->request_word_wait); |
| 173 | if (brcmf_pm_resume_error(sdiodev)) |
| 174 | return -EIO; |
| 175 | /* Claim host controller */ |
| 176 | sdio_claim_host(sdiodev->func[func]); |
| 177 | |
| 178 | if (rw) { /* CMD52 Write */ |
| 179 | if (nbytes == 4) |
| 180 | sdio_writel(sdiodev->func[func], *word, addr, |
| 181 | &err_ret); |
| 182 | else if (nbytes == 2) |
| 183 | sdio_writew(sdiodev->func[func], (*word & 0xFFFF), |
| 184 | addr, &err_ret); |
| 185 | else |
| 186 | brcmf_dbg(ERROR, "Invalid nbytes: %d\n", nbytes); |
| 187 | } else { /* CMD52 Read */ |
| 188 | if (nbytes == 4) |
| 189 | *word = sdio_readl(sdiodev->func[func], addr, &err_ret); |
| 190 | else if (nbytes == 2) |
| 191 | *word = sdio_readw(sdiodev->func[func], addr, |
| 192 | &err_ret) & 0xFFFF; |
| 193 | else |
| 194 | brcmf_dbg(ERROR, "Invalid nbytes: %d\n", nbytes); |
| 195 | } |
| 196 | |
| 197 | /* Release host controller */ |
| 198 | sdio_release_host(sdiodev->func[func]); |
| 199 | |
| 200 | if (err_ret) |
| 201 | brcmf_dbg(ERROR, "Failed to %s word, Err: 0x%08x\n", |
| 202 | rw ? "write" : "read", err_ret); |
| 203 | |
| 204 | return err_ret; |
| 205 | } |
| 206 | |
| 207 | static int |
| 208 | brcmf_sdioh_request_packet(struct brcmf_sdio_dev *sdiodev, uint fix_inc, |
| 209 | uint write, uint func, uint addr, |
| 210 | struct sk_buff *pkt) |
| 211 | { |
| 212 | bool fifo = (fix_inc == SDIOH_DATA_FIX); |
| 213 | u32 SGCount = 0; |
| 214 | int err_ret = 0; |
| 215 | |
| 216 | struct sk_buff *pnext; |
| 217 | |
| 218 | brcmf_dbg(TRACE, "Enter\n"); |
| 219 | |
| 220 | brcmf_pm_resume_wait(sdiodev, &sdiodev->request_packet_wait); |
| 221 | if (brcmf_pm_resume_error(sdiodev)) |
| 222 | return -EIO; |
| 223 | |
| 224 | /* Claim host controller */ |
| 225 | sdio_claim_host(sdiodev->func[func]); |
| 226 | for (pnext = pkt; pnext; pnext = pnext->next) { |
| 227 | uint pkt_len = pnext->len; |
| 228 | pkt_len += 3; |
| 229 | pkt_len &= 0xFFFFFFFC; |
| 230 | |
| 231 | if ((write) && (!fifo)) { |
| 232 | err_ret = sdio_memcpy_toio(sdiodev->func[func], addr, |
| 233 | ((u8 *) (pnext->data)), |
| 234 | pkt_len); |
| 235 | } else if (write) { |
| 236 | err_ret = sdio_memcpy_toio(sdiodev->func[func], addr, |
| 237 | ((u8 *) (pnext->data)), |
| 238 | pkt_len); |
| 239 | } else if (fifo) { |
| 240 | err_ret = sdio_readsb(sdiodev->func[func], |
| 241 | ((u8 *) (pnext->data)), |
| 242 | addr, pkt_len); |
| 243 | } else { |
| 244 | err_ret = sdio_memcpy_fromio(sdiodev->func[func], |
| 245 | ((u8 *) (pnext->data)), |
| 246 | addr, pkt_len); |
| 247 | } |
| 248 | |
| 249 | if (err_ret) { |
| 250 | brcmf_dbg(ERROR, "%s FAILED %p[%d], addr=0x%05x, pkt_len=%d, ERR=0x%08x\n", |
| 251 | write ? "TX" : "RX", pnext, SGCount, addr, |
| 252 | pkt_len, err_ret); |
| 253 | } else { |
| 254 | brcmf_dbg(TRACE, "%s xfr'd %p[%d], addr=0x%05x, len=%d\n", |
| 255 | write ? "TX" : "RX", pnext, SGCount, addr, |
| 256 | pkt_len); |
| 257 | } |
| 258 | |
| 259 | if (!fifo) |
| 260 | addr += pkt_len; |
| 261 | SGCount++; |
| 262 | |
| 263 | } |
| 264 | |
| 265 | /* Release host controller */ |
| 266 | sdio_release_host(sdiodev->func[func]); |
| 267 | |
| 268 | brcmf_dbg(TRACE, "Exit\n"); |
| 269 | return err_ret; |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * This function takes a buffer or packet, and fixes everything up |
| 274 | * so that in the end, a DMA-able packet is created. |
| 275 | * |
| 276 | * A buffer does not have an associated packet pointer, |
| 277 | * and may or may not be aligned. |
| 278 | * A packet may consist of a single packet, or a packet chain. |
| 279 | * If it is a packet chain, then all the packets in the chain |
| 280 | * must be properly aligned. |
| 281 | * |
| 282 | * If the packet data is not aligned, then there may only be |
| 283 | * one packet, and in this case, it is copied to a new |
| 284 | * aligned packet. |
| 285 | * |
| 286 | */ |
| 287 | int brcmf_sdioh_request_buffer(struct brcmf_sdio_dev *sdiodev, |
| 288 | uint fix_inc, uint write, uint func, uint addr, |
| 289 | uint reg_width, uint buflen_u, u8 *buffer, |
| 290 | struct sk_buff *pkt) |
| 291 | { |
| 292 | int Status; |
| 293 | struct sk_buff *mypkt = NULL; |
| 294 | |
| 295 | brcmf_dbg(TRACE, "Enter\n"); |
| 296 | |
| 297 | brcmf_pm_resume_wait(sdiodev, &sdiodev->request_buffer_wait); |
| 298 | if (brcmf_pm_resume_error(sdiodev)) |
| 299 | return -EIO; |
| 300 | /* Case 1: we don't have a packet. */ |
| 301 | if (pkt == NULL) { |
| 302 | brcmf_dbg(DATA, "Creating new %s Packet, len=%d\n", |
| 303 | write ? "TX" : "RX", buflen_u); |
| 304 | mypkt = brcmu_pkt_buf_get_skb(buflen_u); |
| 305 | if (!mypkt) { |
| 306 | brcmf_dbg(ERROR, "brcmu_pkt_buf_get_skb failed: len %d\n", |
| 307 | buflen_u); |
| 308 | return -EIO; |
| 309 | } |
| 310 | |
| 311 | /* For a write, copy the buffer data into the packet. */ |
| 312 | if (write) |
| 313 | memcpy(mypkt->data, buffer, buflen_u); |
| 314 | |
| 315 | Status = brcmf_sdioh_request_packet(sdiodev, fix_inc, write, |
| 316 | func, addr, mypkt); |
| 317 | |
| 318 | /* For a read, copy the packet data back to the buffer. */ |
| 319 | if (!write) |
| 320 | memcpy(buffer, mypkt->data, buflen_u); |
| 321 | |
| 322 | brcmu_pkt_buf_free_skb(mypkt); |
| 323 | } else if (((ulong) (pkt->data) & DMA_ALIGN_MASK) != 0) { |
| 324 | /* |
| 325 | * Case 2: We have a packet, but it is unaligned. |
| 326 | * In this case, we cannot have a chain (pkt->next == NULL) |
| 327 | */ |
| 328 | brcmf_dbg(DATA, "Creating aligned %s Packet, len=%d\n", |
| 329 | write ? "TX" : "RX", pkt->len); |
| 330 | mypkt = brcmu_pkt_buf_get_skb(pkt->len); |
| 331 | if (!mypkt) { |
| 332 | brcmf_dbg(ERROR, "brcmu_pkt_buf_get_skb failed: len %d\n", |
| 333 | pkt->len); |
| 334 | return -EIO; |
| 335 | } |
| 336 | |
| 337 | /* For a write, copy the buffer data into the packet. */ |
| 338 | if (write) |
| 339 | memcpy(mypkt->data, pkt->data, pkt->len); |
| 340 | |
| 341 | Status = brcmf_sdioh_request_packet(sdiodev, fix_inc, write, |
| 342 | func, addr, mypkt); |
| 343 | |
| 344 | /* For a read, copy the packet data back to the buffer. */ |
| 345 | if (!write) |
| 346 | memcpy(pkt->data, mypkt->data, mypkt->len); |
| 347 | |
| 348 | brcmu_pkt_buf_free_skb(mypkt); |
| 349 | } else { /* case 3: We have a packet and |
| 350 | it is aligned. */ |
| 351 | brcmf_dbg(DATA, "Aligned %s Packet, direct DMA\n", |
| 352 | write ? "Tx" : "Rx"); |
| 353 | Status = brcmf_sdioh_request_packet(sdiodev, fix_inc, write, |
| 354 | func, addr, pkt); |
| 355 | } |
| 356 | |
| 357 | return Status; |
| 358 | } |
| 359 | |
| 360 | /* Read client card reg */ |
| 361 | static int |
| 362 | brcmf_sdioh_card_regread(struct brcmf_sdio_dev *sdiodev, int func, u32 regaddr, |
| 363 | int regsize, u32 *data) |
| 364 | { |
| 365 | |
| 366 | if ((func == 0) || (regsize == 1)) { |
| 367 | u8 temp = 0; |
| 368 | |
| 369 | brcmf_sdioh_request_byte(sdiodev, SDIOH_READ, func, regaddr, |
| 370 | &temp); |
| 371 | *data = temp; |
| 372 | *data &= 0xff; |
| 373 | brcmf_dbg(DATA, "byte read data=0x%02x\n", *data); |
| 374 | } else { |
| 375 | brcmf_sdioh_request_word(sdiodev, SDIOH_READ, func, regaddr, |
| 376 | data, regsize); |
| 377 | if (regsize == 2) |
| 378 | *data &= 0xffff; |
| 379 | |
| 380 | brcmf_dbg(DATA, "word read data=0x%08x\n", *data); |
| 381 | } |
| 382 | |
| 383 | return SUCCESS; |
| 384 | } |
| 385 | |
| 386 | static int brcmf_sdioh_get_cisaddr(struct brcmf_sdio_dev *sdiodev, u32 regaddr) |
| 387 | { |
| 388 | /* read 24 bits and return valid 17 bit addr */ |
| 389 | int i; |
| 390 | u32 scratch, regdata; |
| 391 | __le32 scratch_le; |
| 392 | u8 *ptr = (u8 *)&scratch_le; |
| 393 | |
| 394 | for (i = 0; i < 3; i++) { |
| 395 | if ((brcmf_sdioh_card_regread(sdiodev, 0, regaddr, 1, |
| 396 | ®data)) != SUCCESS) |
| 397 | brcmf_dbg(ERROR, "Can't read!\n"); |
| 398 | |
| 399 | *ptr++ = (u8) regdata; |
| 400 | regaddr++; |
| 401 | } |
| 402 | |
| 403 | /* Only the lower 17-bits are valid */ |
| 404 | scratch = le32_to_cpu(scratch_le); |
| 405 | scratch &= 0x0001FFFF; |
| 406 | return scratch; |
| 407 | } |
| 408 | |
| 409 | static int brcmf_sdioh_enablefuncs(struct brcmf_sdio_dev *sdiodev) |
| 410 | { |
| 411 | int err_ret; |
| 412 | u32 fbraddr; |
| 413 | u8 func; |
| 414 | |
| 415 | brcmf_dbg(TRACE, "\n"); |
| 416 | |
| 417 | /* Get the Card's common CIS address */ |
| 418 | sdiodev->func_cis_ptr[0] = brcmf_sdioh_get_cisaddr(sdiodev, |
| 419 | SDIO_CCCR_CIS); |
| 420 | brcmf_dbg(INFO, "Card's Common CIS Ptr = 0x%x\n", |
| 421 | sdiodev->func_cis_ptr[0]); |
| 422 | |
| 423 | /* Get the Card's function CIS (for each function) */ |
| 424 | for (fbraddr = SDIO_FBR_BASE(1), func = 1; |
| 425 | func <= sdiodev->num_funcs; func++, fbraddr += SDIOD_FBR_SIZE) { |
| 426 | sdiodev->func_cis_ptr[func] = |
| 427 | brcmf_sdioh_get_cisaddr(sdiodev, SDIO_FBR_CIS + fbraddr); |
| 428 | brcmf_dbg(INFO, "Function %d CIS Ptr = 0x%x\n", |
| 429 | func, sdiodev->func_cis_ptr[func]); |
| 430 | } |
| 431 | |
| 432 | /* Enable Function 1 */ |
| 433 | sdio_claim_host(sdiodev->func[1]); |
| 434 | err_ret = sdio_enable_func(sdiodev->func[1]); |
| 435 | sdio_release_host(sdiodev->func[1]); |
| 436 | if (err_ret) |
| 437 | brcmf_dbg(ERROR, "Failed to enable F1 Err: 0x%08x\n", err_ret); |
| 438 | |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * Public entry points & extern's |
| 444 | */ |
| 445 | int brcmf_sdioh_attach(struct brcmf_sdio_dev *sdiodev) |
| 446 | { |
| 447 | int err_ret = 0; |
| 448 | |
| 449 | brcmf_dbg(TRACE, "\n"); |
| 450 | |
| 451 | sdiodev->num_funcs = 2; |
| 452 | |
| 453 | sdio_claim_host(sdiodev->func[1]); |
| 454 | err_ret = sdio_set_block_size(sdiodev->func[1], SDIO_FUNC1_BLOCKSIZE); |
| 455 | sdio_release_host(sdiodev->func[1]); |
| 456 | if (err_ret) { |
| 457 | brcmf_dbg(ERROR, "Failed to set F1 blocksize\n"); |
| 458 | goto out; |
| 459 | } |
| 460 | |
| 461 | sdio_claim_host(sdiodev->func[2]); |
| 462 | err_ret = sdio_set_block_size(sdiodev->func[2], SDIO_FUNC2_BLOCKSIZE); |
| 463 | sdio_release_host(sdiodev->func[2]); |
| 464 | if (err_ret) { |
| 465 | brcmf_dbg(ERROR, "Failed to set F2 blocksize\n"); |
| 466 | goto out; |
| 467 | } |
| 468 | |
| 469 | brcmf_sdioh_enablefuncs(sdiodev); |
| 470 | |
| 471 | out: |
| 472 | brcmf_dbg(TRACE, "Done\n"); |
| 473 | return err_ret; |
| 474 | } |
| 475 | |
| 476 | void brcmf_sdioh_detach(struct brcmf_sdio_dev *sdiodev) |
| 477 | { |
| 478 | brcmf_dbg(TRACE, "\n"); |
| 479 | |
| 480 | /* Disable Function 2 */ |
| 481 | sdio_claim_host(sdiodev->func[2]); |
| 482 | sdio_disable_func(sdiodev->func[2]); |
| 483 | sdio_release_host(sdiodev->func[2]); |
| 484 | |
| 485 | /* Disable Function 1 */ |
| 486 | sdio_claim_host(sdiodev->func[1]); |
| 487 | sdio_disable_func(sdiodev->func[1]); |
| 488 | sdio_release_host(sdiodev->func[1]); |
| 489 | |
| 490 | } |
| 491 | |
| 492 | static int brcmf_ops_sdio_probe(struct sdio_func *func, |
| 493 | const struct sdio_device_id *id) |
| 494 | { |
| 495 | int ret = 0; |
| 496 | struct brcmf_sdio_dev *sdiodev; |
| 497 | brcmf_dbg(TRACE, "Enter\n"); |
| 498 | brcmf_dbg(TRACE, "func->class=%x\n", func->class); |
| 499 | brcmf_dbg(TRACE, "sdio_vendor: 0x%04x\n", func->vendor); |
| 500 | brcmf_dbg(TRACE, "sdio_device: 0x%04x\n", func->device); |
| 501 | brcmf_dbg(TRACE, "Function#: 0x%04x\n", func->num); |
| 502 | |
| 503 | if (func->num == 1) { |
| 504 | if (dev_get_drvdata(&func->card->dev)) { |
| 505 | brcmf_dbg(ERROR, "card private drvdata occupied\n"); |
| 506 | return -ENXIO; |
| 507 | } |
| 508 | sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL); |
| 509 | if (!sdiodev) |
| 510 | return -ENOMEM; |
| 511 | sdiodev->func[0] = func->card->sdio_func[0]; |
| 512 | sdiodev->func[1] = func; |
| 513 | dev_set_drvdata(&func->card->dev, sdiodev); |
| 514 | |
| 515 | atomic_set(&sdiodev->suspend, false); |
| 516 | init_waitqueue_head(&sdiodev->request_byte_wait); |
| 517 | init_waitqueue_head(&sdiodev->request_word_wait); |
| 518 | init_waitqueue_head(&sdiodev->request_packet_wait); |
| 519 | init_waitqueue_head(&sdiodev->request_buffer_wait); |
| 520 | } |
| 521 | |
| 522 | if (func->num == 2) { |
| 523 | sdiodev = dev_get_drvdata(&func->card->dev); |
| 524 | if ((!sdiodev) || (sdiodev->func[1]->card != func->card)) |
| 525 | return -ENODEV; |
| 526 | sdiodev->func[2] = func; |
| 527 | |
| 528 | brcmf_dbg(TRACE, "F2 found, calling brcmf_sdio_probe...\n"); |
| 529 | ret = brcmf_sdio_probe(sdiodev); |
| 530 | } |
| 531 | |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | static void brcmf_ops_sdio_remove(struct sdio_func *func) |
| 536 | { |
| 537 | struct brcmf_sdio_dev *sdiodev; |
| 538 | brcmf_dbg(TRACE, "Enter\n"); |
| 539 | brcmf_dbg(INFO, "func->class=%x\n", func->class); |
| 540 | brcmf_dbg(INFO, "sdio_vendor: 0x%04x\n", func->vendor); |
| 541 | brcmf_dbg(INFO, "sdio_device: 0x%04x\n", func->device); |
| 542 | brcmf_dbg(INFO, "Function#: 0x%04x\n", func->num); |
| 543 | |
| 544 | if (func->num == 2) { |
| 545 | sdiodev = dev_get_drvdata(&func->card->dev); |
| 546 | brcmf_dbg(TRACE, "F2 found, calling brcmf_sdio_remove...\n"); |
| 547 | brcmf_sdio_remove(sdiodev); |
| 548 | dev_set_drvdata(&func->card->dev, NULL); |
| 549 | kfree(sdiodev); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | #ifdef CONFIG_PM_SLEEP |
| 554 | static int brcmf_sdio_suspend(struct device *dev) |
| 555 | { |
| 556 | mmc_pm_flag_t sdio_flags; |
| 557 | struct brcmf_sdio_dev *sdiodev; |
| 558 | struct sdio_func *func = dev_to_sdio_func(dev); |
| 559 | int ret = 0; |
| 560 | |
| 561 | brcmf_dbg(TRACE, "\n"); |
| 562 | |
| 563 | sdiodev = dev_get_drvdata(&func->card->dev); |
| 564 | |
| 565 | atomic_set(&sdiodev->suspend, true); |
| 566 | |
| 567 | sdio_flags = sdio_get_host_pm_caps(sdiodev->func[1]); |
| 568 | if (!(sdio_flags & MMC_PM_KEEP_POWER)) { |
| 569 | brcmf_dbg(ERROR, "Host can't keep power while suspended\n"); |
| 570 | return -EINVAL; |
| 571 | } |
| 572 | |
| 573 | ret = sdio_set_host_pm_flags(sdiodev->func[1], MMC_PM_KEEP_POWER); |
| 574 | if (ret) { |
| 575 | brcmf_dbg(ERROR, "Failed to set pm_flags\n"); |
| 576 | return ret; |
| 577 | } |
| 578 | |
| 579 | brcmf_sdio_wdtmr_enable(sdiodev, false); |
| 580 | |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | static int brcmf_sdio_resume(struct device *dev) |
| 585 | { |
| 586 | struct brcmf_sdio_dev *sdiodev; |
| 587 | struct sdio_func *func = dev_to_sdio_func(dev); |
| 588 | |
| 589 | sdiodev = dev_get_drvdata(&func->card->dev); |
| 590 | brcmf_sdio_wdtmr_enable(sdiodev, true); |
| 591 | atomic_set(&sdiodev->suspend, false); |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | static const struct dev_pm_ops brcmf_sdio_pm_ops = { |
| 596 | .suspend = brcmf_sdio_suspend, |
| 597 | .resume = brcmf_sdio_resume, |
| 598 | }; |
| 599 | #endif /* CONFIG_PM_SLEEP */ |
| 600 | |
| 601 | static struct sdio_driver brcmf_sdmmc_driver = { |
| 602 | .probe = brcmf_ops_sdio_probe, |
| 603 | .remove = brcmf_ops_sdio_remove, |
| 604 | .name = "brcmfmac", |
| 605 | .id_table = brcmf_sdmmc_ids, |
| 606 | #ifdef CONFIG_PM_SLEEP |
| 607 | .drv = { |
| 608 | .pm = &brcmf_sdio_pm_ops, |
| 609 | }, |
| 610 | #endif /* CONFIG_PM_SLEEP */ |
| 611 | }; |
| 612 | |
| 613 | /* bus register interface */ |
| 614 | int brcmf_bus_register(void) |
| 615 | { |
| 616 | brcmf_dbg(TRACE, "Enter\n"); |
| 617 | |
| 618 | return sdio_register_driver(&brcmf_sdmmc_driver); |
| 619 | } |
| 620 | |
| 621 | void brcmf_bus_unregister(void) |
| 622 | { |
| 623 | brcmf_dbg(TRACE, "Enter\n"); |
| 624 | |
| 625 | sdio_unregister_driver(&brcmf_sdmmc_driver); |
| 626 | } |