Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Marvell BT-over-SDIO driver: SDIO interface related functions. |
| 3 | * |
| 4 | * Copyright (C) 2009, Marvell International Ltd. |
| 5 | * |
| 6 | * This software file (the "File") is distributed by Marvell International |
| 7 | * Ltd. under the terms of the GNU General Public License Version 2, June 1991 |
| 8 | * (the "License"). You may use, redistribute and/or modify this File in |
| 9 | * accordance with the terms and conditions of the License, a copy of which |
| 10 | * is available by writing to the Free Software Foundation, Inc., |
| 11 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the |
| 12 | * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. |
| 13 | * |
| 14 | * |
| 15 | * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE |
| 17 | * ARE EXPRESSLY DISCLAIMED. The License provides additional details about |
| 18 | * this warranty disclaimer. |
| 19 | **/ |
| 20 | |
| 21 | #include <linux/firmware.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 23 | |
| 24 | #include <linux/mmc/sdio_ids.h> |
| 25 | #include <linux/mmc/sdio_func.h> |
| 26 | |
| 27 | #include <net/bluetooth/bluetooth.h> |
| 28 | #include <net/bluetooth/hci_core.h> |
| 29 | |
| 30 | #include "btmrvl_drv.h" |
| 31 | #include "btmrvl_sdio.h" |
| 32 | |
| 33 | #define VERSION "1.0" |
| 34 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 35 | /* The btmrvl_sdio_remove() callback function is called |
| 36 | * when user removes this module from kernel space or ejects |
| 37 | * the card from the slot. The driver handles these 2 cases |
| 38 | * differently. |
| 39 | * If the user is removing the module, a MODULE_SHUTDOWN_REQ |
| 40 | * command is sent to firmware and interrupt will be disabled. |
| 41 | * If the card is removed, there is no need to send command |
| 42 | * or disable interrupt. |
| 43 | * |
| 44 | * The variable 'user_rmmod' is used to distinguish these two |
| 45 | * scenarios. This flag is initialized as FALSE in case the card |
| 46 | * is removed, and will be set to TRUE for module removal when |
| 47 | * module_exit function is called. |
| 48 | */ |
| 49 | static u8 user_rmmod; |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame^] | 50 | static u8 sdio_ireg; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 51 | |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 52 | static const struct btmrvl_sdio_device btmrvl_sdio_sd6888 = { |
| 53 | .helper = "sd8688_helper.bin", |
| 54 | .firmware = "sd8688.bin", |
| 55 | }; |
| 56 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 57 | static const struct sdio_device_id btmrvl_sdio_ids[] = { |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 58 | /* Marvell SD8688 Bluetooth device */ |
| 59 | { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105), |
| 60 | .driver_data = (unsigned long) &btmrvl_sdio_sd6888 }, |
| 61 | |
| 62 | { } /* Terminating entry */ |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids); |
| 66 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 67 | static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card) |
| 68 | { |
| 69 | u8 reg; |
| 70 | int ret; |
| 71 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 72 | reg = sdio_readb(card->func, CARD_RX_UNIT_REG, &ret); |
| 73 | if (!ret) |
| 74 | card->rx_unit = reg; |
| 75 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 76 | return ret; |
| 77 | } |
| 78 | |
| 79 | static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat) |
| 80 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 81 | u8 fws0, fws1; |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 82 | int ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 83 | |
| 84 | *dat = 0; |
| 85 | |
| 86 | fws0 = sdio_readb(card->func, CARD_FW_STATUS0_REG, &ret); |
Kulikov Vasiliy | 278be27 | 2010-07-05 12:01:22 +0400 | [diff] [blame] | 87 | if (ret) |
| 88 | return -EIO; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 89 | |
Kulikov Vasiliy | 278be27 | 2010-07-05 12:01:22 +0400 | [diff] [blame] | 90 | fws1 = sdio_readb(card->func, CARD_FW_STATUS1_REG, &ret); |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 91 | if (ret) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 92 | return -EIO; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 93 | |
| 94 | *dat = (((u16) fws1) << 8) | fws0; |
| 95 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat) |
| 100 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 101 | u8 reg; |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 102 | int ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 103 | |
| 104 | reg = sdio_readb(card->func, CARD_RX_LEN_REG, &ret); |
| 105 | if (!ret) |
| 106 | *dat = (u16) reg << card->rx_unit; |
| 107 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 112 | u8 mask) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 113 | { |
| 114 | int ret; |
| 115 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 116 | sdio_writeb(card->func, mask, HOST_INT_MASK_REG, &ret); |
| 117 | if (ret) { |
| 118 | BT_ERR("Unable to enable the host interrupt!"); |
| 119 | ret = -EIO; |
| 120 | } |
| 121 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 126 | u8 mask) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 127 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 128 | u8 host_int_mask; |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 129 | int ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 130 | |
| 131 | host_int_mask = sdio_readb(card->func, HOST_INT_MASK_REG, &ret); |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 132 | if (ret) |
| 133 | return -EIO; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 134 | |
| 135 | host_int_mask &= ~mask; |
| 136 | |
| 137 | sdio_writeb(card->func, host_int_mask, HOST_INT_MASK_REG, &ret); |
| 138 | if (ret < 0) { |
| 139 | BT_ERR("Unable to disable the host interrupt!"); |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 140 | return -EIO; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 143 | return 0; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits) |
| 147 | { |
| 148 | unsigned int tries; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 149 | u8 status; |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 150 | int ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 151 | |
| 152 | for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) { |
| 153 | status = sdio_readb(card->func, CARD_STATUS_REG, &ret); |
| 154 | if (ret) |
| 155 | goto failed; |
| 156 | if ((status & bits) == bits) |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 157 | return ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 158 | |
| 159 | udelay(1); |
| 160 | } |
| 161 | |
| 162 | ret = -ETIMEDOUT; |
| 163 | |
| 164 | failed: |
| 165 | BT_ERR("FAILED! ret=%d", ret); |
| 166 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 171 | int pollnum) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 172 | { |
| 173 | int ret = -ETIMEDOUT; |
| 174 | u16 firmwarestat; |
| 175 | unsigned int tries; |
| 176 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 177 | /* Wait for firmware to become ready */ |
| 178 | for (tries = 0; tries < pollnum; tries++) { |
| 179 | if (btmrvl_sdio_read_fw_status(card, &firmwarestat) < 0) |
| 180 | continue; |
| 181 | |
| 182 | if (firmwarestat == FIRMWARE_READY) { |
| 183 | ret = 0; |
| 184 | break; |
| 185 | } else { |
| 186 | msleep(10); |
| 187 | } |
| 188 | } |
| 189 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card) |
| 194 | { |
| 195 | const struct firmware *fw_helper = NULL; |
| 196 | const u8 *helper = NULL; |
| 197 | int ret; |
| 198 | void *tmphlprbuf = NULL; |
| 199 | int tmphlprbufsz, hlprblknow, helperlen; |
| 200 | u8 *helperbuf; |
| 201 | u32 tx_len; |
| 202 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 203 | ret = request_firmware(&fw_helper, card->helper, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 204 | &card->func->dev); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 205 | if ((ret < 0) || !fw_helper) { |
| 206 | BT_ERR("request_firmware(helper) failed, error code = %d", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 207 | ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 208 | ret = -ENOENT; |
| 209 | goto done; |
| 210 | } |
| 211 | |
| 212 | helper = fw_helper->data; |
| 213 | helperlen = fw_helper->size; |
| 214 | |
| 215 | BT_DBG("Downloading helper image (%d bytes), block size %d bytes", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 216 | helperlen, SDIO_BLOCK_SIZE); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 217 | |
| 218 | tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN); |
| 219 | |
| 220 | tmphlprbuf = kmalloc(tmphlprbufsz, GFP_KERNEL); |
| 221 | if (!tmphlprbuf) { |
| 222 | BT_ERR("Unable to allocate buffer for helper." |
| 223 | " Terminating download"); |
| 224 | ret = -ENOMEM; |
| 225 | goto done; |
| 226 | } |
| 227 | |
| 228 | memset(tmphlprbuf, 0, tmphlprbufsz); |
| 229 | |
| 230 | helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN); |
| 231 | |
| 232 | /* Perform helper data transfer */ |
| 233 | tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE) |
| 234 | - SDIO_HEADER_LEN; |
| 235 | hlprblknow = 0; |
| 236 | |
| 237 | do { |
| 238 | ret = btmrvl_sdio_poll_card_status(card, |
| 239 | CARD_IO_READY | DN_LD_CARD_RDY); |
| 240 | if (ret < 0) { |
| 241 | BT_ERR("Helper download poll status timeout @ %d", |
| 242 | hlprblknow); |
| 243 | goto done; |
| 244 | } |
| 245 | |
| 246 | /* Check if there is more data? */ |
| 247 | if (hlprblknow >= helperlen) |
| 248 | break; |
| 249 | |
| 250 | if (helperlen - hlprblknow < tx_len) |
| 251 | tx_len = helperlen - hlprblknow; |
| 252 | |
| 253 | /* Little-endian */ |
| 254 | helperbuf[0] = ((tx_len & 0x000000ff) >> 0); |
| 255 | helperbuf[1] = ((tx_len & 0x0000ff00) >> 8); |
| 256 | helperbuf[2] = ((tx_len & 0x00ff0000) >> 16); |
| 257 | helperbuf[3] = ((tx_len & 0xff000000) >> 24); |
| 258 | |
| 259 | memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow], |
| 260 | tx_len); |
| 261 | |
| 262 | /* Now send the data */ |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 263 | ret = sdio_writesb(card->func, card->ioport, helperbuf, |
| 264 | FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 265 | if (ret < 0) { |
| 266 | BT_ERR("IO error during helper download @ %d", |
| 267 | hlprblknow); |
| 268 | goto done; |
| 269 | } |
| 270 | |
| 271 | hlprblknow += tx_len; |
| 272 | } while (true); |
| 273 | |
| 274 | BT_DBG("Transferring helper image EOF block"); |
| 275 | |
| 276 | memset(helperbuf, 0x0, SDIO_BLOCK_SIZE); |
| 277 | |
| 278 | ret = sdio_writesb(card->func, card->ioport, helperbuf, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 279 | SDIO_BLOCK_SIZE); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 280 | if (ret < 0) { |
| 281 | BT_ERR("IO error in writing helper image EOF block"); |
| 282 | goto done; |
| 283 | } |
| 284 | |
| 285 | ret = 0; |
| 286 | |
| 287 | done: |
| 288 | kfree(tmphlprbuf); |
| 289 | if (fw_helper) |
| 290 | release_firmware(fw_helper); |
| 291 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card) |
| 296 | { |
| 297 | const struct firmware *fw_firmware = NULL; |
| 298 | const u8 *firmware = NULL; |
| 299 | int firmwarelen, tmpfwbufsz, ret; |
| 300 | unsigned int tries, offset; |
| 301 | u8 base0, base1; |
| 302 | void *tmpfwbuf = NULL; |
| 303 | u8 *fwbuf; |
| 304 | u16 len; |
| 305 | int txlen = 0, tx_blocks = 0, count = 0; |
| 306 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 307 | ret = request_firmware(&fw_firmware, card->firmware, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 308 | &card->func->dev); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 309 | if ((ret < 0) || !fw_firmware) { |
| 310 | BT_ERR("request_firmware(firmware) failed, error code = %d", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 311 | ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 312 | ret = -ENOENT; |
| 313 | goto done; |
| 314 | } |
| 315 | |
| 316 | firmware = fw_firmware->data; |
| 317 | firmwarelen = fw_firmware->size; |
| 318 | |
| 319 | BT_DBG("Downloading FW image (%d bytes)", firmwarelen); |
| 320 | |
| 321 | tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN); |
| 322 | tmpfwbuf = kmalloc(tmpfwbufsz, GFP_KERNEL); |
| 323 | if (!tmpfwbuf) { |
| 324 | BT_ERR("Unable to allocate buffer for firmware." |
| 325 | " Terminating download"); |
| 326 | ret = -ENOMEM; |
| 327 | goto done; |
| 328 | } |
| 329 | |
| 330 | memset(tmpfwbuf, 0, tmpfwbufsz); |
| 331 | |
| 332 | /* Ensure aligned firmware buffer */ |
| 333 | fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN); |
| 334 | |
| 335 | /* Perform firmware data transfer */ |
| 336 | offset = 0; |
| 337 | do { |
| 338 | ret = btmrvl_sdio_poll_card_status(card, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 339 | CARD_IO_READY | DN_LD_CARD_RDY); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 340 | if (ret < 0) { |
| 341 | BT_ERR("FW download with helper poll status" |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 342 | " timeout @ %d", offset); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 343 | goto done; |
| 344 | } |
| 345 | |
| 346 | /* Check if there is more data ? */ |
| 347 | if (offset >= firmwarelen) |
| 348 | break; |
| 349 | |
| 350 | for (tries = 0; tries < MAX_POLL_TRIES; tries++) { |
| 351 | base0 = sdio_readb(card->func, |
| 352 | SQ_READ_BASE_ADDRESS_A0_REG, &ret); |
| 353 | if (ret) { |
| 354 | BT_ERR("BASE0 register read failed:" |
| 355 | " base0 = 0x%04X(%d)." |
| 356 | " Terminating download", |
| 357 | base0, base0); |
| 358 | ret = -EIO; |
| 359 | goto done; |
| 360 | } |
| 361 | base1 = sdio_readb(card->func, |
| 362 | SQ_READ_BASE_ADDRESS_A1_REG, &ret); |
| 363 | if (ret) { |
| 364 | BT_ERR("BASE1 register read failed:" |
| 365 | " base1 = 0x%04X(%d)." |
| 366 | " Terminating download", |
| 367 | base1, base1); |
| 368 | ret = -EIO; |
| 369 | goto done; |
| 370 | } |
| 371 | |
| 372 | len = (((u16) base1) << 8) | base0; |
| 373 | if (len) |
| 374 | break; |
| 375 | |
| 376 | udelay(10); |
| 377 | } |
| 378 | |
| 379 | if (!len) |
| 380 | break; |
| 381 | else if (len > BTM_UPLD_SIZE) { |
| 382 | BT_ERR("FW download failure @%d, invalid length %d", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 383 | offset, len); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 384 | ret = -EINVAL; |
| 385 | goto done; |
| 386 | } |
| 387 | |
| 388 | txlen = len; |
| 389 | |
| 390 | if (len & BIT(0)) { |
| 391 | count++; |
| 392 | if (count > MAX_WRITE_IOMEM_RETRY) { |
| 393 | BT_ERR("FW download failure @%d, " |
| 394 | "over max retry count", offset); |
| 395 | ret = -EIO; |
| 396 | goto done; |
| 397 | } |
| 398 | BT_ERR("FW CRC error indicated by the helper: " |
| 399 | "len = 0x%04X, txlen = %d", len, txlen); |
| 400 | len &= ~BIT(0); |
| 401 | /* Set txlen to 0 so as to resend from same offset */ |
| 402 | txlen = 0; |
| 403 | } else { |
| 404 | count = 0; |
| 405 | |
| 406 | /* Last block ? */ |
| 407 | if (firmwarelen - offset < txlen) |
| 408 | txlen = firmwarelen - offset; |
| 409 | |
| 410 | tx_blocks = |
| 411 | (txlen + SDIO_BLOCK_SIZE - 1) / SDIO_BLOCK_SIZE; |
| 412 | |
| 413 | memcpy(fwbuf, &firmware[offset], txlen); |
| 414 | } |
| 415 | |
| 416 | ret = sdio_writesb(card->func, card->ioport, fwbuf, |
| 417 | tx_blocks * SDIO_BLOCK_SIZE); |
| 418 | |
| 419 | if (ret < 0) { |
| 420 | BT_ERR("FW download, writesb(%d) failed @%d", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 421 | count, offset); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 422 | sdio_writeb(card->func, HOST_CMD53_FIN, CONFIG_REG, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 423 | &ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 424 | if (ret) |
| 425 | BT_ERR("writeb failed (CFG)"); |
| 426 | } |
| 427 | |
| 428 | offset += txlen; |
| 429 | } while (true); |
| 430 | |
| 431 | BT_DBG("FW download over, size %d bytes", offset); |
| 432 | |
| 433 | ret = 0; |
| 434 | |
| 435 | done: |
| 436 | kfree(tmpfwbuf); |
| 437 | |
| 438 | if (fw_firmware) |
| 439 | release_firmware(fw_firmware); |
| 440 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 441 | return ret; |
| 442 | } |
| 443 | |
| 444 | static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv) |
| 445 | { |
| 446 | u16 buf_len = 0; |
| 447 | int ret, buf_block_len, blksz; |
| 448 | struct sk_buff *skb = NULL; |
| 449 | u32 type; |
| 450 | u8 *payload = NULL; |
| 451 | struct hci_dev *hdev = priv->btmrvl_dev.hcidev; |
| 452 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 453 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 454 | if (!card || !card->func) { |
| 455 | BT_ERR("card or function is NULL!"); |
| 456 | ret = -EINVAL; |
| 457 | goto exit; |
| 458 | } |
| 459 | |
| 460 | /* Read the length of data to be transferred */ |
| 461 | ret = btmrvl_sdio_read_rx_len(card, &buf_len); |
| 462 | if (ret < 0) { |
| 463 | BT_ERR("read rx_len failed"); |
| 464 | ret = -EIO; |
| 465 | goto exit; |
| 466 | } |
| 467 | |
| 468 | blksz = SDIO_BLOCK_SIZE; |
| 469 | buf_block_len = (buf_len + blksz - 1) / blksz; |
| 470 | |
| 471 | if (buf_len <= SDIO_HEADER_LEN |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 472 | || (buf_block_len * blksz) > ALLOC_BUF_SIZE) { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 473 | BT_ERR("invalid packet length: %d", buf_len); |
| 474 | ret = -EINVAL; |
| 475 | goto exit; |
| 476 | } |
| 477 | |
| 478 | /* Allocate buffer */ |
| 479 | skb = bt_skb_alloc(buf_block_len * blksz + BTSDIO_DMA_ALIGN, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 480 | GFP_ATOMIC); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 481 | if (skb == NULL) { |
| 482 | BT_ERR("No free skb"); |
| 483 | goto exit; |
| 484 | } |
| 485 | |
Bing Zhao | 3318b23 | 2009-07-08 11:44:14 -0700 | [diff] [blame] | 486 | if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) { |
| 487 | skb_put(skb, (unsigned long) skb->data & |
| 488 | (BTSDIO_DMA_ALIGN - 1)); |
| 489 | skb_pull(skb, (unsigned long) skb->data & |
| 490 | (BTSDIO_DMA_ALIGN - 1)); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Bing Zhao | 3318b23 | 2009-07-08 11:44:14 -0700 | [diff] [blame] | 493 | payload = skb->data; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 494 | |
| 495 | ret = sdio_readsb(card->func, payload, card->ioport, |
| 496 | buf_block_len * blksz); |
| 497 | if (ret < 0) { |
| 498 | BT_ERR("readsb failed: %d", ret); |
| 499 | ret = -EIO; |
| 500 | goto exit; |
| 501 | } |
| 502 | |
| 503 | /* This is SDIO specific header length: byte[2][1][0], type: byte[3] |
| 504 | * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor) |
| 505 | */ |
| 506 | |
| 507 | buf_len = payload[0]; |
| 508 | buf_len |= (u16) payload[1] << 8; |
| 509 | type = payload[3]; |
| 510 | |
| 511 | switch (type) { |
| 512 | case HCI_ACLDATA_PKT: |
| 513 | case HCI_SCODATA_PKT: |
| 514 | case HCI_EVENT_PKT: |
| 515 | bt_cb(skb)->pkt_type = type; |
| 516 | skb->dev = (void *)hdev; |
| 517 | skb_put(skb, buf_len); |
| 518 | skb_pull(skb, SDIO_HEADER_LEN); |
| 519 | |
| 520 | if (type == HCI_EVENT_PKT) |
| 521 | btmrvl_check_evtpkt(priv, skb); |
| 522 | |
| 523 | hci_recv_frame(skb); |
| 524 | hdev->stat.byte_rx += buf_len; |
| 525 | break; |
| 526 | |
| 527 | case MRVL_VENDOR_PKT: |
| 528 | bt_cb(skb)->pkt_type = HCI_VENDOR_PKT; |
| 529 | skb->dev = (void *)hdev; |
| 530 | skb_put(skb, buf_len); |
| 531 | skb_pull(skb, SDIO_HEADER_LEN); |
| 532 | |
| 533 | if (btmrvl_process_event(priv, skb)) |
| 534 | hci_recv_frame(skb); |
| 535 | |
| 536 | hdev->stat.byte_rx += buf_len; |
| 537 | break; |
| 538 | |
| 539 | default: |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 540 | BT_ERR("Unknown packet type:%d", type); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 541 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, payload, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 542 | blksz * buf_block_len); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 543 | |
| 544 | kfree_skb(skb); |
| 545 | skb = NULL; |
| 546 | break; |
| 547 | } |
| 548 | |
| 549 | exit: |
| 550 | if (ret) { |
| 551 | hdev->stat.err_rx++; |
| 552 | if (skb) |
| 553 | kfree_skb(skb); |
| 554 | } |
| 555 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 556 | return ret; |
| 557 | } |
| 558 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame^] | 559 | static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 560 | { |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame^] | 561 | ulong flags; |
| 562 | u8 ireg; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 563 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 564 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame^] | 565 | spin_lock_irqsave(&priv->driver_lock, flags); |
| 566 | ireg = sdio_ireg; |
| 567 | sdio_ireg = 0; |
| 568 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 569 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame^] | 570 | sdio_claim_host(card->func); |
| 571 | if (ireg & DN_LD_HOST_INT_STATUS) { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 572 | if (priv->btmrvl_dev.tx_dnld_rdy) |
| 573 | BT_DBG("tx_done already received: " |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame^] | 574 | " int_status=0x%x", ireg); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 575 | else |
| 576 | priv->btmrvl_dev.tx_dnld_rdy = true; |
| 577 | } |
| 578 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame^] | 579 | if (ireg & UP_LD_HOST_INT_STATUS) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 580 | btmrvl_sdio_card_to_host(priv); |
| 581 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame^] | 582 | sdio_release_host(card->func); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 583 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame^] | 584 | return 0; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | static void btmrvl_sdio_interrupt(struct sdio_func *func) |
| 588 | { |
| 589 | struct btmrvl_private *priv; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 590 | struct btmrvl_sdio_card *card; |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame^] | 591 | ulong flags; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 592 | u8 ireg = 0; |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame^] | 593 | int ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 594 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 595 | card = sdio_get_drvdata(func); |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame^] | 596 | if (!card || !card->priv) { |
| 597 | BT_ERR("sbi_interrupt(%p) card or priv is " |
| 598 | "NULL, card=%p\n", func, card); |
| 599 | return; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 600 | } |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame^] | 601 | |
| 602 | priv = card->priv; |
| 603 | |
| 604 | ireg = sdio_readb(card->func, HOST_INTSTATUS_REG, &ret); |
| 605 | if (ret) { |
| 606 | BT_ERR("sdio_readb: read int status register failed"); |
| 607 | return; |
| 608 | } |
| 609 | |
| 610 | if (ireg != 0) { |
| 611 | /* |
| 612 | * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS |
| 613 | * Clear the interrupt status register and re-enable the |
| 614 | * interrupt. |
| 615 | */ |
| 616 | BT_DBG("ireg = 0x%x", ireg); |
| 617 | |
| 618 | sdio_writeb(card->func, ~(ireg) & (DN_LD_HOST_INT_STATUS | |
| 619 | UP_LD_HOST_INT_STATUS), |
| 620 | HOST_INTSTATUS_REG, &ret); |
| 621 | if (ret) { |
| 622 | BT_ERR("sdio_writeb: clear int status register failed"); |
| 623 | return; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | spin_lock_irqsave(&priv->driver_lock, flags); |
| 628 | sdio_ireg |= ireg; |
| 629 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
| 630 | |
| 631 | btmrvl_interrupt(priv); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card) |
| 635 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 636 | struct sdio_func *func; |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 637 | u8 reg; |
| 638 | int ret = 0; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 639 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 640 | if (!card || !card->func) { |
| 641 | BT_ERR("Error: card or function is NULL!"); |
| 642 | ret = -EINVAL; |
| 643 | goto failed; |
| 644 | } |
| 645 | |
| 646 | func = card->func; |
| 647 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 648 | sdio_claim_host(func); |
| 649 | |
| 650 | ret = sdio_enable_func(func); |
| 651 | if (ret) { |
| 652 | BT_ERR("sdio_enable_func() failed: ret=%d", ret); |
| 653 | ret = -EIO; |
| 654 | goto release_host; |
| 655 | } |
| 656 | |
| 657 | ret = sdio_claim_irq(func, btmrvl_sdio_interrupt); |
| 658 | if (ret) { |
| 659 | BT_ERR("sdio_claim_irq failed: ret=%d", ret); |
| 660 | ret = -EIO; |
| 661 | goto disable_func; |
| 662 | } |
| 663 | |
| 664 | ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE); |
| 665 | if (ret) { |
| 666 | BT_ERR("cannot set SDIO block size"); |
| 667 | ret = -EIO; |
| 668 | goto release_irq; |
| 669 | } |
| 670 | |
| 671 | reg = sdio_readb(func, IO_PORT_0_REG, &ret); |
| 672 | if (ret < 0) { |
| 673 | ret = -EIO; |
| 674 | goto release_irq; |
| 675 | } |
| 676 | |
| 677 | card->ioport = reg; |
| 678 | |
| 679 | reg = sdio_readb(func, IO_PORT_1_REG, &ret); |
| 680 | if (ret < 0) { |
| 681 | ret = -EIO; |
| 682 | goto release_irq; |
| 683 | } |
| 684 | |
| 685 | card->ioport |= (reg << 8); |
| 686 | |
| 687 | reg = sdio_readb(func, IO_PORT_2_REG, &ret); |
| 688 | if (ret < 0) { |
| 689 | ret = -EIO; |
| 690 | goto release_irq; |
| 691 | } |
| 692 | |
| 693 | card->ioport |= (reg << 16); |
| 694 | |
| 695 | BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport); |
| 696 | |
| 697 | sdio_set_drvdata(func, card); |
| 698 | |
| 699 | sdio_release_host(func); |
| 700 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 701 | return 0; |
| 702 | |
| 703 | release_irq: |
| 704 | sdio_release_irq(func); |
| 705 | |
| 706 | disable_func: |
| 707 | sdio_disable_func(func); |
| 708 | |
| 709 | release_host: |
| 710 | sdio_release_host(func); |
| 711 | |
| 712 | failed: |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card) |
| 717 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 718 | if (card && card->func) { |
| 719 | sdio_claim_host(card->func); |
| 720 | sdio_release_irq(card->func); |
| 721 | sdio_disable_func(card->func); |
| 722 | sdio_release_host(card->func); |
| 723 | sdio_set_drvdata(card->func, NULL); |
| 724 | } |
| 725 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card) |
| 730 | { |
| 731 | int ret; |
| 732 | |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 733 | if (!card || !card->func) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 734 | return -EINVAL; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 735 | |
| 736 | sdio_claim_host(card->func); |
| 737 | |
| 738 | ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE); |
| 739 | |
| 740 | btmrvl_sdio_get_rx_unit(card); |
| 741 | |
| 742 | sdio_release_host(card->func); |
| 743 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 744 | return ret; |
| 745 | } |
| 746 | |
| 747 | static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card) |
| 748 | { |
| 749 | int ret; |
| 750 | |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 751 | if (!card || !card->func) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 752 | return -EINVAL; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 753 | |
| 754 | sdio_claim_host(card->func); |
| 755 | |
| 756 | ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE); |
| 757 | |
| 758 | sdio_release_host(card->func); |
| 759 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 760 | return ret; |
| 761 | } |
| 762 | |
| 763 | static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv, |
| 764 | u8 *payload, u16 nb) |
| 765 | { |
| 766 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 767 | int ret = 0; |
| 768 | int buf_block_len; |
| 769 | int blksz; |
| 770 | int i = 0; |
| 771 | u8 *buf = NULL; |
| 772 | void *tmpbuf = NULL; |
| 773 | int tmpbufsz; |
| 774 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 775 | if (!card || !card->func) { |
| 776 | BT_ERR("card or function is NULL!"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 777 | return -EINVAL; |
| 778 | } |
| 779 | |
| 780 | buf = payload; |
Bing Zhao | 3318b23 | 2009-07-08 11:44:14 -0700 | [diff] [blame] | 781 | if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 782 | tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN); |
Julia Lawall | 5959809 | 2009-08-06 22:05:18 +0200 | [diff] [blame] | 783 | tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL); |
| 784 | if (!tmpbuf) |
| 785 | return -ENOMEM; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 786 | buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN); |
| 787 | memcpy(buf, payload, nb); |
| 788 | } |
| 789 | |
| 790 | blksz = SDIO_BLOCK_SIZE; |
| 791 | buf_block_len = (nb + blksz - 1) / blksz; |
| 792 | |
| 793 | sdio_claim_host(card->func); |
| 794 | |
| 795 | do { |
| 796 | /* Transfer data to card */ |
| 797 | ret = sdio_writesb(card->func, card->ioport, buf, |
| 798 | buf_block_len * blksz); |
| 799 | if (ret < 0) { |
| 800 | i++; |
| 801 | BT_ERR("i=%d writesb failed: %d", i, ret); |
| 802 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 803 | payload, nb); |
| 804 | ret = -EIO; |
| 805 | if (i > MAX_WRITE_IOMEM_RETRY) |
| 806 | goto exit; |
| 807 | } |
| 808 | } while (ret); |
| 809 | |
| 810 | priv->btmrvl_dev.tx_dnld_rdy = false; |
| 811 | |
| 812 | exit: |
| 813 | sdio_release_host(card->func); |
Yoichi Yuasa | 88d1a0c | 2010-02-04 09:55:51 +0900 | [diff] [blame] | 814 | kfree(tmpbuf); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 815 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 816 | return ret; |
| 817 | } |
| 818 | |
| 819 | static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card) |
| 820 | { |
| 821 | int ret = 0; |
| 822 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 823 | if (!card || !card->func) { |
| 824 | BT_ERR("card or function is NULL!"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 825 | return -EINVAL; |
| 826 | } |
| 827 | sdio_claim_host(card->func); |
| 828 | |
| 829 | if (!btmrvl_sdio_verify_fw_download(card, 1)) { |
| 830 | BT_DBG("Firmware already downloaded!"); |
| 831 | goto done; |
| 832 | } |
| 833 | |
| 834 | ret = btmrvl_sdio_download_helper(card); |
| 835 | if (ret) { |
| 836 | BT_ERR("Failed to download helper!"); |
| 837 | ret = -EIO; |
| 838 | goto done; |
| 839 | } |
| 840 | |
| 841 | if (btmrvl_sdio_download_fw_w_helper(card)) { |
| 842 | BT_ERR("Failed to download firmware!"); |
| 843 | ret = -EIO; |
| 844 | goto done; |
| 845 | } |
| 846 | |
| 847 | if (btmrvl_sdio_verify_fw_download(card, MAX_POLL_TRIES)) { |
| 848 | BT_ERR("FW failed to be active in time!"); |
| 849 | ret = -ETIMEDOUT; |
| 850 | goto done; |
| 851 | } |
| 852 | |
| 853 | done: |
| 854 | sdio_release_host(card->func); |
| 855 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 856 | return ret; |
| 857 | } |
| 858 | |
| 859 | static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv) |
| 860 | { |
| 861 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 862 | int ret = 0; |
| 863 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 864 | if (!card || !card->func) { |
| 865 | BT_ERR("card or function is NULL!"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 866 | return -EINVAL; |
| 867 | } |
| 868 | |
| 869 | sdio_claim_host(card->func); |
| 870 | |
| 871 | sdio_writeb(card->func, HOST_POWER_UP, CONFIG_REG, &ret); |
| 872 | |
| 873 | sdio_release_host(card->func); |
| 874 | |
| 875 | BT_DBG("wake up firmware"); |
| 876 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 877 | return ret; |
| 878 | } |
| 879 | |
| 880 | static int btmrvl_sdio_probe(struct sdio_func *func, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 881 | const struct sdio_device_id *id) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 882 | { |
| 883 | int ret = 0; |
| 884 | struct btmrvl_private *priv = NULL; |
| 885 | struct btmrvl_sdio_card *card = NULL; |
| 886 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 887 | BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d", |
| 888 | id->vendor, id->device, id->class, func->num); |
| 889 | |
| 890 | card = kzalloc(sizeof(*card), GFP_KERNEL); |
| 891 | if (!card) { |
| 892 | ret = -ENOMEM; |
| 893 | goto done; |
| 894 | } |
| 895 | |
| 896 | card->func = func; |
| 897 | |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 898 | if (id->driver_data) { |
| 899 | struct btmrvl_sdio_device *data = (void *) id->driver_data; |
| 900 | card->helper = data->helper; |
| 901 | card->firmware = data->firmware; |
| 902 | } |
| 903 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 904 | if (btmrvl_sdio_register_dev(card) < 0) { |
| 905 | BT_ERR("Failed to register BT device!"); |
| 906 | ret = -ENODEV; |
| 907 | goto free_card; |
| 908 | } |
| 909 | |
| 910 | /* Disable the interrupts on the card */ |
| 911 | btmrvl_sdio_disable_host_int(card); |
| 912 | |
| 913 | if (btmrvl_sdio_download_fw(card)) { |
| 914 | BT_ERR("Downloading firmware failed!"); |
| 915 | ret = -ENODEV; |
| 916 | goto unreg_dev; |
| 917 | } |
| 918 | |
| 919 | msleep(100); |
| 920 | |
| 921 | btmrvl_sdio_enable_host_int(card); |
| 922 | |
| 923 | priv = btmrvl_add_card(card); |
| 924 | if (!priv) { |
| 925 | BT_ERR("Initializing card failed!"); |
| 926 | ret = -ENODEV; |
| 927 | goto disable_host_int; |
| 928 | } |
| 929 | |
| 930 | card->priv = priv; |
| 931 | |
| 932 | /* Initialize the interface specific function pointers */ |
| 933 | priv->hw_host_to_card = btmrvl_sdio_host_to_card; |
| 934 | priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw; |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame^] | 935 | priv->hw_process_int_status = btmrvl_sdio_process_int_status; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 936 | |
Bing Zhao | 6406160 | 2010-03-03 14:37:36 -0800 | [diff] [blame] | 937 | if (btmrvl_register_hdev(priv)) { |
| 938 | BT_ERR("Register hdev failed!"); |
| 939 | ret = -ENODEV; |
| 940 | goto disable_host_int; |
| 941 | } |
| 942 | |
Amitkumar Karwar | 32fd639 | 2009-09-30 11:18:30 -0700 | [diff] [blame] | 943 | priv->btmrvl_dev.psmode = 1; |
| 944 | btmrvl_enable_ps(priv); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 945 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 946 | return 0; |
| 947 | |
| 948 | disable_host_int: |
| 949 | btmrvl_sdio_disable_host_int(card); |
| 950 | unreg_dev: |
| 951 | btmrvl_sdio_unregister_dev(card); |
| 952 | free_card: |
| 953 | kfree(card); |
| 954 | done: |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 955 | return ret; |
| 956 | } |
| 957 | |
| 958 | static void btmrvl_sdio_remove(struct sdio_func *func) |
| 959 | { |
| 960 | struct btmrvl_sdio_card *card; |
| 961 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 962 | if (func) { |
| 963 | card = sdio_get_drvdata(func); |
| 964 | if (card) { |
| 965 | /* Send SHUTDOWN command & disable interrupt |
| 966 | * if user removes the module. |
| 967 | */ |
| 968 | if (user_rmmod) { |
| 969 | btmrvl_send_module_cfg_cmd(card->priv, |
| 970 | MODULE_SHUTDOWN_REQ); |
| 971 | btmrvl_sdio_disable_host_int(card); |
| 972 | } |
| 973 | BT_DBG("unregester dev"); |
| 974 | btmrvl_sdio_unregister_dev(card); |
| 975 | btmrvl_remove_card(card->priv); |
| 976 | kfree(card); |
| 977 | } |
| 978 | } |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | static struct sdio_driver bt_mrvl_sdio = { |
| 982 | .name = "btmrvl_sdio", |
| 983 | .id_table = btmrvl_sdio_ids, |
| 984 | .probe = btmrvl_sdio_probe, |
| 985 | .remove = btmrvl_sdio_remove, |
| 986 | }; |
| 987 | |
Peter Huewe | 8e5b230 | 2009-12-22 09:34:20 +0100 | [diff] [blame] | 988 | static int __init btmrvl_sdio_init_module(void) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 989 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 990 | if (sdio_register_driver(&bt_mrvl_sdio) != 0) { |
| 991 | BT_ERR("SDIO Driver Registration Failed"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 992 | return -ENODEV; |
| 993 | } |
| 994 | |
| 995 | /* Clear the flag in case user removes the card. */ |
| 996 | user_rmmod = 0; |
| 997 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 998 | return 0; |
| 999 | } |
| 1000 | |
Peter Huewe | 8e5b230 | 2009-12-22 09:34:20 +0100 | [diff] [blame] | 1001 | static void __exit btmrvl_sdio_exit_module(void) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1002 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1003 | /* Set the flag as user is removing this module. */ |
| 1004 | user_rmmod = 1; |
| 1005 | |
| 1006 | sdio_unregister_driver(&bt_mrvl_sdio); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | module_init(btmrvl_sdio_init_module); |
| 1010 | module_exit(btmrvl_sdio_exit_module); |
| 1011 | |
| 1012 | MODULE_AUTHOR("Marvell International Ltd."); |
Marcel Holtmann | 9666fb3 | 2009-06-09 21:45:04 +0200 | [diff] [blame] | 1013 | MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1014 | MODULE_VERSION(VERSION); |
| 1015 | MODULE_LICENSE("GPL v2"); |
Ben Hutchings | 2861453 | 2009-11-07 21:41:18 +0000 | [diff] [blame] | 1016 | MODULE_FIRMWARE("sd8688_helper.bin"); |
| 1017 | MODULE_FIRMWARE("sd8688.bin"); |