fwio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * Firmware I/O code for mac80211 ST-Ericsson CW1200 drivers
  3. *
  4. * Copyright (c) 2010, ST-Ericsson
  5. * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
  6. *
  7. * Based on:
  8. * ST-Ericsson UMAC CW1200 driver which is
  9. * Copyright (c) 2010, ST-Ericsson
  10. * Author: Ajitpal Singh <ajitpal.singh@stericsson.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/vmalloc.h>
  17. #include <linux/sched.h>
  18. #include <linux/firmware.h>
  19. #include "cw1200.h"
  20. #include "fwio.h"
  21. #include "hwio.h"
  22. #include "hwbus.h"
  23. #include "bh.h"
  24. static int cw1200_get_hw_type(u32 config_reg_val, int *major_revision)
  25. {
  26. int hw_type = -1;
  27. u32 silicon_type = (config_reg_val >> 24) & 0x7;
  28. u32 silicon_vers = (config_reg_val >> 31) & 0x1;
  29. switch (silicon_type) {
  30. case 0x00:
  31. *major_revision = 1;
  32. hw_type = HIF_9000_SILICON_VERSATILE;
  33. break;
  34. case 0x01:
  35. case 0x02: /* CW1x00 */
  36. case 0x04: /* CW1x60 */
  37. *major_revision = silicon_type;
  38. if (silicon_vers)
  39. hw_type = HIF_8601_VERSATILE;
  40. else
  41. hw_type = HIF_8601_SILICON;
  42. break;
  43. default:
  44. break;
  45. }
  46. return hw_type;
  47. }
  48. static int cw1200_load_firmware_cw1200(struct cw1200_common *priv)
  49. {
  50. int ret, block, num_blocks;
  51. unsigned i;
  52. u32 val32;
  53. u32 put = 0, get = 0;
  54. u8 *buf = NULL;
  55. const char *fw_path;
  56. const struct firmware *firmware = NULL;
  57. /* Macroses are local. */
  58. #define APB_WRITE(reg, val) \
  59. do { \
  60. ret = cw1200_apb_write_32(priv, CW1200_APB(reg), (val)); \
  61. if (ret < 0) \
  62. goto exit; \
  63. } while (0)
  64. #define APB_WRITE2(reg, val) \
  65. do { \
  66. ret = cw1200_apb_write_32(priv, CW1200_APB(reg), (val)); \
  67. if (ret < 0) \
  68. goto free_buffer; \
  69. } while (0)
  70. #define APB_READ(reg, val) \
  71. do { \
  72. ret = cw1200_apb_read_32(priv, CW1200_APB(reg), &(val)); \
  73. if (ret < 0) \
  74. goto free_buffer; \
  75. } while (0)
  76. #define REG_WRITE(reg, val) \
  77. do { \
  78. ret = cw1200_reg_write_32(priv, (reg), (val)); \
  79. if (ret < 0) \
  80. goto exit; \
  81. } while (0)
  82. #define REG_READ(reg, val) \
  83. do { \
  84. ret = cw1200_reg_read_32(priv, (reg), &(val)); \
  85. if (ret < 0) \
  86. goto exit; \
  87. } while (0)
  88. switch (priv->hw_revision) {
  89. case CW1200_HW_REV_CUT10:
  90. fw_path = FIRMWARE_CUT10;
  91. if (!priv->sdd_path)
  92. priv->sdd_path = SDD_FILE_10;
  93. break;
  94. case CW1200_HW_REV_CUT11:
  95. fw_path = FIRMWARE_CUT11;
  96. if (!priv->sdd_path)
  97. priv->sdd_path = SDD_FILE_11;
  98. break;
  99. case CW1200_HW_REV_CUT20:
  100. fw_path = FIRMWARE_CUT20;
  101. if (!priv->sdd_path)
  102. priv->sdd_path = SDD_FILE_20;
  103. break;
  104. case CW1200_HW_REV_CUT22:
  105. fw_path = FIRMWARE_CUT22;
  106. if (!priv->sdd_path)
  107. priv->sdd_path = SDD_FILE_22;
  108. break;
  109. case CW1X60_HW_REV:
  110. fw_path = FIRMWARE_CW1X60;
  111. if (!priv->sdd_path)
  112. priv->sdd_path = SDD_FILE_CW1X60;
  113. break;
  114. default:
  115. pr_err("Invalid silicon revision %d.\n", priv->hw_revision);
  116. return -EINVAL;
  117. }
  118. /* Initialize common registers */
  119. APB_WRITE(DOWNLOAD_IMAGE_SIZE_REG, DOWNLOAD_ARE_YOU_HERE);
  120. APB_WRITE(DOWNLOAD_PUT_REG, 0);
  121. APB_WRITE(DOWNLOAD_GET_REG, 0);
  122. APB_WRITE(DOWNLOAD_STATUS_REG, DOWNLOAD_PENDING);
  123. APB_WRITE(DOWNLOAD_FLAGS_REG, 0);
  124. /* Write the NOP Instruction */
  125. REG_WRITE(ST90TDS_SRAM_BASE_ADDR_REG_ID, 0xFFF20000);
  126. REG_WRITE(ST90TDS_AHB_DPORT_REG_ID, 0xEAFFFFFE);
  127. /* Release CPU from RESET */
  128. REG_READ(ST90TDS_CONFIG_REG_ID, val32);
  129. val32 &= ~ST90TDS_CONFIG_CPU_RESET_BIT;
  130. REG_WRITE(ST90TDS_CONFIG_REG_ID, val32);
  131. /* Enable Clock */
  132. val32 &= ~ST90TDS_CONFIG_CPU_CLK_DIS_BIT;
  133. REG_WRITE(ST90TDS_CONFIG_REG_ID, val32);
  134. /* Load a firmware file */
  135. ret = request_firmware(&firmware, fw_path, priv->pdev);
  136. if (ret) {
  137. pr_err("Can't load firmware file %s.\n", fw_path);
  138. goto exit;
  139. }
  140. buf = kmalloc(DOWNLOAD_BLOCK_SIZE, GFP_KERNEL | GFP_DMA);
  141. if (!buf) {
  142. pr_err("Can't allocate firmware load buffer.\n");
  143. ret = -ENOMEM;
  144. goto firmware_release;
  145. }
  146. /* Check if the bootloader is ready */
  147. for (i = 0; i < 100; i += 1 + i / 2) {
  148. APB_READ(DOWNLOAD_IMAGE_SIZE_REG, val32);
  149. if (val32 == DOWNLOAD_I_AM_HERE)
  150. break;
  151. mdelay(i);
  152. } /* End of for loop */
  153. if (val32 != DOWNLOAD_I_AM_HERE) {
  154. pr_err("Bootloader is not ready.\n");
  155. ret = -ETIMEDOUT;
  156. goto free_buffer;
  157. }
  158. /* Calculcate number of download blocks */
  159. num_blocks = (firmware->size - 1) / DOWNLOAD_BLOCK_SIZE + 1;
  160. /* Updating the length in Download Ctrl Area */
  161. val32 = firmware->size; /* Explicit cast from size_t to u32 */
  162. APB_WRITE2(DOWNLOAD_IMAGE_SIZE_REG, val32);
  163. /* Firmware downloading loop */
  164. for (block = 0; block < num_blocks; block++) {
  165. size_t tx_size;
  166. size_t block_size;
  167. /* check the download status */
  168. APB_READ(DOWNLOAD_STATUS_REG, val32);
  169. if (val32 != DOWNLOAD_PENDING) {
  170. pr_err("Bootloader reported error %d.\n", val32);
  171. ret = -EIO;
  172. goto free_buffer;
  173. }
  174. /* loop until put - get <= 24K */
  175. for (i = 0; i < 100; i++) {
  176. APB_READ(DOWNLOAD_GET_REG, get);
  177. if ((put - get) <=
  178. (DOWNLOAD_FIFO_SIZE - DOWNLOAD_BLOCK_SIZE))
  179. break;
  180. mdelay(i);
  181. }
  182. if ((put - get) > (DOWNLOAD_FIFO_SIZE - DOWNLOAD_BLOCK_SIZE)) {
  183. pr_err("Timeout waiting for FIFO.\n");
  184. ret = -ETIMEDOUT;
  185. goto free_buffer;
  186. }
  187. /* calculate the block size */
  188. tx_size = block_size = min_t(size_t, firmware->size - put,
  189. DOWNLOAD_BLOCK_SIZE);
  190. memcpy(buf, &firmware->data[put], block_size);
  191. if (block_size < DOWNLOAD_BLOCK_SIZE) {
  192. memset(&buf[block_size], 0,
  193. DOWNLOAD_BLOCK_SIZE - block_size);
  194. tx_size = DOWNLOAD_BLOCK_SIZE;
  195. }
  196. /* send the block to sram */
  197. ret = cw1200_apb_write(priv,
  198. CW1200_APB(DOWNLOAD_FIFO_OFFSET +
  199. (put & (DOWNLOAD_FIFO_SIZE - 1))),
  200. buf, tx_size);
  201. if (ret < 0) {
  202. pr_err("Can't write firmware block @ %d!\n",
  203. put & (DOWNLOAD_FIFO_SIZE - 1));
  204. goto free_buffer;
  205. }
  206. /* update the put register */
  207. put += block_size;
  208. APB_WRITE2(DOWNLOAD_PUT_REG, put);
  209. } /* End of firmware download loop */
  210. /* Wait for the download completion */
  211. for (i = 0; i < 300; i += 1 + i / 2) {
  212. APB_READ(DOWNLOAD_STATUS_REG, val32);
  213. if (val32 != DOWNLOAD_PENDING)
  214. break;
  215. mdelay(i);
  216. }
  217. if (val32 != DOWNLOAD_SUCCESS) {
  218. pr_err("Wait for download completion failed: 0x%.8X\n", val32);
  219. ret = -ETIMEDOUT;
  220. goto free_buffer;
  221. } else {
  222. pr_info("Firmware download completed.\n");
  223. ret = 0;
  224. }
  225. free_buffer:
  226. kfree(buf);
  227. firmware_release:
  228. release_firmware(firmware);
  229. exit:
  230. return ret;
  231. #undef APB_WRITE
  232. #undef APB_WRITE2
  233. #undef APB_READ
  234. #undef REG_WRITE
  235. #undef REG_READ
  236. }
  237. static int config_reg_read(struct cw1200_common *priv, u32 *val)
  238. {
  239. switch (priv->hw_type) {
  240. case HIF_9000_SILICON_VERSATILE: {
  241. u16 val16;
  242. int ret = cw1200_reg_read_16(priv,
  243. ST90TDS_CONFIG_REG_ID,
  244. &val16);
  245. if (ret < 0)
  246. return ret;
  247. *val = val16;
  248. return 0;
  249. }
  250. case HIF_8601_VERSATILE:
  251. case HIF_8601_SILICON:
  252. default:
  253. cw1200_reg_read_32(priv, ST90TDS_CONFIG_REG_ID, val);
  254. break;
  255. }
  256. return 0;
  257. }
  258. static int config_reg_write(struct cw1200_common *priv, u32 val)
  259. {
  260. switch (priv->hw_type) {
  261. case HIF_9000_SILICON_VERSATILE:
  262. return cw1200_reg_write_16(priv,
  263. ST90TDS_CONFIG_REG_ID,
  264. (u16)val);
  265. case HIF_8601_VERSATILE:
  266. case HIF_8601_SILICON:
  267. default:
  268. return cw1200_reg_write_32(priv, ST90TDS_CONFIG_REG_ID, val);
  269. }
  270. return 0;
  271. }
  272. int cw1200_load_firmware(struct cw1200_common *priv)
  273. {
  274. int ret;
  275. int i;
  276. u32 val32;
  277. u16 val16;
  278. int major_revision = -1;
  279. /* Read CONFIG Register */
  280. ret = cw1200_reg_read_32(priv, ST90TDS_CONFIG_REG_ID, &val32);
  281. if (ret < 0) {
  282. pr_err("Can't read config register.\n");
  283. goto out;
  284. }
  285. if (val32 == 0 || val32 == 0xffffffff) {
  286. pr_err("Bad config register value (0x%08x)\n", val32);
  287. ret = -EIO;
  288. goto out;
  289. }
  290. priv->hw_type = cw1200_get_hw_type(val32, &major_revision);
  291. if (priv->hw_type < 0) {
  292. pr_err("Can't deduce hardware type.\n");
  293. ret = -ENOTSUPP;
  294. goto out;
  295. }
  296. /* Set DPLL Reg value, and read back to confirm writes work */
  297. ret = cw1200_reg_write_32(priv, ST90TDS_TSET_GEN_R_W_REG_ID,
  298. cw1200_dpll_from_clk(priv->hw_refclk));
  299. if (ret < 0) {
  300. pr_err("Can't write DPLL register.\n");
  301. goto out;
  302. }
  303. msleep(20);
  304. ret = cw1200_reg_read_32(priv,
  305. ST90TDS_TSET_GEN_R_W_REG_ID, &val32);
  306. if (ret < 0) {
  307. pr_err("Can't read DPLL register.\n");
  308. goto out;
  309. }
  310. if (val32 != cw1200_dpll_from_clk(priv->hw_refclk)) {
  311. pr_err("Unable to initialise DPLL register. Wrote 0x%.8X, Read 0x%.8X.\n",
  312. cw1200_dpll_from_clk(priv->hw_refclk), val32);
  313. ret = -EIO;
  314. goto out;
  315. }
  316. /* Set wakeup bit in device */
  317. ret = cw1200_reg_read_16(priv, ST90TDS_CONTROL_REG_ID, &val16);
  318. if (ret < 0) {
  319. pr_err("set_wakeup: can't read control register.\n");
  320. goto out;
  321. }
  322. ret = cw1200_reg_write_16(priv, ST90TDS_CONTROL_REG_ID,
  323. val16 | ST90TDS_CONT_WUP_BIT);
  324. if (ret < 0) {
  325. pr_err("set_wakeup: can't write control register.\n");
  326. goto out;
  327. }
  328. /* Wait for wakeup */
  329. for (i = 0; i < 300; i += (1 + i / 2)) {
  330. ret = cw1200_reg_read_16(priv,
  331. ST90TDS_CONTROL_REG_ID, &val16);
  332. if (ret < 0) {
  333. pr_err("wait_for_wakeup: can't read control register.\n");
  334. goto out;
  335. }
  336. if (val16 & ST90TDS_CONT_RDY_BIT)
  337. break;
  338. msleep(i);
  339. }
  340. if ((val16 & ST90TDS_CONT_RDY_BIT) == 0) {
  341. pr_err("wait_for_wakeup: device is not responding.\n");
  342. ret = -ETIMEDOUT;
  343. goto out;
  344. }
  345. switch (major_revision) {
  346. case 1:
  347. /* CW1200 Hardware detection logic : Check for CUT1.1 */
  348. ret = cw1200_ahb_read_32(priv, CW1200_CUT_ID_ADDR, &val32);
  349. if (ret) {
  350. pr_err("HW detection: can't read CUT ID.\n");
  351. goto out;
  352. }
  353. switch (val32) {
  354. case CW1200_CUT_11_ID_STR:
  355. pr_info("CW1x00 Cut 1.1 silicon detected.\n");
  356. priv->hw_revision = CW1200_HW_REV_CUT11;
  357. break;
  358. default:
  359. pr_info("CW1x00 Cut 1.0 silicon detected.\n");
  360. priv->hw_revision = CW1200_HW_REV_CUT10;
  361. break;
  362. }
  363. /* According to ST-E, CUT<2.0 has busted BA TID0-3.
  364. Just disable it entirely...
  365. */
  366. priv->ba_rx_tid_mask = 0;
  367. priv->ba_tx_tid_mask = 0;
  368. break;
  369. case 2: {
  370. u32 ar1, ar2, ar3;
  371. ret = cw1200_ahb_read_32(priv, CW1200_CUT2_ID_ADDR, &ar1);
  372. if (ret) {
  373. pr_err("(1) HW detection: can't read CUT ID\n");
  374. goto out;
  375. }
  376. ret = cw1200_ahb_read_32(priv, CW1200_CUT2_ID_ADDR + 4, &ar2);
  377. if (ret) {
  378. pr_err("(2) HW detection: can't read CUT ID.\n");
  379. goto out;
  380. }
  381. ret = cw1200_ahb_read_32(priv, CW1200_CUT2_ID_ADDR + 8, &ar3);
  382. if (ret) {
  383. pr_err("(3) HW detection: can't read CUT ID.\n");
  384. goto out;
  385. }
  386. if (ar1 == CW1200_CUT_22_ID_STR1 &&
  387. ar2 == CW1200_CUT_22_ID_STR2 &&
  388. ar3 == CW1200_CUT_22_ID_STR3) {
  389. pr_info("CW1x00 Cut 2.2 silicon detected.\n");
  390. priv->hw_revision = CW1200_HW_REV_CUT22;
  391. } else {
  392. pr_info("CW1x00 Cut 2.0 silicon detected.\n");
  393. priv->hw_revision = CW1200_HW_REV_CUT20;
  394. }
  395. break;
  396. }
  397. case 4:
  398. pr_info("CW1x60 silicon detected.\n");
  399. priv->hw_revision = CW1X60_HW_REV;
  400. break;
  401. default:
  402. pr_err("Unsupported silicon major revision %d.\n",
  403. major_revision);
  404. ret = -ENOTSUPP;
  405. goto out;
  406. }
  407. /* Checking for access mode */
  408. ret = config_reg_read(priv, &val32);
  409. if (ret < 0) {
  410. pr_err("Can't read config register.\n");
  411. goto out;
  412. }
  413. if (!(val32 & ST90TDS_CONFIG_ACCESS_MODE_BIT)) {
  414. pr_err("Device is already in QUEUE mode!\n");
  415. ret = -EINVAL;
  416. goto out;
  417. }
  418. switch (priv->hw_type) {
  419. case HIF_8601_SILICON:
  420. if (priv->hw_revision == CW1X60_HW_REV) {
  421. pr_err("Can't handle CW1160/1260 firmware load yet.\n");
  422. ret = -ENOTSUPP;
  423. goto out;
  424. }
  425. ret = cw1200_load_firmware_cw1200(priv);
  426. break;
  427. default:
  428. pr_err("Can't perform firmware load for hw type %d.\n",
  429. priv->hw_type);
  430. ret = -ENOTSUPP;
  431. goto out;
  432. }
  433. if (ret < 0) {
  434. pr_err("Firmware load error.\n");
  435. goto out;
  436. }
  437. /* Enable interrupt signalling */
  438. priv->hwbus_ops->lock(priv->hwbus_priv);
  439. ret = __cw1200_irq_enable(priv, 1);
  440. priv->hwbus_ops->unlock(priv->hwbus_priv);
  441. if (ret < 0)
  442. goto unsubscribe;
  443. /* Configure device for MESSSAGE MODE */
  444. ret = config_reg_read(priv, &val32);
  445. if (ret < 0) {
  446. pr_err("Can't read config register.\n");
  447. goto unsubscribe;
  448. }
  449. ret = config_reg_write(priv, val32 & ~ST90TDS_CONFIG_ACCESS_MODE_BIT);
  450. if (ret < 0) {
  451. pr_err("Can't write config register.\n");
  452. goto unsubscribe;
  453. }
  454. /* Unless we read the CONFIG Register we are
  455. * not able to get an interrupt
  456. */
  457. mdelay(10);
  458. config_reg_read(priv, &val32);
  459. out:
  460. return ret;
  461. unsubscribe:
  462. /* Disable interrupt signalling */
  463. priv->hwbus_ops->lock(priv->hwbus_priv);
  464. ret = __cw1200_irq_enable(priv, 0);
  465. priv->hwbus_ops->unlock(priv->hwbus_priv);
  466. return ret;
  467. }