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