blob: e65ca19970eaf3875f2a47128536e434710755ee [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;
65 struct btmrvl_plt_wake_cfg *cfg = card->plt_wake_cfg;
Xinming Hubb7f4f02016-04-26 06:57:27 -070066
67 if (cfg->irq_bt >= 0) {
68 pr_info("%s: wake by bt", __func__);
69 cfg->wake_by_bt = true;
70 disable_irq_nosync(irq);
71 }
72
Jeffy Chen17e41ea2017-02-24 14:24:30 +080073 pm_wakeup_event(&card->func->dev, 0);
74 pm_system_wakeup();
75
Xinming Hubb7f4f02016-04-26 06:57:27 -070076 return IRQ_HANDLED;
77}
78
79/* This function parses device tree node using mmc subnode devicetree API.
80 * The device node is saved in card->plt_of_node.
81 * If the device tree node exists and includes interrupts attributes, this
82 * function will request platform specific wakeup interrupt.
83 */
84static int btmrvl_sdio_probe_of(struct device *dev,
85 struct btmrvl_sdio_card *card)
86{
87 struct btmrvl_plt_wake_cfg *cfg;
88 int ret;
89
90 if (!dev->of_node ||
91 !of_match_node(btmrvl_sdio_of_match_table, dev->of_node)) {
92 pr_err("sdio platform data not available");
93 return -1;
94 }
95
96 card->plt_of_node = dev->of_node;
97
98 card->plt_wake_cfg = devm_kzalloc(dev, sizeof(*card->plt_wake_cfg),
99 GFP_KERNEL);
100 cfg = card->plt_wake_cfg;
101 if (cfg && card->plt_of_node) {
102 cfg->irq_bt = irq_of_parse_and_map(card->plt_of_node, 0);
103 if (!cfg->irq_bt) {
104 dev_err(dev, "fail to parse irq_bt from device tree");
Jeffy Chenfd1a88d2017-01-23 12:18:52 +0800105 cfg->irq_bt = -1;
Xinming Hubb7f4f02016-04-26 06:57:27 -0700106 } else {
107 ret = devm_request_irq(dev, cfg->irq_bt,
108 btmrvl_wake_irq_bt,
Jeffy Chen17e41ea2017-02-24 14:24:30 +0800109 0, "bt_wake", card);
Xinming Hubb7f4f02016-04-26 06:57:27 -0700110 if (ret) {
111 dev_err(dev,
112 "Failed to request irq_bt %d (%d)\n",
113 cfg->irq_bt, ret);
114 }
115 disable_irq(cfg->irq_bt);
116 }
117 }
118
119 return 0;
120}
121
Bing Zhao789221e2009-06-02 14:29:36 -0700122/* The btmrvl_sdio_remove() callback function is called
123 * when user removes this module from kernel space or ejects
124 * the card from the slot. The driver handles these 2 cases
125 * differently.
126 * If the user is removing the module, a MODULE_SHUTDOWN_REQ
127 * command is sent to firmware and interrupt will be disabled.
128 * If the card is removed, there is no need to send command
129 * or disable interrupt.
130 *
131 * The variable 'user_rmmod' is used to distinguish these two
132 * scenarios. This flag is initialized as FALSE in case the card
133 * is removed, and will be set to TRUE for module removal when
134 * module_exit function is called.
135 */
136static u8 user_rmmod;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700137static u8 sdio_ireg;
Bing Zhao789221e2009-06-02 14:29:36 -0700138
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700139static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
140 .cfg = 0x03,
141 .host_int_mask = 0x04,
142 .host_intstatus = 0x05,
143 .card_status = 0x20,
144 .sq_read_base_addr_a0 = 0x10,
145 .sq_read_base_addr_a1 = 0x11,
146 .card_fw_status0 = 0x40,
147 .card_fw_status1 = 0x41,
148 .card_rx_len = 0x42,
149 .card_rx_unit = 0x43,
150 .io_port_0 = 0x00,
151 .io_port_1 = 0x01,
152 .io_port_2 = 0x02,
Bing Zhao0d367402014-03-28 14:54:39 -0700153 .int_read_to_clear = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700154};
Bing Zhao2ac654f2011-11-16 20:40:42 -0800155static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700156 .cfg = 0x00,
157 .host_int_mask = 0x02,
158 .host_intstatus = 0x03,
159 .card_status = 0x30,
160 .sq_read_base_addr_a0 = 0x40,
161 .sq_read_base_addr_a1 = 0x41,
162 .card_revision = 0x5c,
163 .card_fw_status0 = 0x60,
164 .card_fw_status1 = 0x61,
165 .card_rx_len = 0x62,
166 .card_rx_unit = 0x63,
167 .io_port_0 = 0x78,
168 .io_port_1 = 0x79,
169 .io_port_2 = 0x7a,
Bing Zhao0d367402014-03-28 14:54:39 -0700170 .int_read_to_clear = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700171};
172
Xinming Hu3907d552014-09-30 06:45:33 -0400173static const struct btmrvl_sdio_card_reg btmrvl_reg_8887 = {
174 .cfg = 0x00,
175 .host_int_mask = 0x08,
176 .host_intstatus = 0x0C,
177 .card_status = 0x5C,
178 .sq_read_base_addr_a0 = 0x6C,
179 .sq_read_base_addr_a1 = 0x6D,
180 .card_revision = 0xC8,
181 .card_fw_status0 = 0x88,
182 .card_fw_status1 = 0x89,
183 .card_rx_len = 0x8A,
184 .card_rx_unit = 0x8B,
185 .io_port_0 = 0xE4,
186 .io_port_1 = 0xE5,
187 .io_port_2 = 0xE6,
188 .int_read_to_clear = true,
189 .host_int_rsr = 0x04,
190 .card_misc_cfg = 0xD8,
191};
192
Amitkumar Karwar1e3e4922014-09-30 06:45:32 -0400193static const struct btmrvl_sdio_card_reg btmrvl_reg_8897 = {
Bing Zhao22f2efe2013-05-13 18:15:32 -0700194 .cfg = 0x00,
195 .host_int_mask = 0x02,
196 .host_intstatus = 0x03,
197 .card_status = 0x50,
198 .sq_read_base_addr_a0 = 0x60,
199 .sq_read_base_addr_a1 = 0x61,
200 .card_revision = 0xbc,
201 .card_fw_status0 = 0xc0,
202 .card_fw_status1 = 0xc1,
203 .card_rx_len = 0xc2,
204 .card_rx_unit = 0xc3,
205 .io_port_0 = 0xd8,
206 .io_port_1 = 0xd9,
207 .io_port_2 = 0xda,
Bing Zhao0d367402014-03-28 14:54:39 -0700208 .int_read_to_clear = true,
209 .host_int_rsr = 0x01,
210 .card_misc_cfg = 0xcc,
Xinming Hudc759612014-11-24 02:40:53 -0800211 .fw_dump_ctrl = 0xe2,
212 .fw_dump_start = 0xe3,
213 .fw_dump_end = 0xea,
Bing Zhao22f2efe2013-05-13 18:15:32 -0700214};
215
Amitkumar Karwarf0ef6742015-09-21 03:06:42 -0700216static const struct btmrvl_sdio_card_reg btmrvl_reg_8997 = {
217 .cfg = 0x00,
218 .host_int_mask = 0x08,
219 .host_intstatus = 0x0c,
220 .card_status = 0x5c,
221 .sq_read_base_addr_a0 = 0xf8,
222 .sq_read_base_addr_a1 = 0xf9,
223 .card_revision = 0xc8,
224 .card_fw_status0 = 0xe8,
225 .card_fw_status1 = 0xe9,
226 .card_rx_len = 0xea,
227 .card_rx_unit = 0xeb,
228 .io_port_0 = 0xe4,
229 .io_port_1 = 0xe5,
230 .io_port_2 = 0xe6,
231 .int_read_to_clear = true,
232 .host_int_rsr = 0x04,
233 .card_misc_cfg = 0xD8,
234 .fw_dump_ctrl = 0xf0,
235 .fw_dump_start = 0xf1,
236 .fw_dump_end = 0xf8,
237};
238
Paul Bollec6757862011-10-05 14:56:46 +0200239static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
Lubomir Rintel77189df2013-03-13 12:04:30 +0100240 .helper = "mrvl/sd8688_helper.bin",
241 .firmware = "mrvl/sd8688.bin",
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700242 .reg = &btmrvl_reg_8688,
Bing Zhao4df82b52014-03-31 14:41:44 -0700243 .support_pscan_win_report = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700244 .sd_blksz_fw_dl = 64,
Xinming Hudc759612014-11-24 02:40:53 -0800245 .supports_fw_dump = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700246};
247
248static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
249 .helper = NULL,
250 .firmware = "mrvl/sd8787_uapsta.bin",
Bing Zhao2ac654f2011-11-16 20:40:42 -0800251 .reg = &btmrvl_reg_87xx,
Bing Zhao4df82b52014-03-31 14:41:44 -0700252 .support_pscan_win_report = false,
Bing Zhao2ac654f2011-11-16 20:40:42 -0800253 .sd_blksz_fw_dl = 256,
Xinming Hudc759612014-11-24 02:40:53 -0800254 .supports_fw_dump = false,
Bing Zhao2ac654f2011-11-16 20:40:42 -0800255};
256
257static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
258 .helper = NULL,
259 .firmware = "mrvl/sd8797_uapsta.bin",
260 .reg = &btmrvl_reg_87xx,
Bing Zhao4df82b52014-03-31 14:41:44 -0700261 .support_pscan_win_report = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700262 .sd_blksz_fw_dl = 256,
Xinming Hudc759612014-11-24 02:40:53 -0800263 .supports_fw_dump = false,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200264};
265
Xinming Hu3907d552014-09-30 06:45:33 -0400266static const struct btmrvl_sdio_device btmrvl_sdio_sd8887 = {
267 .helper = NULL,
268 .firmware = "mrvl/sd8887_uapsta.bin",
269 .reg = &btmrvl_reg_8887,
270 .support_pscan_win_report = true,
271 .sd_blksz_fw_dl = 256,
Xinming Hudc759612014-11-24 02:40:53 -0800272 .supports_fw_dump = false,
Xinming Hu3907d552014-09-30 06:45:33 -0400273};
274
Bing Zhao22f2efe2013-05-13 18:15:32 -0700275static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = {
276 .helper = NULL,
277 .firmware = "mrvl/sd8897_uapsta.bin",
Amitkumar Karwar1e3e4922014-09-30 06:45:32 -0400278 .reg = &btmrvl_reg_8897,
Bing Zhao4df82b52014-03-31 14:41:44 -0700279 .support_pscan_win_report = true,
Bing Zhao22f2efe2013-05-13 18:15:32 -0700280 .sd_blksz_fw_dl = 256,
Xinming Hudc759612014-11-24 02:40:53 -0800281 .supports_fw_dump = true,
Bing Zhao22f2efe2013-05-13 18:15:32 -0700282};
283
Amitkumar Karwarf0ef6742015-09-21 03:06:42 -0700284static const struct btmrvl_sdio_device btmrvl_sdio_sd8997 = {
285 .helper = NULL,
286 .firmware = "mrvl/sd8997_uapsta.bin",
287 .reg = &btmrvl_reg_8997,
288 .support_pscan_win_report = true,
289 .sd_blksz_fw_dl = 256,
290 .supports_fw_dump = true,
291};
292
Bing Zhao789221e2009-06-02 14:29:36 -0700293static const struct sdio_device_id btmrvl_sdio_ids[] = {
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200294 /* Marvell SD8688 Bluetooth device */
295 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
Amitkumar Karwar53d6cfa2015-09-21 03:06:41 -0700296 .driver_data = (unsigned long)&btmrvl_sdio_sd8688 },
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700297 /* Marvell SD8787 Bluetooth device */
298 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
Amitkumar Karwar53d6cfa2015-09-21 03:06:41 -0700299 .driver_data = (unsigned long)&btmrvl_sdio_sd8787 },
Bing Zhao37ed5612012-05-23 18:50:04 -0700300 /* Marvell SD8787 Bluetooth AMP device */
301 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911B),
Amitkumar Karwar53d6cfa2015-09-21 03:06:41 -0700302 .driver_data = (unsigned long)&btmrvl_sdio_sd8787 },
Bing Zhao2ac654f2011-11-16 20:40:42 -0800303 /* Marvell SD8797 Bluetooth device */
304 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A),
Amitkumar Karwar53d6cfa2015-09-21 03:06:41 -0700305 .driver_data = (unsigned long)&btmrvl_sdio_sd8797 },
Xinming Hu3907d552014-09-30 06:45:33 -0400306 /* Marvell SD8887 Bluetooth device */
307 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9136),
308 .driver_data = (unsigned long)&btmrvl_sdio_sd8887 },
Bing Zhao22f2efe2013-05-13 18:15:32 -0700309 /* Marvell SD8897 Bluetooth device */
310 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912E),
Amitkumar Karwar53d6cfa2015-09-21 03:06:41 -0700311 .driver_data = (unsigned long)&btmrvl_sdio_sd8897 },
Amitkumar Karwarf0ef6742015-09-21 03:06:42 -0700312 /* Marvell SD8997 Bluetooth device */
313 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9142),
314 .driver_data = (unsigned long)&btmrvl_sdio_sd8997 },
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200315
316 { } /* Terminating entry */
Bing Zhao789221e2009-06-02 14:29:36 -0700317};
318
319MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
320
Bing Zhao789221e2009-06-02 14:29:36 -0700321static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
322{
323 u8 reg;
324 int ret;
325
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700326 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700327 if (!ret)
328 card->rx_unit = reg;
329
Bing Zhao789221e2009-06-02 14:29:36 -0700330 return ret;
331}
332
333static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
334{
Bing Zhao789221e2009-06-02 14:29:36 -0700335 u8 fws0, fws1;
Marcel Holtmann93742532009-06-13 07:40:18 +0200336 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700337
338 *dat = 0;
339
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700340 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Kulikov Vasiliy278be272010-07-05 12:01:22 +0400341 if (ret)
342 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700343
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700344 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200345 if (ret)
Bing Zhao789221e2009-06-02 14:29:36 -0700346 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700347
348 *dat = (((u16) fws1) << 8) | fws0;
349
Bing Zhao789221e2009-06-02 14:29:36 -0700350 return 0;
351}
352
353static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
354{
Bing Zhao789221e2009-06-02 14:29:36 -0700355 u8 reg;
Marcel Holtmann93742532009-06-13 07:40:18 +0200356 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700357
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700358 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700359 if (!ret)
360 *dat = (u16) reg << card->rx_unit;
361
Bing Zhao789221e2009-06-02 14:29:36 -0700362 return ret;
363}
364
365static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200366 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700367{
368 int ret;
369
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700370 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700371 if (ret) {
372 BT_ERR("Unable to enable the host interrupt!");
373 ret = -EIO;
374 }
375
Bing Zhao789221e2009-06-02 14:29:36 -0700376 return ret;
377}
378
379static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200380 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700381{
Bing Zhao789221e2009-06-02 14:29:36 -0700382 u8 host_int_mask;
Marcel Holtmann93742532009-06-13 07:40:18 +0200383 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700384
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700385 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200386 if (ret)
387 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700388
389 host_int_mask &= ~mask;
390
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700391 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700392 if (ret < 0) {
393 BT_ERR("Unable to disable the host interrupt!");
Marcel Holtmann93742532009-06-13 07:40:18 +0200394 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700395 }
396
Marcel Holtmann93742532009-06-13 07:40:18 +0200397 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700398}
399
400static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
401{
402 unsigned int tries;
Bing Zhao789221e2009-06-02 14:29:36 -0700403 u8 status;
Marcel Holtmann93742532009-06-13 07:40:18 +0200404 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700405
406 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700407 status = sdio_readb(card->func, card->reg->card_status, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700408 if (ret)
409 goto failed;
410 if ((status & bits) == bits)
Marcel Holtmann93742532009-06-13 07:40:18 +0200411 return ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700412
413 udelay(1);
414 }
415
416 ret = -ETIMEDOUT;
417
418failed:
419 BT_ERR("FAILED! ret=%d", ret);
420
Bing Zhao789221e2009-06-02 14:29:36 -0700421 return ret;
422}
423
424static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200425 int pollnum)
Bing Zhao789221e2009-06-02 14:29:36 -0700426{
Bing Zhao789221e2009-06-02 14:29:36 -0700427 u16 firmwarestat;
Andreas Fenkart69676b12013-04-22 11:10:23 +0200428 int tries, ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700429
Bing Zhao789221e2009-06-02 14:29:36 -0700430 /* Wait for firmware to become ready */
431 for (tries = 0; tries < pollnum; tries++) {
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200432 sdio_claim_host(card->func);
433 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat);
434 sdio_release_host(card->func);
435 if (ret < 0)
Bing Zhao789221e2009-06-02 14:29:36 -0700436 continue;
437
Andreas Fenkart69676b12013-04-22 11:10:23 +0200438 if (firmwarestat == FIRMWARE_READY)
439 return 0;
440
Wei-Ning Huang9a012422016-03-08 11:40:06 +0800441 msleep(100);
Bing Zhao789221e2009-06-02 14:29:36 -0700442 }
443
Andreas Fenkart69676b12013-04-22 11:10:23 +0200444 return -ETIMEDOUT;
Bing Zhao789221e2009-06-02 14:29:36 -0700445}
446
447static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
448{
449 const struct firmware *fw_helper = NULL;
450 const u8 *helper = NULL;
451 int ret;
452 void *tmphlprbuf = NULL;
453 int tmphlprbufsz, hlprblknow, helperlen;
454 u8 *helperbuf;
455 u32 tx_len;
456
Bing Zhao789221e2009-06-02 14:29:36 -0700457 ret = request_firmware(&fw_helper, card->helper,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200458 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700459 if ((ret < 0) || !fw_helper) {
460 BT_ERR("request_firmware(helper) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200461 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700462 ret = -ENOENT;
463 goto done;
464 }
465
466 helper = fw_helper->data;
467 helperlen = fw_helper->size;
468
469 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200470 helperlen, SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700471
472 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
473
Julia Lawallf8df39f2010-05-13 22:02:03 +0200474 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700475 if (!tmphlprbuf) {
476 BT_ERR("Unable to allocate buffer for helper."
477 " Terminating download");
478 ret = -ENOMEM;
479 goto done;
480 }
481
Bing Zhao789221e2009-06-02 14:29:36 -0700482 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
483
484 /* Perform helper data transfer */
485 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
486 - SDIO_HEADER_LEN;
487 hlprblknow = 0;
488
489 do {
490 ret = btmrvl_sdio_poll_card_status(card,
491 CARD_IO_READY | DN_LD_CARD_RDY);
492 if (ret < 0) {
493 BT_ERR("Helper download poll status timeout @ %d",
494 hlprblknow);
495 goto done;
496 }
497
498 /* Check if there is more data? */
499 if (hlprblknow >= helperlen)
500 break;
501
502 if (helperlen - hlprblknow < tx_len)
503 tx_len = helperlen - hlprblknow;
504
505 /* Little-endian */
506 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
507 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
508 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
509 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
510
511 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
512 tx_len);
513
514 /* Now send the data */
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200515 ret = sdio_writesb(card->func, card->ioport, helperbuf,
516 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700517 if (ret < 0) {
518 BT_ERR("IO error during helper download @ %d",
519 hlprblknow);
520 goto done;
521 }
522
523 hlprblknow += tx_len;
524 } while (true);
525
526 BT_DBG("Transferring helper image EOF block");
527
528 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
529
530 ret = sdio_writesb(card->func, card->ioport, helperbuf,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200531 SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700532 if (ret < 0) {
533 BT_ERR("IO error in writing helper image EOF block");
534 goto done;
535 }
536
537 ret = 0;
538
539done:
540 kfree(tmphlprbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200541 release_firmware(fw_helper);
Bing Zhao789221e2009-06-02 14:29:36 -0700542 return ret;
543}
544
545static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
546{
547 const struct firmware *fw_firmware = NULL;
548 const u8 *firmware = NULL;
549 int firmwarelen, tmpfwbufsz, ret;
550 unsigned int tries, offset;
551 u8 base0, base1;
552 void *tmpfwbuf = NULL;
553 u8 *fwbuf;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700554 u16 len, blksz_dl = card->sd_blksz_fw_dl;
Bing Zhao789221e2009-06-02 14:29:36 -0700555 int txlen = 0, tx_blocks = 0, count = 0;
556
Bing Zhao789221e2009-06-02 14:29:36 -0700557 ret = request_firmware(&fw_firmware, card->firmware,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200558 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700559 if ((ret < 0) || !fw_firmware) {
560 BT_ERR("request_firmware(firmware) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200561 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700562 ret = -ENOENT;
563 goto done;
564 }
565
566 firmware = fw_firmware->data;
567 firmwarelen = fw_firmware->size;
568
569 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
570
571 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
Julia Lawallf8df39f2010-05-13 22:02:03 +0200572 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700573 if (!tmpfwbuf) {
574 BT_ERR("Unable to allocate buffer for firmware."
575 " Terminating download");
576 ret = -ENOMEM;
577 goto done;
578 }
579
Bing Zhao789221e2009-06-02 14:29:36 -0700580 /* Ensure aligned firmware buffer */
581 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
582
583 /* Perform firmware data transfer */
584 offset = 0;
585 do {
586 ret = btmrvl_sdio_poll_card_status(card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200587 CARD_IO_READY | DN_LD_CARD_RDY);
Bing Zhao789221e2009-06-02 14:29:36 -0700588 if (ret < 0) {
589 BT_ERR("FW download with helper poll status"
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200590 " timeout @ %d", offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700591 goto done;
592 }
593
594 /* Check if there is more data ? */
595 if (offset >= firmwarelen)
596 break;
597
598 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
599 base0 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700600 card->reg->sq_read_base_addr_a0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700601 if (ret) {
602 BT_ERR("BASE0 register read failed:"
603 " base0 = 0x%04X(%d)."
604 " Terminating download",
605 base0, base0);
606 ret = -EIO;
607 goto done;
608 }
609 base1 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700610 card->reg->sq_read_base_addr_a1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700611 if (ret) {
612 BT_ERR("BASE1 register read failed:"
613 " base1 = 0x%04X(%d)."
614 " Terminating download",
615 base1, base1);
616 ret = -EIO;
617 goto done;
618 }
619
620 len = (((u16) base1) << 8) | base0;
621 if (len)
622 break;
623
624 udelay(10);
625 }
626
627 if (!len)
628 break;
629 else if (len > BTM_UPLD_SIZE) {
630 BT_ERR("FW download failure @%d, invalid length %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200631 offset, len);
Bing Zhao789221e2009-06-02 14:29:36 -0700632 ret = -EINVAL;
633 goto done;
634 }
635
636 txlen = len;
637
638 if (len & BIT(0)) {
639 count++;
640 if (count > MAX_WRITE_IOMEM_RETRY) {
641 BT_ERR("FW download failure @%d, "
642 "over max retry count", offset);
643 ret = -EIO;
644 goto done;
645 }
646 BT_ERR("FW CRC error indicated by the helper: "
647 "len = 0x%04X, txlen = %d", len, txlen);
648 len &= ~BIT(0);
649 /* Set txlen to 0 so as to resend from same offset */
650 txlen = 0;
651 } else {
652 count = 0;
653
654 /* Last block ? */
655 if (firmwarelen - offset < txlen)
656 txlen = firmwarelen - offset;
657
Andy Shevchenkoba34dd32013-08-02 14:10:12 +0300658 tx_blocks = DIV_ROUND_UP(txlen, blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700659
660 memcpy(fwbuf, &firmware[offset], txlen);
661 }
662
663 ret = sdio_writesb(card->func, card->ioport, fwbuf,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700664 tx_blocks * blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700665
666 if (ret < 0) {
667 BT_ERR("FW download, writesb(%d) failed @%d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200668 count, offset);
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700669 sdio_writeb(card->func, HOST_CMD53_FIN,
670 card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700671 if (ret)
672 BT_ERR("writeb failed (CFG)");
673 }
674
675 offset += txlen;
676 } while (true);
677
Amitkumar Karwar1c6098e2015-01-01 00:13:42 -0800678 BT_INFO("FW download over, size %d bytes", offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700679
680 ret = 0;
681
682done:
683 kfree(tmpfwbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200684 release_firmware(fw_firmware);
Bing Zhao789221e2009-06-02 14:29:36 -0700685 return ret;
686}
687
688static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
689{
690 u16 buf_len = 0;
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300691 int ret, num_blocks, blksz;
Bing Zhao789221e2009-06-02 14:29:36 -0700692 struct sk_buff *skb = NULL;
693 u32 type;
694 u8 *payload = NULL;
695 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
696 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
697
Bing Zhao789221e2009-06-02 14:29:36 -0700698 if (!card || !card->func) {
699 BT_ERR("card or function is NULL!");
700 ret = -EINVAL;
701 goto exit;
702 }
703
704 /* Read the length of data to be transferred */
705 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
706 if (ret < 0) {
707 BT_ERR("read rx_len failed");
708 ret = -EIO;
709 goto exit;
710 }
711
712 blksz = SDIO_BLOCK_SIZE;
Andrei Emeltchenkoe678bad2012-09-28 14:36:09 +0300713 num_blocks = DIV_ROUND_UP(buf_len, blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700714
715 if (buf_len <= SDIO_HEADER_LEN
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300716 || (num_blocks * blksz) > ALLOC_BUF_SIZE) {
Bing Zhao789221e2009-06-02 14:29:36 -0700717 BT_ERR("invalid packet length: %d", buf_len);
718 ret = -EINVAL;
719 goto exit;
720 }
721
722 /* Allocate buffer */
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300723 skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_ATOMIC);
Prasanna Karthik1f438c62015-09-25 09:03:14 +0000724 if (!skb) {
Bing Zhao789221e2009-06-02 14:29:36 -0700725 BT_ERR("No free skb");
Wei Yongjun6ea81c42013-06-05 10:16:55 +0800726 ret = -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700727 goto exit;
728 }
729
Bing Zhao3318b232009-07-08 11:44:14 -0700730 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
731 skb_put(skb, (unsigned long) skb->data &
732 (BTSDIO_DMA_ALIGN - 1));
733 skb_pull(skb, (unsigned long) skb->data &
734 (BTSDIO_DMA_ALIGN - 1));
Bing Zhao789221e2009-06-02 14:29:36 -0700735 }
736
Bing Zhao3318b232009-07-08 11:44:14 -0700737 payload = skb->data;
Bing Zhao789221e2009-06-02 14:29:36 -0700738
739 ret = sdio_readsb(card->func, payload, card->ioport,
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300740 num_blocks * blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700741 if (ret < 0) {
742 BT_ERR("readsb failed: %d", ret);
743 ret = -EIO;
744 goto exit;
745 }
746
747 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
748 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
749 */
750
751 buf_len = payload[0];
Andrei Emeltchenko9cb23dd2012-09-28 14:36:10 +0300752 buf_len |= payload[1] << 8;
753 buf_len |= payload[2] << 16;
754
755 if (buf_len > blksz * num_blocks) {
756 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
757 buf_len, blksz * num_blocks);
758 ret = -EIO;
759 goto exit;
760 }
761
Bing Zhao789221e2009-06-02 14:29:36 -0700762 type = payload[3];
763
764 switch (type) {
765 case HCI_ACLDATA_PKT:
766 case HCI_SCODATA_PKT:
767 case HCI_EVENT_PKT:
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100768 hci_skb_pkt_type(skb) = type;
Bing Zhao789221e2009-06-02 14:29:36 -0700769 skb_put(skb, buf_len);
770 skb_pull(skb, SDIO_HEADER_LEN);
771
Andrei Emeltchenkoea1e76a2012-06-13 13:35:44 +0300772 if (type == HCI_EVENT_PKT) {
773 if (btmrvl_check_evtpkt(priv, skb))
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700774 hci_recv_frame(hdev, skb);
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +0300775 } else {
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700776 hci_recv_frame(hdev, skb);
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +0300777 }
Bing Zhao789221e2009-06-02 14:29:36 -0700778
Bing Zhao789221e2009-06-02 14:29:36 -0700779 hdev->stat.byte_rx += buf_len;
780 break;
781
782 case MRVL_VENDOR_PKT:
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100783 hci_skb_pkt_type(skb) = HCI_VENDOR_PKT;
Bing Zhao789221e2009-06-02 14:29:36 -0700784 skb_put(skb, buf_len);
785 skb_pull(skb, SDIO_HEADER_LEN);
786
787 if (btmrvl_process_event(priv, skb))
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700788 hci_recv_frame(hdev, skb);
Bing Zhao789221e2009-06-02 14:29:36 -0700789
790 hdev->stat.byte_rx += buf_len;
791 break;
792
793 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200794 BT_ERR("Unknown packet type:%d", type);
Andrei Emeltchenko78c1b8e2012-10-10 17:41:33 +0300795 BT_ERR("hex: %*ph", blksz * num_blocks, payload);
Bing Zhao789221e2009-06-02 14:29:36 -0700796
797 kfree_skb(skb);
798 skb = NULL;
799 break;
800 }
801
802exit:
803 if (ret) {
804 hdev->stat.err_rx++;
Wei Yongjun79232962012-08-28 21:12:48 +0800805 kfree_skb(skb);
Bing Zhao789221e2009-06-02 14:29:36 -0700806 }
807
Bing Zhao789221e2009-06-02 14:29:36 -0700808 return ret;
809}
810
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700811static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
Bing Zhao789221e2009-06-02 14:29:36 -0700812{
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700813 ulong flags;
814 u8 ireg;
Bing Zhao789221e2009-06-02 14:29:36 -0700815 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
816
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700817 spin_lock_irqsave(&priv->driver_lock, flags);
818 ireg = sdio_ireg;
819 sdio_ireg = 0;
820 spin_unlock_irqrestore(&priv->driver_lock, flags);
Bing Zhao789221e2009-06-02 14:29:36 -0700821
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700822 sdio_claim_host(card->func);
823 if (ireg & DN_LD_HOST_INT_STATUS) {
Bing Zhao789221e2009-06-02 14:29:36 -0700824 if (priv->btmrvl_dev.tx_dnld_rdy)
825 BT_DBG("tx_done already received: "
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700826 " int_status=0x%x", ireg);
Bing Zhao789221e2009-06-02 14:29:36 -0700827 else
828 priv->btmrvl_dev.tx_dnld_rdy = true;
829 }
830
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700831 if (ireg & UP_LD_HOST_INT_STATUS)
Bing Zhao789221e2009-06-02 14:29:36 -0700832 btmrvl_sdio_card_to_host(priv);
833
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700834 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -0700835
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700836 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700837}
838
Bing Zhao0d367402014-03-28 14:54:39 -0700839static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
840{
841 struct btmrvl_adapter *adapter = card->priv->adapter;
842 int ret;
843
844 ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE);
845 if (ret) {
846 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret);
847 return ret;
848 }
849
850 *ireg = adapter->hw_regs[card->reg->host_intstatus];
851 BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg);
852
853 return 0;
854}
855
Bing Zhao8af48402014-03-27 21:30:42 -0700856static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
857{
858 int ret;
859
860 *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
861 if (ret) {
862 BT_ERR("sdio_readb: read int status failed: %d", ret);
863 return ret;
864 }
865
866 if (*ireg) {
867 /*
868 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
869 * Clear the interrupt status register and re-enable the
870 * interrupt.
871 */
872 BT_DBG("int_status = 0x%x", *ireg);
873
874 sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS |
875 UP_LD_HOST_INT_STATUS),
876 card->reg->host_intstatus, &ret);
877 if (ret) {
878 BT_ERR("sdio_writeb: clear int status failed: %d", ret);
879 return ret;
880 }
881 }
882
883 return 0;
884}
885
Bing Zhao789221e2009-06-02 14:29:36 -0700886static void btmrvl_sdio_interrupt(struct sdio_func *func)
887{
888 struct btmrvl_private *priv;
Bing Zhao789221e2009-06-02 14:29:36 -0700889 struct btmrvl_sdio_card *card;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700890 ulong flags;
Bing Zhao789221e2009-06-02 14:29:36 -0700891 u8 ireg = 0;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700892 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700893
Bing Zhao789221e2009-06-02 14:29:36 -0700894 card = sdio_get_drvdata(func);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700895 if (!card || !card->priv) {
Amitkumar Karwar7365d472014-11-24 02:40:52 -0800896 BT_ERR("sbi_interrupt(%p) card or priv is NULL, card=%p",
897 func, card);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700898 return;
Bing Zhao789221e2009-06-02 14:29:36 -0700899 }
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700900
901 priv = card->priv;
902
Amitkumar Karwar7b4b8742015-01-01 00:13:41 -0800903 if (priv->surprise_removed)
904 return;
905
Bing Zhao0d367402014-03-28 14:54:39 -0700906 if (card->reg->int_read_to_clear)
907 ret = btmrvl_sdio_read_to_clear(card, &ireg);
908 else
909 ret = btmrvl_sdio_write_to_clear(card, &ireg);
910
Bing Zhao8af48402014-03-27 21:30:42 -0700911 if (ret)
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700912 return;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700913
914 spin_lock_irqsave(&priv->driver_lock, flags);
915 sdio_ireg |= ireg;
916 spin_unlock_irqrestore(&priv->driver_lock, flags);
917
918 btmrvl_interrupt(priv);
Bing Zhao789221e2009-06-02 14:29:36 -0700919}
920
921static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
922{
Bing Zhao789221e2009-06-02 14:29:36 -0700923 struct sdio_func *func;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200924 u8 reg;
925 int ret = 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700926
Bing Zhao789221e2009-06-02 14:29:36 -0700927 if (!card || !card->func) {
928 BT_ERR("Error: card or function is NULL!");
929 ret = -EINVAL;
930 goto failed;
931 }
932
933 func = card->func;
934
Bing Zhao789221e2009-06-02 14:29:36 -0700935 sdio_claim_host(func);
936
937 ret = sdio_enable_func(func);
938 if (ret) {
939 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
940 ret = -EIO;
941 goto release_host;
942 }
943
944 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
945 if (ret) {
946 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
947 ret = -EIO;
948 goto disable_func;
949 }
950
951 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
952 if (ret) {
953 BT_ERR("cannot set SDIO block size");
954 ret = -EIO;
955 goto release_irq;
956 }
957
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700958 reg = sdio_readb(func, card->reg->io_port_0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700959 if (ret < 0) {
960 ret = -EIO;
961 goto release_irq;
962 }
963
964 card->ioport = reg;
965
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700966 reg = sdio_readb(func, card->reg->io_port_1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700967 if (ret < 0) {
968 ret = -EIO;
969 goto release_irq;
970 }
971
972 card->ioport |= (reg << 8);
973
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700974 reg = sdio_readb(func, card->reg->io_port_2, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700975 if (ret < 0) {
976 ret = -EIO;
977 goto release_irq;
978 }
979
980 card->ioport |= (reg << 16);
981
982 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
983
Bing Zhao0d367402014-03-28 14:54:39 -0700984 if (card->reg->int_read_to_clear) {
985 reg = sdio_readb(func, card->reg->host_int_rsr, &ret);
986 if (ret < 0) {
987 ret = -EIO;
988 goto release_irq;
989 }
990 sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret);
991 if (ret < 0) {
992 ret = -EIO;
993 goto release_irq;
994 }
995
996 reg = sdio_readb(func, card->reg->card_misc_cfg, &ret);
997 if (ret < 0) {
998 ret = -EIO;
999 goto release_irq;
1000 }
1001 sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret);
1002 if (ret < 0) {
1003 ret = -EIO;
1004 goto release_irq;
1005 }
1006 }
1007
Bing Zhao789221e2009-06-02 14:29:36 -07001008 sdio_set_drvdata(func, card);
1009
1010 sdio_release_host(func);
1011
Bing Zhao789221e2009-06-02 14:29:36 -07001012 return 0;
1013
1014release_irq:
1015 sdio_release_irq(func);
1016
1017disable_func:
1018 sdio_disable_func(func);
1019
1020release_host:
1021 sdio_release_host(func);
1022
1023failed:
Bing Zhao789221e2009-06-02 14:29:36 -07001024 return ret;
1025}
1026
1027static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
1028{
Bing Zhao789221e2009-06-02 14:29:36 -07001029 if (card && card->func) {
1030 sdio_claim_host(card->func);
1031 sdio_release_irq(card->func);
1032 sdio_disable_func(card->func);
1033 sdio_release_host(card->func);
1034 sdio_set_drvdata(card->func, NULL);
1035 }
1036
Bing Zhao789221e2009-06-02 14:29:36 -07001037 return 0;
1038}
1039
1040static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
1041{
1042 int ret;
1043
Marcel Holtmann93742532009-06-13 07:40:18 +02001044 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -07001045 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -07001046
1047 sdio_claim_host(card->func);
1048
1049 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
1050
1051 btmrvl_sdio_get_rx_unit(card);
1052
1053 sdio_release_host(card->func);
1054
Bing Zhao789221e2009-06-02 14:29:36 -07001055 return ret;
1056}
1057
1058static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
1059{
1060 int ret;
1061
Marcel Holtmann93742532009-06-13 07:40:18 +02001062 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -07001063 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -07001064
1065 sdio_claim_host(card->func);
1066
1067 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
1068
1069 sdio_release_host(card->func);
1070
Bing Zhao789221e2009-06-02 14:29:36 -07001071 return ret;
1072}
1073
1074static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
1075 u8 *payload, u16 nb)
1076{
1077 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1078 int ret = 0;
Bing Zhao789221e2009-06-02 14:29:36 -07001079 int blksz;
1080 int i = 0;
1081 u8 *buf = NULL;
1082 void *tmpbuf = NULL;
1083 int tmpbufsz;
1084
Bing Zhao789221e2009-06-02 14:29:36 -07001085 if (!card || !card->func) {
1086 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -07001087 return -EINVAL;
1088 }
1089
Ricky Liangd2f30242016-06-28 15:06:57 +08001090 blksz = DIV_ROUND_UP(nb, SDIO_BLOCK_SIZE) * SDIO_BLOCK_SIZE;
1091
Bing Zhao789221e2009-06-02 14:29:36 -07001092 buf = payload;
Ricky Liangd2f30242016-06-28 15:06:57 +08001093 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1) ||
1094 nb < blksz) {
1095 tmpbufsz = ALIGN_SZ(blksz, BTSDIO_DMA_ALIGN) +
1096 BTSDIO_DMA_ALIGN;
Julia Lawall59598092009-08-06 22:05:18 +02001097 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
1098 if (!tmpbuf)
1099 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -07001100 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
1101 memcpy(buf, payload, nb);
1102 }
1103
Bing Zhao789221e2009-06-02 14:29:36 -07001104 sdio_claim_host(card->func);
1105
1106 do {
1107 /* Transfer data to card */
1108 ret = sdio_writesb(card->func, card->ioport, buf,
Ricky Liangd2f30242016-06-28 15:06:57 +08001109 blksz);
Bing Zhao789221e2009-06-02 14:29:36 -07001110 if (ret < 0) {
1111 i++;
1112 BT_ERR("i=%d writesb failed: %d", i, ret);
Andrei Emeltchenko78c1b8e2012-10-10 17:41:33 +03001113 BT_ERR("hex: %*ph", nb, payload);
Bing Zhao789221e2009-06-02 14:29:36 -07001114 ret = -EIO;
1115 if (i > MAX_WRITE_IOMEM_RETRY)
1116 goto exit;
1117 }
1118 } while (ret);
1119
1120 priv->btmrvl_dev.tx_dnld_rdy = false;
1121
1122exit:
1123 sdio_release_host(card->func);
Yoichi Yuasa88d1a0c2010-02-04 09:55:51 +09001124 kfree(tmpbuf);
Bing Zhao789221e2009-06-02 14:29:36 -07001125
Bing Zhao789221e2009-06-02 14:29:36 -07001126 return ret;
1127}
1128
1129static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
1130{
Gustavo Padovanfaff7f72013-04-18 19:35:33 -03001131 int ret;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001132 u8 fws0;
1133 int pollnum = MAX_POLL_TRIES;
Bing Zhao789221e2009-06-02 14:29:36 -07001134
Bing Zhao789221e2009-06-02 14:29:36 -07001135 if (!card || !card->func) {
1136 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -07001137 return -EINVAL;
1138 }
Bing Zhao789221e2009-06-02 14:29:36 -07001139
1140 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
1141 BT_DBG("Firmware already downloaded!");
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001142 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -07001143 }
1144
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001145 sdio_claim_host(card->func);
1146
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001147 /* Check if other function driver is downloading the firmware */
1148 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -07001149 if (ret) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001150 BT_ERR("Failed to read FW downloading status!");
Bing Zhao789221e2009-06-02 14:29:36 -07001151 ret = -EIO;
1152 goto done;
1153 }
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001154 if (fws0) {
1155 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
Bing Zhao789221e2009-06-02 14:29:36 -07001156
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001157 /* Give other function more time to download the firmware */
1158 pollnum *= 10;
1159 } else {
1160 if (card->helper) {
1161 ret = btmrvl_sdio_download_helper(card);
1162 if (ret) {
1163 BT_ERR("Failed to download helper!");
1164 ret = -EIO;
1165 goto done;
1166 }
1167 }
1168
1169 if (btmrvl_sdio_download_fw_w_helper(card)) {
1170 BT_ERR("Failed to download firmware!");
1171 ret = -EIO;
1172 goto done;
1173 }
Bing Zhao789221e2009-06-02 14:29:36 -07001174 }
1175
Andreas Fenkart69676b12013-04-22 11:10:23 +02001176 /*
1177 * winner or not, with this test the FW synchronizes when the
1178 * module can continue its initialization
1179 */
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001180 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
Bing Zhao789221e2009-06-02 14:29:36 -07001181 BT_ERR("FW failed to be active in time!");
Chin-Ran Lo86f7ac72015-12-29 04:26:33 -08001182 ret = -ETIMEDOUT;
1183 goto done;
Bing Zhao789221e2009-06-02 14:29:36 -07001184 }
1185
Aniket Nagarnaik4863e4c2015-08-06 03:43:09 -07001186 sdio_release_host(card->func);
1187
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001188 return 0;
1189
Bing Zhao789221e2009-06-02 14:29:36 -07001190done:
1191 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -07001192 return ret;
1193}
1194
1195static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
1196{
1197 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1198 int ret = 0;
1199
Bing Zhao789221e2009-06-02 14:29:36 -07001200 if (!card || !card->func) {
1201 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -07001202 return -EINVAL;
1203 }
1204
1205 sdio_claim_host(card->func);
1206
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001207 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -07001208
1209 sdio_release_host(card->func);
1210
1211 BT_DBG("wake up firmware");
1212
Bing Zhao789221e2009-06-02 14:29:36 -07001213 return ret;
1214}
1215
Xinming Hudc759612014-11-24 02:40:53 -08001216static void btmrvl_sdio_dump_regs(struct btmrvl_private *priv)
1217{
1218 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1219 int ret = 0;
1220 unsigned int reg, reg_start, reg_end;
1221 char buf[256], *ptr;
1222 u8 loop, func, data;
1223 int MAX_LOOP = 2;
1224
1225 btmrvl_sdio_wakeup_fw(priv);
1226 sdio_claim_host(card->func);
1227
1228 for (loop = 0; loop < MAX_LOOP; loop++) {
1229 memset(buf, 0, sizeof(buf));
1230 ptr = buf;
1231
1232 if (loop == 0) {
1233 /* Read the registers of SDIO function0 */
1234 func = loop;
1235 reg_start = 0;
1236 reg_end = 9;
1237 } else {
1238 func = 2;
1239 reg_start = 0;
1240 reg_end = 0x09;
1241 }
1242
1243 ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
1244 func, reg_start, reg_end);
1245 for (reg = reg_start; reg <= reg_end; reg++) {
1246 if (func == 0)
1247 data = sdio_f0_readb(card->func, reg, &ret);
1248 else
1249 data = sdio_readb(card->func, reg, &ret);
1250
1251 if (!ret) {
1252 ptr += sprintf(ptr, "%02x ", data);
1253 } else {
1254 ptr += sprintf(ptr, "ERR");
1255 break;
1256 }
1257 }
1258
1259 BT_INFO("%s", buf);
1260 }
1261
1262 sdio_release_host(card->func);
1263}
1264
1265/* This function read/write firmware */
1266static enum
1267rdwr_status btmrvl_sdio_rdwr_firmware(struct btmrvl_private *priv,
1268 u8 doneflag)
1269{
1270 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1271 int ret, tries;
1272 u8 ctrl_data = 0;
1273
1274 sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl,
1275 &ret);
1276
1277 if (ret) {
1278 BT_ERR("SDIO write err");
1279 return RDWR_STATUS_FAILURE;
1280 }
1281
1282 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1283 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
1284 &ret);
1285
1286 if (ret) {
1287 BT_ERR("SDIO read err");
1288 return RDWR_STATUS_FAILURE;
1289 }
1290
1291 if (ctrl_data == FW_DUMP_DONE)
1292 break;
1293 if (doneflag && ctrl_data == doneflag)
1294 return RDWR_STATUS_DONE;
1295 if (ctrl_data != FW_DUMP_HOST_READY) {
1296 BT_INFO("The ctrl reg was changed, re-try again!");
1297 sdio_writeb(card->func, FW_DUMP_HOST_READY,
1298 card->reg->fw_dump_ctrl, &ret);
1299 if (ret) {
1300 BT_ERR("SDIO write err");
1301 return RDWR_STATUS_FAILURE;
1302 }
1303 }
1304 usleep_range(100, 200);
1305 }
1306
1307 if (ctrl_data == FW_DUMP_HOST_READY) {
1308 BT_ERR("Fail to pull ctrl_data");
1309 return RDWR_STATUS_FAILURE;
1310 }
1311
1312 return RDWR_STATUS_SUCCESS;
1313}
1314
1315/* This function dump sdio register and memory data */
1316static void btmrvl_sdio_dump_firmware(struct btmrvl_private *priv)
1317{
1318 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1319 int ret = 0;
1320 unsigned int reg, reg_start, reg_end;
1321 enum rdwr_status stat;
1322 u8 *dbg_ptr, *end_ptr, *fw_dump_data, *fw_dump_ptr;
Xinming Hua1e85f042015-04-21 06:59:56 -07001323 u8 dump_num = 0, idx, i, read_reg, doneflag = 0;
Xinming Hudc759612014-11-24 02:40:53 -08001324 u32 memory_size, fw_dump_len = 0;
1325
1326 /* dump sdio register first */
1327 btmrvl_sdio_dump_regs(priv);
1328
1329 if (!card->supports_fw_dump) {
1330 BT_ERR("Firmware dump not supported for this card!");
1331 return;
1332 }
1333
1334 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
1335 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1336
1337 if (entry->mem_ptr) {
1338 vfree(entry->mem_ptr);
1339 entry->mem_ptr = NULL;
1340 }
1341 entry->mem_size = 0;
1342 }
1343
1344 btmrvl_sdio_wakeup_fw(priv);
1345 sdio_claim_host(card->func);
1346
1347 BT_INFO("== btmrvl firmware dump start ==");
1348
1349 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1350 if (stat == RDWR_STATUS_FAILURE)
1351 goto done;
1352
1353 reg = card->reg->fw_dump_start;
1354 /* Read the number of the memories which will dump */
1355 dump_num = sdio_readb(card->func, reg, &ret);
1356
1357 if (ret) {
1358 BT_ERR("SDIO read memory length err");
1359 goto done;
1360 }
1361
1362 /* Read the length of every memory which will dump */
1363 for (idx = 0; idx < dump_num; idx++) {
1364 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1365
1366 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1367 if (stat == RDWR_STATUS_FAILURE)
1368 goto done;
1369
1370 memory_size = 0;
1371 reg = card->reg->fw_dump_start;
1372 for (i = 0; i < 4; i++) {
1373 read_reg = sdio_readb(card->func, reg, &ret);
1374 if (ret) {
1375 BT_ERR("SDIO read err");
1376 goto done;
1377 }
1378 memory_size |= (read_reg << i*8);
1379 reg++;
1380 }
1381
1382 if (memory_size == 0) {
1383 BT_INFO("Firmware dump finished!");
Nachiket Kukade475b9ea2015-09-18 06:40:40 -07001384 sdio_writeb(card->func, FW_DUMP_READ_DONE,
1385 card->reg->fw_dump_ctrl, &ret);
1386 if (ret) {
1387 BT_ERR("SDIO Write MEMDUMP_FINISH ERR");
1388 goto done;
1389 }
Xinming Hudc759612014-11-24 02:40:53 -08001390 break;
1391 }
1392
1393 BT_INFO("%s_SIZE=0x%x", entry->mem_name, memory_size);
1394 entry->mem_ptr = vzalloc(memory_size + 1);
1395 entry->mem_size = memory_size;
1396 if (!entry->mem_ptr) {
1397 BT_ERR("Vzalloc %s failed", entry->mem_name);
1398 goto done;
1399 }
1400
1401 fw_dump_len += (strlen("========Start dump ") +
1402 strlen(entry->mem_name) +
1403 strlen("========\n") +
1404 (memory_size + 1) +
1405 strlen("\n========End dump========\n"));
1406
1407 dbg_ptr = entry->mem_ptr;
1408 end_ptr = dbg_ptr + memory_size;
1409
1410 doneflag = entry->done_flag;
1411 BT_INFO("Start %s output, please wait...",
1412 entry->mem_name);
1413
1414 do {
1415 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1416 if (stat == RDWR_STATUS_FAILURE)
1417 goto done;
1418
1419 reg_start = card->reg->fw_dump_start;
1420 reg_end = card->reg->fw_dump_end;
1421 for (reg = reg_start; reg <= reg_end; reg++) {
1422 *dbg_ptr = sdio_readb(card->func, reg, &ret);
1423 if (ret) {
1424 BT_ERR("SDIO read err");
1425 goto done;
1426 }
1427 if (dbg_ptr < end_ptr)
1428 dbg_ptr++;
1429 else
1430 BT_ERR("Allocated buffer not enough");
1431 }
1432
1433 if (stat != RDWR_STATUS_DONE) {
1434 continue;
1435 } else {
1436 BT_INFO("%s done: size=0x%tx",
1437 entry->mem_name,
1438 dbg_ptr - entry->mem_ptr);
1439 break;
1440 }
1441 } while (1);
1442 }
1443
1444 BT_INFO("== btmrvl firmware dump end ==");
1445
1446done:
1447 sdio_release_host(card->func);
1448
1449 if (fw_dump_len == 0)
1450 return;
1451
1452 fw_dump_data = vzalloc(fw_dump_len+1);
1453 if (!fw_dump_data) {
1454 BT_ERR("Vzalloc fw_dump_data fail!");
1455 return;
1456 }
1457 fw_dump_ptr = fw_dump_data;
1458
1459 /* Dump all the memory data into single file, a userspace script will
1460 be used to split all the memory data to multiple files*/
1461 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump start");
1462 for (idx = 0; idx < dump_num; idx++) {
1463 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1464
1465 if (entry->mem_ptr) {
1466 strcpy(fw_dump_ptr, "========Start dump ");
1467 fw_dump_ptr += strlen("========Start dump ");
1468
1469 strcpy(fw_dump_ptr, entry->mem_name);
1470 fw_dump_ptr += strlen(entry->mem_name);
1471
1472 strcpy(fw_dump_ptr, "========\n");
1473 fw_dump_ptr += strlen("========\n");
1474
1475 memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1476 fw_dump_ptr += entry->mem_size;
1477
1478 strcpy(fw_dump_ptr, "\n========End dump========\n");
1479 fw_dump_ptr += strlen("\n========End dump========\n");
1480
1481 vfree(mem_type_mapping_tbl[idx].mem_ptr);
1482 mem_type_mapping_tbl[idx].mem_ptr = NULL;
1483 }
1484 }
1485
1486 /* fw_dump_data will be free in device coredump release function
1487 after 5 min*/
Shengzhen Lid4902632015-08-19 03:12:19 -07001488 dev_coredumpv(&card->func->dev, fw_dump_data, fw_dump_len, GFP_KERNEL);
Xinming Hudc759612014-11-24 02:40:53 -08001489 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump end");
1490}
1491
Bing Zhao789221e2009-06-02 14:29:36 -07001492static int btmrvl_sdio_probe(struct sdio_func *func,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001493 const struct sdio_device_id *id)
Bing Zhao789221e2009-06-02 14:29:36 -07001494{
1495 int ret = 0;
1496 struct btmrvl_private *priv = NULL;
1497 struct btmrvl_sdio_card *card = NULL;
1498
Bing Zhao789221e2009-06-02 14:29:36 -07001499 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1500 id->vendor, id->device, id->class, func->num);
1501
Sachin Kamateb17ea32012-07-27 12:38:36 +05301502 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
1503 if (!card)
1504 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -07001505
1506 card->func = func;
1507
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001508 if (id->driver_data) {
1509 struct btmrvl_sdio_device *data = (void *) id->driver_data;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001510 card->helper = data->helper;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001511 card->firmware = data->firmware;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001512 card->reg = data->reg;
1513 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
Bing Zhao4df82b52014-03-31 14:41:44 -07001514 card->support_pscan_win_report = data->support_pscan_win_report;
Xinming Hudc759612014-11-24 02:40:53 -08001515 card->supports_fw_dump = data->supports_fw_dump;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001516 }
1517
Bing Zhao789221e2009-06-02 14:29:36 -07001518 if (btmrvl_sdio_register_dev(card) < 0) {
1519 BT_ERR("Failed to register BT device!");
Sachin Kamateb17ea32012-07-27 12:38:36 +05301520 return -ENODEV;
Bing Zhao789221e2009-06-02 14:29:36 -07001521 }
1522
1523 /* Disable the interrupts on the card */
1524 btmrvl_sdio_disable_host_int(card);
1525
1526 if (btmrvl_sdio_download_fw(card)) {
1527 BT_ERR("Downloading firmware failed!");
1528 ret = -ENODEV;
1529 goto unreg_dev;
1530 }
1531
Bing Zhao789221e2009-06-02 14:29:36 -07001532 btmrvl_sdio_enable_host_int(card);
1533
Xinming Hubb7f4f02016-04-26 06:57:27 -07001534 /* Device tree node parsing and platform specific configuration*/
1535 btmrvl_sdio_probe_of(&func->dev, card);
1536
Bing Zhao789221e2009-06-02 14:29:36 -07001537 priv = btmrvl_add_card(card);
1538 if (!priv) {
1539 BT_ERR("Initializing card failed!");
1540 ret = -ENODEV;
1541 goto disable_host_int;
1542 }
1543
1544 card->priv = priv;
1545
1546 /* Initialize the interface specific function pointers */
1547 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
1548 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
Amitkumar Karward1d10d72010-05-27 16:38:37 -07001549 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
Xinming Hudc759612014-11-24 02:40:53 -08001550 priv->firmware_dump = btmrvl_sdio_dump_firmware;
Bing Zhao789221e2009-06-02 14:29:36 -07001551
Bing Zhao64061602010-03-03 14:37:36 -08001552 if (btmrvl_register_hdev(priv)) {
1553 BT_ERR("Register hdev failed!");
1554 ret = -ENODEV;
1555 goto disable_host_int;
1556 }
1557
Bing Zhao789221e2009-06-02 14:29:36 -07001558 return 0;
1559
1560disable_host_int:
1561 btmrvl_sdio_disable_host_int(card);
1562unreg_dev:
1563 btmrvl_sdio_unregister_dev(card);
Bing Zhao789221e2009-06-02 14:29:36 -07001564 return ret;
1565}
1566
1567static void btmrvl_sdio_remove(struct sdio_func *func)
1568{
1569 struct btmrvl_sdio_card *card;
1570
Bing Zhao789221e2009-06-02 14:29:36 -07001571 if (func) {
1572 card = sdio_get_drvdata(func);
1573 if (card) {
1574 /* Send SHUTDOWN command & disable interrupt
1575 * if user removes the module.
1576 */
1577 if (user_rmmod) {
1578 btmrvl_send_module_cfg_cmd(card->priv,
1579 MODULE_SHUTDOWN_REQ);
1580 btmrvl_sdio_disable_host_int(card);
1581 }
Colin Ian King92322592017-03-21 12:20:28 +00001582 BT_DBG("unregister dev");
Amitkumar Karwar7b4b8742015-01-01 00:13:41 -08001583 card->priv->surprise_removed = true;
Bing Zhao789221e2009-06-02 14:29:36 -07001584 btmrvl_sdio_unregister_dev(card);
1585 btmrvl_remove_card(card->priv);
Bing Zhao789221e2009-06-02 14:29:36 -07001586 }
1587 }
Bing Zhao789221e2009-06-02 14:29:36 -07001588}
1589
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001590static int btmrvl_sdio_suspend(struct device *dev)
1591{
1592 struct sdio_func *func = dev_to_sdio_func(dev);
1593 struct btmrvl_sdio_card *card;
1594 struct btmrvl_private *priv;
1595 mmc_pm_flag_t pm_flags;
1596 struct hci_dev *hcidev;
1597
1598 if (func) {
1599 pm_flags = sdio_get_host_pm_caps(func);
1600 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1601 pm_flags);
1602 if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1603 BT_ERR("%s: cannot remain alive while suspended",
1604 sdio_func_id(func));
1605 return -ENOSYS;
1606 }
1607 card = sdio_get_drvdata(func);
1608 if (!card || !card->priv) {
1609 BT_ERR("card or priv structure is not valid");
1610 return 0;
1611 }
1612 } else {
1613 BT_ERR("sdio_func is not specified");
1614 return 0;
1615 }
1616
Xinming Hubb7f4f02016-04-26 06:57:27 -07001617 /* Enable platform specific wakeup interrupt */
1618 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_bt >= 0) {
1619 card->plt_wake_cfg->wake_by_bt = false;
1620 enable_irq(card->plt_wake_cfg->irq_bt);
1621 enable_irq_wake(card->plt_wake_cfg->irq_bt);
1622 }
1623
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001624 priv = card->priv;
Chin-Ran Lo8cf60cf2016-01-06 06:34:38 -08001625 priv->adapter->is_suspending = true;
Chin-Ran Lo5bf8a742014-07-14 21:05:37 -07001626 hcidev = priv->btmrvl_dev.hcidev;
1627 BT_DBG("%s: SDIO suspend", hcidev->name);
1628 hci_suspend_dev(hcidev);
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001629
1630 if (priv->adapter->hs_state != HS_ACTIVATED) {
1631 if (btmrvl_enable_hs(priv)) {
Colin Ian King8f915662017-02-18 22:52:55 +00001632 BT_ERR("HS not activated, suspend failed!");
Amitkumar Karware7acf432016-07-15 16:20:52 +05301633 priv->adapter->is_suspending = false;
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001634 return -EBUSY;
1635 }
1636 }
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001637
Chin-Ran Lo8cf60cf2016-01-06 06:34:38 -08001638 priv->adapter->is_suspending = false;
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001639 priv->adapter->is_suspended = true;
1640
1641 /* We will keep the power when hs enabled successfully */
1642 if (priv->adapter->hs_state == HS_ACTIVATED) {
1643 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1644 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1645 } else {
1646 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1647 return 0;
1648 }
1649}
1650
1651static int btmrvl_sdio_resume(struct device *dev)
1652{
1653 struct sdio_func *func = dev_to_sdio_func(dev);
1654 struct btmrvl_sdio_card *card;
1655 struct btmrvl_private *priv;
1656 mmc_pm_flag_t pm_flags;
1657 struct hci_dev *hcidev;
1658
1659 if (func) {
1660 pm_flags = sdio_get_host_pm_caps(func);
1661 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1662 pm_flags);
1663 card = sdio_get_drvdata(func);
1664 if (!card || !card->priv) {
1665 BT_ERR("card or priv structure is not valid");
1666 return 0;
1667 }
1668 } else {
1669 BT_ERR("sdio_func is not specified");
1670 return 0;
1671 }
1672 priv = card->priv;
1673
1674 if (!priv->adapter->is_suspended) {
1675 BT_DBG("device already resumed");
1676 return 0;
1677 }
1678
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001679 priv->hw_wakeup_firmware(priv);
1680 priv->adapter->hs_state = HS_DEACTIVATED;
Chin-Ran Lo5bf8a742014-07-14 21:05:37 -07001681 hcidev = priv->btmrvl_dev.hcidev;
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001682 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
Chin-Ran Lo5bf8a742014-07-14 21:05:37 -07001683 priv->adapter->is_suspended = false;
1684 BT_DBG("%s: SDIO resume", hcidev->name);
1685 hci_resume_dev(hcidev);
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001686
Xinming Hubb7f4f02016-04-26 06:57:27 -07001687 /* Disable platform specific wakeup interrupt */
1688 if (card->plt_wake_cfg && card->plt_wake_cfg->irq_bt >= 0) {
1689 disable_irq_wake(card->plt_wake_cfg->irq_bt);
Jeffy Chen9af02d82017-01-23 12:18:51 +08001690 disable_irq(card->plt_wake_cfg->irq_bt);
1691 if (card->plt_wake_cfg->wake_by_bt)
1692 /* Undo our disable, since interrupt handler already
1693 * did this.
1694 */
1695 enable_irq(card->plt_wake_cfg->irq_bt);
Xinming Hubb7f4f02016-04-26 06:57:27 -07001696 }
1697
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001698 return 0;
1699}
1700
1701static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1702 .suspend = btmrvl_sdio_suspend,
1703 .resume = btmrvl_sdio_resume,
1704};
1705
Bing Zhao789221e2009-06-02 14:29:36 -07001706static struct sdio_driver bt_mrvl_sdio = {
1707 .name = "btmrvl_sdio",
1708 .id_table = btmrvl_sdio_ids,
1709 .probe = btmrvl_sdio_probe,
1710 .remove = btmrvl_sdio_remove,
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001711 .drv = {
1712 .owner = THIS_MODULE,
1713 .pm = &btmrvl_sdio_pm_ops,
1714 }
Bing Zhao789221e2009-06-02 14:29:36 -07001715};
1716
Peter Huewe8e5b2302009-12-22 09:34:20 +01001717static int __init btmrvl_sdio_init_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001718{
Bing Zhao789221e2009-06-02 14:29:36 -07001719 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1720 BT_ERR("SDIO Driver Registration Failed");
Bing Zhao789221e2009-06-02 14:29:36 -07001721 return -ENODEV;
1722 }
1723
1724 /* Clear the flag in case user removes the card. */
1725 user_rmmod = 0;
1726
Bing Zhao789221e2009-06-02 14:29:36 -07001727 return 0;
1728}
1729
Peter Huewe8e5b2302009-12-22 09:34:20 +01001730static void __exit btmrvl_sdio_exit_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001731{
Bing Zhao789221e2009-06-02 14:29:36 -07001732 /* Set the flag as user is removing this module. */
1733 user_rmmod = 1;
1734
1735 sdio_unregister_driver(&bt_mrvl_sdio);
Bing Zhao789221e2009-06-02 14:29:36 -07001736}
1737
1738module_init(btmrvl_sdio_init_module);
1739module_exit(btmrvl_sdio_exit_module);
1740
1741MODULE_AUTHOR("Marvell International Ltd.");
Marcel Holtmann9666fb32009-06-09 21:45:04 +02001742MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
Bing Zhao789221e2009-06-02 14:29:36 -07001743MODULE_VERSION(VERSION);
1744MODULE_LICENSE("GPL v2");
Lubomir Rintel77189df2013-03-13 12:04:30 +01001745MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1746MODULE_FIRMWARE("mrvl/sd8688.bin");
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001747MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
Bing Zhao2ac654f2011-11-16 20:40:42 -08001748MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
Xinming Hu3907d552014-09-30 06:45:33 -04001749MODULE_FIRMWARE("mrvl/sd8887_uapsta.bin");
Bing Zhao22f2efe2013-05-13 18:15:32 -07001750MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");
Amitkumar Karwarf0ef6742015-09-21 03:06:42 -07001751MODULE_FIRMWARE("mrvl/sd8997_uapsta.bin");