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