cadence-quadspi.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  1. /*
  2. * Driver for Cadence QSPI Controller
  3. *
  4. * Copyright Altera Corporation (C) 2012-2014. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/completion.h>
  20. #include <linux/delay.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/dmaengine.h>
  23. #include <linux/err.h>
  24. #include <linux/errno.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/mtd/mtd.h>
  31. #include <linux/mtd/partitions.h>
  32. #include <linux/mtd/spi-nor.h>
  33. #include <linux/of_device.h>
  34. #include <linux/of.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/pm_runtime.h>
  37. #include <linux/sched.h>
  38. #include <linux/spi/spi.h>
  39. #include <linux/timer.h>
  40. #define CQSPI_NAME "cadence-qspi"
  41. #define CQSPI_MAX_CHIPSELECT 16
  42. /* Quirks */
  43. #define CQSPI_NEEDS_WR_DELAY BIT(0)
  44. struct cqspi_st;
  45. struct cqspi_flash_pdata {
  46. struct spi_nor nor;
  47. struct cqspi_st *cqspi;
  48. u32 clk_rate;
  49. u32 read_delay;
  50. u32 tshsl_ns;
  51. u32 tsd2d_ns;
  52. u32 tchsh_ns;
  53. u32 tslch_ns;
  54. u8 inst_width;
  55. u8 addr_width;
  56. u8 data_width;
  57. u8 cs;
  58. bool registered;
  59. bool use_direct_mode;
  60. };
  61. struct cqspi_st {
  62. struct platform_device *pdev;
  63. struct clk *clk;
  64. unsigned int sclk;
  65. void __iomem *iobase;
  66. void __iomem *ahb_base;
  67. resource_size_t ahb_size;
  68. struct completion transfer_complete;
  69. struct mutex bus_mutex;
  70. struct dma_chan *rx_chan;
  71. struct completion rx_dma_complete;
  72. dma_addr_t mmap_phys_base;
  73. int current_cs;
  74. int current_page_size;
  75. int current_erase_size;
  76. int current_addr_width;
  77. unsigned long master_ref_clk_hz;
  78. bool is_decoded_cs;
  79. u32 fifo_depth;
  80. u32 fifo_width;
  81. bool rclk_en;
  82. u32 trigger_address;
  83. u32 wr_delay;
  84. struct cqspi_flash_pdata f_pdata[CQSPI_MAX_CHIPSELECT];
  85. };
  86. /* Operation timeout value */
  87. #define CQSPI_TIMEOUT_MS 500
  88. #define CQSPI_READ_TIMEOUT_MS 10
  89. /* Instruction type */
  90. #define CQSPI_INST_TYPE_SINGLE 0
  91. #define CQSPI_INST_TYPE_DUAL 1
  92. #define CQSPI_INST_TYPE_QUAD 2
  93. #define CQSPI_DUMMY_CLKS_PER_BYTE 8
  94. #define CQSPI_DUMMY_BYTES_MAX 4
  95. #define CQSPI_DUMMY_CLKS_MAX 31
  96. #define CQSPI_STIG_DATA_LEN_MAX 8
  97. /* Register map */
  98. #define CQSPI_REG_CONFIG 0x00
  99. #define CQSPI_REG_CONFIG_ENABLE_MASK BIT(0)
  100. #define CQSPI_REG_CONFIG_ENB_DIR_ACC_CTRL BIT(7)
  101. #define CQSPI_REG_CONFIG_DECODE_MASK BIT(9)
  102. #define CQSPI_REG_CONFIG_CHIPSELECT_LSB 10
  103. #define CQSPI_REG_CONFIG_DMA_MASK BIT(15)
  104. #define CQSPI_REG_CONFIG_BAUD_LSB 19
  105. #define CQSPI_REG_CONFIG_IDLE_LSB 31
  106. #define CQSPI_REG_CONFIG_CHIPSELECT_MASK 0xF
  107. #define CQSPI_REG_CONFIG_BAUD_MASK 0xF
  108. #define CQSPI_REG_RD_INSTR 0x04
  109. #define CQSPI_REG_RD_INSTR_OPCODE_LSB 0
  110. #define CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB 8
  111. #define CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB 12
  112. #define CQSPI_REG_RD_INSTR_TYPE_DATA_LSB 16
  113. #define CQSPI_REG_RD_INSTR_MODE_EN_LSB 20
  114. #define CQSPI_REG_RD_INSTR_DUMMY_LSB 24
  115. #define CQSPI_REG_RD_INSTR_TYPE_INSTR_MASK 0x3
  116. #define CQSPI_REG_RD_INSTR_TYPE_ADDR_MASK 0x3
  117. #define CQSPI_REG_RD_INSTR_TYPE_DATA_MASK 0x3
  118. #define CQSPI_REG_RD_INSTR_DUMMY_MASK 0x1F
  119. #define CQSPI_REG_WR_INSTR 0x08
  120. #define CQSPI_REG_WR_INSTR_OPCODE_LSB 0
  121. #define CQSPI_REG_WR_INSTR_TYPE_ADDR_LSB 12
  122. #define CQSPI_REG_WR_INSTR_TYPE_DATA_LSB 16
  123. #define CQSPI_REG_DELAY 0x0C
  124. #define CQSPI_REG_DELAY_TSLCH_LSB 0
  125. #define CQSPI_REG_DELAY_TCHSH_LSB 8
  126. #define CQSPI_REG_DELAY_TSD2D_LSB 16
  127. #define CQSPI_REG_DELAY_TSHSL_LSB 24
  128. #define CQSPI_REG_DELAY_TSLCH_MASK 0xFF
  129. #define CQSPI_REG_DELAY_TCHSH_MASK 0xFF
  130. #define CQSPI_REG_DELAY_TSD2D_MASK 0xFF
  131. #define CQSPI_REG_DELAY_TSHSL_MASK 0xFF
  132. #define CQSPI_REG_READCAPTURE 0x10
  133. #define CQSPI_REG_READCAPTURE_BYPASS_LSB 0
  134. #define CQSPI_REG_READCAPTURE_DELAY_LSB 1
  135. #define CQSPI_REG_READCAPTURE_DELAY_MASK 0xF
  136. #define CQSPI_REG_SIZE 0x14
  137. #define CQSPI_REG_SIZE_ADDRESS_LSB 0
  138. #define CQSPI_REG_SIZE_PAGE_LSB 4
  139. #define CQSPI_REG_SIZE_BLOCK_LSB 16
  140. #define CQSPI_REG_SIZE_ADDRESS_MASK 0xF
  141. #define CQSPI_REG_SIZE_PAGE_MASK 0xFFF
  142. #define CQSPI_REG_SIZE_BLOCK_MASK 0x3F
  143. #define CQSPI_REG_SRAMPARTITION 0x18
  144. #define CQSPI_REG_INDIRECTTRIGGER 0x1C
  145. #define CQSPI_REG_DMA 0x20
  146. #define CQSPI_REG_DMA_SINGLE_LSB 0
  147. #define CQSPI_REG_DMA_BURST_LSB 8
  148. #define CQSPI_REG_DMA_SINGLE_MASK 0xFF
  149. #define CQSPI_REG_DMA_BURST_MASK 0xFF
  150. #define CQSPI_REG_REMAP 0x24
  151. #define CQSPI_REG_MODE_BIT 0x28
  152. #define CQSPI_REG_SDRAMLEVEL 0x2C
  153. #define CQSPI_REG_SDRAMLEVEL_RD_LSB 0
  154. #define CQSPI_REG_SDRAMLEVEL_WR_LSB 16
  155. #define CQSPI_REG_SDRAMLEVEL_RD_MASK 0xFFFF
  156. #define CQSPI_REG_SDRAMLEVEL_WR_MASK 0xFFFF
  157. #define CQSPI_REG_IRQSTATUS 0x40
  158. #define CQSPI_REG_IRQMASK 0x44
  159. #define CQSPI_REG_INDIRECTRD 0x60
  160. #define CQSPI_REG_INDIRECTRD_START_MASK BIT(0)
  161. #define CQSPI_REG_INDIRECTRD_CANCEL_MASK BIT(1)
  162. #define CQSPI_REG_INDIRECTRD_DONE_MASK BIT(5)
  163. #define CQSPI_REG_INDIRECTRDWATERMARK 0x64
  164. #define CQSPI_REG_INDIRECTRDSTARTADDR 0x68
  165. #define CQSPI_REG_INDIRECTRDBYTES 0x6C
  166. #define CQSPI_REG_CMDCTRL 0x90
  167. #define CQSPI_REG_CMDCTRL_EXECUTE_MASK BIT(0)
  168. #define CQSPI_REG_CMDCTRL_INPROGRESS_MASK BIT(1)
  169. #define CQSPI_REG_CMDCTRL_WR_BYTES_LSB 12
  170. #define CQSPI_REG_CMDCTRL_WR_EN_LSB 15
  171. #define CQSPI_REG_CMDCTRL_ADD_BYTES_LSB 16
  172. #define CQSPI_REG_CMDCTRL_ADDR_EN_LSB 19
  173. #define CQSPI_REG_CMDCTRL_RD_BYTES_LSB 20
  174. #define CQSPI_REG_CMDCTRL_RD_EN_LSB 23
  175. #define CQSPI_REG_CMDCTRL_OPCODE_LSB 24
  176. #define CQSPI_REG_CMDCTRL_WR_BYTES_MASK 0x7
  177. #define CQSPI_REG_CMDCTRL_ADD_BYTES_MASK 0x3
  178. #define CQSPI_REG_CMDCTRL_RD_BYTES_MASK 0x7
  179. #define CQSPI_REG_INDIRECTWR 0x70
  180. #define CQSPI_REG_INDIRECTWR_START_MASK BIT(0)
  181. #define CQSPI_REG_INDIRECTWR_CANCEL_MASK BIT(1)
  182. #define CQSPI_REG_INDIRECTWR_DONE_MASK BIT(5)
  183. #define CQSPI_REG_INDIRECTWRWATERMARK 0x74
  184. #define CQSPI_REG_INDIRECTWRSTARTADDR 0x78
  185. #define CQSPI_REG_INDIRECTWRBYTES 0x7C
  186. #define CQSPI_REG_CMDADDRESS 0x94
  187. #define CQSPI_REG_CMDREADDATALOWER 0xA0
  188. #define CQSPI_REG_CMDREADDATAUPPER 0xA4
  189. #define CQSPI_REG_CMDWRITEDATALOWER 0xA8
  190. #define CQSPI_REG_CMDWRITEDATAUPPER 0xAC
  191. /* Interrupt status bits */
  192. #define CQSPI_REG_IRQ_MODE_ERR BIT(0)
  193. #define CQSPI_REG_IRQ_UNDERFLOW BIT(1)
  194. #define CQSPI_REG_IRQ_IND_COMP BIT(2)
  195. #define CQSPI_REG_IRQ_IND_RD_REJECT BIT(3)
  196. #define CQSPI_REG_IRQ_WR_PROTECTED_ERR BIT(4)
  197. #define CQSPI_REG_IRQ_ILLEGAL_AHB_ERR BIT(5)
  198. #define CQSPI_REG_IRQ_WATERMARK BIT(6)
  199. #define CQSPI_REG_IRQ_IND_SRAM_FULL BIT(12)
  200. #define CQSPI_IRQ_MASK_RD (CQSPI_REG_IRQ_WATERMARK | \
  201. CQSPI_REG_IRQ_IND_SRAM_FULL | \
  202. CQSPI_REG_IRQ_IND_COMP)
  203. #define CQSPI_IRQ_MASK_WR (CQSPI_REG_IRQ_IND_COMP | \
  204. CQSPI_REG_IRQ_WATERMARK | \
  205. CQSPI_REG_IRQ_UNDERFLOW)
  206. #define CQSPI_IRQ_STATUS_MASK 0x1FFFF
  207. static int cqspi_wait_for_bit(void __iomem *reg, const u32 mask, bool clear)
  208. {
  209. unsigned long end = jiffies + msecs_to_jiffies(CQSPI_TIMEOUT_MS);
  210. u32 val;
  211. while (1) {
  212. val = readl(reg);
  213. if (clear)
  214. val = ~val;
  215. val &= mask;
  216. if (val == mask)
  217. return 0;
  218. if (time_after(jiffies, end))
  219. return -ETIMEDOUT;
  220. }
  221. }
  222. static bool cqspi_is_idle(struct cqspi_st *cqspi)
  223. {
  224. u32 reg = readl(cqspi->iobase + CQSPI_REG_CONFIG);
  225. return reg & (1 << CQSPI_REG_CONFIG_IDLE_LSB);
  226. }
  227. static u32 cqspi_get_rd_sram_level(struct cqspi_st *cqspi)
  228. {
  229. u32 reg = readl(cqspi->iobase + CQSPI_REG_SDRAMLEVEL);
  230. reg >>= CQSPI_REG_SDRAMLEVEL_RD_LSB;
  231. return reg & CQSPI_REG_SDRAMLEVEL_RD_MASK;
  232. }
  233. static irqreturn_t cqspi_irq_handler(int this_irq, void *dev)
  234. {
  235. struct cqspi_st *cqspi = dev;
  236. unsigned int irq_status;
  237. /* Read interrupt status */
  238. irq_status = readl(cqspi->iobase + CQSPI_REG_IRQSTATUS);
  239. /* Clear interrupt */
  240. writel(irq_status, cqspi->iobase + CQSPI_REG_IRQSTATUS);
  241. irq_status &= CQSPI_IRQ_MASK_RD | CQSPI_IRQ_MASK_WR;
  242. if (irq_status)
  243. complete(&cqspi->transfer_complete);
  244. return IRQ_HANDLED;
  245. }
  246. static unsigned int cqspi_calc_rdreg(struct spi_nor *nor, const u8 opcode)
  247. {
  248. struct cqspi_flash_pdata *f_pdata = nor->priv;
  249. u32 rdreg = 0;
  250. rdreg |= f_pdata->inst_width << CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB;
  251. rdreg |= f_pdata->addr_width << CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB;
  252. rdreg |= f_pdata->data_width << CQSPI_REG_RD_INSTR_TYPE_DATA_LSB;
  253. return rdreg;
  254. }
  255. static int cqspi_wait_idle(struct cqspi_st *cqspi)
  256. {
  257. const unsigned int poll_idle_retry = 3;
  258. unsigned int count = 0;
  259. unsigned long timeout;
  260. timeout = jiffies + msecs_to_jiffies(CQSPI_TIMEOUT_MS);
  261. while (1) {
  262. /*
  263. * Read few times in succession to ensure the controller
  264. * is indeed idle, that is, the bit does not transition
  265. * low again.
  266. */
  267. if (cqspi_is_idle(cqspi))
  268. count++;
  269. else
  270. count = 0;
  271. if (count >= poll_idle_retry)
  272. return 0;
  273. if (time_after(jiffies, timeout)) {
  274. /* Timeout, in busy mode. */
  275. dev_err(&cqspi->pdev->dev,
  276. "QSPI is still busy after %dms timeout.\n",
  277. CQSPI_TIMEOUT_MS);
  278. return -ETIMEDOUT;
  279. }
  280. cpu_relax();
  281. }
  282. }
  283. static int cqspi_exec_flash_cmd(struct cqspi_st *cqspi, unsigned int reg)
  284. {
  285. void __iomem *reg_base = cqspi->iobase;
  286. int ret;
  287. /* Write the CMDCTRL without start execution. */
  288. writel(reg, reg_base + CQSPI_REG_CMDCTRL);
  289. /* Start execute */
  290. reg |= CQSPI_REG_CMDCTRL_EXECUTE_MASK;
  291. writel(reg, reg_base + CQSPI_REG_CMDCTRL);
  292. /* Polling for completion. */
  293. ret = cqspi_wait_for_bit(reg_base + CQSPI_REG_CMDCTRL,
  294. CQSPI_REG_CMDCTRL_INPROGRESS_MASK, 1);
  295. if (ret) {
  296. dev_err(&cqspi->pdev->dev,
  297. "Flash command execution timed out.\n");
  298. return ret;
  299. }
  300. /* Polling QSPI idle status. */
  301. return cqspi_wait_idle(cqspi);
  302. }
  303. static int cqspi_command_read(struct spi_nor *nor,
  304. const u8 *txbuf, const unsigned n_tx,
  305. u8 *rxbuf, const unsigned n_rx)
  306. {
  307. struct cqspi_flash_pdata *f_pdata = nor->priv;
  308. struct cqspi_st *cqspi = f_pdata->cqspi;
  309. void __iomem *reg_base = cqspi->iobase;
  310. unsigned int rdreg;
  311. unsigned int reg;
  312. unsigned int read_len;
  313. int status;
  314. if (!n_rx || n_rx > CQSPI_STIG_DATA_LEN_MAX || !rxbuf) {
  315. dev_err(nor->dev, "Invalid input argument, len %d rxbuf 0x%p\n",
  316. n_rx, rxbuf);
  317. return -EINVAL;
  318. }
  319. reg = txbuf[0] << CQSPI_REG_CMDCTRL_OPCODE_LSB;
  320. rdreg = cqspi_calc_rdreg(nor, txbuf[0]);
  321. writel(rdreg, reg_base + CQSPI_REG_RD_INSTR);
  322. reg |= (0x1 << CQSPI_REG_CMDCTRL_RD_EN_LSB);
  323. /* 0 means 1 byte. */
  324. reg |= (((n_rx - 1) & CQSPI_REG_CMDCTRL_RD_BYTES_MASK)
  325. << CQSPI_REG_CMDCTRL_RD_BYTES_LSB);
  326. status = cqspi_exec_flash_cmd(cqspi, reg);
  327. if (status)
  328. return status;
  329. reg = readl(reg_base + CQSPI_REG_CMDREADDATALOWER);
  330. /* Put the read value into rx_buf */
  331. read_len = (n_rx > 4) ? 4 : n_rx;
  332. memcpy(rxbuf, &reg, read_len);
  333. rxbuf += read_len;
  334. if (n_rx > 4) {
  335. reg = readl(reg_base + CQSPI_REG_CMDREADDATAUPPER);
  336. read_len = n_rx - read_len;
  337. memcpy(rxbuf, &reg, read_len);
  338. }
  339. return 0;
  340. }
  341. static int cqspi_command_write(struct spi_nor *nor, const u8 opcode,
  342. const u8 *txbuf, const unsigned n_tx)
  343. {
  344. struct cqspi_flash_pdata *f_pdata = nor->priv;
  345. struct cqspi_st *cqspi = f_pdata->cqspi;
  346. void __iomem *reg_base = cqspi->iobase;
  347. unsigned int reg;
  348. unsigned int data;
  349. int ret;
  350. if (n_tx > 4 || (n_tx && !txbuf)) {
  351. dev_err(nor->dev,
  352. "Invalid input argument, cmdlen %d txbuf 0x%p\n",
  353. n_tx, txbuf);
  354. return -EINVAL;
  355. }
  356. reg = opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB;
  357. if (n_tx) {
  358. reg |= (0x1 << CQSPI_REG_CMDCTRL_WR_EN_LSB);
  359. reg |= ((n_tx - 1) & CQSPI_REG_CMDCTRL_WR_BYTES_MASK)
  360. << CQSPI_REG_CMDCTRL_WR_BYTES_LSB;
  361. data = 0;
  362. memcpy(&data, txbuf, n_tx);
  363. writel(data, reg_base + CQSPI_REG_CMDWRITEDATALOWER);
  364. }
  365. ret = cqspi_exec_flash_cmd(cqspi, reg);
  366. return ret;
  367. }
  368. static int cqspi_command_write_addr(struct spi_nor *nor,
  369. const u8 opcode, const unsigned int addr)
  370. {
  371. struct cqspi_flash_pdata *f_pdata = nor->priv;
  372. struct cqspi_st *cqspi = f_pdata->cqspi;
  373. void __iomem *reg_base = cqspi->iobase;
  374. unsigned int reg;
  375. reg = opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB;
  376. reg |= (0x1 << CQSPI_REG_CMDCTRL_ADDR_EN_LSB);
  377. reg |= ((nor->addr_width - 1) & CQSPI_REG_CMDCTRL_ADD_BYTES_MASK)
  378. << CQSPI_REG_CMDCTRL_ADD_BYTES_LSB;
  379. writel(addr, reg_base + CQSPI_REG_CMDADDRESS);
  380. return cqspi_exec_flash_cmd(cqspi, reg);
  381. }
  382. static int cqspi_read_setup(struct spi_nor *nor)
  383. {
  384. struct cqspi_flash_pdata *f_pdata = nor->priv;
  385. struct cqspi_st *cqspi = f_pdata->cqspi;
  386. void __iomem *reg_base = cqspi->iobase;
  387. unsigned int dummy_clk = 0;
  388. unsigned int reg;
  389. reg = nor->read_opcode << CQSPI_REG_RD_INSTR_OPCODE_LSB;
  390. reg |= cqspi_calc_rdreg(nor, nor->read_opcode);
  391. /* Setup dummy clock cycles */
  392. dummy_clk = nor->read_dummy;
  393. if (dummy_clk > CQSPI_DUMMY_CLKS_MAX)
  394. dummy_clk = CQSPI_DUMMY_CLKS_MAX;
  395. if (dummy_clk / 8) {
  396. reg |= (1 << CQSPI_REG_RD_INSTR_MODE_EN_LSB);
  397. /* Set mode bits high to ensure chip doesn't enter XIP */
  398. writel(0xFF, reg_base + CQSPI_REG_MODE_BIT);
  399. /* Need to subtract the mode byte (8 clocks). */
  400. if (f_pdata->inst_width != CQSPI_INST_TYPE_QUAD)
  401. dummy_clk -= 8;
  402. if (dummy_clk)
  403. reg |= (dummy_clk & CQSPI_REG_RD_INSTR_DUMMY_MASK)
  404. << CQSPI_REG_RD_INSTR_DUMMY_LSB;
  405. }
  406. writel(reg, reg_base + CQSPI_REG_RD_INSTR);
  407. /* Set address width */
  408. reg = readl(reg_base + CQSPI_REG_SIZE);
  409. reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
  410. reg |= (nor->addr_width - 1);
  411. writel(reg, reg_base + CQSPI_REG_SIZE);
  412. return 0;
  413. }
  414. static int cqspi_indirect_read_execute(struct spi_nor *nor, u8 *rxbuf,
  415. loff_t from_addr, const size_t n_rx)
  416. {
  417. struct cqspi_flash_pdata *f_pdata = nor->priv;
  418. struct cqspi_st *cqspi = f_pdata->cqspi;
  419. void __iomem *reg_base = cqspi->iobase;
  420. void __iomem *ahb_base = cqspi->ahb_base;
  421. unsigned int remaining = n_rx;
  422. unsigned int mod_bytes = n_rx % 4;
  423. unsigned int bytes_to_read = 0;
  424. u8 *rxbuf_end = rxbuf + n_rx;
  425. int ret = 0;
  426. writel(from_addr, reg_base + CQSPI_REG_INDIRECTRDSTARTADDR);
  427. writel(remaining, reg_base + CQSPI_REG_INDIRECTRDBYTES);
  428. /* Clear all interrupts. */
  429. writel(CQSPI_IRQ_STATUS_MASK, reg_base + CQSPI_REG_IRQSTATUS);
  430. writel(CQSPI_IRQ_MASK_RD, reg_base + CQSPI_REG_IRQMASK);
  431. reinit_completion(&cqspi->transfer_complete);
  432. writel(CQSPI_REG_INDIRECTRD_START_MASK,
  433. reg_base + CQSPI_REG_INDIRECTRD);
  434. while (remaining > 0) {
  435. if (!wait_for_completion_timeout(&cqspi->transfer_complete,
  436. msecs_to_jiffies(CQSPI_READ_TIMEOUT_MS)))
  437. ret = -ETIMEDOUT;
  438. bytes_to_read = cqspi_get_rd_sram_level(cqspi);
  439. if (ret && bytes_to_read == 0) {
  440. dev_err(nor->dev, "Indirect read timeout, no bytes\n");
  441. goto failrd;
  442. }
  443. while (bytes_to_read != 0) {
  444. unsigned int word_remain = round_down(remaining, 4);
  445. bytes_to_read *= cqspi->fifo_width;
  446. bytes_to_read = bytes_to_read > remaining ?
  447. remaining : bytes_to_read;
  448. bytes_to_read = round_down(bytes_to_read, 4);
  449. /* Read 4 byte word chunks then single bytes */
  450. if (bytes_to_read) {
  451. ioread32_rep(ahb_base, rxbuf,
  452. (bytes_to_read / 4));
  453. } else if (!word_remain && mod_bytes) {
  454. unsigned int temp = ioread32(ahb_base);
  455. bytes_to_read = mod_bytes;
  456. memcpy(rxbuf, &temp, min((unsigned int)
  457. (rxbuf_end - rxbuf),
  458. bytes_to_read));
  459. }
  460. rxbuf += bytes_to_read;
  461. remaining -= bytes_to_read;
  462. bytes_to_read = cqspi_get_rd_sram_level(cqspi);
  463. }
  464. if (remaining > 0)
  465. reinit_completion(&cqspi->transfer_complete);
  466. }
  467. /* Check indirect done status */
  468. ret = cqspi_wait_for_bit(reg_base + CQSPI_REG_INDIRECTRD,
  469. CQSPI_REG_INDIRECTRD_DONE_MASK, 0);
  470. if (ret) {
  471. dev_err(nor->dev,
  472. "Indirect read completion error (%i)\n", ret);
  473. goto failrd;
  474. }
  475. /* Disable interrupt */
  476. writel(0, reg_base + CQSPI_REG_IRQMASK);
  477. /* Clear indirect completion status */
  478. writel(CQSPI_REG_INDIRECTRD_DONE_MASK, reg_base + CQSPI_REG_INDIRECTRD);
  479. return 0;
  480. failrd:
  481. /* Disable interrupt */
  482. writel(0, reg_base + CQSPI_REG_IRQMASK);
  483. /* Cancel the indirect read */
  484. writel(CQSPI_REG_INDIRECTWR_CANCEL_MASK,
  485. reg_base + CQSPI_REG_INDIRECTRD);
  486. return ret;
  487. }
  488. static int cqspi_write_setup(struct spi_nor *nor)
  489. {
  490. unsigned int reg;
  491. struct cqspi_flash_pdata *f_pdata = nor->priv;
  492. struct cqspi_st *cqspi = f_pdata->cqspi;
  493. void __iomem *reg_base = cqspi->iobase;
  494. /* Set opcode. */
  495. reg = nor->program_opcode << CQSPI_REG_WR_INSTR_OPCODE_LSB;
  496. writel(reg, reg_base + CQSPI_REG_WR_INSTR);
  497. reg = cqspi_calc_rdreg(nor, nor->program_opcode);
  498. writel(reg, reg_base + CQSPI_REG_RD_INSTR);
  499. reg = readl(reg_base + CQSPI_REG_SIZE);
  500. reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
  501. reg |= (nor->addr_width - 1);
  502. writel(reg, reg_base + CQSPI_REG_SIZE);
  503. return 0;
  504. }
  505. static int cqspi_indirect_write_execute(struct spi_nor *nor, loff_t to_addr,
  506. const u8 *txbuf, const size_t n_tx)
  507. {
  508. const unsigned int page_size = nor->page_size;
  509. struct cqspi_flash_pdata *f_pdata = nor->priv;
  510. struct cqspi_st *cqspi = f_pdata->cqspi;
  511. void __iomem *reg_base = cqspi->iobase;
  512. unsigned int remaining = n_tx;
  513. unsigned int write_bytes;
  514. int ret;
  515. writel(to_addr, reg_base + CQSPI_REG_INDIRECTWRSTARTADDR);
  516. writel(remaining, reg_base + CQSPI_REG_INDIRECTWRBYTES);
  517. /* Clear all interrupts. */
  518. writel(CQSPI_IRQ_STATUS_MASK, reg_base + CQSPI_REG_IRQSTATUS);
  519. writel(CQSPI_IRQ_MASK_WR, reg_base + CQSPI_REG_IRQMASK);
  520. reinit_completion(&cqspi->transfer_complete);
  521. writel(CQSPI_REG_INDIRECTWR_START_MASK,
  522. reg_base + CQSPI_REG_INDIRECTWR);
  523. /*
  524. * As per 66AK2G02 TRM SPRUHY8F section 11.15.5.3 Indirect Access
  525. * Controller programming sequence, couple of cycles of
  526. * QSPI_REF_CLK delay is required for the above bit to
  527. * be internally synchronized by the QSPI module. Provide 5
  528. * cycles of delay.
  529. */
  530. if (cqspi->wr_delay)
  531. ndelay(cqspi->wr_delay);
  532. while (remaining > 0) {
  533. size_t write_words, mod_bytes;
  534. write_bytes = remaining > page_size ? page_size : remaining;
  535. write_words = write_bytes / 4;
  536. mod_bytes = write_bytes % 4;
  537. /* Write 4 bytes at a time then single bytes. */
  538. if (write_words) {
  539. iowrite32_rep(cqspi->ahb_base, txbuf, write_words);
  540. txbuf += (write_words * 4);
  541. }
  542. if (mod_bytes) {
  543. unsigned int temp = 0xFFFFFFFF;
  544. memcpy(&temp, txbuf, mod_bytes);
  545. iowrite32(temp, cqspi->ahb_base);
  546. txbuf += mod_bytes;
  547. }
  548. if (!wait_for_completion_timeout(&cqspi->transfer_complete,
  549. msecs_to_jiffies(CQSPI_TIMEOUT_MS))) {
  550. dev_err(nor->dev, "Indirect write timeout\n");
  551. ret = -ETIMEDOUT;
  552. goto failwr;
  553. }
  554. remaining -= write_bytes;
  555. if (remaining > 0)
  556. reinit_completion(&cqspi->transfer_complete);
  557. }
  558. /* Check indirect done status */
  559. ret = cqspi_wait_for_bit(reg_base + CQSPI_REG_INDIRECTWR,
  560. CQSPI_REG_INDIRECTWR_DONE_MASK, 0);
  561. if (ret) {
  562. dev_err(nor->dev,
  563. "Indirect write completion error (%i)\n", ret);
  564. goto failwr;
  565. }
  566. /* Disable interrupt. */
  567. writel(0, reg_base + CQSPI_REG_IRQMASK);
  568. /* Clear indirect completion status */
  569. writel(CQSPI_REG_INDIRECTWR_DONE_MASK, reg_base + CQSPI_REG_INDIRECTWR);
  570. cqspi_wait_idle(cqspi);
  571. return 0;
  572. failwr:
  573. /* Disable interrupt. */
  574. writel(0, reg_base + CQSPI_REG_IRQMASK);
  575. /* Cancel the indirect write */
  576. writel(CQSPI_REG_INDIRECTWR_CANCEL_MASK,
  577. reg_base + CQSPI_REG_INDIRECTWR);
  578. return ret;
  579. }
  580. static void cqspi_chipselect(struct spi_nor *nor)
  581. {
  582. struct cqspi_flash_pdata *f_pdata = nor->priv;
  583. struct cqspi_st *cqspi = f_pdata->cqspi;
  584. void __iomem *reg_base = cqspi->iobase;
  585. unsigned int chip_select = f_pdata->cs;
  586. unsigned int reg;
  587. reg = readl(reg_base + CQSPI_REG_CONFIG);
  588. if (cqspi->is_decoded_cs) {
  589. reg |= CQSPI_REG_CONFIG_DECODE_MASK;
  590. } else {
  591. reg &= ~CQSPI_REG_CONFIG_DECODE_MASK;
  592. /* Convert CS if without decoder.
  593. * CS0 to 4b'1110
  594. * CS1 to 4b'1101
  595. * CS2 to 4b'1011
  596. * CS3 to 4b'0111
  597. */
  598. chip_select = 0xF & ~(1 << chip_select);
  599. }
  600. reg &= ~(CQSPI_REG_CONFIG_CHIPSELECT_MASK
  601. << CQSPI_REG_CONFIG_CHIPSELECT_LSB);
  602. reg |= (chip_select & CQSPI_REG_CONFIG_CHIPSELECT_MASK)
  603. << CQSPI_REG_CONFIG_CHIPSELECT_LSB;
  604. writel(reg, reg_base + CQSPI_REG_CONFIG);
  605. }
  606. static void cqspi_configure_cs_and_sizes(struct spi_nor *nor)
  607. {
  608. struct cqspi_flash_pdata *f_pdata = nor->priv;
  609. struct cqspi_st *cqspi = f_pdata->cqspi;
  610. void __iomem *iobase = cqspi->iobase;
  611. unsigned int reg;
  612. /* configure page size and block size. */
  613. reg = readl(iobase + CQSPI_REG_SIZE);
  614. reg &= ~(CQSPI_REG_SIZE_PAGE_MASK << CQSPI_REG_SIZE_PAGE_LSB);
  615. reg &= ~(CQSPI_REG_SIZE_BLOCK_MASK << CQSPI_REG_SIZE_BLOCK_LSB);
  616. reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
  617. reg |= (nor->page_size << CQSPI_REG_SIZE_PAGE_LSB);
  618. reg |= (ilog2(nor->mtd.erasesize) << CQSPI_REG_SIZE_BLOCK_LSB);
  619. reg |= (nor->addr_width - 1);
  620. writel(reg, iobase + CQSPI_REG_SIZE);
  621. /* configure the chip select */
  622. cqspi_chipselect(nor);
  623. /* Store the new configuration of the controller */
  624. cqspi->current_page_size = nor->page_size;
  625. cqspi->current_erase_size = nor->mtd.erasesize;
  626. cqspi->current_addr_width = nor->addr_width;
  627. }
  628. static unsigned int calculate_ticks_for_ns(const unsigned int ref_clk_hz,
  629. const unsigned int ns_val)
  630. {
  631. unsigned int ticks;
  632. ticks = ref_clk_hz / 1000; /* kHz */
  633. ticks = DIV_ROUND_UP(ticks * ns_val, 1000000);
  634. return ticks;
  635. }
  636. static void cqspi_delay(struct spi_nor *nor)
  637. {
  638. struct cqspi_flash_pdata *f_pdata = nor->priv;
  639. struct cqspi_st *cqspi = f_pdata->cqspi;
  640. void __iomem *iobase = cqspi->iobase;
  641. const unsigned int ref_clk_hz = cqspi->master_ref_clk_hz;
  642. unsigned int tshsl, tchsh, tslch, tsd2d;
  643. unsigned int reg;
  644. unsigned int tsclk;
  645. /* calculate the number of ref ticks for one sclk tick */
  646. tsclk = DIV_ROUND_UP(ref_clk_hz, cqspi->sclk);
  647. tshsl = calculate_ticks_for_ns(ref_clk_hz, f_pdata->tshsl_ns);
  648. /* this particular value must be at least one sclk */
  649. if (tshsl < tsclk)
  650. tshsl = tsclk;
  651. tchsh = calculate_ticks_for_ns(ref_clk_hz, f_pdata->tchsh_ns);
  652. tslch = calculate_ticks_for_ns(ref_clk_hz, f_pdata->tslch_ns);
  653. tsd2d = calculate_ticks_for_ns(ref_clk_hz, f_pdata->tsd2d_ns);
  654. reg = (tshsl & CQSPI_REG_DELAY_TSHSL_MASK)
  655. << CQSPI_REG_DELAY_TSHSL_LSB;
  656. reg |= (tchsh & CQSPI_REG_DELAY_TCHSH_MASK)
  657. << CQSPI_REG_DELAY_TCHSH_LSB;
  658. reg |= (tslch & CQSPI_REG_DELAY_TSLCH_MASK)
  659. << CQSPI_REG_DELAY_TSLCH_LSB;
  660. reg |= (tsd2d & CQSPI_REG_DELAY_TSD2D_MASK)
  661. << CQSPI_REG_DELAY_TSD2D_LSB;
  662. writel(reg, iobase + CQSPI_REG_DELAY);
  663. }
  664. static void cqspi_config_baudrate_div(struct cqspi_st *cqspi)
  665. {
  666. const unsigned int ref_clk_hz = cqspi->master_ref_clk_hz;
  667. void __iomem *reg_base = cqspi->iobase;
  668. u32 reg, div;
  669. /* Recalculate the baudrate divisor based on QSPI specification. */
  670. div = DIV_ROUND_UP(ref_clk_hz, 2 * cqspi->sclk) - 1;
  671. reg = readl(reg_base + CQSPI_REG_CONFIG);
  672. reg &= ~(CQSPI_REG_CONFIG_BAUD_MASK << CQSPI_REG_CONFIG_BAUD_LSB);
  673. reg |= (div & CQSPI_REG_CONFIG_BAUD_MASK) << CQSPI_REG_CONFIG_BAUD_LSB;
  674. writel(reg, reg_base + CQSPI_REG_CONFIG);
  675. }
  676. static void cqspi_readdata_capture(struct cqspi_st *cqspi,
  677. const bool bypass,
  678. const unsigned int delay)
  679. {
  680. void __iomem *reg_base = cqspi->iobase;
  681. unsigned int reg;
  682. reg = readl(reg_base + CQSPI_REG_READCAPTURE);
  683. if (bypass)
  684. reg |= (1 << CQSPI_REG_READCAPTURE_BYPASS_LSB);
  685. else
  686. reg &= ~(1 << CQSPI_REG_READCAPTURE_BYPASS_LSB);
  687. reg &= ~(CQSPI_REG_READCAPTURE_DELAY_MASK
  688. << CQSPI_REG_READCAPTURE_DELAY_LSB);
  689. reg |= (delay & CQSPI_REG_READCAPTURE_DELAY_MASK)
  690. << CQSPI_REG_READCAPTURE_DELAY_LSB;
  691. writel(reg, reg_base + CQSPI_REG_READCAPTURE);
  692. }
  693. static void cqspi_controller_enable(struct cqspi_st *cqspi, bool enable)
  694. {
  695. void __iomem *reg_base = cqspi->iobase;
  696. unsigned int reg;
  697. reg = readl(reg_base + CQSPI_REG_CONFIG);
  698. if (enable)
  699. reg |= CQSPI_REG_CONFIG_ENABLE_MASK;
  700. else
  701. reg &= ~CQSPI_REG_CONFIG_ENABLE_MASK;
  702. writel(reg, reg_base + CQSPI_REG_CONFIG);
  703. }
  704. static void cqspi_configure(struct spi_nor *nor)
  705. {
  706. struct cqspi_flash_pdata *f_pdata = nor->priv;
  707. struct cqspi_st *cqspi = f_pdata->cqspi;
  708. const unsigned int sclk = f_pdata->clk_rate;
  709. int switch_cs = (cqspi->current_cs != f_pdata->cs);
  710. int switch_ck = (cqspi->sclk != sclk);
  711. if ((cqspi->current_page_size != nor->page_size) ||
  712. (cqspi->current_erase_size != nor->mtd.erasesize) ||
  713. (cqspi->current_addr_width != nor->addr_width))
  714. switch_cs = 1;
  715. if (switch_cs || switch_ck)
  716. cqspi_controller_enable(cqspi, 0);
  717. /* Switch chip select. */
  718. if (switch_cs) {
  719. cqspi->current_cs = f_pdata->cs;
  720. cqspi_configure_cs_and_sizes(nor);
  721. }
  722. /* Setup baudrate divisor and delays */
  723. if (switch_ck) {
  724. cqspi->sclk = sclk;
  725. cqspi_config_baudrate_div(cqspi);
  726. cqspi_delay(nor);
  727. cqspi_readdata_capture(cqspi, !cqspi->rclk_en,
  728. f_pdata->read_delay);
  729. }
  730. if (switch_cs || switch_ck)
  731. cqspi_controller_enable(cqspi, 1);
  732. }
  733. static int cqspi_set_protocol(struct spi_nor *nor, const int read)
  734. {
  735. struct cqspi_flash_pdata *f_pdata = nor->priv;
  736. f_pdata->inst_width = CQSPI_INST_TYPE_SINGLE;
  737. f_pdata->addr_width = CQSPI_INST_TYPE_SINGLE;
  738. f_pdata->data_width = CQSPI_INST_TYPE_SINGLE;
  739. if (read) {
  740. switch (nor->read_proto) {
  741. case SNOR_PROTO_1_1_1:
  742. f_pdata->data_width = CQSPI_INST_TYPE_SINGLE;
  743. break;
  744. case SNOR_PROTO_1_1_2:
  745. f_pdata->data_width = CQSPI_INST_TYPE_DUAL;
  746. break;
  747. case SNOR_PROTO_1_1_4:
  748. f_pdata->data_width = CQSPI_INST_TYPE_QUAD;
  749. break;
  750. default:
  751. return -EINVAL;
  752. }
  753. }
  754. cqspi_configure(nor);
  755. return 0;
  756. }
  757. static ssize_t cqspi_write(struct spi_nor *nor, loff_t to,
  758. size_t len, const u_char *buf)
  759. {
  760. struct cqspi_flash_pdata *f_pdata = nor->priv;
  761. struct cqspi_st *cqspi = f_pdata->cqspi;
  762. int ret;
  763. ret = cqspi_set_protocol(nor, 0);
  764. if (ret)
  765. return ret;
  766. ret = cqspi_write_setup(nor);
  767. if (ret)
  768. return ret;
  769. if (f_pdata->use_direct_mode) {
  770. memcpy_toio(cqspi->ahb_base + to, buf, len);
  771. ret = cqspi_wait_idle(cqspi);
  772. } else {
  773. ret = cqspi_indirect_write_execute(nor, to, buf, len);
  774. }
  775. if (ret)
  776. return ret;
  777. return len;
  778. }
  779. static void cqspi_rx_dma_callback(void *param)
  780. {
  781. struct cqspi_st *cqspi = param;
  782. complete(&cqspi->rx_dma_complete);
  783. }
  784. static int cqspi_direct_read_execute(struct spi_nor *nor, u_char *buf,
  785. loff_t from, size_t len)
  786. {
  787. struct cqspi_flash_pdata *f_pdata = nor->priv;
  788. struct cqspi_st *cqspi = f_pdata->cqspi;
  789. enum dma_ctrl_flags flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
  790. dma_addr_t dma_src = (dma_addr_t)cqspi->mmap_phys_base + from;
  791. int ret = 0;
  792. struct dma_async_tx_descriptor *tx;
  793. dma_cookie_t cookie;
  794. dma_addr_t dma_dst;
  795. if (!cqspi->rx_chan || !virt_addr_valid(buf)) {
  796. memcpy_fromio(buf, cqspi->ahb_base + from, len);
  797. return 0;
  798. }
  799. dma_dst = dma_map_single(nor->dev, buf, len, DMA_FROM_DEVICE);
  800. if (dma_mapping_error(nor->dev, dma_dst)) {
  801. dev_err(nor->dev, "dma mapping failed\n");
  802. return -ENOMEM;
  803. }
  804. tx = dmaengine_prep_dma_memcpy(cqspi->rx_chan, dma_dst, dma_src,
  805. len, flags);
  806. if (!tx) {
  807. dev_err(nor->dev, "device_prep_dma_memcpy error\n");
  808. ret = -EIO;
  809. goto err_unmap;
  810. }
  811. tx->callback = cqspi_rx_dma_callback;
  812. tx->callback_param = cqspi;
  813. cookie = tx->tx_submit(tx);
  814. reinit_completion(&cqspi->rx_dma_complete);
  815. ret = dma_submit_error(cookie);
  816. if (ret) {
  817. dev_err(nor->dev, "dma_submit_error %d\n", cookie);
  818. ret = -EIO;
  819. goto err_unmap;
  820. }
  821. dma_async_issue_pending(cqspi->rx_chan);
  822. if (!wait_for_completion_timeout(&cqspi->rx_dma_complete,
  823. msecs_to_jiffies(len))) {
  824. dmaengine_terminate_sync(cqspi->rx_chan);
  825. dev_err(nor->dev, "DMA wait_for_completion_timeout\n");
  826. ret = -ETIMEDOUT;
  827. goto err_unmap;
  828. }
  829. err_unmap:
  830. dma_unmap_single(nor->dev, dma_dst, len, DMA_FROM_DEVICE);
  831. return ret;
  832. }
  833. static ssize_t cqspi_read(struct spi_nor *nor, loff_t from,
  834. size_t len, u_char *buf)
  835. {
  836. struct cqspi_flash_pdata *f_pdata = nor->priv;
  837. int ret;
  838. ret = cqspi_set_protocol(nor, 1);
  839. if (ret)
  840. return ret;
  841. ret = cqspi_read_setup(nor);
  842. if (ret)
  843. return ret;
  844. if (f_pdata->use_direct_mode)
  845. ret = cqspi_direct_read_execute(nor, buf, from, len);
  846. else
  847. ret = cqspi_indirect_read_execute(nor, buf, from, len);
  848. if (ret)
  849. return ret;
  850. return len;
  851. }
  852. static int cqspi_erase(struct spi_nor *nor, loff_t offs)
  853. {
  854. int ret;
  855. ret = cqspi_set_protocol(nor, 0);
  856. if (ret)
  857. return ret;
  858. /* Send write enable, then erase commands. */
  859. ret = nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
  860. if (ret)
  861. return ret;
  862. /* Set up command buffer. */
  863. ret = cqspi_command_write_addr(nor, nor->erase_opcode, offs);
  864. if (ret)
  865. return ret;
  866. return 0;
  867. }
  868. static int cqspi_prep(struct spi_nor *nor, enum spi_nor_ops ops)
  869. {
  870. struct cqspi_flash_pdata *f_pdata = nor->priv;
  871. struct cqspi_st *cqspi = f_pdata->cqspi;
  872. mutex_lock(&cqspi->bus_mutex);
  873. return 0;
  874. }
  875. static void cqspi_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
  876. {
  877. struct cqspi_flash_pdata *f_pdata = nor->priv;
  878. struct cqspi_st *cqspi = f_pdata->cqspi;
  879. mutex_unlock(&cqspi->bus_mutex);
  880. }
  881. static int cqspi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  882. {
  883. int ret;
  884. ret = cqspi_set_protocol(nor, 0);
  885. if (!ret)
  886. ret = cqspi_command_read(nor, &opcode, 1, buf, len);
  887. return ret;
  888. }
  889. static int cqspi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  890. {
  891. int ret;
  892. ret = cqspi_set_protocol(nor, 0);
  893. if (!ret)
  894. ret = cqspi_command_write(nor, opcode, buf, len);
  895. return ret;
  896. }
  897. static int cqspi_of_get_flash_pdata(struct platform_device *pdev,
  898. struct cqspi_flash_pdata *f_pdata,
  899. struct device_node *np)
  900. {
  901. if (of_property_read_u32(np, "cdns,read-delay", &f_pdata->read_delay)) {
  902. dev_err(&pdev->dev, "couldn't determine read-delay\n");
  903. return -ENXIO;
  904. }
  905. if (of_property_read_u32(np, "cdns,tshsl-ns", &f_pdata->tshsl_ns)) {
  906. dev_err(&pdev->dev, "couldn't determine tshsl-ns\n");
  907. return -ENXIO;
  908. }
  909. if (of_property_read_u32(np, "cdns,tsd2d-ns", &f_pdata->tsd2d_ns)) {
  910. dev_err(&pdev->dev, "couldn't determine tsd2d-ns\n");
  911. return -ENXIO;
  912. }
  913. if (of_property_read_u32(np, "cdns,tchsh-ns", &f_pdata->tchsh_ns)) {
  914. dev_err(&pdev->dev, "couldn't determine tchsh-ns\n");
  915. return -ENXIO;
  916. }
  917. if (of_property_read_u32(np, "cdns,tslch-ns", &f_pdata->tslch_ns)) {
  918. dev_err(&pdev->dev, "couldn't determine tslch-ns\n");
  919. return -ENXIO;
  920. }
  921. if (of_property_read_u32(np, "spi-max-frequency", &f_pdata->clk_rate)) {
  922. dev_err(&pdev->dev, "couldn't determine spi-max-frequency\n");
  923. return -ENXIO;
  924. }
  925. return 0;
  926. }
  927. static int cqspi_of_get_pdata(struct platform_device *pdev)
  928. {
  929. struct device_node *np = pdev->dev.of_node;
  930. struct cqspi_st *cqspi = platform_get_drvdata(pdev);
  931. cqspi->is_decoded_cs = of_property_read_bool(np, "cdns,is-decoded-cs");
  932. if (of_property_read_u32(np, "cdns,fifo-depth", &cqspi->fifo_depth)) {
  933. dev_err(&pdev->dev, "couldn't determine fifo-depth\n");
  934. return -ENXIO;
  935. }
  936. if (of_property_read_u32(np, "cdns,fifo-width", &cqspi->fifo_width)) {
  937. dev_err(&pdev->dev, "couldn't determine fifo-width\n");
  938. return -ENXIO;
  939. }
  940. if (of_property_read_u32(np, "cdns,trigger-address",
  941. &cqspi->trigger_address)) {
  942. dev_err(&pdev->dev, "couldn't determine trigger-address\n");
  943. return -ENXIO;
  944. }
  945. cqspi->rclk_en = of_property_read_bool(np, "cdns,rclk-en");
  946. return 0;
  947. }
  948. static void cqspi_controller_init(struct cqspi_st *cqspi)
  949. {
  950. u32 reg;
  951. cqspi_controller_enable(cqspi, 0);
  952. /* Configure the remap address register, no remap */
  953. writel(0, cqspi->iobase + CQSPI_REG_REMAP);
  954. /* Disable all interrupts. */
  955. writel(0, cqspi->iobase + CQSPI_REG_IRQMASK);
  956. /* Configure the SRAM split to 1:1 . */
  957. writel(cqspi->fifo_depth / 2, cqspi->iobase + CQSPI_REG_SRAMPARTITION);
  958. /* Load indirect trigger address. */
  959. writel(cqspi->trigger_address,
  960. cqspi->iobase + CQSPI_REG_INDIRECTTRIGGER);
  961. /* Program read watermark -- 1/2 of the FIFO. */
  962. writel(cqspi->fifo_depth * cqspi->fifo_width / 2,
  963. cqspi->iobase + CQSPI_REG_INDIRECTRDWATERMARK);
  964. /* Program write watermark -- 1/8 of the FIFO. */
  965. writel(cqspi->fifo_depth * cqspi->fifo_width / 8,
  966. cqspi->iobase + CQSPI_REG_INDIRECTWRWATERMARK);
  967. /* Enable Direct Access Controller */
  968. reg = readl(cqspi->iobase + CQSPI_REG_CONFIG);
  969. reg |= CQSPI_REG_CONFIG_ENB_DIR_ACC_CTRL;
  970. writel(reg, cqspi->iobase + CQSPI_REG_CONFIG);
  971. cqspi_controller_enable(cqspi, 1);
  972. }
  973. static void cqspi_request_mmap_dma(struct cqspi_st *cqspi)
  974. {
  975. dma_cap_mask_t mask;
  976. dma_cap_zero(mask);
  977. dma_cap_set(DMA_MEMCPY, mask);
  978. cqspi->rx_chan = dma_request_chan_by_mask(&mask);
  979. if (IS_ERR(cqspi->rx_chan)) {
  980. dev_err(&cqspi->pdev->dev, "No Rx DMA available\n");
  981. cqspi->rx_chan = NULL;
  982. }
  983. init_completion(&cqspi->rx_dma_complete);
  984. }
  985. static int cqspi_setup_flash(struct cqspi_st *cqspi, struct device_node *np)
  986. {
  987. const struct spi_nor_hwcaps hwcaps = {
  988. .mask = SNOR_HWCAPS_READ |
  989. SNOR_HWCAPS_READ_FAST |
  990. SNOR_HWCAPS_READ_1_1_2 |
  991. SNOR_HWCAPS_READ_1_1_4 |
  992. SNOR_HWCAPS_PP,
  993. };
  994. struct platform_device *pdev = cqspi->pdev;
  995. struct device *dev = &pdev->dev;
  996. struct cqspi_flash_pdata *f_pdata;
  997. struct spi_nor *nor;
  998. struct mtd_info *mtd;
  999. unsigned int cs;
  1000. int i, ret;
  1001. /* Get flash device data */
  1002. for_each_available_child_of_node(dev->of_node, np) {
  1003. ret = of_property_read_u32(np, "reg", &cs);
  1004. if (ret) {
  1005. dev_err(dev, "Couldn't determine chip select.\n");
  1006. goto err;
  1007. }
  1008. if (cs >= CQSPI_MAX_CHIPSELECT) {
  1009. ret = -EINVAL;
  1010. dev_err(dev, "Chip select %d out of range.\n", cs);
  1011. goto err;
  1012. }
  1013. f_pdata = &cqspi->f_pdata[cs];
  1014. f_pdata->cqspi = cqspi;
  1015. f_pdata->cs = cs;
  1016. ret = cqspi_of_get_flash_pdata(pdev, f_pdata, np);
  1017. if (ret)
  1018. goto err;
  1019. nor = &f_pdata->nor;
  1020. mtd = &nor->mtd;
  1021. mtd->priv = nor;
  1022. nor->dev = dev;
  1023. spi_nor_set_flash_node(nor, np);
  1024. nor->priv = f_pdata;
  1025. nor->read_reg = cqspi_read_reg;
  1026. nor->write_reg = cqspi_write_reg;
  1027. nor->read = cqspi_read;
  1028. nor->write = cqspi_write;
  1029. nor->erase = cqspi_erase;
  1030. nor->prepare = cqspi_prep;
  1031. nor->unprepare = cqspi_unprep;
  1032. mtd->name = devm_kasprintf(dev, GFP_KERNEL, "%s.%d",
  1033. dev_name(dev), cs);
  1034. if (!mtd->name) {
  1035. ret = -ENOMEM;
  1036. goto err;
  1037. }
  1038. ret = spi_nor_scan(nor, NULL, &hwcaps);
  1039. if (ret)
  1040. goto err;
  1041. ret = mtd_device_register(mtd, NULL, 0);
  1042. if (ret)
  1043. goto err;
  1044. f_pdata->registered = true;
  1045. if (mtd->size <= cqspi->ahb_size) {
  1046. f_pdata->use_direct_mode = true;
  1047. dev_dbg(nor->dev, "using direct mode for %s\n",
  1048. mtd->name);
  1049. if (!cqspi->rx_chan)
  1050. cqspi_request_mmap_dma(cqspi);
  1051. }
  1052. }
  1053. return 0;
  1054. err:
  1055. for (i = 0; i < CQSPI_MAX_CHIPSELECT; i++)
  1056. if (cqspi->f_pdata[i].registered)
  1057. mtd_device_unregister(&cqspi->f_pdata[i].nor.mtd);
  1058. return ret;
  1059. }
  1060. static int cqspi_probe(struct platform_device *pdev)
  1061. {
  1062. struct device_node *np = pdev->dev.of_node;
  1063. struct device *dev = &pdev->dev;
  1064. struct cqspi_st *cqspi;
  1065. struct resource *res;
  1066. struct resource *res_ahb;
  1067. unsigned long data;
  1068. int ret;
  1069. int irq;
  1070. cqspi = devm_kzalloc(dev, sizeof(*cqspi), GFP_KERNEL);
  1071. if (!cqspi)
  1072. return -ENOMEM;
  1073. mutex_init(&cqspi->bus_mutex);
  1074. cqspi->pdev = pdev;
  1075. platform_set_drvdata(pdev, cqspi);
  1076. /* Obtain configuration from OF. */
  1077. ret = cqspi_of_get_pdata(pdev);
  1078. if (ret) {
  1079. dev_err(dev, "Cannot get mandatory OF data.\n");
  1080. return -ENODEV;
  1081. }
  1082. /* Obtain QSPI clock. */
  1083. cqspi->clk = devm_clk_get(dev, NULL);
  1084. if (IS_ERR(cqspi->clk)) {
  1085. dev_err(dev, "Cannot claim QSPI clock.\n");
  1086. return PTR_ERR(cqspi->clk);
  1087. }
  1088. /* Obtain and remap controller address. */
  1089. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1090. cqspi->iobase = devm_ioremap_resource(dev, res);
  1091. if (IS_ERR(cqspi->iobase)) {
  1092. dev_err(dev, "Cannot remap controller address.\n");
  1093. return PTR_ERR(cqspi->iobase);
  1094. }
  1095. /* Obtain and remap AHB address. */
  1096. res_ahb = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1097. cqspi->ahb_base = devm_ioremap_resource(dev, res_ahb);
  1098. if (IS_ERR(cqspi->ahb_base)) {
  1099. dev_err(dev, "Cannot remap AHB address.\n");
  1100. return PTR_ERR(cqspi->ahb_base);
  1101. }
  1102. cqspi->mmap_phys_base = (dma_addr_t)res_ahb->start;
  1103. cqspi->ahb_size = resource_size(res_ahb);
  1104. init_completion(&cqspi->transfer_complete);
  1105. /* Obtain IRQ line. */
  1106. irq = platform_get_irq(pdev, 0);
  1107. if (irq < 0) {
  1108. dev_err(dev, "Cannot obtain IRQ.\n");
  1109. return -ENXIO;
  1110. }
  1111. pm_runtime_enable(dev);
  1112. ret = pm_runtime_get_sync(dev);
  1113. if (ret < 0) {
  1114. pm_runtime_put_noidle(dev);
  1115. return ret;
  1116. }
  1117. ret = clk_prepare_enable(cqspi->clk);
  1118. if (ret) {
  1119. dev_err(dev, "Cannot enable QSPI clock.\n");
  1120. goto probe_clk_failed;
  1121. }
  1122. cqspi->master_ref_clk_hz = clk_get_rate(cqspi->clk);
  1123. data = (unsigned long)of_device_get_match_data(dev);
  1124. if (data & CQSPI_NEEDS_WR_DELAY)
  1125. cqspi->wr_delay = 5 * DIV_ROUND_UP(NSEC_PER_SEC,
  1126. cqspi->master_ref_clk_hz);
  1127. ret = devm_request_irq(dev, irq, cqspi_irq_handler, 0,
  1128. pdev->name, cqspi);
  1129. if (ret) {
  1130. dev_err(dev, "Cannot request IRQ.\n");
  1131. goto probe_irq_failed;
  1132. }
  1133. cqspi_wait_idle(cqspi);
  1134. cqspi_controller_init(cqspi);
  1135. cqspi->current_cs = -1;
  1136. cqspi->sclk = 0;
  1137. ret = cqspi_setup_flash(cqspi, np);
  1138. if (ret) {
  1139. dev_err(dev, "Cadence QSPI NOR probe failed %d\n", ret);
  1140. goto probe_setup_failed;
  1141. }
  1142. return ret;
  1143. probe_setup_failed:
  1144. cqspi_controller_enable(cqspi, 0);
  1145. probe_irq_failed:
  1146. clk_disable_unprepare(cqspi->clk);
  1147. probe_clk_failed:
  1148. pm_runtime_put_sync(dev);
  1149. pm_runtime_disable(dev);
  1150. return ret;
  1151. }
  1152. static int cqspi_remove(struct platform_device *pdev)
  1153. {
  1154. struct cqspi_st *cqspi = platform_get_drvdata(pdev);
  1155. int i;
  1156. for (i = 0; i < CQSPI_MAX_CHIPSELECT; i++)
  1157. if (cqspi->f_pdata[i].registered)
  1158. mtd_device_unregister(&cqspi->f_pdata[i].nor.mtd);
  1159. cqspi_controller_enable(cqspi, 0);
  1160. if (cqspi->rx_chan)
  1161. dma_release_channel(cqspi->rx_chan);
  1162. clk_disable_unprepare(cqspi->clk);
  1163. pm_runtime_put_sync(&pdev->dev);
  1164. pm_runtime_disable(&pdev->dev);
  1165. return 0;
  1166. }
  1167. #ifdef CONFIG_PM_SLEEP
  1168. static int cqspi_suspend(struct device *dev)
  1169. {
  1170. struct cqspi_st *cqspi = dev_get_drvdata(dev);
  1171. cqspi_controller_enable(cqspi, 0);
  1172. return 0;
  1173. }
  1174. static int cqspi_resume(struct device *dev)
  1175. {
  1176. struct cqspi_st *cqspi = dev_get_drvdata(dev);
  1177. cqspi_controller_enable(cqspi, 1);
  1178. return 0;
  1179. }
  1180. static const struct dev_pm_ops cqspi__dev_pm_ops = {
  1181. .suspend = cqspi_suspend,
  1182. .resume = cqspi_resume,
  1183. };
  1184. #define CQSPI_DEV_PM_OPS (&cqspi__dev_pm_ops)
  1185. #else
  1186. #define CQSPI_DEV_PM_OPS NULL
  1187. #endif
  1188. static const struct of_device_id cqspi_dt_ids[] = {
  1189. {
  1190. .compatible = "cdns,qspi-nor",
  1191. .data = (void *)0,
  1192. },
  1193. {
  1194. .compatible = "ti,k2g-qspi",
  1195. .data = (void *)CQSPI_NEEDS_WR_DELAY,
  1196. },
  1197. { /* end of table */ }
  1198. };
  1199. MODULE_DEVICE_TABLE(of, cqspi_dt_ids);
  1200. static struct platform_driver cqspi_platform_driver = {
  1201. .probe = cqspi_probe,
  1202. .remove = cqspi_remove,
  1203. .driver = {
  1204. .name = CQSPI_NAME,
  1205. .pm = CQSPI_DEV_PM_OPS,
  1206. .of_match_table = cqspi_dt_ids,
  1207. },
  1208. };
  1209. module_platform_driver(cqspi_platform_driver);
  1210. MODULE_DESCRIPTION("Cadence QSPI Controller Driver");
  1211. MODULE_LICENSE("GPL v2");
  1212. MODULE_ALIAS("platform:" CQSPI_NAME);
  1213. MODULE_AUTHOR("Ley Foon Tan <lftan@altera.com>");
  1214. MODULE_AUTHOR("Graham Moore <grmoore@opensource.altera.com>");