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