fwio.c 12 KB

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