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 | |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/netdevice.h> |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 20 | #include <linux/pci.h> |
| 21 | #include <linux/pci_ids.h> |
| 22 | #include <linux/sched.h> |
| 23 | #include <linux/completion.h> |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 24 | #include <linux/scatterlist.h> |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 25 | #include <linux/mmc/sdio.h> |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 26 | #include <linux/mmc/core.h> |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 27 | #include <linux/mmc/sdio_func.h> |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 28 | #include <linux/mmc/sdio_ids.h> |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 29 | #include <linux/mmc/card.h> |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 30 | #include <linux/mmc/host.h> |
| 31 | #include <linux/platform_device.h> |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 32 | #include <linux/platform_data/brcmfmac-sdio.h> |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 33 | #include <linux/suspend.h> |
| 34 | #include <linux/errno.h> |
| 35 | #include <linux/module.h> |
| 36 | #include <net/cfg80211.h> |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 37 | |
| 38 | #include <defs.h> |
| 39 | #include <brcm_hw_ids.h> |
| 40 | #include <brcmu_utils.h> |
| 41 | #include <brcmu_wifi.h> |
| 42 | #include <soc.h> |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 43 | #include "dhd_bus.h" |
| 44 | #include "dhd_dbg.h" |
| 45 | #include "sdio_host.h" |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 46 | #include "sdio_chip.h" |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 47 | |
| 48 | #define SDIOH_API_ACCESS_RETRY_LIMIT 2 |
| 49 | |
Arend van Spriel | e49b06b | 2013-12-12 11:58:51 +0100 | [diff] [blame] | 50 | #define SDIO_VENDOR_ID_BROADCOM 0x02d0 |
| 51 | |
| 52 | #define DMA_ALIGN_MASK 0x03 |
| 53 | |
| 54 | #define SDIO_FUNC1_BLOCKSIZE 64 |
| 55 | #define SDIO_FUNC2_BLOCKSIZE 512 |
Arend van Spriel | 71370eb | 2013-12-12 11:58:53 +0100 | [diff] [blame] | 56 | /* Maximum milliseconds to wait for F2 to come up */ |
| 57 | #define SDIO_WAIT_F2RDY 3000 |
| 58 | |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 59 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 60 | static irqreturn_t brcmf_sdiod_oob_irqhandler(int irq, void *dev_id) |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 61 | { |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 62 | struct brcmf_bus *bus_if = dev_get_drvdata(dev_id); |
| 63 | struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 64 | |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 65 | brcmf_dbg(INTR, "OOB intr triggered\n"); |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 66 | |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 67 | /* out-of-band interrupt is level-triggered which won't |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 68 | * be cleared until dpc |
| 69 | */ |
| 70 | if (sdiodev->irq_en) { |
| 71 | disable_irq_nosync(irq); |
| 72 | sdiodev->irq_en = false; |
| 73 | } |
| 74 | |
Arend van Spriel | 82d7f3c | 2013-12-12 11:59:04 +0100 | [diff] [blame^] | 75 | brcmf_sdio_isr(sdiodev->bus); |
Franky Lin | ba89bf1 | 2012-04-27 18:56:59 -0700 | [diff] [blame] | 76 | |
| 77 | return IRQ_HANDLED; |
| 78 | } |
| 79 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 80 | static void brcmf_sdiod_ib_irqhandler(struct sdio_func *func) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 81 | { |
Hante Meuleman | 5b3c183 | 2012-11-14 18:46:20 -0800 | [diff] [blame] | 82 | struct brcmf_bus *bus_if = dev_get_drvdata(&func->dev); |
| 83 | struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 84 | |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 85 | brcmf_dbg(INTR, "IB intr triggered\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 86 | |
Arend van Spriel | 82d7f3c | 2013-12-12 11:59:04 +0100 | [diff] [blame^] | 87 | brcmf_sdio_isr(sdiodev->bus); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Franky Lin | fbf5910 | 2011-12-16 18:36:53 -0800 | [diff] [blame] | 90 | /* dummy handler for SDIO function 2 interrupt */ |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 91 | static void brcmf_sdiod_dummy_irqhandler(struct sdio_func *func) |
Franky Lin | fbf5910 | 2011-12-16 18:36:53 -0800 | [diff] [blame] | 92 | { |
| 93 | } |
| 94 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 95 | static bool brcmf_sdiod_pm_resume_error(struct brcmf_sdio_dev *sdiodev) |
Arend van Spriel | e49b06b | 2013-12-12 11:58:51 +0100 | [diff] [blame] | 96 | { |
| 97 | bool is_err = false; |
| 98 | #ifdef CONFIG_PM_SLEEP |
| 99 | is_err = atomic_read(&sdiodev->suspend); |
| 100 | #endif |
| 101 | return is_err; |
| 102 | } |
| 103 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 104 | static void brcmf_sdiod_pm_resume_wait(struct brcmf_sdio_dev *sdiodev, |
| 105 | wait_queue_head_t *wq) |
Arend van Spriel | e49b06b | 2013-12-12 11:58:51 +0100 | [diff] [blame] | 106 | { |
| 107 | #ifdef CONFIG_PM_SLEEP |
| 108 | int retry = 0; |
| 109 | while (atomic_read(&sdiodev->suspend) && retry++ != 30) |
| 110 | wait_event_timeout(*wq, false, HZ/100); |
| 111 | #endif |
| 112 | } |
| 113 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 114 | int brcmf_sdiod_intr_register(struct brcmf_sdio_dev *sdiodev) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 115 | { |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 116 | int ret = 0; |
| 117 | u8 data; |
| 118 | unsigned long flags; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 119 | |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 120 | if ((sdiodev->pdata) && (sdiodev->pdata->oob_irq_supported)) { |
| 121 | brcmf_dbg(SDIO, "Enter, register OOB IRQ %d\n", |
| 122 | sdiodev->pdata->oob_irq_nr); |
| 123 | ret = request_irq(sdiodev->pdata->oob_irq_nr, |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 124 | brcmf_sdiod_oob_irqhandler, |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 125 | sdiodev->pdata->oob_irq_flags, |
| 126 | "brcmf_oob_intr", |
| 127 | &sdiodev->func[1]->dev); |
| 128 | if (ret != 0) { |
| 129 | brcmf_err("request_irq failed %d\n", ret); |
| 130 | return ret; |
| 131 | } |
| 132 | sdiodev->oob_irq_requested = true; |
| 133 | spin_lock_init(&sdiodev->irq_en_lock); |
| 134 | spin_lock_irqsave(&sdiodev->irq_en_lock, flags); |
| 135 | sdiodev->irq_en = true; |
| 136 | spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags); |
| 137 | |
| 138 | ret = enable_irq_wake(sdiodev->pdata->oob_irq_nr); |
| 139 | if (ret != 0) { |
| 140 | brcmf_err("enable_irq_wake failed %d\n", ret); |
| 141 | return ret; |
| 142 | } |
| 143 | sdiodev->irq_wake = true; |
| 144 | |
| 145 | sdio_claim_host(sdiodev->func[1]); |
| 146 | |
| 147 | /* must configure SDIO_CCCR_IENx to enable irq */ |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 148 | data = brcmf_sdiod_regrb(sdiodev, SDIO_CCCR_IENx, &ret); |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 149 | data |= 1 << SDIO_FUNC_1 | 1 << SDIO_FUNC_2 | 1; |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 150 | brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_IENx, data, &ret); |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 151 | |
| 152 | /* redirect, configure and enable io for interrupt signal */ |
| 153 | data = SDIO_SEPINT_MASK | SDIO_SEPINT_OE; |
| 154 | if (sdiodev->pdata->oob_irq_flags & IRQF_TRIGGER_HIGH) |
| 155 | data |= SDIO_SEPINT_ACT_HI; |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 156 | brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, data, &ret); |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 157 | |
| 158 | sdio_release_host(sdiodev->func[1]); |
| 159 | } else { |
| 160 | brcmf_dbg(SDIO, "Entering\n"); |
| 161 | sdio_claim_host(sdiodev->func[1]); |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 162 | sdio_claim_irq(sdiodev->func[1], brcmf_sdiod_ib_irqhandler); |
| 163 | sdio_claim_irq(sdiodev->func[2], brcmf_sdiod_dummy_irqhandler); |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 164 | sdio_release_host(sdiodev->func[1]); |
| 165 | } |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 170 | int brcmf_sdiod_intr_unregister(struct brcmf_sdio_dev *sdiodev) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 171 | { |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 172 | brcmf_dbg(SDIO, "Entering\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 173 | |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 174 | if ((sdiodev->pdata) && (sdiodev->pdata->oob_irq_supported)) { |
| 175 | sdio_claim_host(sdiodev->func[1]); |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 176 | brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_BRCM_SEPINT, 0, NULL); |
| 177 | brcmf_sdiod_regwb(sdiodev, SDIO_CCCR_IENx, 0, NULL); |
Hante Meuleman | 668761a | 2013-04-12 10:55:55 +0200 | [diff] [blame] | 178 | sdio_release_host(sdiodev->func[1]); |
| 179 | |
| 180 | if (sdiodev->oob_irq_requested) { |
| 181 | sdiodev->oob_irq_requested = false; |
| 182 | if (sdiodev->irq_wake) { |
| 183 | disable_irq_wake(sdiodev->pdata->oob_irq_nr); |
| 184 | sdiodev->irq_wake = false; |
| 185 | } |
| 186 | free_irq(sdiodev->pdata->oob_irq_nr, |
| 187 | &sdiodev->func[1]->dev); |
| 188 | sdiodev->irq_en = false; |
| 189 | } |
| 190 | } else { |
| 191 | sdio_claim_host(sdiodev->func[1]); |
| 192 | sdio_release_irq(sdiodev->func[2]); |
| 193 | sdio_release_irq(sdiodev->func[1]); |
| 194 | sdio_release_host(sdiodev->func[1]); |
| 195 | } |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 200 | static inline int brcmf_sdiod_f0_write_byte(struct brcmf_sdio_dev *sdiodev, |
Arend van Spriel | e49b06b | 2013-12-12 11:58:51 +0100 | [diff] [blame] | 201 | uint regaddr, u8 *byte) |
| 202 | { |
| 203 | struct sdio_func *sdfunc = sdiodev->func[0]; |
| 204 | int err_ret; |
| 205 | |
| 206 | /* |
| 207 | * Can only directly write to some F0 registers. |
| 208 | * Handle F2 enable/disable and Abort command |
| 209 | * as a special case. |
| 210 | */ |
Arend van Spriel | 71370eb | 2013-12-12 11:58:53 +0100 | [diff] [blame] | 211 | if ((regaddr == SDIO_CCCR_ABORT) || |
| 212 | (regaddr == SDIO_CCCR_IENx)) { |
Arend van Spriel | e49b06b | 2013-12-12 11:58:51 +0100 | [diff] [blame] | 213 | sdfunc = kmemdup(sdiodev->func[0], sizeof(struct sdio_func), |
| 214 | GFP_KERNEL); |
| 215 | if (!sdfunc) |
| 216 | return -ENOMEM; |
| 217 | sdfunc->num = 0; |
| 218 | sdio_writeb(sdfunc, *byte, regaddr, &err_ret); |
| 219 | kfree(sdfunc); |
| 220 | } else if (regaddr < 0xF0) { |
| 221 | brcmf_err("F0 Wr:0x%02x: write disallowed\n", regaddr); |
| 222 | err_ret = -EPERM; |
| 223 | } else { |
| 224 | sdio_f0_writeb(sdfunc, *byte, regaddr, &err_ret); |
| 225 | } |
| 226 | |
| 227 | return err_ret; |
| 228 | } |
| 229 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 230 | static int brcmf_sdiod_request_byte(struct brcmf_sdio_dev *sdiodev, uint rw, |
Arend van Spriel | e49b06b | 2013-12-12 11:58:51 +0100 | [diff] [blame] | 231 | uint func, uint regaddr, u8 *byte) |
| 232 | { |
| 233 | int err_ret; |
| 234 | |
| 235 | brcmf_dbg(SDIO, "rw=%d, func=%d, addr=0x%05x\n", rw, func, regaddr); |
| 236 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 237 | brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_byte_wait); |
| 238 | if (brcmf_sdiod_pm_resume_error(sdiodev)) |
Arend van Spriel | e49b06b | 2013-12-12 11:58:51 +0100 | [diff] [blame] | 239 | return -EIO; |
| 240 | |
| 241 | if (rw && func == 0) { |
| 242 | /* handle F0 separately */ |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 243 | err_ret = brcmf_sdiod_f0_write_byte(sdiodev, regaddr, byte); |
Arend van Spriel | e49b06b | 2013-12-12 11:58:51 +0100 | [diff] [blame] | 244 | } else { |
| 245 | if (rw) /* CMD52 Write */ |
| 246 | sdio_writeb(sdiodev->func[func], *byte, regaddr, |
| 247 | &err_ret); |
| 248 | else if (func == 0) { |
| 249 | *byte = sdio_f0_readb(sdiodev->func[func], regaddr, |
| 250 | &err_ret); |
| 251 | } else { |
| 252 | *byte = sdio_readb(sdiodev->func[func], regaddr, |
| 253 | &err_ret); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (err_ret) { |
| 258 | /* |
| 259 | * SleepCSR register access can fail when |
| 260 | * waking up the device so reduce this noise |
| 261 | * in the logs. |
| 262 | */ |
| 263 | if (regaddr != SBSDIO_FUNC1_SLEEPCSR) |
| 264 | brcmf_err("Failed to %s byte F%d:@0x%05x=%02x, Err: %d\n", |
| 265 | rw ? "write" : "read", func, regaddr, *byte, |
| 266 | err_ret); |
| 267 | else |
| 268 | brcmf_dbg(SDIO, "Failed to %s byte F%d:@0x%05x=%02x, Err: %d\n", |
| 269 | rw ? "write" : "read", func, regaddr, *byte, |
| 270 | err_ret); |
| 271 | } |
| 272 | return err_ret; |
| 273 | } |
| 274 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 275 | static int brcmf_sdiod_request_word(struct brcmf_sdio_dev *sdiodev, uint rw, |
Arend van Spriel | e49b06b | 2013-12-12 11:58:51 +0100 | [diff] [blame] | 276 | uint func, uint addr, u32 *word, |
| 277 | uint nbytes) |
| 278 | { |
| 279 | int err_ret = -EIO; |
| 280 | |
| 281 | if (func == 0) { |
| 282 | brcmf_err("Only CMD52 allowed to F0\n"); |
| 283 | return -EINVAL; |
| 284 | } |
| 285 | |
| 286 | brcmf_dbg(SDIO, "rw=%d, func=%d, addr=0x%05x, nbytes=%d\n", |
| 287 | rw, func, addr, nbytes); |
| 288 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 289 | brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_word_wait); |
| 290 | if (brcmf_sdiod_pm_resume_error(sdiodev)) |
Arend van Spriel | e49b06b | 2013-12-12 11:58:51 +0100 | [diff] [blame] | 291 | return -EIO; |
| 292 | |
| 293 | if (rw) { /* CMD52 Write */ |
| 294 | if (nbytes == 4) |
| 295 | sdio_writel(sdiodev->func[func], *word, addr, |
| 296 | &err_ret); |
| 297 | else if (nbytes == 2) |
| 298 | sdio_writew(sdiodev->func[func], (*word & 0xFFFF), |
| 299 | addr, &err_ret); |
| 300 | else |
| 301 | brcmf_err("Invalid nbytes: %d\n", nbytes); |
| 302 | } else { /* CMD52 Read */ |
| 303 | if (nbytes == 4) |
| 304 | *word = sdio_readl(sdiodev->func[func], addr, &err_ret); |
| 305 | else if (nbytes == 2) |
| 306 | *word = sdio_readw(sdiodev->func[func], addr, |
| 307 | &err_ret) & 0xFFFF; |
| 308 | else |
| 309 | brcmf_err("Invalid nbytes: %d\n", nbytes); |
| 310 | } |
| 311 | |
| 312 | if (err_ret) |
| 313 | brcmf_err("Failed to %s word, Err: 0x%08x\n", |
| 314 | rw ? "write" : "read", err_ret); |
| 315 | |
| 316 | return err_ret; |
| 317 | } |
| 318 | |
Franky Lin | 356bae6 | 2013-06-26 14:20:17 +0200 | [diff] [blame] | 319 | static int |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 320 | brcmf_sdiod_set_sbaddr_window(struct brcmf_sdio_dev *sdiodev, u32 address) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 321 | { |
Franky Lin | 7d9cfc2 | 2012-05-04 18:27:30 -0700 | [diff] [blame] | 322 | int err = 0, i; |
| 323 | u8 addr[3]; |
| 324 | s32 retry; |
| 325 | |
| 326 | addr[0] = (address >> 8) & SBSDIO_SBADDRLOW_MASK; |
| 327 | addr[1] = (address >> 16) & SBSDIO_SBADDRMID_MASK; |
| 328 | addr[2] = (address >> 24) & SBSDIO_SBADDRHIGH_MASK; |
| 329 | |
| 330 | for (i = 0; i < 3; i++) { |
| 331 | retry = 0; |
| 332 | do { |
| 333 | if (retry) |
| 334 | usleep_range(1000, 2000); |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 335 | err = brcmf_sdiod_request_byte(sdiodev, SDIOH_WRITE, |
Franky Lin | 7d9cfc2 | 2012-05-04 18:27:30 -0700 | [diff] [blame] | 336 | SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW + i, |
| 337 | &addr[i]); |
| 338 | } while (err != 0 && retry++ < SDIOH_API_ACCESS_RETRY_LIMIT); |
| 339 | |
| 340 | if (err) { |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 341 | brcmf_err("failed at addr:0x%0x\n", |
Franky Lin | 7d9cfc2 | 2012-05-04 18:27:30 -0700 | [diff] [blame] | 342 | SBSDIO_FUNC1_SBADDRLOW + i); |
| 343 | break; |
| 344 | } |
| 345 | } |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 346 | |
| 347 | return err; |
| 348 | } |
| 349 | |
Franky Lin | 356bae6 | 2013-06-26 14:20:17 +0200 | [diff] [blame] | 350 | static int |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 351 | brcmf_sdiod_addrprep(struct brcmf_sdio_dev *sdiodev, uint width, u32 *addr) |
Franky Lin | 356bae6 | 2013-06-26 14:20:17 +0200 | [diff] [blame] | 352 | { |
| 353 | uint bar0 = *addr & ~SBSDIO_SB_OFT_ADDR_MASK; |
| 354 | int err = 0; |
| 355 | |
| 356 | if (bar0 != sdiodev->sbwad) { |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 357 | err = brcmf_sdiod_set_sbaddr_window(sdiodev, bar0); |
Franky Lin | 356bae6 | 2013-06-26 14:20:17 +0200 | [diff] [blame] | 358 | if (err) |
| 359 | return err; |
| 360 | |
| 361 | sdiodev->sbwad = bar0; |
| 362 | } |
| 363 | |
| 364 | *addr &= SBSDIO_SB_OFT_ADDR_MASK; |
| 365 | |
| 366 | if (width == 4) |
| 367 | *addr |= SBSDIO_SB_ACCESS_2_4B_FLAG; |
| 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 372 | static int brcmf_sdiod_regrw_helper(struct brcmf_sdio_dev *sdiodev, u32 addr, |
| 373 | void *data, bool write) |
Franky Lin | e9b8d91 | 2012-05-04 18:27:31 -0700 | [diff] [blame] | 374 | { |
| 375 | u8 func_num, reg_size; |
Franky Lin | e9b8d91 | 2012-05-04 18:27:31 -0700 | [diff] [blame] | 376 | s32 retry = 0; |
| 377 | int ret; |
| 378 | |
| 379 | /* |
| 380 | * figure out how to read the register based on address range |
Franky Lin | d8b3fc5 | 2012-05-04 18:27:38 -0700 | [diff] [blame] | 381 | * 0x00 ~ 0x7FF: function 0 CCCR and FBR |
Franky Lin | e9b8d91 | 2012-05-04 18:27:31 -0700 | [diff] [blame] | 382 | * 0x10000 ~ 0x1FFFF: function 1 miscellaneous registers |
| 383 | * The rest: function 1 silicon backplane core registers |
| 384 | */ |
Franky Lin | d8b3fc5 | 2012-05-04 18:27:38 -0700 | [diff] [blame] | 385 | if ((addr & ~REG_F0_REG_MASK) == 0) { |
Franky Lin | e9b8d91 | 2012-05-04 18:27:31 -0700 | [diff] [blame] | 386 | func_num = SDIO_FUNC_0; |
| 387 | reg_size = 1; |
| 388 | } else if ((addr & ~REG_F1_MISC_MASK) == 0) { |
| 389 | func_num = SDIO_FUNC_1; |
| 390 | reg_size = 1; |
| 391 | } else { |
| 392 | func_num = SDIO_FUNC_1; |
| 393 | reg_size = 4; |
| 394 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 395 | ret = brcmf_sdiod_addrprep(sdiodev, reg_size, &addr); |
Arend van Spriel | fcaac2b | 2013-10-15 15:44:53 +0200 | [diff] [blame] | 396 | if (ret) |
| 397 | goto done; |
Franky Lin | e9b8d91 | 2012-05-04 18:27:31 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | do { |
| 401 | if (!write) |
| 402 | memset(data, 0, reg_size); |
| 403 | if (retry) /* wait for 1 ms till bus get settled down */ |
| 404 | usleep_range(1000, 2000); |
| 405 | if (reg_size == 1) |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 406 | ret = brcmf_sdiod_request_byte(sdiodev, write, |
Franky Lin | e9b8d91 | 2012-05-04 18:27:31 -0700 | [diff] [blame] | 407 | func_num, addr, data); |
| 408 | else |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 409 | ret = brcmf_sdiod_request_word(sdiodev, write, |
Franky Lin | e9b8d91 | 2012-05-04 18:27:31 -0700 | [diff] [blame] | 410 | func_num, addr, data, 4); |
| 411 | } while (ret != 0 && retry++ < SDIOH_API_ACCESS_RETRY_LIMIT); |
| 412 | |
Arend van Spriel | fcaac2b | 2013-10-15 15:44:53 +0200 | [diff] [blame] | 413 | done: |
Franky Lin | 5c15c23 | 2012-05-04 18:27:37 -0700 | [diff] [blame] | 414 | if (ret != 0) |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 415 | brcmf_err("failed with %d\n", ret); |
Franky Lin | e9b8d91 | 2012-05-04 18:27:31 -0700 | [diff] [blame] | 416 | |
| 417 | return ret; |
| 418 | } |
| 419 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 420 | u8 brcmf_sdiod_regrb(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret) |
Franky Lin | e9b8d91 | 2012-05-04 18:27:31 -0700 | [diff] [blame] | 421 | { |
| 422 | u8 data; |
| 423 | int retval; |
| 424 | |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 425 | brcmf_dbg(SDIO, "addr:0x%08x\n", addr); |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 426 | retval = brcmf_sdiod_regrw_helper(sdiodev, addr, &data, false); |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 427 | brcmf_dbg(SDIO, "data:0x%02x\n", data); |
Franky Lin | e9b8d91 | 2012-05-04 18:27:31 -0700 | [diff] [blame] | 428 | |
| 429 | if (ret) |
| 430 | *ret = retval; |
| 431 | |
| 432 | return data; |
| 433 | } |
| 434 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 435 | u32 brcmf_sdiod_regrl(struct brcmf_sdio_dev *sdiodev, u32 addr, int *ret) |
Franky Lin | e9b8d91 | 2012-05-04 18:27:31 -0700 | [diff] [blame] | 436 | { |
| 437 | u32 data; |
| 438 | int retval; |
| 439 | |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 440 | brcmf_dbg(SDIO, "addr:0x%08x\n", addr); |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 441 | retval = brcmf_sdiod_regrw_helper(sdiodev, addr, &data, false); |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 442 | brcmf_dbg(SDIO, "data:0x%08x\n", data); |
Franky Lin | e9b8d91 | 2012-05-04 18:27:31 -0700 | [diff] [blame] | 443 | |
| 444 | if (ret) |
| 445 | *ret = retval; |
| 446 | |
| 447 | return data; |
| 448 | } |
| 449 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 450 | void brcmf_sdiod_regwb(struct brcmf_sdio_dev *sdiodev, u32 addr, |
Franky Lin | e9b8d91 | 2012-05-04 18:27:31 -0700 | [diff] [blame] | 451 | u8 data, int *ret) |
| 452 | { |
| 453 | int retval; |
| 454 | |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 455 | brcmf_dbg(SDIO, "addr:0x%08x, data:0x%02x\n", addr, data); |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 456 | retval = brcmf_sdiod_regrw_helper(sdiodev, addr, &data, true); |
Franky Lin | e9b8d91 | 2012-05-04 18:27:31 -0700 | [diff] [blame] | 457 | |
| 458 | if (ret) |
| 459 | *ret = retval; |
| 460 | } |
| 461 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 462 | void brcmf_sdiod_regwl(struct brcmf_sdio_dev *sdiodev, u32 addr, |
Franky Lin | e9b8d91 | 2012-05-04 18:27:31 -0700 | [diff] [blame] | 463 | u32 data, int *ret) |
| 464 | { |
| 465 | int retval; |
| 466 | |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 467 | brcmf_dbg(SDIO, "addr:0x%08x, data:0x%08x\n", addr, data); |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 468 | retval = brcmf_sdiod_regrw_helper(sdiodev, addr, &data, true); |
Franky Lin | e9b8d91 | 2012-05-04 18:27:31 -0700 | [diff] [blame] | 469 | |
| 470 | if (ret) |
| 471 | *ret = retval; |
| 472 | } |
| 473 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 474 | static int brcmf_sdiod_buffrw(struct brcmf_sdio_dev *sdiodev, uint fn, |
Arend van Spriel | 4aef267 | 2013-10-15 15:44:52 +0200 | [diff] [blame] | 475 | bool write, u32 addr, struct sk_buff *pkt) |
| 476 | { |
| 477 | unsigned int req_sz; |
| 478 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 479 | brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_buffer_wait); |
| 480 | if (brcmf_sdiod_pm_resume_error(sdiodev)) |
Arend van Spriel | 4aef267 | 2013-10-15 15:44:52 +0200 | [diff] [blame] | 481 | return -EIO; |
| 482 | |
| 483 | /* Single skb use the standard mmc interface */ |
| 484 | req_sz = pkt->len + 3; |
| 485 | req_sz &= (uint)~3; |
| 486 | |
| 487 | if (write) |
| 488 | return sdio_memcpy_toio(sdiodev->func[fn], addr, |
| 489 | ((u8 *)(pkt->data)), |
| 490 | req_sz); |
| 491 | else if (fn == 1) |
| 492 | return sdio_memcpy_fromio(sdiodev->func[fn], |
| 493 | ((u8 *)(pkt->data)), |
| 494 | addr, req_sz); |
| 495 | else |
| 496 | /* function 2 read is FIFO operation */ |
| 497 | return sdio_readsb(sdiodev->func[fn], |
| 498 | ((u8 *)(pkt->data)), addr, |
| 499 | req_sz); |
| 500 | } |
| 501 | |
Franky Lin | 78b3f1c | 2013-06-18 13:29:28 +0200 | [diff] [blame] | 502 | /** |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 503 | * brcmf_sdiod_sglist_rw - SDIO interface function for block data access |
Franky Lin | 78b3f1c | 2013-06-18 13:29:28 +0200 | [diff] [blame] | 504 | * @sdiodev: brcmfmac sdio device |
| 505 | * @fn: SDIO function number |
| 506 | * @write: direction flag |
| 507 | * @addr: dongle memory address as source/destination |
| 508 | * @pkt: skb pointer |
| 509 | * |
| 510 | * This function takes the respbonsibility as the interface function to MMC |
| 511 | * stack for block data access. It assumes that the skb passed down by the |
| 512 | * caller has already been padded and aligned. |
| 513 | */ |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 514 | static int brcmf_sdiod_sglist_rw(struct brcmf_sdio_dev *sdiodev, uint fn, |
| 515 | bool write, u32 addr, |
| 516 | struct sk_buff_head *pktlist) |
Franky Lin | 78b3f1c | 2013-06-18 13:29:28 +0200 | [diff] [blame] | 517 | { |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 518 | unsigned int req_sz, func_blk_sz, sg_cnt, sg_data_sz, pkt_offset; |
Arend van Spriel | 7120149 | 2013-10-15 15:44:49 +0200 | [diff] [blame] | 519 | unsigned int max_req_sz, orig_offset, dst_offset; |
Arend van Spriel | 3f78274 | 2013-10-15 15:44:48 +0200 | [diff] [blame] | 520 | unsigned short max_seg_cnt, seg_sz; |
Franky Lin | 3b81a68 | 2013-06-26 14:20:18 +0200 | [diff] [blame] | 521 | unsigned char *pkt_data, *orig_data, *dst_data; |
| 522 | struct sk_buff *pkt_next = NULL, *local_pkt_next; |
| 523 | struct sk_buff_head local_list, *target_list; |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 524 | struct mmc_request mmc_req; |
| 525 | struct mmc_command mmc_cmd; |
| 526 | struct mmc_data mmc_dat; |
| 527 | struct sg_table st; |
| 528 | struct scatterlist *sgl; |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 529 | int ret = 0; |
| 530 | |
| 531 | if (!pktlist->qlen) |
| 532 | return -EINVAL; |
Franky Lin | 78b3f1c | 2013-06-18 13:29:28 +0200 | [diff] [blame] | 533 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 534 | brcmf_sdiod_pm_resume_wait(sdiodev, &sdiodev->request_buffer_wait); |
| 535 | if (brcmf_sdiod_pm_resume_error(sdiodev)) |
Franky Lin | 78b3f1c | 2013-06-18 13:29:28 +0200 | [diff] [blame] | 536 | return -EIO; |
| 537 | |
Franky Lin | 3b81a68 | 2013-06-26 14:20:18 +0200 | [diff] [blame] | 538 | target_list = pktlist; |
| 539 | /* for host with broken sg support, prepare a page aligned list */ |
| 540 | __skb_queue_head_init(&local_list); |
| 541 | if (sdiodev->pdata && sdiodev->pdata->broken_sg_support && !write) { |
| 542 | req_sz = 0; |
| 543 | skb_queue_walk(pktlist, pkt_next) |
| 544 | req_sz += pkt_next->len; |
| 545 | req_sz = ALIGN(req_sz, sdiodev->func[fn]->cur_blksize); |
| 546 | while (req_sz > PAGE_SIZE) { |
| 547 | pkt_next = brcmu_pkt_buf_get_skb(PAGE_SIZE); |
| 548 | if (pkt_next == NULL) { |
| 549 | ret = -ENOMEM; |
| 550 | goto exit; |
| 551 | } |
| 552 | __skb_queue_tail(&local_list, pkt_next); |
| 553 | req_sz -= PAGE_SIZE; |
| 554 | } |
| 555 | pkt_next = brcmu_pkt_buf_get_skb(req_sz); |
| 556 | if (pkt_next == NULL) { |
| 557 | ret = -ENOMEM; |
| 558 | goto exit; |
| 559 | } |
| 560 | __skb_queue_tail(&local_list, pkt_next); |
| 561 | target_list = &local_list; |
| 562 | } |
| 563 | |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 564 | func_blk_sz = sdiodev->func[fn]->cur_blksize; |
Arend van Spriel | 7120149 | 2013-10-15 15:44:49 +0200 | [diff] [blame] | 565 | max_req_sz = sdiodev->max_request_size; |
| 566 | max_seg_cnt = min_t(unsigned short, sdiodev->max_segment_count, |
| 567 | target_list->qlen); |
Franky Lin | 3b81a68 | 2013-06-26 14:20:18 +0200 | [diff] [blame] | 568 | seg_sz = target_list->qlen; |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 569 | pkt_offset = 0; |
Franky Lin | 3b81a68 | 2013-06-26 14:20:18 +0200 | [diff] [blame] | 570 | pkt_next = target_list->next; |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 571 | |
Arend van Spriel | 3f78274 | 2013-10-15 15:44:48 +0200 | [diff] [blame] | 572 | if (sg_alloc_table(&st, max_seg_cnt, GFP_KERNEL)) { |
Franky Lin | 3b81a68 | 2013-06-26 14:20:18 +0200 | [diff] [blame] | 573 | ret = -ENOMEM; |
| 574 | goto exit; |
| 575 | } |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 576 | |
Arend van Spriel | 7f9a8dc | 2013-10-15 15:44:50 +0200 | [diff] [blame] | 577 | memset(&mmc_req, 0, sizeof(struct mmc_request)); |
| 578 | memset(&mmc_cmd, 0, sizeof(struct mmc_command)); |
| 579 | memset(&mmc_dat, 0, sizeof(struct mmc_data)); |
| 580 | |
| 581 | mmc_dat.sg = st.sgl; |
| 582 | mmc_dat.blksz = func_blk_sz; |
| 583 | mmc_dat.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ; |
| 584 | mmc_cmd.opcode = SD_IO_RW_EXTENDED; |
| 585 | mmc_cmd.arg = write ? 1<<31 : 0; /* write flag */ |
| 586 | mmc_cmd.arg |= (fn & 0x7) << 28; /* SDIO func num */ |
| 587 | mmc_cmd.arg |= 1<<27; /* block mode */ |
| 588 | /* for function 1 the addr will be incremented */ |
| 589 | mmc_cmd.arg |= (fn == 1) ? 1<<26 : 0; |
| 590 | mmc_cmd.flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_ADTC; |
| 591 | mmc_req.cmd = &mmc_cmd; |
| 592 | mmc_req.data = &mmc_dat; |
| 593 | |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 594 | while (seg_sz) { |
| 595 | req_sz = 0; |
| 596 | sg_cnt = 0; |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 597 | sgl = st.sgl; |
| 598 | /* prep sg table */ |
Franky Lin | 3b81a68 | 2013-06-26 14:20:18 +0200 | [diff] [blame] | 599 | while (pkt_next != (struct sk_buff *)target_list) { |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 600 | pkt_data = pkt_next->data + pkt_offset; |
| 601 | sg_data_sz = pkt_next->len - pkt_offset; |
Arend van Spriel | 7120149 | 2013-10-15 15:44:49 +0200 | [diff] [blame] | 602 | if (sg_data_sz > sdiodev->max_segment_size) |
| 603 | sg_data_sz = sdiodev->max_segment_size; |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 604 | if (sg_data_sz > max_req_sz - req_sz) |
| 605 | sg_data_sz = max_req_sz - req_sz; |
| 606 | |
| 607 | sg_set_buf(sgl, pkt_data, sg_data_sz); |
| 608 | |
| 609 | sg_cnt++; |
| 610 | sgl = sg_next(sgl); |
| 611 | req_sz += sg_data_sz; |
| 612 | pkt_offset += sg_data_sz; |
| 613 | if (pkt_offset == pkt_next->len) { |
| 614 | pkt_offset = 0; |
| 615 | pkt_next = pkt_next->next; |
| 616 | } |
| 617 | |
Arend van Spriel | 3f78274 | 2013-10-15 15:44:48 +0200 | [diff] [blame] | 618 | if (req_sz >= max_req_sz || sg_cnt >= max_seg_cnt) |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 619 | break; |
| 620 | } |
| 621 | seg_sz -= sg_cnt; |
| 622 | |
| 623 | if (req_sz % func_blk_sz != 0) { |
| 624 | brcmf_err("sg request length %u is not %u aligned\n", |
| 625 | req_sz, func_blk_sz); |
Franky Lin | 3b81a68 | 2013-06-26 14:20:18 +0200 | [diff] [blame] | 626 | ret = -ENOTBLK; |
| 627 | goto exit; |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 628 | } |
Arend van Spriel | 7f9a8dc | 2013-10-15 15:44:50 +0200 | [diff] [blame] | 629 | |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 630 | mmc_dat.sg_len = sg_cnt; |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 631 | mmc_dat.blocks = req_sz / func_blk_sz; |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 632 | mmc_cmd.arg |= (addr & 0x1FFFF) << 9; /* address */ |
| 633 | mmc_cmd.arg |= mmc_dat.blocks & 0x1FF; /* block count */ |
Arend van Spriel | 7f9a8dc | 2013-10-15 15:44:50 +0200 | [diff] [blame] | 634 | /* incrementing addr for function 1 */ |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 635 | if (fn == 1) |
| 636 | addr += req_sz; |
| 637 | |
| 638 | mmc_set_data_timeout(&mmc_dat, sdiodev->func[fn]->card); |
Arend van Spriel | 7120149 | 2013-10-15 15:44:49 +0200 | [diff] [blame] | 639 | mmc_wait_for_req(sdiodev->func[fn]->card->host, &mmc_req); |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 640 | |
| 641 | ret = mmc_cmd.error ? mmc_cmd.error : mmc_dat.error; |
| 642 | if (ret != 0) { |
| 643 | brcmf_err("CMD53 sg block %s failed %d\n", |
| 644 | write ? "write" : "read", ret); |
| 645 | ret = -EIO; |
| 646 | break; |
| 647 | } |
| 648 | } |
| 649 | |
Franky Lin | 3b81a68 | 2013-06-26 14:20:18 +0200 | [diff] [blame] | 650 | if (sdiodev->pdata && sdiodev->pdata->broken_sg_support && !write) { |
| 651 | local_pkt_next = local_list.next; |
| 652 | orig_offset = 0; |
| 653 | skb_queue_walk(pktlist, pkt_next) { |
| 654 | dst_offset = 0; |
| 655 | do { |
| 656 | req_sz = local_pkt_next->len - orig_offset; |
| 657 | req_sz = min_t(uint, pkt_next->len - dst_offset, |
| 658 | req_sz); |
| 659 | orig_data = local_pkt_next->data + orig_offset; |
| 660 | dst_data = pkt_next->data + dst_offset; |
| 661 | memcpy(dst_data, orig_data, req_sz); |
| 662 | orig_offset += req_sz; |
| 663 | dst_offset += req_sz; |
| 664 | if (orig_offset == local_pkt_next->len) { |
| 665 | orig_offset = 0; |
| 666 | local_pkt_next = local_pkt_next->next; |
| 667 | } |
| 668 | if (dst_offset == pkt_next->len) |
| 669 | break; |
| 670 | } while (!skb_queue_empty(&local_list)); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | exit: |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 675 | sg_free_table(&st); |
Franky Lin | 3b81a68 | 2013-06-26 14:20:18 +0200 | [diff] [blame] | 676 | while ((pkt_next = __skb_dequeue(&local_list)) != NULL) |
| 677 | brcmu_pkt_buf_free_skb(pkt_next); |
Franky Lin | 354b75b | 2013-06-18 13:29:29 +0200 | [diff] [blame] | 678 | |
| 679 | return ret; |
Franky Lin | 78b3f1c | 2013-06-18 13:29:28 +0200 | [diff] [blame] | 680 | } |
| 681 | |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 682 | int brcmf_sdiod_recv_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes) |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 683 | { |
| 684 | struct sk_buff *mypkt; |
| 685 | int err; |
| 686 | |
| 687 | mypkt = brcmu_pkt_buf_get_skb(nbytes); |
| 688 | if (!mypkt) { |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 689 | brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n", |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 690 | nbytes); |
| 691 | return -EIO; |
| 692 | } |
| 693 | |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 694 | err = brcmf_sdiod_recv_pkt(sdiodev, mypkt); |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 695 | if (!err) |
| 696 | memcpy(buf, mypkt->data, nbytes); |
| 697 | |
| 698 | brcmu_pkt_buf_free_skb(mypkt); |
| 699 | return err; |
| 700 | } |
| 701 | |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 702 | int brcmf_sdiod_recv_pkt(struct brcmf_sdio_dev *sdiodev, struct sk_buff *pkt) |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 703 | { |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 704 | u32 addr = sdiodev->sbwad; |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 705 | int err = 0; |
| 706 | |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 707 | brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pkt->len); |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 708 | |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 709 | err = brcmf_sdiod_addrprep(sdiodev, 4, &addr); |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 710 | if (err) |
Franky Lin | 7057fd0 | 2012-09-13 21:12:02 +0200 | [diff] [blame] | 711 | goto done; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 712 | |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 713 | err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr, pkt); |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 714 | |
Franky Lin | 7057fd0 | 2012-09-13 21:12:02 +0200 | [diff] [blame] | 715 | done: |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 716 | return err; |
| 717 | } |
| 718 | |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 719 | int brcmf_sdiod_recv_chain(struct brcmf_sdio_dev *sdiodev, |
| 720 | struct sk_buff_head *pktq, uint totlen) |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 721 | { |
Arend van Spriel | a413e39 | 2013-10-15 15:44:57 +0200 | [diff] [blame] | 722 | struct sk_buff *glom_skb; |
| 723 | struct sk_buff *skb; |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 724 | u32 addr = sdiodev->sbwad; |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 725 | int err = 0; |
| 726 | |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 727 | brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", |
| 728 | addr, pktq->qlen); |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 729 | |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 730 | err = brcmf_sdiod_addrprep(sdiodev, 4, &addr); |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 731 | if (err) |
Franky Lin | 7057fd0 | 2012-09-13 21:12:02 +0200 | [diff] [blame] | 732 | goto done; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 733 | |
Arend van Spriel | a413e39 | 2013-10-15 15:44:57 +0200 | [diff] [blame] | 734 | if (pktq->qlen == 1) |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 735 | err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr, |
| 736 | pktq->next); |
Arend van Spriel | a413e39 | 2013-10-15 15:44:57 +0200 | [diff] [blame] | 737 | else if (!sdiodev->sg_support) { |
| 738 | glom_skb = brcmu_pkt_buf_get_skb(totlen); |
| 739 | if (!glom_skb) |
| 740 | return -ENOMEM; |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 741 | err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, false, addr, |
| 742 | glom_skb); |
Arend van Spriel | a413e39 | 2013-10-15 15:44:57 +0200 | [diff] [blame] | 743 | if (err) |
| 744 | goto done; |
| 745 | |
| 746 | skb_queue_walk(pktq, skb) { |
| 747 | memcpy(skb->data, glom_skb->data, skb->len); |
| 748 | skb_pull(glom_skb, skb->len); |
| 749 | } |
| 750 | } else |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 751 | err = brcmf_sdiod_sglist_rw(sdiodev, SDIO_FUNC_2, false, addr, |
| 752 | pktq); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 753 | |
Franky Lin | 7057fd0 | 2012-09-13 21:12:02 +0200 | [diff] [blame] | 754 | done: |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 755 | return err; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 756 | } |
| 757 | |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 758 | int brcmf_sdiod_send_buf(struct brcmf_sdio_dev *sdiodev, u8 *buf, uint nbytes) |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 759 | { |
| 760 | struct sk_buff *mypkt; |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 761 | u32 addr = sdiodev->sbwad; |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 762 | int err; |
| 763 | |
| 764 | mypkt = brcmu_pkt_buf_get_skb(nbytes); |
| 765 | if (!mypkt) { |
Arend van Spriel | 5e8149f | 2012-12-07 10:49:57 +0100 | [diff] [blame] | 766 | brcmf_err("brcmu_pkt_buf_get_skb failed: len %d\n", |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 767 | nbytes); |
| 768 | return -EIO; |
| 769 | } |
| 770 | |
| 771 | memcpy(mypkt->data, buf, nbytes); |
Arend van Spriel | 4aef267 | 2013-10-15 15:44:52 +0200 | [diff] [blame] | 772 | |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 773 | err = brcmf_sdiod_addrprep(sdiodev, 4, &addr); |
Arend van Spriel | 4aef267 | 2013-10-15 15:44:52 +0200 | [diff] [blame] | 774 | |
Arend van Spriel | fcaac2b | 2013-10-15 15:44:53 +0200 | [diff] [blame] | 775 | if (!err) |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 776 | err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, true, addr, |
| 777 | mypkt); |
Arend van Spriel | 5adfeb6 | 2011-11-22 17:21:38 -0800 | [diff] [blame] | 778 | |
| 779 | brcmu_pkt_buf_free_skb(mypkt); |
| 780 | return err; |
| 781 | |
| 782 | } |
| 783 | |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 784 | int brcmf_sdiod_send_pkt(struct brcmf_sdio_dev *sdiodev, |
| 785 | struct sk_buff_head *pktq) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 786 | { |
Arend van Spriel | c8cce1f | 2013-10-15 15:44:55 +0200 | [diff] [blame] | 787 | struct sk_buff *skb; |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 788 | u32 addr = sdiodev->sbwad; |
Arend van Spriel | fcaac2b | 2013-10-15 15:44:53 +0200 | [diff] [blame] | 789 | int err; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 790 | |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 791 | brcmf_dbg(SDIO, "addr = 0x%x, size = %d\n", addr, pktq->qlen); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 792 | |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 793 | err = brcmf_sdiod_addrprep(sdiodev, 4, &addr); |
Arend van Spriel | fcaac2b | 2013-10-15 15:44:53 +0200 | [diff] [blame] | 794 | if (err) |
| 795 | return err; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 796 | |
Arend van Spriel | c8cce1f | 2013-10-15 15:44:55 +0200 | [diff] [blame] | 797 | if (pktq->qlen == 1 || !sdiodev->sg_support) |
| 798 | skb_queue_walk(pktq, skb) { |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 799 | err = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_2, true, |
| 800 | addr, skb); |
Arend van Spriel | c8cce1f | 2013-10-15 15:44:55 +0200 | [diff] [blame] | 801 | if (err) |
| 802 | break; |
| 803 | } |
| 804 | else |
Arend van Spriel | a7cdd82 | 2013-12-12 11:58:59 +0100 | [diff] [blame] | 805 | err = brcmf_sdiod_sglist_rw(sdiodev, SDIO_FUNC_2, true, addr, |
| 806 | pktq); |
Arend van Spriel | c8cce1f | 2013-10-15 15:44:55 +0200 | [diff] [blame] | 807 | |
| 808 | return err; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 809 | } |
| 810 | |
Franky Lin | ba540b0 | 2013-04-11 13:28:47 +0200 | [diff] [blame] | 811 | int |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 812 | brcmf_sdiod_ramrw(struct brcmf_sdio_dev *sdiodev, bool write, u32 address, |
| 813 | u8 *data, uint size) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 814 | { |
Franky Lin | ba540b0 | 2013-04-11 13:28:47 +0200 | [diff] [blame] | 815 | int bcmerror = 0; |
| 816 | struct sk_buff *pkt; |
| 817 | u32 sdaddr; |
| 818 | uint dsize; |
Arend van Spriel | 4c6e869 | 2011-11-22 17:21:40 -0800 | [diff] [blame] | 819 | |
Franky Lin | ba540b0 | 2013-04-11 13:28:47 +0200 | [diff] [blame] | 820 | dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size); |
| 821 | pkt = dev_alloc_skb(dsize); |
| 822 | if (!pkt) { |
| 823 | brcmf_err("dev_alloc_skb failed: len %d\n", dsize); |
Arend van Spriel | 4c6e869 | 2011-11-22 17:21:40 -0800 | [diff] [blame] | 824 | return -EIO; |
| 825 | } |
Franky Lin | ba540b0 | 2013-04-11 13:28:47 +0200 | [diff] [blame] | 826 | pkt->priority = 0; |
Arend van Spriel | 4c6e869 | 2011-11-22 17:21:40 -0800 | [diff] [blame] | 827 | |
Franky Lin | ba540b0 | 2013-04-11 13:28:47 +0200 | [diff] [blame] | 828 | /* Determine initial transfer parameters */ |
| 829 | sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK; |
| 830 | if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK) |
| 831 | dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr); |
| 832 | else |
| 833 | dsize = size; |
Arend van Spriel | 4c6e869 | 2011-11-22 17:21:40 -0800 | [diff] [blame] | 834 | |
Franky Lin | ba540b0 | 2013-04-11 13:28:47 +0200 | [diff] [blame] | 835 | sdio_claim_host(sdiodev->func[1]); |
Arend van Spriel | 4c6e869 | 2011-11-22 17:21:40 -0800 | [diff] [blame] | 836 | |
Franky Lin | ba540b0 | 2013-04-11 13:28:47 +0200 | [diff] [blame] | 837 | /* Do the transfer(s) */ |
| 838 | while (size) { |
| 839 | /* Set the backplane window to include the start address */ |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 840 | bcmerror = brcmf_sdiod_set_sbaddr_window(sdiodev, address); |
Franky Lin | ba540b0 | 2013-04-11 13:28:47 +0200 | [diff] [blame] | 841 | if (bcmerror) |
| 842 | break; |
Arend van Spriel | 4c6e869 | 2011-11-22 17:21:40 -0800 | [diff] [blame] | 843 | |
Franky Lin | ba540b0 | 2013-04-11 13:28:47 +0200 | [diff] [blame] | 844 | brcmf_dbg(SDIO, "%s %d bytes at offset 0x%08x in window 0x%08x\n", |
| 845 | write ? "write" : "read", dsize, |
| 846 | sdaddr, address & SBSDIO_SBWINDOW_MASK); |
| 847 | |
| 848 | sdaddr &= SBSDIO_SB_OFT_ADDR_MASK; |
| 849 | sdaddr |= SBSDIO_SB_ACCESS_2_4B_FLAG; |
| 850 | |
| 851 | skb_put(pkt, dsize); |
| 852 | if (write) |
| 853 | memcpy(pkt->data, data, dsize); |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 854 | bcmerror = brcmf_sdiod_buffrw(sdiodev, SDIO_FUNC_1, write, |
| 855 | sdaddr, pkt); |
Franky Lin | ba540b0 | 2013-04-11 13:28:47 +0200 | [diff] [blame] | 856 | if (bcmerror) { |
| 857 | brcmf_err("membytes transfer failed\n"); |
| 858 | break; |
| 859 | } |
| 860 | if (!write) |
| 861 | memcpy(data, pkt->data, dsize); |
| 862 | skb_trim(pkt, dsize); |
| 863 | |
| 864 | /* Adjust for next transfer (if any) */ |
| 865 | size -= dsize; |
| 866 | if (size) { |
| 867 | data += dsize; |
| 868 | address += dsize; |
| 869 | sdaddr = 0; |
| 870 | dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size); |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | dev_kfree_skb(pkt); |
| 875 | |
| 876 | /* Return the window to backplane enumeration space for core access */ |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 877 | if (brcmf_sdiod_set_sbaddr_window(sdiodev, sdiodev->sbwad)) |
Franky Lin | ba540b0 | 2013-04-11 13:28:47 +0200 | [diff] [blame] | 878 | brcmf_err("FAILED to set window back to 0x%x\n", |
| 879 | sdiodev->sbwad); |
| 880 | |
| 881 | sdio_release_host(sdiodev->func[1]); |
| 882 | |
| 883 | return bcmerror; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 884 | } |
| 885 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 886 | int brcmf_sdiod_abort(struct brcmf_sdio_dev *sdiodev, uint fn) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 887 | { |
| 888 | char t_func = (char)fn; |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 889 | brcmf_dbg(SDIO, "Enter\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 890 | |
| 891 | /* issue abort cmd52 command through F0 */ |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 892 | brcmf_sdiod_request_byte(sdiodev, SDIOH_WRITE, SDIO_FUNC_0, |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 893 | SDIO_CCCR_ABORT, &t_func); |
| 894 | |
Arend van Spriel | c320337 | 2013-04-03 12:40:44 +0200 | [diff] [blame] | 895 | brcmf_dbg(SDIO, "Exit\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 896 | return 0; |
| 897 | } |
| 898 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 899 | static int brcmf_sdiod_remove(struct brcmf_sdio_dev *sdiodev) |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 900 | { |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 901 | sdiodev->bus_if->state = BRCMF_BUS_DOWN; |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 902 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 903 | if (sdiodev->bus) { |
Arend van Spriel | 82d7f3c | 2013-12-12 11:59:04 +0100 | [diff] [blame^] | 904 | brcmf_sdio_disconnect(sdiodev->bus); |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 905 | sdiodev->bus = NULL; |
| 906 | } |
| 907 | |
| 908 | /* Disable Function 2 */ |
| 909 | sdio_claim_host(sdiodev->func[2]); |
| 910 | sdio_disable_func(sdiodev->func[2]); |
| 911 | sdio_release_host(sdiodev->func[2]); |
| 912 | |
| 913 | /* Disable Function 1 */ |
| 914 | sdio_claim_host(sdiodev->func[1]); |
| 915 | sdio_disable_func(sdiodev->func[1]); |
| 916 | sdio_release_host(sdiodev->func[1]); |
| 917 | |
| 918 | sdiodev->sbwad = 0; |
| 919 | |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | static int brcmf_sdiod_probe(struct brcmf_sdio_dev *sdiodev) |
| 924 | { |
| 925 | struct sdio_func *func; |
| 926 | struct mmc_host *host; |
| 927 | uint max_blocks; |
| 928 | int ret = 0; |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 929 | |
| 930 | sdiodev->num_funcs = 2; |
| 931 | |
| 932 | sdio_claim_host(sdiodev->func[1]); |
| 933 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 934 | ret = sdio_set_block_size(sdiodev->func[1], SDIO_FUNC1_BLOCKSIZE); |
| 935 | if (ret) { |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 936 | brcmf_err("Failed to set F1 blocksize\n"); |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 937 | sdio_release_host(sdiodev->func[1]); |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 938 | goto out; |
| 939 | } |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 940 | ret = sdio_set_block_size(sdiodev->func[2], SDIO_FUNC2_BLOCKSIZE); |
| 941 | if (ret) { |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 942 | brcmf_err("Failed to set F2 blocksize\n"); |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 943 | sdio_release_host(sdiodev->func[1]); |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 944 | goto out; |
| 945 | } |
| 946 | |
Arend van Spriel | 71370eb | 2013-12-12 11:58:53 +0100 | [diff] [blame] | 947 | /* increase F2 timeout */ |
| 948 | sdiodev->func[2]->enable_timeout = SDIO_WAIT_F2RDY; |
| 949 | |
Arend van Spriel | 463c30b | 2013-12-12 11:58:52 +0100 | [diff] [blame] | 950 | /* Enable Function 1 */ |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 951 | ret = sdio_enable_func(sdiodev->func[1]); |
| 952 | sdio_release_host(sdiodev->func[1]); |
| 953 | if (ret) { |
| 954 | brcmf_err("Failed to enable F1: err=%d\n", ret); |
Arend van Spriel | 463c30b | 2013-12-12 11:58:52 +0100 | [diff] [blame] | 955 | goto out; |
| 956 | } |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 957 | |
| 958 | /* |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 959 | * determine host related variables after brcmf_sdiod_probe() |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 960 | * as func->cur_blksize is properly set and F2 init has been |
| 961 | * completed successfully. |
| 962 | */ |
| 963 | func = sdiodev->func[2]; |
| 964 | host = func->card->host; |
| 965 | sdiodev->sg_support = host->max_segs > 1; |
| 966 | max_blocks = min_t(uint, host->max_blk_count, 511u); |
| 967 | sdiodev->max_request_size = min_t(uint, host->max_req_size, |
| 968 | max_blocks * func->cur_blksize); |
| 969 | sdiodev->max_segment_count = min_t(uint, host->max_segs, |
| 970 | SG_MAX_SINGLE_ALLOC); |
| 971 | sdiodev->max_segment_size = host->max_seg_size; |
Arend van Spriel | e49b06b | 2013-12-12 11:58:51 +0100 | [diff] [blame] | 972 | |
Arend van Spriel | e49b06b | 2013-12-12 11:58:51 +0100 | [diff] [blame] | 973 | /* try to attach to the target device */ |
Arend van Spriel | 82d7f3c | 2013-12-12 11:59:04 +0100 | [diff] [blame^] | 974 | sdiodev->bus = brcmf_sdio_probe(sdiodev); |
Arend van Spriel | e49b06b | 2013-12-12 11:58:51 +0100 | [diff] [blame] | 975 | if (!sdiodev->bus) { |
Arend van Spriel | e49b06b | 2013-12-12 11:58:51 +0100 | [diff] [blame] | 976 | ret = -ENODEV; |
| 977 | goto out; |
| 978 | } |
| 979 | |
| 980 | out: |
| 981 | if (ret) |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 982 | brcmf_sdiod_remove(sdiodev); |
Arend van Spriel | e49b06b | 2013-12-12 11:58:51 +0100 | [diff] [blame] | 983 | |
| 984 | return ret; |
| 985 | } |
| 986 | |
| 987 | /* devices we support, null terminated */ |
| 988 | static const struct sdio_device_id brcmf_sdmmc_ids[] = { |
| 989 | {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43143)}, |
| 990 | {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_43241)}, |
| 991 | {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4329)}, |
| 992 | {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4330)}, |
| 993 | {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, SDIO_DEVICE_ID_BROADCOM_4334)}, |
| 994 | {SDIO_DEVICE(SDIO_VENDOR_ID_BROADCOM, |
| 995 | SDIO_DEVICE_ID_BROADCOM_4335_4339)}, |
| 996 | { /* end: all zeroes */ }, |
| 997 | }; |
| 998 | MODULE_DEVICE_TABLE(sdio, brcmf_sdmmc_ids); |
| 999 | |
| 1000 | static struct brcmfmac_sdio_platform_data *brcmfmac_sdio_pdata; |
| 1001 | |
| 1002 | |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 1003 | static int brcmf_ops_sdio_probe(struct sdio_func *func, |
| 1004 | const struct sdio_device_id *id) |
| 1005 | { |
| 1006 | int err; |
| 1007 | struct brcmf_sdio_dev *sdiodev; |
| 1008 | struct brcmf_bus *bus_if; |
| 1009 | |
| 1010 | brcmf_dbg(SDIO, "Enter\n"); |
| 1011 | brcmf_dbg(SDIO, "Class=%x\n", func->class); |
| 1012 | brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor); |
| 1013 | brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device); |
| 1014 | brcmf_dbg(SDIO, "Function#: %d\n", func->num); |
| 1015 | |
| 1016 | /* Consume func num 1 but dont do anything with it. */ |
| 1017 | if (func->num == 1) |
| 1018 | return 0; |
| 1019 | |
| 1020 | /* Ignore anything but func 2 */ |
| 1021 | if (func->num != 2) |
| 1022 | return -ENODEV; |
| 1023 | |
| 1024 | bus_if = kzalloc(sizeof(struct brcmf_bus), GFP_KERNEL); |
| 1025 | if (!bus_if) |
| 1026 | return -ENOMEM; |
| 1027 | sdiodev = kzalloc(sizeof(struct brcmf_sdio_dev), GFP_KERNEL); |
| 1028 | if (!sdiodev) { |
| 1029 | kfree(bus_if); |
| 1030 | return -ENOMEM; |
| 1031 | } |
| 1032 | |
| 1033 | sdiodev->func[0] = func->card->sdio_func[0]; |
| 1034 | sdiodev->func[1] = func->card->sdio_func[0]; |
| 1035 | sdiodev->func[2] = func; |
| 1036 | |
| 1037 | sdiodev->bus_if = bus_if; |
| 1038 | bus_if->bus_priv.sdio = sdiodev; |
Hante Meuleman | 943258b | 2013-12-12 11:59:02 +0100 | [diff] [blame] | 1039 | bus_if->proto_type = BRCMF_PROTO_BCDC; |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 1040 | dev_set_drvdata(&func->dev, bus_if); |
| 1041 | dev_set_drvdata(&sdiodev->func[1]->dev, bus_if); |
| 1042 | sdiodev->dev = &sdiodev->func[1]->dev; |
| 1043 | sdiodev->pdata = brcmfmac_sdio_pdata; |
| 1044 | |
| 1045 | atomic_set(&sdiodev->suspend, false); |
| 1046 | init_waitqueue_head(&sdiodev->request_byte_wait); |
| 1047 | init_waitqueue_head(&sdiodev->request_word_wait); |
| 1048 | init_waitqueue_head(&sdiodev->request_buffer_wait); |
| 1049 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 1050 | brcmf_dbg(SDIO, "F2 found, calling brcmf_sdiod_probe...\n"); |
| 1051 | err = brcmf_sdiod_probe(sdiodev); |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 1052 | if (err) { |
| 1053 | brcmf_err("F2 error, probe failed %d...\n", err); |
| 1054 | goto fail; |
| 1055 | } |
| 1056 | |
| 1057 | brcmf_dbg(SDIO, "F2 init completed...\n"); |
| 1058 | return 0; |
| 1059 | |
| 1060 | fail: |
| 1061 | dev_set_drvdata(&func->dev, NULL); |
| 1062 | dev_set_drvdata(&sdiodev->func[1]->dev, NULL); |
| 1063 | kfree(sdiodev); |
| 1064 | kfree(bus_if); |
| 1065 | return err; |
| 1066 | } |
| 1067 | |
| 1068 | static void brcmf_ops_sdio_remove(struct sdio_func *func) |
| 1069 | { |
| 1070 | struct brcmf_bus *bus_if; |
| 1071 | struct brcmf_sdio_dev *sdiodev; |
| 1072 | |
| 1073 | brcmf_dbg(SDIO, "Enter\n"); |
| 1074 | brcmf_dbg(SDIO, "sdio vendor ID: 0x%04x\n", func->vendor); |
| 1075 | brcmf_dbg(SDIO, "sdio device ID: 0x%04x\n", func->device); |
| 1076 | brcmf_dbg(SDIO, "Function: %d\n", func->num); |
| 1077 | |
| 1078 | if (func->num != 1 && func->num != 2) |
| 1079 | return; |
| 1080 | |
| 1081 | bus_if = dev_get_drvdata(&func->dev); |
| 1082 | if (bus_if) { |
| 1083 | sdiodev = bus_if->bus_priv.sdio; |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 1084 | brcmf_sdiod_remove(sdiodev); |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 1085 | |
| 1086 | dev_set_drvdata(&sdiodev->func[1]->dev, NULL); |
| 1087 | dev_set_drvdata(&sdiodev->func[2]->dev, NULL); |
| 1088 | |
| 1089 | kfree(bus_if); |
| 1090 | kfree(sdiodev); |
| 1091 | } |
| 1092 | |
| 1093 | brcmf_dbg(SDIO, "Exit\n"); |
| 1094 | } |
| 1095 | |
| 1096 | #ifdef CONFIG_PM_SLEEP |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 1097 | static int brcmf_ops_sdio_suspend(struct device *dev) |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 1098 | { |
| 1099 | mmc_pm_flag_t sdio_flags; |
| 1100 | struct brcmf_bus *bus_if = dev_get_drvdata(dev); |
| 1101 | struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; |
| 1102 | int ret = 0; |
| 1103 | |
| 1104 | brcmf_dbg(SDIO, "\n"); |
| 1105 | |
| 1106 | atomic_set(&sdiodev->suspend, true); |
| 1107 | |
| 1108 | sdio_flags = sdio_get_host_pm_caps(sdiodev->func[1]); |
| 1109 | if (!(sdio_flags & MMC_PM_KEEP_POWER)) { |
| 1110 | brcmf_err("Host can't keep power while suspended\n"); |
| 1111 | return -EINVAL; |
| 1112 | } |
| 1113 | |
| 1114 | ret = sdio_set_host_pm_flags(sdiodev->func[1], MMC_PM_KEEP_POWER); |
| 1115 | if (ret) { |
| 1116 | brcmf_err("Failed to set pm_flags\n"); |
| 1117 | return ret; |
| 1118 | } |
| 1119 | |
Arend van Spriel | 82d7f3c | 2013-12-12 11:59:04 +0100 | [diff] [blame^] | 1120 | brcmf_sdio_wd_timer(sdiodev->bus, 0); |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 1121 | |
| 1122 | return ret; |
| 1123 | } |
| 1124 | |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 1125 | static int brcmf_ops_sdio_resume(struct device *dev) |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 1126 | { |
| 1127 | struct brcmf_bus *bus_if = dev_get_drvdata(dev); |
| 1128 | struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; |
| 1129 | |
Arend van Spriel | 82d7f3c | 2013-12-12 11:59:04 +0100 | [diff] [blame^] | 1130 | brcmf_sdio_wd_timer(sdiodev->bus, BRCMF_WD_POLL_MS); |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 1131 | atomic_set(&sdiodev->suspend, false); |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | static const struct dev_pm_ops brcmf_sdio_pm_ops = { |
Arend van Spriel | a39be27 | 2013-12-12 11:58:58 +0100 | [diff] [blame] | 1136 | .suspend = brcmf_ops_sdio_suspend, |
| 1137 | .resume = brcmf_ops_sdio_resume, |
Arend van Spriel | e2dc9ee | 2013-12-12 11:58:50 +0100 | [diff] [blame] | 1138 | }; |
| 1139 | #endif /* CONFIG_PM_SLEEP */ |
| 1140 | |
| 1141 | static struct sdio_driver brcmf_sdmmc_driver = { |
| 1142 | .probe = brcmf_ops_sdio_probe, |
| 1143 | .remove = brcmf_ops_sdio_remove, |
| 1144 | .name = BRCMFMAC_SDIO_PDATA_NAME, |
| 1145 | .id_table = brcmf_sdmmc_ids, |
| 1146 | #ifdef CONFIG_PM_SLEEP |
| 1147 | .drv = { |
| 1148 | .pm = &brcmf_sdio_pm_ops, |
| 1149 | }, |
| 1150 | #endif /* CONFIG_PM_SLEEP */ |
| 1151 | }; |
| 1152 | |
| 1153 | static int brcmf_sdio_pd_probe(struct platform_device *pdev) |
| 1154 | { |
| 1155 | brcmf_dbg(SDIO, "Enter\n"); |
| 1156 | |
| 1157 | brcmfmac_sdio_pdata = dev_get_platdata(&pdev->dev); |
| 1158 | |
| 1159 | if (brcmfmac_sdio_pdata->power_on) |
| 1160 | brcmfmac_sdio_pdata->power_on(); |
| 1161 | |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | static int brcmf_sdio_pd_remove(struct platform_device *pdev) |
| 1166 | { |
| 1167 | brcmf_dbg(SDIO, "Enter\n"); |
| 1168 | |
| 1169 | if (brcmfmac_sdio_pdata->power_off) |
| 1170 | brcmfmac_sdio_pdata->power_off(); |
| 1171 | |
| 1172 | sdio_unregister_driver(&brcmf_sdmmc_driver); |
| 1173 | |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
| 1177 | static struct platform_driver brcmf_sdio_pd = { |
| 1178 | .remove = brcmf_sdio_pd_remove, |
| 1179 | .driver = { |
| 1180 | .name = BRCMFMAC_SDIO_PDATA_NAME, |
| 1181 | .owner = THIS_MODULE, |
| 1182 | } |
| 1183 | }; |
| 1184 | |
| 1185 | void brcmf_sdio_register(void) |
| 1186 | { |
| 1187 | int ret; |
| 1188 | |
| 1189 | ret = sdio_register_driver(&brcmf_sdmmc_driver); |
| 1190 | if (ret) |
| 1191 | brcmf_err("sdio_register_driver failed: %d\n", ret); |
| 1192 | } |
| 1193 | |
| 1194 | void brcmf_sdio_exit(void) |
| 1195 | { |
| 1196 | brcmf_dbg(SDIO, "Enter\n"); |
| 1197 | |
| 1198 | if (brcmfmac_sdio_pdata) |
| 1199 | platform_driver_unregister(&brcmf_sdio_pd); |
| 1200 | else |
| 1201 | sdio_unregister_driver(&brcmf_sdmmc_driver); |
| 1202 | } |
| 1203 | |
| 1204 | void __init brcmf_sdio_init(void) |
| 1205 | { |
| 1206 | int ret; |
| 1207 | |
| 1208 | brcmf_dbg(SDIO, "Enter\n"); |
| 1209 | |
| 1210 | ret = platform_driver_probe(&brcmf_sdio_pd, brcmf_sdio_pd_probe); |
| 1211 | if (ret == -ENODEV) |
| 1212 | brcmf_dbg(SDIO, "No platform data available.\n"); |
| 1213 | } |