fsl-quadspi.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /*
  2. * Freescale QuadSPI driver.
  3. *
  4. * Copyright (C) 2013 Freescale Semiconductor, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/errno.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/sched.h>
  17. #include <linux/delay.h>
  18. #include <linux/io.h>
  19. #include <linux/clk.h>
  20. #include <linux/err.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/timer.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/completion.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/mtd/partitions.h>
  28. #include <linux/mtd/spi-nor.h>
  29. /* The registers */
  30. #define QUADSPI_MCR 0x00
  31. #define QUADSPI_MCR_RESERVED_SHIFT 16
  32. #define QUADSPI_MCR_RESERVED_MASK (0xF << QUADSPI_MCR_RESERVED_SHIFT)
  33. #define QUADSPI_MCR_MDIS_SHIFT 14
  34. #define QUADSPI_MCR_MDIS_MASK (1 << QUADSPI_MCR_MDIS_SHIFT)
  35. #define QUADSPI_MCR_CLR_TXF_SHIFT 11
  36. #define QUADSPI_MCR_CLR_TXF_MASK (1 << QUADSPI_MCR_CLR_TXF_SHIFT)
  37. #define QUADSPI_MCR_CLR_RXF_SHIFT 10
  38. #define QUADSPI_MCR_CLR_RXF_MASK (1 << QUADSPI_MCR_CLR_RXF_SHIFT)
  39. #define QUADSPI_MCR_DDR_EN_SHIFT 7
  40. #define QUADSPI_MCR_DDR_EN_MASK (1 << QUADSPI_MCR_DDR_EN_SHIFT)
  41. #define QUADSPI_MCR_END_CFG_SHIFT 2
  42. #define QUADSPI_MCR_END_CFG_MASK (3 << QUADSPI_MCR_END_CFG_SHIFT)
  43. #define QUADSPI_MCR_SWRSTHD_SHIFT 1
  44. #define QUADSPI_MCR_SWRSTHD_MASK (1 << QUADSPI_MCR_SWRSTHD_SHIFT)
  45. #define QUADSPI_MCR_SWRSTSD_SHIFT 0
  46. #define QUADSPI_MCR_SWRSTSD_MASK (1 << QUADSPI_MCR_SWRSTSD_SHIFT)
  47. #define QUADSPI_IPCR 0x08
  48. #define QUADSPI_IPCR_SEQID_SHIFT 24
  49. #define QUADSPI_IPCR_SEQID_MASK (0xF << QUADSPI_IPCR_SEQID_SHIFT)
  50. #define QUADSPI_BUF0CR 0x10
  51. #define QUADSPI_BUF1CR 0x14
  52. #define QUADSPI_BUF2CR 0x18
  53. #define QUADSPI_BUFXCR_INVALID_MSTRID 0xe
  54. #define QUADSPI_BUF3CR 0x1c
  55. #define QUADSPI_BUF3CR_ALLMST_SHIFT 31
  56. #define QUADSPI_BUF3CR_ALLMST_MASK (1 << QUADSPI_BUF3CR_ALLMST_SHIFT)
  57. #define QUADSPI_BUF3CR_ADATSZ_SHIFT 8
  58. #define QUADSPI_BUF3CR_ADATSZ_MASK (0xFF << QUADSPI_BUF3CR_ADATSZ_SHIFT)
  59. #define QUADSPI_BFGENCR 0x20
  60. #define QUADSPI_BFGENCR_PAR_EN_SHIFT 16
  61. #define QUADSPI_BFGENCR_PAR_EN_MASK (1 << (QUADSPI_BFGENCR_PAR_EN_SHIFT))
  62. #define QUADSPI_BFGENCR_SEQID_SHIFT 12
  63. #define QUADSPI_BFGENCR_SEQID_MASK (0xF << QUADSPI_BFGENCR_SEQID_SHIFT)
  64. #define QUADSPI_BUF0IND 0x30
  65. #define QUADSPI_BUF1IND 0x34
  66. #define QUADSPI_BUF2IND 0x38
  67. #define QUADSPI_SFAR 0x100
  68. #define QUADSPI_SMPR 0x108
  69. #define QUADSPI_SMPR_DDRSMP_SHIFT 16
  70. #define QUADSPI_SMPR_DDRSMP_MASK (7 << QUADSPI_SMPR_DDRSMP_SHIFT)
  71. #define QUADSPI_SMPR_FSDLY_SHIFT 6
  72. #define QUADSPI_SMPR_FSDLY_MASK (1 << QUADSPI_SMPR_FSDLY_SHIFT)
  73. #define QUADSPI_SMPR_FSPHS_SHIFT 5
  74. #define QUADSPI_SMPR_FSPHS_MASK (1 << QUADSPI_SMPR_FSPHS_SHIFT)
  75. #define QUADSPI_SMPR_HSENA_SHIFT 0
  76. #define QUADSPI_SMPR_HSENA_MASK (1 << QUADSPI_SMPR_HSENA_SHIFT)
  77. #define QUADSPI_RBSR 0x10c
  78. #define QUADSPI_RBSR_RDBFL_SHIFT 8
  79. #define QUADSPI_RBSR_RDBFL_MASK (0x3F << QUADSPI_RBSR_RDBFL_SHIFT)
  80. #define QUADSPI_RBCT 0x110
  81. #define QUADSPI_RBCT_WMRK_MASK 0x1F
  82. #define QUADSPI_RBCT_RXBRD_SHIFT 8
  83. #define QUADSPI_RBCT_RXBRD_USEIPS (0x1 << QUADSPI_RBCT_RXBRD_SHIFT)
  84. #define QUADSPI_TBSR 0x150
  85. #define QUADSPI_TBDR 0x154
  86. #define QUADSPI_SR 0x15c
  87. #define QUADSPI_SR_IP_ACC_SHIFT 1
  88. #define QUADSPI_SR_IP_ACC_MASK (0x1 << QUADSPI_SR_IP_ACC_SHIFT)
  89. #define QUADSPI_SR_AHB_ACC_SHIFT 2
  90. #define QUADSPI_SR_AHB_ACC_MASK (0x1 << QUADSPI_SR_AHB_ACC_SHIFT)
  91. #define QUADSPI_FR 0x160
  92. #define QUADSPI_FR_TFF_MASK 0x1
  93. #define QUADSPI_SFA1AD 0x180
  94. #define QUADSPI_SFA2AD 0x184
  95. #define QUADSPI_SFB1AD 0x188
  96. #define QUADSPI_SFB2AD 0x18c
  97. #define QUADSPI_RBDR 0x200
  98. #define QUADSPI_LUTKEY 0x300
  99. #define QUADSPI_LUTKEY_VALUE 0x5AF05AF0
  100. #define QUADSPI_LCKCR 0x304
  101. #define QUADSPI_LCKER_LOCK 0x1
  102. #define QUADSPI_LCKER_UNLOCK 0x2
  103. #define QUADSPI_RSER 0x164
  104. #define QUADSPI_RSER_TFIE (0x1 << 0)
  105. #define QUADSPI_LUT_BASE 0x310
  106. /*
  107. * The definition of the LUT register shows below:
  108. *
  109. * ---------------------------------------------------
  110. * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 |
  111. * ---------------------------------------------------
  112. */
  113. #define OPRND0_SHIFT 0
  114. #define PAD0_SHIFT 8
  115. #define INSTR0_SHIFT 10
  116. #define OPRND1_SHIFT 16
  117. /* Instruction set for the LUT register. */
  118. #define LUT_STOP 0
  119. #define LUT_CMD 1
  120. #define LUT_ADDR 2
  121. #define LUT_DUMMY 3
  122. #define LUT_MODE 4
  123. #define LUT_MODE2 5
  124. #define LUT_MODE4 6
  125. #define LUT_READ 7
  126. #define LUT_WRITE 8
  127. #define LUT_JMP_ON_CS 9
  128. #define LUT_ADDR_DDR 10
  129. #define LUT_MODE_DDR 11
  130. #define LUT_MODE2_DDR 12
  131. #define LUT_MODE4_DDR 13
  132. #define LUT_READ_DDR 14
  133. #define LUT_WRITE_DDR 15
  134. #define LUT_DATA_LEARN 16
  135. /*
  136. * The PAD definitions for LUT register.
  137. *
  138. * The pad stands for the lines number of IO[0:3].
  139. * For example, the Quad read need four IO lines, so you should
  140. * set LUT_PAD4 which means we use four IO lines.
  141. */
  142. #define LUT_PAD1 0
  143. #define LUT_PAD2 1
  144. #define LUT_PAD4 2
  145. /* Oprands for the LUT register. */
  146. #define ADDR24BIT 0x18
  147. #define ADDR32BIT 0x20
  148. /* Macros for constructing the LUT register. */
  149. #define LUT0(ins, pad, opr) \
  150. (((opr) << OPRND0_SHIFT) | ((LUT_##pad) << PAD0_SHIFT) | \
  151. ((LUT_##ins) << INSTR0_SHIFT))
  152. #define LUT1(ins, pad, opr) (LUT0(ins, pad, opr) << OPRND1_SHIFT)
  153. /* other macros for LUT register. */
  154. #define QUADSPI_LUT(x) (QUADSPI_LUT_BASE + (x) * 4)
  155. #define QUADSPI_LUT_NUM 64
  156. /* SEQID -- we can have 16 seqids at most. */
  157. #define SEQID_QUAD_READ 0
  158. #define SEQID_WREN 1
  159. #define SEQID_WRDI 2
  160. #define SEQID_RDSR 3
  161. #define SEQID_SE 4
  162. #define SEQID_CHIP_ERASE 5
  163. #define SEQID_PP 6
  164. #define SEQID_RDID 7
  165. #define SEQID_WRSR 8
  166. #define SEQID_RDCR 9
  167. #define SEQID_EN4B 10
  168. #define SEQID_BRWR 11
  169. enum fsl_qspi_devtype {
  170. FSL_QUADSPI_VYBRID,
  171. FSL_QUADSPI_IMX6SX,
  172. };
  173. struct fsl_qspi_devtype_data {
  174. enum fsl_qspi_devtype devtype;
  175. int rxfifo;
  176. int txfifo;
  177. int ahb_buf_size;
  178. };
  179. static struct fsl_qspi_devtype_data vybrid_data = {
  180. .devtype = FSL_QUADSPI_VYBRID,
  181. .rxfifo = 128,
  182. .txfifo = 64,
  183. .ahb_buf_size = 1024
  184. };
  185. static struct fsl_qspi_devtype_data imx6sx_data = {
  186. .devtype = FSL_QUADSPI_IMX6SX,
  187. .rxfifo = 128,
  188. .txfifo = 512,
  189. .ahb_buf_size = 1024
  190. };
  191. #define FSL_QSPI_MAX_CHIP 4
  192. struct fsl_qspi {
  193. struct mtd_info mtd[FSL_QSPI_MAX_CHIP];
  194. struct spi_nor nor[FSL_QSPI_MAX_CHIP];
  195. void __iomem *iobase;
  196. void __iomem *ahb_base; /* Used when read from AHB bus */
  197. u32 memmap_phy;
  198. struct clk *clk, *clk_en;
  199. struct device *dev;
  200. struct completion c;
  201. struct fsl_qspi_devtype_data *devtype_data;
  202. u32 nor_size;
  203. u32 nor_num;
  204. u32 clk_rate;
  205. unsigned int chip_base_addr; /* We may support two chips. */
  206. bool has_second_chip;
  207. };
  208. static inline int is_vybrid_qspi(struct fsl_qspi *q)
  209. {
  210. return q->devtype_data->devtype == FSL_QUADSPI_VYBRID;
  211. }
  212. static inline int is_imx6sx_qspi(struct fsl_qspi *q)
  213. {
  214. return q->devtype_data->devtype == FSL_QUADSPI_IMX6SX;
  215. }
  216. /*
  217. * An IC bug makes us to re-arrange the 32-bit data.
  218. * The following chips, such as IMX6SLX, have fixed this bug.
  219. */
  220. static inline u32 fsl_qspi_endian_xchg(struct fsl_qspi *q, u32 a)
  221. {
  222. return is_vybrid_qspi(q) ? __swab32(a) : a;
  223. }
  224. static inline void fsl_qspi_unlock_lut(struct fsl_qspi *q)
  225. {
  226. writel(QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
  227. writel(QUADSPI_LCKER_UNLOCK, q->iobase + QUADSPI_LCKCR);
  228. }
  229. static inline void fsl_qspi_lock_lut(struct fsl_qspi *q)
  230. {
  231. writel(QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
  232. writel(QUADSPI_LCKER_LOCK, q->iobase + QUADSPI_LCKCR);
  233. }
  234. static irqreturn_t fsl_qspi_irq_handler(int irq, void *dev_id)
  235. {
  236. struct fsl_qspi *q = dev_id;
  237. u32 reg;
  238. /* clear interrupt */
  239. reg = readl(q->iobase + QUADSPI_FR);
  240. writel(reg, q->iobase + QUADSPI_FR);
  241. if (reg & QUADSPI_FR_TFF_MASK)
  242. complete(&q->c);
  243. dev_dbg(q->dev, "QUADSPI_FR : 0x%.8x:0x%.8x\n", q->chip_base_addr, reg);
  244. return IRQ_HANDLED;
  245. }
  246. static void fsl_qspi_init_lut(struct fsl_qspi *q)
  247. {
  248. void __iomem *base = q->iobase;
  249. int rxfifo = q->devtype_data->rxfifo;
  250. u32 lut_base;
  251. u8 cmd, addrlen, dummy;
  252. int i;
  253. fsl_qspi_unlock_lut(q);
  254. /* Clear all the LUT table */
  255. for (i = 0; i < QUADSPI_LUT_NUM; i++)
  256. writel(0, base + QUADSPI_LUT_BASE + i * 4);
  257. /* Quad Read */
  258. lut_base = SEQID_QUAD_READ * 4;
  259. if (q->nor_size <= SZ_16M) {
  260. cmd = SPINOR_OP_READ_1_1_4;
  261. addrlen = ADDR24BIT;
  262. dummy = 8;
  263. } else {
  264. /* use the 4-byte address */
  265. cmd = SPINOR_OP_READ_1_1_4;
  266. addrlen = ADDR32BIT;
  267. dummy = 8;
  268. }
  269. writel(LUT0(CMD, PAD1, cmd) | LUT1(ADDR, PAD1, addrlen),
  270. base + QUADSPI_LUT(lut_base));
  271. writel(LUT0(DUMMY, PAD1, dummy) | LUT1(READ, PAD4, rxfifo),
  272. base + QUADSPI_LUT(lut_base + 1));
  273. /* Write enable */
  274. lut_base = SEQID_WREN * 4;
  275. writel(LUT0(CMD, PAD1, SPINOR_OP_WREN), base + QUADSPI_LUT(lut_base));
  276. /* Page Program */
  277. lut_base = SEQID_PP * 4;
  278. if (q->nor_size <= SZ_16M) {
  279. cmd = SPINOR_OP_PP;
  280. addrlen = ADDR24BIT;
  281. } else {
  282. /* use the 4-byte address */
  283. cmd = SPINOR_OP_PP;
  284. addrlen = ADDR32BIT;
  285. }
  286. writel(LUT0(CMD, PAD1, cmd) | LUT1(ADDR, PAD1, addrlen),
  287. base + QUADSPI_LUT(lut_base));
  288. writel(LUT0(WRITE, PAD1, 0), base + QUADSPI_LUT(lut_base + 1));
  289. /* Read Status */
  290. lut_base = SEQID_RDSR * 4;
  291. writel(LUT0(CMD, PAD1, SPINOR_OP_RDSR) | LUT1(READ, PAD1, 0x1),
  292. base + QUADSPI_LUT(lut_base));
  293. /* Erase a sector */
  294. lut_base = SEQID_SE * 4;
  295. if (q->nor_size <= SZ_16M) {
  296. cmd = SPINOR_OP_SE;
  297. addrlen = ADDR24BIT;
  298. } else {
  299. /* use the 4-byte address */
  300. cmd = SPINOR_OP_SE;
  301. addrlen = ADDR32BIT;
  302. }
  303. writel(LUT0(CMD, PAD1, cmd) | LUT1(ADDR, PAD1, addrlen),
  304. base + QUADSPI_LUT(lut_base));
  305. /* Erase the whole chip */
  306. lut_base = SEQID_CHIP_ERASE * 4;
  307. writel(LUT0(CMD, PAD1, SPINOR_OP_CHIP_ERASE),
  308. base + QUADSPI_LUT(lut_base));
  309. /* READ ID */
  310. lut_base = SEQID_RDID * 4;
  311. writel(LUT0(CMD, PAD1, SPINOR_OP_RDID) | LUT1(READ, PAD1, 0x8),
  312. base + QUADSPI_LUT(lut_base));
  313. /* Write Register */
  314. lut_base = SEQID_WRSR * 4;
  315. writel(LUT0(CMD, PAD1, SPINOR_OP_WRSR) | LUT1(WRITE, PAD1, 0x2),
  316. base + QUADSPI_LUT(lut_base));
  317. /* Read Configuration Register */
  318. lut_base = SEQID_RDCR * 4;
  319. writel(LUT0(CMD, PAD1, SPINOR_OP_RDCR) | LUT1(READ, PAD1, 0x1),
  320. base + QUADSPI_LUT(lut_base));
  321. /* Write disable */
  322. lut_base = SEQID_WRDI * 4;
  323. writel(LUT0(CMD, PAD1, SPINOR_OP_WRDI), base + QUADSPI_LUT(lut_base));
  324. /* Enter 4 Byte Mode (Micron) */
  325. lut_base = SEQID_EN4B * 4;
  326. writel(LUT0(CMD, PAD1, SPINOR_OP_EN4B), base + QUADSPI_LUT(lut_base));
  327. /* Enter 4 Byte Mode (Spansion) */
  328. lut_base = SEQID_BRWR * 4;
  329. writel(LUT0(CMD, PAD1, SPINOR_OP_BRWR), base + QUADSPI_LUT(lut_base));
  330. fsl_qspi_lock_lut(q);
  331. }
  332. /* Get the SEQID for the command */
  333. static int fsl_qspi_get_seqid(struct fsl_qspi *q, u8 cmd)
  334. {
  335. switch (cmd) {
  336. case SPINOR_OP_READ_1_1_4:
  337. return SEQID_QUAD_READ;
  338. case SPINOR_OP_WREN:
  339. return SEQID_WREN;
  340. case SPINOR_OP_WRDI:
  341. return SEQID_WRDI;
  342. case SPINOR_OP_RDSR:
  343. return SEQID_RDSR;
  344. case SPINOR_OP_SE:
  345. return SEQID_SE;
  346. case SPINOR_OP_CHIP_ERASE:
  347. return SEQID_CHIP_ERASE;
  348. case SPINOR_OP_PP:
  349. return SEQID_PP;
  350. case SPINOR_OP_RDID:
  351. return SEQID_RDID;
  352. case SPINOR_OP_WRSR:
  353. return SEQID_WRSR;
  354. case SPINOR_OP_RDCR:
  355. return SEQID_RDCR;
  356. case SPINOR_OP_EN4B:
  357. return SEQID_EN4B;
  358. case SPINOR_OP_BRWR:
  359. return SEQID_BRWR;
  360. default:
  361. dev_err(q->dev, "Unsupported cmd 0x%.2x\n", cmd);
  362. break;
  363. }
  364. return -EINVAL;
  365. }
  366. static int
  367. fsl_qspi_runcmd(struct fsl_qspi *q, u8 cmd, unsigned int addr, int len)
  368. {
  369. void __iomem *base = q->iobase;
  370. int seqid;
  371. u32 reg, reg2;
  372. int err;
  373. init_completion(&q->c);
  374. dev_dbg(q->dev, "to 0x%.8x:0x%.8x, len:%d, cmd:%.2x\n",
  375. q->chip_base_addr, addr, len, cmd);
  376. /* save the reg */
  377. reg = readl(base + QUADSPI_MCR);
  378. writel(q->memmap_phy + q->chip_base_addr + addr, base + QUADSPI_SFAR);
  379. writel(QUADSPI_RBCT_WMRK_MASK | QUADSPI_RBCT_RXBRD_USEIPS,
  380. base + QUADSPI_RBCT);
  381. writel(reg | QUADSPI_MCR_CLR_RXF_MASK, base + QUADSPI_MCR);
  382. do {
  383. reg2 = readl(base + QUADSPI_SR);
  384. if (reg2 & (QUADSPI_SR_IP_ACC_MASK | QUADSPI_SR_AHB_ACC_MASK)) {
  385. udelay(1);
  386. dev_dbg(q->dev, "The controller is busy, 0x%x\n", reg2);
  387. continue;
  388. }
  389. break;
  390. } while (1);
  391. /* trigger the LUT now */
  392. seqid = fsl_qspi_get_seqid(q, cmd);
  393. writel((seqid << QUADSPI_IPCR_SEQID_SHIFT) | len, base + QUADSPI_IPCR);
  394. /* Wait for the interrupt. */
  395. if (!wait_for_completion_timeout(&q->c, msecs_to_jiffies(1000))) {
  396. dev_err(q->dev,
  397. "cmd 0x%.2x timeout, addr@%.8x, FR:0x%.8x, SR:0x%.8x\n",
  398. cmd, addr, readl(base + QUADSPI_FR),
  399. readl(base + QUADSPI_SR));
  400. err = -ETIMEDOUT;
  401. } else {
  402. err = 0;
  403. }
  404. /* restore the MCR */
  405. writel(reg, base + QUADSPI_MCR);
  406. return err;
  407. }
  408. /* Read out the data from the QUADSPI_RBDR buffer registers. */
  409. static void fsl_qspi_read_data(struct fsl_qspi *q, int len, u8 *rxbuf)
  410. {
  411. u32 tmp;
  412. int i = 0;
  413. while (len > 0) {
  414. tmp = readl(q->iobase + QUADSPI_RBDR + i * 4);
  415. tmp = fsl_qspi_endian_xchg(q, tmp);
  416. dev_dbg(q->dev, "chip addr:0x%.8x, rcv:0x%.8x\n",
  417. q->chip_base_addr, tmp);
  418. if (len >= 4) {
  419. *((u32 *)rxbuf) = tmp;
  420. rxbuf += 4;
  421. } else {
  422. memcpy(rxbuf, &tmp, len);
  423. break;
  424. }
  425. len -= 4;
  426. i++;
  427. }
  428. }
  429. /*
  430. * If we have changed the content of the flash by writing or erasing,
  431. * we need to invalidate the AHB buffer. If we do not do so, we may read out
  432. * the wrong data. The spec tells us reset the AHB domain and Serial Flash
  433. * domain at the same time.
  434. */
  435. static inline void fsl_qspi_invalid(struct fsl_qspi *q)
  436. {
  437. u32 reg;
  438. reg = readl(q->iobase + QUADSPI_MCR);
  439. reg |= QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK;
  440. writel(reg, q->iobase + QUADSPI_MCR);
  441. /*
  442. * The minimum delay : 1 AHB + 2 SFCK clocks.
  443. * Delay 1 us is enough.
  444. */
  445. udelay(1);
  446. reg &= ~(QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK);
  447. writel(reg, q->iobase + QUADSPI_MCR);
  448. }
  449. static int fsl_qspi_nor_write(struct fsl_qspi *q, struct spi_nor *nor,
  450. u8 opcode, unsigned int to, u32 *txbuf,
  451. unsigned count, size_t *retlen)
  452. {
  453. int ret, i, j;
  454. u32 tmp;
  455. dev_dbg(q->dev, "to 0x%.8x:0x%.8x, len : %d\n",
  456. q->chip_base_addr, to, count);
  457. /* clear the TX FIFO. */
  458. tmp = readl(q->iobase + QUADSPI_MCR);
  459. writel(tmp | QUADSPI_MCR_CLR_RXF_MASK, q->iobase + QUADSPI_MCR);
  460. /* fill the TX data to the FIFO */
  461. for (j = 0, i = ((count + 3) / 4); j < i; j++) {
  462. tmp = fsl_qspi_endian_xchg(q, *txbuf);
  463. writel(tmp, q->iobase + QUADSPI_TBDR);
  464. txbuf++;
  465. }
  466. /* Trigger it */
  467. ret = fsl_qspi_runcmd(q, opcode, to, count);
  468. if (ret == 0 && retlen)
  469. *retlen += count;
  470. return ret;
  471. }
  472. static void fsl_qspi_set_map_addr(struct fsl_qspi *q)
  473. {
  474. int nor_size = q->nor_size;
  475. void __iomem *base = q->iobase;
  476. writel(nor_size + q->memmap_phy, base + QUADSPI_SFA1AD);
  477. writel(nor_size * 2 + q->memmap_phy, base + QUADSPI_SFA2AD);
  478. writel(nor_size * 3 + q->memmap_phy, base + QUADSPI_SFB1AD);
  479. writel(nor_size * 4 + q->memmap_phy, base + QUADSPI_SFB2AD);
  480. }
  481. /*
  482. * There are two different ways to read out the data from the flash:
  483. * the "IP Command Read" and the "AHB Command Read".
  484. *
  485. * The IC guy suggests we use the "AHB Command Read" which is faster
  486. * then the "IP Command Read". (What's more is that there is a bug in
  487. * the "IP Command Read" in the Vybrid.)
  488. *
  489. * After we set up the registers for the "AHB Command Read", we can use
  490. * the memcpy to read the data directly. A "missed" access to the buffer
  491. * causes the controller to clear the buffer, and use the sequence pointed
  492. * by the QUADSPI_BFGENCR[SEQID] to initiate a read from the flash.
  493. */
  494. static void fsl_qspi_init_abh_read(struct fsl_qspi *q)
  495. {
  496. void __iomem *base = q->iobase;
  497. int seqid;
  498. /* AHB configuration for access buffer 0/1/2 .*/
  499. writel(QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF0CR);
  500. writel(QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF1CR);
  501. writel(QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF2CR);
  502. /*
  503. * Set ADATSZ with the maximum AHB buffer size to improve the
  504. * read performance.
  505. */
  506. writel(QUADSPI_BUF3CR_ALLMST_MASK | ((q->devtype_data->ahb_buf_size / 8)
  507. << QUADSPI_BUF3CR_ADATSZ_SHIFT), base + QUADSPI_BUF3CR);
  508. /* We only use the buffer3 */
  509. writel(0, base + QUADSPI_BUF0IND);
  510. writel(0, base + QUADSPI_BUF1IND);
  511. writel(0, base + QUADSPI_BUF2IND);
  512. /* Set the default lut sequence for AHB Read. */
  513. seqid = fsl_qspi_get_seqid(q, q->nor[0].read_opcode);
  514. writel(seqid << QUADSPI_BFGENCR_SEQID_SHIFT,
  515. q->iobase + QUADSPI_BFGENCR);
  516. }
  517. /* We use this function to do some basic init for spi_nor_scan(). */
  518. static int fsl_qspi_nor_setup(struct fsl_qspi *q)
  519. {
  520. void __iomem *base = q->iobase;
  521. u32 reg;
  522. int ret;
  523. /* the default frequency, we will change it in the future.*/
  524. ret = clk_set_rate(q->clk, 66000000);
  525. if (ret)
  526. return ret;
  527. /* Init the LUT table. */
  528. fsl_qspi_init_lut(q);
  529. /* Disable the module */
  530. writel(QUADSPI_MCR_MDIS_MASK | QUADSPI_MCR_RESERVED_MASK,
  531. base + QUADSPI_MCR);
  532. reg = readl(base + QUADSPI_SMPR);
  533. writel(reg & ~(QUADSPI_SMPR_FSDLY_MASK
  534. | QUADSPI_SMPR_FSPHS_MASK
  535. | QUADSPI_SMPR_HSENA_MASK
  536. | QUADSPI_SMPR_DDRSMP_MASK), base + QUADSPI_SMPR);
  537. /* Enable the module */
  538. writel(QUADSPI_MCR_RESERVED_MASK | QUADSPI_MCR_END_CFG_MASK,
  539. base + QUADSPI_MCR);
  540. /* enable the interrupt */
  541. writel(QUADSPI_RSER_TFIE, q->iobase + QUADSPI_RSER);
  542. return 0;
  543. }
  544. static int fsl_qspi_nor_setup_last(struct fsl_qspi *q)
  545. {
  546. unsigned long rate = q->clk_rate;
  547. int ret;
  548. if (is_imx6sx_qspi(q))
  549. rate *= 4;
  550. ret = clk_set_rate(q->clk, rate);
  551. if (ret)
  552. return ret;
  553. /* Init the LUT table again. */
  554. fsl_qspi_init_lut(q);
  555. /* Init for AHB read */
  556. fsl_qspi_init_abh_read(q);
  557. return 0;
  558. }
  559. static struct of_device_id fsl_qspi_dt_ids[] = {
  560. { .compatible = "fsl,vf610-qspi", .data = (void *)&vybrid_data, },
  561. { .compatible = "fsl,imx6sx-qspi", .data = (void *)&imx6sx_data, },
  562. { /* sentinel */ }
  563. };
  564. MODULE_DEVICE_TABLE(of, fsl_qspi_dt_ids);
  565. static void fsl_qspi_set_base_addr(struct fsl_qspi *q, struct spi_nor *nor)
  566. {
  567. q->chip_base_addr = q->nor_size * (nor - q->nor);
  568. }
  569. static int fsl_qspi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  570. {
  571. int ret;
  572. struct fsl_qspi *q = nor->priv;
  573. ret = fsl_qspi_runcmd(q, opcode, 0, len);
  574. if (ret)
  575. return ret;
  576. fsl_qspi_read_data(q, len, buf);
  577. return 0;
  578. }
  579. static int fsl_qspi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len,
  580. int write_enable)
  581. {
  582. struct fsl_qspi *q = nor->priv;
  583. int ret;
  584. if (!buf) {
  585. ret = fsl_qspi_runcmd(q, opcode, 0, 1);
  586. if (ret)
  587. return ret;
  588. if (opcode == SPINOR_OP_CHIP_ERASE)
  589. fsl_qspi_invalid(q);
  590. } else if (len > 0) {
  591. ret = fsl_qspi_nor_write(q, nor, opcode, 0,
  592. (u32 *)buf, len, NULL);
  593. } else {
  594. dev_err(q->dev, "invalid cmd %d\n", opcode);
  595. ret = -EINVAL;
  596. }
  597. return ret;
  598. }
  599. static void fsl_qspi_write(struct spi_nor *nor, loff_t to,
  600. size_t len, size_t *retlen, const u_char *buf)
  601. {
  602. struct fsl_qspi *q = nor->priv;
  603. fsl_qspi_nor_write(q, nor, nor->program_opcode, to,
  604. (u32 *)buf, len, retlen);
  605. /* invalid the data in the AHB buffer. */
  606. fsl_qspi_invalid(q);
  607. }
  608. static int fsl_qspi_read(struct spi_nor *nor, loff_t from,
  609. size_t len, size_t *retlen, u_char *buf)
  610. {
  611. struct fsl_qspi *q = nor->priv;
  612. u8 cmd = nor->read_opcode;
  613. dev_dbg(q->dev, "cmd [%x],read from (0x%p, 0x%.8x, 0x%.8x),len:%d\n",
  614. cmd, q->ahb_base, q->chip_base_addr, (unsigned int)from, len);
  615. /* Read out the data directly from the AHB buffer.*/
  616. memcpy(buf, q->ahb_base + q->chip_base_addr + from, len);
  617. *retlen += len;
  618. return 0;
  619. }
  620. static int fsl_qspi_erase(struct spi_nor *nor, loff_t offs)
  621. {
  622. struct fsl_qspi *q = nor->priv;
  623. int ret;
  624. dev_dbg(nor->dev, "%dKiB at 0x%08x:0x%08x\n",
  625. nor->mtd->erasesize / 1024, q->chip_base_addr, (u32)offs);
  626. ret = fsl_qspi_runcmd(q, nor->erase_opcode, offs, 0);
  627. if (ret)
  628. return ret;
  629. fsl_qspi_invalid(q);
  630. return 0;
  631. }
  632. static int fsl_qspi_prep(struct spi_nor *nor, enum spi_nor_ops ops)
  633. {
  634. struct fsl_qspi *q = nor->priv;
  635. int ret;
  636. ret = clk_enable(q->clk_en);
  637. if (ret)
  638. return ret;
  639. ret = clk_enable(q->clk);
  640. if (ret) {
  641. clk_disable(q->clk_en);
  642. return ret;
  643. }
  644. fsl_qspi_set_base_addr(q, nor);
  645. return 0;
  646. }
  647. static void fsl_qspi_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
  648. {
  649. struct fsl_qspi *q = nor->priv;
  650. clk_disable(q->clk);
  651. clk_disable(q->clk_en);
  652. }
  653. static int fsl_qspi_probe(struct platform_device *pdev)
  654. {
  655. struct device_node *np = pdev->dev.of_node;
  656. struct mtd_part_parser_data ppdata;
  657. struct device *dev = &pdev->dev;
  658. struct fsl_qspi *q;
  659. struct resource *res;
  660. struct spi_nor *nor;
  661. struct mtd_info *mtd;
  662. int ret, i = 0;
  663. const struct of_device_id *of_id =
  664. of_match_device(fsl_qspi_dt_ids, &pdev->dev);
  665. q = devm_kzalloc(dev, sizeof(*q), GFP_KERNEL);
  666. if (!q)
  667. return -ENOMEM;
  668. q->nor_num = of_get_child_count(dev->of_node);
  669. if (!q->nor_num || q->nor_num > FSL_QSPI_MAX_CHIP)
  670. return -ENODEV;
  671. /* find the resources */
  672. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "QuadSPI");
  673. q->iobase = devm_ioremap_resource(dev, res);
  674. if (IS_ERR(q->iobase))
  675. return PTR_ERR(q->iobase);
  676. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  677. "QuadSPI-memory");
  678. q->ahb_base = devm_ioremap_resource(dev, res);
  679. if (IS_ERR(q->ahb_base))
  680. return PTR_ERR(q->ahb_base);
  681. q->memmap_phy = res->start;
  682. /* find the clocks */
  683. q->clk_en = devm_clk_get(dev, "qspi_en");
  684. if (IS_ERR(q->clk_en))
  685. return PTR_ERR(q->clk_en);
  686. q->clk = devm_clk_get(dev, "qspi");
  687. if (IS_ERR(q->clk))
  688. return PTR_ERR(q->clk);
  689. ret = clk_prepare_enable(q->clk_en);
  690. if (ret) {
  691. dev_err(dev, "cannot enable the qspi_en clock: %d\n", ret);
  692. return ret;
  693. }
  694. ret = clk_prepare_enable(q->clk);
  695. if (ret) {
  696. dev_err(dev, "cannot enable the qspi clock: %d\n", ret);
  697. goto clk_failed;
  698. }
  699. /* find the irq */
  700. ret = platform_get_irq(pdev, 0);
  701. if (ret < 0) {
  702. dev_err(dev, "failed to get the irq: %d\n", ret);
  703. goto irq_failed;
  704. }
  705. ret = devm_request_irq(dev, ret,
  706. fsl_qspi_irq_handler, 0, pdev->name, q);
  707. if (ret) {
  708. dev_err(dev, "failed to request irq: %d\n", ret);
  709. goto irq_failed;
  710. }
  711. q->dev = dev;
  712. q->devtype_data = (struct fsl_qspi_devtype_data *)of_id->data;
  713. platform_set_drvdata(pdev, q);
  714. ret = fsl_qspi_nor_setup(q);
  715. if (ret)
  716. goto irq_failed;
  717. if (of_get_property(np, "fsl,qspi-has-second-chip", NULL))
  718. q->has_second_chip = true;
  719. /* iterate the subnodes. */
  720. for_each_available_child_of_node(dev->of_node, np) {
  721. char modalias[40];
  722. /* skip the holes */
  723. if (!q->has_second_chip)
  724. i *= 2;
  725. nor = &q->nor[i];
  726. mtd = &q->mtd[i];
  727. nor->mtd = mtd;
  728. nor->dev = dev;
  729. nor->priv = q;
  730. mtd->priv = nor;
  731. /* fill the hooks */
  732. nor->read_reg = fsl_qspi_read_reg;
  733. nor->write_reg = fsl_qspi_write_reg;
  734. nor->read = fsl_qspi_read;
  735. nor->write = fsl_qspi_write;
  736. nor->erase = fsl_qspi_erase;
  737. nor->prepare = fsl_qspi_prep;
  738. nor->unprepare = fsl_qspi_unprep;
  739. ret = of_modalias_node(np, modalias, sizeof(modalias));
  740. if (ret < 0)
  741. goto irq_failed;
  742. ret = of_property_read_u32(np, "spi-max-frequency",
  743. &q->clk_rate);
  744. if (ret < 0)
  745. goto irq_failed;
  746. /* set the chip address for READID */
  747. fsl_qspi_set_base_addr(q, nor);
  748. ret = spi_nor_scan(nor, modalias, SPI_NOR_QUAD);
  749. if (ret)
  750. goto irq_failed;
  751. ppdata.of_node = np;
  752. ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
  753. if (ret)
  754. goto irq_failed;
  755. /* Set the correct NOR size now. */
  756. if (q->nor_size == 0) {
  757. q->nor_size = mtd->size;
  758. /* Map the SPI NOR to accessiable address */
  759. fsl_qspi_set_map_addr(q);
  760. }
  761. /*
  762. * The TX FIFO is 64 bytes in the Vybrid, but the Page Program
  763. * may writes 265 bytes per time. The write is working in the
  764. * unit of the TX FIFO, not in the unit of the SPI NOR's page
  765. * size.
  766. *
  767. * So shrink the spi_nor->page_size if it is larger then the
  768. * TX FIFO.
  769. */
  770. if (nor->page_size > q->devtype_data->txfifo)
  771. nor->page_size = q->devtype_data->txfifo;
  772. i++;
  773. }
  774. /* finish the rest init. */
  775. ret = fsl_qspi_nor_setup_last(q);
  776. if (ret)
  777. goto last_init_failed;
  778. clk_disable(q->clk);
  779. clk_disable(q->clk_en);
  780. return 0;
  781. last_init_failed:
  782. for (i = 0; i < q->nor_num; i++) {
  783. /* skip the holes */
  784. if (!q->has_second_chip)
  785. i *= 2;
  786. mtd_device_unregister(&q->mtd[i]);
  787. }
  788. irq_failed:
  789. clk_disable_unprepare(q->clk);
  790. clk_failed:
  791. clk_disable_unprepare(q->clk_en);
  792. return ret;
  793. }
  794. static int fsl_qspi_remove(struct platform_device *pdev)
  795. {
  796. struct fsl_qspi *q = platform_get_drvdata(pdev);
  797. int i;
  798. for (i = 0; i < q->nor_num; i++) {
  799. /* skip the holes */
  800. if (!q->has_second_chip)
  801. i *= 2;
  802. mtd_device_unregister(&q->mtd[i]);
  803. }
  804. /* disable the hardware */
  805. writel(QUADSPI_MCR_MDIS_MASK, q->iobase + QUADSPI_MCR);
  806. writel(0x0, q->iobase + QUADSPI_RSER);
  807. clk_unprepare(q->clk);
  808. clk_unprepare(q->clk_en);
  809. return 0;
  810. }
  811. static int fsl_qspi_suspend(struct platform_device *pdev, pm_message_t state)
  812. {
  813. return 0;
  814. }
  815. static int fsl_qspi_resume(struct platform_device *pdev)
  816. {
  817. struct fsl_qspi *q = platform_get_drvdata(pdev);
  818. fsl_qspi_nor_setup(q);
  819. fsl_qspi_set_map_addr(q);
  820. fsl_qspi_nor_setup_last(q);
  821. return 0;
  822. }
  823. static struct platform_driver fsl_qspi_driver = {
  824. .driver = {
  825. .name = "fsl-quadspi",
  826. .bus = &platform_bus_type,
  827. .of_match_table = fsl_qspi_dt_ids,
  828. },
  829. .probe = fsl_qspi_probe,
  830. .remove = fsl_qspi_remove,
  831. .suspend = fsl_qspi_suspend,
  832. .resume = fsl_qspi_resume,
  833. };
  834. module_platform_driver(fsl_qspi_driver);
  835. MODULE_DESCRIPTION("Freescale QuadSPI Controller Driver");
  836. MODULE_AUTHOR("Freescale Semiconductor Inc.");
  837. MODULE_LICENSE("GPL v2");