blob: 182e55345d6a28569786b358bfbfdce09cefc681 [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;
Amitkumar Karward1d10d72010-05-27 16:38:37 -070050static u8 sdio_ireg;
Bing Zhao789221e2009-06-02 14:29:36 -070051
Marcel Holtmanndcf47f32009-06-09 16:21:58 +020052static const struct btmrvl_sdio_device btmrvl_sdio_sd6888 = {
53 .helper = "sd8688_helper.bin",
54 .firmware = "sd8688.bin",
55};
56
Bing Zhao789221e2009-06-02 14:29:36 -070057static const struct sdio_device_id btmrvl_sdio_ids[] = {
Marcel Holtmanndcf47f32009-06-09 16:21:58 +020058 /* Marvell SD8688 Bluetooth device */
59 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
60 .driver_data = (unsigned long) &btmrvl_sdio_sd6888 },
61
62 { } /* Terminating entry */
Bing Zhao789221e2009-06-02 14:29:36 -070063};
64
65MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
66
Bing Zhao789221e2009-06-02 14:29:36 -070067static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
68{
69 u8 reg;
70 int ret;
71
Bing Zhao789221e2009-06-02 14:29:36 -070072 reg = sdio_readb(card->func, CARD_RX_UNIT_REG, &ret);
73 if (!ret)
74 card->rx_unit = reg;
75
Bing Zhao789221e2009-06-02 14:29:36 -070076 return ret;
77}
78
79static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
80{
Bing Zhao789221e2009-06-02 14:29:36 -070081 u8 fws0, fws1;
Marcel Holtmann93742532009-06-13 07:40:18 +020082 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -070083
84 *dat = 0;
85
86 fws0 = sdio_readb(card->func, CARD_FW_STATUS0_REG, &ret);
Kulikov Vasiliy278be272010-07-05 12:01:22 +040087 if (ret)
88 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -070089
Kulikov Vasiliy278be272010-07-05 12:01:22 +040090 fws1 = sdio_readb(card->func, CARD_FW_STATUS1_REG, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +020091 if (ret)
Bing Zhao789221e2009-06-02 14:29:36 -070092 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -070093
94 *dat = (((u16) fws1) << 8) | fws0;
95
Bing Zhao789221e2009-06-02 14:29:36 -070096 return 0;
97}
98
99static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
100{
Bing Zhao789221e2009-06-02 14:29:36 -0700101 u8 reg;
Marcel Holtmann93742532009-06-13 07:40:18 +0200102 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700103
104 reg = sdio_readb(card->func, CARD_RX_LEN_REG, &ret);
105 if (!ret)
106 *dat = (u16) reg << card->rx_unit;
107
Bing Zhao789221e2009-06-02 14:29:36 -0700108 return ret;
109}
110
111static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200112 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700113{
114 int ret;
115
Bing Zhao789221e2009-06-02 14:29:36 -0700116 sdio_writeb(card->func, mask, HOST_INT_MASK_REG, &ret);
117 if (ret) {
118 BT_ERR("Unable to enable the host interrupt!");
119 ret = -EIO;
120 }
121
Bing Zhao789221e2009-06-02 14:29:36 -0700122 return ret;
123}
124
125static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200126 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700127{
Bing Zhao789221e2009-06-02 14:29:36 -0700128 u8 host_int_mask;
Marcel Holtmann93742532009-06-13 07:40:18 +0200129 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700130
131 host_int_mask = sdio_readb(card->func, HOST_INT_MASK_REG, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200132 if (ret)
133 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700134
135 host_int_mask &= ~mask;
136
137 sdio_writeb(card->func, host_int_mask, HOST_INT_MASK_REG, &ret);
138 if (ret < 0) {
139 BT_ERR("Unable to disable the host interrupt!");
Marcel Holtmann93742532009-06-13 07:40:18 +0200140 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700141 }
142
Marcel Holtmann93742532009-06-13 07:40:18 +0200143 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700144}
145
146static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
147{
148 unsigned int tries;
Bing Zhao789221e2009-06-02 14:29:36 -0700149 u8 status;
Marcel Holtmann93742532009-06-13 07:40:18 +0200150 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700151
152 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
153 status = sdio_readb(card->func, CARD_STATUS_REG, &ret);
154 if (ret)
155 goto failed;
156 if ((status & bits) == bits)
Marcel Holtmann93742532009-06-13 07:40:18 +0200157 return ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700158
159 udelay(1);
160 }
161
162 ret = -ETIMEDOUT;
163
164failed:
165 BT_ERR("FAILED! ret=%d", ret);
166
Bing Zhao789221e2009-06-02 14:29:36 -0700167 return ret;
168}
169
170static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200171 int pollnum)
Bing Zhao789221e2009-06-02 14:29:36 -0700172{
173 int ret = -ETIMEDOUT;
174 u16 firmwarestat;
175 unsigned int tries;
176
Bing Zhao789221e2009-06-02 14:29:36 -0700177 /* Wait for firmware to become ready */
178 for (tries = 0; tries < pollnum; tries++) {
179 if (btmrvl_sdio_read_fw_status(card, &firmwarestat) < 0)
180 continue;
181
182 if (firmwarestat == FIRMWARE_READY) {
183 ret = 0;
184 break;
185 } else {
186 msleep(10);
187 }
188 }
189
Bing Zhao789221e2009-06-02 14:29:36 -0700190 return ret;
191}
192
193static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
194{
195 const struct firmware *fw_helper = NULL;
196 const u8 *helper = NULL;
197 int ret;
198 void *tmphlprbuf = NULL;
199 int tmphlprbufsz, hlprblknow, helperlen;
200 u8 *helperbuf;
201 u32 tx_len;
202
Bing Zhao789221e2009-06-02 14:29:36 -0700203 ret = request_firmware(&fw_helper, card->helper,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200204 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700205 if ((ret < 0) || !fw_helper) {
206 BT_ERR("request_firmware(helper) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200207 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700208 ret = -ENOENT;
209 goto done;
210 }
211
212 helper = fw_helper->data;
213 helperlen = fw_helper->size;
214
215 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200216 helperlen, SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700217
218 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
219
220 tmphlprbuf = kmalloc(tmphlprbufsz, GFP_KERNEL);
221 if (!tmphlprbuf) {
222 BT_ERR("Unable to allocate buffer for helper."
223 " Terminating download");
224 ret = -ENOMEM;
225 goto done;
226 }
227
228 memset(tmphlprbuf, 0, tmphlprbufsz);
229
230 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
231
232 /* Perform helper data transfer */
233 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
234 - SDIO_HEADER_LEN;
235 hlprblknow = 0;
236
237 do {
238 ret = btmrvl_sdio_poll_card_status(card,
239 CARD_IO_READY | DN_LD_CARD_RDY);
240 if (ret < 0) {
241 BT_ERR("Helper download poll status timeout @ %d",
242 hlprblknow);
243 goto done;
244 }
245
246 /* Check if there is more data? */
247 if (hlprblknow >= helperlen)
248 break;
249
250 if (helperlen - hlprblknow < tx_len)
251 tx_len = helperlen - hlprblknow;
252
253 /* Little-endian */
254 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
255 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
256 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
257 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
258
259 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
260 tx_len);
261
262 /* Now send the data */
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200263 ret = sdio_writesb(card->func, card->ioport, helperbuf,
264 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700265 if (ret < 0) {
266 BT_ERR("IO error during helper download @ %d",
267 hlprblknow);
268 goto done;
269 }
270
271 hlprblknow += tx_len;
272 } while (true);
273
274 BT_DBG("Transferring helper image EOF block");
275
276 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
277
278 ret = sdio_writesb(card->func, card->ioport, helperbuf,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200279 SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700280 if (ret < 0) {
281 BT_ERR("IO error in writing helper image EOF block");
282 goto done;
283 }
284
285 ret = 0;
286
287done:
288 kfree(tmphlprbuf);
289 if (fw_helper)
290 release_firmware(fw_helper);
291
Bing Zhao789221e2009-06-02 14:29:36 -0700292 return ret;
293}
294
295static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
296{
297 const struct firmware *fw_firmware = NULL;
298 const u8 *firmware = NULL;
299 int firmwarelen, tmpfwbufsz, ret;
300 unsigned int tries, offset;
301 u8 base0, base1;
302 void *tmpfwbuf = NULL;
303 u8 *fwbuf;
304 u16 len;
305 int txlen = 0, tx_blocks = 0, count = 0;
306
Bing Zhao789221e2009-06-02 14:29:36 -0700307 ret = request_firmware(&fw_firmware, card->firmware,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200308 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700309 if ((ret < 0) || !fw_firmware) {
310 BT_ERR("request_firmware(firmware) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200311 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700312 ret = -ENOENT;
313 goto done;
314 }
315
316 firmware = fw_firmware->data;
317 firmwarelen = fw_firmware->size;
318
319 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
320
321 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
322 tmpfwbuf = kmalloc(tmpfwbufsz, GFP_KERNEL);
323 if (!tmpfwbuf) {
324 BT_ERR("Unable to allocate buffer for firmware."
325 " Terminating download");
326 ret = -ENOMEM;
327 goto done;
328 }
329
330 memset(tmpfwbuf, 0, tmpfwbufsz);
331
332 /* Ensure aligned firmware buffer */
333 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
334
335 /* Perform firmware data transfer */
336 offset = 0;
337 do {
338 ret = btmrvl_sdio_poll_card_status(card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200339 CARD_IO_READY | DN_LD_CARD_RDY);
Bing Zhao789221e2009-06-02 14:29:36 -0700340 if (ret < 0) {
341 BT_ERR("FW download with helper poll status"
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200342 " timeout @ %d", offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700343 goto done;
344 }
345
346 /* Check if there is more data ? */
347 if (offset >= firmwarelen)
348 break;
349
350 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
351 base0 = sdio_readb(card->func,
352 SQ_READ_BASE_ADDRESS_A0_REG, &ret);
353 if (ret) {
354 BT_ERR("BASE0 register read failed:"
355 " base0 = 0x%04X(%d)."
356 " Terminating download",
357 base0, base0);
358 ret = -EIO;
359 goto done;
360 }
361 base1 = sdio_readb(card->func,
362 SQ_READ_BASE_ADDRESS_A1_REG, &ret);
363 if (ret) {
364 BT_ERR("BASE1 register read failed:"
365 " base1 = 0x%04X(%d)."
366 " Terminating download",
367 base1, base1);
368 ret = -EIO;
369 goto done;
370 }
371
372 len = (((u16) base1) << 8) | base0;
373 if (len)
374 break;
375
376 udelay(10);
377 }
378
379 if (!len)
380 break;
381 else if (len > BTM_UPLD_SIZE) {
382 BT_ERR("FW download failure @%d, invalid length %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200383 offset, len);
Bing Zhao789221e2009-06-02 14:29:36 -0700384 ret = -EINVAL;
385 goto done;
386 }
387
388 txlen = len;
389
390 if (len & BIT(0)) {
391 count++;
392 if (count > MAX_WRITE_IOMEM_RETRY) {
393 BT_ERR("FW download failure @%d, "
394 "over max retry count", offset);
395 ret = -EIO;
396 goto done;
397 }
398 BT_ERR("FW CRC error indicated by the helper: "
399 "len = 0x%04X, txlen = %d", len, txlen);
400 len &= ~BIT(0);
401 /* Set txlen to 0 so as to resend from same offset */
402 txlen = 0;
403 } else {
404 count = 0;
405
406 /* Last block ? */
407 if (firmwarelen - offset < txlen)
408 txlen = firmwarelen - offset;
409
410 tx_blocks =
411 (txlen + SDIO_BLOCK_SIZE - 1) / SDIO_BLOCK_SIZE;
412
413 memcpy(fwbuf, &firmware[offset], txlen);
414 }
415
416 ret = sdio_writesb(card->func, card->ioport, fwbuf,
417 tx_blocks * SDIO_BLOCK_SIZE);
418
419 if (ret < 0) {
420 BT_ERR("FW download, writesb(%d) failed @%d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200421 count, offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700422 sdio_writeb(card->func, HOST_CMD53_FIN, CONFIG_REG,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200423 &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700424 if (ret)
425 BT_ERR("writeb failed (CFG)");
426 }
427
428 offset += txlen;
429 } while (true);
430
431 BT_DBG("FW download over, size %d bytes", offset);
432
433 ret = 0;
434
435done:
436 kfree(tmpfwbuf);
437
438 if (fw_firmware)
439 release_firmware(fw_firmware);
440
Bing Zhao789221e2009-06-02 14:29:36 -0700441 return ret;
442}
443
444static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
445{
446 u16 buf_len = 0;
447 int ret, buf_block_len, blksz;
448 struct sk_buff *skb = NULL;
449 u32 type;
450 u8 *payload = NULL;
451 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
452 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
453
Bing Zhao789221e2009-06-02 14:29:36 -0700454 if (!card || !card->func) {
455 BT_ERR("card or function is NULL!");
456 ret = -EINVAL;
457 goto exit;
458 }
459
460 /* Read the length of data to be transferred */
461 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
462 if (ret < 0) {
463 BT_ERR("read rx_len failed");
464 ret = -EIO;
465 goto exit;
466 }
467
468 blksz = SDIO_BLOCK_SIZE;
469 buf_block_len = (buf_len + blksz - 1) / blksz;
470
471 if (buf_len <= SDIO_HEADER_LEN
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200472 || (buf_block_len * blksz) > ALLOC_BUF_SIZE) {
Bing Zhao789221e2009-06-02 14:29:36 -0700473 BT_ERR("invalid packet length: %d", buf_len);
474 ret = -EINVAL;
475 goto exit;
476 }
477
478 /* Allocate buffer */
479 skb = bt_skb_alloc(buf_block_len * blksz + BTSDIO_DMA_ALIGN,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200480 GFP_ATOMIC);
Bing Zhao789221e2009-06-02 14:29:36 -0700481 if (skb == NULL) {
482 BT_ERR("No free skb");
483 goto exit;
484 }
485
Bing Zhao3318b232009-07-08 11:44:14 -0700486 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
487 skb_put(skb, (unsigned long) skb->data &
488 (BTSDIO_DMA_ALIGN - 1));
489 skb_pull(skb, (unsigned long) skb->data &
490 (BTSDIO_DMA_ALIGN - 1));
Bing Zhao789221e2009-06-02 14:29:36 -0700491 }
492
Bing Zhao3318b232009-07-08 11:44:14 -0700493 payload = skb->data;
Bing Zhao789221e2009-06-02 14:29:36 -0700494
495 ret = sdio_readsb(card->func, payload, card->ioport,
496 buf_block_len * blksz);
497 if (ret < 0) {
498 BT_ERR("readsb failed: %d", ret);
499 ret = -EIO;
500 goto exit;
501 }
502
503 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
504 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
505 */
506
507 buf_len = payload[0];
508 buf_len |= (u16) payload[1] << 8;
509 type = payload[3];
510
511 switch (type) {
512 case HCI_ACLDATA_PKT:
513 case HCI_SCODATA_PKT:
514 case HCI_EVENT_PKT:
515 bt_cb(skb)->pkt_type = type;
516 skb->dev = (void *)hdev;
517 skb_put(skb, buf_len);
518 skb_pull(skb, SDIO_HEADER_LEN);
519
520 if (type == HCI_EVENT_PKT)
521 btmrvl_check_evtpkt(priv, skb);
522
523 hci_recv_frame(skb);
524 hdev->stat.byte_rx += buf_len;
525 break;
526
527 case MRVL_VENDOR_PKT:
528 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
529 skb->dev = (void *)hdev;
530 skb_put(skb, buf_len);
531 skb_pull(skb, SDIO_HEADER_LEN);
532
533 if (btmrvl_process_event(priv, skb))
534 hci_recv_frame(skb);
535
536 hdev->stat.byte_rx += buf_len;
537 break;
538
539 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200540 BT_ERR("Unknown packet type:%d", type);
Bing Zhao789221e2009-06-02 14:29:36 -0700541 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, payload,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200542 blksz * buf_block_len);
Bing Zhao789221e2009-06-02 14:29:36 -0700543
544 kfree_skb(skb);
545 skb = NULL;
546 break;
547 }
548
549exit:
550 if (ret) {
551 hdev->stat.err_rx++;
552 if (skb)
553 kfree_skb(skb);
554 }
555
Bing Zhao789221e2009-06-02 14:29:36 -0700556 return ret;
557}
558
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700559static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
Bing Zhao789221e2009-06-02 14:29:36 -0700560{
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700561 ulong flags;
562 u8 ireg;
Bing Zhao789221e2009-06-02 14:29:36 -0700563 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
564
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700565 spin_lock_irqsave(&priv->driver_lock, flags);
566 ireg = sdio_ireg;
567 sdio_ireg = 0;
568 spin_unlock_irqrestore(&priv->driver_lock, flags);
Bing Zhao789221e2009-06-02 14:29:36 -0700569
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700570 sdio_claim_host(card->func);
571 if (ireg & DN_LD_HOST_INT_STATUS) {
Bing Zhao789221e2009-06-02 14:29:36 -0700572 if (priv->btmrvl_dev.tx_dnld_rdy)
573 BT_DBG("tx_done already received: "
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700574 " int_status=0x%x", ireg);
Bing Zhao789221e2009-06-02 14:29:36 -0700575 else
576 priv->btmrvl_dev.tx_dnld_rdy = true;
577 }
578
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700579 if (ireg & UP_LD_HOST_INT_STATUS)
Bing Zhao789221e2009-06-02 14:29:36 -0700580 btmrvl_sdio_card_to_host(priv);
581
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700582 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -0700583
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700584 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700585}
586
587static void btmrvl_sdio_interrupt(struct sdio_func *func)
588{
589 struct btmrvl_private *priv;
Bing Zhao789221e2009-06-02 14:29:36 -0700590 struct btmrvl_sdio_card *card;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700591 ulong flags;
Bing Zhao789221e2009-06-02 14:29:36 -0700592 u8 ireg = 0;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700593 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700594
Bing Zhao789221e2009-06-02 14:29:36 -0700595 card = sdio_get_drvdata(func);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700596 if (!card || !card->priv) {
597 BT_ERR("sbi_interrupt(%p) card or priv is "
598 "NULL, card=%p\n", func, card);
599 return;
Bing Zhao789221e2009-06-02 14:29:36 -0700600 }
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700601
602 priv = card->priv;
603
604 ireg = sdio_readb(card->func, HOST_INTSTATUS_REG, &ret);
605 if (ret) {
606 BT_ERR("sdio_readb: read int status register failed");
607 return;
608 }
609
610 if (ireg != 0) {
611 /*
612 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
613 * Clear the interrupt status register and re-enable the
614 * interrupt.
615 */
616 BT_DBG("ireg = 0x%x", ireg);
617
618 sdio_writeb(card->func, ~(ireg) & (DN_LD_HOST_INT_STATUS |
619 UP_LD_HOST_INT_STATUS),
620 HOST_INTSTATUS_REG, &ret);
621 if (ret) {
622 BT_ERR("sdio_writeb: clear int status register failed");
623 return;
624 }
625 }
626
627 spin_lock_irqsave(&priv->driver_lock, flags);
628 sdio_ireg |= ireg;
629 spin_unlock_irqrestore(&priv->driver_lock, flags);
630
631 btmrvl_interrupt(priv);
Bing Zhao789221e2009-06-02 14:29:36 -0700632}
633
634static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
635{
Bing Zhao789221e2009-06-02 14:29:36 -0700636 struct sdio_func *func;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200637 u8 reg;
638 int ret = 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700639
Bing Zhao789221e2009-06-02 14:29:36 -0700640 if (!card || !card->func) {
641 BT_ERR("Error: card or function is NULL!");
642 ret = -EINVAL;
643 goto failed;
644 }
645
646 func = card->func;
647
Bing Zhao789221e2009-06-02 14:29:36 -0700648 sdio_claim_host(func);
649
650 ret = sdio_enable_func(func);
651 if (ret) {
652 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
653 ret = -EIO;
654 goto release_host;
655 }
656
657 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
658 if (ret) {
659 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
660 ret = -EIO;
661 goto disable_func;
662 }
663
664 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
665 if (ret) {
666 BT_ERR("cannot set SDIO block size");
667 ret = -EIO;
668 goto release_irq;
669 }
670
671 reg = sdio_readb(func, IO_PORT_0_REG, &ret);
672 if (ret < 0) {
673 ret = -EIO;
674 goto release_irq;
675 }
676
677 card->ioport = reg;
678
679 reg = sdio_readb(func, IO_PORT_1_REG, &ret);
680 if (ret < 0) {
681 ret = -EIO;
682 goto release_irq;
683 }
684
685 card->ioport |= (reg << 8);
686
687 reg = sdio_readb(func, IO_PORT_2_REG, &ret);
688 if (ret < 0) {
689 ret = -EIO;
690 goto release_irq;
691 }
692
693 card->ioport |= (reg << 16);
694
695 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
696
697 sdio_set_drvdata(func, card);
698
699 sdio_release_host(func);
700
Bing Zhao789221e2009-06-02 14:29:36 -0700701 return 0;
702
703release_irq:
704 sdio_release_irq(func);
705
706disable_func:
707 sdio_disable_func(func);
708
709release_host:
710 sdio_release_host(func);
711
712failed:
Bing Zhao789221e2009-06-02 14:29:36 -0700713 return ret;
714}
715
716static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
717{
Bing Zhao789221e2009-06-02 14:29:36 -0700718 if (card && card->func) {
719 sdio_claim_host(card->func);
720 sdio_release_irq(card->func);
721 sdio_disable_func(card->func);
722 sdio_release_host(card->func);
723 sdio_set_drvdata(card->func, NULL);
724 }
725
Bing Zhao789221e2009-06-02 14:29:36 -0700726 return 0;
727}
728
729static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
730{
731 int ret;
732
Marcel Holtmann93742532009-06-13 07:40:18 +0200733 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700734 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700735
736 sdio_claim_host(card->func);
737
738 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
739
740 btmrvl_sdio_get_rx_unit(card);
741
742 sdio_release_host(card->func);
743
Bing Zhao789221e2009-06-02 14:29:36 -0700744 return ret;
745}
746
747static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
748{
749 int ret;
750
Marcel Holtmann93742532009-06-13 07:40:18 +0200751 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700752 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700753
754 sdio_claim_host(card->func);
755
756 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
757
758 sdio_release_host(card->func);
759
Bing Zhao789221e2009-06-02 14:29:36 -0700760 return ret;
761}
762
763static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
764 u8 *payload, u16 nb)
765{
766 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
767 int ret = 0;
768 int buf_block_len;
769 int blksz;
770 int i = 0;
771 u8 *buf = NULL;
772 void *tmpbuf = NULL;
773 int tmpbufsz;
774
Bing Zhao789221e2009-06-02 14:29:36 -0700775 if (!card || !card->func) {
776 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700777 return -EINVAL;
778 }
779
780 buf = payload;
Bing Zhao3318b232009-07-08 11:44:14 -0700781 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
Bing Zhao789221e2009-06-02 14:29:36 -0700782 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
Julia Lawall59598092009-08-06 22:05:18 +0200783 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
784 if (!tmpbuf)
785 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700786 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
787 memcpy(buf, payload, nb);
788 }
789
790 blksz = SDIO_BLOCK_SIZE;
791 buf_block_len = (nb + blksz - 1) / blksz;
792
793 sdio_claim_host(card->func);
794
795 do {
796 /* Transfer data to card */
797 ret = sdio_writesb(card->func, card->ioport, buf,
798 buf_block_len * blksz);
799 if (ret < 0) {
800 i++;
801 BT_ERR("i=%d writesb failed: %d", i, ret);
802 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
803 payload, nb);
804 ret = -EIO;
805 if (i > MAX_WRITE_IOMEM_RETRY)
806 goto exit;
807 }
808 } while (ret);
809
810 priv->btmrvl_dev.tx_dnld_rdy = false;
811
812exit:
813 sdio_release_host(card->func);
Yoichi Yuasa88d1a0c2010-02-04 09:55:51 +0900814 kfree(tmpbuf);
Bing Zhao789221e2009-06-02 14:29:36 -0700815
Bing Zhao789221e2009-06-02 14:29:36 -0700816 return ret;
817}
818
819static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
820{
821 int ret = 0;
822
Bing Zhao789221e2009-06-02 14:29:36 -0700823 if (!card || !card->func) {
824 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700825 return -EINVAL;
826 }
827 sdio_claim_host(card->func);
828
829 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
830 BT_DBG("Firmware already downloaded!");
831 goto done;
832 }
833
834 ret = btmrvl_sdio_download_helper(card);
835 if (ret) {
836 BT_ERR("Failed to download helper!");
837 ret = -EIO;
838 goto done;
839 }
840
841 if (btmrvl_sdio_download_fw_w_helper(card)) {
842 BT_ERR("Failed to download firmware!");
843 ret = -EIO;
844 goto done;
845 }
846
847 if (btmrvl_sdio_verify_fw_download(card, MAX_POLL_TRIES)) {
848 BT_ERR("FW failed to be active in time!");
849 ret = -ETIMEDOUT;
850 goto done;
851 }
852
853done:
854 sdio_release_host(card->func);
855
Bing Zhao789221e2009-06-02 14:29:36 -0700856 return ret;
857}
858
859static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
860{
861 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
862 int ret = 0;
863
Bing Zhao789221e2009-06-02 14:29:36 -0700864 if (!card || !card->func) {
865 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700866 return -EINVAL;
867 }
868
869 sdio_claim_host(card->func);
870
871 sdio_writeb(card->func, HOST_POWER_UP, CONFIG_REG, &ret);
872
873 sdio_release_host(card->func);
874
875 BT_DBG("wake up firmware");
876
Bing Zhao789221e2009-06-02 14:29:36 -0700877 return ret;
878}
879
880static int btmrvl_sdio_probe(struct sdio_func *func,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200881 const struct sdio_device_id *id)
Bing Zhao789221e2009-06-02 14:29:36 -0700882{
883 int ret = 0;
884 struct btmrvl_private *priv = NULL;
885 struct btmrvl_sdio_card *card = NULL;
886
Bing Zhao789221e2009-06-02 14:29:36 -0700887 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
888 id->vendor, id->device, id->class, func->num);
889
890 card = kzalloc(sizeof(*card), GFP_KERNEL);
891 if (!card) {
892 ret = -ENOMEM;
893 goto done;
894 }
895
896 card->func = func;
897
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200898 if (id->driver_data) {
899 struct btmrvl_sdio_device *data = (void *) id->driver_data;
900 card->helper = data->helper;
901 card->firmware = data->firmware;
902 }
903
Bing Zhao789221e2009-06-02 14:29:36 -0700904 if (btmrvl_sdio_register_dev(card) < 0) {
905 BT_ERR("Failed to register BT device!");
906 ret = -ENODEV;
907 goto free_card;
908 }
909
910 /* Disable the interrupts on the card */
911 btmrvl_sdio_disable_host_int(card);
912
913 if (btmrvl_sdio_download_fw(card)) {
914 BT_ERR("Downloading firmware failed!");
915 ret = -ENODEV;
916 goto unreg_dev;
917 }
918
919 msleep(100);
920
921 btmrvl_sdio_enable_host_int(card);
922
923 priv = btmrvl_add_card(card);
924 if (!priv) {
925 BT_ERR("Initializing card failed!");
926 ret = -ENODEV;
927 goto disable_host_int;
928 }
929
930 card->priv = priv;
931
932 /* Initialize the interface specific function pointers */
933 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
934 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700935 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
Bing Zhao789221e2009-06-02 14:29:36 -0700936
Bing Zhao64061602010-03-03 14:37:36 -0800937 if (btmrvl_register_hdev(priv)) {
938 BT_ERR("Register hdev failed!");
939 ret = -ENODEV;
940 goto disable_host_int;
941 }
942
Amitkumar Karwar32fd6392009-09-30 11:18:30 -0700943 priv->btmrvl_dev.psmode = 1;
944 btmrvl_enable_ps(priv);
Bing Zhao789221e2009-06-02 14:29:36 -0700945
Bing Zhao789221e2009-06-02 14:29:36 -0700946 return 0;
947
948disable_host_int:
949 btmrvl_sdio_disable_host_int(card);
950unreg_dev:
951 btmrvl_sdio_unregister_dev(card);
952free_card:
953 kfree(card);
954done:
Bing Zhao789221e2009-06-02 14:29:36 -0700955 return ret;
956}
957
958static void btmrvl_sdio_remove(struct sdio_func *func)
959{
960 struct btmrvl_sdio_card *card;
961
Bing Zhao789221e2009-06-02 14:29:36 -0700962 if (func) {
963 card = sdio_get_drvdata(func);
964 if (card) {
965 /* Send SHUTDOWN command & disable interrupt
966 * if user removes the module.
967 */
968 if (user_rmmod) {
969 btmrvl_send_module_cfg_cmd(card->priv,
970 MODULE_SHUTDOWN_REQ);
971 btmrvl_sdio_disable_host_int(card);
972 }
973 BT_DBG("unregester dev");
974 btmrvl_sdio_unregister_dev(card);
975 btmrvl_remove_card(card->priv);
976 kfree(card);
977 }
978 }
Bing Zhao789221e2009-06-02 14:29:36 -0700979}
980
981static struct sdio_driver bt_mrvl_sdio = {
982 .name = "btmrvl_sdio",
983 .id_table = btmrvl_sdio_ids,
984 .probe = btmrvl_sdio_probe,
985 .remove = btmrvl_sdio_remove,
986};
987
Peter Huewe8e5b2302009-12-22 09:34:20 +0100988static int __init btmrvl_sdio_init_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -0700989{
Bing Zhao789221e2009-06-02 14:29:36 -0700990 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
991 BT_ERR("SDIO Driver Registration Failed");
Bing Zhao789221e2009-06-02 14:29:36 -0700992 return -ENODEV;
993 }
994
995 /* Clear the flag in case user removes the card. */
996 user_rmmod = 0;
997
Bing Zhao789221e2009-06-02 14:29:36 -0700998 return 0;
999}
1000
Peter Huewe8e5b2302009-12-22 09:34:20 +01001001static void __exit btmrvl_sdio_exit_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001002{
Bing Zhao789221e2009-06-02 14:29:36 -07001003 /* Set the flag as user is removing this module. */
1004 user_rmmod = 1;
1005
1006 sdio_unregister_driver(&bt_mrvl_sdio);
Bing Zhao789221e2009-06-02 14:29:36 -07001007}
1008
1009module_init(btmrvl_sdio_init_module);
1010module_exit(btmrvl_sdio_exit_module);
1011
1012MODULE_AUTHOR("Marvell International Ltd.");
Marcel Holtmann9666fb32009-06-09 21:45:04 +02001013MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
Bing Zhao789221e2009-06-02 14:29:36 -07001014MODULE_VERSION(VERSION);
1015MODULE_LICENSE("GPL v2");
Ben Hutchings28614532009-11-07 21:41:18 +00001016MODULE_FIRMWARE("sd8688_helper.bin");
1017MODULE_FIRMWARE("sd8688.bin");