btmrvl_sdio.c 38 KB

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