nxp_fspi.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * NXP FlexSPI(FSPI) controller driver.
  4. *
  5. * Copyright (c) 2019 Michael Walle <michael@walle.cc>
  6. * Copyright (c) 2019 NXP
  7. *
  8. * This driver was originally ported from the linux kernel v5.4-rc3, which had
  9. * the following notes:
  10. *
  11. * FlexSPI is a flexsible SPI host controller which supports two SPI
  12. * channels and up to 4 external devices. Each channel supports
  13. * Single/Dual/Quad/Octal mode data transfer (1/2/4/8 bidirectional
  14. * data lines).
  15. *
  16. * FlexSPI controller is driven by the LUT(Look-up Table) registers
  17. * LUT registers are a look-up-table for sequences of instructions.
  18. * A valid sequence consists of four LUT registers.
  19. * Maximum 32 LUT sequences can be programmed simultaneously.
  20. *
  21. * LUTs are being created at run-time based on the commands passed
  22. * from the spi-mem framework, thus using single LUT index.
  23. *
  24. * Software triggered Flash read/write access by IP Bus.
  25. *
  26. * Memory mapped read access by AHB Bus.
  27. *
  28. * Based on SPI MEM interface and spi-fsl-qspi.c driver.
  29. *
  30. * Author:
  31. * Yogesh Narayan Gaur <yogeshnarayan.gaur@nxp.com>
  32. * Boris Brezillon <bbrezillon@kernel.org>
  33. * Frieder Schrempf <frieder.schrempf@kontron.de>
  34. */
  35. #include <common.h>
  36. #include <clk.h>
  37. #include <dm.h>
  38. #include <dm/device_compat.h>
  39. #include <malloc.h>
  40. #include <spi.h>
  41. #include <spi-mem.h>
  42. #include <asm/io.h>
  43. #ifdef CONFIG_FSL_LAYERSCAPE
  44. #include <asm/arch/clock.h>
  45. #include <asm/arch/soc.h>
  46. #include <asm/arch/speed.h>
  47. #endif
  48. #include <linux/bitops.h>
  49. #include <linux/kernel.h>
  50. #include <linux/sizes.h>
  51. #include <linux/iopoll.h>
  52. #include <linux/bug.h>
  53. #include <linux/err.h>
  54. /*
  55. * The driver only uses one single LUT entry, that is updated on
  56. * each call of exec_op(). Index 0 is preset at boot with a basic
  57. * read operation, so let's use the last entry (31).
  58. */
  59. #define SEQID_LUT 31
  60. /* Registers used by the driver */
  61. #define FSPI_MCR0 0x00
  62. #define FSPI_MCR0_AHB_TIMEOUT(x) ((x) << 24)
  63. #define FSPI_MCR0_IP_TIMEOUT(x) ((x) << 16)
  64. #define FSPI_MCR0_LEARN_EN BIT(15)
  65. #define FSPI_MCR0_SCRFRUN_EN BIT(14)
  66. #define FSPI_MCR0_OCTCOMB_EN BIT(13)
  67. #define FSPI_MCR0_DOZE_EN BIT(12)
  68. #define FSPI_MCR0_HSEN BIT(11)
  69. #define FSPI_MCR0_SERCLKDIV BIT(8)
  70. #define FSPI_MCR0_ATDF_EN BIT(7)
  71. #define FSPI_MCR0_ARDF_EN BIT(6)
  72. #define FSPI_MCR0_RXCLKSRC(x) ((x) << 4)
  73. #define FSPI_MCR0_END_CFG(x) ((x) << 2)
  74. #define FSPI_MCR0_MDIS BIT(1)
  75. #define FSPI_MCR0_SWRST BIT(0)
  76. #define FSPI_MCR1 0x04
  77. #define FSPI_MCR1_SEQ_TIMEOUT(x) ((x) << 16)
  78. #define FSPI_MCR1_AHB_TIMEOUT(x) (x)
  79. #define FSPI_MCR2 0x08
  80. #define FSPI_MCR2_IDLE_WAIT(x) ((x) << 24)
  81. #define FSPI_MCR2_SAMEDEVICEEN BIT(15)
  82. #define FSPI_MCR2_CLRLRPHS BIT(14)
  83. #define FSPI_MCR2_ABRDATSZ BIT(8)
  84. #define FSPI_MCR2_ABRLEARN BIT(7)
  85. #define FSPI_MCR2_ABR_READ BIT(6)
  86. #define FSPI_MCR2_ABRWRITE BIT(5)
  87. #define FSPI_MCR2_ABRDUMMY BIT(4)
  88. #define FSPI_MCR2_ABR_MODE BIT(3)
  89. #define FSPI_MCR2_ABRCADDR BIT(2)
  90. #define FSPI_MCR2_ABRRADDR BIT(1)
  91. #define FSPI_MCR2_ABR_CMD BIT(0)
  92. #define FSPI_AHBCR 0x0c
  93. #define FSPI_AHBCR_RDADDROPT BIT(6)
  94. #define FSPI_AHBCR_PREF_EN BIT(5)
  95. #define FSPI_AHBCR_BUFF_EN BIT(4)
  96. #define FSPI_AHBCR_CACH_EN BIT(3)
  97. #define FSPI_AHBCR_CLRTXBUF BIT(2)
  98. #define FSPI_AHBCR_CLRRXBUF BIT(1)
  99. #define FSPI_AHBCR_PAR_EN BIT(0)
  100. #define FSPI_INTEN 0x10
  101. #define FSPI_INTEN_SCLKSBWR BIT(9)
  102. #define FSPI_INTEN_SCLKSBRD BIT(8)
  103. #define FSPI_INTEN_DATALRNFL BIT(7)
  104. #define FSPI_INTEN_IPTXWE BIT(6)
  105. #define FSPI_INTEN_IPRXWA BIT(5)
  106. #define FSPI_INTEN_AHBCMDERR BIT(4)
  107. #define FSPI_INTEN_IPCMDERR BIT(3)
  108. #define FSPI_INTEN_AHBCMDGE BIT(2)
  109. #define FSPI_INTEN_IPCMDGE BIT(1)
  110. #define FSPI_INTEN_IPCMDDONE BIT(0)
  111. #define FSPI_INTR 0x14
  112. #define FSPI_INTR_SCLKSBWR BIT(9)
  113. #define FSPI_INTR_SCLKSBRD BIT(8)
  114. #define FSPI_INTR_DATALRNFL BIT(7)
  115. #define FSPI_INTR_IPTXWE BIT(6)
  116. #define FSPI_INTR_IPRXWA BIT(5)
  117. #define FSPI_INTR_AHBCMDERR BIT(4)
  118. #define FSPI_INTR_IPCMDERR BIT(3)
  119. #define FSPI_INTR_AHBCMDGE BIT(2)
  120. #define FSPI_INTR_IPCMDGE BIT(1)
  121. #define FSPI_INTR_IPCMDDONE BIT(0)
  122. #define FSPI_LUTKEY 0x18
  123. #define FSPI_LUTKEY_VALUE 0x5AF05AF0
  124. #define FSPI_LCKCR 0x1C
  125. #define FSPI_LCKER_LOCK 0x1
  126. #define FSPI_LCKER_UNLOCK 0x2
  127. #define FSPI_BUFXCR_INVALID_MSTRID 0xE
  128. #define FSPI_AHBRX_BUF0CR0 0x20
  129. #define FSPI_AHBRX_BUF1CR0 0x24
  130. #define FSPI_AHBRX_BUF2CR0 0x28
  131. #define FSPI_AHBRX_BUF3CR0 0x2C
  132. #define FSPI_AHBRX_BUF4CR0 0x30
  133. #define FSPI_AHBRX_BUF5CR0 0x34
  134. #define FSPI_AHBRX_BUF6CR0 0x38
  135. #define FSPI_AHBRX_BUF7CR0 0x3C
  136. #define FSPI_AHBRXBUF0CR7_PREF BIT(31)
  137. #define FSPI_AHBRX_BUF0CR1 0x40
  138. #define FSPI_AHBRX_BUF1CR1 0x44
  139. #define FSPI_AHBRX_BUF2CR1 0x48
  140. #define FSPI_AHBRX_BUF3CR1 0x4C
  141. #define FSPI_AHBRX_BUF4CR1 0x50
  142. #define FSPI_AHBRX_BUF5CR1 0x54
  143. #define FSPI_AHBRX_BUF6CR1 0x58
  144. #define FSPI_AHBRX_BUF7CR1 0x5C
  145. #define FSPI_FLSHA1CR0 0x60
  146. #define FSPI_FLSHA2CR0 0x64
  147. #define FSPI_FLSHB1CR0 0x68
  148. #define FSPI_FLSHB2CR0 0x6C
  149. #define FSPI_FLSHXCR0_SZ_KB 10
  150. #define FSPI_FLSHXCR0_SZ(x) ((x) >> FSPI_FLSHXCR0_SZ_KB)
  151. #define FSPI_FLSHA1CR1 0x70
  152. #define FSPI_FLSHA2CR1 0x74
  153. #define FSPI_FLSHB1CR1 0x78
  154. #define FSPI_FLSHB2CR1 0x7C
  155. #define FSPI_FLSHXCR1_CSINTR(x) ((x) << 16)
  156. #define FSPI_FLSHXCR1_CAS(x) ((x) << 11)
  157. #define FSPI_FLSHXCR1_WA BIT(10)
  158. #define FSPI_FLSHXCR1_TCSH(x) ((x) << 5)
  159. #define FSPI_FLSHXCR1_TCSS(x) (x)
  160. #define FSPI_FLSHA1CR2 0x80
  161. #define FSPI_FLSHA2CR2 0x84
  162. #define FSPI_FLSHB1CR2 0x88
  163. #define FSPI_FLSHB2CR2 0x8C
  164. #define FSPI_FLSHXCR2_CLRINSP BIT(24)
  165. #define FSPI_FLSHXCR2_AWRWAIT BIT(16)
  166. #define FSPI_FLSHXCR2_AWRSEQN_SHIFT 13
  167. #define FSPI_FLSHXCR2_AWRSEQI_SHIFT 8
  168. #define FSPI_FLSHXCR2_ARDSEQN_SHIFT 5
  169. #define FSPI_FLSHXCR2_ARDSEQI_SHIFT 0
  170. #define FSPI_IPCR0 0xA0
  171. #define FSPI_IPCR1 0xA4
  172. #define FSPI_IPCR1_IPAREN BIT(31)
  173. #define FSPI_IPCR1_SEQNUM_SHIFT 24
  174. #define FSPI_IPCR1_SEQID_SHIFT 16
  175. #define FSPI_IPCR1_IDATSZ(x) (x)
  176. #define FSPI_IPCMD 0xB0
  177. #define FSPI_IPCMD_TRG BIT(0)
  178. #define FSPI_DLPR 0xB4
  179. #define FSPI_IPRXFCR 0xB8
  180. #define FSPI_IPRXFCR_CLR BIT(0)
  181. #define FSPI_IPRXFCR_DMA_EN BIT(1)
  182. #define FSPI_IPRXFCR_WMRK(x) ((x) << 2)
  183. #define FSPI_IPTXFCR 0xBC
  184. #define FSPI_IPTXFCR_CLR BIT(0)
  185. #define FSPI_IPTXFCR_DMA_EN BIT(1)
  186. #define FSPI_IPTXFCR_WMRK(x) ((x) << 2)
  187. #define FSPI_DLLACR 0xC0
  188. #define FSPI_DLLACR_OVRDEN BIT(8)
  189. #define FSPI_DLLBCR 0xC4
  190. #define FSPI_DLLBCR_OVRDEN BIT(8)
  191. #define FSPI_STS0 0xE0
  192. #define FSPI_STS0_DLPHB(x) ((x) << 8)
  193. #define FSPI_STS0_DLPHA(x) ((x) << 4)
  194. #define FSPI_STS0_CMD_SRC(x) ((x) << 2)
  195. #define FSPI_STS0_ARB_IDLE BIT(1)
  196. #define FSPI_STS0_SEQ_IDLE BIT(0)
  197. #define FSPI_STS1 0xE4
  198. #define FSPI_STS1_IP_ERRCD(x) ((x) << 24)
  199. #define FSPI_STS1_IP_ERRID(x) ((x) << 16)
  200. #define FSPI_STS1_AHB_ERRCD(x) ((x) << 8)
  201. #define FSPI_STS1_AHB_ERRID(x) (x)
  202. #define FSPI_AHBSPNST 0xEC
  203. #define FSPI_AHBSPNST_DATLFT(x) ((x) << 16)
  204. #define FSPI_AHBSPNST_BUFID(x) ((x) << 1)
  205. #define FSPI_AHBSPNST_ACTIVE BIT(0)
  206. #define FSPI_IPRXFSTS 0xF0
  207. #define FSPI_IPRXFSTS_RDCNTR(x) ((x) << 16)
  208. #define FSPI_IPRXFSTS_FILL(x) (x)
  209. #define FSPI_IPTXFSTS 0xF4
  210. #define FSPI_IPTXFSTS_WRCNTR(x) ((x) << 16)
  211. #define FSPI_IPTXFSTS_FILL(x) (x)
  212. #define FSPI_RFDR 0x100
  213. #define FSPI_TFDR 0x180
  214. #define FSPI_LUT_BASE 0x200
  215. #define FSPI_LUT_OFFSET (SEQID_LUT * 4 * 4)
  216. #define FSPI_LUT_REG(idx) \
  217. (FSPI_LUT_BASE + FSPI_LUT_OFFSET + (idx) * 4)
  218. /* register map end */
  219. /* Instruction set for the LUT register. */
  220. #define LUT_STOP 0x00
  221. #define LUT_CMD 0x01
  222. #define LUT_ADDR 0x02
  223. #define LUT_CADDR_SDR 0x03
  224. #define LUT_MODE 0x04
  225. #define LUT_MODE2 0x05
  226. #define LUT_MODE4 0x06
  227. #define LUT_MODE8 0x07
  228. #define LUT_NXP_WRITE 0x08
  229. #define LUT_NXP_READ 0x09
  230. #define LUT_LEARN_SDR 0x0A
  231. #define LUT_DATSZ_SDR 0x0B
  232. #define LUT_DUMMY 0x0C
  233. #define LUT_DUMMY_RWDS_SDR 0x0D
  234. #define LUT_JMP_ON_CS 0x1F
  235. #define LUT_CMD_DDR 0x21
  236. #define LUT_ADDR_DDR 0x22
  237. #define LUT_CADDR_DDR 0x23
  238. #define LUT_MODE_DDR 0x24
  239. #define LUT_MODE2_DDR 0x25
  240. #define LUT_MODE4_DDR 0x26
  241. #define LUT_MODE8_DDR 0x27
  242. #define LUT_WRITE_DDR 0x28
  243. #define LUT_READ_DDR 0x29
  244. #define LUT_LEARN_DDR 0x2A
  245. #define LUT_DATSZ_DDR 0x2B
  246. #define LUT_DUMMY_DDR 0x2C
  247. #define LUT_DUMMY_RWDS_DDR 0x2D
  248. /*
  249. * Calculate number of required PAD bits for LUT register.
  250. *
  251. * The pad stands for the number of IO lines [0:7].
  252. * For example, the octal read needs eight IO lines,
  253. * so you should use LUT_PAD(8). This macro
  254. * returns 3 i.e. use eight (2^3) IP lines for read.
  255. */
  256. #define LUT_PAD(x) (fls(x) - 1)
  257. /*
  258. * Macro for constructing the LUT entries with the following
  259. * register layout:
  260. *
  261. * ---------------------------------------------------
  262. * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 |
  263. * ---------------------------------------------------
  264. */
  265. #define PAD_SHIFT 8
  266. #define INSTR_SHIFT 10
  267. #define OPRND_SHIFT 16
  268. /* Macros for constructing the LUT register. */
  269. #define LUT_DEF(idx, ins, pad, opr) \
  270. ((((ins) << INSTR_SHIFT) | ((pad) << PAD_SHIFT) | \
  271. (opr)) << (((idx) % 2) * OPRND_SHIFT))
  272. #define POLL_TOUT 5000
  273. #define NXP_FSPI_MAX_CHIPSELECT 4
  274. /* Access flash memory using IP bus only */
  275. #define FSPI_QUIRK_USE_IP_ONLY BIT(0)
  276. struct nxp_fspi_devtype_data {
  277. unsigned int rxfifo;
  278. unsigned int txfifo;
  279. unsigned int ahb_buf_size;
  280. unsigned int quirks;
  281. bool little_endian;
  282. };
  283. static struct nxp_fspi_devtype_data lx2160a_data = {
  284. .rxfifo = SZ_512, /* (64 * 64 bits) */
  285. .txfifo = SZ_1K, /* (128 * 64 bits) */
  286. .ahb_buf_size = SZ_2K, /* (256 * 64 bits) */
  287. .quirks = 0,
  288. .little_endian = true, /* little-endian */
  289. };
  290. static struct nxp_fspi_devtype_data imx8mm_data = {
  291. .rxfifo = SZ_512, /* (64 * 64 bits) */
  292. .txfifo = SZ_1K, /* (128 * 64 bits) */
  293. .ahb_buf_size = SZ_2K, /* (256 * 64 bits) */
  294. .quirks = 0,
  295. .little_endian = true, /* little-endian */
  296. };
  297. struct nxp_fspi {
  298. struct udevice *dev;
  299. void __iomem *iobase;
  300. void __iomem *ahb_addr;
  301. u32 memmap_phy;
  302. u32 memmap_phy_size;
  303. struct clk clk, clk_en;
  304. struct nxp_fspi_devtype_data *devtype_data;
  305. };
  306. static inline int needs_ip_only(struct nxp_fspi *f)
  307. {
  308. return f->devtype_data->quirks & FSPI_QUIRK_USE_IP_ONLY;
  309. }
  310. /*
  311. * R/W functions for big- or little-endian registers:
  312. * The FSPI controller's endianness is independent of
  313. * the CPU core's endianness. So far, although the CPU
  314. * core is little-endian the FSPI controller can use
  315. * big-endian or little-endian.
  316. */
  317. static void fspi_writel(struct nxp_fspi *f, u32 val, void __iomem *addr)
  318. {
  319. if (f->devtype_data->little_endian)
  320. out_le32(addr, val);
  321. else
  322. out_be32(addr, val);
  323. }
  324. static u32 fspi_readl(struct nxp_fspi *f, void __iomem *addr)
  325. {
  326. if (f->devtype_data->little_endian)
  327. return in_le32(addr);
  328. else
  329. return in_be32(addr);
  330. }
  331. static int nxp_fspi_check_buswidth(struct nxp_fspi *f, u8 width)
  332. {
  333. switch (width) {
  334. case 1:
  335. case 2:
  336. case 4:
  337. case 8:
  338. return 0;
  339. }
  340. return -ENOTSUPP;
  341. }
  342. static bool nxp_fspi_supports_op(struct spi_slave *slave,
  343. const struct spi_mem_op *op)
  344. {
  345. struct nxp_fspi *f;
  346. struct udevice *bus;
  347. int ret;
  348. bus = slave->dev->parent;
  349. f = dev_get_priv(bus);
  350. ret = nxp_fspi_check_buswidth(f, op->cmd.buswidth);
  351. if (op->addr.nbytes)
  352. ret |= nxp_fspi_check_buswidth(f, op->addr.buswidth);
  353. if (op->dummy.nbytes)
  354. ret |= nxp_fspi_check_buswidth(f, op->dummy.buswidth);
  355. if (op->data.nbytes)
  356. ret |= nxp_fspi_check_buswidth(f, op->data.buswidth);
  357. if (ret)
  358. return false;
  359. /*
  360. * The number of address bytes should be equal to or less than 4 bytes.
  361. */
  362. if (op->addr.nbytes > 4)
  363. return false;
  364. /*
  365. * If requested address value is greater than controller assigned
  366. * memory mapped space, return error as it didn't fit in the range
  367. * of assigned address space.
  368. */
  369. if (op->addr.val >= f->memmap_phy_size)
  370. return false;
  371. /* Max 64 dummy clock cycles supported */
  372. if (op->dummy.buswidth &&
  373. (op->dummy.nbytes * 8 / op->dummy.buswidth > 64))
  374. return false;
  375. /* Max data length, check controller limits and alignment */
  376. if (op->data.dir == SPI_MEM_DATA_IN &&
  377. (op->data.nbytes > f->devtype_data->ahb_buf_size ||
  378. (op->data.nbytes > f->devtype_data->rxfifo - 4 &&
  379. !IS_ALIGNED(op->data.nbytes, 8))))
  380. return false;
  381. if (op->data.dir == SPI_MEM_DATA_OUT &&
  382. op->data.nbytes > f->devtype_data->txfifo)
  383. return false;
  384. return spi_mem_default_supports_op(slave, op);
  385. }
  386. /* Instead of busy looping invoke readl_poll_sleep_timeout functionality. */
  387. static int fspi_readl_poll_tout(struct nxp_fspi *f, void __iomem *base,
  388. u32 mask, u32 delay_us,
  389. u32 timeout_us, bool c)
  390. {
  391. u32 reg;
  392. if (!f->devtype_data->little_endian)
  393. mask = (u32)cpu_to_be32(mask);
  394. if (c)
  395. return readl_poll_sleep_timeout(base, reg, (reg & mask),
  396. delay_us, timeout_us);
  397. else
  398. return readl_poll_sleep_timeout(base, reg, !(reg & mask),
  399. delay_us, timeout_us);
  400. }
  401. /*
  402. * If the slave device content being changed by Write/Erase, need to
  403. * invalidate the AHB buffer. This can be achieved by doing the reset
  404. * of controller after setting MCR0[SWRESET] bit.
  405. */
  406. static inline void nxp_fspi_invalid(struct nxp_fspi *f)
  407. {
  408. u32 reg;
  409. int ret;
  410. reg = fspi_readl(f, f->iobase + FSPI_MCR0);
  411. fspi_writel(f, reg | FSPI_MCR0_SWRST, f->iobase + FSPI_MCR0);
  412. /* w1c register, wait unit clear */
  413. ret = fspi_readl_poll_tout(f, f->iobase + FSPI_MCR0,
  414. FSPI_MCR0_SWRST, 0, POLL_TOUT, false);
  415. WARN_ON(ret);
  416. }
  417. static void nxp_fspi_prepare_lut(struct nxp_fspi *f,
  418. const struct spi_mem_op *op)
  419. {
  420. void __iomem *base = f->iobase;
  421. u32 lutval[4] = {};
  422. int lutidx = 1, i;
  423. /* cmd */
  424. lutval[0] |= LUT_DEF(0, LUT_CMD, LUT_PAD(op->cmd.buswidth),
  425. op->cmd.opcode);
  426. /* addr bytes */
  427. if (op->addr.nbytes) {
  428. lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_ADDR,
  429. LUT_PAD(op->addr.buswidth),
  430. op->addr.nbytes * 8);
  431. lutidx++;
  432. }
  433. /* dummy bytes, if needed */
  434. if (op->dummy.nbytes) {
  435. lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_DUMMY,
  436. /*
  437. * Due to FlexSPI controller limitation number of PAD for dummy
  438. * buswidth needs to be programmed as equal to data buswidth.
  439. */
  440. LUT_PAD(op->data.buswidth),
  441. op->dummy.nbytes * 8 /
  442. op->dummy.buswidth);
  443. lutidx++;
  444. }
  445. /* read/write data bytes */
  446. if (op->data.nbytes) {
  447. lutval[lutidx / 2] |= LUT_DEF(lutidx,
  448. op->data.dir == SPI_MEM_DATA_IN ?
  449. LUT_NXP_READ : LUT_NXP_WRITE,
  450. LUT_PAD(op->data.buswidth),
  451. 0);
  452. lutidx++;
  453. }
  454. /* stop condition. */
  455. lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_STOP, 0, 0);
  456. /* unlock LUT */
  457. fspi_writel(f, FSPI_LUTKEY_VALUE, f->iobase + FSPI_LUTKEY);
  458. fspi_writel(f, FSPI_LCKER_UNLOCK, f->iobase + FSPI_LCKCR);
  459. /* fill LUT */
  460. for (i = 0; i < ARRAY_SIZE(lutval); i++)
  461. fspi_writel(f, lutval[i], base + FSPI_LUT_REG(i));
  462. dev_dbg(f->dev, "CMD[%x] lutval[0:%x \t 1:%x \t 2:%x \t 3:%x], size: 0x%08x\n",
  463. op->cmd.opcode, lutval[0], lutval[1], lutval[2], lutval[3], op->data.nbytes);
  464. /* lock LUT */
  465. fspi_writel(f, FSPI_LUTKEY_VALUE, f->iobase + FSPI_LUTKEY);
  466. fspi_writel(f, FSPI_LCKER_LOCK, f->iobase + FSPI_LCKCR);
  467. }
  468. #if CONFIG_IS_ENABLED(CLK)
  469. static int nxp_fspi_clk_prep_enable(struct nxp_fspi *f)
  470. {
  471. int ret;
  472. ret = clk_enable(&f->clk_en);
  473. if (ret)
  474. return ret;
  475. ret = clk_enable(&f->clk);
  476. if (ret) {
  477. clk_disable(&f->clk_en);
  478. return ret;
  479. }
  480. return 0;
  481. }
  482. static void nxp_fspi_clk_disable_unprep(struct nxp_fspi *f)
  483. {
  484. clk_disable(&f->clk);
  485. clk_disable(&f->clk_en);
  486. }
  487. #endif
  488. /*
  489. * In FlexSPI controller, flash access is based on value of FSPI_FLSHXXCR0
  490. * register and start base address of the slave device.
  491. *
  492. * (Higher address)
  493. * -------- <-- FLSHB2CR0
  494. * | B2 |
  495. * | |
  496. * B2 start address --> -------- <-- FLSHB1CR0
  497. * | B1 |
  498. * | |
  499. * B1 start address --> -------- <-- FLSHA2CR0
  500. * | A2 |
  501. * | |
  502. * A2 start address --> -------- <-- FLSHA1CR0
  503. * | A1 |
  504. * | |
  505. * A1 start address --> -------- (Lower address)
  506. *
  507. *
  508. * Start base address defines the starting address range for given CS and
  509. * FSPI_FLSHXXCR0 defines the size of the slave device connected at given CS.
  510. *
  511. * But, different targets are having different combinations of number of CS,
  512. * some targets only have single CS or two CS covering controller's full
  513. * memory mapped space area.
  514. * Thus, implementation is being done as independent of the size and number
  515. * of the connected slave device.
  516. * Assign controller memory mapped space size as the size to the connected
  517. * slave device.
  518. * Mark FLSHxxCR0 as zero initially and then assign value only to the selected
  519. * chip-select Flash configuration register.
  520. *
  521. * For e.g. to access CS2 (B1), FLSHB1CR0 register would be equal to the
  522. * memory mapped size of the controller.
  523. * Value for rest of the CS FLSHxxCR0 register would be zero.
  524. *
  525. */
  526. static void nxp_fspi_select_mem(struct nxp_fspi *f, int chip_select)
  527. {
  528. u64 size_kb;
  529. /* Reset FLSHxxCR0 registers */
  530. fspi_writel(f, 0, f->iobase + FSPI_FLSHA1CR0);
  531. fspi_writel(f, 0, f->iobase + FSPI_FLSHA2CR0);
  532. fspi_writel(f, 0, f->iobase + FSPI_FLSHB1CR0);
  533. fspi_writel(f, 0, f->iobase + FSPI_FLSHB2CR0);
  534. /* Assign controller memory mapped space as size, KBytes, of flash. */
  535. size_kb = FSPI_FLSHXCR0_SZ(f->memmap_phy_size);
  536. fspi_writel(f, size_kb, f->iobase + FSPI_FLSHA1CR0 +
  537. 4 * chip_select);
  538. dev_dbg(f->dev, "Slave device [CS:%x] selected\n", chip_select);
  539. }
  540. static void nxp_fspi_read_ahb(struct nxp_fspi *f, const struct spi_mem_op *op)
  541. {
  542. u32 len = op->data.nbytes;
  543. /* Read out the data directly from the AHB buffer. */
  544. memcpy_fromio(op->data.buf.in, (f->ahb_addr + op->addr.val), len);
  545. }
  546. static void nxp_fspi_fill_txfifo(struct nxp_fspi *f,
  547. const struct spi_mem_op *op)
  548. {
  549. void __iomem *base = f->iobase;
  550. int i, ret;
  551. u8 *buf = (u8 *)op->data.buf.out;
  552. /* clear the TX FIFO. */
  553. fspi_writel(f, FSPI_IPTXFCR_CLR, base + FSPI_IPTXFCR);
  554. /*
  555. * Default value of water mark level is 8 bytes, hence in single
  556. * write request controller can write max 8 bytes of data.
  557. */
  558. for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 8); i += 8) {
  559. /* Wait for TXFIFO empty */
  560. ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
  561. FSPI_INTR_IPTXWE, 0,
  562. POLL_TOUT, true);
  563. WARN_ON(ret);
  564. fspi_writel(f, *(u32 *)(buf + i), base + FSPI_TFDR);
  565. fspi_writel(f, *(u32 *)(buf + i + 4), base + FSPI_TFDR + 4);
  566. fspi_writel(f, FSPI_INTR_IPTXWE, base + FSPI_INTR);
  567. }
  568. if (i < op->data.nbytes) {
  569. u32 data = 0;
  570. int j;
  571. /* Wait for TXFIFO empty */
  572. ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
  573. FSPI_INTR_IPTXWE, 0,
  574. POLL_TOUT, true);
  575. WARN_ON(ret);
  576. for (j = 0; j < ALIGN(op->data.nbytes - i, 4); j += 4) {
  577. memcpy(&data, buf + i + j, 4);
  578. fspi_writel(f, data, base + FSPI_TFDR + j);
  579. }
  580. fspi_writel(f, FSPI_INTR_IPTXWE, base + FSPI_INTR);
  581. }
  582. }
  583. static void nxp_fspi_read_rxfifo(struct nxp_fspi *f,
  584. const struct spi_mem_op *op)
  585. {
  586. void __iomem *base = f->iobase;
  587. int i, ret;
  588. int len = op->data.nbytes;
  589. u8 *buf = (u8 *)op->data.buf.in;
  590. /*
  591. * Default value of water mark level is 8 bytes, hence in single
  592. * read request controller can read max 8 bytes of data.
  593. */
  594. for (i = 0; i < ALIGN_DOWN(len, 8); i += 8) {
  595. /* Wait for RXFIFO available */
  596. ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
  597. FSPI_INTR_IPRXWA, 0,
  598. POLL_TOUT, true);
  599. WARN_ON(ret);
  600. *(u32 *)(buf + i) = fspi_readl(f, base + FSPI_RFDR);
  601. *(u32 *)(buf + i + 4) = fspi_readl(f, base + FSPI_RFDR + 4);
  602. /* move the FIFO pointer */
  603. fspi_writel(f, FSPI_INTR_IPRXWA, base + FSPI_INTR);
  604. }
  605. if (i < len) {
  606. u32 tmp;
  607. int size, j;
  608. buf = op->data.buf.in + i;
  609. /* Wait for RXFIFO available */
  610. ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
  611. FSPI_INTR_IPRXWA, 0,
  612. POLL_TOUT, true);
  613. WARN_ON(ret);
  614. len = op->data.nbytes - i;
  615. for (j = 0; j < op->data.nbytes - i; j += 4) {
  616. tmp = fspi_readl(f, base + FSPI_RFDR + j);
  617. size = min(len, 4);
  618. memcpy(buf + j, &tmp, size);
  619. len -= size;
  620. }
  621. }
  622. /* invalid the RXFIFO */
  623. fspi_writel(f, FSPI_IPRXFCR_CLR, base + FSPI_IPRXFCR);
  624. /* move the FIFO pointer */
  625. fspi_writel(f, FSPI_INTR_IPRXWA, base + FSPI_INTR);
  626. }
  627. static int nxp_fspi_do_op(struct nxp_fspi *f, const struct spi_mem_op *op)
  628. {
  629. void __iomem *base = f->iobase;
  630. int seqnum = 0;
  631. int err = 0;
  632. u32 reg;
  633. reg = fspi_readl(f, base + FSPI_IPRXFCR);
  634. /* invalid RXFIFO first */
  635. reg &= ~FSPI_IPRXFCR_DMA_EN;
  636. reg = reg | FSPI_IPRXFCR_CLR;
  637. fspi_writel(f, reg, base + FSPI_IPRXFCR);
  638. fspi_writel(f, op->addr.val, base + FSPI_IPCR0);
  639. /*
  640. * Always start the sequence at the same index since we update
  641. * the LUT at each exec_op() call. And also specify the DATA
  642. * length, since it's has not been specified in the LUT.
  643. */
  644. fspi_writel(f, op->data.nbytes |
  645. (SEQID_LUT << FSPI_IPCR1_SEQID_SHIFT) |
  646. (seqnum << FSPI_IPCR1_SEQNUM_SHIFT),
  647. base + FSPI_IPCR1);
  648. /* Trigger the LUT now. */
  649. fspi_writel(f, FSPI_IPCMD_TRG, base + FSPI_IPCMD);
  650. /* Wait for the completion. */
  651. err = fspi_readl_poll_tout(f, f->iobase + FSPI_STS0,
  652. FSPI_STS0_ARB_IDLE, 1, 1000 * 1000, true);
  653. /* Invoke IP data read, if request is of data read. */
  654. if (!err && op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN)
  655. nxp_fspi_read_rxfifo(f, op);
  656. return err;
  657. }
  658. static int nxp_fspi_exec_op(struct spi_slave *slave,
  659. const struct spi_mem_op *op)
  660. {
  661. struct nxp_fspi *f;
  662. struct udevice *bus;
  663. int err = 0;
  664. bus = slave->dev->parent;
  665. f = dev_get_priv(bus);
  666. /* Wait for controller being ready. */
  667. err = fspi_readl_poll_tout(f, f->iobase + FSPI_STS0,
  668. FSPI_STS0_ARB_IDLE, 1, POLL_TOUT, true);
  669. WARN_ON(err);
  670. nxp_fspi_prepare_lut(f, op);
  671. /*
  672. * If we have large chunks of data, we read them through the AHB bus by
  673. * accessing the mapped memory. In all other cases we use IP commands
  674. * to access the flash. Read via AHB bus may be corrupted due to
  675. * existence of an errata and therefore discard AHB read in such cases.
  676. */
  677. if (op->data.nbytes > (f->devtype_data->rxfifo - 4) &&
  678. op->data.dir == SPI_MEM_DATA_IN &&
  679. !needs_ip_only(f)) {
  680. nxp_fspi_read_ahb(f, op);
  681. } else {
  682. if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT)
  683. nxp_fspi_fill_txfifo(f, op);
  684. err = nxp_fspi_do_op(f, op);
  685. }
  686. /* Invalidate the data in the AHB buffer. */
  687. nxp_fspi_invalid(f);
  688. return err;
  689. }
  690. static int nxp_fspi_adjust_op_size(struct spi_slave *slave,
  691. struct spi_mem_op *op)
  692. {
  693. struct nxp_fspi *f;
  694. struct udevice *bus;
  695. bus = slave->dev->parent;
  696. f = dev_get_priv(bus);
  697. if (op->data.dir == SPI_MEM_DATA_OUT) {
  698. if (op->data.nbytes > f->devtype_data->txfifo)
  699. op->data.nbytes = f->devtype_data->txfifo;
  700. } else {
  701. if (op->data.nbytes > f->devtype_data->ahb_buf_size)
  702. op->data.nbytes = f->devtype_data->ahb_buf_size;
  703. else if (op->data.nbytes > (f->devtype_data->rxfifo - 4))
  704. op->data.nbytes = ALIGN_DOWN(op->data.nbytes, 8);
  705. }
  706. /* Limit data bytes to RX FIFO in case of IP read only */
  707. if (needs_ip_only(f) &&
  708. op->data.dir == SPI_MEM_DATA_IN &&
  709. op->data.nbytes > f->devtype_data->rxfifo)
  710. op->data.nbytes = f->devtype_data->rxfifo;
  711. return 0;
  712. }
  713. #ifdef CONFIG_FSL_LAYERSCAPE
  714. static void erratum_err050568(struct nxp_fspi *f)
  715. {
  716. struct sys_info sysinfo;
  717. u32 svr = 0, freq = 0;
  718. /* Check for LS1028A variants */
  719. svr = SVR_SOC_VER(get_svr());
  720. if (svr != SVR_LS1017A ||
  721. svr != SVR_LS1018A ||
  722. svr != SVR_LS1027A ||
  723. svr != SVR_LS1028A) {
  724. dev_dbg(f->dev, "Errata applicable only for LS1028A variants\n");
  725. return;
  726. }
  727. /* Read PLL frequency */
  728. get_sys_info(&sysinfo);
  729. freq = sysinfo.freq_systembus / 1000000; /* Convert to MHz */
  730. dev_dbg(f->dev, "svr: %08x, Frequency: %dMhz\n", svr, freq);
  731. /* Use IP bus only if PLL is 300MHz */
  732. if (freq == 300)
  733. f->devtype_data->quirks |= FSPI_QUIRK_USE_IP_ONLY;
  734. }
  735. #endif
  736. static int nxp_fspi_default_setup(struct nxp_fspi *f)
  737. {
  738. void __iomem *base = f->iobase;
  739. int ret, i;
  740. u32 reg;
  741. #if CONFIG_IS_ENABLED(CLK)
  742. /* the default frequency, we will change it later if necessary. */
  743. ret = clk_set_rate(&f->clk, 20000000);
  744. if (ret < 0)
  745. return ret;
  746. ret = nxp_fspi_clk_prep_enable(f);
  747. if (ret)
  748. return ret;
  749. #endif
  750. #ifdef CONFIG_FSL_LAYERSCAPE
  751. /*
  752. * ERR050568: Flash access by FlexSPI AHB command may not work with
  753. * platform frequency equal to 300 MHz on LS1028A.
  754. * LS1028A reuses LX2160A compatible entry. Make errata applicable for
  755. * Layerscape LS1028A platform family.
  756. */
  757. if (device_is_compatible(f->dev, "nxp,lx2160a-fspi"))
  758. erratum_err050568(f);
  759. #endif
  760. /* Reset the module */
  761. /* w1c register, wait unit clear */
  762. ret = fspi_readl_poll_tout(f, f->iobase + FSPI_MCR0,
  763. FSPI_MCR0_SWRST, 0, POLL_TOUT, false);
  764. WARN_ON(ret);
  765. /* Disable the module */
  766. fspi_writel(f, FSPI_MCR0_MDIS, base + FSPI_MCR0);
  767. /* Reset the DLL register to default value */
  768. fspi_writel(f, FSPI_DLLACR_OVRDEN, base + FSPI_DLLACR);
  769. fspi_writel(f, FSPI_DLLBCR_OVRDEN, base + FSPI_DLLBCR);
  770. /* enable module */
  771. fspi_writel(f, FSPI_MCR0_AHB_TIMEOUT(0xFF) | FSPI_MCR0_IP_TIMEOUT(0xFF),
  772. base + FSPI_MCR0);
  773. /*
  774. * Disable same device enable bit and configure all slave devices
  775. * independently.
  776. */
  777. reg = fspi_readl(f, f->iobase + FSPI_MCR2);
  778. reg = reg & ~(FSPI_MCR2_SAMEDEVICEEN);
  779. fspi_writel(f, reg, base + FSPI_MCR2);
  780. /* AHB configuration for access buffer 0~7. */
  781. for (i = 0; i < 7; i++)
  782. fspi_writel(f, 0, base + FSPI_AHBRX_BUF0CR0 + 4 * i);
  783. /*
  784. * Set ADATSZ with the maximum AHB buffer size to improve the read
  785. * performance.
  786. */
  787. fspi_writel(f, (f->devtype_data->ahb_buf_size / 8 |
  788. FSPI_AHBRXBUF0CR7_PREF), base + FSPI_AHBRX_BUF7CR0);
  789. /* prefetch and no start address alignment limitation */
  790. fspi_writel(f, FSPI_AHBCR_PREF_EN | FSPI_AHBCR_RDADDROPT,
  791. base + FSPI_AHBCR);
  792. /* AHB Read - Set lut sequence ID for all CS. */
  793. fspi_writel(f, SEQID_LUT, base + FSPI_FLSHA1CR2);
  794. fspi_writel(f, SEQID_LUT, base + FSPI_FLSHA2CR2);
  795. fspi_writel(f, SEQID_LUT, base + FSPI_FLSHB1CR2);
  796. fspi_writel(f, SEQID_LUT, base + FSPI_FLSHB2CR2);
  797. return 0;
  798. }
  799. static int nxp_fspi_probe(struct udevice *bus)
  800. {
  801. struct nxp_fspi *f = dev_get_priv(bus);
  802. f->devtype_data =
  803. (struct nxp_fspi_devtype_data *)dev_get_driver_data(bus);
  804. nxp_fspi_default_setup(f);
  805. return 0;
  806. }
  807. static int nxp_fspi_claim_bus(struct udevice *dev)
  808. {
  809. struct nxp_fspi *f;
  810. struct udevice *bus;
  811. struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
  812. bus = dev->parent;
  813. f = dev_get_priv(bus);
  814. nxp_fspi_select_mem(f, slave_plat->cs);
  815. return 0;
  816. }
  817. static int nxp_fspi_set_speed(struct udevice *bus, uint speed)
  818. {
  819. #if CONFIG_IS_ENABLED(CLK)
  820. struct nxp_fspi *f = dev_get_priv(bus);
  821. int ret;
  822. nxp_fspi_clk_disable_unprep(f);
  823. ret = clk_set_rate(&f->clk, speed);
  824. if (ret < 0)
  825. return ret;
  826. ret = nxp_fspi_clk_prep_enable(f);
  827. if (ret)
  828. return ret;
  829. #endif
  830. return 0;
  831. }
  832. static int nxp_fspi_set_mode(struct udevice *bus, uint mode)
  833. {
  834. /* Nothing to do */
  835. return 0;
  836. }
  837. static int nxp_fspi_of_to_plat(struct udevice *bus)
  838. {
  839. struct nxp_fspi *f = dev_get_priv(bus);
  840. #if CONFIG_IS_ENABLED(CLK)
  841. int ret;
  842. #endif
  843. fdt_addr_t iobase;
  844. fdt_addr_t iobase_size;
  845. fdt_addr_t ahb_addr;
  846. fdt_addr_t ahb_size;
  847. f->dev = bus;
  848. iobase = devfdt_get_addr_size_name(bus, "fspi_base", &iobase_size);
  849. if (iobase == FDT_ADDR_T_NONE) {
  850. dev_err(bus, "fspi_base regs missing\n");
  851. return -ENODEV;
  852. }
  853. f->iobase = map_physmem(iobase, iobase_size, MAP_NOCACHE);
  854. ahb_addr = devfdt_get_addr_size_name(bus, "fspi_mmap", &ahb_size);
  855. if (ahb_addr == FDT_ADDR_T_NONE) {
  856. dev_err(bus, "fspi_mmap regs missing\n");
  857. return -ENODEV;
  858. }
  859. f->ahb_addr = map_physmem(ahb_addr, ahb_size, MAP_NOCACHE);
  860. f->memmap_phy_size = ahb_size;
  861. #if CONFIG_IS_ENABLED(CLK)
  862. ret = clk_get_by_name(bus, "fspi_en", &f->clk_en);
  863. if (ret) {
  864. dev_err(bus, "failed to get fspi_en clock\n");
  865. return ret;
  866. }
  867. ret = clk_get_by_name(bus, "fspi", &f->clk);
  868. if (ret) {
  869. dev_err(bus, "failed to get fspi clock\n");
  870. return ret;
  871. }
  872. #endif
  873. dev_dbg(bus, "iobase=<0x%llx>, ahb_addr=<0x%llx>\n", iobase, ahb_addr);
  874. return 0;
  875. }
  876. static const struct spi_controller_mem_ops nxp_fspi_mem_ops = {
  877. .adjust_op_size = nxp_fspi_adjust_op_size,
  878. .supports_op = nxp_fspi_supports_op,
  879. .exec_op = nxp_fspi_exec_op,
  880. };
  881. static const struct dm_spi_ops nxp_fspi_ops = {
  882. .claim_bus = nxp_fspi_claim_bus,
  883. .set_speed = nxp_fspi_set_speed,
  884. .set_mode = nxp_fspi_set_mode,
  885. .mem_ops = &nxp_fspi_mem_ops,
  886. };
  887. static const struct udevice_id nxp_fspi_ids[] = {
  888. { .compatible = "nxp,lx2160a-fspi", .data = (ulong)&lx2160a_data, },
  889. { .compatible = "nxp,imx8mm-fspi", .data = (ulong)&imx8mm_data, },
  890. { .compatible = "nxp,imx8mp-fspi", .data = (ulong)&imx8mm_data, },
  891. { }
  892. };
  893. U_BOOT_DRIVER(nxp_fspi) = {
  894. .name = "nxp_fspi",
  895. .id = UCLASS_SPI,
  896. .of_match = nxp_fspi_ids,
  897. .ops = &nxp_fspi_ops,
  898. .of_to_plat = nxp_fspi_of_to_plat,
  899. .priv_auto = sizeof(struct nxp_fspi),
  900. .probe = nxp_fspi_probe,
  901. };