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