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