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