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> |
Xinming Hu | dc75961 | 2014-11-24 02:40:53 -0800 | [diff] [blame] | 27 | #include <linux/devcoredump.h> |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 28 | |
| 29 | #include <net/bluetooth/bluetooth.h> |
| 30 | #include <net/bluetooth/hci_core.h> |
| 31 | |
| 32 | #include "btmrvl_drv.h" |
| 33 | #include "btmrvl_sdio.h" |
| 34 | |
| 35 | #define VERSION "1.0" |
| 36 | |
Xinming Hu | dc75961 | 2014-11-24 02:40:53 -0800 | [diff] [blame] | 37 | static struct memory_type_mapping mem_type_mapping_tbl[] = { |
| 38 | {"ITCM", NULL, 0, 0xF0}, |
| 39 | {"DTCM", NULL, 0, 0xF1}, |
| 40 | {"SQRAM", NULL, 0, 0xF2}, |
| 41 | {"APU", NULL, 0, 0xF3}, |
| 42 | {"CIU", NULL, 0, 0xF4}, |
| 43 | {"ICU", NULL, 0, 0xF5}, |
| 44 | {"MAC", NULL, 0, 0xF6}, |
| 45 | {"EXT7", NULL, 0, 0xF7}, |
| 46 | {"EXT8", NULL, 0, 0xF8}, |
| 47 | {"EXT9", NULL, 0, 0xF9}, |
| 48 | {"EXT10", NULL, 0, 0xFA}, |
| 49 | {"EXT11", NULL, 0, 0xFB}, |
| 50 | {"EXT12", NULL, 0, 0xFC}, |
| 51 | {"EXT13", NULL, 0, 0xFD}, |
| 52 | {"EXTLAST", NULL, 0, 0xFE}, |
| 53 | }; |
| 54 | |
Xinming Hu | bb7f4f0 | 2016-04-26 06:57:27 -0700 | [diff] [blame] | 55 | static const struct of_device_id btmrvl_sdio_of_match_table[] = { |
| 56 | { .compatible = "marvell,sd8897-bt" }, |
| 57 | { .compatible = "marvell,sd8997-bt" }, |
| 58 | { } |
| 59 | }; |
| 60 | |
| 61 | static irqreturn_t btmrvl_wake_irq_bt(int irq, void *priv) |
| 62 | { |
| 63 | struct btmrvl_plt_wake_cfg *cfg = priv; |
| 64 | |
| 65 | if (cfg->irq_bt >= 0) { |
| 66 | pr_info("%s: wake by bt", __func__); |
| 67 | cfg->wake_by_bt = true; |
| 68 | disable_irq_nosync(irq); |
| 69 | } |
| 70 | |
| 71 | return IRQ_HANDLED; |
| 72 | } |
| 73 | |
| 74 | /* This function parses device tree node using mmc subnode devicetree API. |
| 75 | * The device node is saved in card->plt_of_node. |
| 76 | * If the device tree node exists and includes interrupts attributes, this |
| 77 | * function will request platform specific wakeup interrupt. |
| 78 | */ |
| 79 | static int btmrvl_sdio_probe_of(struct device *dev, |
| 80 | struct btmrvl_sdio_card *card) |
| 81 | { |
| 82 | struct btmrvl_plt_wake_cfg *cfg; |
| 83 | int ret; |
| 84 | |
| 85 | if (!dev->of_node || |
| 86 | !of_match_node(btmrvl_sdio_of_match_table, dev->of_node)) { |
| 87 | pr_err("sdio platform data not available"); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | card->plt_of_node = dev->of_node; |
| 92 | |
| 93 | card->plt_wake_cfg = devm_kzalloc(dev, sizeof(*card->plt_wake_cfg), |
| 94 | GFP_KERNEL); |
| 95 | cfg = card->plt_wake_cfg; |
| 96 | if (cfg && card->plt_of_node) { |
| 97 | cfg->irq_bt = irq_of_parse_and_map(card->plt_of_node, 0); |
| 98 | if (!cfg->irq_bt) { |
| 99 | dev_err(dev, "fail to parse irq_bt from device tree"); |
| 100 | } else { |
| 101 | ret = devm_request_irq(dev, cfg->irq_bt, |
| 102 | btmrvl_wake_irq_bt, |
| 103 | IRQF_TRIGGER_LOW, |
| 104 | "bt_wake", cfg); |
| 105 | if (ret) { |
| 106 | dev_err(dev, |
| 107 | "Failed to request irq_bt %d (%d)\n", |
| 108 | cfg->irq_bt, ret); |
| 109 | } |
| 110 | disable_irq(cfg->irq_bt); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 117 | /* The btmrvl_sdio_remove() callback function is called |
| 118 | * when user removes this module from kernel space or ejects |
| 119 | * the card from the slot. The driver handles these 2 cases |
| 120 | * differently. |
| 121 | * If the user is removing the module, a MODULE_SHUTDOWN_REQ |
| 122 | * command is sent to firmware and interrupt will be disabled. |
| 123 | * If the card is removed, there is no need to send command |
| 124 | * or disable interrupt. |
| 125 | * |
| 126 | * The variable 'user_rmmod' is used to distinguish these two |
| 127 | * scenarios. This flag is initialized as FALSE in case the card |
| 128 | * is removed, and will be set to TRUE for module removal when |
| 129 | * module_exit function is called. |
| 130 | */ |
| 131 | static u8 user_rmmod; |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 132 | static u8 sdio_ireg; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 133 | |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 134 | static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = { |
| 135 | .cfg = 0x03, |
| 136 | .host_int_mask = 0x04, |
| 137 | .host_intstatus = 0x05, |
| 138 | .card_status = 0x20, |
| 139 | .sq_read_base_addr_a0 = 0x10, |
| 140 | .sq_read_base_addr_a1 = 0x11, |
| 141 | .card_fw_status0 = 0x40, |
| 142 | .card_fw_status1 = 0x41, |
| 143 | .card_rx_len = 0x42, |
| 144 | .card_rx_unit = 0x43, |
| 145 | .io_port_0 = 0x00, |
| 146 | .io_port_1 = 0x01, |
| 147 | .io_port_2 = 0x02, |
Bing Zhao | 0d36740 | 2014-03-28 14:54:39 -0700 | [diff] [blame] | 148 | .int_read_to_clear = false, |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 149 | }; |
Bing Zhao | 2ac654f | 2011-11-16 20:40:42 -0800 | [diff] [blame] | 150 | static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = { |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 151 | .cfg = 0x00, |
| 152 | .host_int_mask = 0x02, |
| 153 | .host_intstatus = 0x03, |
| 154 | .card_status = 0x30, |
| 155 | .sq_read_base_addr_a0 = 0x40, |
| 156 | .sq_read_base_addr_a1 = 0x41, |
| 157 | .card_revision = 0x5c, |
| 158 | .card_fw_status0 = 0x60, |
| 159 | .card_fw_status1 = 0x61, |
| 160 | .card_rx_len = 0x62, |
| 161 | .card_rx_unit = 0x63, |
| 162 | .io_port_0 = 0x78, |
| 163 | .io_port_1 = 0x79, |
| 164 | .io_port_2 = 0x7a, |
Bing Zhao | 0d36740 | 2014-03-28 14:54:39 -0700 | [diff] [blame] | 165 | .int_read_to_clear = false, |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 166 | }; |
| 167 | |
Xinming Hu | 3907d55 | 2014-09-30 06:45:33 -0400 | [diff] [blame] | 168 | static const struct btmrvl_sdio_card_reg btmrvl_reg_8887 = { |
| 169 | .cfg = 0x00, |
| 170 | .host_int_mask = 0x08, |
| 171 | .host_intstatus = 0x0C, |
| 172 | .card_status = 0x5C, |
| 173 | .sq_read_base_addr_a0 = 0x6C, |
| 174 | .sq_read_base_addr_a1 = 0x6D, |
| 175 | .card_revision = 0xC8, |
| 176 | .card_fw_status0 = 0x88, |
| 177 | .card_fw_status1 = 0x89, |
| 178 | .card_rx_len = 0x8A, |
| 179 | .card_rx_unit = 0x8B, |
| 180 | .io_port_0 = 0xE4, |
| 181 | .io_port_1 = 0xE5, |
| 182 | .io_port_2 = 0xE6, |
| 183 | .int_read_to_clear = true, |
| 184 | .host_int_rsr = 0x04, |
| 185 | .card_misc_cfg = 0xD8, |
| 186 | }; |
| 187 | |
Amitkumar Karwar | 1e3e492 | 2014-09-30 06:45:32 -0400 | [diff] [blame] | 188 | static const struct btmrvl_sdio_card_reg btmrvl_reg_8897 = { |
Bing Zhao | 22f2efe | 2013-05-13 18:15:32 -0700 | [diff] [blame] | 189 | .cfg = 0x00, |
| 190 | .host_int_mask = 0x02, |
| 191 | .host_intstatus = 0x03, |
| 192 | .card_status = 0x50, |
| 193 | .sq_read_base_addr_a0 = 0x60, |
| 194 | .sq_read_base_addr_a1 = 0x61, |
| 195 | .card_revision = 0xbc, |
| 196 | .card_fw_status0 = 0xc0, |
| 197 | .card_fw_status1 = 0xc1, |
| 198 | .card_rx_len = 0xc2, |
| 199 | .card_rx_unit = 0xc3, |
| 200 | .io_port_0 = 0xd8, |
| 201 | .io_port_1 = 0xd9, |
| 202 | .io_port_2 = 0xda, |
Bing Zhao | 0d36740 | 2014-03-28 14:54:39 -0700 | [diff] [blame] | 203 | .int_read_to_clear = true, |
| 204 | .host_int_rsr = 0x01, |
| 205 | .card_misc_cfg = 0xcc, |
Xinming Hu | dc75961 | 2014-11-24 02:40:53 -0800 | [diff] [blame] | 206 | .fw_dump_ctrl = 0xe2, |
| 207 | .fw_dump_start = 0xe3, |
| 208 | .fw_dump_end = 0xea, |
Bing Zhao | 22f2efe | 2013-05-13 18:15:32 -0700 | [diff] [blame] | 209 | }; |
| 210 | |
Amitkumar Karwar | f0ef674 | 2015-09-21 03:06:42 -0700 | [diff] [blame] | 211 | static const struct btmrvl_sdio_card_reg btmrvl_reg_8997 = { |
| 212 | .cfg = 0x00, |
| 213 | .host_int_mask = 0x08, |
| 214 | .host_intstatus = 0x0c, |
| 215 | .card_status = 0x5c, |
| 216 | .sq_read_base_addr_a0 = 0xf8, |
| 217 | .sq_read_base_addr_a1 = 0xf9, |
| 218 | .card_revision = 0xc8, |
| 219 | .card_fw_status0 = 0xe8, |
| 220 | .card_fw_status1 = 0xe9, |
| 221 | .card_rx_len = 0xea, |
| 222 | .card_rx_unit = 0xeb, |
| 223 | .io_port_0 = 0xe4, |
| 224 | .io_port_1 = 0xe5, |
| 225 | .io_port_2 = 0xe6, |
| 226 | .int_read_to_clear = true, |
| 227 | .host_int_rsr = 0x04, |
| 228 | .card_misc_cfg = 0xD8, |
| 229 | .fw_dump_ctrl = 0xf0, |
| 230 | .fw_dump_start = 0xf1, |
| 231 | .fw_dump_end = 0xf8, |
| 232 | }; |
| 233 | |
Paul Bolle | c675786 | 2011-10-05 14:56:46 +0200 | [diff] [blame] | 234 | static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = { |
Lubomir Rintel | 77189df | 2013-03-13 12:04:30 +0100 | [diff] [blame] | 235 | .helper = "mrvl/sd8688_helper.bin", |
| 236 | .firmware = "mrvl/sd8688.bin", |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 237 | .reg = &btmrvl_reg_8688, |
Bing Zhao | 4df82b5 | 2014-03-31 14:41:44 -0700 | [diff] [blame] | 238 | .support_pscan_win_report = false, |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 239 | .sd_blksz_fw_dl = 64, |
Xinming Hu | dc75961 | 2014-11-24 02:40:53 -0800 | [diff] [blame] | 240 | .supports_fw_dump = false, |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = { |
| 244 | .helper = NULL, |
| 245 | .firmware = "mrvl/sd8787_uapsta.bin", |
Bing Zhao | 2ac654f | 2011-11-16 20:40:42 -0800 | [diff] [blame] | 246 | .reg = &btmrvl_reg_87xx, |
Bing Zhao | 4df82b5 | 2014-03-31 14:41:44 -0700 | [diff] [blame] | 247 | .support_pscan_win_report = false, |
Bing Zhao | 2ac654f | 2011-11-16 20:40:42 -0800 | [diff] [blame] | 248 | .sd_blksz_fw_dl = 256, |
Xinming Hu | dc75961 | 2014-11-24 02:40:53 -0800 | [diff] [blame] | 249 | .supports_fw_dump = false, |
Bing Zhao | 2ac654f | 2011-11-16 20:40:42 -0800 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = { |
| 253 | .helper = NULL, |
| 254 | .firmware = "mrvl/sd8797_uapsta.bin", |
| 255 | .reg = &btmrvl_reg_87xx, |
Bing Zhao | 4df82b5 | 2014-03-31 14:41:44 -0700 | [diff] [blame] | 256 | .support_pscan_win_report = false, |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 257 | .sd_blksz_fw_dl = 256, |
Xinming Hu | dc75961 | 2014-11-24 02:40:53 -0800 | [diff] [blame] | 258 | .supports_fw_dump = false, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 259 | }; |
| 260 | |
Xinming Hu | 3907d55 | 2014-09-30 06:45:33 -0400 | [diff] [blame] | 261 | static const struct btmrvl_sdio_device btmrvl_sdio_sd8887 = { |
| 262 | .helper = NULL, |
| 263 | .firmware = "mrvl/sd8887_uapsta.bin", |
| 264 | .reg = &btmrvl_reg_8887, |
| 265 | .support_pscan_win_report = true, |
| 266 | .sd_blksz_fw_dl = 256, |
Xinming Hu | dc75961 | 2014-11-24 02:40:53 -0800 | [diff] [blame] | 267 | .supports_fw_dump = false, |
Xinming Hu | 3907d55 | 2014-09-30 06:45:33 -0400 | [diff] [blame] | 268 | }; |
| 269 | |
Bing Zhao | 22f2efe | 2013-05-13 18:15:32 -0700 | [diff] [blame] | 270 | static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = { |
| 271 | .helper = NULL, |
| 272 | .firmware = "mrvl/sd8897_uapsta.bin", |
Amitkumar Karwar | 1e3e492 | 2014-09-30 06:45:32 -0400 | [diff] [blame] | 273 | .reg = &btmrvl_reg_8897, |
Bing Zhao | 4df82b5 | 2014-03-31 14:41:44 -0700 | [diff] [blame] | 274 | .support_pscan_win_report = true, |
Bing Zhao | 22f2efe | 2013-05-13 18:15:32 -0700 | [diff] [blame] | 275 | .sd_blksz_fw_dl = 256, |
Xinming Hu | dc75961 | 2014-11-24 02:40:53 -0800 | [diff] [blame] | 276 | .supports_fw_dump = true, |
Bing Zhao | 22f2efe | 2013-05-13 18:15:32 -0700 | [diff] [blame] | 277 | }; |
| 278 | |
Amitkumar Karwar | f0ef674 | 2015-09-21 03:06:42 -0700 | [diff] [blame] | 279 | static const struct btmrvl_sdio_device btmrvl_sdio_sd8997 = { |
| 280 | .helper = NULL, |
| 281 | .firmware = "mrvl/sd8997_uapsta.bin", |
| 282 | .reg = &btmrvl_reg_8997, |
| 283 | .support_pscan_win_report = true, |
| 284 | .sd_blksz_fw_dl = 256, |
| 285 | .supports_fw_dump = true, |
| 286 | }; |
| 287 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 288 | static const struct sdio_device_id btmrvl_sdio_ids[] = { |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 289 | /* Marvell SD8688 Bluetooth device */ |
| 290 | { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105), |
Amitkumar Karwar | 53d6cfa | 2015-09-21 03:06:41 -0700 | [diff] [blame] | 291 | .driver_data = (unsigned long)&btmrvl_sdio_sd8688 }, |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 292 | /* Marvell SD8787 Bluetooth device */ |
| 293 | { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A), |
Amitkumar Karwar | 53d6cfa | 2015-09-21 03:06:41 -0700 | [diff] [blame] | 294 | .driver_data = (unsigned long)&btmrvl_sdio_sd8787 }, |
Bing Zhao | 37ed561 | 2012-05-23 18:50:04 -0700 | [diff] [blame] | 295 | /* Marvell SD8787 Bluetooth AMP device */ |
| 296 | { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911B), |
Amitkumar Karwar | 53d6cfa | 2015-09-21 03:06:41 -0700 | [diff] [blame] | 297 | .driver_data = (unsigned long)&btmrvl_sdio_sd8787 }, |
Bing Zhao | 2ac654f | 2011-11-16 20:40:42 -0800 | [diff] [blame] | 298 | /* Marvell SD8797 Bluetooth device */ |
| 299 | { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A), |
Amitkumar Karwar | 53d6cfa | 2015-09-21 03:06:41 -0700 | [diff] [blame] | 300 | .driver_data = (unsigned long)&btmrvl_sdio_sd8797 }, |
Xinming Hu | 3907d55 | 2014-09-30 06:45:33 -0400 | [diff] [blame] | 301 | /* Marvell SD8887 Bluetooth device */ |
| 302 | { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9136), |
| 303 | .driver_data = (unsigned long)&btmrvl_sdio_sd8887 }, |
Bing Zhao | 22f2efe | 2013-05-13 18:15:32 -0700 | [diff] [blame] | 304 | /* Marvell SD8897 Bluetooth device */ |
| 305 | { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912E), |
Amitkumar Karwar | 53d6cfa | 2015-09-21 03:06:41 -0700 | [diff] [blame] | 306 | .driver_data = (unsigned long)&btmrvl_sdio_sd8897 }, |
Amitkumar Karwar | f0ef674 | 2015-09-21 03:06:42 -0700 | [diff] [blame] | 307 | /* Marvell SD8997 Bluetooth device */ |
| 308 | { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9142), |
| 309 | .driver_data = (unsigned long)&btmrvl_sdio_sd8997 }, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 310 | |
| 311 | { } /* Terminating entry */ |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids); |
| 315 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 316 | static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card) |
| 317 | { |
| 318 | u8 reg; |
| 319 | int ret; |
| 320 | |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 321 | reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 322 | if (!ret) |
| 323 | card->rx_unit = reg; |
| 324 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat) |
| 329 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 330 | u8 fws0, fws1; |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 331 | int ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 332 | |
| 333 | *dat = 0; |
| 334 | |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 335 | fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret); |
Kulikov Vasiliy | 278be27 | 2010-07-05 12:01:22 +0400 | [diff] [blame] | 336 | if (ret) |
| 337 | return -EIO; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 338 | |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 339 | fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret); |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 340 | if (ret) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 341 | return -EIO; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 342 | |
| 343 | *dat = (((u16) fws1) << 8) | fws0; |
| 344 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat) |
| 349 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 350 | u8 reg; |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 351 | int ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 352 | |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 353 | reg = sdio_readb(card->func, card->reg->card_rx_len, &ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 354 | if (!ret) |
| 355 | *dat = (u16) reg << card->rx_unit; |
| 356 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | 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] | 361 | u8 mask) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 362 | { |
| 363 | int ret; |
| 364 | |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 365 | sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 366 | if (ret) { |
| 367 | BT_ERR("Unable to enable the host interrupt!"); |
| 368 | ret = -EIO; |
| 369 | } |
| 370 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | 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] | 375 | u8 mask) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 376 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 377 | u8 host_int_mask; |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 378 | int ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 379 | |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 380 | 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] | 381 | if (ret) |
| 382 | return -EIO; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 383 | |
| 384 | host_int_mask &= ~mask; |
| 385 | |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 386 | 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] | 387 | if (ret < 0) { |
| 388 | BT_ERR("Unable to disable the host interrupt!"); |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 389 | return -EIO; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 392 | return 0; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits) |
| 396 | { |
| 397 | unsigned int tries; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 398 | u8 status; |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 399 | int ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 400 | |
| 401 | for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) { |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 402 | status = sdio_readb(card->func, card->reg->card_status, &ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 403 | if (ret) |
| 404 | goto failed; |
| 405 | if ((status & bits) == bits) |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 406 | return ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 407 | |
| 408 | udelay(1); |
| 409 | } |
| 410 | |
| 411 | ret = -ETIMEDOUT; |
| 412 | |
| 413 | failed: |
| 414 | BT_ERR("FAILED! ret=%d", ret); |
| 415 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 420 | int pollnum) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 421 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 422 | u16 firmwarestat; |
Andreas Fenkart | 69676b1 | 2013-04-22 11:10:23 +0200 | [diff] [blame] | 423 | int tries, ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 424 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 425 | /* Wait for firmware to become ready */ |
| 426 | for (tries = 0; tries < pollnum; tries++) { |
Andreas Fenkart | c1c999e | 2013-04-22 11:10:22 +0200 | [diff] [blame] | 427 | sdio_claim_host(card->func); |
| 428 | ret = btmrvl_sdio_read_fw_status(card, &firmwarestat); |
| 429 | sdio_release_host(card->func); |
| 430 | if (ret < 0) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 431 | continue; |
| 432 | |
Andreas Fenkart | 69676b1 | 2013-04-22 11:10:23 +0200 | [diff] [blame] | 433 | if (firmwarestat == FIRMWARE_READY) |
| 434 | return 0; |
| 435 | |
Wei-Ning Huang | 9a01242 | 2016-03-08 11:40:06 +0800 | [diff] [blame] | 436 | msleep(100); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Andreas Fenkart | 69676b1 | 2013-04-22 11:10:23 +0200 | [diff] [blame] | 439 | return -ETIMEDOUT; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card) |
| 443 | { |
| 444 | const struct firmware *fw_helper = NULL; |
| 445 | const u8 *helper = NULL; |
| 446 | int ret; |
| 447 | void *tmphlprbuf = NULL; |
| 448 | int tmphlprbufsz, hlprblknow, helperlen; |
| 449 | u8 *helperbuf; |
| 450 | u32 tx_len; |
| 451 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 452 | ret = request_firmware(&fw_helper, card->helper, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 453 | &card->func->dev); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 454 | if ((ret < 0) || !fw_helper) { |
| 455 | BT_ERR("request_firmware(helper) failed, error code = %d", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 456 | ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 457 | ret = -ENOENT; |
| 458 | goto done; |
| 459 | } |
| 460 | |
| 461 | helper = fw_helper->data; |
| 462 | helperlen = fw_helper->size; |
| 463 | |
| 464 | BT_DBG("Downloading helper image (%d bytes), block size %d bytes", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 465 | helperlen, SDIO_BLOCK_SIZE); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 466 | |
| 467 | tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN); |
| 468 | |
Julia Lawall | f8df39f | 2010-05-13 22:02:03 +0200 | [diff] [blame] | 469 | tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 470 | if (!tmphlprbuf) { |
| 471 | BT_ERR("Unable to allocate buffer for helper." |
| 472 | " Terminating download"); |
| 473 | ret = -ENOMEM; |
| 474 | goto done; |
| 475 | } |
| 476 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 477 | helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN); |
| 478 | |
| 479 | /* Perform helper data transfer */ |
| 480 | tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE) |
| 481 | - SDIO_HEADER_LEN; |
| 482 | hlprblknow = 0; |
| 483 | |
| 484 | do { |
| 485 | ret = btmrvl_sdio_poll_card_status(card, |
| 486 | CARD_IO_READY | DN_LD_CARD_RDY); |
| 487 | if (ret < 0) { |
| 488 | BT_ERR("Helper download poll status timeout @ %d", |
| 489 | hlprblknow); |
| 490 | goto done; |
| 491 | } |
| 492 | |
| 493 | /* Check if there is more data? */ |
| 494 | if (hlprblknow >= helperlen) |
| 495 | break; |
| 496 | |
| 497 | if (helperlen - hlprblknow < tx_len) |
| 498 | tx_len = helperlen - hlprblknow; |
| 499 | |
| 500 | /* Little-endian */ |
| 501 | helperbuf[0] = ((tx_len & 0x000000ff) >> 0); |
| 502 | helperbuf[1] = ((tx_len & 0x0000ff00) >> 8); |
| 503 | helperbuf[2] = ((tx_len & 0x00ff0000) >> 16); |
| 504 | helperbuf[3] = ((tx_len & 0xff000000) >> 24); |
| 505 | |
| 506 | memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow], |
| 507 | tx_len); |
| 508 | |
| 509 | /* Now send the data */ |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 510 | ret = sdio_writesb(card->func, card->ioport, helperbuf, |
| 511 | FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 512 | if (ret < 0) { |
| 513 | BT_ERR("IO error during helper download @ %d", |
| 514 | hlprblknow); |
| 515 | goto done; |
| 516 | } |
| 517 | |
| 518 | hlprblknow += tx_len; |
| 519 | } while (true); |
| 520 | |
| 521 | BT_DBG("Transferring helper image EOF block"); |
| 522 | |
| 523 | memset(helperbuf, 0x0, SDIO_BLOCK_SIZE); |
| 524 | |
| 525 | ret = sdio_writesb(card->func, card->ioport, helperbuf, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 526 | SDIO_BLOCK_SIZE); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 527 | if (ret < 0) { |
| 528 | BT_ERR("IO error in writing helper image EOF block"); |
| 529 | goto done; |
| 530 | } |
| 531 | |
| 532 | ret = 0; |
| 533 | |
| 534 | done: |
| 535 | kfree(tmphlprbuf); |
Jesper Juhl | 59f34fb | 2012-04-09 22:49:49 +0200 | [diff] [blame] | 536 | release_firmware(fw_helper); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card) |
| 541 | { |
| 542 | const struct firmware *fw_firmware = NULL; |
| 543 | const u8 *firmware = NULL; |
| 544 | int firmwarelen, tmpfwbufsz, ret; |
| 545 | unsigned int tries, offset; |
| 546 | u8 base0, base1; |
| 547 | void *tmpfwbuf = NULL; |
| 548 | u8 *fwbuf; |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 549 | u16 len, blksz_dl = card->sd_blksz_fw_dl; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 550 | int txlen = 0, tx_blocks = 0, count = 0; |
| 551 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 552 | ret = request_firmware(&fw_firmware, card->firmware, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 553 | &card->func->dev); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 554 | if ((ret < 0) || !fw_firmware) { |
| 555 | BT_ERR("request_firmware(firmware) failed, error code = %d", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 556 | ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 557 | ret = -ENOENT; |
| 558 | goto done; |
| 559 | } |
| 560 | |
| 561 | firmware = fw_firmware->data; |
| 562 | firmwarelen = fw_firmware->size; |
| 563 | |
| 564 | BT_DBG("Downloading FW image (%d bytes)", firmwarelen); |
| 565 | |
| 566 | tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN); |
Julia Lawall | f8df39f | 2010-05-13 22:02:03 +0200 | [diff] [blame] | 567 | tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 568 | if (!tmpfwbuf) { |
| 569 | BT_ERR("Unable to allocate buffer for firmware." |
| 570 | " Terminating download"); |
| 571 | ret = -ENOMEM; |
| 572 | goto done; |
| 573 | } |
| 574 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 575 | /* Ensure aligned firmware buffer */ |
| 576 | fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN); |
| 577 | |
| 578 | /* Perform firmware data transfer */ |
| 579 | offset = 0; |
| 580 | do { |
| 581 | ret = btmrvl_sdio_poll_card_status(card, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 582 | CARD_IO_READY | DN_LD_CARD_RDY); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 583 | if (ret < 0) { |
| 584 | BT_ERR("FW download with helper poll status" |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 585 | " timeout @ %d", offset); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 586 | goto done; |
| 587 | } |
| 588 | |
| 589 | /* Check if there is more data ? */ |
| 590 | if (offset >= firmwarelen) |
| 591 | break; |
| 592 | |
| 593 | for (tries = 0; tries < MAX_POLL_TRIES; tries++) { |
| 594 | base0 = sdio_readb(card->func, |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 595 | card->reg->sq_read_base_addr_a0, &ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 596 | if (ret) { |
| 597 | BT_ERR("BASE0 register read failed:" |
| 598 | " base0 = 0x%04X(%d)." |
| 599 | " Terminating download", |
| 600 | base0, base0); |
| 601 | ret = -EIO; |
| 602 | goto done; |
| 603 | } |
| 604 | base1 = sdio_readb(card->func, |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 605 | card->reg->sq_read_base_addr_a1, &ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 606 | if (ret) { |
| 607 | BT_ERR("BASE1 register read failed:" |
| 608 | " base1 = 0x%04X(%d)." |
| 609 | " Terminating download", |
| 610 | base1, base1); |
| 611 | ret = -EIO; |
| 612 | goto done; |
| 613 | } |
| 614 | |
| 615 | len = (((u16) base1) << 8) | base0; |
| 616 | if (len) |
| 617 | break; |
| 618 | |
| 619 | udelay(10); |
| 620 | } |
| 621 | |
| 622 | if (!len) |
| 623 | break; |
| 624 | else if (len > BTM_UPLD_SIZE) { |
| 625 | BT_ERR("FW download failure @%d, invalid length %d", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 626 | offset, len); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 627 | ret = -EINVAL; |
| 628 | goto done; |
| 629 | } |
| 630 | |
| 631 | txlen = len; |
| 632 | |
| 633 | if (len & BIT(0)) { |
| 634 | count++; |
| 635 | if (count > MAX_WRITE_IOMEM_RETRY) { |
| 636 | BT_ERR("FW download failure @%d, " |
| 637 | "over max retry count", offset); |
| 638 | ret = -EIO; |
| 639 | goto done; |
| 640 | } |
| 641 | BT_ERR("FW CRC error indicated by the helper: " |
| 642 | "len = 0x%04X, txlen = %d", len, txlen); |
| 643 | len &= ~BIT(0); |
| 644 | /* Set txlen to 0 so as to resend from same offset */ |
| 645 | txlen = 0; |
| 646 | } else { |
| 647 | count = 0; |
| 648 | |
| 649 | /* Last block ? */ |
| 650 | if (firmwarelen - offset < txlen) |
| 651 | txlen = firmwarelen - offset; |
| 652 | |
Andy Shevchenko | ba34dd3 | 2013-08-02 14:10:12 +0300 | [diff] [blame] | 653 | tx_blocks = DIV_ROUND_UP(txlen, blksz_dl); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 654 | |
| 655 | memcpy(fwbuf, &firmware[offset], txlen); |
| 656 | } |
| 657 | |
| 658 | ret = sdio_writesb(card->func, card->ioport, fwbuf, |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 659 | tx_blocks * blksz_dl); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 660 | |
| 661 | if (ret < 0) { |
| 662 | BT_ERR("FW download, writesb(%d) failed @%d", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 663 | count, offset); |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 664 | sdio_writeb(card->func, HOST_CMD53_FIN, |
| 665 | card->reg->cfg, &ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 666 | if (ret) |
| 667 | BT_ERR("writeb failed (CFG)"); |
| 668 | } |
| 669 | |
| 670 | offset += txlen; |
| 671 | } while (true); |
| 672 | |
Amitkumar Karwar | 1c6098e | 2015-01-01 00:13:42 -0800 | [diff] [blame] | 673 | BT_INFO("FW download over, size %d bytes", offset); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 674 | |
| 675 | ret = 0; |
| 676 | |
| 677 | done: |
| 678 | kfree(tmpfwbuf); |
Jesper Juhl | 59f34fb | 2012-04-09 22:49:49 +0200 | [diff] [blame] | 679 | release_firmware(fw_firmware); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 680 | return ret; |
| 681 | } |
| 682 | |
| 683 | static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv) |
| 684 | { |
| 685 | u16 buf_len = 0; |
Andrei Emeltchenko | 4263280 | 2012-09-28 14:36:08 +0300 | [diff] [blame] | 686 | int ret, num_blocks, blksz; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 687 | struct sk_buff *skb = NULL; |
| 688 | u32 type; |
| 689 | u8 *payload = NULL; |
| 690 | struct hci_dev *hdev = priv->btmrvl_dev.hcidev; |
| 691 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 692 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 693 | if (!card || !card->func) { |
| 694 | BT_ERR("card or function is NULL!"); |
| 695 | ret = -EINVAL; |
| 696 | goto exit; |
| 697 | } |
| 698 | |
| 699 | /* Read the length of data to be transferred */ |
| 700 | ret = btmrvl_sdio_read_rx_len(card, &buf_len); |
| 701 | if (ret < 0) { |
| 702 | BT_ERR("read rx_len failed"); |
| 703 | ret = -EIO; |
| 704 | goto exit; |
| 705 | } |
| 706 | |
| 707 | blksz = SDIO_BLOCK_SIZE; |
Andrei Emeltchenko | e678bad | 2012-09-28 14:36:09 +0300 | [diff] [blame] | 708 | num_blocks = DIV_ROUND_UP(buf_len, blksz); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 709 | |
| 710 | if (buf_len <= SDIO_HEADER_LEN |
Andrei Emeltchenko | 4263280 | 2012-09-28 14:36:08 +0300 | [diff] [blame] | 711 | || (num_blocks * blksz) > ALLOC_BUF_SIZE) { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 712 | BT_ERR("invalid packet length: %d", buf_len); |
| 713 | ret = -EINVAL; |
| 714 | goto exit; |
| 715 | } |
| 716 | |
| 717 | /* Allocate buffer */ |
Andrei Emeltchenko | 4263280 | 2012-09-28 14:36:08 +0300 | [diff] [blame] | 718 | skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_ATOMIC); |
Prasanna Karthik | 1f438c6 | 2015-09-25 09:03:14 +0000 | [diff] [blame] | 719 | if (!skb) { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 720 | BT_ERR("No free skb"); |
Wei Yongjun | 6ea81c4 | 2013-06-05 10:16:55 +0800 | [diff] [blame] | 721 | ret = -ENOMEM; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 722 | goto exit; |
| 723 | } |
| 724 | |
Bing Zhao | 3318b23 | 2009-07-08 11:44:14 -0700 | [diff] [blame] | 725 | if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) { |
| 726 | skb_put(skb, (unsigned long) skb->data & |
| 727 | (BTSDIO_DMA_ALIGN - 1)); |
| 728 | skb_pull(skb, (unsigned long) skb->data & |
| 729 | (BTSDIO_DMA_ALIGN - 1)); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Bing Zhao | 3318b23 | 2009-07-08 11:44:14 -0700 | [diff] [blame] | 732 | payload = skb->data; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 733 | |
| 734 | ret = sdio_readsb(card->func, payload, card->ioport, |
Andrei Emeltchenko | 4263280 | 2012-09-28 14:36:08 +0300 | [diff] [blame] | 735 | num_blocks * blksz); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 736 | if (ret < 0) { |
| 737 | BT_ERR("readsb failed: %d", ret); |
| 738 | ret = -EIO; |
| 739 | goto exit; |
| 740 | } |
| 741 | |
| 742 | /* This is SDIO specific header length: byte[2][1][0], type: byte[3] |
| 743 | * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor) |
| 744 | */ |
| 745 | |
| 746 | buf_len = payload[0]; |
Andrei Emeltchenko | 9cb23dd | 2012-09-28 14:36:10 +0300 | [diff] [blame] | 747 | buf_len |= payload[1] << 8; |
| 748 | buf_len |= payload[2] << 16; |
| 749 | |
| 750 | if (buf_len > blksz * num_blocks) { |
| 751 | BT_ERR("Skip incorrect packet: hdrlen %d buffer %d", |
| 752 | buf_len, blksz * num_blocks); |
| 753 | ret = -EIO; |
| 754 | goto exit; |
| 755 | } |
| 756 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 757 | type = payload[3]; |
| 758 | |
| 759 | switch (type) { |
| 760 | case HCI_ACLDATA_PKT: |
| 761 | case HCI_SCODATA_PKT: |
| 762 | case HCI_EVENT_PKT: |
Marcel Holtmann | 618e8bc | 2015-11-05 07:33:56 +0100 | [diff] [blame] | 763 | hci_skb_pkt_type(skb) = type; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 764 | skb_put(skb, buf_len); |
| 765 | skb_pull(skb, SDIO_HEADER_LEN); |
| 766 | |
Andrei Emeltchenko | ea1e76a | 2012-06-13 13:35:44 +0300 | [diff] [blame] | 767 | if (type == HCI_EVENT_PKT) { |
| 768 | if (btmrvl_check_evtpkt(priv, skb)) |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 769 | hci_recv_frame(hdev, skb); |
Andrei Emeltchenko | 2c2453f | 2012-07-09 13:57:18 +0300 | [diff] [blame] | 770 | } else { |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 771 | hci_recv_frame(hdev, skb); |
Andrei Emeltchenko | 2c2453f | 2012-07-09 13:57:18 +0300 | [diff] [blame] | 772 | } |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 773 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 774 | hdev->stat.byte_rx += buf_len; |
| 775 | break; |
| 776 | |
| 777 | case MRVL_VENDOR_PKT: |
Marcel Holtmann | 618e8bc | 2015-11-05 07:33:56 +0100 | [diff] [blame] | 778 | hci_skb_pkt_type(skb) = HCI_VENDOR_PKT; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 779 | skb_put(skb, buf_len); |
| 780 | skb_pull(skb, SDIO_HEADER_LEN); |
| 781 | |
| 782 | if (btmrvl_process_event(priv, skb)) |
Marcel Holtmann | e1a2617 | 2013-10-10 16:52:43 -0700 | [diff] [blame] | 783 | hci_recv_frame(hdev, skb); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 784 | |
| 785 | hdev->stat.byte_rx += buf_len; |
| 786 | break; |
| 787 | |
| 788 | default: |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 789 | BT_ERR("Unknown packet type:%d", type); |
Andrei Emeltchenko | 78c1b8e | 2012-10-10 17:41:33 +0300 | [diff] [blame] | 790 | BT_ERR("hex: %*ph", blksz * num_blocks, payload); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 791 | |
| 792 | kfree_skb(skb); |
| 793 | skb = NULL; |
| 794 | break; |
| 795 | } |
| 796 | |
| 797 | exit: |
| 798 | if (ret) { |
| 799 | hdev->stat.err_rx++; |
Wei Yongjun | 7923296 | 2012-08-28 21:12:48 +0800 | [diff] [blame] | 800 | kfree_skb(skb); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 803 | return ret; |
| 804 | } |
| 805 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 806 | static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 807 | { |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 808 | ulong flags; |
| 809 | u8 ireg; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 810 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 811 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 812 | spin_lock_irqsave(&priv->driver_lock, flags); |
| 813 | ireg = sdio_ireg; |
| 814 | sdio_ireg = 0; |
| 815 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 816 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 817 | sdio_claim_host(card->func); |
| 818 | if (ireg & DN_LD_HOST_INT_STATUS) { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 819 | if (priv->btmrvl_dev.tx_dnld_rdy) |
| 820 | BT_DBG("tx_done already received: " |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 821 | " int_status=0x%x", ireg); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 822 | else |
| 823 | priv->btmrvl_dev.tx_dnld_rdy = true; |
| 824 | } |
| 825 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 826 | if (ireg & UP_LD_HOST_INT_STATUS) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 827 | btmrvl_sdio_card_to_host(priv); |
| 828 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 829 | sdio_release_host(card->func); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 830 | |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 831 | return 0; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Bing Zhao | 0d36740 | 2014-03-28 14:54:39 -0700 | [diff] [blame] | 834 | static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg) |
| 835 | { |
| 836 | struct btmrvl_adapter *adapter = card->priv->adapter; |
| 837 | int ret; |
| 838 | |
| 839 | ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE); |
| 840 | if (ret) { |
| 841 | BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret); |
| 842 | return ret; |
| 843 | } |
| 844 | |
| 845 | *ireg = adapter->hw_regs[card->reg->host_intstatus]; |
| 846 | BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg); |
| 847 | |
| 848 | return 0; |
| 849 | } |
| 850 | |
Bing Zhao | 8af4840 | 2014-03-27 21:30:42 -0700 | [diff] [blame] | 851 | static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg) |
| 852 | { |
| 853 | int ret; |
| 854 | |
| 855 | *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret); |
| 856 | if (ret) { |
| 857 | BT_ERR("sdio_readb: read int status failed: %d", ret); |
| 858 | return ret; |
| 859 | } |
| 860 | |
| 861 | if (*ireg) { |
| 862 | /* |
| 863 | * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS |
| 864 | * Clear the interrupt status register and re-enable the |
| 865 | * interrupt. |
| 866 | */ |
| 867 | BT_DBG("int_status = 0x%x", *ireg); |
| 868 | |
| 869 | sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS | |
| 870 | UP_LD_HOST_INT_STATUS), |
| 871 | card->reg->host_intstatus, &ret); |
| 872 | if (ret) { |
| 873 | BT_ERR("sdio_writeb: clear int status failed: %d", ret); |
| 874 | return ret; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | return 0; |
| 879 | } |
| 880 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 881 | static void btmrvl_sdio_interrupt(struct sdio_func *func) |
| 882 | { |
| 883 | struct btmrvl_private *priv; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 884 | struct btmrvl_sdio_card *card; |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 885 | ulong flags; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 886 | u8 ireg = 0; |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 887 | int ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 888 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 889 | card = sdio_get_drvdata(func); |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 890 | if (!card || !card->priv) { |
Amitkumar Karwar | 7365d47 | 2014-11-24 02:40:52 -0800 | [diff] [blame] | 891 | BT_ERR("sbi_interrupt(%p) card or priv is NULL, card=%p", |
| 892 | func, card); |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 893 | return; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 894 | } |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 895 | |
| 896 | priv = card->priv; |
| 897 | |
Amitkumar Karwar | 7b4b874 | 2015-01-01 00:13:41 -0800 | [diff] [blame] | 898 | if (priv->surprise_removed) |
| 899 | return; |
| 900 | |
Bing Zhao | 0d36740 | 2014-03-28 14:54:39 -0700 | [diff] [blame] | 901 | if (card->reg->int_read_to_clear) |
| 902 | ret = btmrvl_sdio_read_to_clear(card, &ireg); |
| 903 | else |
| 904 | ret = btmrvl_sdio_write_to_clear(card, &ireg); |
| 905 | |
Bing Zhao | 8af4840 | 2014-03-27 21:30:42 -0700 | [diff] [blame] | 906 | if (ret) |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 907 | return; |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 908 | |
| 909 | spin_lock_irqsave(&priv->driver_lock, flags); |
| 910 | sdio_ireg |= ireg; |
| 911 | spin_unlock_irqrestore(&priv->driver_lock, flags); |
| 912 | |
| 913 | btmrvl_interrupt(priv); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card) |
| 917 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 918 | struct sdio_func *func; |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 919 | u8 reg; |
| 920 | int ret = 0; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 921 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 922 | if (!card || !card->func) { |
| 923 | BT_ERR("Error: card or function is NULL!"); |
| 924 | ret = -EINVAL; |
| 925 | goto failed; |
| 926 | } |
| 927 | |
| 928 | func = card->func; |
| 929 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 930 | sdio_claim_host(func); |
| 931 | |
| 932 | ret = sdio_enable_func(func); |
| 933 | if (ret) { |
| 934 | BT_ERR("sdio_enable_func() failed: ret=%d", ret); |
| 935 | ret = -EIO; |
| 936 | goto release_host; |
| 937 | } |
| 938 | |
| 939 | ret = sdio_claim_irq(func, btmrvl_sdio_interrupt); |
| 940 | if (ret) { |
| 941 | BT_ERR("sdio_claim_irq failed: ret=%d", ret); |
| 942 | ret = -EIO; |
| 943 | goto disable_func; |
| 944 | } |
| 945 | |
| 946 | ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE); |
| 947 | if (ret) { |
| 948 | BT_ERR("cannot set SDIO block size"); |
| 949 | ret = -EIO; |
| 950 | goto release_irq; |
| 951 | } |
| 952 | |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 953 | reg = sdio_readb(func, card->reg->io_port_0, &ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 954 | if (ret < 0) { |
| 955 | ret = -EIO; |
| 956 | goto release_irq; |
| 957 | } |
| 958 | |
| 959 | card->ioport = reg; |
| 960 | |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 961 | reg = sdio_readb(func, card->reg->io_port_1, &ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 962 | if (ret < 0) { |
| 963 | ret = -EIO; |
| 964 | goto release_irq; |
| 965 | } |
| 966 | |
| 967 | card->ioport |= (reg << 8); |
| 968 | |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 969 | reg = sdio_readb(func, card->reg->io_port_2, &ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 970 | if (ret < 0) { |
| 971 | ret = -EIO; |
| 972 | goto release_irq; |
| 973 | } |
| 974 | |
| 975 | card->ioport |= (reg << 16); |
| 976 | |
| 977 | BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport); |
| 978 | |
Bing Zhao | 0d36740 | 2014-03-28 14:54:39 -0700 | [diff] [blame] | 979 | if (card->reg->int_read_to_clear) { |
| 980 | reg = sdio_readb(func, card->reg->host_int_rsr, &ret); |
| 981 | if (ret < 0) { |
| 982 | ret = -EIO; |
| 983 | goto release_irq; |
| 984 | } |
| 985 | sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret); |
| 986 | if (ret < 0) { |
| 987 | ret = -EIO; |
| 988 | goto release_irq; |
| 989 | } |
| 990 | |
| 991 | reg = sdio_readb(func, card->reg->card_misc_cfg, &ret); |
| 992 | if (ret < 0) { |
| 993 | ret = -EIO; |
| 994 | goto release_irq; |
| 995 | } |
| 996 | sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret); |
| 997 | if (ret < 0) { |
| 998 | ret = -EIO; |
| 999 | goto release_irq; |
| 1000 | } |
| 1001 | } |
| 1002 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1003 | sdio_set_drvdata(func, card); |
| 1004 | |
| 1005 | sdio_release_host(func); |
| 1006 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1007 | return 0; |
| 1008 | |
| 1009 | release_irq: |
| 1010 | sdio_release_irq(func); |
| 1011 | |
| 1012 | disable_func: |
| 1013 | sdio_disable_func(func); |
| 1014 | |
| 1015 | release_host: |
| 1016 | sdio_release_host(func); |
| 1017 | |
| 1018 | failed: |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1019 | return ret; |
| 1020 | } |
| 1021 | |
| 1022 | static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card) |
| 1023 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1024 | if (card && card->func) { |
| 1025 | sdio_claim_host(card->func); |
| 1026 | sdio_release_irq(card->func); |
| 1027 | sdio_disable_func(card->func); |
| 1028 | sdio_release_host(card->func); |
| 1029 | sdio_set_drvdata(card->func, NULL); |
| 1030 | } |
| 1031 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1032 | return 0; |
| 1033 | } |
| 1034 | |
| 1035 | static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card) |
| 1036 | { |
| 1037 | int ret; |
| 1038 | |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 1039 | if (!card || !card->func) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1040 | return -EINVAL; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1041 | |
| 1042 | sdio_claim_host(card->func); |
| 1043 | |
| 1044 | ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE); |
| 1045 | |
| 1046 | btmrvl_sdio_get_rx_unit(card); |
| 1047 | |
| 1048 | sdio_release_host(card->func); |
| 1049 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1050 | return ret; |
| 1051 | } |
| 1052 | |
| 1053 | static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card) |
| 1054 | { |
| 1055 | int ret; |
| 1056 | |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 1057 | if (!card || !card->func) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1058 | return -EINVAL; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1059 | |
| 1060 | sdio_claim_host(card->func); |
| 1061 | |
| 1062 | ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE); |
| 1063 | |
| 1064 | sdio_release_host(card->func); |
| 1065 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1066 | return ret; |
| 1067 | } |
| 1068 | |
| 1069 | static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv, |
| 1070 | u8 *payload, u16 nb) |
| 1071 | { |
| 1072 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 1073 | int ret = 0; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1074 | int blksz; |
| 1075 | int i = 0; |
| 1076 | u8 *buf = NULL; |
| 1077 | void *tmpbuf = NULL; |
| 1078 | int tmpbufsz; |
| 1079 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1080 | if (!card || !card->func) { |
| 1081 | BT_ERR("card or function is NULL!"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1082 | return -EINVAL; |
| 1083 | } |
| 1084 | |
Ricky Liang | d2f3024 | 2016-06-28 15:06:57 +0800 | [diff] [blame] | 1085 | blksz = DIV_ROUND_UP(nb, SDIO_BLOCK_SIZE) * SDIO_BLOCK_SIZE; |
| 1086 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1087 | buf = payload; |
Ricky Liang | d2f3024 | 2016-06-28 15:06:57 +0800 | [diff] [blame] | 1088 | if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1) || |
| 1089 | nb < blksz) { |
| 1090 | tmpbufsz = ALIGN_SZ(blksz, BTSDIO_DMA_ALIGN) + |
| 1091 | BTSDIO_DMA_ALIGN; |
Julia Lawall | 5959809 | 2009-08-06 22:05:18 +0200 | [diff] [blame] | 1092 | tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL); |
| 1093 | if (!tmpbuf) |
| 1094 | return -ENOMEM; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1095 | buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN); |
| 1096 | memcpy(buf, payload, nb); |
| 1097 | } |
| 1098 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1099 | sdio_claim_host(card->func); |
| 1100 | |
| 1101 | do { |
| 1102 | /* Transfer data to card */ |
| 1103 | ret = sdio_writesb(card->func, card->ioport, buf, |
Ricky Liang | d2f3024 | 2016-06-28 15:06:57 +0800 | [diff] [blame] | 1104 | blksz); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1105 | if (ret < 0) { |
| 1106 | i++; |
| 1107 | BT_ERR("i=%d writesb failed: %d", i, ret); |
Andrei Emeltchenko | 78c1b8e | 2012-10-10 17:41:33 +0300 | [diff] [blame] | 1108 | BT_ERR("hex: %*ph", nb, payload); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1109 | ret = -EIO; |
| 1110 | if (i > MAX_WRITE_IOMEM_RETRY) |
| 1111 | goto exit; |
| 1112 | } |
| 1113 | } while (ret); |
| 1114 | |
| 1115 | priv->btmrvl_dev.tx_dnld_rdy = false; |
| 1116 | |
| 1117 | exit: |
| 1118 | sdio_release_host(card->func); |
Yoichi Yuasa | 88d1a0c | 2010-02-04 09:55:51 +0900 | [diff] [blame] | 1119 | kfree(tmpbuf); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1120 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1121 | return ret; |
| 1122 | } |
| 1123 | |
| 1124 | static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card) |
| 1125 | { |
Gustavo Padovan | faff7f7 | 2013-04-18 19:35:33 -0300 | [diff] [blame] | 1126 | int ret; |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 1127 | u8 fws0; |
| 1128 | int pollnum = MAX_POLL_TRIES; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1129 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1130 | if (!card || !card->func) { |
| 1131 | BT_ERR("card or function is NULL!"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1132 | return -EINVAL; |
| 1133 | } |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1134 | |
| 1135 | if (!btmrvl_sdio_verify_fw_download(card, 1)) { |
| 1136 | BT_DBG("Firmware already downloaded!"); |
Andreas Fenkart | c1c999e | 2013-04-22 11:10:22 +0200 | [diff] [blame] | 1137 | return 0; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Andreas Fenkart | c1c999e | 2013-04-22 11:10:22 +0200 | [diff] [blame] | 1140 | sdio_claim_host(card->func); |
| 1141 | |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 1142 | /* Check if other function driver is downloading the firmware */ |
| 1143 | fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1144 | if (ret) { |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 1145 | BT_ERR("Failed to read FW downloading status!"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1146 | ret = -EIO; |
| 1147 | goto done; |
| 1148 | } |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 1149 | if (fws0) { |
| 1150 | BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1151 | |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 1152 | /* Give other function more time to download the firmware */ |
| 1153 | pollnum *= 10; |
| 1154 | } else { |
| 1155 | if (card->helper) { |
| 1156 | ret = btmrvl_sdio_download_helper(card); |
| 1157 | if (ret) { |
| 1158 | BT_ERR("Failed to download helper!"); |
| 1159 | ret = -EIO; |
| 1160 | goto done; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | if (btmrvl_sdio_download_fw_w_helper(card)) { |
| 1165 | BT_ERR("Failed to download firmware!"); |
| 1166 | ret = -EIO; |
| 1167 | goto done; |
| 1168 | } |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
Andreas Fenkart | 69676b1 | 2013-04-22 11:10:23 +0200 | [diff] [blame] | 1171 | /* |
| 1172 | * winner or not, with this test the FW synchronizes when the |
| 1173 | * module can continue its initialization |
| 1174 | */ |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 1175 | if (btmrvl_sdio_verify_fw_download(card, pollnum)) { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1176 | BT_ERR("FW failed to be active in time!"); |
Chin-Ran Lo | 86f7ac7 | 2015-12-29 04:26:33 -0800 | [diff] [blame] | 1177 | ret = -ETIMEDOUT; |
| 1178 | goto done; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
Aniket Nagarnaik | 4863e4c | 2015-08-06 03:43:09 -0700 | [diff] [blame] | 1181 | sdio_release_host(card->func); |
| 1182 | |
Andreas Fenkart | c1c999e | 2013-04-22 11:10:22 +0200 | [diff] [blame] | 1183 | return 0; |
| 1184 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1185 | done: |
| 1186 | sdio_release_host(card->func); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1187 | return ret; |
| 1188 | } |
| 1189 | |
| 1190 | static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv) |
| 1191 | { |
| 1192 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 1193 | int ret = 0; |
| 1194 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1195 | if (!card || !card->func) { |
| 1196 | BT_ERR("card or function is NULL!"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1197 | return -EINVAL; |
| 1198 | } |
| 1199 | |
| 1200 | sdio_claim_host(card->func); |
| 1201 | |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 1202 | sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1203 | |
| 1204 | sdio_release_host(card->func); |
| 1205 | |
| 1206 | BT_DBG("wake up firmware"); |
| 1207 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1208 | return ret; |
| 1209 | } |
| 1210 | |
Xinming Hu | dc75961 | 2014-11-24 02:40:53 -0800 | [diff] [blame] | 1211 | static void btmrvl_sdio_dump_regs(struct btmrvl_private *priv) |
| 1212 | { |
| 1213 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 1214 | int ret = 0; |
| 1215 | unsigned int reg, reg_start, reg_end; |
| 1216 | char buf[256], *ptr; |
| 1217 | u8 loop, func, data; |
| 1218 | int MAX_LOOP = 2; |
| 1219 | |
| 1220 | btmrvl_sdio_wakeup_fw(priv); |
| 1221 | sdio_claim_host(card->func); |
| 1222 | |
| 1223 | for (loop = 0; loop < MAX_LOOP; loop++) { |
| 1224 | memset(buf, 0, sizeof(buf)); |
| 1225 | ptr = buf; |
| 1226 | |
| 1227 | if (loop == 0) { |
| 1228 | /* Read the registers of SDIO function0 */ |
| 1229 | func = loop; |
| 1230 | reg_start = 0; |
| 1231 | reg_end = 9; |
| 1232 | } else { |
| 1233 | func = 2; |
| 1234 | reg_start = 0; |
| 1235 | reg_end = 0x09; |
| 1236 | } |
| 1237 | |
| 1238 | ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ", |
| 1239 | func, reg_start, reg_end); |
| 1240 | for (reg = reg_start; reg <= reg_end; reg++) { |
| 1241 | if (func == 0) |
| 1242 | data = sdio_f0_readb(card->func, reg, &ret); |
| 1243 | else |
| 1244 | data = sdio_readb(card->func, reg, &ret); |
| 1245 | |
| 1246 | if (!ret) { |
| 1247 | ptr += sprintf(ptr, "%02x ", data); |
| 1248 | } else { |
| 1249 | ptr += sprintf(ptr, "ERR"); |
| 1250 | break; |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | BT_INFO("%s", buf); |
| 1255 | } |
| 1256 | |
| 1257 | sdio_release_host(card->func); |
| 1258 | } |
| 1259 | |
| 1260 | /* This function read/write firmware */ |
| 1261 | static enum |
| 1262 | rdwr_status btmrvl_sdio_rdwr_firmware(struct btmrvl_private *priv, |
| 1263 | u8 doneflag) |
| 1264 | { |
| 1265 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 1266 | int ret, tries; |
| 1267 | u8 ctrl_data = 0; |
| 1268 | |
| 1269 | sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl, |
| 1270 | &ret); |
| 1271 | |
| 1272 | if (ret) { |
| 1273 | BT_ERR("SDIO write err"); |
| 1274 | return RDWR_STATUS_FAILURE; |
| 1275 | } |
| 1276 | |
| 1277 | for (tries = 0; tries < MAX_POLL_TRIES; tries++) { |
| 1278 | ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl, |
| 1279 | &ret); |
| 1280 | |
| 1281 | if (ret) { |
| 1282 | BT_ERR("SDIO read err"); |
| 1283 | return RDWR_STATUS_FAILURE; |
| 1284 | } |
| 1285 | |
| 1286 | if (ctrl_data == FW_DUMP_DONE) |
| 1287 | break; |
| 1288 | if (doneflag && ctrl_data == doneflag) |
| 1289 | return RDWR_STATUS_DONE; |
| 1290 | if (ctrl_data != FW_DUMP_HOST_READY) { |
| 1291 | BT_INFO("The ctrl reg was changed, re-try again!"); |
| 1292 | sdio_writeb(card->func, FW_DUMP_HOST_READY, |
| 1293 | card->reg->fw_dump_ctrl, &ret); |
| 1294 | if (ret) { |
| 1295 | BT_ERR("SDIO write err"); |
| 1296 | return RDWR_STATUS_FAILURE; |
| 1297 | } |
| 1298 | } |
| 1299 | usleep_range(100, 200); |
| 1300 | } |
| 1301 | |
| 1302 | if (ctrl_data == FW_DUMP_HOST_READY) { |
| 1303 | BT_ERR("Fail to pull ctrl_data"); |
| 1304 | return RDWR_STATUS_FAILURE; |
| 1305 | } |
| 1306 | |
| 1307 | return RDWR_STATUS_SUCCESS; |
| 1308 | } |
| 1309 | |
| 1310 | /* This function dump sdio register and memory data */ |
| 1311 | static void btmrvl_sdio_dump_firmware(struct btmrvl_private *priv) |
| 1312 | { |
| 1313 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 1314 | int ret = 0; |
| 1315 | unsigned int reg, reg_start, reg_end; |
| 1316 | enum rdwr_status stat; |
| 1317 | u8 *dbg_ptr, *end_ptr, *fw_dump_data, *fw_dump_ptr; |
Xinming Hu | a1e85f04 | 2015-04-21 06:59:56 -0700 | [diff] [blame] | 1318 | u8 dump_num = 0, idx, i, read_reg, doneflag = 0; |
Xinming Hu | dc75961 | 2014-11-24 02:40:53 -0800 | [diff] [blame] | 1319 | u32 memory_size, fw_dump_len = 0; |
| 1320 | |
| 1321 | /* dump sdio register first */ |
| 1322 | btmrvl_sdio_dump_regs(priv); |
| 1323 | |
| 1324 | if (!card->supports_fw_dump) { |
| 1325 | BT_ERR("Firmware dump not supported for this card!"); |
| 1326 | return; |
| 1327 | } |
| 1328 | |
| 1329 | for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) { |
| 1330 | struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx]; |
| 1331 | |
| 1332 | if (entry->mem_ptr) { |
| 1333 | vfree(entry->mem_ptr); |
| 1334 | entry->mem_ptr = NULL; |
| 1335 | } |
| 1336 | entry->mem_size = 0; |
| 1337 | } |
| 1338 | |
| 1339 | btmrvl_sdio_wakeup_fw(priv); |
| 1340 | sdio_claim_host(card->func); |
| 1341 | |
| 1342 | BT_INFO("== btmrvl firmware dump start =="); |
| 1343 | |
| 1344 | stat = btmrvl_sdio_rdwr_firmware(priv, doneflag); |
| 1345 | if (stat == RDWR_STATUS_FAILURE) |
| 1346 | goto done; |
| 1347 | |
| 1348 | reg = card->reg->fw_dump_start; |
| 1349 | /* Read the number of the memories which will dump */ |
| 1350 | dump_num = sdio_readb(card->func, reg, &ret); |
| 1351 | |
| 1352 | if (ret) { |
| 1353 | BT_ERR("SDIO read memory length err"); |
| 1354 | goto done; |
| 1355 | } |
| 1356 | |
| 1357 | /* Read the length of every memory which will dump */ |
| 1358 | for (idx = 0; idx < dump_num; idx++) { |
| 1359 | struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx]; |
| 1360 | |
| 1361 | stat = btmrvl_sdio_rdwr_firmware(priv, doneflag); |
| 1362 | if (stat == RDWR_STATUS_FAILURE) |
| 1363 | goto done; |
| 1364 | |
| 1365 | memory_size = 0; |
| 1366 | reg = card->reg->fw_dump_start; |
| 1367 | for (i = 0; i < 4; i++) { |
| 1368 | read_reg = sdio_readb(card->func, reg, &ret); |
| 1369 | if (ret) { |
| 1370 | BT_ERR("SDIO read err"); |
| 1371 | goto done; |
| 1372 | } |
| 1373 | memory_size |= (read_reg << i*8); |
| 1374 | reg++; |
| 1375 | } |
| 1376 | |
| 1377 | if (memory_size == 0) { |
| 1378 | BT_INFO("Firmware dump finished!"); |
Nachiket Kukade | 475b9ea | 2015-09-18 06:40:40 -0700 | [diff] [blame] | 1379 | sdio_writeb(card->func, FW_DUMP_READ_DONE, |
| 1380 | card->reg->fw_dump_ctrl, &ret); |
| 1381 | if (ret) { |
| 1382 | BT_ERR("SDIO Write MEMDUMP_FINISH ERR"); |
| 1383 | goto done; |
| 1384 | } |
Xinming Hu | dc75961 | 2014-11-24 02:40:53 -0800 | [diff] [blame] | 1385 | break; |
| 1386 | } |
| 1387 | |
| 1388 | BT_INFO("%s_SIZE=0x%x", entry->mem_name, memory_size); |
| 1389 | entry->mem_ptr = vzalloc(memory_size + 1); |
| 1390 | entry->mem_size = memory_size; |
| 1391 | if (!entry->mem_ptr) { |
| 1392 | BT_ERR("Vzalloc %s failed", entry->mem_name); |
| 1393 | goto done; |
| 1394 | } |
| 1395 | |
| 1396 | fw_dump_len += (strlen("========Start dump ") + |
| 1397 | strlen(entry->mem_name) + |
| 1398 | strlen("========\n") + |
| 1399 | (memory_size + 1) + |
| 1400 | strlen("\n========End dump========\n")); |
| 1401 | |
| 1402 | dbg_ptr = entry->mem_ptr; |
| 1403 | end_ptr = dbg_ptr + memory_size; |
| 1404 | |
| 1405 | doneflag = entry->done_flag; |
| 1406 | BT_INFO("Start %s output, please wait...", |
| 1407 | entry->mem_name); |
| 1408 | |
| 1409 | do { |
| 1410 | stat = btmrvl_sdio_rdwr_firmware(priv, doneflag); |
| 1411 | if (stat == RDWR_STATUS_FAILURE) |
| 1412 | goto done; |
| 1413 | |
| 1414 | reg_start = card->reg->fw_dump_start; |
| 1415 | reg_end = card->reg->fw_dump_end; |
| 1416 | for (reg = reg_start; reg <= reg_end; reg++) { |
| 1417 | *dbg_ptr = sdio_readb(card->func, reg, &ret); |
| 1418 | if (ret) { |
| 1419 | BT_ERR("SDIO read err"); |
| 1420 | goto done; |
| 1421 | } |
| 1422 | if (dbg_ptr < end_ptr) |
| 1423 | dbg_ptr++; |
| 1424 | else |
| 1425 | BT_ERR("Allocated buffer not enough"); |
| 1426 | } |
| 1427 | |
| 1428 | if (stat != RDWR_STATUS_DONE) { |
| 1429 | continue; |
| 1430 | } else { |
| 1431 | BT_INFO("%s done: size=0x%tx", |
| 1432 | entry->mem_name, |
| 1433 | dbg_ptr - entry->mem_ptr); |
| 1434 | break; |
| 1435 | } |
| 1436 | } while (1); |
| 1437 | } |
| 1438 | |
| 1439 | BT_INFO("== btmrvl firmware dump end =="); |
| 1440 | |
| 1441 | done: |
| 1442 | sdio_release_host(card->func); |
| 1443 | |
| 1444 | if (fw_dump_len == 0) |
| 1445 | return; |
| 1446 | |
| 1447 | fw_dump_data = vzalloc(fw_dump_len+1); |
| 1448 | if (!fw_dump_data) { |
| 1449 | BT_ERR("Vzalloc fw_dump_data fail!"); |
| 1450 | return; |
| 1451 | } |
| 1452 | fw_dump_ptr = fw_dump_data; |
| 1453 | |
| 1454 | /* Dump all the memory data into single file, a userspace script will |
| 1455 | be used to split all the memory data to multiple files*/ |
| 1456 | BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump start"); |
| 1457 | for (idx = 0; idx < dump_num; idx++) { |
| 1458 | struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx]; |
| 1459 | |
| 1460 | if (entry->mem_ptr) { |
| 1461 | strcpy(fw_dump_ptr, "========Start dump "); |
| 1462 | fw_dump_ptr += strlen("========Start dump "); |
| 1463 | |
| 1464 | strcpy(fw_dump_ptr, entry->mem_name); |
| 1465 | fw_dump_ptr += strlen(entry->mem_name); |
| 1466 | |
| 1467 | strcpy(fw_dump_ptr, "========\n"); |
| 1468 | fw_dump_ptr += strlen("========\n"); |
| 1469 | |
| 1470 | memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size); |
| 1471 | fw_dump_ptr += entry->mem_size; |
| 1472 | |
| 1473 | strcpy(fw_dump_ptr, "\n========End dump========\n"); |
| 1474 | fw_dump_ptr += strlen("\n========End dump========\n"); |
| 1475 | |
| 1476 | vfree(mem_type_mapping_tbl[idx].mem_ptr); |
| 1477 | mem_type_mapping_tbl[idx].mem_ptr = NULL; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | /* fw_dump_data will be free in device coredump release function |
| 1482 | after 5 min*/ |
Shengzhen Li | d490263 | 2015-08-19 03:12:19 -0700 | [diff] [blame] | 1483 | dev_coredumpv(&card->func->dev, fw_dump_data, fw_dump_len, GFP_KERNEL); |
Xinming Hu | dc75961 | 2014-11-24 02:40:53 -0800 | [diff] [blame] | 1484 | BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump end"); |
| 1485 | } |
| 1486 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1487 | static int btmrvl_sdio_probe(struct sdio_func *func, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 1488 | const struct sdio_device_id *id) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1489 | { |
| 1490 | int ret = 0; |
| 1491 | struct btmrvl_private *priv = NULL; |
| 1492 | struct btmrvl_sdio_card *card = NULL; |
| 1493 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1494 | BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d", |
| 1495 | id->vendor, id->device, id->class, func->num); |
| 1496 | |
Sachin Kamat | eb17ea3 | 2012-07-27 12:38:36 +0530 | [diff] [blame] | 1497 | card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL); |
| 1498 | if (!card) |
| 1499 | return -ENOMEM; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1500 | |
| 1501 | card->func = func; |
| 1502 | |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 1503 | if (id->driver_data) { |
| 1504 | struct btmrvl_sdio_device *data = (void *) id->driver_data; |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 1505 | card->helper = data->helper; |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 1506 | card->firmware = data->firmware; |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 1507 | card->reg = data->reg; |
| 1508 | card->sd_blksz_fw_dl = data->sd_blksz_fw_dl; |
Bing Zhao | 4df82b5 | 2014-03-31 14:41:44 -0700 | [diff] [blame] | 1509 | card->support_pscan_win_report = data->support_pscan_win_report; |
Xinming Hu | dc75961 | 2014-11-24 02:40:53 -0800 | [diff] [blame] | 1510 | card->supports_fw_dump = data->supports_fw_dump; |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 1511 | } |
| 1512 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1513 | if (btmrvl_sdio_register_dev(card) < 0) { |
| 1514 | BT_ERR("Failed to register BT device!"); |
Sachin Kamat | eb17ea3 | 2012-07-27 12:38:36 +0530 | [diff] [blame] | 1515 | return -ENODEV; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | /* Disable the interrupts on the card */ |
| 1519 | btmrvl_sdio_disable_host_int(card); |
| 1520 | |
| 1521 | if (btmrvl_sdio_download_fw(card)) { |
| 1522 | BT_ERR("Downloading firmware failed!"); |
| 1523 | ret = -ENODEV; |
| 1524 | goto unreg_dev; |
| 1525 | } |
| 1526 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1527 | btmrvl_sdio_enable_host_int(card); |
| 1528 | |
Xinming Hu | bb7f4f0 | 2016-04-26 06:57:27 -0700 | [diff] [blame] | 1529 | /* Device tree node parsing and platform specific configuration*/ |
| 1530 | btmrvl_sdio_probe_of(&func->dev, card); |
| 1531 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1532 | priv = btmrvl_add_card(card); |
| 1533 | if (!priv) { |
| 1534 | BT_ERR("Initializing card failed!"); |
| 1535 | ret = -ENODEV; |
| 1536 | goto disable_host_int; |
| 1537 | } |
| 1538 | |
| 1539 | card->priv = priv; |
| 1540 | |
| 1541 | /* Initialize the interface specific function pointers */ |
| 1542 | priv->hw_host_to_card = btmrvl_sdio_host_to_card; |
| 1543 | priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw; |
Amitkumar Karwar | d1d10d7 | 2010-05-27 16:38:37 -0700 | [diff] [blame] | 1544 | priv->hw_process_int_status = btmrvl_sdio_process_int_status; |
Xinming Hu | dc75961 | 2014-11-24 02:40:53 -0800 | [diff] [blame] | 1545 | priv->firmware_dump = btmrvl_sdio_dump_firmware; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1546 | |
Bing Zhao | 6406160 | 2010-03-03 14:37:36 -0800 | [diff] [blame] | 1547 | if (btmrvl_register_hdev(priv)) { |
| 1548 | BT_ERR("Register hdev failed!"); |
| 1549 | ret = -ENODEV; |
| 1550 | goto disable_host_int; |
| 1551 | } |
| 1552 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1553 | return 0; |
| 1554 | |
| 1555 | disable_host_int: |
| 1556 | btmrvl_sdio_disable_host_int(card); |
| 1557 | unreg_dev: |
| 1558 | btmrvl_sdio_unregister_dev(card); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1559 | return ret; |
| 1560 | } |
| 1561 | |
| 1562 | static void btmrvl_sdio_remove(struct sdio_func *func) |
| 1563 | { |
| 1564 | struct btmrvl_sdio_card *card; |
| 1565 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1566 | if (func) { |
| 1567 | card = sdio_get_drvdata(func); |
| 1568 | if (card) { |
| 1569 | /* Send SHUTDOWN command & disable interrupt |
| 1570 | * if user removes the module. |
| 1571 | */ |
| 1572 | if (user_rmmod) { |
| 1573 | btmrvl_send_module_cfg_cmd(card->priv, |
| 1574 | MODULE_SHUTDOWN_REQ); |
| 1575 | btmrvl_sdio_disable_host_int(card); |
| 1576 | } |
| 1577 | BT_DBG("unregester dev"); |
Amitkumar Karwar | 7b4b874 | 2015-01-01 00:13:41 -0800 | [diff] [blame] | 1578 | card->priv->surprise_removed = true; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1579 | btmrvl_sdio_unregister_dev(card); |
| 1580 | btmrvl_remove_card(card->priv); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1581 | } |
| 1582 | } |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
Amitkumar Karwar | ba54a16 | 2012-04-25 11:43:54 -0700 | [diff] [blame] | 1585 | static int btmrvl_sdio_suspend(struct device *dev) |
| 1586 | { |
| 1587 | struct sdio_func *func = dev_to_sdio_func(dev); |
| 1588 | struct btmrvl_sdio_card *card; |
| 1589 | struct btmrvl_private *priv; |
| 1590 | mmc_pm_flag_t pm_flags; |
| 1591 | struct hci_dev *hcidev; |
| 1592 | |
| 1593 | if (func) { |
| 1594 | pm_flags = sdio_get_host_pm_caps(func); |
| 1595 | BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func), |
| 1596 | pm_flags); |
| 1597 | if (!(pm_flags & MMC_PM_KEEP_POWER)) { |
| 1598 | BT_ERR("%s: cannot remain alive while suspended", |
| 1599 | sdio_func_id(func)); |
| 1600 | return -ENOSYS; |
| 1601 | } |
| 1602 | card = sdio_get_drvdata(func); |
| 1603 | if (!card || !card->priv) { |
| 1604 | BT_ERR("card or priv structure is not valid"); |
| 1605 | return 0; |
| 1606 | } |
| 1607 | } else { |
| 1608 | BT_ERR("sdio_func is not specified"); |
| 1609 | return 0; |
| 1610 | } |
| 1611 | |
Xinming Hu | bb7f4f0 | 2016-04-26 06:57:27 -0700 | [diff] [blame] | 1612 | /* Enable platform specific wakeup interrupt */ |
| 1613 | if (card->plt_wake_cfg && card->plt_wake_cfg->irq_bt >= 0) { |
| 1614 | card->plt_wake_cfg->wake_by_bt = false; |
| 1615 | enable_irq(card->plt_wake_cfg->irq_bt); |
| 1616 | enable_irq_wake(card->plt_wake_cfg->irq_bt); |
| 1617 | } |
| 1618 | |
Amitkumar Karwar | ba54a16 | 2012-04-25 11:43:54 -0700 | [diff] [blame] | 1619 | priv = card->priv; |
Chin-Ran Lo | 8cf60cf | 2016-01-06 06:34:38 -0800 | [diff] [blame] | 1620 | priv->adapter->is_suspending = true; |
Chin-Ran Lo | 5bf8a74 | 2014-07-14 21:05:37 -0700 | [diff] [blame] | 1621 | hcidev = priv->btmrvl_dev.hcidev; |
| 1622 | BT_DBG("%s: SDIO suspend", hcidev->name); |
| 1623 | hci_suspend_dev(hcidev); |
Amitkumar Karwar | ba54a16 | 2012-04-25 11:43:54 -0700 | [diff] [blame] | 1624 | |
| 1625 | if (priv->adapter->hs_state != HS_ACTIVATED) { |
| 1626 | if (btmrvl_enable_hs(priv)) { |
| 1627 | BT_ERR("HS not actived, suspend failed!"); |
Amitkumar Karwar | e7acf43 | 2016-07-15 16:20:52 +0530 | [diff] [blame] | 1628 | priv->adapter->is_suspending = false; |
Amitkumar Karwar | ba54a16 | 2012-04-25 11:43:54 -0700 | [diff] [blame] | 1629 | return -EBUSY; |
| 1630 | } |
| 1631 | } |
Amitkumar Karwar | ba54a16 | 2012-04-25 11:43:54 -0700 | [diff] [blame] | 1632 | |
Chin-Ran Lo | 8cf60cf | 2016-01-06 06:34:38 -0800 | [diff] [blame] | 1633 | priv->adapter->is_suspending = false; |
Amitkumar Karwar | ba54a16 | 2012-04-25 11:43:54 -0700 | [diff] [blame] | 1634 | priv->adapter->is_suspended = true; |
| 1635 | |
| 1636 | /* We will keep the power when hs enabled successfully */ |
| 1637 | if (priv->adapter->hs_state == HS_ACTIVATED) { |
| 1638 | BT_DBG("suspend with MMC_PM_KEEP_POWER"); |
| 1639 | return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER); |
| 1640 | } else { |
| 1641 | BT_DBG("suspend without MMC_PM_KEEP_POWER"); |
| 1642 | return 0; |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | static int btmrvl_sdio_resume(struct device *dev) |
| 1647 | { |
| 1648 | struct sdio_func *func = dev_to_sdio_func(dev); |
| 1649 | struct btmrvl_sdio_card *card; |
| 1650 | struct btmrvl_private *priv; |
| 1651 | mmc_pm_flag_t pm_flags; |
| 1652 | struct hci_dev *hcidev; |
| 1653 | |
| 1654 | if (func) { |
| 1655 | pm_flags = sdio_get_host_pm_caps(func); |
| 1656 | BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func), |
| 1657 | pm_flags); |
| 1658 | card = sdio_get_drvdata(func); |
| 1659 | if (!card || !card->priv) { |
| 1660 | BT_ERR("card or priv structure is not valid"); |
| 1661 | return 0; |
| 1662 | } |
| 1663 | } else { |
| 1664 | BT_ERR("sdio_func is not specified"); |
| 1665 | return 0; |
| 1666 | } |
| 1667 | priv = card->priv; |
| 1668 | |
| 1669 | if (!priv->adapter->is_suspended) { |
| 1670 | BT_DBG("device already resumed"); |
| 1671 | return 0; |
| 1672 | } |
| 1673 | |
Amitkumar Karwar | ba54a16 | 2012-04-25 11:43:54 -0700 | [diff] [blame] | 1674 | priv->hw_wakeup_firmware(priv); |
| 1675 | priv->adapter->hs_state = HS_DEACTIVATED; |
Chin-Ran Lo | 5bf8a74 | 2014-07-14 21:05:37 -0700 | [diff] [blame] | 1676 | hcidev = priv->btmrvl_dev.hcidev; |
Amitkumar Karwar | ba54a16 | 2012-04-25 11:43:54 -0700 | [diff] [blame] | 1677 | BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name); |
Chin-Ran Lo | 5bf8a74 | 2014-07-14 21:05:37 -0700 | [diff] [blame] | 1678 | priv->adapter->is_suspended = false; |
| 1679 | BT_DBG("%s: SDIO resume", hcidev->name); |
| 1680 | hci_resume_dev(hcidev); |
Amitkumar Karwar | ba54a16 | 2012-04-25 11:43:54 -0700 | [diff] [blame] | 1681 | |
Xinming Hu | bb7f4f0 | 2016-04-26 06:57:27 -0700 | [diff] [blame] | 1682 | /* Disable platform specific wakeup interrupt */ |
| 1683 | if (card->plt_wake_cfg && card->plt_wake_cfg->irq_bt >= 0) { |
| 1684 | disable_irq_wake(card->plt_wake_cfg->irq_bt); |
| 1685 | if (!card->plt_wake_cfg->wake_by_bt) |
| 1686 | disable_irq(card->plt_wake_cfg->irq_bt); |
| 1687 | } |
| 1688 | |
Amitkumar Karwar | ba54a16 | 2012-04-25 11:43:54 -0700 | [diff] [blame] | 1689 | return 0; |
| 1690 | } |
| 1691 | |
| 1692 | static const struct dev_pm_ops btmrvl_sdio_pm_ops = { |
| 1693 | .suspend = btmrvl_sdio_suspend, |
| 1694 | .resume = btmrvl_sdio_resume, |
| 1695 | }; |
| 1696 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1697 | static struct sdio_driver bt_mrvl_sdio = { |
| 1698 | .name = "btmrvl_sdio", |
| 1699 | .id_table = btmrvl_sdio_ids, |
| 1700 | .probe = btmrvl_sdio_probe, |
| 1701 | .remove = btmrvl_sdio_remove, |
Amitkumar Karwar | ba54a16 | 2012-04-25 11:43:54 -0700 | [diff] [blame] | 1702 | .drv = { |
| 1703 | .owner = THIS_MODULE, |
| 1704 | .pm = &btmrvl_sdio_pm_ops, |
| 1705 | } |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1706 | }; |
| 1707 | |
Peter Huewe | 8e5b230 | 2009-12-22 09:34:20 +0100 | [diff] [blame] | 1708 | static int __init btmrvl_sdio_init_module(void) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1709 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1710 | if (sdio_register_driver(&bt_mrvl_sdio) != 0) { |
| 1711 | BT_ERR("SDIO Driver Registration Failed"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1712 | return -ENODEV; |
| 1713 | } |
| 1714 | |
| 1715 | /* Clear the flag in case user removes the card. */ |
| 1716 | user_rmmod = 0; |
| 1717 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1718 | return 0; |
| 1719 | } |
| 1720 | |
Peter Huewe | 8e5b230 | 2009-12-22 09:34:20 +0100 | [diff] [blame] | 1721 | static void __exit btmrvl_sdio_exit_module(void) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1722 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1723 | /* Set the flag as user is removing this module. */ |
| 1724 | user_rmmod = 1; |
| 1725 | |
| 1726 | sdio_unregister_driver(&bt_mrvl_sdio); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | module_init(btmrvl_sdio_init_module); |
| 1730 | module_exit(btmrvl_sdio_exit_module); |
| 1731 | |
| 1732 | MODULE_AUTHOR("Marvell International Ltd."); |
Marcel Holtmann | 9666fb3 | 2009-06-09 21:45:04 +0200 | [diff] [blame] | 1733 | MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1734 | MODULE_VERSION(VERSION); |
| 1735 | MODULE_LICENSE("GPL v2"); |
Lubomir Rintel | 77189df | 2013-03-13 12:04:30 +0100 | [diff] [blame] | 1736 | MODULE_FIRMWARE("mrvl/sd8688_helper.bin"); |
| 1737 | MODULE_FIRMWARE("mrvl/sd8688.bin"); |
Kevin Gan | 9f72c1d | 2011-04-08 18:19:33 -0700 | [diff] [blame] | 1738 | MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin"); |
Bing Zhao | 2ac654f | 2011-11-16 20:40:42 -0800 | [diff] [blame] | 1739 | MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin"); |
Xinming Hu | 3907d55 | 2014-09-30 06:45:33 -0400 | [diff] [blame] | 1740 | MODULE_FIRMWARE("mrvl/sd8887_uapsta.bin"); |
Bing Zhao | 22f2efe | 2013-05-13 18:15:32 -0700 | [diff] [blame] | 1741 | MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin"); |
Amitkumar Karwar | f0ef674 | 2015-09-21 03:06:42 -0700 | [diff] [blame] | 1742 | MODULE_FIRMWARE("mrvl/sd8997_uapsta.bin"); |