fwio.c 12 KB

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