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