btmrvl_sdio.c 40 KB

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