btmrvl_sdio.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. /**
  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. #include <linux/firmware.h>
  21. #include <linux/slab.h>
  22. #include <linux/mmc/sdio_ids.h>
  23. #include <linux/mmc/sdio_func.h>
  24. #include <net/bluetooth/bluetooth.h>
  25. #include <net/bluetooth/hci_core.h>
  26. #include "btmrvl_drv.h"
  27. #include "btmrvl_sdio.h"
  28. #define VERSION "1.0"
  29. /* The btmrvl_sdio_remove() callback function is called
  30. * when user removes this module from kernel space or ejects
  31. * the card from the slot. The driver handles these 2 cases
  32. * differently.
  33. * If the user is removing the module, a MODULE_SHUTDOWN_REQ
  34. * command is sent to firmware and interrupt will be disabled.
  35. * If the card is removed, there is no need to send command
  36. * or disable interrupt.
  37. *
  38. * The variable 'user_rmmod' is used to distinguish these two
  39. * scenarios. This flag is initialized as FALSE in case the card
  40. * is removed, and will be set to TRUE for module removal when
  41. * module_exit function is called.
  42. */
  43. static u8 user_rmmod;
  44. static u8 sdio_ireg;
  45. static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
  46. .cfg = 0x03,
  47. .host_int_mask = 0x04,
  48. .host_intstatus = 0x05,
  49. .card_status = 0x20,
  50. .sq_read_base_addr_a0 = 0x10,
  51. .sq_read_base_addr_a1 = 0x11,
  52. .card_fw_status0 = 0x40,
  53. .card_fw_status1 = 0x41,
  54. .card_rx_len = 0x42,
  55. .card_rx_unit = 0x43,
  56. .io_port_0 = 0x00,
  57. .io_port_1 = 0x01,
  58. .io_port_2 = 0x02,
  59. };
  60. static const struct btmrvl_sdio_card_reg btmrvl_reg_8787 = {
  61. .cfg = 0x00,
  62. .host_int_mask = 0x02,
  63. .host_intstatus = 0x03,
  64. .card_status = 0x30,
  65. .sq_read_base_addr_a0 = 0x40,
  66. .sq_read_base_addr_a1 = 0x41,
  67. .card_revision = 0x5c,
  68. .card_fw_status0 = 0x60,
  69. .card_fw_status1 = 0x61,
  70. .card_rx_len = 0x62,
  71. .card_rx_unit = 0x63,
  72. .io_port_0 = 0x78,
  73. .io_port_1 = 0x79,
  74. .io_port_2 = 0x7a,
  75. };
  76. static const struct btmrvl_sdio_device btmrvl_sdio_sd6888 = {
  77. .helper = "sd8688_helper.bin",
  78. .firmware = "sd8688.bin",
  79. .reg = &btmrvl_reg_8688,
  80. .sd_blksz_fw_dl = 64,
  81. };
  82. static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
  83. .helper = NULL,
  84. .firmware = "mrvl/sd8787_uapsta.bin",
  85. .reg = &btmrvl_reg_8787,
  86. .sd_blksz_fw_dl = 256,
  87. };
  88. static const struct sdio_device_id btmrvl_sdio_ids[] = {
  89. /* Marvell SD8688 Bluetooth device */
  90. { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
  91. .driver_data = (unsigned long) &btmrvl_sdio_sd6888 },
  92. /* Marvell SD8787 Bluetooth device */
  93. { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
  94. .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
  95. { } /* Terminating entry */
  96. };
  97. MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
  98. static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
  99. {
  100. u8 reg;
  101. int ret;
  102. reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
  103. if (!ret)
  104. card->rx_unit = reg;
  105. return ret;
  106. }
  107. static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
  108. {
  109. u8 fws0, fws1;
  110. int ret;
  111. *dat = 0;
  112. fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
  113. if (ret)
  114. return -EIO;
  115. fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
  116. if (ret)
  117. return -EIO;
  118. *dat = (((u16) fws1) << 8) | fws0;
  119. return 0;
  120. }
  121. static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
  122. {
  123. u8 reg;
  124. int ret;
  125. reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
  126. if (!ret)
  127. *dat = (u16) reg << card->rx_unit;
  128. return ret;
  129. }
  130. static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
  131. u8 mask)
  132. {
  133. int ret;
  134. sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
  135. if (ret) {
  136. BT_ERR("Unable to enable the host interrupt!");
  137. ret = -EIO;
  138. }
  139. return ret;
  140. }
  141. static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
  142. u8 mask)
  143. {
  144. u8 host_int_mask;
  145. int ret;
  146. host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
  147. if (ret)
  148. return -EIO;
  149. host_int_mask &= ~mask;
  150. sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
  151. if (ret < 0) {
  152. BT_ERR("Unable to disable the host interrupt!");
  153. return -EIO;
  154. }
  155. return 0;
  156. }
  157. static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
  158. {
  159. unsigned int tries;
  160. u8 status;
  161. int ret;
  162. for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
  163. status = sdio_readb(card->func, card->reg->card_status, &ret);
  164. if (ret)
  165. goto failed;
  166. if ((status & bits) == bits)
  167. return ret;
  168. udelay(1);
  169. }
  170. ret = -ETIMEDOUT;
  171. failed:
  172. BT_ERR("FAILED! ret=%d", ret);
  173. return ret;
  174. }
  175. static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
  176. int pollnum)
  177. {
  178. int ret = -ETIMEDOUT;
  179. u16 firmwarestat;
  180. unsigned int tries;
  181. /* Wait for firmware to become ready */
  182. for (tries = 0; tries < pollnum; tries++) {
  183. if (btmrvl_sdio_read_fw_status(card, &firmwarestat) < 0)
  184. continue;
  185. if (firmwarestat == FIRMWARE_READY) {
  186. ret = 0;
  187. break;
  188. } else {
  189. msleep(10);
  190. }
  191. }
  192. return ret;
  193. }
  194. static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
  195. {
  196. const struct firmware *fw_helper = NULL;
  197. const u8 *helper = NULL;
  198. int ret;
  199. void *tmphlprbuf = NULL;
  200. int tmphlprbufsz, hlprblknow, helperlen;
  201. u8 *helperbuf;
  202. u32 tx_len;
  203. ret = request_firmware(&fw_helper, card->helper,
  204. &card->func->dev);
  205. if ((ret < 0) || !fw_helper) {
  206. BT_ERR("request_firmware(helper) failed, error code = %d",
  207. ret);
  208. ret = -ENOENT;
  209. goto done;
  210. }
  211. helper = fw_helper->data;
  212. helperlen = fw_helper->size;
  213. BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
  214. helperlen, SDIO_BLOCK_SIZE);
  215. tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
  216. tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
  217. if (!tmphlprbuf) {
  218. BT_ERR("Unable to allocate buffer for helper."
  219. " Terminating download");
  220. ret = -ENOMEM;
  221. goto done;
  222. }
  223. helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
  224. /* Perform helper data transfer */
  225. tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
  226. - SDIO_HEADER_LEN;
  227. hlprblknow = 0;
  228. do {
  229. ret = btmrvl_sdio_poll_card_status(card,
  230. CARD_IO_READY | DN_LD_CARD_RDY);
  231. if (ret < 0) {
  232. BT_ERR("Helper download poll status timeout @ %d",
  233. hlprblknow);
  234. goto done;
  235. }
  236. /* Check if there is more data? */
  237. if (hlprblknow >= helperlen)
  238. break;
  239. if (helperlen - hlprblknow < tx_len)
  240. tx_len = helperlen - hlprblknow;
  241. /* Little-endian */
  242. helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
  243. helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
  244. helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
  245. helperbuf[3] = ((tx_len & 0xff000000) >> 24);
  246. memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
  247. tx_len);
  248. /* Now send the data */
  249. ret = sdio_writesb(card->func, card->ioport, helperbuf,
  250. FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
  251. if (ret < 0) {
  252. BT_ERR("IO error during helper download @ %d",
  253. hlprblknow);
  254. goto done;
  255. }
  256. hlprblknow += tx_len;
  257. } while (true);
  258. BT_DBG("Transferring helper image EOF block");
  259. memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
  260. ret = sdio_writesb(card->func, card->ioport, helperbuf,
  261. SDIO_BLOCK_SIZE);
  262. if (ret < 0) {
  263. BT_ERR("IO error in writing helper image EOF block");
  264. goto done;
  265. }
  266. ret = 0;
  267. done:
  268. kfree(tmphlprbuf);
  269. if (fw_helper)
  270. release_firmware(fw_helper);
  271. return ret;
  272. }
  273. static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
  274. {
  275. const struct firmware *fw_firmware = NULL;
  276. const u8 *firmware = NULL;
  277. int firmwarelen, tmpfwbufsz, ret;
  278. unsigned int tries, offset;
  279. u8 base0, base1;
  280. void *tmpfwbuf = NULL;
  281. u8 *fwbuf;
  282. u16 len, blksz_dl = card->sd_blksz_fw_dl;
  283. int txlen = 0, tx_blocks = 0, count = 0;
  284. ret = request_firmware(&fw_firmware, card->firmware,
  285. &card->func->dev);
  286. if ((ret < 0) || !fw_firmware) {
  287. BT_ERR("request_firmware(firmware) failed, error code = %d",
  288. ret);
  289. ret = -ENOENT;
  290. goto done;
  291. }
  292. firmware = fw_firmware->data;
  293. firmwarelen = fw_firmware->size;
  294. BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
  295. tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
  296. tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
  297. if (!tmpfwbuf) {
  298. BT_ERR("Unable to allocate buffer for firmware."
  299. " Terminating download");
  300. ret = -ENOMEM;
  301. goto done;
  302. }
  303. /* Ensure aligned firmware buffer */
  304. fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
  305. /* Perform firmware data transfer */
  306. offset = 0;
  307. do {
  308. ret = btmrvl_sdio_poll_card_status(card,
  309. CARD_IO_READY | DN_LD_CARD_RDY);
  310. if (ret < 0) {
  311. BT_ERR("FW download with helper poll status"
  312. " timeout @ %d", offset);
  313. goto done;
  314. }
  315. /* Check if there is more data ? */
  316. if (offset >= firmwarelen)
  317. break;
  318. for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  319. base0 = sdio_readb(card->func,
  320. card->reg->sq_read_base_addr_a0, &ret);
  321. if (ret) {
  322. BT_ERR("BASE0 register read failed:"
  323. " base0 = 0x%04X(%d)."
  324. " Terminating download",
  325. base0, base0);
  326. ret = -EIO;
  327. goto done;
  328. }
  329. base1 = sdio_readb(card->func,
  330. card->reg->sq_read_base_addr_a1, &ret);
  331. if (ret) {
  332. BT_ERR("BASE1 register read failed:"
  333. " base1 = 0x%04X(%d)."
  334. " Terminating download",
  335. base1, base1);
  336. ret = -EIO;
  337. goto done;
  338. }
  339. len = (((u16) base1) << 8) | base0;
  340. if (len)
  341. break;
  342. udelay(10);
  343. }
  344. if (!len)
  345. break;
  346. else if (len > BTM_UPLD_SIZE) {
  347. BT_ERR("FW download failure @%d, invalid length %d",
  348. offset, len);
  349. ret = -EINVAL;
  350. goto done;
  351. }
  352. txlen = len;
  353. if (len & BIT(0)) {
  354. count++;
  355. if (count > MAX_WRITE_IOMEM_RETRY) {
  356. BT_ERR("FW download failure @%d, "
  357. "over max retry count", offset);
  358. ret = -EIO;
  359. goto done;
  360. }
  361. BT_ERR("FW CRC error indicated by the helper: "
  362. "len = 0x%04X, txlen = %d", len, txlen);
  363. len &= ~BIT(0);
  364. /* Set txlen to 0 so as to resend from same offset */
  365. txlen = 0;
  366. } else {
  367. count = 0;
  368. /* Last block ? */
  369. if (firmwarelen - offset < txlen)
  370. txlen = firmwarelen - offset;
  371. tx_blocks = (txlen + blksz_dl - 1) / blksz_dl;
  372. memcpy(fwbuf, &firmware[offset], txlen);
  373. }
  374. ret = sdio_writesb(card->func, card->ioport, fwbuf,
  375. tx_blocks * blksz_dl);
  376. if (ret < 0) {
  377. BT_ERR("FW download, writesb(%d) failed @%d",
  378. count, offset);
  379. sdio_writeb(card->func, HOST_CMD53_FIN,
  380. card->reg->cfg, &ret);
  381. if (ret)
  382. BT_ERR("writeb failed (CFG)");
  383. }
  384. offset += txlen;
  385. } while (true);
  386. BT_DBG("FW download over, size %d bytes", offset);
  387. ret = 0;
  388. done:
  389. kfree(tmpfwbuf);
  390. if (fw_firmware)
  391. release_firmware(fw_firmware);
  392. return ret;
  393. }
  394. static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
  395. {
  396. u16 buf_len = 0;
  397. int ret, buf_block_len, blksz;
  398. struct sk_buff *skb = NULL;
  399. u32 type;
  400. u8 *payload = NULL;
  401. struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
  402. struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
  403. if (!card || !card->func) {
  404. BT_ERR("card or function is NULL!");
  405. ret = -EINVAL;
  406. goto exit;
  407. }
  408. /* Read the length of data to be transferred */
  409. ret = btmrvl_sdio_read_rx_len(card, &buf_len);
  410. if (ret < 0) {
  411. BT_ERR("read rx_len failed");
  412. ret = -EIO;
  413. goto exit;
  414. }
  415. blksz = SDIO_BLOCK_SIZE;
  416. buf_block_len = (buf_len + blksz - 1) / blksz;
  417. if (buf_len <= SDIO_HEADER_LEN
  418. || (buf_block_len * blksz) > ALLOC_BUF_SIZE) {
  419. BT_ERR("invalid packet length: %d", buf_len);
  420. ret = -EINVAL;
  421. goto exit;
  422. }
  423. /* Allocate buffer */
  424. skb = bt_skb_alloc(buf_block_len * blksz + BTSDIO_DMA_ALIGN,
  425. GFP_ATOMIC);
  426. if (skb == NULL) {
  427. BT_ERR("No free skb");
  428. goto exit;
  429. }
  430. if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
  431. skb_put(skb, (unsigned long) skb->data &
  432. (BTSDIO_DMA_ALIGN - 1));
  433. skb_pull(skb, (unsigned long) skb->data &
  434. (BTSDIO_DMA_ALIGN - 1));
  435. }
  436. payload = skb->data;
  437. ret = sdio_readsb(card->func, payload, card->ioport,
  438. buf_block_len * blksz);
  439. if (ret < 0) {
  440. BT_ERR("readsb failed: %d", ret);
  441. ret = -EIO;
  442. goto exit;
  443. }
  444. /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
  445. * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
  446. */
  447. buf_len = payload[0];
  448. buf_len |= (u16) payload[1] << 8;
  449. type = payload[3];
  450. switch (type) {
  451. case HCI_ACLDATA_PKT:
  452. case HCI_SCODATA_PKT:
  453. case HCI_EVENT_PKT:
  454. bt_cb(skb)->pkt_type = type;
  455. skb->dev = (void *)hdev;
  456. skb_put(skb, buf_len);
  457. skb_pull(skb, SDIO_HEADER_LEN);
  458. if (type == HCI_EVENT_PKT)
  459. btmrvl_check_evtpkt(priv, skb);
  460. hci_recv_frame(skb);
  461. hdev->stat.byte_rx += buf_len;
  462. break;
  463. case MRVL_VENDOR_PKT:
  464. bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
  465. skb->dev = (void *)hdev;
  466. skb_put(skb, buf_len);
  467. skb_pull(skb, SDIO_HEADER_LEN);
  468. if (btmrvl_process_event(priv, skb))
  469. hci_recv_frame(skb);
  470. hdev->stat.byte_rx += buf_len;
  471. break;
  472. default:
  473. BT_ERR("Unknown packet type:%d", type);
  474. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, payload,
  475. blksz * buf_block_len);
  476. kfree_skb(skb);
  477. skb = NULL;
  478. break;
  479. }
  480. exit:
  481. if (ret) {
  482. hdev->stat.err_rx++;
  483. if (skb)
  484. kfree_skb(skb);
  485. }
  486. return ret;
  487. }
  488. static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
  489. {
  490. ulong flags;
  491. u8 ireg;
  492. struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
  493. spin_lock_irqsave(&priv->driver_lock, flags);
  494. ireg = sdio_ireg;
  495. sdio_ireg = 0;
  496. spin_unlock_irqrestore(&priv->driver_lock, flags);
  497. sdio_claim_host(card->func);
  498. if (ireg & DN_LD_HOST_INT_STATUS) {
  499. if (priv->btmrvl_dev.tx_dnld_rdy)
  500. BT_DBG("tx_done already received: "
  501. " int_status=0x%x", ireg);
  502. else
  503. priv->btmrvl_dev.tx_dnld_rdy = true;
  504. }
  505. if (ireg & UP_LD_HOST_INT_STATUS)
  506. btmrvl_sdio_card_to_host(priv);
  507. sdio_release_host(card->func);
  508. return 0;
  509. }
  510. static void btmrvl_sdio_interrupt(struct sdio_func *func)
  511. {
  512. struct btmrvl_private *priv;
  513. struct btmrvl_sdio_card *card;
  514. ulong flags;
  515. u8 ireg = 0;
  516. int ret;
  517. card = sdio_get_drvdata(func);
  518. if (!card || !card->priv) {
  519. BT_ERR("sbi_interrupt(%p) card or priv is "
  520. "NULL, card=%p\n", func, card);
  521. return;
  522. }
  523. priv = card->priv;
  524. ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
  525. if (ret) {
  526. BT_ERR("sdio_readb: read int status register failed");
  527. return;
  528. }
  529. if (ireg != 0) {
  530. /*
  531. * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
  532. * Clear the interrupt status register and re-enable the
  533. * interrupt.
  534. */
  535. BT_DBG("ireg = 0x%x", ireg);
  536. sdio_writeb(card->func, ~(ireg) & (DN_LD_HOST_INT_STATUS |
  537. UP_LD_HOST_INT_STATUS),
  538. card->reg->host_intstatus, &ret);
  539. if (ret) {
  540. BT_ERR("sdio_writeb: clear int status register failed");
  541. return;
  542. }
  543. }
  544. spin_lock_irqsave(&priv->driver_lock, flags);
  545. sdio_ireg |= ireg;
  546. spin_unlock_irqrestore(&priv->driver_lock, flags);
  547. btmrvl_interrupt(priv);
  548. }
  549. static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
  550. {
  551. struct sdio_func *func;
  552. u8 reg;
  553. int ret = 0;
  554. if (!card || !card->func) {
  555. BT_ERR("Error: card or function is NULL!");
  556. ret = -EINVAL;
  557. goto failed;
  558. }
  559. func = card->func;
  560. sdio_claim_host(func);
  561. ret = sdio_enable_func(func);
  562. if (ret) {
  563. BT_ERR("sdio_enable_func() failed: ret=%d", ret);
  564. ret = -EIO;
  565. goto release_host;
  566. }
  567. ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
  568. if (ret) {
  569. BT_ERR("sdio_claim_irq failed: ret=%d", ret);
  570. ret = -EIO;
  571. goto disable_func;
  572. }
  573. ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
  574. if (ret) {
  575. BT_ERR("cannot set SDIO block size");
  576. ret = -EIO;
  577. goto release_irq;
  578. }
  579. reg = sdio_readb(func, card->reg->io_port_0, &ret);
  580. if (ret < 0) {
  581. ret = -EIO;
  582. goto release_irq;
  583. }
  584. card->ioport = reg;
  585. reg = sdio_readb(func, card->reg->io_port_1, &ret);
  586. if (ret < 0) {
  587. ret = -EIO;
  588. goto release_irq;
  589. }
  590. card->ioport |= (reg << 8);
  591. reg = sdio_readb(func, card->reg->io_port_2, &ret);
  592. if (ret < 0) {
  593. ret = -EIO;
  594. goto release_irq;
  595. }
  596. card->ioport |= (reg << 16);
  597. BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
  598. sdio_set_drvdata(func, card);
  599. sdio_release_host(func);
  600. return 0;
  601. release_irq:
  602. sdio_release_irq(func);
  603. disable_func:
  604. sdio_disable_func(func);
  605. release_host:
  606. sdio_release_host(func);
  607. failed:
  608. return ret;
  609. }
  610. static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
  611. {
  612. if (card && card->func) {
  613. sdio_claim_host(card->func);
  614. sdio_release_irq(card->func);
  615. sdio_disable_func(card->func);
  616. sdio_release_host(card->func);
  617. sdio_set_drvdata(card->func, NULL);
  618. }
  619. return 0;
  620. }
  621. static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
  622. {
  623. int ret;
  624. if (!card || !card->func)
  625. return -EINVAL;
  626. sdio_claim_host(card->func);
  627. ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
  628. btmrvl_sdio_get_rx_unit(card);
  629. sdio_release_host(card->func);
  630. return ret;
  631. }
  632. static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
  633. {
  634. int ret;
  635. if (!card || !card->func)
  636. return -EINVAL;
  637. sdio_claim_host(card->func);
  638. ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
  639. sdio_release_host(card->func);
  640. return ret;
  641. }
  642. static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
  643. u8 *payload, u16 nb)
  644. {
  645. struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
  646. int ret = 0;
  647. int buf_block_len;
  648. int blksz;
  649. int i = 0;
  650. u8 *buf = NULL;
  651. void *tmpbuf = NULL;
  652. int tmpbufsz;
  653. if (!card || !card->func) {
  654. BT_ERR("card or function is NULL!");
  655. return -EINVAL;
  656. }
  657. buf = payload;
  658. if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
  659. tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
  660. tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
  661. if (!tmpbuf)
  662. return -ENOMEM;
  663. buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
  664. memcpy(buf, payload, nb);
  665. }
  666. blksz = SDIO_BLOCK_SIZE;
  667. buf_block_len = (nb + blksz - 1) / blksz;
  668. sdio_claim_host(card->func);
  669. do {
  670. /* Transfer data to card */
  671. ret = sdio_writesb(card->func, card->ioport, buf,
  672. buf_block_len * blksz);
  673. if (ret < 0) {
  674. i++;
  675. BT_ERR("i=%d writesb failed: %d", i, ret);
  676. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
  677. payload, nb);
  678. ret = -EIO;
  679. if (i > MAX_WRITE_IOMEM_RETRY)
  680. goto exit;
  681. }
  682. } while (ret);
  683. priv->btmrvl_dev.tx_dnld_rdy = false;
  684. exit:
  685. sdio_release_host(card->func);
  686. kfree(tmpbuf);
  687. return ret;
  688. }
  689. static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
  690. {
  691. int ret = 0;
  692. u8 fws0;
  693. int pollnum = MAX_POLL_TRIES;
  694. if (!card || !card->func) {
  695. BT_ERR("card or function is NULL!");
  696. return -EINVAL;
  697. }
  698. sdio_claim_host(card->func);
  699. if (!btmrvl_sdio_verify_fw_download(card, 1)) {
  700. BT_DBG("Firmware already downloaded!");
  701. goto done;
  702. }
  703. /* Check if other function driver is downloading the firmware */
  704. fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
  705. if (ret) {
  706. BT_ERR("Failed to read FW downloading status!");
  707. ret = -EIO;
  708. goto done;
  709. }
  710. if (fws0) {
  711. BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
  712. /* Give other function more time to download the firmware */
  713. pollnum *= 10;
  714. } else {
  715. if (card->helper) {
  716. ret = btmrvl_sdio_download_helper(card);
  717. if (ret) {
  718. BT_ERR("Failed to download helper!");
  719. ret = -EIO;
  720. goto done;
  721. }
  722. }
  723. if (btmrvl_sdio_download_fw_w_helper(card)) {
  724. BT_ERR("Failed to download firmware!");
  725. ret = -EIO;
  726. goto done;
  727. }
  728. }
  729. if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
  730. BT_ERR("FW failed to be active in time!");
  731. ret = -ETIMEDOUT;
  732. goto done;
  733. }
  734. done:
  735. sdio_release_host(card->func);
  736. return ret;
  737. }
  738. static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
  739. {
  740. struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
  741. int ret = 0;
  742. if (!card || !card->func) {
  743. BT_ERR("card or function is NULL!");
  744. return -EINVAL;
  745. }
  746. sdio_claim_host(card->func);
  747. sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
  748. sdio_release_host(card->func);
  749. BT_DBG("wake up firmware");
  750. return ret;
  751. }
  752. static int btmrvl_sdio_probe(struct sdio_func *func,
  753. const struct sdio_device_id *id)
  754. {
  755. int ret = 0;
  756. struct btmrvl_private *priv = NULL;
  757. struct btmrvl_sdio_card *card = NULL;
  758. BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
  759. id->vendor, id->device, id->class, func->num);
  760. card = kzalloc(sizeof(*card), GFP_KERNEL);
  761. if (!card) {
  762. ret = -ENOMEM;
  763. goto done;
  764. }
  765. card->func = func;
  766. if (id->driver_data) {
  767. struct btmrvl_sdio_device *data = (void *) id->driver_data;
  768. card->helper = data->helper;
  769. card->firmware = data->firmware;
  770. card->reg = data->reg;
  771. card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
  772. }
  773. if (btmrvl_sdio_register_dev(card) < 0) {
  774. BT_ERR("Failed to register BT device!");
  775. ret = -ENODEV;
  776. goto free_card;
  777. }
  778. /* Disable the interrupts on the card */
  779. btmrvl_sdio_disable_host_int(card);
  780. if (btmrvl_sdio_download_fw(card)) {
  781. BT_ERR("Downloading firmware failed!");
  782. ret = -ENODEV;
  783. goto unreg_dev;
  784. }
  785. msleep(100);
  786. btmrvl_sdio_enable_host_int(card);
  787. priv = btmrvl_add_card(card);
  788. if (!priv) {
  789. BT_ERR("Initializing card failed!");
  790. ret = -ENODEV;
  791. goto disable_host_int;
  792. }
  793. card->priv = priv;
  794. /* Initialize the interface specific function pointers */
  795. priv->hw_host_to_card = btmrvl_sdio_host_to_card;
  796. priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
  797. priv->hw_process_int_status = btmrvl_sdio_process_int_status;
  798. if (btmrvl_register_hdev(priv)) {
  799. BT_ERR("Register hdev failed!");
  800. ret = -ENODEV;
  801. goto disable_host_int;
  802. }
  803. priv->btmrvl_dev.psmode = 1;
  804. btmrvl_enable_ps(priv);
  805. return 0;
  806. disable_host_int:
  807. btmrvl_sdio_disable_host_int(card);
  808. unreg_dev:
  809. btmrvl_sdio_unregister_dev(card);
  810. free_card:
  811. kfree(card);
  812. done:
  813. return ret;
  814. }
  815. static void btmrvl_sdio_remove(struct sdio_func *func)
  816. {
  817. struct btmrvl_sdio_card *card;
  818. if (func) {
  819. card = sdio_get_drvdata(func);
  820. if (card) {
  821. /* Send SHUTDOWN command & disable interrupt
  822. * if user removes the module.
  823. */
  824. if (user_rmmod) {
  825. btmrvl_send_module_cfg_cmd(card->priv,
  826. MODULE_SHUTDOWN_REQ);
  827. btmrvl_sdio_disable_host_int(card);
  828. }
  829. BT_DBG("unregester dev");
  830. btmrvl_sdio_unregister_dev(card);
  831. btmrvl_remove_card(card->priv);
  832. kfree(card);
  833. }
  834. }
  835. }
  836. static struct sdio_driver bt_mrvl_sdio = {
  837. .name = "btmrvl_sdio",
  838. .id_table = btmrvl_sdio_ids,
  839. .probe = btmrvl_sdio_probe,
  840. .remove = btmrvl_sdio_remove,
  841. };
  842. static int __init btmrvl_sdio_init_module(void)
  843. {
  844. if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
  845. BT_ERR("SDIO Driver Registration Failed");
  846. return -ENODEV;
  847. }
  848. /* Clear the flag in case user removes the card. */
  849. user_rmmod = 0;
  850. return 0;
  851. }
  852. static void __exit btmrvl_sdio_exit_module(void)
  853. {
  854. /* Set the flag as user is removing this module. */
  855. user_rmmod = 1;
  856. sdio_unregister_driver(&bt_mrvl_sdio);
  857. }
  858. module_init(btmrvl_sdio_init_module);
  859. module_exit(btmrvl_sdio_exit_module);
  860. MODULE_AUTHOR("Marvell International Ltd.");
  861. MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
  862. MODULE_VERSION(VERSION);
  863. MODULE_LICENSE("GPL v2");
  864. MODULE_FIRMWARE("sd8688_helper.bin");
  865. MODULE_FIRMWARE("sd8688.bin");
  866. MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");