blob: 0dba76aa2232d9231e466ef2e610fffb29513df9 [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
21#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>
26
27#include <net/bluetooth/bluetooth.h>
28#include <net/bluetooth/hci_core.h>
29
30#include "btmrvl_drv.h"
31#include "btmrvl_sdio.h"
32
33#define VERSION "1.0"
34
Bing Zhao789221e2009-06-02 14:29:36 -070035/* The btmrvl_sdio_remove() callback function is called
36 * when user removes this module from kernel space or ejects
37 * the card from the slot. The driver handles these 2 cases
38 * differently.
39 * If the user is removing the module, a MODULE_SHUTDOWN_REQ
40 * command is sent to firmware and interrupt will be disabled.
41 * If the card is removed, there is no need to send command
42 * or disable interrupt.
43 *
44 * The variable 'user_rmmod' is used to distinguish these two
45 * scenarios. This flag is initialized as FALSE in case the card
46 * is removed, and will be set to TRUE for module removal when
47 * module_exit function is called.
48 */
49static u8 user_rmmod;
50
Marcel Holtmanndcf47f32009-06-09 16:21:58 +020051static const struct btmrvl_sdio_device btmrvl_sdio_sd6888 = {
52 .helper = "sd8688_helper.bin",
53 .firmware = "sd8688.bin",
54};
55
Bing Zhao789221e2009-06-02 14:29:36 -070056static const struct sdio_device_id btmrvl_sdio_ids[] = {
Marcel Holtmanndcf47f32009-06-09 16:21:58 +020057 /* Marvell SD8688 Bluetooth device */
58 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
59 .driver_data = (unsigned long) &btmrvl_sdio_sd6888 },
60
61 { } /* Terminating entry */
Bing Zhao789221e2009-06-02 14:29:36 -070062};
63
64MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
65
Bing Zhao789221e2009-06-02 14:29:36 -070066static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
67{
68 u8 reg;
69 int ret;
70
Bing Zhao789221e2009-06-02 14:29:36 -070071 reg = sdio_readb(card->func, CARD_RX_UNIT_REG, &ret);
72 if (!ret)
73 card->rx_unit = reg;
74
Bing Zhao789221e2009-06-02 14:29:36 -070075 return ret;
76}
77
78static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
79{
Bing Zhao789221e2009-06-02 14:29:36 -070080 u8 fws0, fws1;
Marcel Holtmann93742532009-06-13 07:40:18 +020081 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -070082
83 *dat = 0;
84
85 fws0 = sdio_readb(card->func, CARD_FW_STATUS0_REG, &ret);
86
87 if (!ret)
88 fws1 = sdio_readb(card->func, CARD_FW_STATUS1_REG, &ret);
89
Marcel Holtmann93742532009-06-13 07:40:18 +020090 if (ret)
Bing Zhao789221e2009-06-02 14:29:36 -070091 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -070092
93 *dat = (((u16) fws1) << 8) | fws0;
94
Bing Zhao789221e2009-06-02 14:29:36 -070095 return 0;
96}
97
98static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
99{
Bing Zhao789221e2009-06-02 14:29:36 -0700100 u8 reg;
Marcel Holtmann93742532009-06-13 07:40:18 +0200101 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700102
103 reg = sdio_readb(card->func, CARD_RX_LEN_REG, &ret);
104 if (!ret)
105 *dat = (u16) reg << card->rx_unit;
106
Bing Zhao789221e2009-06-02 14:29:36 -0700107 return ret;
108}
109
110static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200111 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700112{
113 int ret;
114
Bing Zhao789221e2009-06-02 14:29:36 -0700115 sdio_writeb(card->func, mask, HOST_INT_MASK_REG, &ret);
116 if (ret) {
117 BT_ERR("Unable to enable the host interrupt!");
118 ret = -EIO;
119 }
120
Bing Zhao789221e2009-06-02 14:29:36 -0700121 return ret;
122}
123
124static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200125 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700126{
Bing Zhao789221e2009-06-02 14:29:36 -0700127 u8 host_int_mask;
Marcel Holtmann93742532009-06-13 07:40:18 +0200128 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700129
130 host_int_mask = sdio_readb(card->func, HOST_INT_MASK_REG, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200131 if (ret)
132 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700133
134 host_int_mask &= ~mask;
135
136 sdio_writeb(card->func, host_int_mask, HOST_INT_MASK_REG, &ret);
137 if (ret < 0) {
138 BT_ERR("Unable to disable the host interrupt!");
Marcel Holtmann93742532009-06-13 07:40:18 +0200139 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700140 }
141
Marcel Holtmann93742532009-06-13 07:40:18 +0200142 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700143}
144
145static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
146{
147 unsigned int tries;
Bing Zhao789221e2009-06-02 14:29:36 -0700148 u8 status;
Marcel Holtmann93742532009-06-13 07:40:18 +0200149 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700150
151 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
152 status = sdio_readb(card->func, CARD_STATUS_REG, &ret);
153 if (ret)
154 goto failed;
155 if ((status & bits) == bits)
Marcel Holtmann93742532009-06-13 07:40:18 +0200156 return ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700157
158 udelay(1);
159 }
160
161 ret = -ETIMEDOUT;
162
163failed:
164 BT_ERR("FAILED! ret=%d", ret);
165
Bing Zhao789221e2009-06-02 14:29:36 -0700166 return ret;
167}
168
169static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200170 int pollnum)
Bing Zhao789221e2009-06-02 14:29:36 -0700171{
172 int ret = -ETIMEDOUT;
173 u16 firmwarestat;
174 unsigned int tries;
175
Bing Zhao789221e2009-06-02 14:29:36 -0700176 /* Wait for firmware to become ready */
177 for (tries = 0; tries < pollnum; tries++) {
178 if (btmrvl_sdio_read_fw_status(card, &firmwarestat) < 0)
179 continue;
180
181 if (firmwarestat == FIRMWARE_READY) {
182 ret = 0;
183 break;
184 } else {
185 msleep(10);
186 }
187 }
188
Bing Zhao789221e2009-06-02 14:29:36 -0700189 return ret;
190}
191
192static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
193{
194 const struct firmware *fw_helper = NULL;
195 const u8 *helper = NULL;
196 int ret;
197 void *tmphlprbuf = NULL;
198 int tmphlprbufsz, hlprblknow, helperlen;
199 u8 *helperbuf;
200 u32 tx_len;
201
Bing Zhao789221e2009-06-02 14:29:36 -0700202 ret = request_firmware(&fw_helper, card->helper,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200203 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700204 if ((ret < 0) || !fw_helper) {
205 BT_ERR("request_firmware(helper) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200206 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700207 ret = -ENOENT;
208 goto done;
209 }
210
211 helper = fw_helper->data;
212 helperlen = fw_helper->size;
213
214 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200215 helperlen, SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700216
217 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
218
219 tmphlprbuf = kmalloc(tmphlprbufsz, GFP_KERNEL);
220 if (!tmphlprbuf) {
221 BT_ERR("Unable to allocate buffer for helper."
222 " Terminating download");
223 ret = -ENOMEM;
224 goto done;
225 }
226
227 memset(tmphlprbuf, 0, tmphlprbufsz);
228
229 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
230
231 /* Perform helper data transfer */
232 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
233 - SDIO_HEADER_LEN;
234 hlprblknow = 0;
235
236 do {
237 ret = btmrvl_sdio_poll_card_status(card,
238 CARD_IO_READY | DN_LD_CARD_RDY);
239 if (ret < 0) {
240 BT_ERR("Helper download poll status timeout @ %d",
241 hlprblknow);
242 goto done;
243 }
244
245 /* Check if there is more data? */
246 if (hlprblknow >= helperlen)
247 break;
248
249 if (helperlen - hlprblknow < tx_len)
250 tx_len = helperlen - hlprblknow;
251
252 /* Little-endian */
253 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
254 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
255 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
256 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
257
258 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
259 tx_len);
260
261 /* Now send the data */
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200262 ret = sdio_writesb(card->func, card->ioport, helperbuf,
263 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700264 if (ret < 0) {
265 BT_ERR("IO error during helper download @ %d",
266 hlprblknow);
267 goto done;
268 }
269
270 hlprblknow += tx_len;
271 } while (true);
272
273 BT_DBG("Transferring helper image EOF block");
274
275 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
276
277 ret = sdio_writesb(card->func, card->ioport, helperbuf,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200278 SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700279 if (ret < 0) {
280 BT_ERR("IO error in writing helper image EOF block");
281 goto done;
282 }
283
284 ret = 0;
285
286done:
287 kfree(tmphlprbuf);
288 if (fw_helper)
289 release_firmware(fw_helper);
290
Bing Zhao789221e2009-06-02 14:29:36 -0700291 return ret;
292}
293
294static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
295{
296 const struct firmware *fw_firmware = NULL;
297 const u8 *firmware = NULL;
298 int firmwarelen, tmpfwbufsz, ret;
299 unsigned int tries, offset;
300 u8 base0, base1;
301 void *tmpfwbuf = NULL;
302 u8 *fwbuf;
303 u16 len;
304 int txlen = 0, tx_blocks = 0, count = 0;
305
Bing Zhao789221e2009-06-02 14:29:36 -0700306 ret = request_firmware(&fw_firmware, card->firmware,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200307 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700308 if ((ret < 0) || !fw_firmware) {
309 BT_ERR("request_firmware(firmware) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200310 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700311 ret = -ENOENT;
312 goto done;
313 }
314
315 firmware = fw_firmware->data;
316 firmwarelen = fw_firmware->size;
317
318 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
319
320 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
321 tmpfwbuf = kmalloc(tmpfwbufsz, GFP_KERNEL);
322 if (!tmpfwbuf) {
323 BT_ERR("Unable to allocate buffer for firmware."
324 " Terminating download");
325 ret = -ENOMEM;
326 goto done;
327 }
328
329 memset(tmpfwbuf, 0, tmpfwbufsz);
330
331 /* Ensure aligned firmware buffer */
332 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
333
334 /* Perform firmware data transfer */
335 offset = 0;
336 do {
337 ret = btmrvl_sdio_poll_card_status(card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200338 CARD_IO_READY | DN_LD_CARD_RDY);
Bing Zhao789221e2009-06-02 14:29:36 -0700339 if (ret < 0) {
340 BT_ERR("FW download with helper poll status"
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200341 " timeout @ %d", offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700342 goto done;
343 }
344
345 /* Check if there is more data ? */
346 if (offset >= firmwarelen)
347 break;
348
349 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
350 base0 = sdio_readb(card->func,
351 SQ_READ_BASE_ADDRESS_A0_REG, &ret);
352 if (ret) {
353 BT_ERR("BASE0 register read failed:"
354 " base0 = 0x%04X(%d)."
355 " Terminating download",
356 base0, base0);
357 ret = -EIO;
358 goto done;
359 }
360 base1 = sdio_readb(card->func,
361 SQ_READ_BASE_ADDRESS_A1_REG, &ret);
362 if (ret) {
363 BT_ERR("BASE1 register read failed:"
364 " base1 = 0x%04X(%d)."
365 " Terminating download",
366 base1, base1);
367 ret = -EIO;
368 goto done;
369 }
370
371 len = (((u16) base1) << 8) | base0;
372 if (len)
373 break;
374
375 udelay(10);
376 }
377
378 if (!len)
379 break;
380 else if (len > BTM_UPLD_SIZE) {
381 BT_ERR("FW download failure @%d, invalid length %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200382 offset, len);
Bing Zhao789221e2009-06-02 14:29:36 -0700383 ret = -EINVAL;
384 goto done;
385 }
386
387 txlen = len;
388
389 if (len & BIT(0)) {
390 count++;
391 if (count > MAX_WRITE_IOMEM_RETRY) {
392 BT_ERR("FW download failure @%d, "
393 "over max retry count", offset);
394 ret = -EIO;
395 goto done;
396 }
397 BT_ERR("FW CRC error indicated by the helper: "
398 "len = 0x%04X, txlen = %d", len, txlen);
399 len &= ~BIT(0);
400 /* Set txlen to 0 so as to resend from same offset */
401 txlen = 0;
402 } else {
403 count = 0;
404
405 /* Last block ? */
406 if (firmwarelen - offset < txlen)
407 txlen = firmwarelen - offset;
408
409 tx_blocks =
410 (txlen + SDIO_BLOCK_SIZE - 1) / SDIO_BLOCK_SIZE;
411
412 memcpy(fwbuf, &firmware[offset], txlen);
413 }
414
415 ret = sdio_writesb(card->func, card->ioport, fwbuf,
416 tx_blocks * SDIO_BLOCK_SIZE);
417
418 if (ret < 0) {
419 BT_ERR("FW download, writesb(%d) failed @%d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200420 count, offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700421 sdio_writeb(card->func, HOST_CMD53_FIN, CONFIG_REG,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200422 &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700423 if (ret)
424 BT_ERR("writeb failed (CFG)");
425 }
426
427 offset += txlen;
428 } while (true);
429
430 BT_DBG("FW download over, size %d bytes", offset);
431
432 ret = 0;
433
434done:
435 kfree(tmpfwbuf);
436
437 if (fw_firmware)
438 release_firmware(fw_firmware);
439
Bing Zhao789221e2009-06-02 14:29:36 -0700440 return ret;
441}
442
443static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
444{
445 u16 buf_len = 0;
446 int ret, buf_block_len, blksz;
447 struct sk_buff *skb = NULL;
448 u32 type;
449 u8 *payload = NULL;
450 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
451 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
452
Bing Zhao789221e2009-06-02 14:29:36 -0700453 if (!card || !card->func) {
454 BT_ERR("card or function is NULL!");
455 ret = -EINVAL;
456 goto exit;
457 }
458
459 /* Read the length of data to be transferred */
460 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
461 if (ret < 0) {
462 BT_ERR("read rx_len failed");
463 ret = -EIO;
464 goto exit;
465 }
466
467 blksz = SDIO_BLOCK_SIZE;
468 buf_block_len = (buf_len + blksz - 1) / blksz;
469
470 if (buf_len <= SDIO_HEADER_LEN
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200471 || (buf_block_len * blksz) > ALLOC_BUF_SIZE) {
Bing Zhao789221e2009-06-02 14:29:36 -0700472 BT_ERR("invalid packet length: %d", buf_len);
473 ret = -EINVAL;
474 goto exit;
475 }
476
477 /* Allocate buffer */
478 skb = bt_skb_alloc(buf_block_len * blksz + BTSDIO_DMA_ALIGN,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200479 GFP_ATOMIC);
Bing Zhao789221e2009-06-02 14:29:36 -0700480 if (skb == NULL) {
481 BT_ERR("No free skb");
482 goto exit;
483 }
484
Bing Zhao3318b232009-07-08 11:44:14 -0700485 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
486 skb_put(skb, (unsigned long) skb->data &
487 (BTSDIO_DMA_ALIGN - 1));
488 skb_pull(skb, (unsigned long) skb->data &
489 (BTSDIO_DMA_ALIGN - 1));
Bing Zhao789221e2009-06-02 14:29:36 -0700490 }
491
Bing Zhao3318b232009-07-08 11:44:14 -0700492 payload = skb->data;
Bing Zhao789221e2009-06-02 14:29:36 -0700493
494 ret = sdio_readsb(card->func, payload, card->ioport,
495 buf_block_len * blksz);
496 if (ret < 0) {
497 BT_ERR("readsb failed: %d", ret);
498 ret = -EIO;
499 goto exit;
500 }
501
502 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
503 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
504 */
505
506 buf_len = payload[0];
507 buf_len |= (u16) payload[1] << 8;
508 type = payload[3];
509
510 switch (type) {
511 case HCI_ACLDATA_PKT:
512 case HCI_SCODATA_PKT:
513 case HCI_EVENT_PKT:
514 bt_cb(skb)->pkt_type = type;
515 skb->dev = (void *)hdev;
516 skb_put(skb, buf_len);
517 skb_pull(skb, SDIO_HEADER_LEN);
518
519 if (type == HCI_EVENT_PKT)
520 btmrvl_check_evtpkt(priv, skb);
521
522 hci_recv_frame(skb);
523 hdev->stat.byte_rx += buf_len;
524 break;
525
526 case MRVL_VENDOR_PKT:
527 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
528 skb->dev = (void *)hdev;
529 skb_put(skb, buf_len);
530 skb_pull(skb, SDIO_HEADER_LEN);
531
532 if (btmrvl_process_event(priv, skb))
533 hci_recv_frame(skb);
534
535 hdev->stat.byte_rx += buf_len;
536 break;
537
538 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200539 BT_ERR("Unknown packet type:%d", type);
Bing Zhao789221e2009-06-02 14:29:36 -0700540 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, payload,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200541 blksz * buf_block_len);
Bing Zhao789221e2009-06-02 14:29:36 -0700542
543 kfree_skb(skb);
544 skb = NULL;
545 break;
546 }
547
548exit:
549 if (ret) {
550 hdev->stat.err_rx++;
551 if (skb)
552 kfree_skb(skb);
553 }
554
Bing Zhao789221e2009-06-02 14:29:36 -0700555 return ret;
556}
557
558static int btmrvl_sdio_get_int_status(struct btmrvl_private *priv, u8 * ireg)
559{
560 int ret;
561 u8 sdio_ireg = 0;
562 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
563
Bing Zhao789221e2009-06-02 14:29:36 -0700564 *ireg = 0;
565
566 sdio_ireg = sdio_readb(card->func, HOST_INTSTATUS_REG, &ret);
567 if (ret) {
568 BT_ERR("sdio_readb: read int status register failed");
569 ret = -EIO;
570 goto done;
571 }
572
573 if (sdio_ireg != 0) {
574 /*
575 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
576 * Clear the interrupt status register and re-enable the
577 * interrupt.
578 */
579 BT_DBG("sdio_ireg = 0x%x", sdio_ireg);
580
581 sdio_writeb(card->func, ~(sdio_ireg) & (DN_LD_HOST_INT_STATUS |
582 UP_LD_HOST_INT_STATUS),
583 HOST_INTSTATUS_REG, &ret);
584 if (ret) {
585 BT_ERR("sdio_writeb: clear int status register "
586 "failed");
587 ret = -EIO;
588 goto done;
589 }
590 }
591
592 if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
593 if (priv->btmrvl_dev.tx_dnld_rdy)
594 BT_DBG("tx_done already received: "
595 " int_status=0x%x", sdio_ireg);
596 else
597 priv->btmrvl_dev.tx_dnld_rdy = true;
598 }
599
600 if (sdio_ireg & UP_LD_HOST_INT_STATUS)
601 btmrvl_sdio_card_to_host(priv);
602
603 *ireg = sdio_ireg;
604
605 ret = 0;
606
607done:
Bing Zhao789221e2009-06-02 14:29:36 -0700608 return ret;
609}
610
611static void btmrvl_sdio_interrupt(struct sdio_func *func)
612{
613 struct btmrvl_private *priv;
614 struct hci_dev *hcidev;
615 struct btmrvl_sdio_card *card;
616 u8 ireg = 0;
617
Bing Zhao789221e2009-06-02 14:29:36 -0700618 card = sdio_get_drvdata(func);
619 if (card && card->priv) {
620 priv = card->priv;
621 hcidev = priv->btmrvl_dev.hcidev;
622
623 if (btmrvl_sdio_get_int_status(priv, &ireg))
624 BT_ERR("reading HOST_INT_STATUS_REG failed");
625 else
626 BT_DBG("HOST_INT_STATUS_REG %#x", ireg);
627
628 btmrvl_interrupt(priv);
629 }
Bing Zhao789221e2009-06-02 14:29:36 -0700630}
631
632static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
633{
Bing Zhao789221e2009-06-02 14:29:36 -0700634 struct sdio_func *func;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200635 u8 reg;
636 int ret = 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700637
Bing Zhao789221e2009-06-02 14:29:36 -0700638 if (!card || !card->func) {
639 BT_ERR("Error: card or function is NULL!");
640 ret = -EINVAL;
641 goto failed;
642 }
643
644 func = card->func;
645
Bing Zhao789221e2009-06-02 14:29:36 -0700646 sdio_claim_host(func);
647
648 ret = sdio_enable_func(func);
649 if (ret) {
650 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
651 ret = -EIO;
652 goto release_host;
653 }
654
655 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
656 if (ret) {
657 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
658 ret = -EIO;
659 goto disable_func;
660 }
661
662 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
663 if (ret) {
664 BT_ERR("cannot set SDIO block size");
665 ret = -EIO;
666 goto release_irq;
667 }
668
669 reg = sdio_readb(func, IO_PORT_0_REG, &ret);
670 if (ret < 0) {
671 ret = -EIO;
672 goto release_irq;
673 }
674
675 card->ioport = reg;
676
677 reg = sdio_readb(func, IO_PORT_1_REG, &ret);
678 if (ret < 0) {
679 ret = -EIO;
680 goto release_irq;
681 }
682
683 card->ioport |= (reg << 8);
684
685 reg = sdio_readb(func, IO_PORT_2_REG, &ret);
686 if (ret < 0) {
687 ret = -EIO;
688 goto release_irq;
689 }
690
691 card->ioport |= (reg << 16);
692
693 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
694
695 sdio_set_drvdata(func, card);
696
697 sdio_release_host(func);
698
Bing Zhao789221e2009-06-02 14:29:36 -0700699 return 0;
700
701release_irq:
702 sdio_release_irq(func);
703
704disable_func:
705 sdio_disable_func(func);
706
707release_host:
708 sdio_release_host(func);
709
710failed:
Bing Zhao789221e2009-06-02 14:29:36 -0700711 return ret;
712}
713
714static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
715{
Bing Zhao789221e2009-06-02 14:29:36 -0700716 if (card && card->func) {
717 sdio_claim_host(card->func);
718 sdio_release_irq(card->func);
719 sdio_disable_func(card->func);
720 sdio_release_host(card->func);
721 sdio_set_drvdata(card->func, NULL);
722 }
723
Bing Zhao789221e2009-06-02 14:29:36 -0700724 return 0;
725}
726
727static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
728{
729 int ret;
730
Marcel Holtmann93742532009-06-13 07:40:18 +0200731 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700732 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700733
734 sdio_claim_host(card->func);
735
736 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
737
738 btmrvl_sdio_get_rx_unit(card);
739
740 sdio_release_host(card->func);
741
Bing Zhao789221e2009-06-02 14:29:36 -0700742 return ret;
743}
744
745static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
746{
747 int ret;
748
Marcel Holtmann93742532009-06-13 07:40:18 +0200749 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700750 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700751
752 sdio_claim_host(card->func);
753
754 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
755
756 sdio_release_host(card->func);
757
Bing Zhao789221e2009-06-02 14:29:36 -0700758 return ret;
759}
760
761static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
762 u8 *payload, u16 nb)
763{
764 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
765 int ret = 0;
766 int buf_block_len;
767 int blksz;
768 int i = 0;
769 u8 *buf = NULL;
770 void *tmpbuf = NULL;
771 int tmpbufsz;
772
Bing Zhao789221e2009-06-02 14:29:36 -0700773 if (!card || !card->func) {
774 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700775 return -EINVAL;
776 }
777
778 buf = payload;
Bing Zhao3318b232009-07-08 11:44:14 -0700779 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
Bing Zhao789221e2009-06-02 14:29:36 -0700780 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
Julia Lawall59598092009-08-06 22:05:18 +0200781 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
782 if (!tmpbuf)
783 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700784 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
785 memcpy(buf, payload, nb);
786 }
787
788 blksz = SDIO_BLOCK_SIZE;
789 buf_block_len = (nb + blksz - 1) / blksz;
790
791 sdio_claim_host(card->func);
792
793 do {
794 /* Transfer data to card */
795 ret = sdio_writesb(card->func, card->ioport, buf,
796 buf_block_len * blksz);
797 if (ret < 0) {
798 i++;
799 BT_ERR("i=%d writesb failed: %d", i, ret);
800 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
801 payload, nb);
802 ret = -EIO;
803 if (i > MAX_WRITE_IOMEM_RETRY)
804 goto exit;
805 }
806 } while (ret);
807
808 priv->btmrvl_dev.tx_dnld_rdy = false;
809
810exit:
811 sdio_release_host(card->func);
Yoichi Yuasa88d1a0c2010-02-04 09:55:51 +0900812 kfree(tmpbuf);
Bing Zhao789221e2009-06-02 14:29:36 -0700813
Bing Zhao789221e2009-06-02 14:29:36 -0700814 return ret;
815}
816
817static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
818{
819 int ret = 0;
820
Bing Zhao789221e2009-06-02 14:29:36 -0700821 if (!card || !card->func) {
822 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700823 return -EINVAL;
824 }
825 sdio_claim_host(card->func);
826
827 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
828 BT_DBG("Firmware already downloaded!");
829 goto done;
830 }
831
832 ret = btmrvl_sdio_download_helper(card);
833 if (ret) {
834 BT_ERR("Failed to download helper!");
835 ret = -EIO;
836 goto done;
837 }
838
839 if (btmrvl_sdio_download_fw_w_helper(card)) {
840 BT_ERR("Failed to download firmware!");
841 ret = -EIO;
842 goto done;
843 }
844
845 if (btmrvl_sdio_verify_fw_download(card, MAX_POLL_TRIES)) {
846 BT_ERR("FW failed to be active in time!");
847 ret = -ETIMEDOUT;
848 goto done;
849 }
850
851done:
852 sdio_release_host(card->func);
853
Bing Zhao789221e2009-06-02 14:29:36 -0700854 return ret;
855}
856
857static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
858{
859 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
860 int ret = 0;
861
Bing Zhao789221e2009-06-02 14:29:36 -0700862 if (!card || !card->func) {
863 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700864 return -EINVAL;
865 }
866
867 sdio_claim_host(card->func);
868
869 sdio_writeb(card->func, HOST_POWER_UP, CONFIG_REG, &ret);
870
871 sdio_release_host(card->func);
872
873 BT_DBG("wake up firmware");
874
Bing Zhao789221e2009-06-02 14:29:36 -0700875 return ret;
876}
877
878static int btmrvl_sdio_probe(struct sdio_func *func,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200879 const struct sdio_device_id *id)
Bing Zhao789221e2009-06-02 14:29:36 -0700880{
881 int ret = 0;
882 struct btmrvl_private *priv = NULL;
883 struct btmrvl_sdio_card *card = NULL;
884
Bing Zhao789221e2009-06-02 14:29:36 -0700885 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
886 id->vendor, id->device, id->class, func->num);
887
888 card = kzalloc(sizeof(*card), GFP_KERNEL);
889 if (!card) {
890 ret = -ENOMEM;
891 goto done;
892 }
893
894 card->func = func;
895
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200896 if (id->driver_data) {
897 struct btmrvl_sdio_device *data = (void *) id->driver_data;
898 card->helper = data->helper;
899 card->firmware = data->firmware;
900 }
901
Bing Zhao789221e2009-06-02 14:29:36 -0700902 if (btmrvl_sdio_register_dev(card) < 0) {
903 BT_ERR("Failed to register BT device!");
904 ret = -ENODEV;
905 goto free_card;
906 }
907
908 /* Disable the interrupts on the card */
909 btmrvl_sdio_disable_host_int(card);
910
911 if (btmrvl_sdio_download_fw(card)) {
912 BT_ERR("Downloading firmware failed!");
913 ret = -ENODEV;
914 goto unreg_dev;
915 }
916
917 msleep(100);
918
919 btmrvl_sdio_enable_host_int(card);
920
921 priv = btmrvl_add_card(card);
922 if (!priv) {
923 BT_ERR("Initializing card failed!");
924 ret = -ENODEV;
925 goto disable_host_int;
926 }
927
928 card->priv = priv;
929
930 /* Initialize the interface specific function pointers */
931 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
932 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
933
Bing Zhao789221e2009-06-02 14:29:36 -0700934 btmrvl_send_module_cfg_cmd(priv, MODULE_BRINGUP_REQ);
Amitkumar Karwar32fd6392009-09-30 11:18:30 -0700935 priv->btmrvl_dev.psmode = 1;
936 btmrvl_enable_ps(priv);
Bing Zhao789221e2009-06-02 14:29:36 -0700937
Bing Zhao789221e2009-06-02 14:29:36 -0700938 return 0;
939
940disable_host_int:
941 btmrvl_sdio_disable_host_int(card);
942unreg_dev:
943 btmrvl_sdio_unregister_dev(card);
944free_card:
945 kfree(card);
946done:
Bing Zhao789221e2009-06-02 14:29:36 -0700947 return ret;
948}
949
950static void btmrvl_sdio_remove(struct sdio_func *func)
951{
952 struct btmrvl_sdio_card *card;
953
Bing Zhao789221e2009-06-02 14:29:36 -0700954 if (func) {
955 card = sdio_get_drvdata(func);
956 if (card) {
957 /* Send SHUTDOWN command & disable interrupt
958 * if user removes the module.
959 */
960 if (user_rmmod) {
961 btmrvl_send_module_cfg_cmd(card->priv,
962 MODULE_SHUTDOWN_REQ);
963 btmrvl_sdio_disable_host_int(card);
964 }
965 BT_DBG("unregester dev");
966 btmrvl_sdio_unregister_dev(card);
967 btmrvl_remove_card(card->priv);
968 kfree(card);
969 }
970 }
Bing Zhao789221e2009-06-02 14:29:36 -0700971}
972
973static struct sdio_driver bt_mrvl_sdio = {
974 .name = "btmrvl_sdio",
975 .id_table = btmrvl_sdio_ids,
976 .probe = btmrvl_sdio_probe,
977 .remove = btmrvl_sdio_remove,
978};
979
Peter Huewe8e5b2302009-12-22 09:34:20 +0100980static int __init btmrvl_sdio_init_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -0700981{
Bing Zhao789221e2009-06-02 14:29:36 -0700982 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
983 BT_ERR("SDIO Driver Registration Failed");
Bing Zhao789221e2009-06-02 14:29:36 -0700984 return -ENODEV;
985 }
986
987 /* Clear the flag in case user removes the card. */
988 user_rmmod = 0;
989
Bing Zhao789221e2009-06-02 14:29:36 -0700990 return 0;
991}
992
Peter Huewe8e5b2302009-12-22 09:34:20 +0100993static void __exit btmrvl_sdio_exit_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -0700994{
Bing Zhao789221e2009-06-02 14:29:36 -0700995 /* Set the flag as user is removing this module. */
996 user_rmmod = 1;
997
998 sdio_unregister_driver(&bt_mrvl_sdio);
Bing Zhao789221e2009-06-02 14:29:36 -0700999}
1000
1001module_init(btmrvl_sdio_init_module);
1002module_exit(btmrvl_sdio_exit_module);
1003
1004MODULE_AUTHOR("Marvell International Ltd.");
Marcel Holtmann9666fb32009-06-09 21:45:04 +02001005MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
Bing Zhao789221e2009-06-02 14:29:36 -07001006MODULE_VERSION(VERSION);
1007MODULE_LICENSE("GPL v2");
Ben Hutchings28614532009-11-07 21:41:18 +00001008MODULE_FIRMWARE("sd8688_helper.bin");
1009MODULE_FIRMWARE("sd8688.bin");