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 | */ |
Joe Perches | 02f7719 | 2012-01-15 00:38:44 -0800 | [diff] [blame] | 16 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 17 | #include <linux/types.h> |
| 18 | #include <linux/netdevice.h> |
| 19 | #include <linux/mmc/sdio.h> |
| 20 | #include <linux/mmc/core.h> |
| 21 | #include <linux/mmc/sdio_func.h> |
| 22 | #include <linux/mmc/sdio_ids.h> |
| 23 | #include <linux/mmc/card.h> |
| 24 | #include <linux/suspend.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/sched.h> /* request_irq() */ |
Stephen Rothwell | b7a57e7 | 2011-10-12 21:35:07 +0200 | [diff] [blame] | 27 | #include <linux/module.h> |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 28 | #include <linux/platform_device.h> |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 29 | #include <linux/platform_data/brcmfmac-sdio.h> |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 30 | #include <net/cfg80211.h> |
| 31 | |
| 32 | #include <defs.h> |
| 33 | #include <brcm_hw_ids.h> |
| 34 | #include <brcmu_utils.h> |
| 35 | #include <brcmu_wifi.h> |
| 36 | #include "sdio_host.h" |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 37 | #include "dhd_dbg.h" |
Franky Lin | a8a363a | 2011-12-16 18:37:13 -0800 | [diff] [blame] | 38 | #include "dhd_bus.h" |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 39 | |
| 40 | #define SDIO_VENDOR_ID_BROADCOM 0x02d0 |
| 41 | |
| 42 | #define DMA_ALIGN_MASK 0x03 |
| 43 | |
Hante Meuleman | 369508c | 2013-04-11 13:28:54 +0200 | [diff] [blame] | 44 | #define SDIO_DEVICE_ID_BROADCOM_43143 43143 |
Franky Lin | 4a1c02c | 2012-08-30 19:42:59 +0200 | [diff] [blame] | 45 | #define SDIO_DEVICE_ID_BROADCOM_43241 0x4324 |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 46 | #define SDIO_DEVICE_ID_BROADCOM_4329 0x4329 |
Franky Lin | ce2d7d7 | 2011-12-08 15:06:39 -0800 | [diff] [blame] | 47 | #define SDIO_DEVICE_ID_BROADCOM_4330 0x4330 |
Franky Lin | 85a4a1c | 2012-06-26 21:26:39 +0200 | [diff] [blame] | 48 | #define SDIO_DEVICE_ID_BROADCOM_4334 0x4334 |
Franky Lin | 6a1c748 | 2013-04-11 13:28:53 +0200 | [diff] [blame] | 49 | #define SDIO_DEVICE_ID_BROADCOM_4335 0x4335 |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 50 | |
| 51 | #define SDIO_FUNC1_BLOCKSIZE 64 |
| 52 | #define SDIO_FUNC2_BLOCKSIZE 512 |
| 53 | |
| 54 | /* devices we support, null terminated */ |
| 55 | static const struct sdio_device_id brcmf_sdmmc_ids[] = { |
Hante Meuleman | 369508c | 2013-04-11 13:28:54 +0200 | [diff] [blame] | 56 | {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43143)}, |
Franky Lin | 4a1c02c | 2012-08-30 19:42:59 +0200 | [diff] [blame] | 57 | {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43241)}, |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 58 | {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4329)}, |
Franky Lin | ce2d7d7 | 2011-12-08 15:06:39 -0800 | [diff] [blame] | 59 | {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4330)}, |
Franky Lin | 85a4a1c | 2012-06-26 21:26:39 +0200 | [diff] [blame] | 60 | {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4334)}, |
Franky Lin | 6a1c748 | 2013-04-11 13:28:53 +0200 | [diff] [blame] | 61 | {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4335)}, |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 62 | { /* end: all zeroes */ }, |
| 63 | }; |
| 64 | MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids); |
| 65 | |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 66 | static struct brcmfmac_sdio_platform_data *brcmfmac_sdio_pdata; |
| 67 | |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 68 | |
Franky Lin | 78b3f1c | 2013-06-18 13:29:28 +0200 | [diff] [blame] | 69 | bool |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 70 | brcmf_pm_resume_error(struct brcmf_sdio_dev *sdiodev) |
| 71 | { |
| 72 | bool is_err = false; |
| 73 | #ifdef CONFIG_PM_SLEEP |
| 74 | is_err = atomic_read(&sdiodev->suspend); |
| 75 | #endif |
| 76 | return is_err; |
| 77 | } |
| 78 | |
Franky Lin | 78b3f1c | 2013-06-18 13:29:28 +0200 | [diff] [blame] | 79 | void |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 80 | brcmf_pm_resume_wait(struct brcmf_sdio_dev *sdiodev, wait_queue_head_t *wq) |
| 81 | { |
| 82 | #ifdef CONFIG_PM_SLEEP |
| 83 | int retry = 0; |
| 84 | while (atomic_read(&sdiodev->suspend) && retry++ != 30) |
| 85 | wait_event_timeout(*wq, false, HZ/100); |
| 86 | #endif |
| 87 | } |
| 88 | |
| 89 | static inline int brcmf_sdioh_f0_write_byte(struct brcmf_sdio_dev *sdiodev, |
| 90 | uint regaddr, u8 *byte) |
| 91 | { |
| 92 | struct sdio_func *sdfunc = sdiodev->func[0]; |
| 93 | int err_ret; |
| 94 | |
| 95 | /* |
| 96 | * Can only directly write to some F0 registers. |
| 97 | * Handle F2 enable/disable and Abort command |
| 98 | * as a special case. |
| 99 | */ |
| 100 | if (regaddr == SDIO_CCCR_IOEx) { |
| 101 | sdfunc = sdiodev->func[2]; |
| 102 | if (sdfunc) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 103 | if (*byte & SDIO_FUNC_ENABLE_2) { |
| 104 | /* Enable Function 2 */ |
| 105 | err_ret = sdio_enable_func(sdfunc); |
| 106 | if (err_ret) |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 107 | brcmf_err("enable F2 failed:%d\n", |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 108 | err_ret); |
| 109 | } else { |
| 110 | /* Disable Function 2 */ |
| 111 | err_ret = sdio_disable_func(sdfunc); |
| 112 | if (err_ret) |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 113 | brcmf_err("Disable F2 failed:%d\n", |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 114 | err_ret); |
| 115 | } |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 116 | } |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 117 | } else if ((regaddr == SDIO_CCCR_ABORT) || |
| 118 | (regaddr == SDIO_CCCR_IENx)) { |
Franky Lin | 1cc2699 | 2012-04-23 14:24:52 -0700 | [diff] [blame] | 119 | sdfunc = kmemdup(sdiodev->func[0], sizeof(struct sdio_func), |
| 120 | GFP_KERNEL); |
| 121 | if (!sdfunc) |
| 122 | return -ENOMEM; |
| 123 | sdfunc->num = 0; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 124 | sdio_writeb(sdfunc, *byte, regaddr, &err_ret); |
Franky Lin | 1cc2699 | 2012-04-23 14:24:52 -0700 | [diff] [blame] | 125 | kfree(sdfunc); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 126 | } else if (regaddr < 0xF0) { |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 127 | brcmf_err("F0 Wr:0x%02x: write disallowed\n", regaddr); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 128 | err_ret = -EPERM; |
| 129 | } else { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 130 | sdio_f0_writeb(sdfunc, *byte, regaddr, &err_ret); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | return err_ret; |
| 134 | } |
| 135 | |
| 136 | int brcmf_sdioh_request_byte(struct brcmf_sdio_dev *sdiodev, uint rw, uint func, |
| 137 | uint regaddr, u8 *byte) |
| 138 | { |
| 139 | int err_ret; |
| 140 | |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 141 | brcmf_dbg(SDIO, "rw=%d, func=%d, addr=0x%05x\n", rw, func, regaddr); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 142 | |
| 143 | brcmf_pm_resume_wait(sdiodev, &sdiodev->request_byte_wait); |
| 144 | if (brcmf_pm_resume_error(sdiodev)) |
| 145 | return -EIO; |
| 146 | |
| 147 | if (rw && func == 0) { |
| 148 | /* handle F0 separately */ |
| 149 | err_ret = brcmf_sdioh_f0_write_byte(sdiodev, regaddr, byte); |
| 150 | } else { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 151 | if (rw) /* CMD52 Write */ |
| 152 | sdio_writeb(sdiodev->func[func], *byte, regaddr, |
| 153 | &err_ret); |
| 154 | else if (func == 0) { |
| 155 | *byte = sdio_f0_readb(sdiodev->func[func], regaddr, |
| 156 | &err_ret); |
| 157 | } else { |
| 158 | *byte = sdio_readb(sdiodev->func[func], regaddr, |
| 159 | &err_ret); |
| 160 | } |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | if (err_ret) |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 164 | brcmf_err("Failed to %s byte F%d:@0x%05x=%02x, Err: %d\n", |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 165 | rw ? "write" : "read", func, regaddr, *byte, err_ret); |
| 166 | |
| 167 | return err_ret; |
| 168 | } |
| 169 | |
| 170 | int brcmf_sdioh_request_word(struct brcmf_sdio_dev *sdiodev, |
| 171 | uint rw, uint func, uint addr, u32 *word, |
| 172 | uint nbytes) |
| 173 | { |
| 174 | int err_ret = -EIO; |
| 175 | |
| 176 | if (func == 0) { |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 177 | brcmf_err("Only CMD52 allowed to F0\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 178 | return -EINVAL; |
| 179 | } |
| 180 | |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 181 | brcmf_dbg(SDIO, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n", |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 182 | rw, func, addr, nbytes); |
| 183 | |
| 184 | brcmf_pm_resume_wait(sdiodev, &sdiodev->request_word_wait); |
| 185 | if (brcmf_pm_resume_error(sdiodev)) |
| 186 | return -EIO; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 187 | |
| 188 | if (rw) { /* CMD52 Write */ |
| 189 | if (nbytes == 4) |
| 190 | sdio_writel(sdiodev->func[func], *word, addr, |
| 191 | &err_ret); |
| 192 | else if (nbytes == 2) |
| 193 | sdio_writew(sdiodev->func[func], (*word & 0xFFFF), |
| 194 | addr, &err_ret); |
| 195 | else |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 196 | brcmf_err("Invalid nbytes: %d\n", nbytes); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 197 | } else { /* CMD52 Read */ |
| 198 | if (nbytes == 4) |
| 199 | *word = sdio_readl(sdiodev->func[func], addr, &err_ret); |
| 200 | else if (nbytes == 2) |
| 201 | *word = sdio_readw(sdiodev->func[func], addr, |
| 202 | &err_ret) & 0xFFFF; |
| 203 | else |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 204 | brcmf_err("Invalid nbytes: %d\n", nbytes); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 207 | if (err_ret) |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 208 | brcmf_err("Failed to %s word, Err: 0x%08x\n", |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 209 | rw ? "write" : "read", err_ret); |
| 210 | |
| 211 | return err_ret; |
| 212 | } |
| 213 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 214 | static int brcmf_sdioh_get_cisaddr(struct brcmf_sdio_dev *sdiodev, u32 regaddr) |
| 215 | { |
| 216 | /* read 24 bits and return valid 17 bit addr */ |
Franky Lin | d8b3fc5 | 2012-05-04 18:27:38 -0700 | [diff] [blame] | 217 | int i, ret; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 218 | u32 scratch, regdata; |
| 219 | __le32 scratch_le; |
| 220 | u8 *ptr = (u8 *)&scratch_le; |
| 221 | |
| 222 | for (i = 0; i < 3; i++) { |
Franky Lin | d8b3fc5 | 2012-05-04 18:27:38 -0700 | [diff] [blame] | 223 | regdata = brcmf_sdio_regrl(sdiodev, regaddr, &ret); |
| 224 | if (ret != 0) |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 225 | brcmf_err("Can't read!\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 226 | |
| 227 | *ptr++ = (u8) regdata; |
| 228 | regaddr++; |
| 229 | } |
| 230 | |
| 231 | /* Only the lower 17-bits are valid */ |
| 232 | scratch = le32_to_cpu(scratch_le); |
| 233 | scratch &= 0x0001FFFF; |
| 234 | return scratch; |
| 235 | } |
| 236 | |
| 237 | static int brcmf_sdioh_enablefuncs(struct brcmf_sdio_dev *sdiodev) |
| 238 | { |
| 239 | int err_ret; |
| 240 | u32 fbraddr; |
| 241 | u8 func; |
| 242 | |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 243 | brcmf_dbg(SDIO, "\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 244 | |
| 245 | /* Get the Card's common CIS address */ |
| 246 | sdiodev->func_cis_ptr[0] = brcmf_sdioh_get_cisaddr(sdiodev, |
| 247 | SDIO_CCCR_CIS); |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 248 | brcmf_dbg(SDIO, "Card's Common CIS Ptr = 0x%x\n", |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 249 | sdiodev->func_cis_ptr[0]); |
| 250 | |
| 251 | /* Get the Card's function CIS (for each function) */ |
| 252 | for (fbraddr = SDIO_FBR_BASE(1), func = 1; |
| 253 | func <= sdiodev->num_funcs; func++, fbraddr += SDIOD_FBR_SIZE) { |
| 254 | sdiodev->func_cis_ptr[func] = |
| 255 | brcmf_sdioh_get_cisaddr(sdiodev, SDIO_FBR_CIS + fbraddr); |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 256 | brcmf_dbg(SDIO, "Function %d CIS Ptr = 0x%x\n", |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 257 | func, sdiodev->func_cis_ptr[func]); |
| 258 | } |
| 259 | |
| 260 | /* Enable Function 1 */ |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 261 | err_ret = sdio_enable_func(sdiodev->func[1]); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 262 | if (err_ret) |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 263 | brcmf_err("Failed to enable F1 Err: 0x%08x\n", err_ret); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 264 | |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Public entry points & extern's |
| 270 | */ |
| 271 | int brcmf_sdioh_attach(struct brcmf_sdio_dev *sdiodev) |
| 272 | { |
| 273 | int err_ret = 0; |
| 274 | |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 275 | brcmf_dbg(SDIO, "\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 276 | |
| 277 | sdiodev->num_funcs = 2; |
| 278 | |
| 279 | sdio_claim_host(sdiodev->func[1]); |
Franky Lin | 38b0b0d | 2012-11-05 16:22:24 -0800 | [diff] [blame] | 280 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 281 | err_ret = sdio_set_block_size(sdiodev->func[1], SDIO_FUNC1_BLOCKSIZE); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 282 | if (err_ret) { |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 283 | brcmf_err("Failed to set F1 blocksize\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 284 | goto out; |
| 285 | } |
| 286 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 287 | err_ret = sdio_set_block_size(sdiodev->func[2], SDIO_FUNC2_BLOCKSIZE); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 288 | if (err_ret) { |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 289 | brcmf_err("Failed to set F2 blocksize\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 290 | goto out; |
| 291 | } |
| 292 | |
| 293 | brcmf_sdioh_enablefuncs(sdiodev); |
| 294 | |
| 295 | out: |
Franky Lin | 38b0b0d | 2012-11-05 16:22:24 -0800 | [diff] [blame] | 296 | sdio_release_host(sdiodev->func[1]); |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 297 | brcmf_dbg(SDIO, "Done\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 298 | return err_ret; |
| 299 | } |
| 300 | |
| 301 | void brcmf_sdioh_detach(struct brcmf_sdio_dev *sdiodev) |
| 302 | { |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 303 | brcmf_dbg(SDIO, "\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 304 | |
| 305 | /* Disable Function 2 */ |
| 306 | sdio_claim_host(sdiodev->func[2]); |
| 307 | sdio_disable_func(sdiodev->func[2]); |
| 308 | sdio_release_host(sdiodev->func[2]); |
| 309 | |
| 310 | /* Disable Function 1 */ |
| 311 | sdio_claim_host(sdiodev->func[1]); |
| 312 | sdio_disable_func(sdiodev->func[1]); |
| 313 | sdio_release_host(sdiodev->func[1]); |
| 314 | |
| 315 | } |
| 316 | |
| 317 | static int brcmf_ops_sdio_probe(struct sdio_func *func, |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 318 | const struct sdio_device_id *id) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 319 | { |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 320 | int err; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 321 | struct brcmf_sdio_dev *sdiodev; |
Franky Lin | 655713b | 2011-11-22 17:21:51 -0800 | [diff] [blame] | 322 | struct brcmf_bus *bus_if; |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 323 | |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 324 | brcmf_dbg(SDIO, "Enter\n"); |
| 325 | brcmf_dbg(SDIO, "Class=%x\n", func->class); |
| 326 | brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor); |
| 327 | brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device); |
| 328 | brcmf_dbg(SDIO, "Function#: %d\n", func->num); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 329 | |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 330 | /* Consume func num 1 but dont do anything with it. */ |
| 331 | if (func->num == 1) |
| 332 | return 0; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 333 | |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 334 | /* Ignore anything but func 2 */ |
| 335 | if (func->num != 2) |
| 336 | return -ENODEV; |
| 337 | |
| 338 | bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL); |
| 339 | if (!bus_if) |
| 340 | return -ENOMEM; |
| 341 | sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL); |
| 342 | if (!sdiodev) { |
| 343 | kfree(bus_if); |
| 344 | return -ENOMEM; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 345 | } |
| 346 | |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 347 | sdiodev->func[0] = func->card->sdio_func[0]; |
| 348 | sdiodev->func[1] = func->card->sdio_func[0]; |
| 349 | sdiodev->func[2] = func; |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 350 | |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 351 | sdiodev->bus_if = bus_if; |
| 352 | bus_if->bus_priv.sdio = sdiodev; |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 353 | dev_set_drvdata(&func->dev, bus_if); |
| 354 | dev_set_drvdata(&sdiodev->func[1]->dev, bus_if); |
| 355 | sdiodev->dev = &sdiodev->func[1]->dev; |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 356 | sdiodev->pdata = brcmfmac_sdio_pdata; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 357 | |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 358 | atomic_set(&sdiodev->suspend, false); |
| 359 | init_waitqueue_head(&sdiodev->request_byte_wait); |
| 360 | init_waitqueue_head(&sdiodev->request_word_wait); |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 361 | init_waitqueue_head(&sdiodev->request_buffer_wait); |
Franky Lin | d76d1c8 | 2011-12-08 15:06:37 -0800 | [diff] [blame] | 362 | |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 363 | brcmf_dbg(SDIO, "F2 found, calling brcmf_sdio_probe...\n"); |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 364 | err = brcmf_sdio_probe(sdiodev); |
| 365 | if (err) { |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 366 | brcmf_err("F2 error, probe failed %d...\n", err); |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 367 | goto fail; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 368 | } |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 369 | brcmf_dbg(SDIO, "F2 init completed...\n"); |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 370 | return 0; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 371 | |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 372 | fail: |
| 373 | dev_set_drvdata(&func->dev, NULL); |
| 374 | dev_set_drvdata(&sdiodev->func[1]->dev, NULL); |
| 375 | kfree(sdiodev); |
| 376 | kfree(bus_if); |
| 377 | return err; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static void brcmf_ops_sdio_remove(struct sdio_func *func) |
| 381 | { |
Franky Lin | 655713b | 2011-11-22 17:21:51 -0800 | [diff] [blame] | 382 | struct brcmf_bus *bus_if; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 383 | struct brcmf_sdio_dev *sdiodev; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 384 | |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 385 | brcmf_dbg(SDIO, "Enter\n"); |
| 386 | brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor); |
| 387 | brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device); |
| 388 | brcmf_dbg(SDIO, "Function: %d\n", func->num); |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 389 | |
| 390 | if (func->num != 1 && func->num != 2) |
| 391 | return; |
| 392 | |
| 393 | bus_if = dev_get_drvdata(&func->dev); |
| 394 | if (bus_if) { |
Arend van Spriel | 0a332e4 | 2012-02-09 21:09:02 +0100 | [diff] [blame] | 395 | sdiodev = bus_if->bus_priv.sdio; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 396 | brcmf_sdio_remove(sdiodev); |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 397 | |
| 398 | dev_set_drvdata(&sdiodev->func[1]->dev, NULL); |
| 399 | dev_set_drvdata(&sdiodev->func[2]->dev, NULL); |
| 400 | |
Franky Lin | 655713b | 2011-11-22 17:21:51 -0800 | [diff] [blame] | 401 | kfree(bus_if); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 402 | kfree(sdiodev); |
| 403 | } |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 404 | |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 405 | brcmf_dbg(SDIO, "Exit\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | #ifdef CONFIG_PM_SLEEP |
| 409 | static int brcmf_sdio_suspend(struct device *dev) |
| 410 | { |
| 411 | mmc_pm_flag_t sdio_flags; |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 412 | struct brcmf_bus *bus_if = dev_get_drvdata(dev); |
| 413 | struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 414 | int ret = 0; |
| 415 | |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 416 | brcmf_dbg(SDIO, "\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 417 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 418 | atomic_set(&sdiodev->suspend, true); |
| 419 | |
| 420 | sdio_flags = sdio_get_host_pm_caps(sdiodev->func[1]); |
| 421 | if (!(sdio_flags & MMC_PM_KEEP_POWER)) { |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 422 | brcmf_err("Host can't keep power while suspended\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 423 | return -EINVAL; |
| 424 | } |
| 425 | |
| 426 | ret = sdio_set_host_pm_flags(sdiodev->func[1], MMC_PM_KEEP_POWER); |
| 427 | if (ret) { |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 428 | brcmf_err("Failed to set pm_flags\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 429 | return ret; |
| 430 | } |
| 431 | |
| 432 | brcmf_sdio_wdtmr_enable(sdiodev, false); |
| 433 | |
| 434 | return ret; |
| 435 | } |
| 436 | |
| 437 | static int brcmf_sdio_resume(struct device *dev) |
| 438 | { |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 439 | struct brcmf_bus *bus_if = dev_get_drvdata(dev); |
| 440 | struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 441 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 442 | brcmf_sdio_wdtmr_enable(sdiodev, true); |
| 443 | atomic_set(&sdiodev->suspend, false); |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static const struct dev_pm_ops brcmf_sdio_pm_ops = { |
| 448 | .suspend = brcmf_sdio_suspend, |
| 449 | .resume = brcmf_sdio_resume, |
| 450 | }; |
| 451 | #endif /* CONFIG_PM_SLEEP */ |
| 452 | |
| 453 | static struct sdio_driver brcmf_sdmmc_driver = { |
| 454 | .probe = brcmf_ops_sdio_probe, |
| 455 | .remove = brcmf_ops_sdio_remove, |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 456 | .name = BRCMFMAC_SDIO_PDATA_NAME, |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 457 | .id_table = brcmf_sdmmc_ids, |
| 458 | #ifdef CONFIG_PM_SLEEP |
| 459 | .drv = { |
| 460 | .pm = &brcmf_sdio_pm_ops, |
| 461 | }, |
| 462 | #endif /* CONFIG_PM_SLEEP */ |
| 463 | }; |
| 464 | |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 465 | static int brcmf_sdio_pd_probe(struct platform_device *pdev) |
| 466 | { |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 467 | brcmf_dbg(SDIO, "Enter\n"); |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 468 | |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 469 | brcmfmac_sdio_pdata = pdev->dev.platform_data; |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 470 | |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 471 | if (brcmfmac_sdio_pdata->power_on) |
| 472 | brcmfmac_sdio_pdata->power_on(); |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 473 | |
Arend van Spriel | db4efbb | 2013-09-25 12:11:01 +0200 | [diff] [blame^] | 474 | return 0; |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 477 | static int brcmf_sdio_pd_remove(struct platform_device *pdev) |
| 478 | { |
| 479 | brcmf_dbg(SDIO, "Enter\n"); |
| 480 | |
| 481 | if (brcmfmac_sdio_pdata->power_off) |
| 482 | brcmfmac_sdio_pdata->power_off(); |
| 483 | |
| 484 | sdio_unregister_driver(&brcmf_sdmmc_driver); |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 489 | static struct platform_driver brcmf_sdio_pd = { |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 490 | .remove = brcmf_sdio_pd_remove, |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 491 | .driver = { |
Libo Chen | f91f5f0 | 2013-05-21 10:42:40 +0800 | [diff] [blame] | 492 | .name = BRCMFMAC_SDIO_PDATA_NAME, |
| 493 | .owner = THIS_MODULE, |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 494 | } |
| 495 | }; |
| 496 | |
Arend van Spriel | db4efbb | 2013-09-25 12:11:01 +0200 | [diff] [blame^] | 497 | void brcmf_sdio_register(void) |
| 498 | { |
| 499 | int ret; |
| 500 | |
| 501 | ret = sdio_register_driver(&brcmf_sdmmc_driver); |
| 502 | if (ret) |
| 503 | brcmf_err("sdio_register_driver failed: %d\n", ret); |
| 504 | } |
| 505 | |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 506 | void brcmf_sdio_exit(void) |
| 507 | { |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 508 | brcmf_dbg(SDIO, "Enter\n"); |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 509 | |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 510 | if (brcmfmac_sdio_pdata) |
| 511 | platform_driver_unregister(&brcmf_sdio_pd); |
| 512 | else |
| 513 | sdio_unregister_driver(&brcmf_sdmmc_driver); |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Arend van Spriel | db4efbb | 2013-09-25 12:11:01 +0200 | [diff] [blame^] | 516 | void __init brcmf_sdio_init(void) |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 517 | { |
| 518 | int ret; |
| 519 | |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 520 | brcmf_dbg(SDIO, "Enter\n"); |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 521 | |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 522 | ret = platform_driver_probe(&brcmf_sdio_pd, brcmf_sdio_pd_probe); |
Arend van Spriel | db4efbb | 2013-09-25 12:11:01 +0200 | [diff] [blame^] | 523 | if (ret == -ENODEV) |
| 524 | brcmf_dbg(SDIO, "No platform data available.\n"); |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 525 | } |