btmrvl_sdio.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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 <linux/module.h>
  25. #include <linux/devcoredump.h>
  26. #include <net/bluetooth/bluetooth.h>
  27. #include <net/bluetooth/hci_core.h>
  28. #include "btmrvl_drv.h"
  29. #include "btmrvl_sdio.h"
  30. #define VERSION "1.0"
  31. static struct memory_type_mapping mem_type_mapping_tbl[] = {
  32. {"ITCM", NULL, 0, 0xF0},
  33. {"DTCM", NULL, 0, 0xF1},
  34. {"SQRAM", NULL, 0, 0xF2},
  35. {"APU", NULL, 0, 0xF3},
  36. {"CIU", NULL, 0, 0xF4},
  37. {"ICU", NULL, 0, 0xF5},
  38. {"MAC", NULL, 0, 0xF6},
  39. {"EXT7", NULL, 0, 0xF7},
  40. {"EXT8", NULL, 0, 0xF8},
  41. {"EXT9", NULL, 0, 0xF9},
  42. {"EXT10", NULL, 0, 0xFA},
  43. {"EXT11", NULL, 0, 0xFB},
  44. {"EXT12", NULL, 0, 0xFC},
  45. {"EXT13", NULL, 0, 0xFD},
  46. {"EXTLAST", NULL, 0, 0xFE},
  47. };
  48. /* The btmrvl_sdio_remove() callback function is called
  49. * when user removes this module from kernel space or ejects
  50. * the card from the slot. The driver handles these 2 cases
  51. * differently.
  52. * If the user is removing the module, a MODULE_SHUTDOWN_REQ
  53. * command is sent to firmware and interrupt will be disabled.
  54. * If the card is removed, there is no need to send command
  55. * or disable interrupt.
  56. *
  57. * The variable 'user_rmmod' is used to distinguish these two
  58. * scenarios. This flag is initialized as FALSE in case the card
  59. * is removed, and will be set to TRUE for module removal when
  60. * module_exit function is called.
  61. */
  62. static u8 user_rmmod;
  63. static u8 sdio_ireg;
  64. static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
  65. .cfg = 0x03,
  66. .host_int_mask = 0x04,
  67. .host_intstatus = 0x05,
  68. .card_status = 0x20,
  69. .sq_read_base_addr_a0 = 0x10,
  70. .sq_read_base_addr_a1 = 0x11,
  71. .card_fw_status0 = 0x40,
  72. .card_fw_status1 = 0x41,
  73. .card_rx_len = 0x42,
  74. .card_rx_unit = 0x43,
  75. .io_port_0 = 0x00,
  76. .io_port_1 = 0x01,
  77. .io_port_2 = 0x02,
  78. .int_read_to_clear = false,
  79. };
  80. static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = {
  81. .cfg = 0x00,
  82. .host_int_mask = 0x02,
  83. .host_intstatus = 0x03,
  84. .card_status = 0x30,
  85. .sq_read_base_addr_a0 = 0x40,
  86. .sq_read_base_addr_a1 = 0x41,
  87. .card_revision = 0x5c,
  88. .card_fw_status0 = 0x60,
  89. .card_fw_status1 = 0x61,
  90. .card_rx_len = 0x62,
  91. .card_rx_unit = 0x63,
  92. .io_port_0 = 0x78,
  93. .io_port_1 = 0x79,
  94. .io_port_2 = 0x7a,
  95. .int_read_to_clear = false,
  96. };
  97. static const struct btmrvl_sdio_card_reg btmrvl_reg_8887 = {
  98. .cfg = 0x00,
  99. .host_int_mask = 0x08,
  100. .host_intstatus = 0x0C,
  101. .card_status = 0x5C,
  102. .sq_read_base_addr_a0 = 0x6C,
  103. .sq_read_base_addr_a1 = 0x6D,
  104. .card_revision = 0xC8,
  105. .card_fw_status0 = 0x88,
  106. .card_fw_status1 = 0x89,
  107. .card_rx_len = 0x8A,
  108. .card_rx_unit = 0x8B,
  109. .io_port_0 = 0xE4,
  110. .io_port_1 = 0xE5,
  111. .io_port_2 = 0xE6,
  112. .int_read_to_clear = true,
  113. .host_int_rsr = 0x04,
  114. .card_misc_cfg = 0xD8,
  115. };
  116. static const struct btmrvl_sdio_card_reg btmrvl_reg_8897 = {
  117. .cfg = 0x00,
  118. .host_int_mask = 0x02,
  119. .host_intstatus = 0x03,
  120. .card_status = 0x50,
  121. .sq_read_base_addr_a0 = 0x60,
  122. .sq_read_base_addr_a1 = 0x61,
  123. .card_revision = 0xbc,
  124. .card_fw_status0 = 0xc0,
  125. .card_fw_status1 = 0xc1,
  126. .card_rx_len = 0xc2,
  127. .card_rx_unit = 0xc3,
  128. .io_port_0 = 0xd8,
  129. .io_port_1 = 0xd9,
  130. .io_port_2 = 0xda,
  131. .int_read_to_clear = true,
  132. .host_int_rsr = 0x01,
  133. .card_misc_cfg = 0xcc,
  134. .fw_dump_ctrl = 0xe2,
  135. .fw_dump_start = 0xe3,
  136. .fw_dump_end = 0xea,
  137. };
  138. static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
  139. .helper = "/*(DEBLOBBED)*/",
  140. .firmware = "/*(DEBLOBBED)*/",
  141. .reg = &btmrvl_reg_8688,
  142. .support_pscan_win_report = false,
  143. .sd_blksz_fw_dl = 64,
  144. .supports_fw_dump = false,
  145. };
  146. static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
  147. .helper = NULL,
  148. .firmware = "/*(DEBLOBBED)*/",
  149. .reg = &btmrvl_reg_87xx,
  150. .support_pscan_win_report = false,
  151. .sd_blksz_fw_dl = 256,
  152. .supports_fw_dump = false,
  153. };
  154. static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
  155. .helper = NULL,
  156. .firmware = "/*(DEBLOBBED)*/",
  157. .reg = &btmrvl_reg_87xx,
  158. .support_pscan_win_report = false,
  159. .sd_blksz_fw_dl = 256,
  160. .supports_fw_dump = false,
  161. };
  162. static const struct btmrvl_sdio_device btmrvl_sdio_sd8887 = {
  163. .helper = NULL,
  164. .firmware = "/*(DEBLOBBED)*/",
  165. .reg = &btmrvl_reg_8887,
  166. .support_pscan_win_report = true,
  167. .sd_blksz_fw_dl = 256,
  168. .supports_fw_dump = false,
  169. };
  170. static const struct btmrvl_sdio_device btmrvl_sdio_sd8897 = {
  171. .helper = NULL,
  172. .firmware = "/*(DEBLOBBED)*/",
  173. .reg = &btmrvl_reg_8897,
  174. .support_pscan_win_report = true,
  175. .sd_blksz_fw_dl = 256,
  176. .supports_fw_dump = true,
  177. };
  178. static const struct sdio_device_id btmrvl_sdio_ids[] = {
  179. /* Marvell SD8688 Bluetooth device */
  180. { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
  181. .driver_data = (unsigned long) &btmrvl_sdio_sd8688 },
  182. /* Marvell SD8787 Bluetooth device */
  183. { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
  184. .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
  185. /* Marvell SD8787 Bluetooth AMP device */
  186. { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911B),
  187. .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
  188. /* Marvell SD8797 Bluetooth device */
  189. { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A),
  190. .driver_data = (unsigned long) &btmrvl_sdio_sd8797 },
  191. /* Marvell SD8887 Bluetooth device */
  192. { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9136),
  193. .driver_data = (unsigned long)&btmrvl_sdio_sd8887 },
  194. /* Marvell SD8897 Bluetooth device */
  195. { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912E),
  196. .driver_data = (unsigned long) &btmrvl_sdio_sd8897 },
  197. { } /* Terminating entry */
  198. };
  199. MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
  200. static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
  201. {
  202. u8 reg;
  203. int ret;
  204. reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
  205. if (!ret)
  206. card->rx_unit = reg;
  207. return ret;
  208. }
  209. static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
  210. {
  211. u8 fws0, fws1;
  212. int ret;
  213. *dat = 0;
  214. fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
  215. if (ret)
  216. return -EIO;
  217. fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
  218. if (ret)
  219. return -EIO;
  220. *dat = (((u16) fws1) << 8) | fws0;
  221. return 0;
  222. }
  223. static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
  224. {
  225. u8 reg;
  226. int ret;
  227. reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
  228. if (!ret)
  229. *dat = (u16) reg << card->rx_unit;
  230. return ret;
  231. }
  232. static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
  233. u8 mask)
  234. {
  235. int ret;
  236. sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
  237. if (ret) {
  238. BT_ERR("Unable to enable the host interrupt!");
  239. ret = -EIO;
  240. }
  241. return ret;
  242. }
  243. static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
  244. u8 mask)
  245. {
  246. u8 host_int_mask;
  247. int ret;
  248. host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
  249. if (ret)
  250. return -EIO;
  251. host_int_mask &= ~mask;
  252. sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
  253. if (ret < 0) {
  254. BT_ERR("Unable to disable the host interrupt!");
  255. return -EIO;
  256. }
  257. return 0;
  258. }
  259. static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
  260. {
  261. unsigned int tries;
  262. u8 status;
  263. int ret;
  264. for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
  265. status = sdio_readb(card->func, card->reg->card_status, &ret);
  266. if (ret)
  267. goto failed;
  268. if ((status & bits) == bits)
  269. return ret;
  270. udelay(1);
  271. }
  272. ret = -ETIMEDOUT;
  273. failed:
  274. BT_ERR("FAILED! ret=%d", ret);
  275. return ret;
  276. }
  277. static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
  278. int pollnum)
  279. {
  280. u16 firmwarestat;
  281. int tries, ret;
  282. /* Wait for firmware to become ready */
  283. for (tries = 0; tries < pollnum; tries++) {
  284. sdio_claim_host(card->func);
  285. ret = btmrvl_sdio_read_fw_status(card, &firmwarestat);
  286. sdio_release_host(card->func);
  287. if (ret < 0)
  288. continue;
  289. if (firmwarestat == FIRMWARE_READY)
  290. return 0;
  291. msleep(10);
  292. }
  293. return -ETIMEDOUT;
  294. }
  295. static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
  296. {
  297. const struct firmware *fw_helper = NULL;
  298. const u8 *helper = NULL;
  299. int ret;
  300. void *tmphlprbuf = NULL;
  301. int tmphlprbufsz, hlprblknow, helperlen;
  302. u8 *helperbuf;
  303. u32 tx_len;
  304. ret = reject_firmware(&fw_helper, card->helper,
  305. &card->func->dev);
  306. if ((ret < 0) || !fw_helper) {
  307. BT_ERR("reject_firmware(helper) failed, error code = %d",
  308. ret);
  309. ret = -ENOENT;
  310. goto done;
  311. }
  312. helper = fw_helper->data;
  313. helperlen = fw_helper->size;
  314. BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
  315. helperlen, SDIO_BLOCK_SIZE);
  316. tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
  317. tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
  318. if (!tmphlprbuf) {
  319. BT_ERR("Unable to allocate buffer for helper."
  320. " Terminating download");
  321. ret = -ENOMEM;
  322. goto done;
  323. }
  324. helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
  325. /* Perform helper data transfer */
  326. tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
  327. - SDIO_HEADER_LEN;
  328. hlprblknow = 0;
  329. do {
  330. ret = btmrvl_sdio_poll_card_status(card,
  331. CARD_IO_READY | DN_LD_CARD_RDY);
  332. if (ret < 0) {
  333. BT_ERR("Helper download poll status timeout @ %d",
  334. hlprblknow);
  335. goto done;
  336. }
  337. /* Check if there is more data? */
  338. if (hlprblknow >= helperlen)
  339. break;
  340. if (helperlen - hlprblknow < tx_len)
  341. tx_len = helperlen - hlprblknow;
  342. /* Little-endian */
  343. helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
  344. helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
  345. helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
  346. helperbuf[3] = ((tx_len & 0xff000000) >> 24);
  347. memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
  348. tx_len);
  349. /* Now send the data */
  350. ret = sdio_writesb(card->func, card->ioport, helperbuf,
  351. FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
  352. if (ret < 0) {
  353. BT_ERR("IO error during helper download @ %d",
  354. hlprblknow);
  355. goto done;
  356. }
  357. hlprblknow += tx_len;
  358. } while (true);
  359. BT_DBG("Transferring helper image EOF block");
  360. memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
  361. ret = sdio_writesb(card->func, card->ioport, helperbuf,
  362. SDIO_BLOCK_SIZE);
  363. if (ret < 0) {
  364. BT_ERR("IO error in writing helper image EOF block");
  365. goto done;
  366. }
  367. ret = 0;
  368. done:
  369. kfree(tmphlprbuf);
  370. release_firmware(fw_helper);
  371. return ret;
  372. }
  373. static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
  374. {
  375. const struct firmware *fw_firmware = NULL;
  376. const u8 *firmware = NULL;
  377. int firmwarelen, tmpfwbufsz, ret;
  378. unsigned int tries, offset;
  379. u8 base0, base1;
  380. void *tmpfwbuf = NULL;
  381. u8 *fwbuf;
  382. u16 len, blksz_dl = card->sd_blksz_fw_dl;
  383. int txlen = 0, tx_blocks = 0, count = 0;
  384. ret = reject_firmware(&fw_firmware, card->firmware,
  385. &card->func->dev);
  386. if ((ret < 0) || !fw_firmware) {
  387. BT_ERR("reject_firmware(firmware) failed, error code = %d",
  388. ret);
  389. ret = -ENOENT;
  390. goto done;
  391. }
  392. firmware = fw_firmware->data;
  393. firmwarelen = fw_firmware->size;
  394. BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
  395. tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
  396. tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
  397. if (!tmpfwbuf) {
  398. BT_ERR("Unable to allocate buffer for firmware."
  399. " Terminating download");
  400. ret = -ENOMEM;
  401. goto done;
  402. }
  403. /* Ensure aligned firmware buffer */
  404. fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
  405. /* Perform firmware data transfer */
  406. offset = 0;
  407. do {
  408. ret = btmrvl_sdio_poll_card_status(card,
  409. CARD_IO_READY | DN_LD_CARD_RDY);
  410. if (ret < 0) {
  411. BT_ERR("FW download with helper poll status"
  412. " timeout @ %d", offset);
  413. goto done;
  414. }
  415. /* Check if there is more data ? */
  416. if (offset >= firmwarelen)
  417. break;
  418. for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  419. base0 = sdio_readb(card->func,
  420. card->reg->sq_read_base_addr_a0, &ret);
  421. if (ret) {
  422. BT_ERR("BASE0 register read failed:"
  423. " base0 = 0x%04X(%d)."
  424. " Terminating download",
  425. base0, base0);
  426. ret = -EIO;
  427. goto done;
  428. }
  429. base1 = sdio_readb(card->func,
  430. card->reg->sq_read_base_addr_a1, &ret);
  431. if (ret) {
  432. BT_ERR("BASE1 register read failed:"
  433. " base1 = 0x%04X(%d)."
  434. " Terminating download",
  435. base1, base1);
  436. ret = -EIO;
  437. goto done;
  438. }
  439. len = (((u16) base1) << 8) | base0;
  440. if (len)
  441. break;
  442. udelay(10);
  443. }
  444. if (!len)
  445. break;
  446. else if (len > BTM_UPLD_SIZE) {
  447. BT_ERR("FW download failure @%d, invalid length %d",
  448. offset, len);
  449. ret = -EINVAL;
  450. goto done;
  451. }
  452. txlen = len;
  453. if (len & BIT(0)) {
  454. count++;
  455. if (count > MAX_WRITE_IOMEM_RETRY) {
  456. BT_ERR("FW download failure @%d, "
  457. "over max retry count", offset);
  458. ret = -EIO;
  459. goto done;
  460. }
  461. BT_ERR("FW CRC error indicated by the helper: "
  462. "len = 0x%04X, txlen = %d", len, txlen);
  463. len &= ~BIT(0);
  464. /* Set txlen to 0 so as to resend from same offset */
  465. txlen = 0;
  466. } else {
  467. count = 0;
  468. /* Last block ? */
  469. if (firmwarelen - offset < txlen)
  470. txlen = firmwarelen - offset;
  471. tx_blocks = DIV_ROUND_UP(txlen, blksz_dl);
  472. memcpy(fwbuf, &firmware[offset], txlen);
  473. }
  474. ret = sdio_writesb(card->func, card->ioport, fwbuf,
  475. tx_blocks * blksz_dl);
  476. if (ret < 0) {
  477. BT_ERR("FW download, writesb(%d) failed @%d",
  478. count, offset);
  479. sdio_writeb(card->func, HOST_CMD53_FIN,
  480. card->reg->cfg, &ret);
  481. if (ret)
  482. BT_ERR("writeb failed (CFG)");
  483. }
  484. offset += txlen;
  485. } while (true);
  486. BT_INFO("FW download over, size %d bytes", offset);
  487. ret = 0;
  488. done:
  489. kfree(tmpfwbuf);
  490. release_firmware(fw_firmware);
  491. return ret;
  492. }
  493. static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
  494. {
  495. u16 buf_len = 0;
  496. int ret, num_blocks, blksz;
  497. struct sk_buff *skb = NULL;
  498. u32 type;
  499. u8 *payload = NULL;
  500. struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
  501. struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
  502. if (!card || !card->func) {
  503. BT_ERR("card or function is NULL!");
  504. ret = -EINVAL;
  505. goto exit;
  506. }
  507. /* Read the length of data to be transferred */
  508. ret = btmrvl_sdio_read_rx_len(card, &buf_len);
  509. if (ret < 0) {
  510. BT_ERR("read rx_len failed");
  511. ret = -EIO;
  512. goto exit;
  513. }
  514. blksz = SDIO_BLOCK_SIZE;
  515. num_blocks = DIV_ROUND_UP(buf_len, blksz);
  516. if (buf_len <= SDIO_HEADER_LEN
  517. || (num_blocks * blksz) > ALLOC_BUF_SIZE) {
  518. BT_ERR("invalid packet length: %d", buf_len);
  519. ret = -EINVAL;
  520. goto exit;
  521. }
  522. /* Allocate buffer */
  523. skb = bt_skb_alloc(num_blocks * blksz + BTSDIO_DMA_ALIGN, GFP_ATOMIC);
  524. if (skb == NULL) {
  525. BT_ERR("No free skb");
  526. ret = -ENOMEM;
  527. goto exit;
  528. }
  529. if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
  530. skb_put(skb, (unsigned long) skb->data &
  531. (BTSDIO_DMA_ALIGN - 1));
  532. skb_pull(skb, (unsigned long) skb->data &
  533. (BTSDIO_DMA_ALIGN - 1));
  534. }
  535. payload = skb->data;
  536. ret = sdio_readsb(card->func, payload, card->ioport,
  537. num_blocks * blksz);
  538. if (ret < 0) {
  539. BT_ERR("readsb failed: %d", ret);
  540. ret = -EIO;
  541. goto exit;
  542. }
  543. /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
  544. * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
  545. */
  546. buf_len = payload[0];
  547. buf_len |= payload[1] << 8;
  548. buf_len |= payload[2] << 16;
  549. if (buf_len > blksz * num_blocks) {
  550. BT_ERR("Skip incorrect packet: hdrlen %d buffer %d",
  551. buf_len, blksz * num_blocks);
  552. ret = -EIO;
  553. goto exit;
  554. }
  555. type = payload[3];
  556. switch (type) {
  557. case HCI_ACLDATA_PKT:
  558. case HCI_SCODATA_PKT:
  559. case HCI_EVENT_PKT:
  560. bt_cb(skb)->pkt_type = type;
  561. skb_put(skb, buf_len);
  562. skb_pull(skb, SDIO_HEADER_LEN);
  563. if (type == HCI_EVENT_PKT) {
  564. if (btmrvl_check_evtpkt(priv, skb))
  565. hci_recv_frame(hdev, skb);
  566. } else {
  567. hci_recv_frame(hdev, skb);
  568. }
  569. hdev->stat.byte_rx += buf_len;
  570. break;
  571. case MRVL_VENDOR_PKT:
  572. bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
  573. skb_put(skb, buf_len);
  574. skb_pull(skb, SDIO_HEADER_LEN);
  575. if (btmrvl_process_event(priv, skb))
  576. hci_recv_frame(hdev, skb);
  577. hdev->stat.byte_rx += buf_len;
  578. break;
  579. default:
  580. BT_ERR("Unknown packet type:%d", type);
  581. BT_ERR("hex: %*ph", blksz * num_blocks, payload);
  582. kfree_skb(skb);
  583. skb = NULL;
  584. break;
  585. }
  586. exit:
  587. if (ret) {
  588. hdev->stat.err_rx++;
  589. kfree_skb(skb);
  590. }
  591. return ret;
  592. }
  593. static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
  594. {
  595. ulong flags;
  596. u8 ireg;
  597. struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
  598. spin_lock_irqsave(&priv->driver_lock, flags);
  599. ireg = sdio_ireg;
  600. sdio_ireg = 0;
  601. spin_unlock_irqrestore(&priv->driver_lock, flags);
  602. sdio_claim_host(card->func);
  603. if (ireg & DN_LD_HOST_INT_STATUS) {
  604. if (priv->btmrvl_dev.tx_dnld_rdy)
  605. BT_DBG("tx_done already received: "
  606. " int_status=0x%x", ireg);
  607. else
  608. priv->btmrvl_dev.tx_dnld_rdy = true;
  609. }
  610. if (ireg & UP_LD_HOST_INT_STATUS)
  611. btmrvl_sdio_card_to_host(priv);
  612. sdio_release_host(card->func);
  613. return 0;
  614. }
  615. static int btmrvl_sdio_read_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
  616. {
  617. struct btmrvl_adapter *adapter = card->priv->adapter;
  618. int ret;
  619. ret = sdio_readsb(card->func, adapter->hw_regs, 0, SDIO_BLOCK_SIZE);
  620. if (ret) {
  621. BT_ERR("sdio_readsb: read int hw_regs failed: %d", ret);
  622. return ret;
  623. }
  624. *ireg = adapter->hw_regs[card->reg->host_intstatus];
  625. BT_DBG("hw_regs[%#x]=%#x", card->reg->host_intstatus, *ireg);
  626. return 0;
  627. }
  628. static int btmrvl_sdio_write_to_clear(struct btmrvl_sdio_card *card, u8 *ireg)
  629. {
  630. int ret;
  631. *ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
  632. if (ret) {
  633. BT_ERR("sdio_readb: read int status failed: %d", ret);
  634. return ret;
  635. }
  636. if (*ireg) {
  637. /*
  638. * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
  639. * Clear the interrupt status register and re-enable the
  640. * interrupt.
  641. */
  642. BT_DBG("int_status = 0x%x", *ireg);
  643. sdio_writeb(card->func, ~(*ireg) & (DN_LD_HOST_INT_STATUS |
  644. UP_LD_HOST_INT_STATUS),
  645. card->reg->host_intstatus, &ret);
  646. if (ret) {
  647. BT_ERR("sdio_writeb: clear int status failed: %d", ret);
  648. return ret;
  649. }
  650. }
  651. return 0;
  652. }
  653. static void btmrvl_sdio_interrupt(struct sdio_func *func)
  654. {
  655. struct btmrvl_private *priv;
  656. struct btmrvl_sdio_card *card;
  657. ulong flags;
  658. u8 ireg = 0;
  659. int ret;
  660. card = sdio_get_drvdata(func);
  661. if (!card || !card->priv) {
  662. BT_ERR("sbi_interrupt(%p) card or priv is NULL, card=%p",
  663. func, card);
  664. return;
  665. }
  666. priv = card->priv;
  667. if (priv->surprise_removed)
  668. return;
  669. if (card->reg->int_read_to_clear)
  670. ret = btmrvl_sdio_read_to_clear(card, &ireg);
  671. else
  672. ret = btmrvl_sdio_write_to_clear(card, &ireg);
  673. if (ret)
  674. return;
  675. spin_lock_irqsave(&priv->driver_lock, flags);
  676. sdio_ireg |= ireg;
  677. spin_unlock_irqrestore(&priv->driver_lock, flags);
  678. btmrvl_interrupt(priv);
  679. }
  680. static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
  681. {
  682. struct sdio_func *func;
  683. u8 reg;
  684. int ret = 0;
  685. if (!card || !card->func) {
  686. BT_ERR("Error: card or function is NULL!");
  687. ret = -EINVAL;
  688. goto failed;
  689. }
  690. func = card->func;
  691. sdio_claim_host(func);
  692. ret = sdio_enable_func(func);
  693. if (ret) {
  694. BT_ERR("sdio_enable_func() failed: ret=%d", ret);
  695. ret = -EIO;
  696. goto release_host;
  697. }
  698. ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
  699. if (ret) {
  700. BT_ERR("sdio_claim_irq failed: ret=%d", ret);
  701. ret = -EIO;
  702. goto disable_func;
  703. }
  704. ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
  705. if (ret) {
  706. BT_ERR("cannot set SDIO block size");
  707. ret = -EIO;
  708. goto release_irq;
  709. }
  710. reg = sdio_readb(func, card->reg->io_port_0, &ret);
  711. if (ret < 0) {
  712. ret = -EIO;
  713. goto release_irq;
  714. }
  715. card->ioport = reg;
  716. reg = sdio_readb(func, card->reg->io_port_1, &ret);
  717. if (ret < 0) {
  718. ret = -EIO;
  719. goto release_irq;
  720. }
  721. card->ioport |= (reg << 8);
  722. reg = sdio_readb(func, card->reg->io_port_2, &ret);
  723. if (ret < 0) {
  724. ret = -EIO;
  725. goto release_irq;
  726. }
  727. card->ioport |= (reg << 16);
  728. BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
  729. if (card->reg->int_read_to_clear) {
  730. reg = sdio_readb(func, card->reg->host_int_rsr, &ret);
  731. if (ret < 0) {
  732. ret = -EIO;
  733. goto release_irq;
  734. }
  735. sdio_writeb(func, reg | 0x3f, card->reg->host_int_rsr, &ret);
  736. if (ret < 0) {
  737. ret = -EIO;
  738. goto release_irq;
  739. }
  740. reg = sdio_readb(func, card->reg->card_misc_cfg, &ret);
  741. if (ret < 0) {
  742. ret = -EIO;
  743. goto release_irq;
  744. }
  745. sdio_writeb(func, reg | 0x10, card->reg->card_misc_cfg, &ret);
  746. if (ret < 0) {
  747. ret = -EIO;
  748. goto release_irq;
  749. }
  750. }
  751. sdio_set_drvdata(func, card);
  752. sdio_release_host(func);
  753. return 0;
  754. release_irq:
  755. sdio_release_irq(func);
  756. disable_func:
  757. sdio_disable_func(func);
  758. release_host:
  759. sdio_release_host(func);
  760. failed:
  761. return ret;
  762. }
  763. static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
  764. {
  765. if (card && card->func) {
  766. sdio_claim_host(card->func);
  767. sdio_release_irq(card->func);
  768. sdio_disable_func(card->func);
  769. sdio_release_host(card->func);
  770. sdio_set_drvdata(card->func, NULL);
  771. }
  772. return 0;
  773. }
  774. static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
  775. {
  776. int ret;
  777. if (!card || !card->func)
  778. return -EINVAL;
  779. sdio_claim_host(card->func);
  780. ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
  781. btmrvl_sdio_get_rx_unit(card);
  782. sdio_release_host(card->func);
  783. return ret;
  784. }
  785. static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
  786. {
  787. int ret;
  788. if (!card || !card->func)
  789. return -EINVAL;
  790. sdio_claim_host(card->func);
  791. ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
  792. sdio_release_host(card->func);
  793. return ret;
  794. }
  795. static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
  796. u8 *payload, u16 nb)
  797. {
  798. struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
  799. int ret = 0;
  800. int buf_block_len;
  801. int blksz;
  802. int i = 0;
  803. u8 *buf = NULL;
  804. void *tmpbuf = NULL;
  805. int tmpbufsz;
  806. if (!card || !card->func) {
  807. BT_ERR("card or function is NULL!");
  808. return -EINVAL;
  809. }
  810. buf = payload;
  811. if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
  812. tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
  813. tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
  814. if (!tmpbuf)
  815. return -ENOMEM;
  816. buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
  817. memcpy(buf, payload, nb);
  818. }
  819. blksz = SDIO_BLOCK_SIZE;
  820. buf_block_len = DIV_ROUND_UP(nb, blksz);
  821. sdio_claim_host(card->func);
  822. do {
  823. /* Transfer data to card */
  824. ret = sdio_writesb(card->func, card->ioport, buf,
  825. buf_block_len * blksz);
  826. if (ret < 0) {
  827. i++;
  828. BT_ERR("i=%d writesb failed: %d", i, ret);
  829. BT_ERR("hex: %*ph", nb, payload);
  830. ret = -EIO;
  831. if (i > MAX_WRITE_IOMEM_RETRY)
  832. goto exit;
  833. }
  834. } while (ret);
  835. priv->btmrvl_dev.tx_dnld_rdy = false;
  836. exit:
  837. sdio_release_host(card->func);
  838. kfree(tmpbuf);
  839. return ret;
  840. }
  841. static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
  842. {
  843. int ret;
  844. u8 fws0;
  845. int pollnum = MAX_POLL_TRIES;
  846. if (!card || !card->func) {
  847. BT_ERR("card or function is NULL!");
  848. return -EINVAL;
  849. }
  850. if (!btmrvl_sdio_verify_fw_download(card, 1)) {
  851. BT_DBG("Firmware already downloaded!");
  852. return 0;
  853. }
  854. sdio_claim_host(card->func);
  855. /* Check if other function driver is downloading the firmware */
  856. fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
  857. if (ret) {
  858. BT_ERR("Failed to read FW downloading status!");
  859. ret = -EIO;
  860. goto done;
  861. }
  862. if (fws0) {
  863. BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
  864. /* Give other function more time to download the firmware */
  865. pollnum *= 10;
  866. } else {
  867. if (card->helper) {
  868. ret = btmrvl_sdio_download_helper(card);
  869. if (ret) {
  870. BT_ERR("Failed to download helper!");
  871. ret = -EIO;
  872. goto done;
  873. }
  874. }
  875. if (btmrvl_sdio_download_fw_w_helper(card)) {
  876. BT_ERR("Failed to download firmware!");
  877. ret = -EIO;
  878. goto done;
  879. }
  880. }
  881. sdio_release_host(card->func);
  882. /*
  883. * winner or not, with this test the FW synchronizes when the
  884. * module can continue its initialization
  885. */
  886. if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
  887. BT_ERR("FW failed to be active in time!");
  888. return -ETIMEDOUT;
  889. }
  890. return 0;
  891. done:
  892. sdio_release_host(card->func);
  893. return ret;
  894. }
  895. static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
  896. {
  897. struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
  898. int ret = 0;
  899. if (!card || !card->func) {
  900. BT_ERR("card or function is NULL!");
  901. return -EINVAL;
  902. }
  903. sdio_claim_host(card->func);
  904. sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
  905. sdio_release_host(card->func);
  906. BT_DBG("wake up firmware");
  907. return ret;
  908. }
  909. static void btmrvl_sdio_dump_regs(struct btmrvl_private *priv)
  910. {
  911. struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
  912. int ret = 0;
  913. unsigned int reg, reg_start, reg_end;
  914. char buf[256], *ptr;
  915. u8 loop, func, data;
  916. int MAX_LOOP = 2;
  917. btmrvl_sdio_wakeup_fw(priv);
  918. sdio_claim_host(card->func);
  919. for (loop = 0; loop < MAX_LOOP; loop++) {
  920. memset(buf, 0, sizeof(buf));
  921. ptr = buf;
  922. if (loop == 0) {
  923. /* Read the registers of SDIO function0 */
  924. func = loop;
  925. reg_start = 0;
  926. reg_end = 9;
  927. } else {
  928. func = 2;
  929. reg_start = 0;
  930. reg_end = 0x09;
  931. }
  932. ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
  933. func, reg_start, reg_end);
  934. for (reg = reg_start; reg <= reg_end; reg++) {
  935. if (func == 0)
  936. data = sdio_f0_readb(card->func, reg, &ret);
  937. else
  938. data = sdio_readb(card->func, reg, &ret);
  939. if (!ret) {
  940. ptr += sprintf(ptr, "%02x ", data);
  941. } else {
  942. ptr += sprintf(ptr, "ERR");
  943. break;
  944. }
  945. }
  946. BT_INFO("%s", buf);
  947. }
  948. sdio_release_host(card->func);
  949. }
  950. /* This function read/write firmware */
  951. static enum
  952. rdwr_status btmrvl_sdio_rdwr_firmware(struct btmrvl_private *priv,
  953. u8 doneflag)
  954. {
  955. struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
  956. int ret, tries;
  957. u8 ctrl_data = 0;
  958. sdio_writeb(card->func, FW_DUMP_HOST_READY, card->reg->fw_dump_ctrl,
  959. &ret);
  960. if (ret) {
  961. BT_ERR("SDIO write err");
  962. return RDWR_STATUS_FAILURE;
  963. }
  964. for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  965. ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
  966. &ret);
  967. if (ret) {
  968. BT_ERR("SDIO read err");
  969. return RDWR_STATUS_FAILURE;
  970. }
  971. if (ctrl_data == FW_DUMP_DONE)
  972. break;
  973. if (doneflag && ctrl_data == doneflag)
  974. return RDWR_STATUS_DONE;
  975. if (ctrl_data != FW_DUMP_HOST_READY) {
  976. BT_INFO("The ctrl reg was changed, re-try again!");
  977. sdio_writeb(card->func, FW_DUMP_HOST_READY,
  978. card->reg->fw_dump_ctrl, &ret);
  979. if (ret) {
  980. BT_ERR("SDIO write err");
  981. return RDWR_STATUS_FAILURE;
  982. }
  983. }
  984. usleep_range(100, 200);
  985. }
  986. if (ctrl_data == FW_DUMP_HOST_READY) {
  987. BT_ERR("Fail to pull ctrl_data");
  988. return RDWR_STATUS_FAILURE;
  989. }
  990. return RDWR_STATUS_SUCCESS;
  991. }
  992. /* This function dump sdio register and memory data */
  993. static void btmrvl_sdio_dump_firmware(struct btmrvl_private *priv)
  994. {
  995. struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
  996. int ret = 0;
  997. unsigned int reg, reg_start, reg_end;
  998. enum rdwr_status stat;
  999. u8 *dbg_ptr, *end_ptr, *fw_dump_data, *fw_dump_ptr;
  1000. u8 dump_num = 0, idx, i, read_reg, doneflag = 0;
  1001. u32 memory_size, fw_dump_len = 0;
  1002. /* dump sdio register first */
  1003. btmrvl_sdio_dump_regs(priv);
  1004. if (!card->supports_fw_dump) {
  1005. BT_ERR("Firmware dump not supported for this card!");
  1006. return;
  1007. }
  1008. for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
  1009. struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
  1010. if (entry->mem_ptr) {
  1011. vfree(entry->mem_ptr);
  1012. entry->mem_ptr = NULL;
  1013. }
  1014. entry->mem_size = 0;
  1015. }
  1016. btmrvl_sdio_wakeup_fw(priv);
  1017. sdio_claim_host(card->func);
  1018. BT_INFO("== btmrvl firmware dump start ==");
  1019. stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
  1020. if (stat == RDWR_STATUS_FAILURE)
  1021. goto done;
  1022. reg = card->reg->fw_dump_start;
  1023. /* Read the number of the memories which will dump */
  1024. dump_num = sdio_readb(card->func, reg, &ret);
  1025. if (ret) {
  1026. BT_ERR("SDIO read memory length err");
  1027. goto done;
  1028. }
  1029. /* Read the length of every memory which will dump */
  1030. for (idx = 0; idx < dump_num; idx++) {
  1031. struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
  1032. stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
  1033. if (stat == RDWR_STATUS_FAILURE)
  1034. goto done;
  1035. memory_size = 0;
  1036. reg = card->reg->fw_dump_start;
  1037. for (i = 0; i < 4; i++) {
  1038. read_reg = sdio_readb(card->func, reg, &ret);
  1039. if (ret) {
  1040. BT_ERR("SDIO read err");
  1041. goto done;
  1042. }
  1043. memory_size |= (read_reg << i*8);
  1044. reg++;
  1045. }
  1046. if (memory_size == 0) {
  1047. BT_INFO("Firmware dump finished!");
  1048. break;
  1049. }
  1050. BT_INFO("%s_SIZE=0x%x", entry->mem_name, memory_size);
  1051. entry->mem_ptr = vzalloc(memory_size + 1);
  1052. entry->mem_size = memory_size;
  1053. if (!entry->mem_ptr) {
  1054. BT_ERR("Vzalloc %s failed", entry->mem_name);
  1055. goto done;
  1056. }
  1057. fw_dump_len += (strlen("========Start dump ") +
  1058. strlen(entry->mem_name) +
  1059. strlen("========\n") +
  1060. (memory_size + 1) +
  1061. strlen("\n========End dump========\n"));
  1062. dbg_ptr = entry->mem_ptr;
  1063. end_ptr = dbg_ptr + memory_size;
  1064. doneflag = entry->done_flag;
  1065. BT_INFO("Start %s output, please wait...",
  1066. entry->mem_name);
  1067. do {
  1068. stat = btmrvl_sdio_rdwr_firmware(priv, doneflag);
  1069. if (stat == RDWR_STATUS_FAILURE)
  1070. goto done;
  1071. reg_start = card->reg->fw_dump_start;
  1072. reg_end = card->reg->fw_dump_end;
  1073. for (reg = reg_start; reg <= reg_end; reg++) {
  1074. *dbg_ptr = sdio_readb(card->func, reg, &ret);
  1075. if (ret) {
  1076. BT_ERR("SDIO read err");
  1077. goto done;
  1078. }
  1079. if (dbg_ptr < end_ptr)
  1080. dbg_ptr++;
  1081. else
  1082. BT_ERR("Allocated buffer not enough");
  1083. }
  1084. if (stat != RDWR_STATUS_DONE) {
  1085. continue;
  1086. } else {
  1087. BT_INFO("%s done: size=0x%tx",
  1088. entry->mem_name,
  1089. dbg_ptr - entry->mem_ptr);
  1090. break;
  1091. }
  1092. } while (1);
  1093. }
  1094. BT_INFO("== btmrvl firmware dump end ==");
  1095. done:
  1096. sdio_release_host(card->func);
  1097. if (fw_dump_len == 0)
  1098. return;
  1099. fw_dump_data = vzalloc(fw_dump_len+1);
  1100. if (!fw_dump_data) {
  1101. BT_ERR("Vzalloc fw_dump_data fail!");
  1102. return;
  1103. }
  1104. fw_dump_ptr = fw_dump_data;
  1105. /* Dump all the memory data into single file, a userspace script will
  1106. be used to split all the memory data to multiple files*/
  1107. BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump start");
  1108. for (idx = 0; idx < dump_num; idx++) {
  1109. struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
  1110. if (entry->mem_ptr) {
  1111. strcpy(fw_dump_ptr, "========Start dump ");
  1112. fw_dump_ptr += strlen("========Start dump ");
  1113. strcpy(fw_dump_ptr, entry->mem_name);
  1114. fw_dump_ptr += strlen(entry->mem_name);
  1115. strcpy(fw_dump_ptr, "========\n");
  1116. fw_dump_ptr += strlen("========\n");
  1117. memcpy(fw_dump_ptr, entry->mem_ptr, entry->mem_size);
  1118. fw_dump_ptr += entry->mem_size;
  1119. strcpy(fw_dump_ptr, "\n========End dump========\n");
  1120. fw_dump_ptr += strlen("\n========End dump========\n");
  1121. vfree(mem_type_mapping_tbl[idx].mem_ptr);
  1122. mem_type_mapping_tbl[idx].mem_ptr = NULL;
  1123. }
  1124. }
  1125. /* fw_dump_data will be free in device coredump release function
  1126. after 5 min*/
  1127. dev_coredumpv(&priv->btmrvl_dev.hcidev->dev, fw_dump_data,
  1128. fw_dump_len, GFP_KERNEL);
  1129. BT_INFO("== btmrvl firmware dump to /sys/class/devcoredump end");
  1130. }
  1131. static int btmrvl_sdio_probe(struct sdio_func *func,
  1132. const struct sdio_device_id *id)
  1133. {
  1134. int ret = 0;
  1135. struct btmrvl_private *priv = NULL;
  1136. struct btmrvl_sdio_card *card = NULL;
  1137. BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
  1138. id->vendor, id->device, id->class, func->num);
  1139. card = devm_kzalloc(&func->dev, sizeof(*card), GFP_KERNEL);
  1140. if (!card)
  1141. return -ENOMEM;
  1142. card->func = func;
  1143. if (id->driver_data) {
  1144. struct btmrvl_sdio_device *data = (void *) id->driver_data;
  1145. card->helper = data->helper;
  1146. card->firmware = data->firmware;
  1147. card->reg = data->reg;
  1148. card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
  1149. card->support_pscan_win_report = data->support_pscan_win_report;
  1150. card->supports_fw_dump = data->supports_fw_dump;
  1151. }
  1152. if (btmrvl_sdio_register_dev(card) < 0) {
  1153. BT_ERR("Failed to register BT device!");
  1154. return -ENODEV;
  1155. }
  1156. /* Disable the interrupts on the card */
  1157. btmrvl_sdio_disable_host_int(card);
  1158. if (btmrvl_sdio_download_fw(card)) {
  1159. BT_ERR("Downloading firmware failed!");
  1160. ret = -ENODEV;
  1161. goto unreg_dev;
  1162. }
  1163. btmrvl_sdio_enable_host_int(card);
  1164. priv = btmrvl_add_card(card);
  1165. if (!priv) {
  1166. BT_ERR("Initializing card failed!");
  1167. ret = -ENODEV;
  1168. goto disable_host_int;
  1169. }
  1170. card->priv = priv;
  1171. /* Initialize the interface specific function pointers */
  1172. priv->hw_host_to_card = btmrvl_sdio_host_to_card;
  1173. priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
  1174. priv->hw_process_int_status = btmrvl_sdio_process_int_status;
  1175. priv->firmware_dump = btmrvl_sdio_dump_firmware;
  1176. if (btmrvl_register_hdev(priv)) {
  1177. BT_ERR("Register hdev failed!");
  1178. ret = -ENODEV;
  1179. goto disable_host_int;
  1180. }
  1181. return 0;
  1182. disable_host_int:
  1183. btmrvl_sdio_disable_host_int(card);
  1184. unreg_dev:
  1185. btmrvl_sdio_unregister_dev(card);
  1186. return ret;
  1187. }
  1188. static void btmrvl_sdio_remove(struct sdio_func *func)
  1189. {
  1190. struct btmrvl_sdio_card *card;
  1191. if (func) {
  1192. card = sdio_get_drvdata(func);
  1193. if (card) {
  1194. /* Send SHUTDOWN command & disable interrupt
  1195. * if user removes the module.
  1196. */
  1197. if (user_rmmod) {
  1198. btmrvl_send_module_cfg_cmd(card->priv,
  1199. MODULE_SHUTDOWN_REQ);
  1200. btmrvl_sdio_disable_host_int(card);
  1201. }
  1202. BT_DBG("unregester dev");
  1203. card->priv->surprise_removed = true;
  1204. btmrvl_sdio_unregister_dev(card);
  1205. btmrvl_remove_card(card->priv);
  1206. }
  1207. }
  1208. }
  1209. static int btmrvl_sdio_suspend(struct device *dev)
  1210. {
  1211. struct sdio_func *func = dev_to_sdio_func(dev);
  1212. struct btmrvl_sdio_card *card;
  1213. struct btmrvl_private *priv;
  1214. mmc_pm_flag_t pm_flags;
  1215. struct hci_dev *hcidev;
  1216. if (func) {
  1217. pm_flags = sdio_get_host_pm_caps(func);
  1218. BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
  1219. pm_flags);
  1220. if (!(pm_flags & MMC_PM_KEEP_POWER)) {
  1221. BT_ERR("%s: cannot remain alive while suspended",
  1222. sdio_func_id(func));
  1223. return -ENOSYS;
  1224. }
  1225. card = sdio_get_drvdata(func);
  1226. if (!card || !card->priv) {
  1227. BT_ERR("card or priv structure is not valid");
  1228. return 0;
  1229. }
  1230. } else {
  1231. BT_ERR("sdio_func is not specified");
  1232. return 0;
  1233. }
  1234. priv = card->priv;
  1235. hcidev = priv->btmrvl_dev.hcidev;
  1236. BT_DBG("%s: SDIO suspend", hcidev->name);
  1237. hci_suspend_dev(hcidev);
  1238. skb_queue_purge(&priv->adapter->tx_queue);
  1239. if (priv->adapter->hs_state != HS_ACTIVATED) {
  1240. if (btmrvl_enable_hs(priv)) {
  1241. BT_ERR("HS not actived, suspend failed!");
  1242. return -EBUSY;
  1243. }
  1244. }
  1245. priv->adapter->is_suspended = true;
  1246. /* We will keep the power when hs enabled successfully */
  1247. if (priv->adapter->hs_state == HS_ACTIVATED) {
  1248. BT_DBG("suspend with MMC_PM_KEEP_POWER");
  1249. return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  1250. } else {
  1251. BT_DBG("suspend without MMC_PM_KEEP_POWER");
  1252. return 0;
  1253. }
  1254. }
  1255. static int btmrvl_sdio_resume(struct device *dev)
  1256. {
  1257. struct sdio_func *func = dev_to_sdio_func(dev);
  1258. struct btmrvl_sdio_card *card;
  1259. struct btmrvl_private *priv;
  1260. mmc_pm_flag_t pm_flags;
  1261. struct hci_dev *hcidev;
  1262. if (func) {
  1263. pm_flags = sdio_get_host_pm_caps(func);
  1264. BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
  1265. pm_flags);
  1266. card = sdio_get_drvdata(func);
  1267. if (!card || !card->priv) {
  1268. BT_ERR("card or priv structure is not valid");
  1269. return 0;
  1270. }
  1271. } else {
  1272. BT_ERR("sdio_func is not specified");
  1273. return 0;
  1274. }
  1275. priv = card->priv;
  1276. if (!priv->adapter->is_suspended) {
  1277. BT_DBG("device already resumed");
  1278. return 0;
  1279. }
  1280. priv->hw_wakeup_firmware(priv);
  1281. priv->adapter->hs_state = HS_DEACTIVATED;
  1282. hcidev = priv->btmrvl_dev.hcidev;
  1283. BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
  1284. priv->adapter->is_suspended = false;
  1285. BT_DBG("%s: SDIO resume", hcidev->name);
  1286. hci_resume_dev(hcidev);
  1287. return 0;
  1288. }
  1289. static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
  1290. .suspend = btmrvl_sdio_suspend,
  1291. .resume = btmrvl_sdio_resume,
  1292. };
  1293. static struct sdio_driver bt_mrvl_sdio = {
  1294. .name = "btmrvl_sdio",
  1295. .id_table = btmrvl_sdio_ids,
  1296. .probe = btmrvl_sdio_probe,
  1297. .remove = btmrvl_sdio_remove,
  1298. .drv = {
  1299. .owner = THIS_MODULE,
  1300. .pm = &btmrvl_sdio_pm_ops,
  1301. }
  1302. };
  1303. static int __init btmrvl_sdio_init_module(void)
  1304. {
  1305. if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
  1306. BT_ERR("SDIO Driver Registration Failed");
  1307. return -ENODEV;
  1308. }
  1309. /* Clear the flag in case user removes the card. */
  1310. user_rmmod = 0;
  1311. return 0;
  1312. }
  1313. static void __exit btmrvl_sdio_exit_module(void)
  1314. {
  1315. /* Set the flag as user is removing this module. */
  1316. user_rmmod = 1;
  1317. sdio_unregister_driver(&bt_mrvl_sdio);
  1318. }
  1319. module_init(btmrvl_sdio_init_module);
  1320. module_exit(btmrvl_sdio_exit_module);
  1321. MODULE_AUTHOR("Marvell International Ltd.");
  1322. MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
  1323. MODULE_VERSION(VERSION);
  1324. MODULE_LICENSE("GPL v2");
  1325. /*(DEBLOBBED)*/