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