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