fsl-quadspi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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. #include <linux/mutex.h>
  30. #include <linux/pm_qos.h>
  31. #include <linux/sizes.h>
  32. /* Controller needs driver to swap endian */
  33. #define QUADSPI_QUIRK_SWAP_ENDIAN (1 << 0)
  34. /* Controller needs 4x internal clock */
  35. #define QUADSPI_QUIRK_4X_INT_CLK (1 << 1)
  36. /*
  37. * TKT253890, Controller needs driver to fill txfifo till 16 byte to
  38. * trigger data transfer even though extern data will not transferred.
  39. */
  40. #define QUADSPI_QUIRK_TKT253890 (1 << 2)
  41. /* Controller cannot wake up from wait mode, TKT245618 */
  42. #define QUADSPI_QUIRK_TKT245618 (1 << 3)
  43. /* The registers */
  44. #define QUADSPI_MCR 0x00
  45. #define QUADSPI_MCR_RESERVED_SHIFT 16
  46. #define QUADSPI_MCR_RESERVED_MASK (0xF << QUADSPI_MCR_RESERVED_SHIFT)
  47. #define QUADSPI_MCR_MDIS_SHIFT 14
  48. #define QUADSPI_MCR_MDIS_MASK (1 << QUADSPI_MCR_MDIS_SHIFT)
  49. #define QUADSPI_MCR_CLR_TXF_SHIFT 11
  50. #define QUADSPI_MCR_CLR_TXF_MASK (1 << QUADSPI_MCR_CLR_TXF_SHIFT)
  51. #define QUADSPI_MCR_CLR_RXF_SHIFT 10
  52. #define QUADSPI_MCR_CLR_RXF_MASK (1 << QUADSPI_MCR_CLR_RXF_SHIFT)
  53. #define QUADSPI_MCR_DDR_EN_SHIFT 7
  54. #define QUADSPI_MCR_DDR_EN_MASK (1 << QUADSPI_MCR_DDR_EN_SHIFT)
  55. #define QUADSPI_MCR_END_CFG_SHIFT 2
  56. #define QUADSPI_MCR_END_CFG_MASK (3 << QUADSPI_MCR_END_CFG_SHIFT)
  57. #define QUADSPI_MCR_SWRSTHD_SHIFT 1
  58. #define QUADSPI_MCR_SWRSTHD_MASK (1 << QUADSPI_MCR_SWRSTHD_SHIFT)
  59. #define QUADSPI_MCR_SWRSTSD_SHIFT 0
  60. #define QUADSPI_MCR_SWRSTSD_MASK (1 << QUADSPI_MCR_SWRSTSD_SHIFT)
  61. #define QUADSPI_IPCR 0x08
  62. #define QUADSPI_IPCR_SEQID_SHIFT 24
  63. #define QUADSPI_IPCR_SEQID_MASK (0xF << QUADSPI_IPCR_SEQID_SHIFT)
  64. #define QUADSPI_BUF0CR 0x10
  65. #define QUADSPI_BUF1CR 0x14
  66. #define QUADSPI_BUF2CR 0x18
  67. #define QUADSPI_BUFXCR_INVALID_MSTRID 0xe
  68. #define QUADSPI_BUF3CR 0x1c
  69. #define QUADSPI_BUF3CR_ALLMST_SHIFT 31
  70. #define QUADSPI_BUF3CR_ALLMST_MASK (1 << QUADSPI_BUF3CR_ALLMST_SHIFT)
  71. #define QUADSPI_BUF3CR_ADATSZ_SHIFT 8
  72. #define QUADSPI_BUF3CR_ADATSZ_MASK (0xFF << QUADSPI_BUF3CR_ADATSZ_SHIFT)
  73. #define QUADSPI_BFGENCR 0x20
  74. #define QUADSPI_BFGENCR_PAR_EN_SHIFT 16
  75. #define QUADSPI_BFGENCR_PAR_EN_MASK (1 << (QUADSPI_BFGENCR_PAR_EN_SHIFT))
  76. #define QUADSPI_BFGENCR_SEQID_SHIFT 12
  77. #define QUADSPI_BFGENCR_SEQID_MASK (0xF << QUADSPI_BFGENCR_SEQID_SHIFT)
  78. #define QUADSPI_BUF0IND 0x30
  79. #define QUADSPI_BUF1IND 0x34
  80. #define QUADSPI_BUF2IND 0x38
  81. #define QUADSPI_SFAR 0x100
  82. #define QUADSPI_SMPR 0x108
  83. #define QUADSPI_SMPR_DDRSMP_SHIFT 16
  84. #define QUADSPI_SMPR_DDRSMP_MASK (7 << QUADSPI_SMPR_DDRSMP_SHIFT)
  85. #define QUADSPI_SMPR_FSDLY_SHIFT 6
  86. #define QUADSPI_SMPR_FSDLY_MASK (1 << QUADSPI_SMPR_FSDLY_SHIFT)
  87. #define QUADSPI_SMPR_FSPHS_SHIFT 5
  88. #define QUADSPI_SMPR_FSPHS_MASK (1 << QUADSPI_SMPR_FSPHS_SHIFT)
  89. #define QUADSPI_SMPR_HSENA_SHIFT 0
  90. #define QUADSPI_SMPR_HSENA_MASK (1 << QUADSPI_SMPR_HSENA_SHIFT)
  91. #define QUADSPI_RBSR 0x10c
  92. #define QUADSPI_RBSR_RDBFL_SHIFT 8
  93. #define QUADSPI_RBSR_RDBFL_MASK (0x3F << QUADSPI_RBSR_RDBFL_SHIFT)
  94. #define QUADSPI_RBCT 0x110
  95. #define QUADSPI_RBCT_WMRK_MASK 0x1F
  96. #define QUADSPI_RBCT_RXBRD_SHIFT 8
  97. #define QUADSPI_RBCT_RXBRD_USEIPS (0x1 << QUADSPI_RBCT_RXBRD_SHIFT)
  98. #define QUADSPI_TBSR 0x150
  99. #define QUADSPI_TBDR 0x154
  100. #define QUADSPI_SR 0x15c
  101. #define QUADSPI_SR_IP_ACC_SHIFT 1
  102. #define QUADSPI_SR_IP_ACC_MASK (0x1 << QUADSPI_SR_IP_ACC_SHIFT)
  103. #define QUADSPI_SR_AHB_ACC_SHIFT 2
  104. #define QUADSPI_SR_AHB_ACC_MASK (0x1 << QUADSPI_SR_AHB_ACC_SHIFT)
  105. #define QUADSPI_FR 0x160
  106. #define QUADSPI_FR_TFF_MASK 0x1
  107. #define QUADSPI_SFA1AD 0x180
  108. #define QUADSPI_SFA2AD 0x184
  109. #define QUADSPI_SFB1AD 0x188
  110. #define QUADSPI_SFB2AD 0x18c
  111. #define QUADSPI_RBDR 0x200
  112. #define QUADSPI_LUTKEY 0x300
  113. #define QUADSPI_LUTKEY_VALUE 0x5AF05AF0
  114. #define QUADSPI_LCKCR 0x304
  115. #define QUADSPI_LCKER_LOCK 0x1
  116. #define QUADSPI_LCKER_UNLOCK 0x2
  117. #define QUADSPI_RSER 0x164
  118. #define QUADSPI_RSER_TFIE (0x1 << 0)
  119. #define QUADSPI_LUT_BASE 0x310
  120. /*
  121. * The definition of the LUT register shows below:
  122. *
  123. * ---------------------------------------------------
  124. * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 |
  125. * ---------------------------------------------------
  126. */
  127. #define OPRND0_SHIFT 0
  128. #define PAD0_SHIFT 8
  129. #define INSTR0_SHIFT 10
  130. #define OPRND1_SHIFT 16
  131. /* Instruction set for the LUT register. */
  132. #define LUT_STOP 0
  133. #define LUT_CMD 1
  134. #define LUT_ADDR 2
  135. #define LUT_DUMMY 3
  136. #define LUT_MODE 4
  137. #define LUT_MODE2 5
  138. #define LUT_MODE4 6
  139. #define LUT_FSL_READ 7
  140. #define LUT_FSL_WRITE 8
  141. #define LUT_JMP_ON_CS 9
  142. #define LUT_ADDR_DDR 10
  143. #define LUT_MODE_DDR 11
  144. #define LUT_MODE2_DDR 12
  145. #define LUT_MODE4_DDR 13
  146. #define LUT_FSL_READ_DDR 14
  147. #define LUT_FSL_WRITE_DDR 15
  148. #define LUT_DATA_LEARN 16
  149. /*
  150. * The PAD definitions for LUT register.
  151. *
  152. * The pad stands for the lines number of IO[0:3].
  153. * For example, the Quad read need four IO lines, so you should
  154. * set LUT_PAD4 which means we use four IO lines.
  155. */
  156. #define LUT_PAD1 0
  157. #define LUT_PAD2 1
  158. #define LUT_PAD4 2
  159. /* Oprands for the LUT register. */
  160. #define ADDR24BIT 0x18
  161. #define ADDR32BIT 0x20
  162. /* Macros for constructing the LUT register. */
  163. #define LUT0(ins, pad, opr) \
  164. (((opr) << OPRND0_SHIFT) | ((LUT_##pad) << PAD0_SHIFT) | \
  165. ((LUT_##ins) << INSTR0_SHIFT))
  166. #define LUT1(ins, pad, opr) (LUT0(ins, pad, opr) << OPRND1_SHIFT)
  167. /* other macros for LUT register. */
  168. #define QUADSPI_LUT(x) (QUADSPI_LUT_BASE + (x) * 4)
  169. #define QUADSPI_LUT_NUM 64
  170. /* SEQID -- we can have 16 seqids at most. */
  171. #define SEQID_READ 0
  172. #define SEQID_WREN 1
  173. #define SEQID_WRDI 2
  174. #define SEQID_RDSR 3
  175. #define SEQID_SE 4
  176. #define SEQID_CHIP_ERASE 5
  177. #define SEQID_PP 6
  178. #define SEQID_RDID 7
  179. #define SEQID_WRSR 8
  180. #define SEQID_RDCR 9
  181. #define SEQID_EN4B 10
  182. #define SEQID_BRWR 11
  183. #define QUADSPI_MIN_IOMAP SZ_4M
  184. enum fsl_qspi_devtype {
  185. FSL_QUADSPI_VYBRID,
  186. FSL_QUADSPI_IMX6SX,
  187. FSL_QUADSPI_IMX7D,
  188. FSL_QUADSPI_IMX6UL,
  189. FSL_QUADSPI_LS1021A,
  190. FSL_QUADSPI_LS2080A,
  191. };
  192. struct fsl_qspi_devtype_data {
  193. enum fsl_qspi_devtype devtype;
  194. int rxfifo;
  195. int txfifo;
  196. int ahb_buf_size;
  197. int driver_data;
  198. };
  199. static const struct fsl_qspi_devtype_data vybrid_data = {
  200. .devtype = FSL_QUADSPI_VYBRID,
  201. .rxfifo = 128,
  202. .txfifo = 64,
  203. .ahb_buf_size = 1024,
  204. .driver_data = QUADSPI_QUIRK_SWAP_ENDIAN,
  205. };
  206. static const struct fsl_qspi_devtype_data imx6sx_data = {
  207. .devtype = FSL_QUADSPI_IMX6SX,
  208. .rxfifo = 128,
  209. .txfifo = 512,
  210. .ahb_buf_size = 1024,
  211. .driver_data = QUADSPI_QUIRK_4X_INT_CLK
  212. | QUADSPI_QUIRK_TKT245618,
  213. };
  214. static const struct fsl_qspi_devtype_data imx7d_data = {
  215. .devtype = FSL_QUADSPI_IMX7D,
  216. .rxfifo = 512,
  217. .txfifo = 512,
  218. .ahb_buf_size = 1024,
  219. .driver_data = QUADSPI_QUIRK_TKT253890
  220. | QUADSPI_QUIRK_4X_INT_CLK,
  221. };
  222. static const struct fsl_qspi_devtype_data imx6ul_data = {
  223. .devtype = FSL_QUADSPI_IMX6UL,
  224. .rxfifo = 128,
  225. .txfifo = 512,
  226. .ahb_buf_size = 1024,
  227. .driver_data = QUADSPI_QUIRK_TKT253890
  228. | QUADSPI_QUIRK_4X_INT_CLK,
  229. };
  230. static struct fsl_qspi_devtype_data ls1021a_data = {
  231. .devtype = FSL_QUADSPI_LS1021A,
  232. .rxfifo = 128,
  233. .txfifo = 64,
  234. .ahb_buf_size = 1024,
  235. .driver_data = 0,
  236. };
  237. static const struct fsl_qspi_devtype_data ls2080a_data = {
  238. .devtype = FSL_QUADSPI_LS2080A,
  239. .rxfifo = 128,
  240. .txfifo = 64,
  241. .ahb_buf_size = 1024,
  242. .driver_data = QUADSPI_QUIRK_TKT253890,
  243. };
  244. #define FSL_QSPI_MAX_CHIP 4
  245. struct fsl_qspi {
  246. struct spi_nor nor[FSL_QSPI_MAX_CHIP];
  247. void __iomem *iobase;
  248. void __iomem *ahb_addr;
  249. u32 memmap_phy;
  250. u32 memmap_offs;
  251. u32 memmap_len;
  252. struct clk *clk, *clk_en;
  253. struct device *dev;
  254. struct completion c;
  255. const struct fsl_qspi_devtype_data *devtype_data;
  256. u32 nor_size;
  257. u32 nor_num;
  258. u32 clk_rate;
  259. unsigned int chip_base_addr; /* We may support two chips. */
  260. bool has_second_chip;
  261. bool big_endian;
  262. struct mutex lock;
  263. struct pm_qos_request pm_qos_req;
  264. };
  265. static inline int needs_swap_endian(struct fsl_qspi *q)
  266. {
  267. return q->devtype_data->driver_data & QUADSPI_QUIRK_SWAP_ENDIAN;
  268. }
  269. static inline int needs_4x_clock(struct fsl_qspi *q)
  270. {
  271. return q->devtype_data->driver_data & QUADSPI_QUIRK_4X_INT_CLK;
  272. }
  273. static inline int needs_fill_txfifo(struct fsl_qspi *q)
  274. {
  275. return q->devtype_data->driver_data & QUADSPI_QUIRK_TKT253890;
  276. }
  277. static inline int needs_wakeup_wait_mode(struct fsl_qspi *q)
  278. {
  279. return q->devtype_data->driver_data & QUADSPI_QUIRK_TKT245618;
  280. }
  281. /*
  282. * R/W functions for big- or little-endian registers:
  283. * The qSPI controller's endian is independent of the CPU core's endian.
  284. * So far, although the CPU core is little-endian but the qSPI have two
  285. * versions for big-endian and little-endian.
  286. */
  287. static void qspi_writel(struct fsl_qspi *q, u32 val, void __iomem *addr)
  288. {
  289. if (q->big_endian)
  290. iowrite32be(val, addr);
  291. else
  292. iowrite32(val, addr);
  293. }
  294. static u32 qspi_readl(struct fsl_qspi *q, void __iomem *addr)
  295. {
  296. if (q->big_endian)
  297. return ioread32be(addr);
  298. else
  299. return ioread32(addr);
  300. }
  301. /*
  302. * An IC bug makes us to re-arrange the 32-bit data.
  303. * The following chips, such as IMX6SLX, have fixed this bug.
  304. */
  305. static inline u32 fsl_qspi_endian_xchg(struct fsl_qspi *q, u32 a)
  306. {
  307. return needs_swap_endian(q) ? __swab32(a) : a;
  308. }
  309. static inline void fsl_qspi_unlock_lut(struct fsl_qspi *q)
  310. {
  311. qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
  312. qspi_writel(q, QUADSPI_LCKER_UNLOCK, q->iobase + QUADSPI_LCKCR);
  313. }
  314. static inline void fsl_qspi_lock_lut(struct fsl_qspi *q)
  315. {
  316. qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
  317. qspi_writel(q, QUADSPI_LCKER_LOCK, q->iobase + QUADSPI_LCKCR);
  318. }
  319. static irqreturn_t fsl_qspi_irq_handler(int irq, void *dev_id)
  320. {
  321. struct fsl_qspi *q = dev_id;
  322. u32 reg;
  323. /* clear interrupt */
  324. reg = qspi_readl(q, q->iobase + QUADSPI_FR);
  325. qspi_writel(q, reg, q->iobase + QUADSPI_FR);
  326. if (reg & QUADSPI_FR_TFF_MASK)
  327. complete(&q->c);
  328. dev_dbg(q->dev, "QUADSPI_FR : 0x%.8x:0x%.8x\n", q->chip_base_addr, reg);
  329. return IRQ_HANDLED;
  330. }
  331. static void fsl_qspi_init_lut(struct fsl_qspi *q)
  332. {
  333. void __iomem *base = q->iobase;
  334. int rxfifo = q->devtype_data->rxfifo;
  335. u32 lut_base;
  336. int i;
  337. struct spi_nor *nor = &q->nor[0];
  338. u8 addrlen = (nor->addr_width == 3) ? ADDR24BIT : ADDR32BIT;
  339. u8 read_op = nor->read_opcode;
  340. u8 read_dm = nor->read_dummy;
  341. fsl_qspi_unlock_lut(q);
  342. /* Clear all the LUT table */
  343. for (i = 0; i < QUADSPI_LUT_NUM; i++)
  344. qspi_writel(q, 0, base + QUADSPI_LUT_BASE + i * 4);
  345. /* Read */
  346. lut_base = SEQID_READ * 4;
  347. qspi_writel(q, LUT0(CMD, PAD1, read_op) | LUT1(ADDR, PAD1, addrlen),
  348. base + QUADSPI_LUT(lut_base));
  349. qspi_writel(q, LUT0(DUMMY, PAD1, read_dm) |
  350. LUT1(FSL_READ, PAD4, rxfifo),
  351. base + QUADSPI_LUT(lut_base + 1));
  352. /* Write enable */
  353. lut_base = SEQID_WREN * 4;
  354. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_WREN),
  355. base + QUADSPI_LUT(lut_base));
  356. /* Page Program */
  357. lut_base = SEQID_PP * 4;
  358. qspi_writel(q, LUT0(CMD, PAD1, nor->program_opcode) |
  359. LUT1(ADDR, PAD1, addrlen),
  360. base + QUADSPI_LUT(lut_base));
  361. qspi_writel(q, LUT0(FSL_WRITE, PAD1, 0),
  362. base + QUADSPI_LUT(lut_base + 1));
  363. /* Read Status */
  364. lut_base = SEQID_RDSR * 4;
  365. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_RDSR) |
  366. LUT1(FSL_READ, PAD1, 0x1),
  367. base + QUADSPI_LUT(lut_base));
  368. /* Erase a sector */
  369. lut_base = SEQID_SE * 4;
  370. qspi_writel(q, LUT0(CMD, PAD1, nor->erase_opcode) |
  371. LUT1(ADDR, PAD1, addrlen),
  372. base + QUADSPI_LUT(lut_base));
  373. /* Erase the whole chip */
  374. lut_base = SEQID_CHIP_ERASE * 4;
  375. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_CHIP_ERASE),
  376. base + QUADSPI_LUT(lut_base));
  377. /* READ ID */
  378. lut_base = SEQID_RDID * 4;
  379. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_RDID) |
  380. LUT1(FSL_READ, PAD1, 0x8),
  381. base + QUADSPI_LUT(lut_base));
  382. /* Write Register */
  383. lut_base = SEQID_WRSR * 4;
  384. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_WRSR) |
  385. LUT1(FSL_WRITE, PAD1, 0x2),
  386. base + QUADSPI_LUT(lut_base));
  387. /* Read Configuration Register */
  388. lut_base = SEQID_RDCR * 4;
  389. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_RDCR) |
  390. LUT1(FSL_READ, PAD1, 0x1),
  391. base + QUADSPI_LUT(lut_base));
  392. /* Write disable */
  393. lut_base = SEQID_WRDI * 4;
  394. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_WRDI),
  395. base + QUADSPI_LUT(lut_base));
  396. /* Enter 4 Byte Mode (Micron) */
  397. lut_base = SEQID_EN4B * 4;
  398. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_EN4B),
  399. base + QUADSPI_LUT(lut_base));
  400. /* Enter 4 Byte Mode (Spansion) */
  401. lut_base = SEQID_BRWR * 4;
  402. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_BRWR),
  403. base + QUADSPI_LUT(lut_base));
  404. fsl_qspi_lock_lut(q);
  405. }
  406. /* Get the SEQID for the command */
  407. static int fsl_qspi_get_seqid(struct fsl_qspi *q, u8 cmd)
  408. {
  409. switch (cmd) {
  410. case SPINOR_OP_READ_1_1_4:
  411. case SPINOR_OP_READ_1_1_4_4B:
  412. return SEQID_READ;
  413. case SPINOR_OP_WREN:
  414. return SEQID_WREN;
  415. case SPINOR_OP_WRDI:
  416. return SEQID_WRDI;
  417. case SPINOR_OP_RDSR:
  418. return SEQID_RDSR;
  419. case SPINOR_OP_SE:
  420. return SEQID_SE;
  421. case SPINOR_OP_CHIP_ERASE:
  422. return SEQID_CHIP_ERASE;
  423. case SPINOR_OP_PP:
  424. return SEQID_PP;
  425. case SPINOR_OP_RDID:
  426. return SEQID_RDID;
  427. case SPINOR_OP_WRSR:
  428. return SEQID_WRSR;
  429. case SPINOR_OP_RDCR:
  430. return SEQID_RDCR;
  431. case SPINOR_OP_EN4B:
  432. return SEQID_EN4B;
  433. case SPINOR_OP_BRWR:
  434. return SEQID_BRWR;
  435. default:
  436. if (cmd == q->nor[0].erase_opcode)
  437. return SEQID_SE;
  438. dev_err(q->dev, "Unsupported cmd 0x%.2x\n", cmd);
  439. break;
  440. }
  441. return -EINVAL;
  442. }
  443. static int
  444. fsl_qspi_runcmd(struct fsl_qspi *q, u8 cmd, unsigned int addr, int len)
  445. {
  446. void __iomem *base = q->iobase;
  447. int seqid;
  448. u32 reg, reg2;
  449. int err;
  450. init_completion(&q->c);
  451. dev_dbg(q->dev, "to 0x%.8x:0x%.8x, len:%d, cmd:%.2x\n",
  452. q->chip_base_addr, addr, len, cmd);
  453. /* save the reg */
  454. reg = qspi_readl(q, base + QUADSPI_MCR);
  455. qspi_writel(q, q->memmap_phy + q->chip_base_addr + addr,
  456. base + QUADSPI_SFAR);
  457. qspi_writel(q, QUADSPI_RBCT_WMRK_MASK | QUADSPI_RBCT_RXBRD_USEIPS,
  458. base + QUADSPI_RBCT);
  459. qspi_writel(q, reg | QUADSPI_MCR_CLR_RXF_MASK, base + QUADSPI_MCR);
  460. do {
  461. reg2 = qspi_readl(q, base + QUADSPI_SR);
  462. if (reg2 & (QUADSPI_SR_IP_ACC_MASK | QUADSPI_SR_AHB_ACC_MASK)) {
  463. udelay(1);
  464. dev_dbg(q->dev, "The controller is busy, 0x%x\n", reg2);
  465. continue;
  466. }
  467. break;
  468. } while (1);
  469. /* trigger the LUT now */
  470. seqid = fsl_qspi_get_seqid(q, cmd);
  471. if (seqid < 0)
  472. return seqid;
  473. qspi_writel(q, (seqid << QUADSPI_IPCR_SEQID_SHIFT) | len,
  474. base + QUADSPI_IPCR);
  475. /* Wait for the interrupt. */
  476. if (!wait_for_completion_timeout(&q->c, msecs_to_jiffies(1000))) {
  477. dev_err(q->dev,
  478. "cmd 0x%.2x timeout, addr@%.8x, FR:0x%.8x, SR:0x%.8x\n",
  479. cmd, addr, qspi_readl(q, base + QUADSPI_FR),
  480. qspi_readl(q, base + QUADSPI_SR));
  481. err = -ETIMEDOUT;
  482. } else {
  483. err = 0;
  484. }
  485. /* restore the MCR */
  486. qspi_writel(q, reg, base + QUADSPI_MCR);
  487. return err;
  488. }
  489. /* Read out the data from the QUADSPI_RBDR buffer registers. */
  490. static void fsl_qspi_read_data(struct fsl_qspi *q, int len, u8 *rxbuf)
  491. {
  492. u32 tmp;
  493. int i = 0;
  494. while (len > 0) {
  495. tmp = qspi_readl(q, q->iobase + QUADSPI_RBDR + i * 4);
  496. tmp = fsl_qspi_endian_xchg(q, tmp);
  497. dev_dbg(q->dev, "chip addr:0x%.8x, rcv:0x%.8x\n",
  498. q->chip_base_addr, tmp);
  499. if (len >= 4) {
  500. *((u32 *)rxbuf) = tmp;
  501. rxbuf += 4;
  502. } else {
  503. memcpy(rxbuf, &tmp, len);
  504. break;
  505. }
  506. len -= 4;
  507. i++;
  508. }
  509. }
  510. /*
  511. * If we have changed the content of the flash by writing or erasing,
  512. * we need to invalidate the AHB buffer. If we do not do so, we may read out
  513. * the wrong data. The spec tells us reset the AHB domain and Serial Flash
  514. * domain at the same time.
  515. */
  516. static inline void fsl_qspi_invalid(struct fsl_qspi *q)
  517. {
  518. u32 reg;
  519. reg = qspi_readl(q, q->iobase + QUADSPI_MCR);
  520. reg |= QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK;
  521. qspi_writel(q, reg, q->iobase + QUADSPI_MCR);
  522. /*
  523. * The minimum delay : 1 AHB + 2 SFCK clocks.
  524. * Delay 1 us is enough.
  525. */
  526. udelay(1);
  527. reg &= ~(QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK);
  528. qspi_writel(q, reg, q->iobase + QUADSPI_MCR);
  529. }
  530. static ssize_t fsl_qspi_nor_write(struct fsl_qspi *q, struct spi_nor *nor,
  531. u8 opcode, unsigned int to, u32 *txbuf,
  532. unsigned count)
  533. {
  534. int ret, i, j;
  535. u32 tmp;
  536. dev_dbg(q->dev, "to 0x%.8x:0x%.8x, len : %d\n",
  537. q->chip_base_addr, to, count);
  538. /* clear the TX FIFO. */
  539. tmp = qspi_readl(q, q->iobase + QUADSPI_MCR);
  540. qspi_writel(q, tmp | QUADSPI_MCR_CLR_TXF_MASK, q->iobase + QUADSPI_MCR);
  541. /* fill the TX data to the FIFO */
  542. for (j = 0, i = ((count + 3) / 4); j < i; j++) {
  543. tmp = fsl_qspi_endian_xchg(q, *txbuf);
  544. qspi_writel(q, tmp, q->iobase + QUADSPI_TBDR);
  545. txbuf++;
  546. }
  547. /* fill the TXFIFO upto 16 bytes for i.MX7d */
  548. if (needs_fill_txfifo(q))
  549. for (; i < 4; i++)
  550. qspi_writel(q, tmp, q->iobase + QUADSPI_TBDR);
  551. /* Trigger it */
  552. ret = fsl_qspi_runcmd(q, opcode, to, count);
  553. if (ret == 0)
  554. return count;
  555. return ret;
  556. }
  557. static void fsl_qspi_set_map_addr(struct fsl_qspi *q)
  558. {
  559. int nor_size = q->nor_size;
  560. void __iomem *base = q->iobase;
  561. qspi_writel(q, nor_size + q->memmap_phy, base + QUADSPI_SFA1AD);
  562. qspi_writel(q, nor_size * 2 + q->memmap_phy, base + QUADSPI_SFA2AD);
  563. qspi_writel(q, nor_size * 3 + q->memmap_phy, base + QUADSPI_SFB1AD);
  564. qspi_writel(q, nor_size * 4 + q->memmap_phy, base + QUADSPI_SFB2AD);
  565. }
  566. /*
  567. * There are two different ways to read out the data from the flash:
  568. * the "IP Command Read" and the "AHB Command Read".
  569. *
  570. * The IC guy suggests we use the "AHB Command Read" which is faster
  571. * then the "IP Command Read". (What's more is that there is a bug in
  572. * the "IP Command Read" in the Vybrid.)
  573. *
  574. * After we set up the registers for the "AHB Command Read", we can use
  575. * the memcpy to read the data directly. A "missed" access to the buffer
  576. * causes the controller to clear the buffer, and use the sequence pointed
  577. * by the QUADSPI_BFGENCR[SEQID] to initiate a read from the flash.
  578. */
  579. static int fsl_qspi_init_ahb_read(struct fsl_qspi *q)
  580. {
  581. void __iomem *base = q->iobase;
  582. int seqid;
  583. /* AHB configuration for access buffer 0/1/2 .*/
  584. qspi_writel(q, QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF0CR);
  585. qspi_writel(q, QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF1CR);
  586. qspi_writel(q, QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF2CR);
  587. /*
  588. * Set ADATSZ with the maximum AHB buffer size to improve the
  589. * read performance.
  590. */
  591. qspi_writel(q, QUADSPI_BUF3CR_ALLMST_MASK |
  592. ((q->devtype_data->ahb_buf_size / 8)
  593. << QUADSPI_BUF3CR_ADATSZ_SHIFT),
  594. base + QUADSPI_BUF3CR);
  595. /* We only use the buffer3 */
  596. qspi_writel(q, 0, base + QUADSPI_BUF0IND);
  597. qspi_writel(q, 0, base + QUADSPI_BUF1IND);
  598. qspi_writel(q, 0, base + QUADSPI_BUF2IND);
  599. /* Set the default lut sequence for AHB Read. */
  600. seqid = fsl_qspi_get_seqid(q, q->nor[0].read_opcode);
  601. if (seqid < 0)
  602. return seqid;
  603. qspi_writel(q, seqid << QUADSPI_BFGENCR_SEQID_SHIFT,
  604. q->iobase + QUADSPI_BFGENCR);
  605. return 0;
  606. }
  607. /* This function was used to prepare and enable QSPI clock */
  608. static int fsl_qspi_clk_prep_enable(struct fsl_qspi *q)
  609. {
  610. int ret;
  611. ret = clk_prepare_enable(q->clk_en);
  612. if (ret)
  613. return ret;
  614. ret = clk_prepare_enable(q->clk);
  615. if (ret) {
  616. clk_disable_unprepare(q->clk_en);
  617. return ret;
  618. }
  619. if (needs_wakeup_wait_mode(q))
  620. pm_qos_add_request(&q->pm_qos_req, PM_QOS_CPU_DMA_LATENCY, 0);
  621. return 0;
  622. }
  623. /* This function was used to disable and unprepare QSPI clock */
  624. static void fsl_qspi_clk_disable_unprep(struct fsl_qspi *q)
  625. {
  626. if (needs_wakeup_wait_mode(q))
  627. pm_qos_remove_request(&q->pm_qos_req);
  628. clk_disable_unprepare(q->clk);
  629. clk_disable_unprepare(q->clk_en);
  630. }
  631. /* We use this function to do some basic init for spi_nor_scan(). */
  632. static int fsl_qspi_nor_setup(struct fsl_qspi *q)
  633. {
  634. void __iomem *base = q->iobase;
  635. u32 reg;
  636. int ret;
  637. /* disable and unprepare clock to avoid glitch pass to controller */
  638. fsl_qspi_clk_disable_unprep(q);
  639. /* the default frequency, we will change it in the future. */
  640. ret = clk_set_rate(q->clk, 66000000);
  641. if (ret)
  642. return ret;
  643. ret = fsl_qspi_clk_prep_enable(q);
  644. if (ret)
  645. return ret;
  646. /* Reset the module */
  647. qspi_writel(q, QUADSPI_MCR_SWRSTSD_MASK | QUADSPI_MCR_SWRSTHD_MASK,
  648. base + QUADSPI_MCR);
  649. udelay(1);
  650. /* Init the LUT table. */
  651. fsl_qspi_init_lut(q);
  652. /* Disable the module */
  653. qspi_writel(q, QUADSPI_MCR_MDIS_MASK | QUADSPI_MCR_RESERVED_MASK,
  654. base + QUADSPI_MCR);
  655. reg = qspi_readl(q, base + QUADSPI_SMPR);
  656. qspi_writel(q, reg & ~(QUADSPI_SMPR_FSDLY_MASK
  657. | QUADSPI_SMPR_FSPHS_MASK
  658. | QUADSPI_SMPR_HSENA_MASK
  659. | QUADSPI_SMPR_DDRSMP_MASK), base + QUADSPI_SMPR);
  660. /* Enable the module */
  661. qspi_writel(q, QUADSPI_MCR_RESERVED_MASK | QUADSPI_MCR_END_CFG_MASK,
  662. base + QUADSPI_MCR);
  663. /* clear all interrupt status */
  664. qspi_writel(q, 0xffffffff, q->iobase + QUADSPI_FR);
  665. /* enable the interrupt */
  666. qspi_writel(q, QUADSPI_RSER_TFIE, q->iobase + QUADSPI_RSER);
  667. return 0;
  668. }
  669. static int fsl_qspi_nor_setup_last(struct fsl_qspi *q)
  670. {
  671. unsigned long rate = q->clk_rate;
  672. int ret;
  673. if (needs_4x_clock(q))
  674. rate *= 4;
  675. /* disable and unprepare clock to avoid glitch pass to controller */
  676. fsl_qspi_clk_disable_unprep(q);
  677. ret = clk_set_rate(q->clk, rate);
  678. if (ret)
  679. return ret;
  680. ret = fsl_qspi_clk_prep_enable(q);
  681. if (ret)
  682. return ret;
  683. /* Init the LUT table again. */
  684. fsl_qspi_init_lut(q);
  685. /* Init for AHB read */
  686. return fsl_qspi_init_ahb_read(q);
  687. }
  688. static const struct of_device_id fsl_qspi_dt_ids[] = {
  689. { .compatible = "fsl,vf610-qspi", .data = &vybrid_data, },
  690. { .compatible = "fsl,imx6sx-qspi", .data = &imx6sx_data, },
  691. { .compatible = "fsl,imx7d-qspi", .data = &imx7d_data, },
  692. { .compatible = "fsl,imx6ul-qspi", .data = &imx6ul_data, },
  693. { .compatible = "fsl,ls1021a-qspi", .data = (void *)&ls1021a_data, },
  694. { .compatible = "fsl,ls2080a-qspi", .data = &ls2080a_data, },
  695. { /* sentinel */ }
  696. };
  697. MODULE_DEVICE_TABLE(of, fsl_qspi_dt_ids);
  698. static void fsl_qspi_set_base_addr(struct fsl_qspi *q, struct spi_nor *nor)
  699. {
  700. q->chip_base_addr = q->nor_size * (nor - q->nor);
  701. }
  702. static int fsl_qspi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  703. {
  704. int ret;
  705. struct fsl_qspi *q = nor->priv;
  706. ret = fsl_qspi_runcmd(q, opcode, 0, len);
  707. if (ret)
  708. return ret;
  709. fsl_qspi_read_data(q, len, buf);
  710. return 0;
  711. }
  712. static int fsl_qspi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  713. {
  714. struct fsl_qspi *q = nor->priv;
  715. int ret;
  716. if (!buf) {
  717. ret = fsl_qspi_runcmd(q, opcode, 0, 1);
  718. if (ret)
  719. return ret;
  720. if (opcode == SPINOR_OP_CHIP_ERASE)
  721. fsl_qspi_invalid(q);
  722. } else if (len > 0) {
  723. ret = fsl_qspi_nor_write(q, nor, opcode, 0,
  724. (u32 *)buf, len);
  725. if (ret > 0)
  726. return 0;
  727. } else {
  728. dev_err(q->dev, "invalid cmd %d\n", opcode);
  729. ret = -EINVAL;
  730. }
  731. return ret;
  732. }
  733. static ssize_t fsl_qspi_write(struct spi_nor *nor, loff_t to,
  734. size_t len, const u_char *buf)
  735. {
  736. struct fsl_qspi *q = nor->priv;
  737. ssize_t ret = fsl_qspi_nor_write(q, nor, nor->program_opcode, to,
  738. (u32 *)buf, len);
  739. /* invalid the data in the AHB buffer. */
  740. fsl_qspi_invalid(q);
  741. return ret;
  742. }
  743. static ssize_t fsl_qspi_read(struct spi_nor *nor, loff_t from,
  744. size_t len, u_char *buf)
  745. {
  746. struct fsl_qspi *q = nor->priv;
  747. u8 cmd = nor->read_opcode;
  748. /* if necessary,ioremap buffer before AHB read, */
  749. if (!q->ahb_addr) {
  750. q->memmap_offs = q->chip_base_addr + from;
  751. q->memmap_len = len > QUADSPI_MIN_IOMAP ? len : QUADSPI_MIN_IOMAP;
  752. q->ahb_addr = ioremap_nocache(
  753. q->memmap_phy + q->memmap_offs,
  754. q->memmap_len);
  755. if (!q->ahb_addr) {
  756. dev_err(q->dev, "ioremap failed\n");
  757. return -ENOMEM;
  758. }
  759. /* ioremap if the data requested is out of range */
  760. } else if (q->chip_base_addr + from < q->memmap_offs
  761. || q->chip_base_addr + from + len >
  762. q->memmap_offs + q->memmap_len) {
  763. iounmap(q->ahb_addr);
  764. q->memmap_offs = q->chip_base_addr + from;
  765. q->memmap_len = len > QUADSPI_MIN_IOMAP ? len : QUADSPI_MIN_IOMAP;
  766. q->ahb_addr = ioremap_nocache(
  767. q->memmap_phy + q->memmap_offs,
  768. q->memmap_len);
  769. if (!q->ahb_addr) {
  770. dev_err(q->dev, "ioremap failed\n");
  771. return -ENOMEM;
  772. }
  773. }
  774. dev_dbg(q->dev, "cmd [%x],read from %p, len:%zd\n",
  775. cmd, q->ahb_addr + q->chip_base_addr + from - q->memmap_offs,
  776. len);
  777. /* Read out the data directly from the AHB buffer.*/
  778. memcpy(buf, q->ahb_addr + q->chip_base_addr + from - q->memmap_offs,
  779. len);
  780. return len;
  781. }
  782. static int fsl_qspi_erase(struct spi_nor *nor, loff_t offs)
  783. {
  784. struct fsl_qspi *q = nor->priv;
  785. int ret;
  786. dev_dbg(nor->dev, "%dKiB at 0x%08x:0x%08x\n",
  787. nor->mtd.erasesize / 1024, q->chip_base_addr, (u32)offs);
  788. ret = fsl_qspi_runcmd(q, nor->erase_opcode, offs, 0);
  789. if (ret)
  790. return ret;
  791. fsl_qspi_invalid(q);
  792. return 0;
  793. }
  794. static int fsl_qspi_prep(struct spi_nor *nor, enum spi_nor_ops ops)
  795. {
  796. struct fsl_qspi *q = nor->priv;
  797. int ret;
  798. mutex_lock(&q->lock);
  799. ret = fsl_qspi_clk_prep_enable(q);
  800. if (ret)
  801. goto err_mutex;
  802. fsl_qspi_set_base_addr(q, nor);
  803. return 0;
  804. err_mutex:
  805. mutex_unlock(&q->lock);
  806. return ret;
  807. }
  808. static void fsl_qspi_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
  809. {
  810. struct fsl_qspi *q = nor->priv;
  811. fsl_qspi_clk_disable_unprep(q);
  812. mutex_unlock(&q->lock);
  813. }
  814. static int fsl_qspi_probe(struct platform_device *pdev)
  815. {
  816. const struct spi_nor_hwcaps hwcaps = {
  817. .mask = SNOR_HWCAPS_READ_1_1_4 |
  818. SNOR_HWCAPS_PP,
  819. };
  820. struct device_node *np = pdev->dev.of_node;
  821. struct device *dev = &pdev->dev;
  822. struct fsl_qspi *q;
  823. struct resource *res;
  824. struct spi_nor *nor;
  825. struct mtd_info *mtd;
  826. int ret, i = 0;
  827. q = devm_kzalloc(dev, sizeof(*q), GFP_KERNEL);
  828. if (!q)
  829. return -ENOMEM;
  830. q->nor_num = of_get_child_count(dev->of_node);
  831. if (!q->nor_num || q->nor_num > FSL_QSPI_MAX_CHIP)
  832. return -ENODEV;
  833. q->dev = dev;
  834. q->devtype_data = of_device_get_match_data(dev);
  835. if (!q->devtype_data)
  836. return -ENODEV;
  837. platform_set_drvdata(pdev, q);
  838. /* find the resources */
  839. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "QuadSPI");
  840. q->iobase = devm_ioremap_resource(dev, res);
  841. if (IS_ERR(q->iobase))
  842. return PTR_ERR(q->iobase);
  843. q->big_endian = of_property_read_bool(np, "big-endian");
  844. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  845. "QuadSPI-memory");
  846. if (!devm_request_mem_region(dev, res->start, resource_size(res),
  847. res->name)) {
  848. dev_err(dev, "can't request region for resource %pR\n", res);
  849. return -EBUSY;
  850. }
  851. q->memmap_phy = res->start;
  852. /* find the clocks */
  853. q->clk_en = devm_clk_get(dev, "qspi_en");
  854. if (IS_ERR(q->clk_en))
  855. return PTR_ERR(q->clk_en);
  856. q->clk = devm_clk_get(dev, "qspi");
  857. if (IS_ERR(q->clk))
  858. return PTR_ERR(q->clk);
  859. ret = fsl_qspi_clk_prep_enable(q);
  860. if (ret) {
  861. dev_err(dev, "can not enable the clock\n");
  862. goto clk_failed;
  863. }
  864. /* find the irq */
  865. ret = platform_get_irq(pdev, 0);
  866. if (ret < 0) {
  867. dev_err(dev, "failed to get the irq: %d\n", ret);
  868. goto irq_failed;
  869. }
  870. ret = devm_request_irq(dev, ret,
  871. fsl_qspi_irq_handler, 0, pdev->name, q);
  872. if (ret) {
  873. dev_err(dev, "failed to request irq: %d\n", ret);
  874. goto irq_failed;
  875. }
  876. ret = fsl_qspi_nor_setup(q);
  877. if (ret)
  878. goto irq_failed;
  879. if (of_get_property(np, "fsl,qspi-has-second-chip", NULL))
  880. q->has_second_chip = true;
  881. mutex_init(&q->lock);
  882. /* iterate the subnodes. */
  883. for_each_available_child_of_node(dev->of_node, np) {
  884. /* skip the holes */
  885. if (!q->has_second_chip)
  886. i *= 2;
  887. nor = &q->nor[i];
  888. mtd = &nor->mtd;
  889. nor->dev = dev;
  890. spi_nor_set_flash_node(nor, np);
  891. nor->priv = q;
  892. if (q->nor_num > 1 && !mtd->name) {
  893. int spiflash_idx;
  894. ret = of_property_read_u32(np, "reg", &spiflash_idx);
  895. if (!ret) {
  896. mtd->name = devm_kasprintf(dev, GFP_KERNEL,
  897. "%s-%d",
  898. dev_name(dev),
  899. spiflash_idx);
  900. if (!mtd->name) {
  901. ret = -ENOMEM;
  902. goto mutex_failed;
  903. }
  904. } else {
  905. dev_warn(dev, "reg property is missing\n");
  906. }
  907. }
  908. /* fill the hooks */
  909. nor->read_reg = fsl_qspi_read_reg;
  910. nor->write_reg = fsl_qspi_write_reg;
  911. nor->read = fsl_qspi_read;
  912. nor->write = fsl_qspi_write;
  913. nor->erase = fsl_qspi_erase;
  914. nor->prepare = fsl_qspi_prep;
  915. nor->unprepare = fsl_qspi_unprep;
  916. ret = of_property_read_u32(np, "spi-max-frequency",
  917. &q->clk_rate);
  918. if (ret < 0)
  919. goto mutex_failed;
  920. /* set the chip address for READID */
  921. fsl_qspi_set_base_addr(q, nor);
  922. ret = spi_nor_scan(nor, NULL, &hwcaps);
  923. if (ret)
  924. goto mutex_failed;
  925. ret = mtd_device_register(mtd, NULL, 0);
  926. if (ret)
  927. goto mutex_failed;
  928. /* Set the correct NOR size now. */
  929. if (q->nor_size == 0) {
  930. q->nor_size = mtd->size;
  931. /* Map the SPI NOR to accessiable address */
  932. fsl_qspi_set_map_addr(q);
  933. }
  934. /*
  935. * The TX FIFO is 64 bytes in the Vybrid, but the Page Program
  936. * may writes 265 bytes per time. The write is working in the
  937. * unit of the TX FIFO, not in the unit of the SPI NOR's page
  938. * size.
  939. *
  940. * So shrink the spi_nor->page_size if it is larger then the
  941. * TX FIFO.
  942. */
  943. if (nor->page_size > q->devtype_data->txfifo)
  944. nor->page_size = q->devtype_data->txfifo;
  945. i++;
  946. }
  947. /* finish the rest init. */
  948. ret = fsl_qspi_nor_setup_last(q);
  949. if (ret)
  950. goto last_init_failed;
  951. fsl_qspi_clk_disable_unprep(q);
  952. return 0;
  953. last_init_failed:
  954. for (i = 0; i < q->nor_num; i++) {
  955. /* skip the holes */
  956. if (!q->has_second_chip)
  957. i *= 2;
  958. mtd_device_unregister(&q->nor[i].mtd);
  959. }
  960. mutex_failed:
  961. mutex_destroy(&q->lock);
  962. irq_failed:
  963. fsl_qspi_clk_disable_unprep(q);
  964. clk_failed:
  965. dev_err(dev, "Freescale QuadSPI probe failed\n");
  966. return ret;
  967. }
  968. static int fsl_qspi_remove(struct platform_device *pdev)
  969. {
  970. struct fsl_qspi *q = platform_get_drvdata(pdev);
  971. int i;
  972. for (i = 0; i < q->nor_num; i++) {
  973. /* skip the holes */
  974. if (!q->has_second_chip)
  975. i *= 2;
  976. mtd_device_unregister(&q->nor[i].mtd);
  977. }
  978. /* disable the hardware */
  979. qspi_writel(q, QUADSPI_MCR_MDIS_MASK, q->iobase + QUADSPI_MCR);
  980. qspi_writel(q, 0x0, q->iobase + QUADSPI_RSER);
  981. mutex_destroy(&q->lock);
  982. if (q->ahb_addr)
  983. iounmap(q->ahb_addr);
  984. return 0;
  985. }
  986. static int fsl_qspi_suspend(struct platform_device *pdev, pm_message_t state)
  987. {
  988. return 0;
  989. }
  990. static int fsl_qspi_resume(struct platform_device *pdev)
  991. {
  992. int ret;
  993. struct fsl_qspi *q = platform_get_drvdata(pdev);
  994. ret = fsl_qspi_clk_prep_enable(q);
  995. if (ret)
  996. return ret;
  997. fsl_qspi_nor_setup(q);
  998. fsl_qspi_set_map_addr(q);
  999. fsl_qspi_nor_setup_last(q);
  1000. fsl_qspi_clk_disable_unprep(q);
  1001. return 0;
  1002. }
  1003. static struct platform_driver fsl_qspi_driver = {
  1004. .driver = {
  1005. .name = "fsl-quadspi",
  1006. .of_match_table = fsl_qspi_dt_ids,
  1007. },
  1008. .probe = fsl_qspi_probe,
  1009. .remove = fsl_qspi_remove,
  1010. .suspend = fsl_qspi_suspend,
  1011. .resume = fsl_qspi_resume,
  1012. };
  1013. module_platform_driver(fsl_qspi_driver);
  1014. MODULE_DESCRIPTION("Freescale QuadSPI Controller Driver");
  1015. MODULE_AUTHOR("Freescale Semiconductor Inc.");
  1016. MODULE_LICENSE("GPL v2");