fwio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. break;
  262. }
  263. return 0;
  264. }
  265. int cw1200_load_firmware(struct cw1200_common *priv)
  266. {
  267. int ret;
  268. int i;
  269. u32 val32;
  270. u16 val16;
  271. int major_revision = -1;
  272. /* Read CONFIG Register */
  273. ret = cw1200_reg_read_32(priv, ST90TDS_CONFIG_REG_ID, &val32);
  274. if (ret < 0) {
  275. pr_err("Can't read config register.\n");
  276. goto out;
  277. }
  278. if (val32 == 0 || val32 == 0xffffffff) {
  279. pr_err("Bad config register value (0x%08x)\n", val32);
  280. ret = -EIO;
  281. goto out;
  282. }
  283. priv->hw_type = cw1200_get_hw_type(val32, &major_revision);
  284. if (priv->hw_type < 0) {
  285. pr_err("Can't deduce hardware type.\n");
  286. ret = -ENOTSUPP;
  287. goto out;
  288. }
  289. /* Set DPLL Reg value, and read back to confirm writes work */
  290. ret = cw1200_reg_write_32(priv, ST90TDS_TSET_GEN_R_W_REG_ID,
  291. cw1200_dpll_from_clk(priv->hw_refclk));
  292. if (ret < 0) {
  293. pr_err("Can't write DPLL register.\n");
  294. goto out;
  295. }
  296. msleep(20);
  297. ret = cw1200_reg_read_32(priv,
  298. ST90TDS_TSET_GEN_R_W_REG_ID, &val32);
  299. if (ret < 0) {
  300. pr_err("Can't read DPLL register.\n");
  301. goto out;
  302. }
  303. if (val32 != cw1200_dpll_from_clk(priv->hw_refclk)) {
  304. pr_err("Unable to initialise DPLL register. Wrote 0x%.8X, Read 0x%.8X.\n",
  305. cw1200_dpll_from_clk(priv->hw_refclk), val32);
  306. ret = -EIO;
  307. goto out;
  308. }
  309. /* Set wakeup bit in device */
  310. ret = cw1200_reg_read_16(priv, ST90TDS_CONTROL_REG_ID, &val16);
  311. if (ret < 0) {
  312. pr_err("set_wakeup: can't read control register.\n");
  313. goto out;
  314. }
  315. ret = cw1200_reg_write_16(priv, ST90TDS_CONTROL_REG_ID,
  316. val16 | ST90TDS_CONT_WUP_BIT);
  317. if (ret < 0) {
  318. pr_err("set_wakeup: can't write control register.\n");
  319. goto out;
  320. }
  321. /* Wait for wakeup */
  322. for (i = 0; i < 300; i += (1 + i / 2)) {
  323. ret = cw1200_reg_read_16(priv,
  324. ST90TDS_CONTROL_REG_ID, &val16);
  325. if (ret < 0) {
  326. pr_err("wait_for_wakeup: can't read control register.\n");
  327. goto out;
  328. }
  329. if (val16 & ST90TDS_CONT_RDY_BIT)
  330. break;
  331. msleep(i);
  332. }
  333. if ((val16 & ST90TDS_CONT_RDY_BIT) == 0) {
  334. pr_err("wait_for_wakeup: device is not responding.\n");
  335. ret = -ETIMEDOUT;
  336. goto out;
  337. }
  338. switch (major_revision) {
  339. case 1:
  340. /* CW1200 Hardware detection logic : Check for CUT1.1 */
  341. ret = cw1200_ahb_read_32(priv, CW1200_CUT_ID_ADDR, &val32);
  342. if (ret) {
  343. pr_err("HW detection: can't read CUT ID.\n");
  344. goto out;
  345. }
  346. switch (val32) {
  347. case CW1200_CUT_11_ID_STR:
  348. pr_info("CW1x00 Cut 1.1 silicon detected.\n");
  349. priv->hw_revision = CW1200_HW_REV_CUT11;
  350. break;
  351. default:
  352. pr_info("CW1x00 Cut 1.0 silicon detected.\n");
  353. priv->hw_revision = CW1200_HW_REV_CUT10;
  354. break;
  355. }
  356. /* According to ST-E, CUT<2.0 has busted BA TID0-3.
  357. Just disable it entirely...
  358. */
  359. priv->ba_rx_tid_mask = 0;
  360. priv->ba_tx_tid_mask = 0;
  361. break;
  362. case 2: {
  363. u32 ar1, ar2, ar3;
  364. ret = cw1200_ahb_read_32(priv, CW1200_CUT2_ID_ADDR, &ar1);
  365. if (ret) {
  366. pr_err("(1) HW detection: can't read CUT ID\n");
  367. goto out;
  368. }
  369. ret = cw1200_ahb_read_32(priv, CW1200_CUT2_ID_ADDR + 4, &ar2);
  370. if (ret) {
  371. pr_err("(2) HW detection: can't read CUT ID.\n");
  372. goto out;
  373. }
  374. ret = cw1200_ahb_read_32(priv, CW1200_CUT2_ID_ADDR + 8, &ar3);
  375. if (ret) {
  376. pr_err("(3) HW detection: can't read CUT ID.\n");
  377. goto out;
  378. }
  379. if (ar1 == CW1200_CUT_22_ID_STR1 &&
  380. ar2 == CW1200_CUT_22_ID_STR2 &&
  381. ar3 == CW1200_CUT_22_ID_STR3) {
  382. pr_info("CW1x00 Cut 2.2 silicon detected.\n");
  383. priv->hw_revision = CW1200_HW_REV_CUT22;
  384. } else {
  385. pr_info("CW1x00 Cut 2.0 silicon detected.\n");
  386. priv->hw_revision = CW1200_HW_REV_CUT20;
  387. }
  388. break;
  389. }
  390. case 4:
  391. pr_info("CW1x60 silicon detected.\n");
  392. priv->hw_revision = CW1X60_HW_REV;
  393. break;
  394. default:
  395. pr_err("Unsupported silicon major revision %d.\n",
  396. major_revision);
  397. ret = -ENOTSUPP;
  398. goto out;
  399. }
  400. /* Checking for access mode */
  401. ret = config_reg_read(priv, &val32);
  402. if (ret < 0) {
  403. pr_err("Can't read config register.\n");
  404. goto out;
  405. }
  406. if (!(val32 & ST90TDS_CONFIG_ACCESS_MODE_BIT)) {
  407. pr_err("Device is already in QUEUE mode!\n");
  408. ret = -EINVAL;
  409. goto out;
  410. }
  411. switch (priv->hw_type) {
  412. case HIF_8601_SILICON:
  413. if (priv->hw_revision == CW1X60_HW_REV) {
  414. pr_err("Can't handle CW1160/1260 firmware load yet.\n");
  415. ret = -ENOTSUPP;
  416. goto out;
  417. }
  418. ret = cw1200_load_firmware_cw1200(priv);
  419. break;
  420. default:
  421. pr_err("Can't perform firmware load for hw type %d.\n",
  422. priv->hw_type);
  423. ret = -ENOTSUPP;
  424. goto out;
  425. }
  426. if (ret < 0) {
  427. pr_err("Firmware load error.\n");
  428. goto out;
  429. }
  430. /* Enable interrupt signalling */
  431. priv->hwbus_ops->lock(priv->hwbus_priv);
  432. ret = __cw1200_irq_enable(priv, 1);
  433. priv->hwbus_ops->unlock(priv->hwbus_priv);
  434. if (ret < 0)
  435. goto unsubscribe;
  436. /* Configure device for MESSSAGE MODE */
  437. ret = config_reg_read(priv, &val32);
  438. if (ret < 0) {
  439. pr_err("Can't read config register.\n");
  440. goto unsubscribe;
  441. }
  442. ret = config_reg_write(priv, val32 & ~ST90TDS_CONFIG_ACCESS_MODE_BIT);
  443. if (ret < 0) {
  444. pr_err("Can't write config register.\n");
  445. goto unsubscribe;
  446. }
  447. /* Unless we read the CONFIG Register we are
  448. * not able to get an interrupt
  449. */
  450. mdelay(10);
  451. config_reg_read(priv, &val32);
  452. out:
  453. return ret;
  454. unsubscribe:
  455. /* Disable interrupt signalling */
  456. priv->hwbus_ops->lock(priv->hwbus_priv);
  457. ret = __cw1200_irq_enable(priv, 0);
  458. priv->hwbus_ops->unlock(priv->hwbus_priv);
  459. return ret;
  460. }