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