btmrvl_sdio.c 40 KB

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