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