blob: 68378b42ea7fdde1bc705f5fc9238bbba9b6e227 [file] [log] [blame]
Bing Zhao789221e2009-06-02 14:29:36 -07001/**
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 Zhao433a9382013-11-01 15:28:24 -070021#include <linux/firmware.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Jeffy Chen17e41ea2017-02-24 14:24:30 +080023#include <linux/suspend.h>
Bing Zhao789221e2009-06-02 14:29:36 -070024
25#include <linux/mmc/sdio_ids.h>
26#include <linux/mmc/sdio_func.h>
Paul Gortmakera59b9682011-08-29 16:44:23 -040027#include <linux/module.h>
Xinming Hudc759612014-11-24 02:40:53 -080028#include <linux/devcoredump.h>
Bing Zhao789221e2009-06-02 14:29:36 -070029
30#include <net/bluetooth/bluetooth.h>
31#include <net/bluetooth/hci_core.h>
32
33#include "btmrvl_drv.h"
34#include "btmrvl_sdio.h"
35
36#define VERSION "1.0"
37
Xinming Hudc759612014-11-24 02:40:53 -080038static struct memory_type_mapping mem_type_mapping_tbl[] = {
39 {"ITCM", NULL, 0, 0xF0},
40 {"DTCM", NULL, 0, 0xF1},
41 {"SQRAM", NULL, 0, 0xF2},
42 {"APU", NULL, 0, 0xF3},
43 {"CIU", NULL, 0, 0xF4},
44 {"ICU", NULL, 0, 0xF5},
45 {"MAC", NULL, 0, 0xF6},
46 {"EXT7", NULL, 0, 0xF7},
47 {"EXT8", NULL, 0, 0xF8},
48 {"EXT9", NULL, 0, 0xF9},
49 {"EXT10", NULL, 0, 0xFA},
50 {"EXT11", NULL, 0, 0xFB},
51 {"EXT12", NULL, 0, 0xFC},
52 {"EXT13", NULL, 0, 0xFD},
53 {"EXTLAST", NULL, 0, 0xFE},
54};
55
Xinming Hubb7f4f02016-04-26 06:57:27 -070056static const struct of_device_id btmrvl_sdio_of_match_table[] = {
57 { .compatible = "marvell,sd8897-bt" },
58 { .compatible = "marvell,sd8997-bt" },
59 { }
60};
61
62static irqreturn_t btmrvl_wake_irq_bt(int irq, void *priv)
63{
Jeffy Chen17e41ea2017-02-24 14:24:30 +080064 struct btmrvl_sdio_card *card = priv;
Stefan Agner10004f82019-01-11 21:56:56 +010065 struct device *dev = &card->func->dev;
Jeffy Chen17e41ea2017-02-24 14:24:30 +080066 struct btmrvl_plt_wake_cfg *cfg = card->plt_wake_cfg;
Xinming Hubb7f4f02016-04-26 06:57:27 -070067
Stefan Agner10004f82019-01-11 21:56:56 +010068 dev_info(dev, "wake by bt\n");
Xinming Huc8ba8042017-03-31 14:32:32 +080069 cfg->wake_by_bt = true;
70 disable_irq_nosync(irq);
Xinming Hubb7f4f02016-04-26 06:57:27 -070071
Stefan Agner10004f82019-01-11 21:56:56 +010072 pm_wakeup_event(dev, 0);
Jeffy Chen17e41ea2017-02-24 14:24:30 +080073 pm_system_wakeup();
74
Xinming Hubb7f4f02016-04-26 06:57:27 -070075 return IRQ_HANDLED;
76}
77
78/* This function parses device tree node using mmc subnode devicetree API.
79 * The device node is saved in card->plt_of_node.
80 * If the device tree node exists and includes interrupts attributes, this
81 * function will request platform specific wakeup interrupt.
82 */
83static int btmrvl_sdio_probe_of(struct device *dev,
84 struct btmrvl_sdio_card *card)
85{
86 struct btmrvl_plt_wake_cfg *cfg;
87 int ret;
88
89 if (!dev->of_node ||
90 !of_match_node(btmrvl_sdio_of_match_table, dev->of_node)) {
Stefan Agner37c589e2019-01-11 21:56:57 +010091 dev_info(dev, "sdio device tree data not available\n");
Xinming Hubb7f4f02016-04-26 06:57:27 -070092 return -1;
93 }
94
95 card->plt_of_node = dev->of_node;
96
97 card->plt_wake_cfg = devm_kzalloc(dev, sizeof(*card->plt_wake_cfg),
98 GFP_KERNEL);
99 cfg = card->plt_wake_cfg;
100 if (cfg && card->plt_of_node) {
101 cfg->irq_bt = irq_of_parse_and_map(card->plt_of_node, 0);
102 if (!cfg->irq_bt) {
Arvind Yadav24a3a322017-09-25 13:07:39 +0530103 dev_err(dev, "fail to parse irq_bt from device tree\n");
Jeffy Chenfd1a88d2017-01-23 12:18:52 +0800104 cfg->irq_bt = -1;
Xinming Hubb7f4f02016-04-26 06:57:27 -0700105 } else {
106 ret = devm_request_irq(dev, cfg->irq_bt,
107 btmrvl_wake_irq_bt,
Jeffy Chen17e41ea2017-02-24 14:24:30 +0800108 0, "bt_wake", card);
Xinming Hubb7f4f02016-04-26 06:57:27 -0700109 if (ret) {
110 dev_err(dev,
111 "Failed to request irq_bt %d (%d)\n",
112 cfg->irq_bt, ret);
113 }
Abhishek Pandit-Subedie660b352020-06-10 18:53:55 -0700114
115 /* Configure wakeup (enabled by default) */
116 device_init_wakeup(dev, true);
Xinming Hubb7f4f02016-04-26 06:57:27 -0700117 disable_irq(cfg->irq_bt);
118 }
119 }
120
121 return 0;
122}
123
Bing Zhao789221e2009-06-02 14:29:36 -0700124/* The btmrvl_sdio_remove() callback function is called
125 * when user removes this module from kernel space or ejects
126 * the card from the slot. The driver handles these 2 cases
127 * differently.
128 * If the user is removing the module, a MODULE_SHUTDOWN_REQ
129 * command is sent to firmware and interrupt will be disabled.
130 * If the card is removed, there is no need to send command
131 * or disable interrupt.
132 *
133 * The variable 'user_rmmod' is used to distinguish these two
134 * scenarios. This flag is initialized as FALSE in case the card
135 * is removed, and will be set to TRUE for module removal when
136 * module_exit function is called.
137 */
138static u8 user_rmmod;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700139static u8 sdio_ireg;
Bing Zhao789221e2009-06-02 14:29:36 -0700140
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700141static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
142 .cfg = 0x03,
143 .host_int_mask = 0x04,
144 .host_intstatus = 0x05,
145 .card_status = 0x20,
146 .sq_read_base_addr_a0 = 0x10,
147 .sq_read_base_addr_a1 = 0x11,
148 .card_fw_status0 = 0x40,
149 .card_fw_status1 = 0x41,
150 .card_rx_len = 0x42,
151 .card_rx_unit = 0x43,
152 .io_port_0 = 0x00,
153 .io_port_1 = 0x01,
154 .io_port_2 = 0x02,
Bing Zhao0d367402014-03-28 14:54:39 -0700155 .int_read_to_clear = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700156};
Bing Zhao2ac654f2011-11-16 20:40:42 -0800157static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700158 .cfg = 0x00,
159 .host_int_mask = 0x02,
160 .host_intstatus = 0x03,
161 .card_status = 0x30,
162 .sq_read_base_addr_a0 = 0x40,
163 .sq_read_base_addr_a1 = 0x41,
164 .card_revision = 0x5c,
165 .card_fw_status0 = 0x60,
166 .card_fw_status1 = 0x61,
167 .card_rx_len = 0x62,
168 .card_rx_unit = 0x63,
169 .io_port_0 = 0x78,
170 .io_port_1 = 0x79,
171 .io_port_2 = 0x7a,
Bing Zhao0d367402014-03-28 14:54:39 -0700172 .int_read_to_clear = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700173};
174
Xinming Hu3907d552014-09-30 06:45:33 -0400175static const struct btmrvl_sdio_card_reg btmrvl_reg_8887 = {
176 .cfg = 0x00,
177 .host_int_mask = 0x08,
178 .host_intstatus = 0x0C,
179 .card_status = 0x5C,
180 .sq_read_base_addr_a0 = 0x6C,
181 .sq_read_base_addr_a1 = 0x6D,
182 .card_revision = 0xC8,
183 .card_fw_status0 = 0x88,
184 .card_fw_status1 = 0x89,
185 .card_rx_len = 0x8A,
186 .card_rx_unit = 0x8B,
187 .io_port_0 = 0xE4,
188 .io_port_1 = 0xE5,
189 .io_port_2 = 0xE6,
190 .int_read_to_clear = true,
191 .host_int_rsr = 0x04,
192 .card_misc_cfg = 0xD8,
193};
194
Amitkumar Karwar1e3e4922014-09-30 06:45:32 -0400195static const struct btmrvl_sdio_card_reg btmrvl_reg_8897 = {
Bing Zhao22f2efe2013-05-13 18:15:32 -0700196 .cfg = 0x00,
197 .host_int_mask = 0x02,
198 .host_intstatus = 0x03,
199 .card_status = 0x50,
200 .sq_read_base_addr_a0 = 0x60,
201 .sq_read_base_addr_a1 = 0x61,
202 .card_revision = 0xbc,
203 .card_fw_status0 = 0xc0,
204 .card_fw_status1 = 0xc1,
205 .card_rx_len = 0xc2,
206 .card_rx_unit = 0xc3,
207 .io_port_0 = 0xd8,
208 .io_port_1 = 0xd9,
209 .io_port_2 = 0xda,
Bing Zhao0d367402014-03-28 14:54:39 -0700210 .int_read_to_clear = true,
211 .host_int_rsr = 0x01,
212 .card_misc_cfg = 0xcc,
Xinming Hudc759612014-11-24 02:40:53 -0800213 .fw_dump_ctrl = 0xe2,
214 .fw_dump_start = 0xe3,
215 .fw_dump_end = 0xea,
Bing Zhao22f2efe2013-05-13 18:15:32 -0700216};
217
Tamás Szűcs7cfc21e2020-08-02 16:56:42 +0200218static const struct btmrvl_sdio_card_reg btmrvl_reg_89xx = {
Tamás Szűcs736233402019-04-14 20:38:14 +0200219 .cfg = 0x00,
220 .host_int_mask = 0x08,
221 .host_intstatus = 0x0c,
222 .card_status = 0x5c,
223 .sq_read_base_addr_a0 = 0xf8,
224 .sq_read_base_addr_a1 = 0xf9,
225 .card_revision = 0xc8,
226 .card_fw_status0 = 0xe8,
227 .card_fw_status1 = 0xe9,
228 .card_rx_len = 0xea,
229 .card_rx_unit = 0xeb,
230 .io_port_0 = 0xe4,
231 .io_port_1 = 0xe5,
232 .io_port_2 = 0xe6,
233 .int_read_to_clear = true,
234 .host_int_rsr = 0x04,
235 .card_misc_cfg = 0xd8,
236 .fw_dump_ctrl = 0xf0,
237 .fw_dump_start = 0xf1,
238 .fw_dump_end = 0xf8,
239};
240
Paul Bollec6757862011-10-05 14:56:46 +0200241static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
Lubomir Rintel77189df2013-03-13 12:04:30 +0100242 .helper = "mrvl/sd8688_helper.bin",
243 .firmware = "mrvl/sd8688.bin",
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700244 .reg = &btmrvl_reg_8688,
Bing Zhao4df82b52014-03-31 14:41:44 -0700245 .support_pscan_win_report = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700246 .sd_blksz_fw_dl = 64,
Xinming Hudc759612014-11-24 02:40:53 -0800247 .supports_fw_dump = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700248};
249
250static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
251 .helper = NULL,
252 .firmware = "mrvl/sd8787_uapsta.bin",
Bing Zhao2ac654f2011-11-16 20:40:42 -0800253 .reg = &btmrvl_reg_87xx,
Bing Zhao4df82b52014-03-31 14:41:44 -0700254 .support_pscan_win_report = false,
Bing Zhao2ac654f2011-11-16 20:40:42 -0800255 .sd_blksz_fw_dl = 256,
Xinming Hudc759612014-11-24 02:40:53 -0800256 .supports_fw_dump = false,
Bing Zhao2ac654f2011-11-16 20:40:42 -0800257};
258
259static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
260 .helper = NULL,
261 .firmware = "mrvl/sd8797_uapsta.bin",
262 .reg = &btmrvl_reg_87xx,
Bing Zhao4df82b52014-03-31 14:41:44 -0700263 .support_pscan_win_report = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700264 .sd_blksz_fw_dl = 256,
Xinming Hudc759612014-11-24 02:40:53 -0800265 .supports_fw_dump = false,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200266};
267
Xinming Hu3907d552014-09-30 06:45:33 -0400268static const struct btmrvl_sdio_device btmrvl_sdio_sd8887 = {
269 .helper = NULL,
270 .firmware = "mrvl/sd8887_uapsta.bin",
271 .reg = &btmrvl_reg_8887,
272 .support_pscan_win_report = true,
273 .sd_blksz_fw_dl = 256,
Xinming Hudc759612014-11-24 02:40:53 -0800274 .supports_fw_dump = false,
Xinming Hu3907d552014-09-30 06:45:33 -0400275};
276
Bing Zhao22f2efe2013-05-13 18:15:32 -0700277static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = {
278 .helper = NULL,
279 .firmware = "mrvl/sd8897_uapsta.bin",
Amitkumar Karwar1e3e4922014-09-30 06:45:32 -0400280 .reg = &btmrvl_reg_8897,
Bing Zhao4df82b52014-03-31 14:41:44 -0700281 .support_pscan_win_report = true,
Bing Zhao22f2efe2013-05-13 18:15:32 -0700282 .sd_blksz_fw_dl = 256,
Xinming Hudc759612014-11-24 02:40:53 -0800283 .supports_fw_dump = true,
Bing Zhao22f2efe2013-05-13 18:15:32 -0700284};
285
Hemantkumar Suthar8c579832019-01-09 15:03:57 +0000286static const struct btmrvl_sdio_device btmrvl_sdio_sd8977 = {
287 .helper = NULL,
Pali Rohárdbec3af2020-06-03 10:22:28 +0200288 .firmware = "mrvl/sdsd8977_combo_v2.bin",
Tamás Szűcs7cfc21e2020-08-02 16:56:42 +0200289 .reg = &btmrvl_reg_89xx,
Hemantkumar Suthar8c579832019-01-09 15:03:57 +0000290 .support_pscan_win_report = true,
291 .sd_blksz_fw_dl = 256,
292 .supports_fw_dump = true,
293};
294
Tamás Szűcs736233402019-04-14 20:38:14 +0200295static const struct btmrvl_sdio_device btmrvl_sdio_sd8987 = {
296 .helper = NULL,
297 .firmware = "mrvl/sd8987_uapsta.bin",
Tamás Szűcs7cfc21e2020-08-02 16:56:42 +0200298 .reg = &btmrvl_reg_89xx,
Tamás Szűcs736233402019-04-14 20:38:14 +0200299 .support_pscan_win_report = true,
300 .sd_blksz_fw_dl = 256,
301 .supports_fw_dump = true,
302};
303
Amitkumar Karwarf0ef6742015-09-21 03:06:42 -0700304static const struct btmrvl_sdio_device btmrvl_sdio_sd8997 = {
305 .helper = NULL,
Pali Rohár00eb0cb2020-06-03 10:22:29 +0200306 .firmware = "mrvl/sdsd8997_combo_v4.bin",
Tamás Szűcs7cfc21e2020-08-02 16:56:42 +0200307 .reg = &btmrvl_reg_89xx,
Amitkumar Karwarf0ef6742015-09-21 03:06:42 -0700308 .support_pscan_win_report = true,
309 .sd_blksz_fw_dl = 256,
310 .supports_fw_dump = true,
311};
312
Bing Zhao789221e2009-06-02 14:29:36 -0700313static const struct sdio_device_id btmrvl_sdio_ids[] = {
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200314 /* Marvell SD8688 Bluetooth device */
Pali Rohár649c7d72020-05-22 16:44:05 +0200315 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8688_BT),
Amitkumar Karwar53d6cfa2015-09-21 03:06:41 -0700316 .driver_data = (unsigned long)&btmrvl_sdio_sd8688 },
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700317 /* Marvell SD8787 Bluetooth device */
Pali Rohár649c7d72020-05-22 16:44:05 +0200318 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787_BT),
Amitkumar Karwar53d6cfa2015-09-21 03:06:41 -0700319 .driver_data = (unsigned long)&btmrvl_sdio_sd8787 },
Bing Zhao37ed5612012-05-23 18:50:04 -0700320 /* Marvell SD8787 Bluetooth AMP device */
Pali Rohár649c7d72020-05-22 16:44:05 +0200321 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787_BT_AMP),
Amitkumar Karwar53d6cfa2015-09-21 03:06:41 -0700322 .driver_data = (unsigned long)&btmrvl_sdio_sd8787 },
Bing Zhao2ac654f2011-11-16 20:40:42 -0800323 /* Marvell SD8797 Bluetooth device */
Pali Rohár649c7d72020-05-22 16:44:05 +0200324 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797_BT),
Amitkumar Karwar53d6cfa2015-09-21 03:06:41 -0700325 .driver_data = (unsigned long)&btmrvl_sdio_sd8797 },
Xinming Hu3907d552014-09-30 06:45:33 -0400326 /* Marvell SD8887 Bluetooth device */
Pali Rohár649c7d72020-05-22 16:44:05 +0200327 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887_BT),
Xinming Hu3907d552014-09-30 06:45:33 -0400328 .driver_data = (unsigned long)&btmrvl_sdio_sd8887 },
Bing Zhao22f2efe2013-05-13 18:15:32 -0700329 /* Marvell SD8897 Bluetooth device */
Pali Rohár649c7d72020-05-22 16:44:05 +0200330 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897_BT),
Amitkumar Karwar53d6cfa2015-09-21 03:06:41 -0700331 .driver_data = (unsigned long)&btmrvl_sdio_sd8897 },
Hemantkumar Suthar8c579832019-01-09 15:03:57 +0000332 /* Marvell SD8977 Bluetooth device */
Pali Rohár649c7d72020-05-22 16:44:05 +0200333 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8977_BT),
Hemantkumar Suthar8c579832019-01-09 15:03:57 +0000334 .driver_data = (unsigned long)&btmrvl_sdio_sd8977 },
Tamás Szűcs736233402019-04-14 20:38:14 +0200335 /* Marvell SD8987 Bluetooth device */
Pali Rohár649c7d72020-05-22 16:44:05 +0200336 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8987_BT),
Tamás Szűcs736233402019-04-14 20:38:14 +0200337 .driver_data = (unsigned long)&btmrvl_sdio_sd8987 },
Amitkumar Karwarf0ef6742015-09-21 03:06:42 -0700338 /* Marvell SD8997 Bluetooth device */
Pali Rohár649c7d72020-05-22 16:44:05 +0200339 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997_BT),
Amitkumar Karwarf0ef6742015-09-21 03:06:42 -0700340 .driver_data = (unsigned long)&btmrvl_sdio_sd8997 },
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200341
342 { } /* Terminating entry */
Bing Zhao789221e2009-06-02 14:29:36 -0700343};
344
345MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
346
Bing Zhao789221e2009-06-02 14:29:36 -0700347static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
348{
349 u8 reg;
350 int ret;
351
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700352 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700353 if (!ret)
354 card->rx_unit = reg;
355
Bing Zhao789221e2009-06-02 14:29:36 -0700356 return ret;
357}
358
359static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
360{
Bing Zhao789221e2009-06-02 14:29:36 -0700361 u8 fws0, fws1;
Marcel Holtmann93742532009-06-13 07:40:18 +0200362 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700363
364 *dat = 0;
365
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700366 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Kulikov Vasiliy278be272010-07-05 12:01:22 +0400367 if (ret)
368 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700369
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700370 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200371 if (ret)
Bing Zhao789221e2009-06-02 14:29:36 -0700372 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700373
374 *dat = (((u16) fws1) << 8) | fws0;
375
Bing Zhao789221e2009-06-02 14:29:36 -0700376 return 0;
377}
378
379static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
380{
Bing Zhao789221e2009-06-02 14:29:36 -0700381 u8 reg;
Marcel Holtmann93742532009-06-13 07:40:18 +0200382 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700383
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700384 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700385 if (!ret)
386 *dat = (u16) reg << card->rx_unit;
387
Bing Zhao789221e2009-06-02 14:29:36 -0700388 return ret;
389}
390
391static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200392 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700393{
394 int ret;
395
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700396 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700397 if (ret) {
398 BT_ERR("Unable to enable the host interrupt!");
399 ret = -EIO;
400 }
401
Bing Zhao789221e2009-06-02 14:29:36 -0700402 return ret;
403}
404
405static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200406 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700407{
Bing Zhao789221e2009-06-02 14:29:36 -0700408 u8 host_int_mask;
Marcel Holtmann93742532009-06-13 07:40:18 +0200409 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700410
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700411 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200412 if (ret)
413 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700414
415 host_int_mask &= ~mask;
416
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700417 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700418 if (ret < 0) {
419 BT_ERR("Unable to disable the host interrupt!");
Marcel Holtmann93742532009-06-13 07:40:18 +0200420 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700421 }
422
Marcel Holtmann93742532009-06-13 07:40:18 +0200423 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700424}
425
426static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
427{
428 unsigned int tries;
Bing Zhao789221e2009-06-02 14:29:36 -0700429 u8 status;
Marcel Holtmann93742532009-06-13 07:40:18 +0200430 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700431
432 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700433 status = sdio_readb(card->func, card->reg->card_status, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700434 if (ret)
435 goto failed;
436 if ((status & bits) == bits)
Marcel Holtmann93742532009-06-13 07:40:18 +0200437 return ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700438
439 udelay(1);
440 }
441
442 ret = -ETIMEDOUT;
443
444failed:
445 BT_ERR("FAILED! ret=%d", ret);
446
Bing Zhao789221e2009-06-02 14:29:36 -0700447 return ret;
448}
449
450static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200451 int pollnum)
Bing Zhao789221e2009-06-02 14:29:36 -0700452{
Bing Zhao789221e2009-06-02 14:29:36 -0700453 u16 firmwarestat;
Andreas Fenkart69676b12013-04-22 11:10:23 +0200454 int tries, ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700455
Bing Zhao789221e2009-06-02 14:29:36 -0700456 /* Wait for firmware to become ready */
457 for (tries = 0; tries < pollnum; tries++) {
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200458 sdio_claim_host(card->func);
459 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat);
460 sdio_release_host(card->func);
461 if (ret < 0)
Bing Zhao789221e2009-06-02 14:29:36 -0700462 continue;
463
Andreas Fenkart69676b12013-04-22 11:10:23 +0200464 if (firmwarestat == FIRMWARE_READY)
465 return 0;
466
Wei-Ning Huang9a012422016-03-08 11:40:06 +0800467 msleep(100);
Bing Zhao789221e2009-06-02 14:29:36 -0700468 }
469
Andreas Fenkart69676b12013-04-22 11:10:23 +0200470 return -ETIMEDOUT;
Bing Zhao789221e2009-06-02 14:29:36 -0700471}
472
473static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
474{
475 const struct firmware *fw_helper = NULL;
476 const u8 *helper = NULL;
477 int ret;
478 void *tmphlprbuf = NULL;
479 int tmphlprbufsz, hlprblknow, helperlen;
480 u8 *helperbuf;
481 u32 tx_len;
482
Bing Zhao789221e2009-06-02 14:29:36 -0700483 ret = request_firmware(&fw_helper, card->helper,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200484 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700485 if ((ret < 0) || !fw_helper) {
486 BT_ERR("request_firmware(helper) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200487 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700488 ret = -ENOENT;
489 goto done;
490 }
491
492 helper = fw_helper->data;
493 helperlen = fw_helper->size;
494
495 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200496 helperlen, SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700497
498 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
499
Julia Lawallf8df39f2010-05-13 22:02:03 +0200500 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700501 if (!tmphlprbuf) {
502 BT_ERR("Unable to allocate buffer for helper."
503 " Terminating download");
504 ret = -ENOMEM;
505 goto done;
506 }
507
Bing Zhao789221e2009-06-02 14:29:36 -0700508 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
509
510 /* Perform helper data transfer */
511 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
512 - SDIO_HEADER_LEN;
513 hlprblknow = 0;
514
515 do {
516 ret = btmrvl_sdio_poll_card_status(card,
517 CARD_IO_READY | DN_LD_CARD_RDY);
518 if (ret < 0) {
519 BT_ERR("Helper download poll status timeout @ %d",
520 hlprblknow);
521 goto done;
522 }
523
524 /* Check if there is more data? */
525 if (hlprblknow >= helperlen)
526 break;
527
528 if (helperlen - hlprblknow < tx_len)
529 tx_len = helperlen - hlprblknow;
530
531 /* Little-endian */
532 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
533 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
534 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
535 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
536
537 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
538 tx_len);
539
540 /* Now send the data */
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200541 ret = sdio_writesb(card->func, card->ioport, helperbuf,
542 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700543 if (ret < 0) {
544 BT_ERR("IO error during helper download @ %d",
545 hlprblknow);
546 goto done;
547 }
548
549 hlprblknow += tx_len;
550 } while (true);
551
552 BT_DBG("Transferring helper image EOF block");
553
554 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
555
556 ret = sdio_writesb(card->func, card->ioport, helperbuf,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200557 SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700558 if (ret < 0) {
559 BT_ERR("IO error in writing helper image EOF block");
560 goto done;
561 }
562
563 ret = 0;
564
565done:
566 kfree(tmphlprbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200567 release_firmware(fw_helper);
Bing Zhao789221e2009-06-02 14:29:36 -0700568 return ret;
569}
570
571static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
572{
573 const struct firmware *fw_firmware = NULL;
574 const u8 *firmware = NULL;
575 int firmwarelen, tmpfwbufsz, ret;
576 unsigned int tries, offset;
577 u8 base0, base1;
578 void *tmpfwbuf = NULL;
579 u8 *fwbuf;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700580 u16 len, blksz_dl = card->sd_blksz_fw_dl;
Bing Zhao789221e2009-06-02 14:29:36 -0700581 int txlen = 0, tx_blocks = 0, count = 0;
582
Bing Zhao789221e2009-06-02 14:29:36 -0700583 ret = request_firmware(&fw_firmware, card->firmware,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200584 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700585 if ((ret < 0) || !fw_firmware) {
586 BT_ERR("request_firmware(firmware) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200587 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700588 ret = -ENOENT;
589 goto done;
590 }
591
592 firmware = fw_firmware->data;
593 firmwarelen = fw_firmware->size;
594
595 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
596
597 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
Julia Lawallf8df39f2010-05-13 22:02:03 +0200598 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700599 if (!tmpfwbuf) {
600 BT_ERR("Unable to allocate buffer for firmware."
601 " Terminating download");
602 ret = -ENOMEM;
603 goto done;
604 }
605
Bing Zhao789221e2009-06-02 14:29:36 -0700606 /* Ensure aligned firmware buffer */
607 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
608
609 /* Perform firmware data transfer */
610 offset = 0;
611 do {
612 ret = btmrvl_sdio_poll_card_status(card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200613 CARD_IO_READY | DN_LD_CARD_RDY);
Bing Zhao789221e2009-06-02 14:29:36 -0700614 if (ret < 0) {
615 BT_ERR("FW download with helper poll status"
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200616 " timeout @ %d", offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700617 goto done;
618 }
619
620 /* Check if there is more data ? */
621 if (offset >= firmwarelen)
622 break;
623
624 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
625 base0 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700626 card->reg->sq_read_base_addr_a0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700627 if (ret) {
628 BT_ERR("BASE0 register read failed:"
629 " base0 = 0x%04X(%d)."
630 " Terminating download",
631 base0, base0);
632 ret = -EIO;
633 goto done;
634 }
635 base1 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700636 card->reg->sq_read_base_addr_a1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700637 if (ret) {
638 BT_ERR("BASE1 register read failed:"
639 " base1 = 0x%04X(%d)."
640 " Terminating download",
641 base1, base1);
642 ret = -EIO;
643 goto done;
644 }
645
646 len = (((u16) base1) << 8) | base0;
647 if (len)
648 break;
649
650 udelay(10);
651 }
652
653 if (!len)
654 break;
655 else if (len > BTM_UPLD_SIZE) {
656 BT_ERR("FW download failure @%d, invalid length %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200657 offset, len);
Bing Zhao789221e2009-06-02 14:29:36 -0700658 ret = -EINVAL;
659 goto done;
660 }
661
662 txlen = len;
663
664 if (len & BIT(0)) {
665 count++;
666 if (count > MAX_WRITE_IOMEM_RETRY) {
667 BT_ERR("FW download failure @%d, "
668 "over max retry count", offset);
669 ret = -EIO;
670 goto done;
671 }
672 BT_ERR("FW CRC error indicated by the helper: "
673 "len = 0x%04X, txlen = %d", len, txlen);
674 len &= ~BIT(0);
675 /* Set txlen to 0 so as to resend from same offset */
676 txlen = 0;
677 } else {
678 count = 0;
679
680 /* Last block ? */
681 if (firmwarelen - offset < txlen)
682 txlen = firmwarelen - offset;
683
Andy Shevchenkoba34dd32013-08-02 14:10:12 +0300684 tx_blocks = DIV_ROUND_UP(txlen, blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700685
686 memcpy(fwbuf, &firmware[offset], txlen);
687 }
688
689 ret = sdio_writesb(card->func, card->ioport, fwbuf,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700690 tx_blocks * blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700691
692 if (ret < 0) {
693 BT_ERR("FW download, writesb(%d) failed @%d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200694 count, offset);
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700695 sdio_writeb(card->func, HOST_CMD53_FIN,
696 card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700697 if (ret)
698 BT_ERR("writeb failed (CFG)");
699 }
700
701 offset += txlen;
702 } while (true);
703
Amitkumar Karwar1c6098e2015-01-01 00:13:42 -0800704 BT_INFO("FW download over, size %d bytes", offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700705
706 ret = 0;
707
708done:
709 kfree(tmpfwbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200710 release_firmware(fw_firmware);
Bing Zhao789221e2009-06-02 14:29:36 -0700711 return ret;
712}
713
714static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
715{
716 u16 buf_len = 0;
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300717 int ret, num_blocks, blksz;
Bing Zhao789221e2009-06-02 14:29:36 -0700718 struct sk_buff *skb = NULL;
719 u32 type;
Markus Elfring9376e4a2018-03-12 11:15:59 +0100720 u8 *payload;
Bing Zhao789221e2009-06-02 14:29:36 -0700721 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
722 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
723
Bing Zhao789221e2009-06-02 14:29:36 -0700724 if (!card || !card->func) {
725 BT_ERR("card or function is NULL!");
726 ret = -EINVAL;
727 goto exit;
728 }
729
730 /* Read the length of data to be transferred */
731 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
732 if (ret < 0) {
733 BT_ERR("read rx_len failed");
734 ret = -EIO;
735 goto exit;
736 }
737
738 blksz = SDIO_BLOCK_SIZE;
Andrei Emeltchenkoe678bad2012-09-28 14:36:09 +0300739 num_blocks = DIV_ROUND_UP(buf_len, blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700740
741 if (buf_len <= SDIO_HEADER_LEN
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300742 || (num_blocks * blksz) > ALLOC_BUF_SIZE) {
Bing Zhao789221e2009-06-02 14:29:36 -0700743 BT_ERR("invalid packet length: %d", buf_len);
744 ret = -EINVAL;
745 goto exit;
746 }
747
748 /* Allocate buffer */
Jia-Ju Bai436018e2018-07-23 11:30:03 +0800749 skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_KERNEL);
Prasanna Karthik1f438c62015-09-25 09:03:14 +0000750 if (!skb) {
Bing Zhao789221e2009-06-02 14:29:36 -0700751 BT_ERR("No free skb");
Wei Yongjun6ea81c42013-06-05 10:16:55 +0800752 ret = -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700753 goto exit;
754 }
755
Bing Zhao3318b232009-07-08 11:44:14 -0700756 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
757 skb_put(skb, (unsigned long) skb->data &
758 (BTSDIO_DMA_ALIGN - 1));
759 skb_pull(skb, (unsigned long) skb->data &
760 (BTSDIO_DMA_ALIGN - 1));
Bing Zhao789221e2009-06-02 14:29:36 -0700761 }
762
Bing Zhao3318b232009-07-08 11:44:14 -0700763 payload = skb->data;
Bing Zhao789221e2009-06-02 14:29:36 -0700764
765 ret = sdio_readsb(card->func, payload, card->ioport,
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300766 num_blocks * blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700767 if (ret < 0) {
768 BT_ERR("readsb failed: %d", ret);
769 ret = -EIO;
770 goto exit;
771 }
772
773 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
774 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
775 */
776
777 buf_len = payload[0];
Andrei Emeltchenko9cb23dd2012-09-28 14:36:10 +0300778 buf_len |= payload[1] << 8;
779 buf_len |= payload[2] << 16;
780
781 if (buf_len > blksz * num_blocks) {
782 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
783 buf_len, blksz * num_blocks);
784 ret = -EIO;
785 goto exit;
786 }
787
Bing Zhao789221e2009-06-02 14:29:36 -0700788 type = payload[3];
789
790 switch (type) {
791 case HCI_ACLDATA_PKT:
792 case HCI_SCODATA_PKT:
793 case HCI_EVENT_PKT:
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100794 hci_skb_pkt_type(skb) = type;
Bing Zhao789221e2009-06-02 14:29:36 -0700795 skb_put(skb, buf_len);
796 skb_pull(skb, SDIO_HEADER_LEN);
797
Andrei Emeltchenkoea1e76a2012-06-13 13:35:44 +0300798 if (type == HCI_EVENT_PKT) {
799 if (btmrvl_check_evtpkt(priv, skb))
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700800 hci_recv_frame(hdev, skb);
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +0300801 } else {
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700802 hci_recv_frame(hdev, skb);
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +0300803 }
Bing Zhao789221e2009-06-02 14:29:36 -0700804
Bing Zhao789221e2009-06-02 14:29:36 -0700805 hdev->stat.byte_rx += buf_len;
806 break;
807
808 case MRVL_VENDOR_PKT:
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100809 hci_skb_pkt_type(skb) = HCI_VENDOR_PKT;
Bing Zhao789221e2009-06-02 14:29:36 -0700810 skb_put(skb, buf_len);
811 skb_pull(skb, SDIO_HEADER_LEN);
812
813 if (btmrvl_process_event(priv, skb))
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700814 hci_recv_frame(hdev, skb);
Bing Zhao789221e2009-06-02 14:29:36 -0700815
816 hdev->stat.byte_rx += buf_len;
817 break;
818
819 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200820 BT_ERR("Unknown packet type:%d", type);
Andrei Emeltchenko78c1b8e2012-10-10 17:41:33 +0300821 BT_ERR("hex: %*ph", blksz * num_blocks, payload);
Bing Zhao789221e2009-06-02 14:29:36 -0700822
823 kfree_skb(skb);
824 skb = NULL;
825 break;
826 }
827
828exit:
829 if (ret) {
830 hdev->stat.err_rx++;
Wei Yongjun79232962012-08-28 21:12:48 +0800831 kfree_skb(skb);
Bing Zhao789221e2009-06-02 14:29:36 -0700832 }
833
Bing Zhao789221e2009-06-02 14:29:36 -0700834 return ret;
835}
836
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700837static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
Bing Zhao789221e2009-06-02 14:29:36 -0700838{
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700839 ulong flags;
840 u8 ireg;
Bing Zhao789221e2009-06-02 14:29:36 -0700841 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
842
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700843 spin_lock_irqsave(&priv->driver_lock, flags);
844 ireg = sdio_ireg;
845 sdio_ireg = 0;
846 spin_unlock_irqrestore(&priv->driver_lock, flags);
Bing Zhao789221e2009-06-02 14:29:36 -0700847
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700848 sdio_claim_host(card->func);
849 if (ireg & DN_LD_HOST_INT_STATUS) {
Bing Zhao789221e2009-06-02 14:29:36 -0700850 if (priv->btmrvl_dev.tx_dnld_rdy)
851 BT_DBG("tx_done already received: "
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700852 " int_status=0x%x", ireg);
Bing Zhao789221e2009-06-02 14:29:36 -0700853 else
854 priv->btmrvl_dev.tx_dnld_rdy = true;
855 }
856
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700857 if (ireg & UP_LD_HOST_INT_STATUS)
Bing Zhao789221e2009-06-02 14:29:36 -0700858 btmrvl_sdio_card_to_host(priv);
859
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700860 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -0700861
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700862 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700863}
864
Bing Zhao0d367402014-03-28 14:54:39 -0700865static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
866{
867 struct btmrvl_adapter *adapter = card->priv->adapter;
868 int ret;
869
870 ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE);
871 if (ret) {
872 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret);
873 return ret;
874 }
875
876 *ireg = adapter->hw_regs[card->reg->host_intstatus];
877 BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg);
878
879 return 0;
880}
881
Bing Zhao8af48402014-03-27 21:30:42 -0700882static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
883{
884 int ret;
885
886 *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
887 if (ret) {
888 BT_ERR("sdio_readb: read int status failed: %d", ret);
889 return ret;
890 }
891
892 if (*ireg) {
893 /*
894 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
895 * Clear the interrupt status register and re-enable the
896 * interrupt.
897 */
898 BT_DBG("int_status = 0x%x", *ireg);
899
900 sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS |
901 UP_LD_HOST_INT_STATUS),
902 card->reg->host_intstatus, &ret);
903 if (ret) {
904 BT_ERR("sdio_writeb: clear int status failed: %d", ret);
905 return ret;
906 }
907 }
908
909 return 0;
910}
911
Bing Zhao789221e2009-06-02 14:29:36 -0700912static void btmrvl_sdio_interrupt(struct sdio_func *func)
913{
914 struct btmrvl_private *priv;
Bing Zhao789221e2009-06-02 14:29:36 -0700915 struct btmrvl_sdio_card *card;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700916 ulong flags;
Bing Zhao789221e2009-06-02 14:29:36 -0700917 u8 ireg = 0;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700918 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700919
Bing Zhao789221e2009-06-02 14:29:36 -0700920 card = sdio_get_drvdata(func);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700921 if (!card || !card->priv) {
Amitkumar Karwar7365d472014-11-24 02:40:52 -0800922 BT_ERR("sbi_interrupt(%p) card or priv is NULL, card=%p",
923 func, card);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700924 return;
Bing Zhao789221e2009-06-02 14:29:36 -0700925 }
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700926
927 priv = card->priv;
928
Amitkumar Karwar7b4b8742015-01-01 00:13:41 -0800929 if (priv->surprise_removed)
930 return;
931
Bing Zhao0d367402014-03-28 14:54:39 -0700932 if (card->reg->int_read_to_clear)
933 ret = btmrvl_sdio_read_to_clear(card, &ireg);
934 else
935 ret = btmrvl_sdio_write_to_clear(card, &ireg);
936
Bing Zhao8af48402014-03-27 21:30:42 -0700937 if (ret)
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700938 return;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700939
940 spin_lock_irqsave(&priv->driver_lock, flags);
941 sdio_ireg |= ireg;
942 spin_unlock_irqrestore(&priv->driver_lock, flags);
943
944 btmrvl_interrupt(priv);
Bing Zhao789221e2009-06-02 14:29:36 -0700945}
946
947static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
948{
Bing Zhao789221e2009-06-02 14:29:36 -0700949 struct sdio_func *func;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200950 u8 reg;
Markus Elfringa5c2f4c2018-03-12 10:20:04 +0100951 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700952
Bing Zhao789221e2009-06-02 14:29:36 -0700953 if (!card || !card->func) {
954 BT_ERR("Error: card or function is NULL!");
955 ret = -EINVAL;
956 goto failed;
957 }
958
959 func = card->func;
960
Bing Zhao789221e2009-06-02 14:29:36 -0700961 sdio_claim_host(func);
962
963 ret = sdio_enable_func(func);
964 if (ret) {
965 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
966 ret = -EIO;
967 goto release_host;
968 }
969
970 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
971 if (ret) {
972 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
973 ret = -EIO;
974 goto disable_func;
975 }
976
977 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
978 if (ret) {
979 BT_ERR("cannot set SDIO block size");
980 ret = -EIO;
981 goto release_irq;
982 }
983
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700984 reg = sdio_readb(func, card->reg->io_port_0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700985 if (ret < 0) {
986 ret = -EIO;
987 goto release_irq;
988 }
989
990 card->ioport = reg;
991
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700992 reg = sdio_readb(func, card->reg->io_port_1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700993 if (ret < 0) {
994 ret = -EIO;
995 goto release_irq;
996 }
997
998 card->ioport |= (reg << 8);
999
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001000 reg = sdio_readb(func, card->reg->io_port_2, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -07001001 if (ret < 0) {
1002 ret = -EIO;
1003 goto release_irq;
1004 }
1005
1006 card->ioport |= (reg << 16);
1007
1008 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
1009
Bing Zhao0d367402014-03-28 14:54:39 -07001010 if (card->reg->int_read_to_clear) {
1011 reg = sdio_readb(func, card->reg->host_int_rsr, &ret);
1012 if (ret < 0) {
1013 ret = -EIO;
1014 goto release_irq;
1015 }
1016 sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret);
1017 if (ret < 0) {
1018 ret = -EIO;
1019 goto release_irq;
1020 }
1021
1022 reg = sdio_readb(func, card->reg->card_misc_cfg, &ret);
1023 if (ret < 0) {
1024 ret = -EIO;
1025 goto release_irq;
1026 }
1027 sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret);
1028 if (ret < 0) {
1029 ret = -EIO;
1030 goto release_irq;
1031 }
1032 }
1033
Bing Zhao789221e2009-06-02 14:29:36 -07001034 sdio_set_drvdata(func, card);
1035
1036 sdio_release_host(func);
1037
Bing Zhao789221e2009-06-02 14:29:36 -07001038 return 0;
1039
1040release_irq:
1041 sdio_release_irq(func);
1042
1043disable_func:
1044 sdio_disable_func(func);
1045
1046release_host:
1047 sdio_release_host(func);
1048
1049failed:
Bing Zhao789221e2009-06-02 14:29:36 -07001050 return ret;
1051}
1052
1053static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
1054{
Bing Zhao789221e2009-06-02 14:29:36 -07001055 if (card && card->func) {
1056 sdio_claim_host(card->func);
1057 sdio_release_irq(card->func);
1058 sdio_disable_func(card->func);
1059 sdio_release_host(card->func);
1060 sdio_set_drvdata(card->func, NULL);
1061 }
1062
Bing Zhao789221e2009-06-02 14:29:36 -07001063 return 0;
1064}
1065
1066static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
1067{
1068 int ret;
1069
Marcel Holtmann93742532009-06-13 07:40:18 +02001070 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -07001071 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -07001072
1073 sdio_claim_host(card->func);
1074
1075 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
1076
1077 btmrvl_sdio_get_rx_unit(card);
1078
1079 sdio_release_host(card->func);
1080
Bing Zhao789221e2009-06-02 14:29:36 -07001081 return ret;
1082}
1083
1084static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
1085{
1086 int ret;
1087
Marcel Holtmann93742532009-06-13 07:40:18 +02001088 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -07001089 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -07001090
1091 sdio_claim_host(card->func);
1092
1093 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
1094
1095 sdio_release_host(card->func);
1096
Bing Zhao789221e2009-06-02 14:29:36 -07001097 return ret;
1098}
1099
1100static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
1101 u8 *payload, u16 nb)
1102{
1103 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1104 int ret = 0;
Bing Zhao789221e2009-06-02 14:29:36 -07001105 int blksz;
1106 int i = 0;
1107 u8 *buf = NULL;
1108 void *tmpbuf = NULL;
1109 int tmpbufsz;
1110
Bing Zhao789221e2009-06-02 14:29:36 -07001111 if (!card || !card->func) {
1112 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -07001113 return -EINVAL;
1114 }
1115
Ricky Liangd2f30242016-06-28 15:06:57 +08001116 blksz = DIV_ROUND_UP(nb, SDIO_BLOCK_SIZE) * SDIO_BLOCK_SIZE;
1117
Bing Zhao789221e2009-06-02 14:29:36 -07001118 buf = payload;
Ricky Liangd2f30242016-06-28 15:06:57 +08001119 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1) ||
1120 nb < blksz) {
1121 tmpbufsz = ALIGN_SZ(blksz, BTSDIO_DMA_ALIGN) +
1122 BTSDIO_DMA_ALIGN;
Julia Lawall59598092009-08-06 22:05:18 +02001123 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
1124 if (!tmpbuf)
1125 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -07001126 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
1127 memcpy(buf, payload, nb);
1128 }
1129
Bing Zhao789221e2009-06-02 14:29:36 -07001130 sdio_claim_host(card->func);
1131
1132 do {
1133 /* Transfer data to card */
1134 ret = sdio_writesb(card->func, card->ioport, buf,
Ricky Liangd2f30242016-06-28 15:06:57 +08001135 blksz);
Bing Zhao789221e2009-06-02 14:29:36 -07001136 if (ret < 0) {
1137 i++;
1138 BT_ERR("i=%d writesb failed: %d", i, ret);
Andrei Emeltchenko78c1b8e2012-10-10 17:41:33 +03001139 BT_ERR("hex: %*ph", nb, payload);
Bing Zhao789221e2009-06-02 14:29:36 -07001140 ret = -EIO;
1141 if (i > MAX_WRITE_IOMEM_RETRY)
1142 goto exit;
1143 }
1144 } while (ret);
1145
1146 priv->btmrvl_dev.tx_dnld_rdy = false;
1147
1148exit:
1149 sdio_release_host(card->func);
Yoichi Yuasa88d1a0c2010-02-04 09:55:51 +09001150 kfree(tmpbuf);
Bing Zhao789221e2009-06-02 14:29:36 -07001151
Bing Zhao789221e2009-06-02 14:29:36 -07001152 return ret;
1153}
1154
1155static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
1156{
Gustavo Padovanfaff7f72013-04-18 19:35:33 -03001157 int ret;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001158 u8 fws0;
1159 int pollnum = MAX_POLL_TRIES;
Bing Zhao789221e2009-06-02 14:29:36 -07001160
Bing Zhao789221e2009-06-02 14:29:36 -07001161 if (!card || !card->func) {
1162 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -07001163 return -EINVAL;
1164 }
Bing Zhao789221e2009-06-02 14:29:36 -07001165
1166 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
1167 BT_DBG("Firmware already downloaded!");
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001168 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -07001169 }
1170
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001171 sdio_claim_host(card->func);
1172
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001173 /* Check if other function driver is downloading the firmware */
1174 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -07001175 if (ret) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001176 BT_ERR("Failed to read FW downloading status!");
Bing Zhao789221e2009-06-02 14:29:36 -07001177 ret = -EIO;
1178 goto done;
1179 }
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001180 if (fws0) {
1181 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
Bing Zhao789221e2009-06-02 14:29:36 -07001182
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001183 /* Give other function more time to download the firmware */
1184 pollnum *= 10;
1185 } else {
1186 if (card->helper) {
1187 ret = btmrvl_sdio_download_helper(card);
1188 if (ret) {
1189 BT_ERR("Failed to download helper!");
1190 ret = -EIO;
1191 goto done;
1192 }
1193 }
1194
1195 if (btmrvl_sdio_download_fw_w_helper(card)) {
1196 BT_ERR("Failed to download firmware!");
1197 ret = -EIO;
1198 goto done;
1199 }
Bing Zhao789221e2009-06-02 14:29:36 -07001200 }
1201
Andreas Fenkart69676b12013-04-22 11:10:23 +02001202 /*
1203 * winner or not, with this test the FW synchronizes when the
1204 * module can continue its initialization
1205 */
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001206 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
Bing Zhao789221e2009-06-02 14:29:36 -07001207 BT_ERR("FW failed to be active in time!");
Chin-Ran Lo86f7ac72015-12-29 04:26:33 -08001208 ret = -ETIMEDOUT;
1209 goto done;
Bing Zhao789221e2009-06-02 14:29:36 -07001210 }
1211
Aniket Nagarnaik4863e4c2015-08-06 03:43:09 -07001212 sdio_release_host(card->func);
1213
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001214 return 0;
1215
Bing Zhao789221e2009-06-02 14:29:36 -07001216done:
1217 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -07001218 return ret;
1219}
1220
1221static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
1222{
1223 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1224 int ret = 0;
1225
Bing Zhao789221e2009-06-02 14:29:36 -07001226 if (!card || !card->func) {
1227 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -07001228 return -EINVAL;
1229 }
1230
1231 sdio_claim_host(card->func);
1232
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001233 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -07001234
1235 sdio_release_host(card->func);
1236
1237 BT_DBG("wake up firmware");
1238
Bing Zhao789221e2009-06-02 14:29:36 -07001239 return ret;
1240}
1241
Xinming Hudc759612014-11-24 02:40:53 -08001242static void btmrvl_sdio_dump_regs(struct btmrvl_private *priv)
1243{
1244 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1245 int ret = 0;
1246 unsigned int reg, reg_start, reg_end;
1247 char buf[256], *ptr;
1248 u8 loop, func, data;
1249 int MAX_LOOP = 2;
1250
1251 btmrvl_sdio_wakeup_fw(priv);
1252 sdio_claim_host(card->func);
1253
1254 for (loop = 0; loop < MAX_LOOP; loop++) {
1255 memset(buf, 0, sizeof(buf));
1256 ptr = buf;
1257
1258 if (loop == 0) {
1259 /* Read the registers of SDIO function0 */
1260 func = loop;
1261 reg_start = 0;
1262 reg_end = 9;
1263 } else {
1264 func = 2;
1265 reg_start = 0;
1266 reg_end = 0x09;
1267 }
1268
1269 ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
1270 func, reg_start, reg_end);
1271 for (reg = reg_start; reg <= reg_end; reg++) {
1272 if (func == 0)
1273 data = sdio_f0_readb(card->func, reg, &ret);
1274 else
1275 data = sdio_readb(card->func, reg, &ret);
1276
1277 if (!ret) {
1278 ptr += sprintf(ptr, "%02x ", data);
1279 } else {
1280 ptr += sprintf(ptr, "ERR");
1281 break;
1282 }
1283 }
1284
1285 BT_INFO("%s", buf);
1286 }
1287
1288 sdio_release_host(card->func);
1289}
1290
1291/* This function read/write firmware */
1292static enum
1293rdwr_status btmrvl_sdio_rdwr_firmware(struct btmrvl_private *priv,
1294 u8 doneflag)
1295{
1296 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1297 int ret, tries;
1298 u8 ctrl_data = 0;
1299
1300 sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl,
1301 &ret);
1302
1303 if (ret) {
1304 BT_ERR("SDIO write err");
1305 return RDWR_STATUS_FAILURE;
1306 }
1307
1308 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1309 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
1310 &ret);
1311
1312 if (ret) {
1313 BT_ERR("SDIO read err");
1314 return RDWR_STATUS_FAILURE;
1315 }
1316
1317 if (ctrl_data == FW_DUMP_DONE)
1318 break;
1319 if (doneflag && ctrl_data == doneflag)
1320 return RDWR_STATUS_DONE;
1321 if (ctrl_data != FW_DUMP_HOST_READY) {
1322 BT_INFO("The ctrl reg was changed, re-try again!");
1323 sdio_writeb(card->func, FW_DUMP_HOST_READY,
1324 card->reg->fw_dump_ctrl, &ret);
1325 if (ret) {
1326 BT_ERR("SDIO write err");
1327 return RDWR_STATUS_FAILURE;
1328 }
1329 }
1330 usleep_range(100, 200);
1331 }
1332
1333 if (ctrl_data == FW_DUMP_HOST_READY) {
1334 BT_ERR("Fail to pull ctrl_data");
1335 return RDWR_STATUS_FAILURE;
1336 }
1337
1338 return RDWR_STATUS_SUCCESS;
1339}
1340
1341/* This function dump sdio register and memory data */
Arend van Spriel45650492018-05-18 00:15:12 +02001342static void btmrvl_sdio_coredump(struct device *dev)
Xinming Hudc759612014-11-24 02:40:53 -08001343{
Arend van Spriel45650492018-05-18 00:15:12 +02001344 struct sdio_func *func = dev_to_sdio_func(dev);
1345 struct btmrvl_sdio_card *card;
1346 struct btmrvl_private *priv;
Xinming Hudc759612014-11-24 02:40:53 -08001347 int ret = 0;
1348 unsigned int reg, reg_start, reg_end;
1349 enum rdwr_status stat;
1350 u8 *dbg_ptr, *end_ptr, *fw_dump_data, *fw_dump_ptr;
Xinming Hua1e85f042015-04-21 06:59:56 -07001351 u8 dump_num = 0, idx, i, read_reg, doneflag = 0;
Xinming Hudc759612014-11-24 02:40:53 -08001352 u32 memory_size, fw_dump_len = 0;
Len Baker785077f2021-07-24 14:21:52 +02001353 int size = 0;
Xinming Hudc759612014-11-24 02:40:53 -08001354
Arend van Spriel45650492018-05-18 00:15:12 +02001355 card = sdio_get_drvdata(func);
1356 priv = card->priv;
1357
Xinming Hudc759612014-11-24 02:40:53 -08001358 /* dump sdio register first */
1359 btmrvl_sdio_dump_regs(priv);
1360
1361 if (!card->supports_fw_dump) {
1362 BT_ERR("Firmware dump not supported for this card!");
1363 return;
1364 }
1365
1366 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
1367 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1368
1369 if (entry->mem_ptr) {
1370 vfree(entry->mem_ptr);
1371 entry->mem_ptr = NULL;
1372 }
1373 entry->mem_size = 0;
1374 }
1375
1376 btmrvl_sdio_wakeup_fw(priv);
1377 sdio_claim_host(card->func);
1378
1379 BT_INFO("== btmrvl firmware dump start ==");
1380
1381 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1382 if (stat == RDWR_STATUS_FAILURE)
1383 goto done;
1384
1385 reg = card->reg->fw_dump_start;
1386 /* Read the number of the memories which will dump */
1387 dump_num = sdio_readb(card->func, reg, &ret);
1388
1389 if (ret) {
1390 BT_ERR("SDIO read memory length err");
1391 goto done;
1392 }
1393
1394 /* Read the length of every memory which will dump */
1395 for (idx = 0; idx < dump_num; idx++) {
1396 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1397
1398 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1399 if (stat == RDWR_STATUS_FAILURE)
1400 goto done;
1401
1402 memory_size = 0;
1403 reg = card->reg->fw_dump_start;
1404 for (i = 0; i < 4; i++) {
1405 read_reg = sdio_readb(card->func, reg, &ret);
1406 if (ret) {
1407 BT_ERR("SDIO read err");
1408 goto done;
1409 }
1410 memory_size |= (read_reg << i*8);
1411 reg++;
1412 }
1413
1414 if (memory_size == 0) {
1415 BT_INFO("Firmware dump finished!");
Nachiket Kukade475b9ea2015-09-18 06:40:40 -07001416 sdio_writeb(card->func, FW_DUMP_READ_DONE,
1417 card->reg->fw_dump_ctrl, &ret);
1418 if (ret) {
1419 BT_ERR("SDIO Write MEMDUMP_FINISH ERR");
1420 goto done;
1421 }
Xinming Hudc759612014-11-24 02:40:53 -08001422 break;
1423 }
1424
1425 BT_INFO("%s_SIZE=0x%x", entry->mem_name, memory_size);
1426 entry->mem_ptr = vzalloc(memory_size + 1);
1427 entry->mem_size = memory_size;
1428 if (!entry->mem_ptr) {
1429 BT_ERR("Vzalloc %s failed", entry->mem_name);
1430 goto done;
1431 }
1432
1433 fw_dump_len += (strlen("========Start dump ") +
1434 strlen(entry->mem_name) +
1435 strlen("========\n") +
1436 (memory_size + 1) +
1437 strlen("\n========End dump========\n"));
1438
1439 dbg_ptr = entry->mem_ptr;
1440 end_ptr = dbg_ptr + memory_size;
1441
1442 doneflag = entry->done_flag;
1443 BT_INFO("Start %s output, please wait...",
1444 entry->mem_name);
1445
1446 do {
1447 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1448 if (stat == RDWR_STATUS_FAILURE)
1449 goto done;
1450
1451 reg_start = card->reg->fw_dump_start;
1452 reg_end = card->reg->fw_dump_end;
1453 for (reg = reg_start; reg <= reg_end; reg++) {
1454 *dbg_ptr = sdio_readb(card->func, reg, &ret);
1455 if (ret) {
1456 BT_ERR("SDIO read err");
1457 goto done;
1458 }
1459 if (dbg_ptr < end_ptr)
1460 dbg_ptr++;
1461 else
1462 BT_ERR("Allocated buffer not enough");
1463 }
1464
Colin Ian Kingdd912f42021-06-16 14:07:57 +01001465 if (stat == RDWR_STATUS_DONE) {
Xinming Hudc759612014-11-24 02:40:53 -08001466 BT_INFO("%s done: size=0x%tx",
1467 entry->mem_name,
1468 dbg_ptr - entry->mem_ptr);
1469 break;
1470 }
1471 } while (1);
1472 }
1473
1474 BT_INFO("== btmrvl firmware dump end ==");
1475
1476done:
1477 sdio_release_host(card->func);
1478
1479 if (fw_dump_len == 0)
1480 return;
1481
Len Baker785077f2021-07-24 14:21:52 +02001482 fw_dump_data = vzalloc(fw_dump_len + 1);
Xinming Hudc759612014-11-24 02:40:53 -08001483 if (!fw_dump_data) {
1484 BT_ERR("Vzalloc fw_dump_data fail!");
1485 return;
1486 }
1487 fw_dump_ptr = fw_dump_data;
1488
1489 /* Dump all the memory data into single file, a userspace script will
Derek Robsond98422c2017-07-22 13:47:07 +12001490 * be used to split all the memory data to multiple files
1491 */
Xinming Hudc759612014-11-24 02:40:53 -08001492 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump start");
1493 for (idx = 0; idx < dump_num; idx++) {
1494 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1495
1496 if (entry->mem_ptr) {
Len Baker785077f2021-07-24 14:21:52 +02001497 size += scnprintf(fw_dump_ptr + size,
1498 fw_dump_len + 1 - size,
1499 "========Start dump %s========\n",
1500 entry->mem_name);
Xinming Hudc759612014-11-24 02:40:53 -08001501
Len Baker785077f2021-07-24 14:21:52 +02001502 memcpy(fw_dump_ptr + size, entry->mem_ptr,
1503 entry->mem_size);
1504 size += entry->mem_size;
Xinming Hudc759612014-11-24 02:40:53 -08001505
Len Baker785077f2021-07-24 14:21:52 +02001506 size += scnprintf(fw_dump_ptr + size,
1507 fw_dump_len + 1 - size,
1508 "\n========End dump========\n");
Xinming Hudc759612014-11-24 02:40:53 -08001509
1510 vfree(mem_type_mapping_tbl[idx].mem_ptr);
1511 mem_type_mapping_tbl[idx].mem_ptr = NULL;
1512 }
1513 }
1514
1515 /* fw_dump_data will be free in device coredump release function
Derek Robsond98422c2017-07-22 13:47:07 +12001516 * after 5 min
1517 */
Shengzhen Lid4902632015-08-19 03:12:19 -07001518 dev_coredumpv(&card->func->dev, fw_dump_data, fw_dump_len, GFP_KERNEL);
Xinming Hudc759612014-11-24 02:40:53 -08001519 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump end");
1520}
1521
Bing Zhao789221e2009-06-02 14:29:36 -07001522static int btmrvl_sdio_probe(struct sdio_func *func,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001523 const struct sdio_device_id *id)
Bing Zhao789221e2009-06-02 14:29:36 -07001524{
1525 int ret = 0;
1526 struct btmrvl_private *priv = NULL;
1527 struct btmrvl_sdio_card *card = NULL;
1528
Bing Zhao789221e2009-06-02 14:29:36 -07001529 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1530 id->vendor, id->device, id->class, func->num);
1531
Sachin Kamateb17ea32012-07-27 12:38:36 +05301532 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
1533 if (!card)
1534 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -07001535
1536 card->func = func;
1537
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001538 if (id->driver_data) {
1539 struct btmrvl_sdio_device *data = (void *) id->driver_data;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001540 card->helper = data->helper;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001541 card->firmware = data->firmware;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001542 card->reg = data->reg;
1543 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
Bing Zhao4df82b52014-03-31 14:41:44 -07001544 card->support_pscan_win_report = data->support_pscan_win_report;
Xinming Hudc759612014-11-24 02:40:53 -08001545 card->supports_fw_dump = data->supports_fw_dump;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001546 }
1547
Bing Zhao789221e2009-06-02 14:29:36 -07001548 if (btmrvl_sdio_register_dev(card) < 0) {
1549 BT_ERR("Failed to register BT device!");
Sachin Kamateb17ea32012-07-27 12:38:36 +05301550 return -ENODEV;
Bing Zhao789221e2009-06-02 14:29:36 -07001551 }
1552
1553 /* Disable the interrupts on the card */
1554 btmrvl_sdio_disable_host_int(card);
1555
1556 if (btmrvl_sdio_download_fw(card)) {
1557 BT_ERR("Downloading firmware failed!");
1558 ret = -ENODEV;
1559 goto unreg_dev;
1560 }
1561
Bing Zhao789221e2009-06-02 14:29:36 -07001562 btmrvl_sdio_enable_host_int(card);
1563
Xinming Hubb7f4f02016-04-26 06:57:27 -07001564 /* Device tree node parsing and platform specific configuration*/
1565 btmrvl_sdio_probe_of(&func->dev, card);
1566
Bing Zhao789221e2009-06-02 14:29:36 -07001567 priv = btmrvl_add_card(card);
1568 if (!priv) {
1569 BT_ERR("Initializing card failed!");
1570 ret = -ENODEV;
1571 goto disable_host_int;
1572 }
1573
1574 card->priv = priv;
1575
1576 /* Initialize the interface specific function pointers */
1577 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
1578 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
Amitkumar Karward1d10d72010-05-27 16:38:37 -07001579 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
Bing Zhao789221e2009-06-02 14:29:36 -07001580
Bing Zhao64061602010-03-03 14:37:36 -08001581 if (btmrvl_register_hdev(priv)) {
1582 BT_ERR("Register hdev failed!");
1583 ret = -ENODEV;
1584 goto disable_host_int;
1585 }
1586
Bing Zhao789221e2009-06-02 14:29:36 -07001587 return 0;
1588
1589disable_host_int:
1590 btmrvl_sdio_disable_host_int(card);
1591unreg_dev:
1592 btmrvl_sdio_unregister_dev(card);
Bing Zhao789221e2009-06-02 14:29:36 -07001593 return ret;
1594}
1595
1596static void btmrvl_sdio_remove(struct sdio_func *func)
1597{
1598 struct btmrvl_sdio_card *card;
1599
Bing Zhao789221e2009-06-02 14:29:36 -07001600 if (func) {
1601 card = sdio_get_drvdata(func);
1602 if (card) {
1603 /* Send SHUTDOWN command & disable interrupt
1604 * if user removes the module.
1605 */
1606 if (user_rmmod) {
1607 btmrvl_send_module_cfg_cmd(card->priv,
1608 MODULE_SHUTDOWN_REQ);
1609 btmrvl_sdio_disable_host_int(card);
1610 }
Abhishek Pandit-Subedie660b352020-06-10 18:53:55 -07001611
Colin Ian King92322592017-03-21 12:20:28 +00001612 BT_DBG("unregister dev");
Amitkumar Karwar7b4b8742015-01-01 00:13:41 -08001613 card->priv->surprise_removed = true;
Bing Zhao789221e2009-06-02 14:29:36 -07001614 btmrvl_sdio_unregister_dev(card);
1615 btmrvl_remove_card(card->priv);
Bing Zhao789221e2009-06-02 14:29:36 -07001616 }
1617 }
Bing Zhao789221e2009-06-02 14:29:36 -07001618}
1619
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001620static int btmrvl_sdio_suspend(struct device *dev)
1621{
1622 struct sdio_func *func = dev_to_sdio_func(dev);
1623 struct btmrvl_sdio_card *card;
1624 struct btmrvl_private *priv;
1625 mmc_pm_flag_t pm_flags;
1626 struct hci_dev *hcidev;
1627
1628 if (func) {
1629 pm_flags = sdio_get_host_pm_caps(func);
1630 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1631 pm_flags);
1632 if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1633 BT_ERR("%s: cannot remain alive while suspended",
1634 sdio_func_id(func));
1635 return -ENOSYS;
1636 }
1637 card = sdio_get_drvdata(func);
1638 if (!card || !card->priv) {
1639 BT_ERR("card or priv structure is not valid");
1640 return 0;
1641 }
1642 } else {
1643 BT_ERR("sdio_func is not specified");
1644 return 0;
1645 }
1646
Xinming Hubb7f4f02016-04-26 06:57:27 -07001647 /* Enable platform specific wakeup interrupt */
Abhishek Pandit-Subedie660b352020-06-10 18:53:55 -07001648 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_bt >= 0 &&
1649 device_may_wakeup(dev)) {
Xinming Hubb7f4f02016-04-26 06:57:27 -07001650 card->plt_wake_cfg->wake_by_bt = false;
1651 enable_irq(card->plt_wake_cfg->irq_bt);
1652 enable_irq_wake(card->plt_wake_cfg->irq_bt);
1653 }
1654
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001655 priv = card->priv;
Chin-Ran Lo8cf60cf2016-01-06 06:34:38 -08001656 priv->adapter->is_suspending = true;
Chin-Ran Lo5bf8a742014-07-14 21:05:37 -07001657 hcidev = priv->btmrvl_dev.hcidev;
1658 BT_DBG("%s: SDIO suspend", hcidev->name);
1659 hci_suspend_dev(hcidev);
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001660
1661 if (priv->adapter->hs_state != HS_ACTIVATED) {
1662 if (btmrvl_enable_hs(priv)) {
Colin Ian King8f915662017-02-18 22:52:55 +00001663 BT_ERR("HS not activated, suspend failed!");
Xinming Hu6eb7bd62017-03-31 14:32:31 +08001664 /* Disable platform specific wakeup interrupt */
1665 if (card->plt_wake_cfg &&
Abhishek Pandit-Subedie660b352020-06-10 18:53:55 -07001666 card->plt_wake_cfg->irq_bt >= 0 &&
1667 device_may_wakeup(dev)) {
Xinming Hu6eb7bd62017-03-31 14:32:31 +08001668 disable_irq_wake(card->plt_wake_cfg->irq_bt);
1669 disable_irq(card->plt_wake_cfg->irq_bt);
1670 }
1671
Amitkumar Karware7acf432016-07-15 16:20:52 +05301672 priv->adapter->is_suspending = false;
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001673 return -EBUSY;
1674 }
1675 }
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001676
Chin-Ran Lo8cf60cf2016-01-06 06:34:38 -08001677 priv->adapter->is_suspending = false;
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001678 priv->adapter->is_suspended = true;
1679
1680 /* We will keep the power when hs enabled successfully */
1681 if (priv->adapter->hs_state == HS_ACTIVATED) {
1682 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1683 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001684 }
prasanna karthikf1554b72017-03-28 19:44:00 +00001685
1686 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1687 return 0;
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001688}
1689
1690static int btmrvl_sdio_resume(struct device *dev)
1691{
1692 struct sdio_func *func = dev_to_sdio_func(dev);
1693 struct btmrvl_sdio_card *card;
1694 struct btmrvl_private *priv;
1695 mmc_pm_flag_t pm_flags;
1696 struct hci_dev *hcidev;
1697
1698 if (func) {
1699 pm_flags = sdio_get_host_pm_caps(func);
1700 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1701 pm_flags);
1702 card = sdio_get_drvdata(func);
1703 if (!card || !card->priv) {
1704 BT_ERR("card or priv structure is not valid");
1705 return 0;
1706 }
1707 } else {
1708 BT_ERR("sdio_func is not specified");
1709 return 0;
1710 }
1711 priv = card->priv;
1712
1713 if (!priv->adapter->is_suspended) {
1714 BT_DBG("device already resumed");
1715 return 0;
1716 }
1717
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001718 priv->hw_wakeup_firmware(priv);
1719 priv->adapter->hs_state = HS_DEACTIVATED;
Chin-Ran Lo5bf8a742014-07-14 21:05:37 -07001720 hcidev = priv->btmrvl_dev.hcidev;
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001721 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
Chin-Ran Lo5bf8a742014-07-14 21:05:37 -07001722 priv->adapter->is_suspended = false;
1723 BT_DBG("%s: SDIO resume", hcidev->name);
1724 hci_resume_dev(hcidev);
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001725
Xinming Hubb7f4f02016-04-26 06:57:27 -07001726 /* Disable platform specific wakeup interrupt */
Abhishek Pandit-Subedie660b352020-06-10 18:53:55 -07001727 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_bt >= 0 &&
1728 device_may_wakeup(dev)) {
Xinming Hubb7f4f02016-04-26 06:57:27 -07001729 disable_irq_wake(card->plt_wake_cfg->irq_bt);
Jeffy Chen9af02d82017-01-23 12:18:51 +08001730 disable_irq(card->plt_wake_cfg->irq_bt);
1731 if (card->plt_wake_cfg->wake_by_bt)
1732 /* Undo our disable, since interrupt handler already
1733 * did this.
1734 */
1735 enable_irq(card->plt_wake_cfg->irq_bt);
Xinming Hubb7f4f02016-04-26 06:57:27 -07001736 }
1737
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001738 return 0;
1739}
1740
1741static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1742 .suspend = btmrvl_sdio_suspend,
1743 .resume = btmrvl_sdio_resume,
1744};
1745
Bing Zhao789221e2009-06-02 14:29:36 -07001746static struct sdio_driver bt_mrvl_sdio = {
1747 .name = "btmrvl_sdio",
1748 .id_table = btmrvl_sdio_ids,
1749 .probe = btmrvl_sdio_probe,
1750 .remove = btmrvl_sdio_remove,
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001751 .drv = {
1752 .owner = THIS_MODULE,
Arend van Spriel45650492018-05-18 00:15:12 +02001753 .coredump = btmrvl_sdio_coredump,
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001754 .pm = &btmrvl_sdio_pm_ops,
1755 }
Bing Zhao789221e2009-06-02 14:29:36 -07001756};
1757
Peter Huewe8e5b2302009-12-22 09:34:20 +01001758static int __init btmrvl_sdio_init_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001759{
Bing Zhao789221e2009-06-02 14:29:36 -07001760 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1761 BT_ERR("SDIO Driver Registration Failed");
Bing Zhao789221e2009-06-02 14:29:36 -07001762 return -ENODEV;
1763 }
1764
1765 /* Clear the flag in case user removes the card. */
1766 user_rmmod = 0;
1767
Bing Zhao789221e2009-06-02 14:29:36 -07001768 return 0;
1769}
1770
Peter Huewe8e5b2302009-12-22 09:34:20 +01001771static void __exit btmrvl_sdio_exit_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001772{
Bing Zhao789221e2009-06-02 14:29:36 -07001773 /* Set the flag as user is removing this module. */
1774 user_rmmod = 1;
1775
1776 sdio_unregister_driver(&bt_mrvl_sdio);
Bing Zhao789221e2009-06-02 14:29:36 -07001777}
1778
1779module_init(btmrvl_sdio_init_module);
1780module_exit(btmrvl_sdio_exit_module);
1781
1782MODULE_AUTHOR("Marvell International Ltd.");
Marcel Holtmann9666fb32009-06-09 21:45:04 +02001783MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
Bing Zhao789221e2009-06-02 14:29:36 -07001784MODULE_VERSION(VERSION);
1785MODULE_LICENSE("GPL v2");
Lubomir Rintel77189df2013-03-13 12:04:30 +01001786MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1787MODULE_FIRMWARE("mrvl/sd8688.bin");
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001788MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
Bing Zhao2ac654f2011-11-16 20:40:42 -08001789MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
Xinming Hu3907d552014-09-30 06:45:33 -04001790MODULE_FIRMWARE("mrvl/sd8887_uapsta.bin");
Bing Zhao22f2efe2013-05-13 18:15:32 -07001791MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");
Pali Rohárdbec3af2020-06-03 10:22:28 +02001792MODULE_FIRMWARE("mrvl/sdsd8977_combo_v2.bin");
Tamás Szűcs736233402019-04-14 20:38:14 +02001793MODULE_FIRMWARE("mrvl/sd8987_uapsta.bin");
Pali Rohár00eb0cb2020-06-03 10:22:29 +02001794MODULE_FIRMWARE("mrvl/sdsd8997_combo_v4.bin");