fsl-quadspi.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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_QUAD_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. };
  191. struct fsl_qspi_devtype_data {
  192. enum fsl_qspi_devtype devtype;
  193. int rxfifo;
  194. int txfifo;
  195. int ahb_buf_size;
  196. int driver_data;
  197. };
  198. static struct fsl_qspi_devtype_data vybrid_data = {
  199. .devtype = FSL_QUADSPI_VYBRID,
  200. .rxfifo = 128,
  201. .txfifo = 64,
  202. .ahb_buf_size = 1024,
  203. .driver_data = QUADSPI_QUIRK_SWAP_ENDIAN,
  204. };
  205. static struct fsl_qspi_devtype_data imx6sx_data = {
  206. .devtype = FSL_QUADSPI_IMX6SX,
  207. .rxfifo = 128,
  208. .txfifo = 512,
  209. .ahb_buf_size = 1024,
  210. .driver_data = QUADSPI_QUIRK_4X_INT_CLK
  211. | QUADSPI_QUIRK_TKT245618,
  212. };
  213. static struct fsl_qspi_devtype_data imx7d_data = {
  214. .devtype = FSL_QUADSPI_IMX7D,
  215. .rxfifo = 512,
  216. .txfifo = 512,
  217. .ahb_buf_size = 1024,
  218. .driver_data = QUADSPI_QUIRK_TKT253890
  219. | QUADSPI_QUIRK_4X_INT_CLK,
  220. };
  221. static struct fsl_qspi_devtype_data imx6ul_data = {
  222. .devtype = FSL_QUADSPI_IMX6UL,
  223. .rxfifo = 128,
  224. .txfifo = 512,
  225. .ahb_buf_size = 1024,
  226. .driver_data = QUADSPI_QUIRK_TKT253890
  227. | QUADSPI_QUIRK_4X_INT_CLK,
  228. };
  229. static struct fsl_qspi_devtype_data ls1021a_data = {
  230. .devtype = FSL_QUADSPI_LS1021A,
  231. .rxfifo = 128,
  232. .txfifo = 64,
  233. .ahb_buf_size = 1024,
  234. .driver_data = 0,
  235. };
  236. #define FSL_QSPI_MAX_CHIP 4
  237. struct fsl_qspi {
  238. struct spi_nor nor[FSL_QSPI_MAX_CHIP];
  239. void __iomem *iobase;
  240. void __iomem *ahb_addr;
  241. u32 memmap_phy;
  242. u32 memmap_offs;
  243. u32 memmap_len;
  244. struct clk *clk, *clk_en;
  245. struct device *dev;
  246. struct completion c;
  247. const struct fsl_qspi_devtype_data *devtype_data;
  248. u32 nor_size;
  249. u32 nor_num;
  250. u32 clk_rate;
  251. unsigned int chip_base_addr; /* We may support two chips. */
  252. bool has_second_chip;
  253. bool big_endian;
  254. struct mutex lock;
  255. struct pm_qos_request pm_qos_req;
  256. };
  257. static inline int needs_swap_endian(struct fsl_qspi *q)
  258. {
  259. return q->devtype_data->driver_data & QUADSPI_QUIRK_SWAP_ENDIAN;
  260. }
  261. static inline int needs_4x_clock(struct fsl_qspi *q)
  262. {
  263. return q->devtype_data->driver_data & QUADSPI_QUIRK_4X_INT_CLK;
  264. }
  265. static inline int needs_fill_txfifo(struct fsl_qspi *q)
  266. {
  267. return q->devtype_data->driver_data & QUADSPI_QUIRK_TKT253890;
  268. }
  269. static inline int needs_wakeup_wait_mode(struct fsl_qspi *q)
  270. {
  271. return q->devtype_data->driver_data & QUADSPI_QUIRK_TKT245618;
  272. }
  273. /*
  274. * R/W functions for big- or little-endian registers:
  275. * The qSPI controller's endian is independent of the CPU core's endian.
  276. * So far, although the CPU core is little-endian but the qSPI have two
  277. * versions for big-endian and little-endian.
  278. */
  279. static void qspi_writel(struct fsl_qspi *q, u32 val, void __iomem *addr)
  280. {
  281. if (q->big_endian)
  282. iowrite32be(val, addr);
  283. else
  284. iowrite32(val, addr);
  285. }
  286. static u32 qspi_readl(struct fsl_qspi *q, void __iomem *addr)
  287. {
  288. if (q->big_endian)
  289. return ioread32be(addr);
  290. else
  291. return ioread32(addr);
  292. }
  293. /*
  294. * An IC bug makes us to re-arrange the 32-bit data.
  295. * The following chips, such as IMX6SLX, have fixed this bug.
  296. */
  297. static inline u32 fsl_qspi_endian_xchg(struct fsl_qspi *q, u32 a)
  298. {
  299. return needs_swap_endian(q) ? __swab32(a) : a;
  300. }
  301. static inline void fsl_qspi_unlock_lut(struct fsl_qspi *q)
  302. {
  303. qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
  304. qspi_writel(q, QUADSPI_LCKER_UNLOCK, q->iobase + QUADSPI_LCKCR);
  305. }
  306. static inline void fsl_qspi_lock_lut(struct fsl_qspi *q)
  307. {
  308. qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
  309. qspi_writel(q, QUADSPI_LCKER_LOCK, q->iobase + QUADSPI_LCKCR);
  310. }
  311. static irqreturn_t fsl_qspi_irq_handler(int irq, void *dev_id)
  312. {
  313. struct fsl_qspi *q = dev_id;
  314. u32 reg;
  315. /* clear interrupt */
  316. reg = qspi_readl(q, q->iobase + QUADSPI_FR);
  317. qspi_writel(q, reg, q->iobase + QUADSPI_FR);
  318. if (reg & QUADSPI_FR_TFF_MASK)
  319. complete(&q->c);
  320. dev_dbg(q->dev, "QUADSPI_FR : 0x%.8x:0x%.8x\n", q->chip_base_addr, reg);
  321. return IRQ_HANDLED;
  322. }
  323. static void fsl_qspi_init_lut(struct fsl_qspi *q)
  324. {
  325. void __iomem *base = q->iobase;
  326. int rxfifo = q->devtype_data->rxfifo;
  327. u32 lut_base;
  328. u8 cmd, addrlen, dummy;
  329. int i;
  330. fsl_qspi_unlock_lut(q);
  331. /* Clear all the LUT table */
  332. for (i = 0; i < QUADSPI_LUT_NUM; i++)
  333. qspi_writel(q, 0, base + QUADSPI_LUT_BASE + i * 4);
  334. /* Quad Read */
  335. lut_base = SEQID_QUAD_READ * 4;
  336. if (q->nor_size <= SZ_16M) {
  337. cmd = SPINOR_OP_READ_1_1_4;
  338. addrlen = ADDR24BIT;
  339. dummy = 8;
  340. } else {
  341. /* use the 4-byte address */
  342. cmd = SPINOR_OP_READ_1_1_4;
  343. addrlen = ADDR32BIT;
  344. dummy = 8;
  345. }
  346. qspi_writel(q, LUT0(CMD, PAD1, cmd) | LUT1(ADDR, PAD1, addrlen),
  347. base + QUADSPI_LUT(lut_base));
  348. qspi_writel(q, LUT0(DUMMY, PAD1, dummy) | LUT1(FSL_READ, PAD4, rxfifo),
  349. base + QUADSPI_LUT(lut_base + 1));
  350. /* Write enable */
  351. lut_base = SEQID_WREN * 4;
  352. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_WREN),
  353. base + QUADSPI_LUT(lut_base));
  354. /* Page Program */
  355. lut_base = SEQID_PP * 4;
  356. if (q->nor_size <= SZ_16M) {
  357. cmd = SPINOR_OP_PP;
  358. addrlen = ADDR24BIT;
  359. } else {
  360. /* use the 4-byte address */
  361. cmd = SPINOR_OP_PP;
  362. addrlen = ADDR32BIT;
  363. }
  364. qspi_writel(q, LUT0(CMD, PAD1, cmd) | LUT1(ADDR, PAD1, addrlen),
  365. base + QUADSPI_LUT(lut_base));
  366. qspi_writel(q, LUT0(FSL_WRITE, PAD1, 0),
  367. base + QUADSPI_LUT(lut_base + 1));
  368. /* Read Status */
  369. lut_base = SEQID_RDSR * 4;
  370. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_RDSR) |
  371. LUT1(FSL_READ, PAD1, 0x1),
  372. base + QUADSPI_LUT(lut_base));
  373. /* Erase a sector */
  374. lut_base = SEQID_SE * 4;
  375. cmd = q->nor[0].erase_opcode;
  376. addrlen = q->nor_size <= SZ_16M ? ADDR24BIT : ADDR32BIT;
  377. qspi_writel(q, LUT0(CMD, PAD1, cmd) | LUT1(ADDR, PAD1, addrlen),
  378. base + QUADSPI_LUT(lut_base));
  379. /* Erase the whole chip */
  380. lut_base = SEQID_CHIP_ERASE * 4;
  381. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_CHIP_ERASE),
  382. base + QUADSPI_LUT(lut_base));
  383. /* READ ID */
  384. lut_base = SEQID_RDID * 4;
  385. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_RDID) |
  386. LUT1(FSL_READ, PAD1, 0x8),
  387. base + QUADSPI_LUT(lut_base));
  388. /* Write Register */
  389. lut_base = SEQID_WRSR * 4;
  390. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_WRSR) |
  391. LUT1(FSL_WRITE, PAD1, 0x2),
  392. base + QUADSPI_LUT(lut_base));
  393. /* Read Configuration Register */
  394. lut_base = SEQID_RDCR * 4;
  395. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_RDCR) |
  396. LUT1(FSL_READ, PAD1, 0x1),
  397. base + QUADSPI_LUT(lut_base));
  398. /* Write disable */
  399. lut_base = SEQID_WRDI * 4;
  400. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_WRDI),
  401. base + QUADSPI_LUT(lut_base));
  402. /* Enter 4 Byte Mode (Micron) */
  403. lut_base = SEQID_EN4B * 4;
  404. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_EN4B),
  405. base + QUADSPI_LUT(lut_base));
  406. /* Enter 4 Byte Mode (Spansion) */
  407. lut_base = SEQID_BRWR * 4;
  408. qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_BRWR),
  409. base + QUADSPI_LUT(lut_base));
  410. fsl_qspi_lock_lut(q);
  411. }
  412. /* Get the SEQID for the command */
  413. static int fsl_qspi_get_seqid(struct fsl_qspi *q, u8 cmd)
  414. {
  415. switch (cmd) {
  416. case SPINOR_OP_READ_1_1_4:
  417. return SEQID_QUAD_READ;
  418. case SPINOR_OP_WREN:
  419. return SEQID_WREN;
  420. case SPINOR_OP_WRDI:
  421. return SEQID_WRDI;
  422. case SPINOR_OP_RDSR:
  423. return SEQID_RDSR;
  424. case SPINOR_OP_SE:
  425. return SEQID_SE;
  426. case SPINOR_OP_CHIP_ERASE:
  427. return SEQID_CHIP_ERASE;
  428. case SPINOR_OP_PP:
  429. return SEQID_PP;
  430. case SPINOR_OP_RDID:
  431. return SEQID_RDID;
  432. case SPINOR_OP_WRSR:
  433. return SEQID_WRSR;
  434. case SPINOR_OP_RDCR:
  435. return SEQID_RDCR;
  436. case SPINOR_OP_EN4B:
  437. return SEQID_EN4B;
  438. case SPINOR_OP_BRWR:
  439. return SEQID_BRWR;
  440. default:
  441. if (cmd == q->nor[0].erase_opcode)
  442. return SEQID_SE;
  443. dev_err(q->dev, "Unsupported cmd 0x%.2x\n", cmd);
  444. break;
  445. }
  446. return -EINVAL;
  447. }
  448. static int
  449. fsl_qspi_runcmd(struct fsl_qspi *q, u8 cmd, unsigned int addr, int len)
  450. {
  451. void __iomem *base = q->iobase;
  452. int seqid;
  453. u32 reg, reg2;
  454. int err;
  455. init_completion(&q->c);
  456. dev_dbg(q->dev, "to 0x%.8x:0x%.8x, len:%d, cmd:%.2x\n",
  457. q->chip_base_addr, addr, len, cmd);
  458. /* save the reg */
  459. reg = qspi_readl(q, base + QUADSPI_MCR);
  460. qspi_writel(q, q->memmap_phy + q->chip_base_addr + addr,
  461. base + QUADSPI_SFAR);
  462. qspi_writel(q, QUADSPI_RBCT_WMRK_MASK | QUADSPI_RBCT_RXBRD_USEIPS,
  463. base + QUADSPI_RBCT);
  464. qspi_writel(q, reg | QUADSPI_MCR_CLR_RXF_MASK, base + QUADSPI_MCR);
  465. do {
  466. reg2 = qspi_readl(q, base + QUADSPI_SR);
  467. if (reg2 & (QUADSPI_SR_IP_ACC_MASK | QUADSPI_SR_AHB_ACC_MASK)) {
  468. udelay(1);
  469. dev_dbg(q->dev, "The controller is busy, 0x%x\n", reg2);
  470. continue;
  471. }
  472. break;
  473. } while (1);
  474. /* trigger the LUT now */
  475. seqid = fsl_qspi_get_seqid(q, cmd);
  476. qspi_writel(q, (seqid << QUADSPI_IPCR_SEQID_SHIFT) | len,
  477. base + QUADSPI_IPCR);
  478. /* Wait for the interrupt. */
  479. if (!wait_for_completion_timeout(&q->c, msecs_to_jiffies(1000))) {
  480. dev_err(q->dev,
  481. "cmd 0x%.2x timeout, addr@%.8x, FR:0x%.8x, SR:0x%.8x\n",
  482. cmd, addr, qspi_readl(q, base + QUADSPI_FR),
  483. qspi_readl(q, base + QUADSPI_SR));
  484. err = -ETIMEDOUT;
  485. } else {
  486. err = 0;
  487. }
  488. /* restore the MCR */
  489. qspi_writel(q, reg, base + QUADSPI_MCR);
  490. return err;
  491. }
  492. /* Read out the data from the QUADSPI_RBDR buffer registers. */
  493. static void fsl_qspi_read_data(struct fsl_qspi *q, int len, u8 *rxbuf)
  494. {
  495. u32 tmp;
  496. int i = 0;
  497. while (len > 0) {
  498. tmp = qspi_readl(q, q->iobase + QUADSPI_RBDR + i * 4);
  499. tmp = fsl_qspi_endian_xchg(q, tmp);
  500. dev_dbg(q->dev, "chip addr:0x%.8x, rcv:0x%.8x\n",
  501. q->chip_base_addr, tmp);
  502. if (len >= 4) {
  503. *((u32 *)rxbuf) = tmp;
  504. rxbuf += 4;
  505. } else {
  506. memcpy(rxbuf, &tmp, len);
  507. break;
  508. }
  509. len -= 4;
  510. i++;
  511. }
  512. }
  513. /*
  514. * If we have changed the content of the flash by writing or erasing,
  515. * we need to invalidate the AHB buffer. If we do not do so, we may read out
  516. * the wrong data. The spec tells us reset the AHB domain and Serial Flash
  517. * domain at the same time.
  518. */
  519. static inline void fsl_qspi_invalid(struct fsl_qspi *q)
  520. {
  521. u32 reg;
  522. reg = qspi_readl(q, q->iobase + QUADSPI_MCR);
  523. reg |= QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK;
  524. qspi_writel(q, reg, q->iobase + QUADSPI_MCR);
  525. /*
  526. * The minimum delay : 1 AHB + 2 SFCK clocks.
  527. * Delay 1 us is enough.
  528. */
  529. udelay(1);
  530. reg &= ~(QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK);
  531. qspi_writel(q, reg, q->iobase + QUADSPI_MCR);
  532. }
  533. static ssize_t fsl_qspi_nor_write(struct fsl_qspi *q, struct spi_nor *nor,
  534. u8 opcode, unsigned int to, u32 *txbuf,
  535. unsigned count)
  536. {
  537. int ret, i, j;
  538. u32 tmp;
  539. dev_dbg(q->dev, "to 0x%.8x:0x%.8x, len : %d\n",
  540. q->chip_base_addr, to, count);
  541. /* clear the TX FIFO. */
  542. tmp = qspi_readl(q, q->iobase + QUADSPI_MCR);
  543. qspi_writel(q, tmp | QUADSPI_MCR_CLR_TXF_MASK, q->iobase + QUADSPI_MCR);
  544. /* fill the TX data to the FIFO */
  545. for (j = 0, i = ((count + 3) / 4); j < i; j++) {
  546. tmp = fsl_qspi_endian_xchg(q, *txbuf);
  547. qspi_writel(q, tmp, q->iobase + QUADSPI_TBDR);
  548. txbuf++;
  549. }
  550. /* fill the TXFIFO upto 16 bytes for i.MX7d */
  551. if (needs_fill_txfifo(q))
  552. for (; i < 4; i++)
  553. qspi_writel(q, tmp, q->iobase + QUADSPI_TBDR);
  554. /* Trigger it */
  555. ret = fsl_qspi_runcmd(q, opcode, to, count);
  556. if (ret == 0)
  557. return count;
  558. return ret;
  559. }
  560. static void fsl_qspi_set_map_addr(struct fsl_qspi *q)
  561. {
  562. int nor_size = q->nor_size;
  563. void __iomem *base = q->iobase;
  564. qspi_writel(q, nor_size + q->memmap_phy, base + QUADSPI_SFA1AD);
  565. qspi_writel(q, nor_size * 2 + q->memmap_phy, base + QUADSPI_SFA2AD);
  566. qspi_writel(q, nor_size * 3 + q->memmap_phy, base + QUADSPI_SFB1AD);
  567. qspi_writel(q, nor_size * 4 + q->memmap_phy, base + QUADSPI_SFB2AD);
  568. }
  569. /*
  570. * There are two different ways to read out the data from the flash:
  571. * the "IP Command Read" and the "AHB Command Read".
  572. *
  573. * The IC guy suggests we use the "AHB Command Read" which is faster
  574. * then the "IP Command Read". (What's more is that there is a bug in
  575. * the "IP Command Read" in the Vybrid.)
  576. *
  577. * After we set up the registers for the "AHB Command Read", we can use
  578. * the memcpy to read the data directly. A "missed" access to the buffer
  579. * causes the controller to clear the buffer, and use the sequence pointed
  580. * by the QUADSPI_BFGENCR[SEQID] to initiate a read from the flash.
  581. */
  582. static void fsl_qspi_init_abh_read(struct fsl_qspi *q)
  583. {
  584. void __iomem *base = q->iobase;
  585. int seqid;
  586. /* AHB configuration for access buffer 0/1/2 .*/
  587. qspi_writel(q, QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF0CR);
  588. qspi_writel(q, QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF1CR);
  589. qspi_writel(q, QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF2CR);
  590. /*
  591. * Set ADATSZ with the maximum AHB buffer size to improve the
  592. * read performance.
  593. */
  594. qspi_writel(q, QUADSPI_BUF3CR_ALLMST_MASK |
  595. ((q->devtype_data->ahb_buf_size / 8)
  596. << QUADSPI_BUF3CR_ADATSZ_SHIFT),
  597. base + QUADSPI_BUF3CR);
  598. /* We only use the buffer3 */
  599. qspi_writel(q, 0, base + QUADSPI_BUF0IND);
  600. qspi_writel(q, 0, base + QUADSPI_BUF1IND);
  601. qspi_writel(q, 0, base + QUADSPI_BUF2IND);
  602. /* Set the default lut sequence for AHB Read. */
  603. seqid = fsl_qspi_get_seqid(q, q->nor[0].read_opcode);
  604. qspi_writel(q, seqid << QUADSPI_BFGENCR_SEQID_SHIFT,
  605. q->iobase + QUADSPI_BFGENCR);
  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. fsl_qspi_init_abh_read(q);
  687. return 0;
  688. }
  689. static const struct of_device_id fsl_qspi_dt_ids[] = {
  690. { .compatible = "fsl,vf610-qspi", .data = (void *)&vybrid_data, },
  691. { .compatible = "fsl,imx6sx-qspi", .data = (void *)&imx6sx_data, },
  692. { .compatible = "fsl,imx7d-qspi", .data = (void *)&imx7d_data, },
  693. { .compatible = "fsl,imx6ul-qspi", .data = (void *)&imx6ul_data, },
  694. { .compatible = "fsl,ls1021a-qspi", .data = (void *)&ls1021a_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. struct device_node *np = pdev->dev.of_node;
  817. struct device *dev = &pdev->dev;
  818. struct fsl_qspi *q;
  819. struct resource *res;
  820. struct spi_nor *nor;
  821. struct mtd_info *mtd;
  822. int ret, i = 0;
  823. q = devm_kzalloc(dev, sizeof(*q), GFP_KERNEL);
  824. if (!q)
  825. return -ENOMEM;
  826. q->nor_num = of_get_child_count(dev->of_node);
  827. if (!q->nor_num || q->nor_num > FSL_QSPI_MAX_CHIP)
  828. return -ENODEV;
  829. q->dev = dev;
  830. q->devtype_data = of_device_get_match_data(dev);
  831. if (!q->devtype_data)
  832. return -ENODEV;
  833. platform_set_drvdata(pdev, q);
  834. /* find the resources */
  835. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "QuadSPI");
  836. q->iobase = devm_ioremap_resource(dev, res);
  837. if (IS_ERR(q->iobase))
  838. return PTR_ERR(q->iobase);
  839. q->big_endian = of_property_read_bool(np, "big-endian");
  840. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  841. "QuadSPI-memory");
  842. if (!devm_request_mem_region(dev, res->start, resource_size(res),
  843. res->name)) {
  844. dev_err(dev, "can't request region for resource %pR\n", res);
  845. return -EBUSY;
  846. }
  847. q->memmap_phy = res->start;
  848. /* find the clocks */
  849. q->clk_en = devm_clk_get(dev, "qspi_en");
  850. if (IS_ERR(q->clk_en))
  851. return PTR_ERR(q->clk_en);
  852. q->clk = devm_clk_get(dev, "qspi");
  853. if (IS_ERR(q->clk))
  854. return PTR_ERR(q->clk);
  855. ret = fsl_qspi_clk_prep_enable(q);
  856. if (ret) {
  857. dev_err(dev, "can not enable the clock\n");
  858. goto clk_failed;
  859. }
  860. /* find the irq */
  861. ret = platform_get_irq(pdev, 0);
  862. if (ret < 0) {
  863. dev_err(dev, "failed to get the irq: %d\n", ret);
  864. goto irq_failed;
  865. }
  866. ret = devm_request_irq(dev, ret,
  867. fsl_qspi_irq_handler, 0, pdev->name, q);
  868. if (ret) {
  869. dev_err(dev, "failed to request irq: %d\n", ret);
  870. goto irq_failed;
  871. }
  872. ret = fsl_qspi_nor_setup(q);
  873. if (ret)
  874. goto irq_failed;
  875. if (of_get_property(np, "fsl,qspi-has-second-chip", NULL))
  876. q->has_second_chip = true;
  877. mutex_init(&q->lock);
  878. /* iterate the subnodes. */
  879. for_each_available_child_of_node(dev->of_node, np) {
  880. /* skip the holes */
  881. if (!q->has_second_chip)
  882. i *= 2;
  883. nor = &q->nor[i];
  884. mtd = &nor->mtd;
  885. nor->dev = dev;
  886. spi_nor_set_flash_node(nor, np);
  887. nor->priv = q;
  888. /* fill the hooks */
  889. nor->read_reg = fsl_qspi_read_reg;
  890. nor->write_reg = fsl_qspi_write_reg;
  891. nor->read = fsl_qspi_read;
  892. nor->write = fsl_qspi_write;
  893. nor->erase = fsl_qspi_erase;
  894. nor->prepare = fsl_qspi_prep;
  895. nor->unprepare = fsl_qspi_unprep;
  896. ret = of_property_read_u32(np, "spi-max-frequency",
  897. &q->clk_rate);
  898. if (ret < 0)
  899. goto mutex_failed;
  900. /* set the chip address for READID */
  901. fsl_qspi_set_base_addr(q, nor);
  902. ret = spi_nor_scan(nor, NULL, SPI_NOR_QUAD);
  903. if (ret)
  904. goto mutex_failed;
  905. ret = mtd_device_register(mtd, NULL, 0);
  906. if (ret)
  907. goto mutex_failed;
  908. /* Set the correct NOR size now. */
  909. if (q->nor_size == 0) {
  910. q->nor_size = mtd->size;
  911. /* Map the SPI NOR to accessiable address */
  912. fsl_qspi_set_map_addr(q);
  913. }
  914. /*
  915. * The TX FIFO is 64 bytes in the Vybrid, but the Page Program
  916. * may writes 265 bytes per time. The write is working in the
  917. * unit of the TX FIFO, not in the unit of the SPI NOR's page
  918. * size.
  919. *
  920. * So shrink the spi_nor->page_size if it is larger then the
  921. * TX FIFO.
  922. */
  923. if (nor->page_size > q->devtype_data->txfifo)
  924. nor->page_size = q->devtype_data->txfifo;
  925. i++;
  926. }
  927. /* finish the rest init. */
  928. ret = fsl_qspi_nor_setup_last(q);
  929. if (ret)
  930. goto last_init_failed;
  931. fsl_qspi_clk_disable_unprep(q);
  932. return 0;
  933. last_init_failed:
  934. for (i = 0; i < q->nor_num; i++) {
  935. /* skip the holes */
  936. if (!q->has_second_chip)
  937. i *= 2;
  938. mtd_device_unregister(&q->nor[i].mtd);
  939. }
  940. mutex_failed:
  941. mutex_destroy(&q->lock);
  942. irq_failed:
  943. fsl_qspi_clk_disable_unprep(q);
  944. clk_failed:
  945. dev_err(dev, "Freescale QuadSPI probe failed\n");
  946. return ret;
  947. }
  948. static int fsl_qspi_remove(struct platform_device *pdev)
  949. {
  950. struct fsl_qspi *q = platform_get_drvdata(pdev);
  951. int i;
  952. for (i = 0; i < q->nor_num; i++) {
  953. /* skip the holes */
  954. if (!q->has_second_chip)
  955. i *= 2;
  956. mtd_device_unregister(&q->nor[i].mtd);
  957. }
  958. /* disable the hardware */
  959. qspi_writel(q, QUADSPI_MCR_MDIS_MASK, q->iobase + QUADSPI_MCR);
  960. qspi_writel(q, 0x0, q->iobase + QUADSPI_RSER);
  961. mutex_destroy(&q->lock);
  962. if (q->ahb_addr)
  963. iounmap(q->ahb_addr);
  964. return 0;
  965. }
  966. static int fsl_qspi_suspend(struct platform_device *pdev, pm_message_t state)
  967. {
  968. return 0;
  969. }
  970. static int fsl_qspi_resume(struct platform_device *pdev)
  971. {
  972. int ret;
  973. struct fsl_qspi *q = platform_get_drvdata(pdev);
  974. ret = fsl_qspi_clk_prep_enable(q);
  975. if (ret)
  976. return ret;
  977. fsl_qspi_nor_setup(q);
  978. fsl_qspi_set_map_addr(q);
  979. fsl_qspi_nor_setup_last(q);
  980. fsl_qspi_clk_disable_unprep(q);
  981. return 0;
  982. }
  983. static struct platform_driver fsl_qspi_driver = {
  984. .driver = {
  985. .name = "fsl-quadspi",
  986. .bus = &platform_bus_type,
  987. .of_match_table = fsl_qspi_dt_ids,
  988. },
  989. .probe = fsl_qspi_probe,
  990. .remove = fsl_qspi_remove,
  991. .suspend = fsl_qspi_suspend,
  992. .resume = fsl_qspi_resume,
  993. };
  994. module_platform_driver(fsl_qspi_driver);
  995. MODULE_DESCRIPTION("Freescale QuadSPI Controller Driver");
  996. MODULE_AUTHOR("Freescale Semiconductor Inc.");
  997. MODULE_LICENSE("GPL v2");