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