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