fwio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. /* Load a firmware file */
  130. ret = request_firmware(&firmware, fw_path, priv->pdev);
  131. if (ret) {
  132. pr_err("Can't load firmware file %s.\n", fw_path);
  133. goto error;
  134. }
  135. buf = kmalloc(DOWNLOAD_BLOCK_SIZE, GFP_KERNEL | GFP_DMA);
  136. if (!buf) {
  137. pr_err("Can't allocate firmware load buffer.\n");
  138. ret = -ENOMEM;
  139. goto error;
  140. }
  141. /* Check if the bootloader is ready */
  142. for (i = 0; i < 100; i += 1 + i / 2) {
  143. APB_READ(DOWNLOAD_IMAGE_SIZE_REG, val32);
  144. if (val32 == DOWNLOAD_I_AM_HERE)
  145. break;
  146. mdelay(i);
  147. } /* End of for loop */
  148. if (val32 != DOWNLOAD_I_AM_HERE) {
  149. pr_err("Bootloader is not ready.\n");
  150. ret = -ETIMEDOUT;
  151. goto error;
  152. }
  153. /* Calculcate number of download blocks */
  154. num_blocks = (firmware->size - 1) / DOWNLOAD_BLOCK_SIZE + 1;
  155. /* Updating the length in Download Ctrl Area */
  156. val32 = firmware->size; /* Explicit cast from size_t to u32 */
  157. APB_WRITE(DOWNLOAD_IMAGE_SIZE_REG, val32);
  158. /* Firmware downloading loop */
  159. for (block = 0; block < num_blocks; block++) {
  160. size_t tx_size;
  161. size_t block_size;
  162. /* check the download status */
  163. APB_READ(DOWNLOAD_STATUS_REG, val32);
  164. if (val32 != DOWNLOAD_PENDING) {
  165. pr_err("Bootloader reported error %d.\n", val32);
  166. ret = -EIO;
  167. goto error;
  168. }
  169. /* loop until put - get <= 24K */
  170. for (i = 0; i < 100; i++) {
  171. APB_READ(DOWNLOAD_GET_REG, get);
  172. if ((put - get) <=
  173. (DOWNLOAD_FIFO_SIZE - DOWNLOAD_BLOCK_SIZE))
  174. break;
  175. mdelay(i);
  176. }
  177. if ((put - get) > (DOWNLOAD_FIFO_SIZE - DOWNLOAD_BLOCK_SIZE)) {
  178. pr_err("Timeout waiting for FIFO.\n");
  179. ret = -ETIMEDOUT;
  180. goto error;
  181. }
  182. /* calculate the block size */
  183. tx_size = block_size = min((size_t)(firmware->size - put),
  184. (size_t)DOWNLOAD_BLOCK_SIZE);
  185. memcpy(buf, &firmware->data[put], block_size);
  186. if (block_size < DOWNLOAD_BLOCK_SIZE) {
  187. memset(&buf[block_size], 0,
  188. DOWNLOAD_BLOCK_SIZE - block_size);
  189. tx_size = DOWNLOAD_BLOCK_SIZE;
  190. }
  191. /* send the block to sram */
  192. ret = cw1200_apb_write(priv,
  193. CW1200_APB(DOWNLOAD_FIFO_OFFSET +
  194. (put & (DOWNLOAD_FIFO_SIZE - 1))),
  195. buf, tx_size);
  196. if (ret < 0) {
  197. pr_err("Can't write firmware block @ %d!\n",
  198. put & (DOWNLOAD_FIFO_SIZE - 1));
  199. goto error;
  200. }
  201. /* update the put register */
  202. put += block_size;
  203. APB_WRITE(DOWNLOAD_PUT_REG, put);
  204. } /* End of firmware download loop */
  205. /* Wait for the download completion */
  206. for (i = 0; i < 300; i += 1 + i / 2) {
  207. APB_READ(DOWNLOAD_STATUS_REG, val32);
  208. if (val32 != DOWNLOAD_PENDING)
  209. break;
  210. mdelay(i);
  211. }
  212. if (val32 != DOWNLOAD_SUCCESS) {
  213. pr_err("Wait for download completion failed: 0x%.8X\n", val32);
  214. ret = -ETIMEDOUT;
  215. goto error;
  216. } else {
  217. pr_info("Firmware download completed.\n");
  218. ret = 0;
  219. }
  220. error:
  221. kfree(buf);
  222. if (firmware)
  223. release_firmware(firmware);
  224. return ret;
  225. #undef APB_WRITE
  226. #undef APB_READ
  227. #undef REG_WRITE
  228. #undef REG_READ
  229. }
  230. static int config_reg_read(struct cw1200_common *priv, u32 *val)
  231. {
  232. switch (priv->hw_type) {
  233. case HIF_9000_SILICON_VERSATILE: {
  234. u16 val16;
  235. int ret = cw1200_reg_read_16(priv,
  236. ST90TDS_CONFIG_REG_ID,
  237. &val16);
  238. if (ret < 0)
  239. return ret;
  240. *val = val16;
  241. return 0;
  242. }
  243. case HIF_8601_VERSATILE:
  244. case HIF_8601_SILICON:
  245. default:
  246. cw1200_reg_read_32(priv, ST90TDS_CONFIG_REG_ID, val);
  247. break;
  248. }
  249. return 0;
  250. }
  251. static int config_reg_write(struct cw1200_common *priv, u32 val)
  252. {
  253. switch (priv->hw_type) {
  254. case HIF_9000_SILICON_VERSATILE:
  255. return cw1200_reg_write_16(priv,
  256. ST90TDS_CONFIG_REG_ID,
  257. (u16)val);
  258. case HIF_8601_VERSATILE:
  259. case HIF_8601_SILICON:
  260. default:
  261. return cw1200_reg_write_32(priv, ST90TDS_CONFIG_REG_ID, val);
  262. break;
  263. }
  264. return 0;
  265. }
  266. int cw1200_load_firmware(struct cw1200_common *priv)
  267. {
  268. int ret;
  269. int i;
  270. u32 val32;
  271. u16 val16;
  272. int major_revision = -1;
  273. /* Read CONFIG Register */
  274. ret = cw1200_reg_read_32(priv, ST90TDS_CONFIG_REG_ID, &val32);
  275. if (ret < 0) {
  276. pr_err("Can't read config register.\n");
  277. goto out;
  278. }
  279. if (val32 == 0 || val32 == 0xffffffff) {
  280. pr_err("Bad config register value (0x%08x)\n", val32);
  281. ret = -EIO;
  282. goto out;
  283. }
  284. priv->hw_type = cw1200_get_hw_type(val32, &major_revision);
  285. if (priv->hw_type < 0) {
  286. pr_err("Can't deduce hardware type.\n");
  287. ret = -ENOTSUPP;
  288. goto out;
  289. }
  290. /* Set DPLL Reg value, and read back to confirm writes work */
  291. ret = cw1200_reg_write_32(priv, ST90TDS_TSET_GEN_R_W_REG_ID,
  292. cw1200_dpll_from_clk(priv->hw_refclk));
  293. if (ret < 0) {
  294. pr_err("Can't write DPLL register.\n");
  295. goto out;
  296. }
  297. msleep(20);
  298. ret = cw1200_reg_read_32(priv,
  299. ST90TDS_TSET_GEN_R_W_REG_ID, &val32);
  300. if (ret < 0) {
  301. pr_err("Can't read DPLL register.\n");
  302. goto out;
  303. }
  304. if (val32 != cw1200_dpll_from_clk(priv->hw_refclk)) {
  305. pr_err("Unable to initialise DPLL register. Wrote 0x%.8X, Read 0x%.8X.\n",
  306. cw1200_dpll_from_clk(priv->hw_refclk), val32);
  307. ret = -EIO;
  308. goto out;
  309. }
  310. /* Set wakeup bit in device */
  311. ret = cw1200_reg_read_16(priv, ST90TDS_CONTROL_REG_ID, &val16);
  312. if (ret < 0) {
  313. pr_err("set_wakeup: can't read control register.\n");
  314. goto out;
  315. }
  316. ret = cw1200_reg_write_16(priv, ST90TDS_CONTROL_REG_ID,
  317. val16 | ST90TDS_CONT_WUP_BIT);
  318. if (ret < 0) {
  319. pr_err("set_wakeup: can't write control register.\n");
  320. goto out;
  321. }
  322. /* Wait for wakeup */
  323. for (i = 0; i < 300; i += (1 + i / 2)) {
  324. ret = cw1200_reg_read_16(priv,
  325. ST90TDS_CONTROL_REG_ID, &val16);
  326. if (ret < 0) {
  327. pr_err("wait_for_wakeup: can't read control register.\n");
  328. goto out;
  329. }
  330. if (val16 & ST90TDS_CONT_RDY_BIT)
  331. break;
  332. msleep(i);
  333. }
  334. if ((val16 & ST90TDS_CONT_RDY_BIT) == 0) {
  335. pr_err("wait_for_wakeup: device is not responding.\n");
  336. ret = -ETIMEDOUT;
  337. goto out;
  338. }
  339. switch (major_revision) {
  340. case 1:
  341. /* CW1200 Hardware detection logic : Check for CUT1.1 */
  342. ret = cw1200_ahb_read_32(priv, CW1200_CUT_ID_ADDR, &val32);
  343. if (ret) {
  344. pr_err("HW detection: can't read CUT ID.\n");
  345. goto out;
  346. }
  347. switch (val32) {
  348. case CW1200_CUT_11_ID_STR:
  349. pr_info("CW1x00 Cut 1.1 silicon detected.\n");
  350. priv->hw_revision = CW1200_HW_REV_CUT11;
  351. break;
  352. default:
  353. pr_info("CW1x00 Cut 1.0 silicon detected.\n");
  354. priv->hw_revision = CW1200_HW_REV_CUT10;
  355. break;
  356. }
  357. /* According to ST-E, CUT<2.0 has busted BA TID0-3.
  358. Just disable it entirely...
  359. */
  360. priv->ba_rx_tid_mask = 0;
  361. priv->ba_tx_tid_mask = 0;
  362. break;
  363. case 2: {
  364. u32 ar1, ar2, ar3;
  365. ret = cw1200_ahb_read_32(priv, CW1200_CUT2_ID_ADDR, &ar1);
  366. if (ret) {
  367. pr_err("(1) HW detection: can't read CUT ID\n");
  368. goto out;
  369. }
  370. ret = cw1200_ahb_read_32(priv, CW1200_CUT2_ID_ADDR + 4, &ar2);
  371. if (ret) {
  372. pr_err("(2) HW detection: can't read CUT ID.\n");
  373. goto out;
  374. }
  375. ret = cw1200_ahb_read_32(priv, CW1200_CUT2_ID_ADDR + 8, &ar3);
  376. if (ret) {
  377. pr_err("(3) HW detection: can't read CUT ID.\n");
  378. goto out;
  379. }
  380. if (ar1 == CW1200_CUT_22_ID_STR1 &&
  381. ar2 == CW1200_CUT_22_ID_STR2 &&
  382. ar3 == CW1200_CUT_22_ID_STR3) {
  383. pr_info("CW1x00 Cut 2.2 silicon detected.\n");
  384. priv->hw_revision = CW1200_HW_REV_CUT22;
  385. } else {
  386. pr_info("CW1x00 Cut 2.0 silicon detected.\n");
  387. priv->hw_revision = CW1200_HW_REV_CUT20;
  388. }
  389. break;
  390. }
  391. case 4:
  392. pr_info("CW1x60 silicon detected.\n");
  393. priv->hw_revision = CW1X60_HW_REV;
  394. break;
  395. default:
  396. pr_err("Unsupported silicon major revision %d.\n",
  397. major_revision);
  398. ret = -ENOTSUPP;
  399. goto out;
  400. }
  401. /* Checking for access mode */
  402. ret = config_reg_read(priv, &val32);
  403. if (ret < 0) {
  404. pr_err("Can't read config register.\n");
  405. goto out;
  406. }
  407. if (!(val32 & ST90TDS_CONFIG_ACCESS_MODE_BIT)) {
  408. pr_err("Device is already in QUEUE mode!\n");
  409. ret = -EINVAL;
  410. goto out;
  411. }
  412. switch (priv->hw_type) {
  413. case HIF_8601_SILICON:
  414. if (priv->hw_revision == CW1X60_HW_REV) {
  415. pr_err("Can't handle CW1160/1260 firmware load yet.\n");
  416. ret = -ENOTSUPP;
  417. goto out;
  418. }
  419. ret = cw1200_load_firmware_cw1200(priv);
  420. break;
  421. default:
  422. pr_err("Can't perform firmware load for hw type %d.\n",
  423. priv->hw_type);
  424. ret = -ENOTSUPP;
  425. goto out;
  426. }
  427. if (ret < 0) {
  428. pr_err("Firmware load error.\n");
  429. goto out;
  430. }
  431. /* Enable interrupt signalling */
  432. priv->hwbus_ops->lock(priv->hwbus_priv);
  433. ret = __cw1200_irq_enable(priv, 1);
  434. priv->hwbus_ops->unlock(priv->hwbus_priv);
  435. if (ret < 0)
  436. goto unsubscribe;
  437. /* Configure device for MESSSAGE MODE */
  438. ret = config_reg_read(priv, &val32);
  439. if (ret < 0) {
  440. pr_err("Can't read config register.\n");
  441. goto unsubscribe;
  442. }
  443. ret = config_reg_write(priv, val32 & ~ST90TDS_CONFIG_ACCESS_MODE_BIT);
  444. if (ret < 0) {
  445. pr_err("Can't write config register.\n");
  446. goto unsubscribe;
  447. }
  448. /* Unless we read the CONFIG Register we are
  449. * not able to get an interrupt
  450. */
  451. mdelay(10);
  452. config_reg_read(priv, &val32);
  453. out:
  454. return ret;
  455. unsubscribe:
  456. /* Disable interrupt signalling */
  457. priv->hwbus_ops->lock(priv->hwbus_priv);
  458. ret = __cw1200_irq_enable(priv, 0);
  459. priv->hwbus_ops->unlock(priv->hwbus_priv);
  460. return ret;
  461. }