fwio.c 12 KB

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