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 | |
Julia Lawall | f8df39f | 2010-05-13 22:02:03 +0200 | [diff] [blame] | 220 | tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 221 | if (!tmphlprbuf) { |
| 222 | BT_ERR("Unable to allocate buffer for helper." |
| 223 | " Terminating download"); |
| 224 | ret = -ENOMEM; |
| 225 | goto done; |
| 226 | } |
| 227 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 228 | helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN); |
| 229 | |
| 230 | /* Perform helper data transfer */ |
| 231 | tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE) |
| 232 | - SDIO_HEADER_LEN; |
| 233 | hlprblknow = 0; |
| 234 | |
| 235 | do { |
| 236 | ret = btmrvl_sdio_poll_card_status(card, |
| 237 | CARD_IO_READY | DN_LD_CARD_RDY); |
| 238 | if (ret < 0) { |
| 239 | BT_ERR("Helper download poll status timeout @ %d", |
| 240 | hlprblknow); |
| 241 | goto done; |
| 242 | } |
| 243 | |
| 244 | /* Check if there is more data? */ |
| 245 | if (hlprblknow >= helperlen) |
| 246 | break; |
| 247 | |
| 248 | if (helperlen - hlprblknow < tx_len) |
| 249 | tx_len = helperlen - hlprblknow; |
| 250 | |
| 251 | /* Little-endian */ |
| 252 | helperbuf[0] = ((tx_len & 0x000000ff) >> 0); |
| 253 | helperbuf[1] = ((tx_len & 0x0000ff00) >> 8); |
| 254 | helperbuf[2] = ((tx_len & 0x00ff0000) >> 16); |
| 255 | helperbuf[3] = ((tx_len & 0xff000000) >> 24); |
| 256 | |
| 257 | memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow], |
| 258 | tx_len); |
| 259 | |
| 260 | /* Now send the data */ |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 261 | ret = sdio_writesb(card->func, card->ioport, helperbuf, |
| 262 | FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 263 | if (ret < 0) { |
| 264 | BT_ERR("IO error during helper download @ %d", |
| 265 | hlprblknow); |
| 266 | goto done; |
| 267 | } |
| 268 | |
| 269 | hlprblknow += tx_len; |
| 270 | } while (true); |
| 271 | |
| 272 | BT_DBG("Transferring helper image EOF block"); |
| 273 | |
| 274 | memset(helperbuf, 0x0, SDIO_BLOCK_SIZE); |
| 275 | |
| 276 | ret = sdio_writesb(card->func, card->ioport, helperbuf, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 277 | SDIO_BLOCK_SIZE); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 278 | if (ret < 0) { |
| 279 | BT_ERR("IO error in writing helper image EOF block"); |
| 280 | goto done; |
| 281 | } |
| 282 | |
| 283 | ret = 0; |
| 284 | |
| 285 | done: |
| 286 | kfree(tmphlprbuf); |
| 287 | if (fw_helper) |
| 288 | release_firmware(fw_helper); |
| 289 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card) |
| 294 | { |
| 295 | const struct firmware *fw_firmware = NULL; |
| 296 | const u8 *firmware = NULL; |
| 297 | int firmwarelen, tmpfwbufsz, ret; |
| 298 | unsigned int tries, offset; |
| 299 | u8 base0, base1; |
| 300 | void *tmpfwbuf = NULL; |
| 301 | u8 *fwbuf; |
| 302 | u16 len; |
| 303 | int txlen = 0, tx_blocks = 0, count = 0; |
| 304 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 305 | ret = request_firmware(&fw_firmware, card->firmware, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 306 | &card->func->dev); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 307 | if ((ret < 0) || !fw_firmware) { |
| 308 | BT_ERR("request_firmware(firmware) failed, error code = %d", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 309 | ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 310 | ret = -ENOENT; |
| 311 | goto done; |
| 312 | } |
| 313 | |
| 314 | firmware = fw_firmware->data; |
| 315 | firmwarelen = fw_firmware->size; |
| 316 | |
| 317 | BT_DBG("Downloading FW image (%d bytes)", firmwarelen); |
| 318 | |
| 319 | tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN); |
Julia Lawall | f8df39f | 2010-05-13 22:02:03 +0200 | [diff] [blame] | 320 | tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 321 | if (!tmpfwbuf) { |
| 322 | BT_ERR("Unable to allocate buffer for firmware." |
| 323 | " Terminating download"); |
| 324 | ret = -ENOMEM; |
| 325 | goto done; |
| 326 | } |
| 327 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 328 | /* Ensure aligned firmware buffer */ |
| 329 | fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN); |
| 330 | |
| 331 | /* Perform firmware data transfer */ |
| 332 | offset = 0; |
| 333 | do { |
| 334 | ret = btmrvl_sdio_poll_card_status(card, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 335 | CARD_IO_READY | DN_LD_CARD_RDY); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 336 | if (ret < 0) { |
| 337 | BT_ERR("FW download with helper poll status" |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 338 | " timeout @ %d", offset); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 339 | goto done; |
| 340 | } |
| 341 | |
| 342 | /* Check if there is more data ? */ |
| 343 | if (offset >= firmwarelen) |
| 344 | break; |
| 345 | |
| 346 | for (tries = 0; tries < MAX_POLL_TRIES; tries++) { |
| 347 | base0 = sdio_readb(card->func, |
| 348 | SQ_READ_BASE_ADDRESS_A0_REG, &ret); |
| 349 | if (ret) { |
| 350 | BT_ERR("BASE0 register read failed:" |
| 351 | " base0 = 0x%04X(%d)." |
| 352 | " Terminating download", |
| 353 | base0, base0); |
| 354 | ret = -EIO; |
| 355 | goto done; |
| 356 | } |
| 357 | base1 = sdio_readb(card->func, |
| 358 | SQ_READ_BASE_ADDRESS_A1_REG, &ret); |
| 359 | if (ret) { |
| 360 | BT_ERR("BASE1 register read failed:" |
| 361 | " base1 = 0x%04X(%d)." |
| 362 | " Terminating download", |
| 363 | base1, base1); |
| 364 | ret = -EIO; |
| 365 | goto done; |
| 366 | } |
| 367 | |
| 368 | len = (((u16) base1) << 8) | base0; |
| 369 | if (len) |
| 370 | break; |
| 371 | |
| 372 | udelay(10); |
| 373 | } |
| 374 | |
| 375 | if (!len) |
| 376 | break; |
| 377 | else if (len > BTM_UPLD_SIZE) { |
| 378 | BT_ERR("FW download failure @%d, invalid length %d", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 379 | offset, len); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 380 | ret = -EINVAL; |
| 381 | goto done; |
| 382 | } |
| 383 | |
| 384 | txlen = len; |
| 385 | |
| 386 | if (len & BIT(0)) { |
| 387 | count++; |
| 388 | if (count > MAX_WRITE_IOMEM_RETRY) { |
| 389 | BT_ERR("FW download failure @%d, " |
| 390 | "over max retry count", offset); |
| 391 | ret = -EIO; |
| 392 | goto done; |
| 393 | } |
| 394 | BT_ERR("FW CRC error indicated by the helper: " |
| 395 | "len = 0x%04X, txlen = %d", len, txlen); |
| 396 | len &= ~BIT(0); |
| 397 | /* Set txlen to 0 so as to resend from same offset */ |
| 398 | txlen = 0; |
| 399 | } else { |
| 400 | count = 0; |
| 401 | |
| 402 | /* Last block ? */ |
| 403 | if (firmwarelen - offset < txlen) |
| 404 | txlen = firmwarelen - offset; |
| 405 | |
| 406 | tx_blocks = |
| 407 | (txlen + SDIO_BLOCK_SIZE - 1) / SDIO_BLOCK_SIZE; |
| 408 | |
| 409 | memcpy(fwbuf, &firmware[offset], txlen); |
| 410 | } |
| 411 | |
| 412 | ret = sdio_writesb(card->func, card->ioport, fwbuf, |
| 413 | tx_blocks * SDIO_BLOCK_SIZE); |
| 414 | |
| 415 | if (ret < 0) { |
| 416 | BT_ERR("FW download, writesb(%d) failed @%d", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 417 | count, offset); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 418 | sdio_writeb(card->func, HOST_CMD53_FIN, CONFIG_REG, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 419 | &ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 420 | if (ret) |
| 421 | BT_ERR("writeb failed (CFG)"); |
| 422 | } |
| 423 | |
| 424 | offset += txlen; |
| 425 | } while (true); |
| 426 | |
| 427 | BT_DBG("FW download over, size %d bytes", offset); |
| 428 | |
| 429 | ret = 0; |
| 430 | |
| 431 | done: |
| 432 | kfree(tmpfwbuf); |
| 433 | |
| 434 | if (fw_firmware) |
| 435 | release_firmware(fw_firmware); |
| 436 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 437 | return ret; |
| 438 | } |
| 439 | |
| 440 | static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv) |
| 441 | { |
| 442 | u16 buf_len = 0; |
| 443 | int ret, buf_block_len, blksz; |
| 444 | struct sk_buff *skb = NULL; |
| 445 | u32 type; |
| 446 | u8 *payload = NULL; |
| 447 | struct hci_dev *hdev = priv->btmrvl_dev.hcidev; |
| 448 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 449 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 450 | if (!card || !card->func) { |
| 451 | BT_ERR("card or function is NULL!"); |
| 452 | ret = -EINVAL; |
| 453 | goto exit; |
| 454 | } |
| 455 | |
| 456 | /* Read the length of data to be transferred */ |
| 457 | ret = btmrvl_sdio_read_rx_len(card, &buf_len); |
| 458 | if (ret < 0) { |
| 459 | BT_ERR("read rx_len failed"); |
| 460 | ret = -EIO; |
| 461 | goto exit; |
| 462 | } |
| 463 | |
| 464 | blksz = SDIO_BLOCK_SIZE; |
| 465 | buf_block_len = (buf_len + blksz - 1) / blksz; |
| 466 | |
| 467 | if (buf_len <= SDIO_HEADER_LEN |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 468 | || (buf_block_len * blksz) > ALLOC_BUF_SIZE) { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 469 | BT_ERR("invalid packet length: %d", buf_len); |
| 470 | ret = -EINVAL; |
| 471 | goto exit; |
| 472 | } |
| 473 | |
| 474 | /* Allocate buffer */ |
| 475 | skb = bt_skb_alloc(buf_block_len * blksz + BTSDIO_DMA_ALIGN, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 476 | GFP_ATOMIC); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 477 | if (skb == NULL) { |
| 478 | BT_ERR("No free skb"); |
| 479 | goto exit; |
| 480 | } |
| 481 | |
Bing Zhao | 3318b23 | 2009-07-08 11:44:14 -0700 | [diff] [blame] | 482 | if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) { |
| 483 | skb_put(skb, (unsigned long) skb->data & |
| 484 | (BTSDIO_DMA_ALIGN - 1)); |
| 485 | skb_pull(skb, (unsigned long) skb->data & |
| 486 | (BTSDIO_DMA_ALIGN - 1)); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Bing Zhao | 3318b23 | 2009-07-08 11:44:14 -0700 | [diff] [blame] | 489 | payload = skb->data; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 490 | |
| 491 | ret = sdio_readsb(card->func, payload, card->ioport, |
| 492 | buf_block_len * blksz); |
| 493 | if (ret < 0) { |
| 494 | BT_ERR("readsb failed: %d", ret); |
| 495 | ret = -EIO; |
| 496 | goto exit; |
| 497 | } |
| 498 | |
| 499 | /* This is SDIO specific header length: byte[2][1][0], type: byte[3] |
| 500 | * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor) |
| 501 | */ |
| 502 | |
| 503 | buf_len = payload[0]; |
| 504 | buf_len |= (u16) payload[1] << 8; |
| 505 | type = payload[3]; |
| 506 | |
| 507 | switch (type) { |
| 508 | case HCI_ACLDATA_PKT: |
| 509 | case HCI_SCODATA_PKT: |
| 510 | case HCI_EVENT_PKT: |
| 511 | bt_cb(skb)->pkt_type = type; |
| 512 | skb->dev = (void *)hdev; |
| 513 | skb_put(skb, buf_len); |
| 514 | skb_pull(skb, SDIO_HEADER_LEN); |
| 515 | |
| 516 | if (type == HCI_EVENT_PKT) |
| 517 | btmrvl_check_evtpkt(priv, skb); |
| 518 | |
| 519 | hci_recv_frame(skb); |
| 520 | hdev->stat.byte_rx += buf_len; |
| 521 | break; |
| 522 | |
| 523 | case MRVL_VENDOR_PKT: |
| 524 | bt_cb(skb)->pkt_type = HCI_VENDOR_PKT; |
| 525 | skb->dev = (void *)hdev; |
| 526 | skb_put(skb, buf_len); |
| 527 | skb_pull(skb, SDIO_HEADER_LEN); |
| 528 | |
| 529 | if (btmrvl_process_event(priv, skb)) |
| 530 | hci_recv_frame(skb); |
| 531 | |
| 532 | hdev->stat.byte_rx += buf_len; |
| 533 | break; |
| 534 | |
| 535 | default: |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 536 | BT_ERR("Unknown packet type:%d", type); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 537 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, payload, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 538 | blksz * buf_block_len); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 539 | |
| 540 | kfree_skb(skb); |
| 541 | skb = NULL; |
| 542 | break; |
| 543 | } |
| 544 | |
| 545 | exit: |
| 546 | if (ret) { |
| 547 | hdev->stat.err_rx++; |
| 548 | if (skb) |
| 549 | kfree_skb(skb); |
| 550 | } |
| 551 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 552 | return ret; |
| 553 | } |
| 554 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 555 | static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 556 | { |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 557 | ulong flags; |
| 558 | u8 ireg; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 559 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 560 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 561 | spin_lock_irqsave(&priv->driver_lock, flags); |
| 562 | ireg = sdio_ireg; |
| 563 | sdio_ireg = 0; |
| 564 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 565 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 566 | sdio_claim_host(card->func); |
| 567 | if (ireg & DN_LD_HOST_INT_STATUS) { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 568 | if (priv->btmrvl_dev.tx_dnld_rdy) |
| 569 | BT_DBG("tx_done already received: " |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 570 | " int_status=0x%x", ireg); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 571 | else |
| 572 | priv->btmrvl_dev.tx_dnld_rdy = true; |
| 573 | } |
| 574 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 575 | if (ireg & UP_LD_HOST_INT_STATUS) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 576 | btmrvl_sdio_card_to_host(priv); |
| 577 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 578 | sdio_release_host(card->func); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 579 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 580 | return 0; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | static void btmrvl_sdio_interrupt(struct sdio_func *func) |
| 584 | { |
| 585 | struct btmrvl_private *priv; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 586 | struct btmrvl_sdio_card *card; |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 587 | ulong flags; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 588 | u8 ireg = 0; |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 589 | int ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 590 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 591 | card = sdio_get_drvdata(func); |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 592 | if (!card || !card->priv) { |
| 593 | BT_ERR("sbi_interrupt(%p) card or priv is " |
| 594 | "NULL, card=%p\n", func, card); |
| 595 | return; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 596 | } |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 597 | |
| 598 | priv = card->priv; |
| 599 | |
| 600 | ireg = sdio_readb(card->func, HOST_INTSTATUS_REG, &ret); |
| 601 | if (ret) { |
| 602 | BT_ERR("sdio_readb: read int status register failed"); |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | if (ireg != 0) { |
| 607 | /* |
| 608 | * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS |
| 609 | * Clear the interrupt status register and re-enable the |
| 610 | * interrupt. |
| 611 | */ |
| 612 | BT_DBG("ireg = 0x%x", ireg); |
| 613 | |
| 614 | sdio_writeb(card->func, ~(ireg) & (DN_LD_HOST_INT_STATUS | |
| 615 | UP_LD_HOST_INT_STATUS), |
| 616 | HOST_INTSTATUS_REG, &ret); |
| 617 | if (ret) { |
| 618 | BT_ERR("sdio_writeb: clear int status register failed"); |
| 619 | return; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | spin_lock_irqsave(&priv->driver_lock, flags); |
| 624 | sdio_ireg |= ireg; |
| 625 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
| 626 | |
| 627 | btmrvl_interrupt(priv); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card) |
| 631 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 632 | struct sdio_func *func; |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 633 | u8 reg; |
| 634 | int ret = 0; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 635 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 636 | if (!card || !card->func) { |
| 637 | BT_ERR("Error: card or function is NULL!"); |
| 638 | ret = -EINVAL; |
| 639 | goto failed; |
| 640 | } |
| 641 | |
| 642 | func = card->func; |
| 643 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 644 | sdio_claim_host(func); |
| 645 | |
| 646 | ret = sdio_enable_func(func); |
| 647 | if (ret) { |
| 648 | BT_ERR("sdio_enable_func() failed: ret=%d", ret); |
| 649 | ret = -EIO; |
| 650 | goto release_host; |
| 651 | } |
| 652 | |
| 653 | ret = sdio_claim_irq(func, btmrvl_sdio_interrupt); |
| 654 | if (ret) { |
| 655 | BT_ERR("sdio_claim_irq failed: ret=%d", ret); |
| 656 | ret = -EIO; |
| 657 | goto disable_func; |
| 658 | } |
| 659 | |
| 660 | ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE); |
| 661 | if (ret) { |
| 662 | BT_ERR("cannot set SDIO block size"); |
| 663 | ret = -EIO; |
| 664 | goto release_irq; |
| 665 | } |
| 666 | |
| 667 | reg = sdio_readb(func, IO_PORT_0_REG, &ret); |
| 668 | if (ret < 0) { |
| 669 | ret = -EIO; |
| 670 | goto release_irq; |
| 671 | } |
| 672 | |
| 673 | card->ioport = reg; |
| 674 | |
| 675 | reg = sdio_readb(func, IO_PORT_1_REG, &ret); |
| 676 | if (ret < 0) { |
| 677 | ret = -EIO; |
| 678 | goto release_irq; |
| 679 | } |
| 680 | |
| 681 | card->ioport |= (reg << 8); |
| 682 | |
| 683 | reg = sdio_readb(func, IO_PORT_2_REG, &ret); |
| 684 | if (ret < 0) { |
| 685 | ret = -EIO; |
| 686 | goto release_irq; |
| 687 | } |
| 688 | |
| 689 | card->ioport |= (reg << 16); |
| 690 | |
| 691 | BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport); |
| 692 | |
| 693 | sdio_set_drvdata(func, card); |
| 694 | |
| 695 | sdio_release_host(func); |
| 696 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 697 | return 0; |
| 698 | |
| 699 | release_irq: |
| 700 | sdio_release_irq(func); |
| 701 | |
| 702 | disable_func: |
| 703 | sdio_disable_func(func); |
| 704 | |
| 705 | release_host: |
| 706 | sdio_release_host(func); |
| 707 | |
| 708 | failed: |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 709 | return ret; |
| 710 | } |
| 711 | |
| 712 | static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card) |
| 713 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 714 | if (card && card->func) { |
| 715 | sdio_claim_host(card->func); |
| 716 | sdio_release_irq(card->func); |
| 717 | sdio_disable_func(card->func); |
| 718 | sdio_release_host(card->func); |
| 719 | sdio_set_drvdata(card->func, NULL); |
| 720 | } |
| 721 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card) |
| 726 | { |
| 727 | int ret; |
| 728 | |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 729 | if (!card || !card->func) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 730 | return -EINVAL; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 731 | |
| 732 | sdio_claim_host(card->func); |
| 733 | |
| 734 | ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE); |
| 735 | |
| 736 | btmrvl_sdio_get_rx_unit(card); |
| 737 | |
| 738 | sdio_release_host(card->func); |
| 739 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 740 | return ret; |
| 741 | } |
| 742 | |
| 743 | static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card) |
| 744 | { |
| 745 | int ret; |
| 746 | |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 747 | if (!card || !card->func) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 748 | return -EINVAL; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 749 | |
| 750 | sdio_claim_host(card->func); |
| 751 | |
| 752 | ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE); |
| 753 | |
| 754 | sdio_release_host(card->func); |
| 755 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 756 | return ret; |
| 757 | } |
| 758 | |
| 759 | static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv, |
| 760 | u8 *payload, u16 nb) |
| 761 | { |
| 762 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 763 | int ret = 0; |
| 764 | int buf_block_len; |
| 765 | int blksz; |
| 766 | int i = 0; |
| 767 | u8 *buf = NULL; |
| 768 | void *tmpbuf = NULL; |
| 769 | int tmpbufsz; |
| 770 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 771 | if (!card || !card->func) { |
| 772 | BT_ERR("card or function is NULL!"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 773 | return -EINVAL; |
| 774 | } |
| 775 | |
| 776 | buf = payload; |
Bing Zhao | 3318b23 | 2009-07-08 11:44:14 -0700 | [diff] [blame] | 777 | if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 778 | tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN); |
Julia Lawall | 5959809 | 2009-08-06 22:05:18 +0200 | [diff] [blame] | 779 | tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL); |
| 780 | if (!tmpbuf) |
| 781 | return -ENOMEM; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 782 | buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN); |
| 783 | memcpy(buf, payload, nb); |
| 784 | } |
| 785 | |
| 786 | blksz = SDIO_BLOCK_SIZE; |
| 787 | buf_block_len = (nb + blksz - 1) / blksz; |
| 788 | |
| 789 | sdio_claim_host(card->func); |
| 790 | |
| 791 | do { |
| 792 | /* Transfer data to card */ |
| 793 | ret = sdio_writesb(card->func, card->ioport, buf, |
| 794 | buf_block_len * blksz); |
| 795 | if (ret < 0) { |
| 796 | i++; |
| 797 | BT_ERR("i=%d writesb failed: %d", i, ret); |
| 798 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 799 | payload, nb); |
| 800 | ret = -EIO; |
| 801 | if (i > MAX_WRITE_IOMEM_RETRY) |
| 802 | goto exit; |
| 803 | } |
| 804 | } while (ret); |
| 805 | |
| 806 | priv->btmrvl_dev.tx_dnld_rdy = false; |
| 807 | |
| 808 | exit: |
| 809 | sdio_release_host(card->func); |
Yoichi Yuasa | 88d1a0c | 2010-02-04 09:55:51 +0900 | [diff] [blame] | 810 | kfree(tmpbuf); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 811 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 812 | return ret; |
| 813 | } |
| 814 | |
| 815 | static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card) |
| 816 | { |
| 817 | int ret = 0; |
| 818 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 819 | if (!card || !card->func) { |
| 820 | BT_ERR("card or function is NULL!"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 821 | return -EINVAL; |
| 822 | } |
| 823 | sdio_claim_host(card->func); |
| 824 | |
| 825 | if (!btmrvl_sdio_verify_fw_download(card, 1)) { |
| 826 | BT_DBG("Firmware already downloaded!"); |
| 827 | goto done; |
| 828 | } |
| 829 | |
| 830 | ret = btmrvl_sdio_download_helper(card); |
| 831 | if (ret) { |
| 832 | BT_ERR("Failed to download helper!"); |
| 833 | ret = -EIO; |
| 834 | goto done; |
| 835 | } |
| 836 | |
| 837 | if (btmrvl_sdio_download_fw_w_helper(card)) { |
| 838 | BT_ERR("Failed to download firmware!"); |
| 839 | ret = -EIO; |
| 840 | goto done; |
| 841 | } |
| 842 | |
| 843 | if (btmrvl_sdio_verify_fw_download(card, MAX_POLL_TRIES)) { |
| 844 | BT_ERR("FW failed to be active in time!"); |
| 845 | ret = -ETIMEDOUT; |
| 846 | goto done; |
| 847 | } |
| 848 | |
| 849 | done: |
| 850 | sdio_release_host(card->func); |
| 851 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 852 | return ret; |
| 853 | } |
| 854 | |
| 855 | static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv) |
| 856 | { |
| 857 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 858 | int ret = 0; |
| 859 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 860 | if (!card || !card->func) { |
| 861 | BT_ERR("card or function is NULL!"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 862 | return -EINVAL; |
| 863 | } |
| 864 | |
| 865 | sdio_claim_host(card->func); |
| 866 | |
| 867 | sdio_writeb(card->func, HOST_POWER_UP, CONFIG_REG, &ret); |
| 868 | |
| 869 | sdio_release_host(card->func); |
| 870 | |
| 871 | BT_DBG("wake up firmware"); |
| 872 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 873 | return ret; |
| 874 | } |
| 875 | |
| 876 | static int btmrvl_sdio_probe(struct sdio_func *func, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 877 | const struct sdio_device_id *id) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 878 | { |
| 879 | int ret = 0; |
| 880 | struct btmrvl_private *priv = NULL; |
| 881 | struct btmrvl_sdio_card *card = NULL; |
| 882 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 883 | BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d", |
| 884 | id->vendor, id->device, id->class, func->num); |
| 885 | |
| 886 | card = kzalloc(sizeof(*card), GFP_KERNEL); |
| 887 | if (!card) { |
| 888 | ret = -ENOMEM; |
| 889 | goto done; |
| 890 | } |
| 891 | |
| 892 | card->func = func; |
| 893 | |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 894 | if (id->driver_data) { |
| 895 | struct btmrvl_sdio_device *data = (void *) id->driver_data; |
| 896 | card->helper = data->helper; |
| 897 | card->firmware = data->firmware; |
| 898 | } |
| 899 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 900 | if (btmrvl_sdio_register_dev(card) < 0) { |
| 901 | BT_ERR("Failed to register BT device!"); |
| 902 | ret = -ENODEV; |
| 903 | goto free_card; |
| 904 | } |
| 905 | |
| 906 | /* Disable the interrupts on the card */ |
| 907 | btmrvl_sdio_disable_host_int(card); |
| 908 | |
| 909 | if (btmrvl_sdio_download_fw(card)) { |
| 910 | BT_ERR("Downloading firmware failed!"); |
| 911 | ret = -ENODEV; |
| 912 | goto unreg_dev; |
| 913 | } |
| 914 | |
| 915 | msleep(100); |
| 916 | |
| 917 | btmrvl_sdio_enable_host_int(card); |
| 918 | |
| 919 | priv = btmrvl_add_card(card); |
| 920 | if (!priv) { |
| 921 | BT_ERR("Initializing card failed!"); |
| 922 | ret = -ENODEV; |
| 923 | goto disable_host_int; |
| 924 | } |
| 925 | |
| 926 | card->priv = priv; |
| 927 | |
| 928 | /* Initialize the interface specific function pointers */ |
| 929 | priv->hw_host_to_card = btmrvl_sdio_host_to_card; |
| 930 | priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw; |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 931 | priv->hw_process_int_status = btmrvl_sdio_process_int_status; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 932 | |
Bing Zhao | 6406160 | 2010-03-03 14:37:36 -0800 | [diff] [blame] | 933 | if (btmrvl_register_hdev(priv)) { |
| 934 | BT_ERR("Register hdev failed!"); |
| 935 | ret = -ENODEV; |
| 936 | goto disable_host_int; |
| 937 | } |
| 938 | |
Amitkumar Karwar | 32fd639 | 2009-09-30 11:18:30 -0700 | [diff] [blame] | 939 | priv->btmrvl_dev.psmode = 1; |
| 940 | btmrvl_enable_ps(priv); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 941 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 942 | return 0; |
| 943 | |
| 944 | disable_host_int: |
| 945 | btmrvl_sdio_disable_host_int(card); |
| 946 | unreg_dev: |
| 947 | btmrvl_sdio_unregister_dev(card); |
| 948 | free_card: |
| 949 | kfree(card); |
| 950 | done: |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 951 | return ret; |
| 952 | } |
| 953 | |
| 954 | static void btmrvl_sdio_remove(struct sdio_func *func) |
| 955 | { |
| 956 | struct btmrvl_sdio_card *card; |
| 957 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 958 | if (func) { |
| 959 | card = sdio_get_drvdata(func); |
| 960 | if (card) { |
| 961 | /* Send SHUTDOWN command & disable interrupt |
| 962 | * if user removes the module. |
| 963 | */ |
| 964 | if (user_rmmod) { |
| 965 | btmrvl_send_module_cfg_cmd(card->priv, |
| 966 | MODULE_SHUTDOWN_REQ); |
| 967 | btmrvl_sdio_disable_host_int(card); |
| 968 | } |
| 969 | BT_DBG("unregester dev"); |
| 970 | btmrvl_sdio_unregister_dev(card); |
| 971 | btmrvl_remove_card(card->priv); |
| 972 | kfree(card); |
| 973 | } |
| 974 | } |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | static struct sdio_driver bt_mrvl_sdio = { |
| 978 | .name = "btmrvl_sdio", |
| 979 | .id_table = btmrvl_sdio_ids, |
| 980 | .probe = btmrvl_sdio_probe, |
| 981 | .remove = btmrvl_sdio_remove, |
| 982 | }; |
| 983 | |
Peter Huewe | 8e5b230 | 2009-12-22 09:34:20 +0100 | [diff] [blame] | 984 | static int __init btmrvl_sdio_init_module(void) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 985 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 986 | if (sdio_register_driver(&bt_mrvl_sdio) != 0) { |
| 987 | BT_ERR("SDIO Driver Registration Failed"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 988 | return -ENODEV; |
| 989 | } |
| 990 | |
| 991 | /* Clear the flag in case user removes the card. */ |
| 992 | user_rmmod = 0; |
| 993 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 994 | return 0; |
| 995 | } |
| 996 | |
Peter Huewe | 8e5b230 | 2009-12-22 09:34:20 +0100 | [diff] [blame] | 997 | static void __exit btmrvl_sdio_exit_module(void) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 998 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 999 | /* Set the flag as user is removing this module. */ |
| 1000 | user_rmmod = 1; |
| 1001 | |
| 1002 | sdio_unregister_driver(&bt_mrvl_sdio); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | module_init(btmrvl_sdio_init_module); |
| 1006 | module_exit(btmrvl_sdio_exit_module); |
| 1007 | |
| 1008 | MODULE_AUTHOR("Marvell International Ltd."); |
Marcel Holtmann | 9666fb3 | 2009-06-09 21:45:04 +0200 | [diff] [blame] | 1009 | MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1010 | MODULE_VERSION(VERSION); |
| 1011 | MODULE_LICENSE("GPL v2"); |
Ben Hutchings | 2861453 | 2009-11-07 21:41:18 +0000 | [diff] [blame] | 1012 | MODULE_FIRMWARE("sd8688_helper.bin"); |
| 1013 | MODULE_FIRMWARE("sd8688.bin"); |