cadence_qspi_apb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. /*
  2. * Copyright (C) 2012 Altera Corporation <www.altera.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * - Neither the name of the Altera Corporation nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL ALTERA CORPORATION BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <common.h>
  28. #include <asm/io.h>
  29. #include <linux/errno.h>
  30. #include <wait_bit.h>
  31. #include <spi.h>
  32. #include <malloc.h>
  33. #include "cadence_qspi.h"
  34. #define CQSPI_REG_POLL_US 1 /* 1us */
  35. #define CQSPI_REG_RETRY 10000
  36. #define CQSPI_POLL_IDLE_RETRY 3
  37. /* Transfer mode */
  38. #define CQSPI_INST_TYPE_SINGLE 0
  39. #define CQSPI_INST_TYPE_DUAL 1
  40. #define CQSPI_INST_TYPE_QUAD 2
  41. #define CQSPI_STIG_DATA_LEN_MAX 8
  42. #define CQSPI_DUMMY_CLKS_PER_BYTE 8
  43. #define CQSPI_DUMMY_BYTES_MAX 4
  44. /****************************************************************************
  45. * Controller's configuration and status register (offset from QSPI_BASE)
  46. ****************************************************************************/
  47. #define CQSPI_REG_CONFIG 0x00
  48. #define CQSPI_REG_CONFIG_ENABLE BIT(0)
  49. #define CQSPI_REG_CONFIG_CLK_POL BIT(1)
  50. #define CQSPI_REG_CONFIG_CLK_PHA BIT(2)
  51. #define CQSPI_REG_CONFIG_DIRECT BIT(7)
  52. #define CQSPI_REG_CONFIG_DECODE BIT(9)
  53. #define CQSPI_REG_CONFIG_XIP_IMM BIT(18)
  54. #define CQSPI_REG_CONFIG_CHIPSELECT_LSB 10
  55. #define CQSPI_REG_CONFIG_BAUD_LSB 19
  56. #define CQSPI_REG_CONFIG_IDLE_LSB 31
  57. #define CQSPI_REG_CONFIG_CHIPSELECT_MASK 0xF
  58. #define CQSPI_REG_CONFIG_BAUD_MASK 0xF
  59. #define CQSPI_REG_RD_INSTR 0x04
  60. #define CQSPI_REG_RD_INSTR_OPCODE_LSB 0
  61. #define CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB 8
  62. #define CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB 12
  63. #define CQSPI_REG_RD_INSTR_TYPE_DATA_LSB 16
  64. #define CQSPI_REG_RD_INSTR_MODE_EN_LSB 20
  65. #define CQSPI_REG_RD_INSTR_DUMMY_LSB 24
  66. #define CQSPI_REG_RD_INSTR_TYPE_INSTR_MASK 0x3
  67. #define CQSPI_REG_RD_INSTR_TYPE_ADDR_MASK 0x3
  68. #define CQSPI_REG_RD_INSTR_TYPE_DATA_MASK 0x3
  69. #define CQSPI_REG_RD_INSTR_DUMMY_MASK 0x1F
  70. #define CQSPI_REG_WR_INSTR 0x08
  71. #define CQSPI_REG_WR_INSTR_OPCODE_LSB 0
  72. #define CQSPI_REG_DELAY 0x0C
  73. #define CQSPI_REG_DELAY_TSLCH_LSB 0
  74. #define CQSPI_REG_DELAY_TCHSH_LSB 8
  75. #define CQSPI_REG_DELAY_TSD2D_LSB 16
  76. #define CQSPI_REG_DELAY_TSHSL_LSB 24
  77. #define CQSPI_REG_DELAY_TSLCH_MASK 0xFF
  78. #define CQSPI_REG_DELAY_TCHSH_MASK 0xFF
  79. #define CQSPI_REG_DELAY_TSD2D_MASK 0xFF
  80. #define CQSPI_REG_DELAY_TSHSL_MASK 0xFF
  81. #define CQSPI_REG_RD_DATA_CAPTURE 0x10
  82. #define CQSPI_REG_RD_DATA_CAPTURE_BYPASS BIT(0)
  83. #define CQSPI_REG_RD_DATA_CAPTURE_DELAY_LSB 1
  84. #define CQSPI_REG_RD_DATA_CAPTURE_DELAY_MASK 0xF
  85. #define CQSPI_REG_SIZE 0x14
  86. #define CQSPI_REG_SIZE_ADDRESS_LSB 0
  87. #define CQSPI_REG_SIZE_PAGE_LSB 4
  88. #define CQSPI_REG_SIZE_BLOCK_LSB 16
  89. #define CQSPI_REG_SIZE_ADDRESS_MASK 0xF
  90. #define CQSPI_REG_SIZE_PAGE_MASK 0xFFF
  91. #define CQSPI_REG_SIZE_BLOCK_MASK 0x3F
  92. #define CQSPI_REG_SRAMPARTITION 0x18
  93. #define CQSPI_REG_INDIRECTTRIGGER 0x1C
  94. #define CQSPI_REG_REMAP 0x24
  95. #define CQSPI_REG_MODE_BIT 0x28
  96. #define CQSPI_REG_SDRAMLEVEL 0x2C
  97. #define CQSPI_REG_SDRAMLEVEL_RD_LSB 0
  98. #define CQSPI_REG_SDRAMLEVEL_WR_LSB 16
  99. #define CQSPI_REG_SDRAMLEVEL_RD_MASK 0xFFFF
  100. #define CQSPI_REG_SDRAMLEVEL_WR_MASK 0xFFFF
  101. #define CQSPI_REG_IRQSTATUS 0x40
  102. #define CQSPI_REG_IRQMASK 0x44
  103. #define CQSPI_REG_INDIRECTRD 0x60
  104. #define CQSPI_REG_INDIRECTRD_START BIT(0)
  105. #define CQSPI_REG_INDIRECTRD_CANCEL BIT(1)
  106. #define CQSPI_REG_INDIRECTRD_INPROGRESS BIT(2)
  107. #define CQSPI_REG_INDIRECTRD_DONE BIT(5)
  108. #define CQSPI_REG_INDIRECTRDWATERMARK 0x64
  109. #define CQSPI_REG_INDIRECTRDSTARTADDR 0x68
  110. #define CQSPI_REG_INDIRECTRDBYTES 0x6C
  111. #define CQSPI_REG_CMDCTRL 0x90
  112. #define CQSPI_REG_CMDCTRL_EXECUTE BIT(0)
  113. #define CQSPI_REG_CMDCTRL_INPROGRESS BIT(1)
  114. #define CQSPI_REG_CMDCTRL_DUMMY_LSB 7
  115. #define CQSPI_REG_CMDCTRL_WR_BYTES_LSB 12
  116. #define CQSPI_REG_CMDCTRL_WR_EN_LSB 15
  117. #define CQSPI_REG_CMDCTRL_ADD_BYTES_LSB 16
  118. #define CQSPI_REG_CMDCTRL_ADDR_EN_LSB 19
  119. #define CQSPI_REG_CMDCTRL_RD_BYTES_LSB 20
  120. #define CQSPI_REG_CMDCTRL_RD_EN_LSB 23
  121. #define CQSPI_REG_CMDCTRL_OPCODE_LSB 24
  122. #define CQSPI_REG_CMDCTRL_DUMMY_MASK 0x1F
  123. #define CQSPI_REG_CMDCTRL_WR_BYTES_MASK 0x7
  124. #define CQSPI_REG_CMDCTRL_ADD_BYTES_MASK 0x3
  125. #define CQSPI_REG_CMDCTRL_RD_BYTES_MASK 0x7
  126. #define CQSPI_REG_CMDCTRL_OPCODE_MASK 0xFF
  127. #define CQSPI_REG_INDIRECTWR 0x70
  128. #define CQSPI_REG_INDIRECTWR_START BIT(0)
  129. #define CQSPI_REG_INDIRECTWR_CANCEL BIT(1)
  130. #define CQSPI_REG_INDIRECTWR_INPROGRESS BIT(2)
  131. #define CQSPI_REG_INDIRECTWR_DONE BIT(5)
  132. #define CQSPI_REG_INDIRECTWRWATERMARK 0x74
  133. #define CQSPI_REG_INDIRECTWRSTARTADDR 0x78
  134. #define CQSPI_REG_INDIRECTWRBYTES 0x7C
  135. #define CQSPI_REG_CMDADDRESS 0x94
  136. #define CQSPI_REG_CMDREADDATALOWER 0xA0
  137. #define CQSPI_REG_CMDREADDATAUPPER 0xA4
  138. #define CQSPI_REG_CMDWRITEDATALOWER 0xA8
  139. #define CQSPI_REG_CMDWRITEDATAUPPER 0xAC
  140. #define CQSPI_REG_IS_IDLE(base) \
  141. ((readl(base + CQSPI_REG_CONFIG) >> \
  142. CQSPI_REG_CONFIG_IDLE_LSB) & 0x1)
  143. #define CQSPI_GET_RD_SRAM_LEVEL(reg_base) \
  144. (((readl(reg_base + CQSPI_REG_SDRAMLEVEL)) >> \
  145. CQSPI_REG_SDRAMLEVEL_RD_LSB) & CQSPI_REG_SDRAMLEVEL_RD_MASK)
  146. #define CQSPI_GET_WR_SRAM_LEVEL(reg_base) \
  147. (((readl(reg_base + CQSPI_REG_SDRAMLEVEL)) >> \
  148. CQSPI_REG_SDRAMLEVEL_WR_LSB) & CQSPI_REG_SDRAMLEVEL_WR_MASK)
  149. static unsigned int cadence_qspi_apb_cmd2addr(const unsigned char *addr_buf,
  150. unsigned int addr_width)
  151. {
  152. unsigned int addr;
  153. addr = (addr_buf[0] << 16) | (addr_buf[1] << 8) | addr_buf[2];
  154. if (addr_width == 4)
  155. addr = (addr << 8) | addr_buf[3];
  156. return addr;
  157. }
  158. void cadence_qspi_apb_controller_enable(void *reg_base)
  159. {
  160. unsigned int reg;
  161. reg = readl(reg_base + CQSPI_REG_CONFIG);
  162. reg |= CQSPI_REG_CONFIG_ENABLE;
  163. writel(reg, reg_base + CQSPI_REG_CONFIG);
  164. }
  165. void cadence_qspi_apb_controller_disable(void *reg_base)
  166. {
  167. unsigned int reg;
  168. reg = readl(reg_base + CQSPI_REG_CONFIG);
  169. reg &= ~CQSPI_REG_CONFIG_ENABLE;
  170. writel(reg, reg_base + CQSPI_REG_CONFIG);
  171. }
  172. /* Return 1 if idle, otherwise return 0 (busy). */
  173. static unsigned int cadence_qspi_wait_idle(void *reg_base)
  174. {
  175. unsigned int start, count = 0;
  176. /* timeout in unit of ms */
  177. unsigned int timeout = 5000;
  178. start = get_timer(0);
  179. for ( ; get_timer(start) < timeout ; ) {
  180. if (CQSPI_REG_IS_IDLE(reg_base))
  181. count++;
  182. else
  183. count = 0;
  184. /*
  185. * Ensure the QSPI controller is in true idle state after
  186. * reading back the same idle status consecutively
  187. */
  188. if (count >= CQSPI_POLL_IDLE_RETRY)
  189. return 1;
  190. }
  191. /* Timeout, still in busy mode. */
  192. printf("QSPI: QSPI is still busy after poll for %d times.\n",
  193. CQSPI_REG_RETRY);
  194. return 0;
  195. }
  196. void cadence_qspi_apb_readdata_capture(void *reg_base,
  197. unsigned int bypass, unsigned int delay)
  198. {
  199. unsigned int reg;
  200. cadence_qspi_apb_controller_disable(reg_base);
  201. reg = readl(reg_base + CQSPI_REG_RD_DATA_CAPTURE);
  202. if (bypass)
  203. reg |= CQSPI_REG_RD_DATA_CAPTURE_BYPASS;
  204. else
  205. reg &= ~CQSPI_REG_RD_DATA_CAPTURE_BYPASS;
  206. reg &= ~(CQSPI_REG_RD_DATA_CAPTURE_DELAY_MASK
  207. << CQSPI_REG_RD_DATA_CAPTURE_DELAY_LSB);
  208. reg |= (delay & CQSPI_REG_RD_DATA_CAPTURE_DELAY_MASK)
  209. << CQSPI_REG_RD_DATA_CAPTURE_DELAY_LSB;
  210. writel(reg, reg_base + CQSPI_REG_RD_DATA_CAPTURE);
  211. cadence_qspi_apb_controller_enable(reg_base);
  212. }
  213. void cadence_qspi_apb_config_baudrate_div(void *reg_base,
  214. unsigned int ref_clk_hz, unsigned int sclk_hz)
  215. {
  216. unsigned int reg;
  217. unsigned int div;
  218. cadence_qspi_apb_controller_disable(reg_base);
  219. reg = readl(reg_base + CQSPI_REG_CONFIG);
  220. reg &= ~(CQSPI_REG_CONFIG_BAUD_MASK << CQSPI_REG_CONFIG_BAUD_LSB);
  221. /*
  222. * The baud_div field in the config reg is 4 bits, and the ref clock is
  223. * divided by 2 * (baud_div + 1). Round up the divider to ensure the
  224. * SPI clock rate is less than or equal to the requested clock rate.
  225. */
  226. div = DIV_ROUND_UP(ref_clk_hz, sclk_hz * 2) - 1;
  227. /* ensure the baud rate doesn't exceed the max value */
  228. if (div > CQSPI_REG_CONFIG_BAUD_MASK)
  229. div = CQSPI_REG_CONFIG_BAUD_MASK;
  230. debug("%s: ref_clk %dHz sclk %dHz Div 0x%x, actual %dHz\n", __func__,
  231. ref_clk_hz, sclk_hz, div, ref_clk_hz / (2 * (div + 1)));
  232. reg |= (div << CQSPI_REG_CONFIG_BAUD_LSB);
  233. writel(reg, reg_base + CQSPI_REG_CONFIG);
  234. cadence_qspi_apb_controller_enable(reg_base);
  235. }
  236. void cadence_qspi_apb_set_clk_mode(void *reg_base, uint mode)
  237. {
  238. unsigned int reg;
  239. cadence_qspi_apb_controller_disable(reg_base);
  240. reg = readl(reg_base + CQSPI_REG_CONFIG);
  241. reg &= ~(CQSPI_REG_CONFIG_CLK_POL | CQSPI_REG_CONFIG_CLK_PHA);
  242. if (mode & SPI_CPOL)
  243. reg |= CQSPI_REG_CONFIG_CLK_POL;
  244. if (mode & SPI_CPHA)
  245. reg |= CQSPI_REG_CONFIG_CLK_PHA;
  246. writel(reg, reg_base + CQSPI_REG_CONFIG);
  247. cadence_qspi_apb_controller_enable(reg_base);
  248. }
  249. void cadence_qspi_apb_chipselect(void *reg_base,
  250. unsigned int chip_select, unsigned int decoder_enable)
  251. {
  252. unsigned int reg;
  253. cadence_qspi_apb_controller_disable(reg_base);
  254. debug("%s : chipselect %d decode %d\n", __func__, chip_select,
  255. decoder_enable);
  256. reg = readl(reg_base + CQSPI_REG_CONFIG);
  257. /* docoder */
  258. if (decoder_enable) {
  259. reg |= CQSPI_REG_CONFIG_DECODE;
  260. } else {
  261. reg &= ~CQSPI_REG_CONFIG_DECODE;
  262. /* Convert CS if without decoder.
  263. * CS0 to 4b'1110
  264. * CS1 to 4b'1101
  265. * CS2 to 4b'1011
  266. * CS3 to 4b'0111
  267. */
  268. chip_select = 0xF & ~(1 << chip_select);
  269. }
  270. reg &= ~(CQSPI_REG_CONFIG_CHIPSELECT_MASK
  271. << CQSPI_REG_CONFIG_CHIPSELECT_LSB);
  272. reg |= (chip_select & CQSPI_REG_CONFIG_CHIPSELECT_MASK)
  273. << CQSPI_REG_CONFIG_CHIPSELECT_LSB;
  274. writel(reg, reg_base + CQSPI_REG_CONFIG);
  275. cadence_qspi_apb_controller_enable(reg_base);
  276. }
  277. void cadence_qspi_apb_delay(void *reg_base,
  278. unsigned int ref_clk, unsigned int sclk_hz,
  279. unsigned int tshsl_ns, unsigned int tsd2d_ns,
  280. unsigned int tchsh_ns, unsigned int tslch_ns)
  281. {
  282. unsigned int ref_clk_ns;
  283. unsigned int sclk_ns;
  284. unsigned int tshsl, tchsh, tslch, tsd2d;
  285. unsigned int reg;
  286. cadence_qspi_apb_controller_disable(reg_base);
  287. /* Convert to ns. */
  288. ref_clk_ns = DIV_ROUND_UP(1000000000, ref_clk);
  289. /* Convert to ns. */
  290. sclk_ns = DIV_ROUND_UP(1000000000, sclk_hz);
  291. /* The controller adds additional delay to that programmed in the reg */
  292. if (tshsl_ns >= sclk_ns + ref_clk_ns)
  293. tshsl_ns -= sclk_ns + ref_clk_ns;
  294. if (tchsh_ns >= sclk_ns + 3 * ref_clk_ns)
  295. tchsh_ns -= sclk_ns + 3 * ref_clk_ns;
  296. tshsl = DIV_ROUND_UP(tshsl_ns, ref_clk_ns);
  297. tchsh = DIV_ROUND_UP(tchsh_ns, ref_clk_ns);
  298. tslch = DIV_ROUND_UP(tslch_ns, ref_clk_ns);
  299. tsd2d = DIV_ROUND_UP(tsd2d_ns, ref_clk_ns);
  300. reg = ((tshsl & CQSPI_REG_DELAY_TSHSL_MASK)
  301. << CQSPI_REG_DELAY_TSHSL_LSB);
  302. reg |= ((tchsh & CQSPI_REG_DELAY_TCHSH_MASK)
  303. << CQSPI_REG_DELAY_TCHSH_LSB);
  304. reg |= ((tslch & CQSPI_REG_DELAY_TSLCH_MASK)
  305. << CQSPI_REG_DELAY_TSLCH_LSB);
  306. reg |= ((tsd2d & CQSPI_REG_DELAY_TSD2D_MASK)
  307. << CQSPI_REG_DELAY_TSD2D_LSB);
  308. writel(reg, reg_base + CQSPI_REG_DELAY);
  309. cadence_qspi_apb_controller_enable(reg_base);
  310. }
  311. void cadence_qspi_apb_controller_init(struct cadence_spi_platdata *plat)
  312. {
  313. unsigned reg;
  314. cadence_qspi_apb_controller_disable(plat->regbase);
  315. /* Configure the device size and address bytes */
  316. reg = readl(plat->regbase + CQSPI_REG_SIZE);
  317. /* Clear the previous value */
  318. reg &= ~(CQSPI_REG_SIZE_PAGE_MASK << CQSPI_REG_SIZE_PAGE_LSB);
  319. reg &= ~(CQSPI_REG_SIZE_BLOCK_MASK << CQSPI_REG_SIZE_BLOCK_LSB);
  320. reg |= (plat->page_size << CQSPI_REG_SIZE_PAGE_LSB);
  321. reg |= (plat->block_size << CQSPI_REG_SIZE_BLOCK_LSB);
  322. writel(reg, plat->regbase + CQSPI_REG_SIZE);
  323. /* Configure the remap address register, no remap */
  324. writel(0, plat->regbase + CQSPI_REG_REMAP);
  325. /* Indirect mode configurations */
  326. writel(plat->fifo_depth / 2, plat->regbase + CQSPI_REG_SRAMPARTITION);
  327. /* Disable all interrupts */
  328. writel(0, plat->regbase + CQSPI_REG_IRQMASK);
  329. cadence_qspi_apb_controller_enable(plat->regbase);
  330. }
  331. static int cadence_qspi_apb_exec_flash_cmd(void *reg_base,
  332. unsigned int reg)
  333. {
  334. unsigned int retry = CQSPI_REG_RETRY;
  335. /* Write the CMDCTRL without start execution. */
  336. writel(reg, reg_base + CQSPI_REG_CMDCTRL);
  337. /* Start execute */
  338. reg |= CQSPI_REG_CMDCTRL_EXECUTE;
  339. writel(reg, reg_base + CQSPI_REG_CMDCTRL);
  340. while (retry--) {
  341. reg = readl(reg_base + CQSPI_REG_CMDCTRL);
  342. if ((reg & CQSPI_REG_CMDCTRL_INPROGRESS) == 0)
  343. break;
  344. udelay(1);
  345. }
  346. if (!retry) {
  347. printf("QSPI: flash command execution timeout\n");
  348. return -EIO;
  349. }
  350. /* Polling QSPI idle status. */
  351. if (!cadence_qspi_wait_idle(reg_base))
  352. return -EIO;
  353. return 0;
  354. }
  355. /* For command RDID, RDSR. */
  356. int cadence_qspi_apb_command_read(void *reg_base,
  357. unsigned int cmdlen, const u8 *cmdbuf, unsigned int rxlen,
  358. u8 *rxbuf)
  359. {
  360. unsigned int reg;
  361. unsigned int read_len;
  362. int status;
  363. if (!cmdlen || rxlen > CQSPI_STIG_DATA_LEN_MAX || rxbuf == NULL) {
  364. printf("QSPI: Invalid input arguments cmdlen %d rxlen %d\n",
  365. cmdlen, rxlen);
  366. return -EINVAL;
  367. }
  368. reg = cmdbuf[0] << CQSPI_REG_CMDCTRL_OPCODE_LSB;
  369. reg |= (0x1 << CQSPI_REG_CMDCTRL_RD_EN_LSB);
  370. /* 0 means 1 byte. */
  371. reg |= (((rxlen - 1) & CQSPI_REG_CMDCTRL_RD_BYTES_MASK)
  372. << CQSPI_REG_CMDCTRL_RD_BYTES_LSB);
  373. status = cadence_qspi_apb_exec_flash_cmd(reg_base, reg);
  374. if (status != 0)
  375. return status;
  376. reg = readl(reg_base + CQSPI_REG_CMDREADDATALOWER);
  377. /* Put the read value into rx_buf */
  378. read_len = (rxlen > 4) ? 4 : rxlen;
  379. memcpy(rxbuf, &reg, read_len);
  380. rxbuf += read_len;
  381. if (rxlen > 4) {
  382. reg = readl(reg_base + CQSPI_REG_CMDREADDATAUPPER);
  383. read_len = rxlen - read_len;
  384. memcpy(rxbuf, &reg, read_len);
  385. }
  386. return 0;
  387. }
  388. /* For commands: WRSR, WREN, WRDI, CHIP_ERASE, BE, etc. */
  389. int cadence_qspi_apb_command_write(void *reg_base, unsigned int cmdlen,
  390. const u8 *cmdbuf, unsigned int txlen, const u8 *txbuf)
  391. {
  392. unsigned int reg = 0;
  393. unsigned int addr_value;
  394. unsigned int wr_data;
  395. unsigned int wr_len;
  396. if (!cmdlen || cmdlen > 5 || txlen > 8 || cmdbuf == NULL) {
  397. printf("QSPI: Invalid input arguments cmdlen %d txlen %d\n",
  398. cmdlen, txlen);
  399. return -EINVAL;
  400. }
  401. reg |= cmdbuf[0] << CQSPI_REG_CMDCTRL_OPCODE_LSB;
  402. if (cmdlen == 4 || cmdlen == 5) {
  403. /* Command with address */
  404. reg |= (0x1 << CQSPI_REG_CMDCTRL_ADDR_EN_LSB);
  405. /* Number of bytes to write. */
  406. reg |= ((cmdlen - 2) & CQSPI_REG_CMDCTRL_ADD_BYTES_MASK)
  407. << CQSPI_REG_CMDCTRL_ADD_BYTES_LSB;
  408. /* Get address */
  409. addr_value = cadence_qspi_apb_cmd2addr(&cmdbuf[1],
  410. cmdlen >= 5 ? 4 : 3);
  411. writel(addr_value, reg_base + CQSPI_REG_CMDADDRESS);
  412. }
  413. if (txlen) {
  414. /* writing data = yes */
  415. reg |= (0x1 << CQSPI_REG_CMDCTRL_WR_EN_LSB);
  416. reg |= ((txlen - 1) & CQSPI_REG_CMDCTRL_WR_BYTES_MASK)
  417. << CQSPI_REG_CMDCTRL_WR_BYTES_LSB;
  418. wr_len = txlen > 4 ? 4 : txlen;
  419. memcpy(&wr_data, txbuf, wr_len);
  420. writel(wr_data, reg_base +
  421. CQSPI_REG_CMDWRITEDATALOWER);
  422. if (txlen > 4) {
  423. txbuf += wr_len;
  424. wr_len = txlen - wr_len;
  425. memcpy(&wr_data, txbuf, wr_len);
  426. writel(wr_data, reg_base +
  427. CQSPI_REG_CMDWRITEDATAUPPER);
  428. }
  429. }
  430. /* Execute the command */
  431. return cadence_qspi_apb_exec_flash_cmd(reg_base, reg);
  432. }
  433. /* Opcode + Address (3/4 bytes) + dummy bytes (0-4 bytes) */
  434. int cadence_qspi_apb_indirect_read_setup(struct cadence_spi_platdata *plat,
  435. unsigned int cmdlen, unsigned int rx_width, const u8 *cmdbuf)
  436. {
  437. unsigned int reg;
  438. unsigned int rd_reg;
  439. unsigned int addr_value;
  440. unsigned int dummy_clk;
  441. unsigned int dummy_bytes;
  442. unsigned int addr_bytes;
  443. /*
  444. * Identify addr_byte. All NOR flash device drivers are using fast read
  445. * which always expecting 1 dummy byte, 1 cmd byte and 3/4 addr byte.
  446. * With that, the length is in value of 5 or 6. Only FRAM chip from
  447. * ramtron using normal read (which won't need dummy byte).
  448. * Unlikely NOR flash using normal read due to performance issue.
  449. */
  450. if (cmdlen >= 5)
  451. /* to cater fast read where cmd + addr + dummy */
  452. addr_bytes = cmdlen - 2;
  453. else
  454. /* for normal read (only ramtron as of now) */
  455. addr_bytes = cmdlen - 1;
  456. /* Setup the indirect trigger address */
  457. writel(plat->trigger_address,
  458. plat->regbase + CQSPI_REG_INDIRECTTRIGGER);
  459. /* Configure the opcode */
  460. rd_reg = cmdbuf[0] << CQSPI_REG_RD_INSTR_OPCODE_LSB;
  461. if (rx_width & SPI_RX_QUAD)
  462. /* Instruction and address at DQ0, data at DQ0-3. */
  463. rd_reg |= CQSPI_INST_TYPE_QUAD << CQSPI_REG_RD_INSTR_TYPE_DATA_LSB;
  464. /* Get address */
  465. addr_value = cadence_qspi_apb_cmd2addr(&cmdbuf[1], addr_bytes);
  466. writel(addr_value, plat->regbase + CQSPI_REG_INDIRECTRDSTARTADDR);
  467. /* The remaining lenght is dummy bytes. */
  468. dummy_bytes = cmdlen - addr_bytes - 1;
  469. if (dummy_bytes) {
  470. if (dummy_bytes > CQSPI_DUMMY_BYTES_MAX)
  471. dummy_bytes = CQSPI_DUMMY_BYTES_MAX;
  472. rd_reg |= (1 << CQSPI_REG_RD_INSTR_MODE_EN_LSB);
  473. #if defined(CONFIG_SPL_SPI_XIP) && defined(CONFIG_SPL_BUILD)
  474. writel(0x0, plat->regbase + CQSPI_REG_MODE_BIT);
  475. #else
  476. writel(0xFF, plat->regbase + CQSPI_REG_MODE_BIT);
  477. #endif
  478. /* Convert to clock cycles. */
  479. dummy_clk = dummy_bytes * CQSPI_DUMMY_CLKS_PER_BYTE;
  480. /* Need to minus the mode byte (8 clocks). */
  481. dummy_clk -= CQSPI_DUMMY_CLKS_PER_BYTE;
  482. if (dummy_clk)
  483. rd_reg |= (dummy_clk & CQSPI_REG_RD_INSTR_DUMMY_MASK)
  484. << CQSPI_REG_RD_INSTR_DUMMY_LSB;
  485. }
  486. writel(rd_reg, plat->regbase + CQSPI_REG_RD_INSTR);
  487. /* set device size */
  488. reg = readl(plat->regbase + CQSPI_REG_SIZE);
  489. reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
  490. reg |= (addr_bytes - 1);
  491. writel(reg, plat->regbase + CQSPI_REG_SIZE);
  492. return 0;
  493. }
  494. static u32 cadence_qspi_get_rd_sram_level(struct cadence_spi_platdata *plat)
  495. {
  496. u32 reg = readl(plat->regbase + CQSPI_REG_SDRAMLEVEL);
  497. reg >>= CQSPI_REG_SDRAMLEVEL_RD_LSB;
  498. return reg & CQSPI_REG_SDRAMLEVEL_RD_MASK;
  499. }
  500. static int cadence_qspi_wait_for_data(struct cadence_spi_platdata *plat)
  501. {
  502. unsigned int timeout = 10000;
  503. u32 reg;
  504. while (timeout--) {
  505. reg = cadence_qspi_get_rd_sram_level(plat);
  506. if (reg)
  507. return reg;
  508. udelay(1);
  509. }
  510. return -ETIMEDOUT;
  511. }
  512. int cadence_qspi_apb_indirect_read_execute(struct cadence_spi_platdata *plat,
  513. unsigned int n_rx, u8 *rxbuf)
  514. {
  515. unsigned int remaining = n_rx;
  516. unsigned int bytes_to_read = 0;
  517. int ret;
  518. writel(n_rx, plat->regbase + CQSPI_REG_INDIRECTRDBYTES);
  519. /* Start the indirect read transfer */
  520. writel(CQSPI_REG_INDIRECTRD_START,
  521. plat->regbase + CQSPI_REG_INDIRECTRD);
  522. while (remaining > 0) {
  523. ret = cadence_qspi_wait_for_data(plat);
  524. if (ret < 0) {
  525. printf("Indirect write timed out (%i)\n", ret);
  526. goto failrd;
  527. }
  528. bytes_to_read = ret;
  529. while (bytes_to_read != 0) {
  530. bytes_to_read *= plat->fifo_width;
  531. bytes_to_read = bytes_to_read > remaining ?
  532. remaining : bytes_to_read;
  533. /*
  534. * Handle non-4-byte aligned access to avoid
  535. * data abort.
  536. */
  537. if (((uintptr_t)rxbuf % 4) || (bytes_to_read % 4))
  538. readsb(plat->ahbbase, rxbuf, bytes_to_read);
  539. else
  540. readsl(plat->ahbbase, rxbuf,
  541. bytes_to_read >> 2);
  542. rxbuf += bytes_to_read;
  543. remaining -= bytes_to_read;
  544. bytes_to_read = cadence_qspi_get_rd_sram_level(plat);
  545. }
  546. }
  547. /* Check indirect done status */
  548. ret = wait_for_bit_le32(plat->regbase + CQSPI_REG_INDIRECTRD,
  549. CQSPI_REG_INDIRECTRD_DONE, 1, 10, 0);
  550. if (ret) {
  551. printf("Indirect read completion error (%i)\n", ret);
  552. goto failrd;
  553. }
  554. /* Clear indirect completion status */
  555. writel(CQSPI_REG_INDIRECTRD_DONE,
  556. plat->regbase + CQSPI_REG_INDIRECTRD);
  557. return 0;
  558. failrd:
  559. /* Cancel the indirect read */
  560. writel(CQSPI_REG_INDIRECTRD_CANCEL,
  561. plat->regbase + CQSPI_REG_INDIRECTRD);
  562. return ret;
  563. }
  564. /* Opcode + Address (3/4 bytes) */
  565. int cadence_qspi_apb_indirect_write_setup(struct cadence_spi_platdata *plat,
  566. unsigned int cmdlen, const u8 *cmdbuf)
  567. {
  568. unsigned int reg;
  569. unsigned int addr_bytes = cmdlen > 4 ? 4 : 3;
  570. if (cmdlen < 4 || cmdbuf == NULL) {
  571. printf("QSPI: Invalid input argument, len %d cmdbuf %p\n",
  572. cmdlen, cmdbuf);
  573. return -EINVAL;
  574. }
  575. /* Setup the indirect trigger address */
  576. writel(plat->trigger_address,
  577. plat->regbase + CQSPI_REG_INDIRECTTRIGGER);
  578. /* Configure the opcode */
  579. reg = cmdbuf[0] << CQSPI_REG_WR_INSTR_OPCODE_LSB;
  580. writel(reg, plat->regbase + CQSPI_REG_WR_INSTR);
  581. /* Setup write address. */
  582. reg = cadence_qspi_apb_cmd2addr(&cmdbuf[1], addr_bytes);
  583. writel(reg, plat->regbase + CQSPI_REG_INDIRECTWRSTARTADDR);
  584. reg = readl(plat->regbase + CQSPI_REG_SIZE);
  585. reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
  586. reg |= (addr_bytes - 1);
  587. writel(reg, plat->regbase + CQSPI_REG_SIZE);
  588. return 0;
  589. }
  590. int cadence_qspi_apb_indirect_write_execute(struct cadence_spi_platdata *plat,
  591. unsigned int n_tx, const u8 *txbuf)
  592. {
  593. unsigned int page_size = plat->page_size;
  594. unsigned int remaining = n_tx;
  595. const u8 *bb_txbuf = txbuf;
  596. void *bounce_buf = NULL;
  597. unsigned int write_bytes;
  598. int ret;
  599. /*
  600. * Use bounce buffer for non 32 bit aligned txbuf to avoid data
  601. * aborts
  602. */
  603. if ((uintptr_t)txbuf % 4) {
  604. bounce_buf = malloc(n_tx);
  605. if (!bounce_buf)
  606. return -ENOMEM;
  607. memcpy(bounce_buf, txbuf, n_tx);
  608. bb_txbuf = bounce_buf;
  609. }
  610. /* Configure the indirect read transfer bytes */
  611. writel(n_tx, plat->regbase + CQSPI_REG_INDIRECTWRBYTES);
  612. /* Start the indirect write transfer */
  613. writel(CQSPI_REG_INDIRECTWR_START,
  614. plat->regbase + CQSPI_REG_INDIRECTWR);
  615. while (remaining > 0) {
  616. write_bytes = remaining > page_size ? page_size : remaining;
  617. writesl(plat->ahbbase, bb_txbuf, write_bytes >> 2);
  618. if (write_bytes % 4)
  619. writesb(plat->ahbbase,
  620. bb_txbuf + rounddown(write_bytes, 4),
  621. write_bytes % 4);
  622. ret = wait_for_bit_le32(plat->regbase + CQSPI_REG_SDRAMLEVEL,
  623. CQSPI_REG_SDRAMLEVEL_WR_MASK <<
  624. CQSPI_REG_SDRAMLEVEL_WR_LSB, 0, 10, 0);
  625. if (ret) {
  626. printf("Indirect write timed out (%i)\n", ret);
  627. goto failwr;
  628. }
  629. bb_txbuf += write_bytes;
  630. remaining -= write_bytes;
  631. }
  632. /* Check indirect done status */
  633. ret = wait_for_bit_le32(plat->regbase + CQSPI_REG_INDIRECTWR,
  634. CQSPI_REG_INDIRECTWR_DONE, 1, 10, 0);
  635. if (ret) {
  636. printf("Indirect write completion error (%i)\n", ret);
  637. goto failwr;
  638. }
  639. /* Clear indirect completion status */
  640. writel(CQSPI_REG_INDIRECTWR_DONE,
  641. plat->regbase + CQSPI_REG_INDIRECTWR);
  642. if (bounce_buf)
  643. free(bounce_buf);
  644. return 0;
  645. failwr:
  646. /* Cancel the indirect write */
  647. writel(CQSPI_REG_INDIRECTWR_CANCEL,
  648. plat->regbase + CQSPI_REG_INDIRECTWR);
  649. if (bounce_buf)
  650. free(bounce_buf);
  651. return ret;
  652. }
  653. void cadence_qspi_apb_enter_xip(void *reg_base, char xip_dummy)
  654. {
  655. unsigned int reg;
  656. /* enter XiP mode immediately and enable direct mode */
  657. reg = readl(reg_base + CQSPI_REG_CONFIG);
  658. reg |= CQSPI_REG_CONFIG_ENABLE;
  659. reg |= CQSPI_REG_CONFIG_DIRECT;
  660. reg |= CQSPI_REG_CONFIG_XIP_IMM;
  661. writel(reg, reg_base + CQSPI_REG_CONFIG);
  662. /* keep the XiP mode */
  663. writel(xip_dummy, reg_base + CQSPI_REG_MODE_BIT);
  664. /* Enable mode bit at devrd */
  665. reg = readl(reg_base + CQSPI_REG_RD_INSTR);
  666. reg |= (1 << CQSPI_REG_RD_INSTR_MODE_EN_LSB);
  667. writel(reg, reg_base + CQSPI_REG_RD_INSTR);
  668. }