blob: 01d6da577eeb0713127f57c264e6345b5055781d [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>
Bing Zhao789221e2009-06-02 14:29:36 -070023
24#include <linux/mmc/sdio_ids.h>
25#include <linux/mmc/sdio_func.h>
Paul Gortmakera59b9682011-08-29 16:44:23 -040026#include <linux/module.h>
Xinming Hudc759612014-11-24 02:40:53 -080027#include <linux/devcoredump.h>
Bing Zhao789221e2009-06-02 14:29:36 -070028
29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
31
32#include "btmrvl_drv.h"
33#include "btmrvl_sdio.h"
34
35#define VERSION "1.0"
36
Xinming Hudc759612014-11-24 02:40:53 -080037static struct memory_type_mapping mem_type_mapping_tbl[] = {
38 {"ITCM", NULL, 0, 0xF0},
39 {"DTCM", NULL, 0, 0xF1},
40 {"SQRAM", NULL, 0, 0xF2},
41 {"APU", NULL, 0, 0xF3},
42 {"CIU", NULL, 0, 0xF4},
43 {"ICU", NULL, 0, 0xF5},
44 {"MAC", NULL, 0, 0xF6},
45 {"EXT7", NULL, 0, 0xF7},
46 {"EXT8", NULL, 0, 0xF8},
47 {"EXT9", NULL, 0, 0xF9},
48 {"EXT10", NULL, 0, 0xFA},
49 {"EXT11", NULL, 0, 0xFB},
50 {"EXT12", NULL, 0, 0xFC},
51 {"EXT13", NULL, 0, 0xFD},
52 {"EXTLAST", NULL, 0, 0xFE},
53};
54
Bing Zhao789221e2009-06-02 14:29:36 -070055/* The btmrvl_sdio_remove() callback function is called
56 * when user removes this module from kernel space or ejects
57 * the card from the slot. The driver handles these 2 cases
58 * differently.
59 * If the user is removing the module, a MODULE_SHUTDOWN_REQ
60 * command is sent to firmware and interrupt will be disabled.
61 * If the card is removed, there is no need to send command
62 * or disable interrupt.
63 *
64 * The variable 'user_rmmod' is used to distinguish these two
65 * scenarios. This flag is initialized as FALSE in case the card
66 * is removed, and will be set to TRUE for module removal when
67 * module_exit function is called.
68 */
69static u8 user_rmmod;
Amitkumar Karward1d10d72010-05-27 16:38:37 -070070static u8 sdio_ireg;
Bing Zhao789221e2009-06-02 14:29:36 -070071
Kevin Gan9f72c1d2011-04-08 18:19:33 -070072static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
73 .cfg = 0x03,
74 .host_int_mask = 0x04,
75 .host_intstatus = 0x05,
76 .card_status = 0x20,
77 .sq_read_base_addr_a0 = 0x10,
78 .sq_read_base_addr_a1 = 0x11,
79 .card_fw_status0 = 0x40,
80 .card_fw_status1 = 0x41,
81 .card_rx_len = 0x42,
82 .card_rx_unit = 0x43,
83 .io_port_0 = 0x00,
84 .io_port_1 = 0x01,
85 .io_port_2 = 0x02,
Bing Zhao0d367402014-03-28 14:54:39 -070086 .int_read_to_clear = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -070087};
Bing Zhao2ac654f2011-11-16 20:40:42 -080088static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = {
Kevin Gan9f72c1d2011-04-08 18:19:33 -070089 .cfg = 0x00,
90 .host_int_mask = 0x02,
91 .host_intstatus = 0x03,
92 .card_status = 0x30,
93 .sq_read_base_addr_a0 = 0x40,
94 .sq_read_base_addr_a1 = 0x41,
95 .card_revision = 0x5c,
96 .card_fw_status0 = 0x60,
97 .card_fw_status1 = 0x61,
98 .card_rx_len = 0x62,
99 .card_rx_unit = 0x63,
100 .io_port_0 = 0x78,
101 .io_port_1 = 0x79,
102 .io_port_2 = 0x7a,
Bing Zhao0d367402014-03-28 14:54:39 -0700103 .int_read_to_clear = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700104};
105
Xinming Hu3907d552014-09-30 06:45:33 -0400106static const struct btmrvl_sdio_card_reg btmrvl_reg_8887 = {
107 .cfg = 0x00,
108 .host_int_mask = 0x08,
109 .host_intstatus = 0x0C,
110 .card_status = 0x5C,
111 .sq_read_base_addr_a0 = 0x6C,
112 .sq_read_base_addr_a1 = 0x6D,
113 .card_revision = 0xC8,
114 .card_fw_status0 = 0x88,
115 .card_fw_status1 = 0x89,
116 .card_rx_len = 0x8A,
117 .card_rx_unit = 0x8B,
118 .io_port_0 = 0xE4,
119 .io_port_1 = 0xE5,
120 .io_port_2 = 0xE6,
121 .int_read_to_clear = true,
122 .host_int_rsr = 0x04,
123 .card_misc_cfg = 0xD8,
124};
125
Amitkumar Karwar1e3e4922014-09-30 06:45:32 -0400126static const struct btmrvl_sdio_card_reg btmrvl_reg_8897 = {
Bing Zhao22f2efe2013-05-13 18:15:32 -0700127 .cfg = 0x00,
128 .host_int_mask = 0x02,
129 .host_intstatus = 0x03,
130 .card_status = 0x50,
131 .sq_read_base_addr_a0 = 0x60,
132 .sq_read_base_addr_a1 = 0x61,
133 .card_revision = 0xbc,
134 .card_fw_status0 = 0xc0,
135 .card_fw_status1 = 0xc1,
136 .card_rx_len = 0xc2,
137 .card_rx_unit = 0xc3,
138 .io_port_0 = 0xd8,
139 .io_port_1 = 0xd9,
140 .io_port_2 = 0xda,
Bing Zhao0d367402014-03-28 14:54:39 -0700141 .int_read_to_clear = true,
142 .host_int_rsr = 0x01,
143 .card_misc_cfg = 0xcc,
Xinming Hudc759612014-11-24 02:40:53 -0800144 .fw_dump_ctrl = 0xe2,
145 .fw_dump_start = 0xe3,
146 .fw_dump_end = 0xea,
Bing Zhao22f2efe2013-05-13 18:15:32 -0700147};
148
Paul Bollec6757862011-10-05 14:56:46 +0200149static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
Lubomir Rintel77189df2013-03-13 12:04:30 +0100150 .helper = "mrvl/sd8688_helper.bin",
151 .firmware = "mrvl/sd8688.bin",
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700152 .reg = &btmrvl_reg_8688,
Bing Zhao4df82b52014-03-31 14:41:44 -0700153 .support_pscan_win_report = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700154 .sd_blksz_fw_dl = 64,
Xinming Hudc759612014-11-24 02:40:53 -0800155 .supports_fw_dump = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700156};
157
158static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
159 .helper = NULL,
160 .firmware = "mrvl/sd8787_uapsta.bin",
Bing Zhao2ac654f2011-11-16 20:40:42 -0800161 .reg = &btmrvl_reg_87xx,
Bing Zhao4df82b52014-03-31 14:41:44 -0700162 .support_pscan_win_report = false,
Bing Zhao2ac654f2011-11-16 20:40:42 -0800163 .sd_blksz_fw_dl = 256,
Xinming Hudc759612014-11-24 02:40:53 -0800164 .supports_fw_dump = false,
Bing Zhao2ac654f2011-11-16 20:40:42 -0800165};
166
167static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
168 .helper = NULL,
169 .firmware = "mrvl/sd8797_uapsta.bin",
170 .reg = &btmrvl_reg_87xx,
Bing Zhao4df82b52014-03-31 14:41:44 -0700171 .support_pscan_win_report = false,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700172 .sd_blksz_fw_dl = 256,
Xinming Hudc759612014-11-24 02:40:53 -0800173 .supports_fw_dump = false,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200174};
175
Xinming Hu3907d552014-09-30 06:45:33 -0400176static const struct btmrvl_sdio_device btmrvl_sdio_sd8887 = {
177 .helper = NULL,
178 .firmware = "mrvl/sd8887_uapsta.bin",
179 .reg = &btmrvl_reg_8887,
180 .support_pscan_win_report = true,
181 .sd_blksz_fw_dl = 256,
Xinming Hudc759612014-11-24 02:40:53 -0800182 .supports_fw_dump = false,
Xinming Hu3907d552014-09-30 06:45:33 -0400183};
184
Bing Zhao22f2efe2013-05-13 18:15:32 -0700185static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = {
186 .helper = NULL,
187 .firmware = "mrvl/sd8897_uapsta.bin",
Amitkumar Karwar1e3e4922014-09-30 06:45:32 -0400188 .reg = &btmrvl_reg_8897,
Bing Zhao4df82b52014-03-31 14:41:44 -0700189 .support_pscan_win_report = true,
Bing Zhao22f2efe2013-05-13 18:15:32 -0700190 .sd_blksz_fw_dl = 256,
Xinming Hudc759612014-11-24 02:40:53 -0800191 .supports_fw_dump = true,
Bing Zhao22f2efe2013-05-13 18:15:32 -0700192};
193
Bing Zhao789221e2009-06-02 14:29:36 -0700194static const struct sdio_device_id btmrvl_sdio_ids[] = {
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200195 /* Marvell SD8688 Bluetooth device */
196 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
Paul Bollec6757862011-10-05 14:56:46 +0200197 .driver_data = (unsigned long) &btmrvl_sdio_sd8688 },
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700198 /* Marvell SD8787 Bluetooth device */
199 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
200 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
Bing Zhao37ed5612012-05-23 18:50:04 -0700201 /* Marvell SD8787 Bluetooth AMP device */
202 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911B),
203 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
Bing Zhao2ac654f2011-11-16 20:40:42 -0800204 /* Marvell SD8797 Bluetooth device */
205 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A),
206 .driver_data = (unsigned long) &btmrvl_sdio_sd8797 },
Xinming Hu3907d552014-09-30 06:45:33 -0400207 /* Marvell SD8887 Bluetooth device */
208 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9136),
209 .driver_data = (unsigned long)&btmrvl_sdio_sd8887 },
Bing Zhao22f2efe2013-05-13 18:15:32 -0700210 /* Marvell SD8897 Bluetooth device */
211 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912E),
212 .driver_data = (unsigned long) &btmrvl_sdio_sd8897 },
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200213
214 { } /* Terminating entry */
Bing Zhao789221e2009-06-02 14:29:36 -0700215};
216
217MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
218
Bing Zhao789221e2009-06-02 14:29:36 -0700219static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
220{
221 u8 reg;
222 int ret;
223
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700224 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700225 if (!ret)
226 card->rx_unit = reg;
227
Bing Zhao789221e2009-06-02 14:29:36 -0700228 return ret;
229}
230
231static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
232{
Bing Zhao789221e2009-06-02 14:29:36 -0700233 u8 fws0, fws1;
Marcel Holtmann93742532009-06-13 07:40:18 +0200234 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700235
236 *dat = 0;
237
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700238 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Kulikov Vasiliy278be272010-07-05 12:01:22 +0400239 if (ret)
240 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700241
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700242 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200243 if (ret)
Bing Zhao789221e2009-06-02 14:29:36 -0700244 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700245
246 *dat = (((u16) fws1) << 8) | fws0;
247
Bing Zhao789221e2009-06-02 14:29:36 -0700248 return 0;
249}
250
251static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
252{
Bing Zhao789221e2009-06-02 14:29:36 -0700253 u8 reg;
Marcel Holtmann93742532009-06-13 07:40:18 +0200254 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700255
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700256 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700257 if (!ret)
258 *dat = (u16) reg << card->rx_unit;
259
Bing Zhao789221e2009-06-02 14:29:36 -0700260 return ret;
261}
262
263static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200264 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700265{
266 int ret;
267
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700268 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700269 if (ret) {
270 BT_ERR("Unable to enable the host interrupt!");
271 ret = -EIO;
272 }
273
Bing Zhao789221e2009-06-02 14:29:36 -0700274 return ret;
275}
276
277static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200278 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700279{
Bing Zhao789221e2009-06-02 14:29:36 -0700280 u8 host_int_mask;
Marcel Holtmann93742532009-06-13 07:40:18 +0200281 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700282
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700283 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200284 if (ret)
285 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700286
287 host_int_mask &= ~mask;
288
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700289 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700290 if (ret < 0) {
291 BT_ERR("Unable to disable the host interrupt!");
Marcel Holtmann93742532009-06-13 07:40:18 +0200292 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700293 }
294
Marcel Holtmann93742532009-06-13 07:40:18 +0200295 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700296}
297
298static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
299{
300 unsigned int tries;
Bing Zhao789221e2009-06-02 14:29:36 -0700301 u8 status;
Marcel Holtmann93742532009-06-13 07:40:18 +0200302 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700303
304 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700305 status = sdio_readb(card->func, card->reg->card_status, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700306 if (ret)
307 goto failed;
308 if ((status & bits) == bits)
Marcel Holtmann93742532009-06-13 07:40:18 +0200309 return ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700310
311 udelay(1);
312 }
313
314 ret = -ETIMEDOUT;
315
316failed:
317 BT_ERR("FAILED! ret=%d", ret);
318
Bing Zhao789221e2009-06-02 14:29:36 -0700319 return ret;
320}
321
322static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200323 int pollnum)
Bing Zhao789221e2009-06-02 14:29:36 -0700324{
Bing Zhao789221e2009-06-02 14:29:36 -0700325 u16 firmwarestat;
Andreas Fenkart69676b12013-04-22 11:10:23 +0200326 int tries, ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700327
Bing Zhao789221e2009-06-02 14:29:36 -0700328 /* Wait for firmware to become ready */
329 for (tries = 0; tries < pollnum; tries++) {
Andreas Fenkartc1c999e2013-04-22 11:10:22 +0200330 sdio_claim_host(card->func);
331 ret = btmrvl_sdio_read_fw_status(card, &firmwarestat);
332 sdio_release_host(card->func);
333 if (ret < 0)
Bing Zhao789221e2009-06-02 14:29:36 -0700334 continue;
335
Andreas Fenkart69676b12013-04-22 11:10:23 +0200336 if (firmwarestat == FIRMWARE_READY)
337 return 0;
338
339 msleep(10);
Bing Zhao789221e2009-06-02 14:29:36 -0700340 }
341
Andreas Fenkart69676b12013-04-22 11:10:23 +0200342 return -ETIMEDOUT;
Bing Zhao789221e2009-06-02 14:29:36 -0700343}
344
345static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
346{
347 const struct firmware *fw_helper = NULL;
348 const u8 *helper = NULL;
349 int ret;
350 void *tmphlprbuf = NULL;
351 int tmphlprbufsz, hlprblknow, helperlen;
352 u8 *helperbuf;
353 u32 tx_len;
354
Bing Zhao789221e2009-06-02 14:29:36 -0700355 ret = request_firmware(&fw_helper, card->helper,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200356 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700357 if ((ret < 0) || !fw_helper) {
358 BT_ERR("request_firmware(helper) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200359 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700360 ret = -ENOENT;
361 goto done;
362 }
363
364 helper = fw_helper->data;
365 helperlen = fw_helper->size;
366
367 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200368 helperlen, SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700369
370 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
371
Julia Lawallf8df39f2010-05-13 22:02:03 +0200372 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700373 if (!tmphlprbuf) {
374 BT_ERR("Unable to allocate buffer for helper."
375 " Terminating download");
376 ret = -ENOMEM;
377 goto done;
378 }
379
Bing Zhao789221e2009-06-02 14:29:36 -0700380 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
381
382 /* Perform helper data transfer */
383 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
384 - SDIO_HEADER_LEN;
385 hlprblknow = 0;
386
387 do {
388 ret = btmrvl_sdio_poll_card_status(card,
389 CARD_IO_READY | DN_LD_CARD_RDY);
390 if (ret < 0) {
391 BT_ERR("Helper download poll status timeout @ %d",
392 hlprblknow);
393 goto done;
394 }
395
396 /* Check if there is more data? */
397 if (hlprblknow >= helperlen)
398 break;
399
400 if (helperlen - hlprblknow < tx_len)
401 tx_len = helperlen - hlprblknow;
402
403 /* Little-endian */
404 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
405 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
406 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
407 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
408
409 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
410 tx_len);
411
412 /* Now send the data */
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200413 ret = sdio_writesb(card->func, card->ioport, helperbuf,
414 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700415 if (ret < 0) {
416 BT_ERR("IO error during helper download @ %d",
417 hlprblknow);
418 goto done;
419 }
420
421 hlprblknow += tx_len;
422 } while (true);
423
424 BT_DBG("Transferring helper image EOF block");
425
426 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
427
428 ret = sdio_writesb(card->func, card->ioport, helperbuf,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200429 SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700430 if (ret < 0) {
431 BT_ERR("IO error in writing helper image EOF block");
432 goto done;
433 }
434
435 ret = 0;
436
437done:
438 kfree(tmphlprbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200439 release_firmware(fw_helper);
Bing Zhao789221e2009-06-02 14:29:36 -0700440 return ret;
441}
442
443static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
444{
445 const struct firmware *fw_firmware = NULL;
446 const u8 *firmware = NULL;
447 int firmwarelen, tmpfwbufsz, ret;
448 unsigned int tries, offset;
449 u8 base0, base1;
450 void *tmpfwbuf = NULL;
451 u8 *fwbuf;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700452 u16 len, blksz_dl = card->sd_blksz_fw_dl;
Bing Zhao789221e2009-06-02 14:29:36 -0700453 int txlen = 0, tx_blocks = 0, count = 0;
454
Bing Zhao789221e2009-06-02 14:29:36 -0700455 ret = request_firmware(&fw_firmware, card->firmware,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200456 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700457 if ((ret < 0) || !fw_firmware) {
458 BT_ERR("request_firmware(firmware) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200459 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700460 ret = -ENOENT;
461 goto done;
462 }
463
464 firmware = fw_firmware->data;
465 firmwarelen = fw_firmware->size;
466
467 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
468
469 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
Julia Lawallf8df39f2010-05-13 22:02:03 +0200470 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700471 if (!tmpfwbuf) {
472 BT_ERR("Unable to allocate buffer for firmware."
473 " Terminating download");
474 ret = -ENOMEM;
475 goto done;
476 }
477
Bing Zhao789221e2009-06-02 14:29:36 -0700478 /* Ensure aligned firmware buffer */
479 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
480
481 /* Perform firmware data transfer */
482 offset = 0;
483 do {
484 ret = btmrvl_sdio_poll_card_status(card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200485 CARD_IO_READY | DN_LD_CARD_RDY);
Bing Zhao789221e2009-06-02 14:29:36 -0700486 if (ret < 0) {
487 BT_ERR("FW download with helper poll status"
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200488 " timeout @ %d", offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700489 goto done;
490 }
491
492 /* Check if there is more data ? */
493 if (offset >= firmwarelen)
494 break;
495
496 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
497 base0 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700498 card->reg->sq_read_base_addr_a0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700499 if (ret) {
500 BT_ERR("BASE0 register read failed:"
501 " base0 = 0x%04X(%d)."
502 " Terminating download",
503 base0, base0);
504 ret = -EIO;
505 goto done;
506 }
507 base1 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700508 card->reg->sq_read_base_addr_a1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700509 if (ret) {
510 BT_ERR("BASE1 register read failed:"
511 " base1 = 0x%04X(%d)."
512 " Terminating download",
513 base1, base1);
514 ret = -EIO;
515 goto done;
516 }
517
518 len = (((u16) base1) << 8) | base0;
519 if (len)
520 break;
521
522 udelay(10);
523 }
524
525 if (!len)
526 break;
527 else if (len > BTM_UPLD_SIZE) {
528 BT_ERR("FW download failure @%d, invalid length %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200529 offset, len);
Bing Zhao789221e2009-06-02 14:29:36 -0700530 ret = -EINVAL;
531 goto done;
532 }
533
534 txlen = len;
535
536 if (len & BIT(0)) {
537 count++;
538 if (count > MAX_WRITE_IOMEM_RETRY) {
539 BT_ERR("FW download failure @%d, "
540 "over max retry count", offset);
541 ret = -EIO;
542 goto done;
543 }
544 BT_ERR("FW CRC error indicated by the helper: "
545 "len = 0x%04X, txlen = %d", len, txlen);
546 len &= ~BIT(0);
547 /* Set txlen to 0 so as to resend from same offset */
548 txlen = 0;
549 } else {
550 count = 0;
551
552 /* Last block ? */
553 if (firmwarelen - offset < txlen)
554 txlen = firmwarelen - offset;
555
Andy Shevchenkoba34dd32013-08-02 14:10:12 +0300556 tx_blocks = DIV_ROUND_UP(txlen, blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700557
558 memcpy(fwbuf, &firmware[offset], txlen);
559 }
560
561 ret = sdio_writesb(card->func, card->ioport, fwbuf,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700562 tx_blocks * blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700563
564 if (ret < 0) {
565 BT_ERR("FW download, writesb(%d) failed @%d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200566 count, offset);
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700567 sdio_writeb(card->func, HOST_CMD53_FIN,
568 card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700569 if (ret)
570 BT_ERR("writeb failed (CFG)");
571 }
572
573 offset += txlen;
574 } while (true);
575
Amitkumar Karwar1c6098e2015-01-01 00:13:42 -0800576 BT_INFO("FW download over, size %d bytes", offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700577
578 ret = 0;
579
580done:
581 kfree(tmpfwbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200582 release_firmware(fw_firmware);
Bing Zhao789221e2009-06-02 14:29:36 -0700583 return ret;
584}
585
586static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
587{
588 u16 buf_len = 0;
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300589 int ret, num_blocks, blksz;
Bing Zhao789221e2009-06-02 14:29:36 -0700590 struct sk_buff *skb = NULL;
591 u32 type;
592 u8 *payload = NULL;
593 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
594 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
595
Bing Zhao789221e2009-06-02 14:29:36 -0700596 if (!card || !card->func) {
597 BT_ERR("card or function is NULL!");
598 ret = -EINVAL;
599 goto exit;
600 }
601
602 /* Read the length of data to be transferred */
603 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
604 if (ret < 0) {
605 BT_ERR("read rx_len failed");
606 ret = -EIO;
607 goto exit;
608 }
609
610 blksz = SDIO_BLOCK_SIZE;
Andrei Emeltchenkoe678bad2012-09-28 14:36:09 +0300611 num_blocks = DIV_ROUND_UP(buf_len, blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700612
613 if (buf_len <= SDIO_HEADER_LEN
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300614 || (num_blocks * blksz) > ALLOC_BUF_SIZE) {
Bing Zhao789221e2009-06-02 14:29:36 -0700615 BT_ERR("invalid packet length: %d", buf_len);
616 ret = -EINVAL;
617 goto exit;
618 }
619
620 /* Allocate buffer */
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300621 skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_ATOMIC);
Bing Zhao789221e2009-06-02 14:29:36 -0700622 if (skb == NULL) {
623 BT_ERR("No free skb");
Wei Yongjun6ea81c42013-06-05 10:16:55 +0800624 ret = -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700625 goto exit;
626 }
627
Bing Zhao3318b232009-07-08 11:44:14 -0700628 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
629 skb_put(skb, (unsigned long) skb->data &
630 (BTSDIO_DMA_ALIGN - 1));
631 skb_pull(skb, (unsigned long) skb->data &
632 (BTSDIO_DMA_ALIGN - 1));
Bing Zhao789221e2009-06-02 14:29:36 -0700633 }
634
Bing Zhao3318b232009-07-08 11:44:14 -0700635 payload = skb->data;
Bing Zhao789221e2009-06-02 14:29:36 -0700636
637 ret = sdio_readsb(card->func, payload, card->ioport,
Andrei Emeltchenko42632802012-09-28 14:36:08 +0300638 num_blocks * blksz);
Bing Zhao789221e2009-06-02 14:29:36 -0700639 if (ret < 0) {
640 BT_ERR("readsb failed: %d", ret);
641 ret = -EIO;
642 goto exit;
643 }
644
645 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
646 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
647 */
648
649 buf_len = payload[0];
Andrei Emeltchenko9cb23dd2012-09-28 14:36:10 +0300650 buf_len |= payload[1] << 8;
651 buf_len |= payload[2] << 16;
652
653 if (buf_len > blksz * num_blocks) {
654 BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
655 buf_len, blksz * num_blocks);
656 ret = -EIO;
657 goto exit;
658 }
659
Bing Zhao789221e2009-06-02 14:29:36 -0700660 type = payload[3];
661
662 switch (type) {
663 case HCI_ACLDATA_PKT:
664 case HCI_SCODATA_PKT:
665 case HCI_EVENT_PKT:
666 bt_cb(skb)->pkt_type = type;
Bing Zhao789221e2009-06-02 14:29:36 -0700667 skb_put(skb, buf_len);
668 skb_pull(skb, SDIO_HEADER_LEN);
669
Andrei Emeltchenkoea1e76a2012-06-13 13:35:44 +0300670 if (type == HCI_EVENT_PKT) {
671 if (btmrvl_check_evtpkt(priv, skb))
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700672 hci_recv_frame(hdev, skb);
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +0300673 } else {
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700674 hci_recv_frame(hdev, skb);
Andrei Emeltchenko2c2453f2012-07-09 13:57:18 +0300675 }
Bing Zhao789221e2009-06-02 14:29:36 -0700676
Bing Zhao789221e2009-06-02 14:29:36 -0700677 hdev->stat.byte_rx += buf_len;
678 break;
679
680 case MRVL_VENDOR_PKT:
681 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
Bing Zhao789221e2009-06-02 14:29:36 -0700682 skb_put(skb, buf_len);
683 skb_pull(skb, SDIO_HEADER_LEN);
684
685 if (btmrvl_process_event(priv, skb))
Marcel Holtmanne1a26172013-10-10 16:52:43 -0700686 hci_recv_frame(hdev, skb);
Bing Zhao789221e2009-06-02 14:29:36 -0700687
688 hdev->stat.byte_rx += buf_len;
689 break;
690
691 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200692 BT_ERR("Unknown packet type:%d", type);
Andrei Emeltchenko78c1b8e2012-10-10 17:41:33 +0300693 BT_ERR("hex: %*ph", blksz * num_blocks, payload);
Bing Zhao789221e2009-06-02 14:29:36 -0700694
695 kfree_skb(skb);
696 skb = NULL;
697 break;
698 }
699
700exit:
701 if (ret) {
702 hdev->stat.err_rx++;
Wei Yongjun79232962012-08-28 21:12:48 +0800703 kfree_skb(skb);
Bing Zhao789221e2009-06-02 14:29:36 -0700704 }
705
Bing Zhao789221e2009-06-02 14:29:36 -0700706 return ret;
707}
708
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700709static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
Bing Zhao789221e2009-06-02 14:29:36 -0700710{
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700711 ulong flags;
712 u8 ireg;
Bing Zhao789221e2009-06-02 14:29:36 -0700713 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
714
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700715 spin_lock_irqsave(&priv->driver_lock, flags);
716 ireg = sdio_ireg;
717 sdio_ireg = 0;
718 spin_unlock_irqrestore(&priv->driver_lock, flags);
Bing Zhao789221e2009-06-02 14:29:36 -0700719
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700720 sdio_claim_host(card->func);
721 if (ireg & DN_LD_HOST_INT_STATUS) {
Bing Zhao789221e2009-06-02 14:29:36 -0700722 if (priv->btmrvl_dev.tx_dnld_rdy)
723 BT_DBG("tx_done already received: "
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700724 " int_status=0x%x", ireg);
Bing Zhao789221e2009-06-02 14:29:36 -0700725 else
726 priv->btmrvl_dev.tx_dnld_rdy = true;
727 }
728
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700729 if (ireg & UP_LD_HOST_INT_STATUS)
Bing Zhao789221e2009-06-02 14:29:36 -0700730 btmrvl_sdio_card_to_host(priv);
731
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700732 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -0700733
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700734 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700735}
736
Bing Zhao0d367402014-03-28 14:54:39 -0700737static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
738{
739 struct btmrvl_adapter *adapter = card->priv->adapter;
740 int ret;
741
742 ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE);
743 if (ret) {
744 BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret);
745 return ret;
746 }
747
748 *ireg = adapter->hw_regs[card->reg->host_intstatus];
749 BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg);
750
751 return 0;
752}
753
Bing Zhao8af48402014-03-27 21:30:42 -0700754static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
755{
756 int ret;
757
758 *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
759 if (ret) {
760 BT_ERR("sdio_readb: read int status failed: %d", ret);
761 return ret;
762 }
763
764 if (*ireg) {
765 /*
766 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
767 * Clear the interrupt status register and re-enable the
768 * interrupt.
769 */
770 BT_DBG("int_status = 0x%x", *ireg);
771
772 sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS |
773 UP_LD_HOST_INT_STATUS),
774 card->reg->host_intstatus, &ret);
775 if (ret) {
776 BT_ERR("sdio_writeb: clear int status failed: %d", ret);
777 return ret;
778 }
779 }
780
781 return 0;
782}
783
Bing Zhao789221e2009-06-02 14:29:36 -0700784static void btmrvl_sdio_interrupt(struct sdio_func *func)
785{
786 struct btmrvl_private *priv;
Bing Zhao789221e2009-06-02 14:29:36 -0700787 struct btmrvl_sdio_card *card;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700788 ulong flags;
Bing Zhao789221e2009-06-02 14:29:36 -0700789 u8 ireg = 0;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700790 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700791
Bing Zhao789221e2009-06-02 14:29:36 -0700792 card = sdio_get_drvdata(func);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700793 if (!card || !card->priv) {
Amitkumar Karwar7365d472014-11-24 02:40:52 -0800794 BT_ERR("sbi_interrupt(%p) card or priv is NULL, card=%p",
795 func, card);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700796 return;
Bing Zhao789221e2009-06-02 14:29:36 -0700797 }
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700798
799 priv = card->priv;
800
Amitkumar Karwar7b4b8742015-01-01 00:13:41 -0800801 if (priv->surprise_removed)
802 return;
803
Bing Zhao0d367402014-03-28 14:54:39 -0700804 if (card->reg->int_read_to_clear)
805 ret = btmrvl_sdio_read_to_clear(card, &ireg);
806 else
807 ret = btmrvl_sdio_write_to_clear(card, &ireg);
808
Bing Zhao8af48402014-03-27 21:30:42 -0700809 if (ret)
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700810 return;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700811
812 spin_lock_irqsave(&priv->driver_lock, flags);
813 sdio_ireg |= ireg;
814 spin_unlock_irqrestore(&priv->driver_lock, flags);
815
816 btmrvl_interrupt(priv);
Bing Zhao789221e2009-06-02 14:29:36 -0700817}
818
819static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
820{
Bing Zhao789221e2009-06-02 14:29:36 -0700821 struct sdio_func *func;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200822 u8 reg;
823 int ret = 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700824
Bing Zhao789221e2009-06-02 14:29:36 -0700825 if (!card || !card->func) {
826 BT_ERR("Error: card or function is NULL!");
827 ret = -EINVAL;
828 goto failed;
829 }
830
831 func = card->func;
832
Bing Zhao789221e2009-06-02 14:29:36 -0700833 sdio_claim_host(func);
834
835 ret = sdio_enable_func(func);
836 if (ret) {
837 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
838 ret = -EIO;
839 goto release_host;
840 }
841
842 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
843 if (ret) {
844 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
845 ret = -EIO;
846 goto disable_func;
847 }
848
849 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
850 if (ret) {
851 BT_ERR("cannot set SDIO block size");
852 ret = -EIO;
853 goto release_irq;
854 }
855
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700856 reg = sdio_readb(func, card->reg->io_port_0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700857 if (ret < 0) {
858 ret = -EIO;
859 goto release_irq;
860 }
861
862 card->ioport = reg;
863
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700864 reg = sdio_readb(func, card->reg->io_port_1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700865 if (ret < 0) {
866 ret = -EIO;
867 goto release_irq;
868 }
869
870 card->ioport |= (reg << 8);
871
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700872 reg = sdio_readb(func, card->reg->io_port_2, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700873 if (ret < 0) {
874 ret = -EIO;
875 goto release_irq;
876 }
877
878 card->ioport |= (reg << 16);
879
880 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
881
Bing Zhao0d367402014-03-28 14:54:39 -0700882 if (card->reg->int_read_to_clear) {
883 reg = sdio_readb(func, card->reg->host_int_rsr, &ret);
884 if (ret < 0) {
885 ret = -EIO;
886 goto release_irq;
887 }
888 sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret);
889 if (ret < 0) {
890 ret = -EIO;
891 goto release_irq;
892 }
893
894 reg = sdio_readb(func, card->reg->card_misc_cfg, &ret);
895 if (ret < 0) {
896 ret = -EIO;
897 goto release_irq;
898 }
899 sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret);
900 if (ret < 0) {
901 ret = -EIO;
902 goto release_irq;
903 }
904 }
905
Bing Zhao789221e2009-06-02 14:29:36 -0700906 sdio_set_drvdata(func, card);
907
908 sdio_release_host(func);
909
Bing Zhao789221e2009-06-02 14:29:36 -0700910 return 0;
911
912release_irq:
913 sdio_release_irq(func);
914
915disable_func:
916 sdio_disable_func(func);
917
918release_host:
919 sdio_release_host(func);
920
921failed:
Bing Zhao789221e2009-06-02 14:29:36 -0700922 return ret;
923}
924
925static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
926{
Bing Zhao789221e2009-06-02 14:29:36 -0700927 if (card && card->func) {
928 sdio_claim_host(card->func);
929 sdio_release_irq(card->func);
930 sdio_disable_func(card->func);
931 sdio_release_host(card->func);
932 sdio_set_drvdata(card->func, NULL);
933 }
934
Bing Zhao789221e2009-06-02 14:29:36 -0700935 return 0;
936}
937
938static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
939{
940 int ret;
941
Marcel Holtmann93742532009-06-13 07:40:18 +0200942 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700943 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700944
945 sdio_claim_host(card->func);
946
947 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
948
949 btmrvl_sdio_get_rx_unit(card);
950
951 sdio_release_host(card->func);
952
Bing Zhao789221e2009-06-02 14:29:36 -0700953 return ret;
954}
955
956static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
957{
958 int ret;
959
Marcel Holtmann93742532009-06-13 07:40:18 +0200960 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700961 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700962
963 sdio_claim_host(card->func);
964
965 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
966
967 sdio_release_host(card->func);
968
Bing Zhao789221e2009-06-02 14:29:36 -0700969 return ret;
970}
971
972static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
973 u8 *payload, u16 nb)
974{
975 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
976 int ret = 0;
977 int buf_block_len;
978 int blksz;
979 int i = 0;
980 u8 *buf = NULL;
981 void *tmpbuf = NULL;
982 int tmpbufsz;
983
Bing Zhao789221e2009-06-02 14:29:36 -0700984 if (!card || !card->func) {
985 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700986 return -EINVAL;
987 }
988
989 buf = payload;
Bing Zhao3318b232009-07-08 11:44:14 -0700990 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
Bing Zhao789221e2009-06-02 14:29:36 -0700991 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
Julia Lawall59598092009-08-06 22:05:18 +0200992 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
993 if (!tmpbuf)
994 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700995 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
996 memcpy(buf, payload, nb);
997 }
998
999 blksz = SDIO_BLOCK_SIZE;
Andy Shevchenkoba34dd32013-08-02 14:10:12 +03001000 buf_block_len = DIV_ROUND_UP(nb, blksz);
Bing Zhao789221e2009-06-02 14:29:36 -07001001
1002 sdio_claim_host(card->func);
1003
1004 do {
1005 /* Transfer data to card */
1006 ret = sdio_writesb(card->func, card->ioport, buf,
1007 buf_block_len * blksz);
1008 if (ret < 0) {
1009 i++;
1010 BT_ERR("i=%d writesb failed: %d", i, ret);
Andrei Emeltchenko78c1b8e2012-10-10 17:41:33 +03001011 BT_ERR("hex: %*ph", nb, payload);
Bing Zhao789221e2009-06-02 14:29:36 -07001012 ret = -EIO;
1013 if (i > MAX_WRITE_IOMEM_RETRY)
1014 goto exit;
1015 }
1016 } while (ret);
1017
1018 priv->btmrvl_dev.tx_dnld_rdy = false;
1019
1020exit:
1021 sdio_release_host(card->func);
Yoichi Yuasa88d1a0c2010-02-04 09:55:51 +09001022 kfree(tmpbuf);
Bing Zhao789221e2009-06-02 14:29:36 -07001023
Bing Zhao789221e2009-06-02 14:29:36 -07001024 return ret;
1025}
1026
1027static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
1028{
Gustavo Padovanfaff7f72013-04-18 19:35:33 -03001029 int ret;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001030 u8 fws0;
1031 int pollnum = MAX_POLL_TRIES;
Bing Zhao789221e2009-06-02 14:29:36 -07001032
Bing Zhao789221e2009-06-02 14:29:36 -07001033 if (!card || !card->func) {
1034 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -07001035 return -EINVAL;
1036 }
Bing Zhao789221e2009-06-02 14:29:36 -07001037
1038 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
1039 BT_DBG("Firmware already downloaded!");
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001040 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -07001041 }
1042
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001043 sdio_claim_host(card->func);
1044
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001045 /* Check if other function driver is downloading the firmware */
1046 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -07001047 if (ret) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001048 BT_ERR("Failed to read FW downloading status!");
Bing Zhao789221e2009-06-02 14:29:36 -07001049 ret = -EIO;
1050 goto done;
1051 }
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001052 if (fws0) {
1053 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
Bing Zhao789221e2009-06-02 14:29:36 -07001054
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001055 /* Give other function more time to download the firmware */
1056 pollnum *= 10;
1057 } else {
1058 if (card->helper) {
1059 ret = btmrvl_sdio_download_helper(card);
1060 if (ret) {
1061 BT_ERR("Failed to download helper!");
1062 ret = -EIO;
1063 goto done;
1064 }
1065 }
1066
1067 if (btmrvl_sdio_download_fw_w_helper(card)) {
1068 BT_ERR("Failed to download firmware!");
1069 ret = -EIO;
1070 goto done;
1071 }
Bing Zhao789221e2009-06-02 14:29:36 -07001072 }
1073
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001074 sdio_release_host(card->func);
1075
Andreas Fenkart69676b12013-04-22 11:10:23 +02001076 /*
1077 * winner or not, with this test the FW synchronizes when the
1078 * module can continue its initialization
1079 */
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001080 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
Bing Zhao789221e2009-06-02 14:29:36 -07001081 BT_ERR("FW failed to be active in time!");
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001082 return -ETIMEDOUT;
Bing Zhao789221e2009-06-02 14:29:36 -07001083 }
1084
Andreas Fenkartc1c999e2013-04-22 11:10:22 +02001085 return 0;
1086
Bing Zhao789221e2009-06-02 14:29:36 -07001087done:
1088 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -07001089 return ret;
1090}
1091
1092static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
1093{
1094 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1095 int ret = 0;
1096
Bing Zhao789221e2009-06-02 14:29:36 -07001097 if (!card || !card->func) {
1098 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -07001099 return -EINVAL;
1100 }
1101
1102 sdio_claim_host(card->func);
1103
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001104 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -07001105
1106 sdio_release_host(card->func);
1107
1108 BT_DBG("wake up firmware");
1109
Bing Zhao789221e2009-06-02 14:29:36 -07001110 return ret;
1111}
1112
Xinming Hudc759612014-11-24 02:40:53 -08001113static void btmrvl_sdio_dump_regs(struct btmrvl_private *priv)
1114{
1115 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1116 int ret = 0;
1117 unsigned int reg, reg_start, reg_end;
1118 char buf[256], *ptr;
1119 u8 loop, func, data;
1120 int MAX_LOOP = 2;
1121
1122 btmrvl_sdio_wakeup_fw(priv);
1123 sdio_claim_host(card->func);
1124
1125 for (loop = 0; loop < MAX_LOOP; loop++) {
1126 memset(buf, 0, sizeof(buf));
1127 ptr = buf;
1128
1129 if (loop == 0) {
1130 /* Read the registers of SDIO function0 */
1131 func = loop;
1132 reg_start = 0;
1133 reg_end = 9;
1134 } else {
1135 func = 2;
1136 reg_start = 0;
1137 reg_end = 0x09;
1138 }
1139
1140 ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
1141 func, reg_start, reg_end);
1142 for (reg = reg_start; reg <= reg_end; reg++) {
1143 if (func == 0)
1144 data = sdio_f0_readb(card->func, reg, &ret);
1145 else
1146 data = sdio_readb(card->func, reg, &ret);
1147
1148 if (!ret) {
1149 ptr += sprintf(ptr, "%02x ", data);
1150 } else {
1151 ptr += sprintf(ptr, "ERR");
1152 break;
1153 }
1154 }
1155
1156 BT_INFO("%s", buf);
1157 }
1158
1159 sdio_release_host(card->func);
1160}
1161
1162/* This function read/write firmware */
1163static enum
1164rdwr_status btmrvl_sdio_rdwr_firmware(struct btmrvl_private *priv,
1165 u8 doneflag)
1166{
1167 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1168 int ret, tries;
1169 u8 ctrl_data = 0;
1170
1171 sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl,
1172 &ret);
1173
1174 if (ret) {
1175 BT_ERR("SDIO write err");
1176 return RDWR_STATUS_FAILURE;
1177 }
1178
1179 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
1180 ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
1181 &ret);
1182
1183 if (ret) {
1184 BT_ERR("SDIO read err");
1185 return RDWR_STATUS_FAILURE;
1186 }
1187
1188 if (ctrl_data == FW_DUMP_DONE)
1189 break;
1190 if (doneflag && ctrl_data == doneflag)
1191 return RDWR_STATUS_DONE;
1192 if (ctrl_data != FW_DUMP_HOST_READY) {
1193 BT_INFO("The ctrl reg was changed, re-try again!");
1194 sdio_writeb(card->func, FW_DUMP_HOST_READY,
1195 card->reg->fw_dump_ctrl, &ret);
1196 if (ret) {
1197 BT_ERR("SDIO write err");
1198 return RDWR_STATUS_FAILURE;
1199 }
1200 }
1201 usleep_range(100, 200);
1202 }
1203
1204 if (ctrl_data == FW_DUMP_HOST_READY) {
1205 BT_ERR("Fail to pull ctrl_data");
1206 return RDWR_STATUS_FAILURE;
1207 }
1208
1209 return RDWR_STATUS_SUCCESS;
1210}
1211
1212/* This function dump sdio register and memory data */
1213static void btmrvl_sdio_dump_firmware(struct btmrvl_private *priv)
1214{
1215 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
1216 int ret = 0;
1217 unsigned int reg, reg_start, reg_end;
1218 enum rdwr_status stat;
1219 u8 *dbg_ptr, *end_ptr, *fw_dump_data, *fw_dump_ptr;
1220 u8 dump_num, idx, i, read_reg, doneflag = 0;
1221 u32 memory_size, fw_dump_len = 0;
1222
1223 /* dump sdio register first */
1224 btmrvl_sdio_dump_regs(priv);
1225
1226 if (!card->supports_fw_dump) {
1227 BT_ERR("Firmware dump not supported for this card!");
1228 return;
1229 }
1230
1231 for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
1232 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1233
1234 if (entry->mem_ptr) {
1235 vfree(entry->mem_ptr);
1236 entry->mem_ptr = NULL;
1237 }
1238 entry->mem_size = 0;
1239 }
1240
1241 btmrvl_sdio_wakeup_fw(priv);
1242 sdio_claim_host(card->func);
1243
1244 BT_INFO("== btmrvl firmware dump start ==");
1245
1246 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1247 if (stat == RDWR_STATUS_FAILURE)
1248 goto done;
1249
1250 reg = card->reg->fw_dump_start;
1251 /* Read the number of the memories which will dump */
1252 dump_num = sdio_readb(card->func, reg, &ret);
1253
1254 if (ret) {
1255 BT_ERR("SDIO read memory length err");
1256 goto done;
1257 }
1258
1259 /* Read the length of every memory which will dump */
1260 for (idx = 0; idx < dump_num; idx++) {
1261 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1262
1263 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1264 if (stat == RDWR_STATUS_FAILURE)
1265 goto done;
1266
1267 memory_size = 0;
1268 reg = card->reg->fw_dump_start;
1269 for (i = 0; i < 4; i++) {
1270 read_reg = sdio_readb(card->func, reg, &ret);
1271 if (ret) {
1272 BT_ERR("SDIO read err");
1273 goto done;
1274 }
1275 memory_size |= (read_reg << i*8);
1276 reg++;
1277 }
1278
1279 if (memory_size == 0) {
1280 BT_INFO("Firmware dump finished!");
1281 break;
1282 }
1283
1284 BT_INFO("%s_SIZE=0x%x", entry->mem_name, memory_size);
1285 entry->mem_ptr = vzalloc(memory_size + 1);
1286 entry->mem_size = memory_size;
1287 if (!entry->mem_ptr) {
1288 BT_ERR("Vzalloc %s failed", entry->mem_name);
1289 goto done;
1290 }
1291
1292 fw_dump_len += (strlen("========Start dump ") +
1293 strlen(entry->mem_name) +
1294 strlen("========\n") +
1295 (memory_size + 1) +
1296 strlen("\n========End dump========\n"));
1297
1298 dbg_ptr = entry->mem_ptr;
1299 end_ptr = dbg_ptr + memory_size;
1300
1301 doneflag = entry->done_flag;
1302 BT_INFO("Start %s output, please wait...",
1303 entry->mem_name);
1304
1305 do {
1306 stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
1307 if (stat == RDWR_STATUS_FAILURE)
1308 goto done;
1309
1310 reg_start = card->reg->fw_dump_start;
1311 reg_end = card->reg->fw_dump_end;
1312 for (reg = reg_start; reg <= reg_end; reg++) {
1313 *dbg_ptr = sdio_readb(card->func, reg, &ret);
1314 if (ret) {
1315 BT_ERR("SDIO read err");
1316 goto done;
1317 }
1318 if (dbg_ptr < end_ptr)
1319 dbg_ptr++;
1320 else
1321 BT_ERR("Allocated buffer not enough");
1322 }
1323
1324 if (stat != RDWR_STATUS_DONE) {
1325 continue;
1326 } else {
1327 BT_INFO("%s done: size=0x%tx",
1328 entry->mem_name,
1329 dbg_ptr - entry->mem_ptr);
1330 break;
1331 }
1332 } while (1);
1333 }
1334
1335 BT_INFO("== btmrvl firmware dump end ==");
1336
1337done:
1338 sdio_release_host(card->func);
1339
1340 if (fw_dump_len == 0)
1341 return;
1342
1343 fw_dump_data = vzalloc(fw_dump_len+1);
1344 if (!fw_dump_data) {
1345 BT_ERR("Vzalloc fw_dump_data fail!");
1346 return;
1347 }
1348 fw_dump_ptr = fw_dump_data;
1349
1350 /* Dump all the memory data into single file, a userspace script will
1351 be used to split all the memory data to multiple files*/
1352 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump start");
1353 for (idx = 0; idx < dump_num; idx++) {
1354 struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
1355
1356 if (entry->mem_ptr) {
1357 strcpy(fw_dump_ptr, "========Start dump ");
1358 fw_dump_ptr += strlen("========Start dump ");
1359
1360 strcpy(fw_dump_ptr, entry->mem_name);
1361 fw_dump_ptr += strlen(entry->mem_name);
1362
1363 strcpy(fw_dump_ptr, "========\n");
1364 fw_dump_ptr += strlen("========\n");
1365
1366 memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
1367 fw_dump_ptr += entry->mem_size;
1368
1369 strcpy(fw_dump_ptr, "\n========End dump========\n");
1370 fw_dump_ptr += strlen("\n========End dump========\n");
1371
1372 vfree(mem_type_mapping_tbl[idx].mem_ptr);
1373 mem_type_mapping_tbl[idx].mem_ptr = NULL;
1374 }
1375 }
1376
1377 /* fw_dump_data will be free in device coredump release function
1378 after 5 min*/
1379 dev_coredumpv(&priv->btmrvl_dev.hcidev->dev, fw_dump_data,
1380 fw_dump_len, GFP_KERNEL);
1381 BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump end");
1382}
1383
Bing Zhao789221e2009-06-02 14:29:36 -07001384static int btmrvl_sdio_probe(struct sdio_func *func,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001385 const struct sdio_device_id *id)
Bing Zhao789221e2009-06-02 14:29:36 -07001386{
1387 int ret = 0;
1388 struct btmrvl_private *priv = NULL;
1389 struct btmrvl_sdio_card *card = NULL;
1390
Bing Zhao789221e2009-06-02 14:29:36 -07001391 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
1392 id->vendor, id->device, id->class, func->num);
1393
Sachin Kamateb17ea32012-07-27 12:38:36 +05301394 card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
1395 if (!card)
1396 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -07001397
1398 card->func = func;
1399
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001400 if (id->driver_data) {
1401 struct btmrvl_sdio_device *data = (void *) id->driver_data;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001402 card->helper = data->helper;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001403 card->firmware = data->firmware;
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001404 card->reg = data->reg;
1405 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
Bing Zhao4df82b52014-03-31 14:41:44 -07001406 card->support_pscan_win_report = data->support_pscan_win_report;
Xinming Hudc759612014-11-24 02:40:53 -08001407 card->supports_fw_dump = data->supports_fw_dump;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +02001408 }
1409
Bing Zhao789221e2009-06-02 14:29:36 -07001410 if (btmrvl_sdio_register_dev(card) < 0) {
1411 BT_ERR("Failed to register BT device!");
Sachin Kamateb17ea32012-07-27 12:38:36 +05301412 return -ENODEV;
Bing Zhao789221e2009-06-02 14:29:36 -07001413 }
1414
1415 /* Disable the interrupts on the card */
1416 btmrvl_sdio_disable_host_int(card);
1417
1418 if (btmrvl_sdio_download_fw(card)) {
1419 BT_ERR("Downloading firmware failed!");
1420 ret = -ENODEV;
1421 goto unreg_dev;
1422 }
1423
Bing Zhao789221e2009-06-02 14:29:36 -07001424 btmrvl_sdio_enable_host_int(card);
1425
1426 priv = btmrvl_add_card(card);
1427 if (!priv) {
1428 BT_ERR("Initializing card failed!");
1429 ret = -ENODEV;
1430 goto disable_host_int;
1431 }
1432
1433 card->priv = priv;
1434
1435 /* Initialize the interface specific function pointers */
1436 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
1437 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
Amitkumar Karward1d10d72010-05-27 16:38:37 -07001438 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
Xinming Hudc759612014-11-24 02:40:53 -08001439 priv->firmware_dump = btmrvl_sdio_dump_firmware;
Bing Zhao789221e2009-06-02 14:29:36 -07001440
Bing Zhao64061602010-03-03 14:37:36 -08001441 if (btmrvl_register_hdev(priv)) {
1442 BT_ERR("Register hdev failed!");
1443 ret = -ENODEV;
1444 goto disable_host_int;
1445 }
1446
Bing Zhao789221e2009-06-02 14:29:36 -07001447 return 0;
1448
1449disable_host_int:
1450 btmrvl_sdio_disable_host_int(card);
1451unreg_dev:
1452 btmrvl_sdio_unregister_dev(card);
Bing Zhao789221e2009-06-02 14:29:36 -07001453 return ret;
1454}
1455
1456static void btmrvl_sdio_remove(struct sdio_func *func)
1457{
1458 struct btmrvl_sdio_card *card;
1459
Bing Zhao789221e2009-06-02 14:29:36 -07001460 if (func) {
1461 card = sdio_get_drvdata(func);
1462 if (card) {
1463 /* Send SHUTDOWN command & disable interrupt
1464 * if user removes the module.
1465 */
1466 if (user_rmmod) {
1467 btmrvl_send_module_cfg_cmd(card->priv,
1468 MODULE_SHUTDOWN_REQ);
1469 btmrvl_sdio_disable_host_int(card);
1470 }
1471 BT_DBG("unregester dev");
Amitkumar Karwar7b4b8742015-01-01 00:13:41 -08001472 card->priv->surprise_removed = true;
Bing Zhao789221e2009-06-02 14:29:36 -07001473 btmrvl_sdio_unregister_dev(card);
1474 btmrvl_remove_card(card->priv);
Bing Zhao789221e2009-06-02 14:29:36 -07001475 }
1476 }
Bing Zhao789221e2009-06-02 14:29:36 -07001477}
1478
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001479static int btmrvl_sdio_suspend(struct device *dev)
1480{
1481 struct sdio_func *func = dev_to_sdio_func(dev);
1482 struct btmrvl_sdio_card *card;
1483 struct btmrvl_private *priv;
1484 mmc_pm_flag_t pm_flags;
1485 struct hci_dev *hcidev;
1486
1487 if (func) {
1488 pm_flags = sdio_get_host_pm_caps(func);
1489 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1490 pm_flags);
1491 if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1492 BT_ERR("%s: cannot remain alive while suspended",
1493 sdio_func_id(func));
1494 return -ENOSYS;
1495 }
1496 card = sdio_get_drvdata(func);
1497 if (!card || !card->priv) {
1498 BT_ERR("card or priv structure is not valid");
1499 return 0;
1500 }
1501 } else {
1502 BT_ERR("sdio_func is not specified");
1503 return 0;
1504 }
1505
1506 priv = card->priv;
Chin-Ran Lo5bf8a742014-07-14 21:05:37 -07001507 hcidev = priv->btmrvl_dev.hcidev;
1508 BT_DBG("%s: SDIO suspend", hcidev->name);
1509 hci_suspend_dev(hcidev);
1510 skb_queue_purge(&priv->adapter->tx_queue);
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001511
1512 if (priv->adapter->hs_state != HS_ACTIVATED) {
1513 if (btmrvl_enable_hs(priv)) {
1514 BT_ERR("HS not actived, suspend failed!");
1515 return -EBUSY;
1516 }
1517 }
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001518
1519 priv->adapter->is_suspended = true;
1520
1521 /* We will keep the power when hs enabled successfully */
1522 if (priv->adapter->hs_state == HS_ACTIVATED) {
1523 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1524 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1525 } else {
1526 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1527 return 0;
1528 }
1529}
1530
1531static int btmrvl_sdio_resume(struct device *dev)
1532{
1533 struct sdio_func *func = dev_to_sdio_func(dev);
1534 struct btmrvl_sdio_card *card;
1535 struct btmrvl_private *priv;
1536 mmc_pm_flag_t pm_flags;
1537 struct hci_dev *hcidev;
1538
1539 if (func) {
1540 pm_flags = sdio_get_host_pm_caps(func);
1541 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1542 pm_flags);
1543 card = sdio_get_drvdata(func);
1544 if (!card || !card->priv) {
1545 BT_ERR("card or priv structure is not valid");
1546 return 0;
1547 }
1548 } else {
1549 BT_ERR("sdio_func is not specified");
1550 return 0;
1551 }
1552 priv = card->priv;
1553
1554 if (!priv->adapter->is_suspended) {
1555 BT_DBG("device already resumed");
1556 return 0;
1557 }
1558
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001559 priv->hw_wakeup_firmware(priv);
1560 priv->adapter->hs_state = HS_DEACTIVATED;
Chin-Ran Lo5bf8a742014-07-14 21:05:37 -07001561 hcidev = priv->btmrvl_dev.hcidev;
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001562 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
Chin-Ran Lo5bf8a742014-07-14 21:05:37 -07001563 priv->adapter->is_suspended = false;
1564 BT_DBG("%s: SDIO resume", hcidev->name);
1565 hci_resume_dev(hcidev);
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001566
1567 return 0;
1568}
1569
1570static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1571 .suspend = btmrvl_sdio_suspend,
1572 .resume = btmrvl_sdio_resume,
1573};
1574
Bing Zhao789221e2009-06-02 14:29:36 -07001575static struct sdio_driver bt_mrvl_sdio = {
1576 .name = "btmrvl_sdio",
1577 .id_table = btmrvl_sdio_ids,
1578 .probe = btmrvl_sdio_probe,
1579 .remove = btmrvl_sdio_remove,
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001580 .drv = {
1581 .owner = THIS_MODULE,
1582 .pm = &btmrvl_sdio_pm_ops,
1583 }
Bing Zhao789221e2009-06-02 14:29:36 -07001584};
1585
Peter Huewe8e5b2302009-12-22 09:34:20 +01001586static int __init btmrvl_sdio_init_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001587{
Bing Zhao789221e2009-06-02 14:29:36 -07001588 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1589 BT_ERR("SDIO Driver Registration Failed");
Bing Zhao789221e2009-06-02 14:29:36 -07001590 return -ENODEV;
1591 }
1592
1593 /* Clear the flag in case user removes the card. */
1594 user_rmmod = 0;
1595
Bing Zhao789221e2009-06-02 14:29:36 -07001596 return 0;
1597}
1598
Peter Huewe8e5b2302009-12-22 09:34:20 +01001599static void __exit btmrvl_sdio_exit_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001600{
Bing Zhao789221e2009-06-02 14:29:36 -07001601 /* Set the flag as user is removing this module. */
1602 user_rmmod = 1;
1603
1604 sdio_unregister_driver(&bt_mrvl_sdio);
Bing Zhao789221e2009-06-02 14:29:36 -07001605}
1606
1607module_init(btmrvl_sdio_init_module);
1608module_exit(btmrvl_sdio_exit_module);
1609
1610MODULE_AUTHOR("Marvell International Ltd.");
Marcel Holtmann9666fb32009-06-09 21:45:04 +02001611MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
Bing Zhao789221e2009-06-02 14:29:36 -07001612MODULE_VERSION(VERSION);
1613MODULE_LICENSE("GPL v2");
Lubomir Rintel77189df2013-03-13 12:04:30 +01001614MODULE_FIRMWARE("mrvl/sd8688_helper.bin");
1615MODULE_FIRMWARE("mrvl/sd8688.bin");
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001616MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
Bing Zhao2ac654f2011-11-16 20:40:42 -08001617MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");
Xinming Hu3907d552014-09-30 06:45:33 -04001618MODULE_FIRMWARE("mrvl/sd8887_uapsta.bin");
Bing Zhao22f2efe2013-05-13 18:15:32 -07001619MODULE_FIRMWARE("mrvl/sd8897_uapsta.bin");