spi-nxp-fspi.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * NXP FlexSPI(FSPI) controller driver.
  4. *
  5. * Copyright 2019-2020 NXP
  6. * Copyright 2020 Puresoftware Ltd.
  7. *
  8. * FlexSPI is a flexsible SPI host controller which supports two SPI
  9. * channels and up to 4 external devices. Each channel supports
  10. * Single/Dual/Quad/Octal mode data transfer (1/2/4/8 bidirectional
  11. * data lines).
  12. *
  13. * FlexSPI controller is driven by the LUT(Look-up Table) registers
  14. * LUT registers are a look-up-table for sequences of instructions.
  15. * A valid sequence consists of four LUT registers.
  16. * Maximum 32 LUT sequences can be programmed simultaneously.
  17. *
  18. * LUTs are being created at run-time based on the commands passed
  19. * from the spi-mem framework, thus using single LUT index.
  20. *
  21. * Software triggered Flash read/write access by IP Bus.
  22. *
  23. * Memory mapped read access by AHB Bus.
  24. *
  25. * Based on SPI MEM interface and spi-fsl-qspi.c driver.
  26. *
  27. * Author:
  28. * Yogesh Narayan Gaur <[email protected]>
  29. * Boris Brezillon <[email protected]>
  30. * Frieder Schrempf <[email protected]>
  31. */
  32. #include <linux/acpi.h>
  33. #include <linux/bitops.h>
  34. #include <linux/bitfield.h>
  35. #include <linux/clk.h>
  36. #include <linux/completion.h>
  37. #include <linux/delay.h>
  38. #include <linux/err.h>
  39. #include <linux/errno.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/io.h>
  42. #include <linux/iopoll.h>
  43. #include <linux/jiffies.h>
  44. #include <linux/kernel.h>
  45. #include <linux/module.h>
  46. #include <linux/mutex.h>
  47. #include <linux/of.h>
  48. #include <linux/of_device.h>
  49. #include <linux/platform_device.h>
  50. #include <linux/pm_qos.h>
  51. #include <linux/regmap.h>
  52. #include <linux/sizes.h>
  53. #include <linux/sys_soc.h>
  54. #include <linux/mfd/syscon.h>
  55. #include <linux/spi/spi.h>
  56. #include <linux/spi/spi-mem.h>
  57. /*
  58. * The driver only uses one single LUT entry, that is updated on
  59. * each call of exec_op(). Index 0 is preset at boot with a basic
  60. * read operation, so let's use the last entry (31).
  61. */
  62. #define SEQID_LUT 31
  63. /* Registers used by the driver */
  64. #define FSPI_MCR0 0x00
  65. #define FSPI_MCR0_AHB_TIMEOUT(x) ((x) << 24)
  66. #define FSPI_MCR0_IP_TIMEOUT(x) ((x) << 16)
  67. #define FSPI_MCR0_LEARN_EN BIT(15)
  68. #define FSPI_MCR0_SCRFRUN_EN BIT(14)
  69. #define FSPI_MCR0_OCTCOMB_EN BIT(13)
  70. #define FSPI_MCR0_DOZE_EN BIT(12)
  71. #define FSPI_MCR0_HSEN BIT(11)
  72. #define FSPI_MCR0_SERCLKDIV BIT(8)
  73. #define FSPI_MCR0_ATDF_EN BIT(7)
  74. #define FSPI_MCR0_ARDF_EN BIT(6)
  75. #define FSPI_MCR0_RXCLKSRC(x) ((x) << 4)
  76. #define FSPI_MCR0_END_CFG(x) ((x) << 2)
  77. #define FSPI_MCR0_MDIS BIT(1)
  78. #define FSPI_MCR0_SWRST BIT(0)
  79. #define FSPI_MCR1 0x04
  80. #define FSPI_MCR1_SEQ_TIMEOUT(x) ((x) << 16)
  81. #define FSPI_MCR1_AHB_TIMEOUT(x) (x)
  82. #define FSPI_MCR2 0x08
  83. #define FSPI_MCR2_IDLE_WAIT(x) ((x) << 24)
  84. #define FSPI_MCR2_SAMEDEVICEEN BIT(15)
  85. #define FSPI_MCR2_CLRLRPHS BIT(14)
  86. #define FSPI_MCR2_ABRDATSZ BIT(8)
  87. #define FSPI_MCR2_ABRLEARN BIT(7)
  88. #define FSPI_MCR2_ABR_READ BIT(6)
  89. #define FSPI_MCR2_ABRWRITE BIT(5)
  90. #define FSPI_MCR2_ABRDUMMY BIT(4)
  91. #define FSPI_MCR2_ABR_MODE BIT(3)
  92. #define FSPI_MCR2_ABRCADDR BIT(2)
  93. #define FSPI_MCR2_ABRRADDR BIT(1)
  94. #define FSPI_MCR2_ABR_CMD BIT(0)
  95. #define FSPI_AHBCR 0x0c
  96. #define FSPI_AHBCR_RDADDROPT BIT(6)
  97. #define FSPI_AHBCR_PREF_EN BIT(5)
  98. #define FSPI_AHBCR_BUFF_EN BIT(4)
  99. #define FSPI_AHBCR_CACH_EN BIT(3)
  100. #define FSPI_AHBCR_CLRTXBUF BIT(2)
  101. #define FSPI_AHBCR_CLRRXBUF BIT(1)
  102. #define FSPI_AHBCR_PAR_EN BIT(0)
  103. #define FSPI_INTEN 0x10
  104. #define FSPI_INTEN_SCLKSBWR BIT(9)
  105. #define FSPI_INTEN_SCLKSBRD BIT(8)
  106. #define FSPI_INTEN_DATALRNFL BIT(7)
  107. #define FSPI_INTEN_IPTXWE BIT(6)
  108. #define FSPI_INTEN_IPRXWA BIT(5)
  109. #define FSPI_INTEN_AHBCMDERR BIT(4)
  110. #define FSPI_INTEN_IPCMDERR BIT(3)
  111. #define FSPI_INTEN_AHBCMDGE BIT(2)
  112. #define FSPI_INTEN_IPCMDGE BIT(1)
  113. #define FSPI_INTEN_IPCMDDONE BIT(0)
  114. #define FSPI_INTR 0x14
  115. #define FSPI_INTR_SCLKSBWR BIT(9)
  116. #define FSPI_INTR_SCLKSBRD BIT(8)
  117. #define FSPI_INTR_DATALRNFL BIT(7)
  118. #define FSPI_INTR_IPTXWE BIT(6)
  119. #define FSPI_INTR_IPRXWA BIT(5)
  120. #define FSPI_INTR_AHBCMDERR BIT(4)
  121. #define FSPI_INTR_IPCMDERR BIT(3)
  122. #define FSPI_INTR_AHBCMDGE BIT(2)
  123. #define FSPI_INTR_IPCMDGE BIT(1)
  124. #define FSPI_INTR_IPCMDDONE BIT(0)
  125. #define FSPI_LUTKEY 0x18
  126. #define FSPI_LUTKEY_VALUE 0x5AF05AF0
  127. #define FSPI_LCKCR 0x1C
  128. #define FSPI_LCKER_LOCK 0x1
  129. #define FSPI_LCKER_UNLOCK 0x2
  130. #define FSPI_BUFXCR_INVALID_MSTRID 0xE
  131. #define FSPI_AHBRX_BUF0CR0 0x20
  132. #define FSPI_AHBRX_BUF1CR0 0x24
  133. #define FSPI_AHBRX_BUF2CR0 0x28
  134. #define FSPI_AHBRX_BUF3CR0 0x2C
  135. #define FSPI_AHBRX_BUF4CR0 0x30
  136. #define FSPI_AHBRX_BUF5CR0 0x34
  137. #define FSPI_AHBRX_BUF6CR0 0x38
  138. #define FSPI_AHBRX_BUF7CR0 0x3C
  139. #define FSPI_AHBRXBUF0CR7_PREF BIT(31)
  140. #define FSPI_AHBRX_BUF0CR1 0x40
  141. #define FSPI_AHBRX_BUF1CR1 0x44
  142. #define FSPI_AHBRX_BUF2CR1 0x48
  143. #define FSPI_AHBRX_BUF3CR1 0x4C
  144. #define FSPI_AHBRX_BUF4CR1 0x50
  145. #define FSPI_AHBRX_BUF5CR1 0x54
  146. #define FSPI_AHBRX_BUF6CR1 0x58
  147. #define FSPI_AHBRX_BUF7CR1 0x5C
  148. #define FSPI_FLSHA1CR0 0x60
  149. #define FSPI_FLSHA2CR0 0x64
  150. #define FSPI_FLSHB1CR0 0x68
  151. #define FSPI_FLSHB2CR0 0x6C
  152. #define FSPI_FLSHXCR0_SZ_KB 10
  153. #define FSPI_FLSHXCR0_SZ(x) ((x) >> FSPI_FLSHXCR0_SZ_KB)
  154. #define FSPI_FLSHA1CR1 0x70
  155. #define FSPI_FLSHA2CR1 0x74
  156. #define FSPI_FLSHB1CR1 0x78
  157. #define FSPI_FLSHB2CR1 0x7C
  158. #define FSPI_FLSHXCR1_CSINTR(x) ((x) << 16)
  159. #define FSPI_FLSHXCR1_CAS(x) ((x) << 11)
  160. #define FSPI_FLSHXCR1_WA BIT(10)
  161. #define FSPI_FLSHXCR1_TCSH(x) ((x) << 5)
  162. #define FSPI_FLSHXCR1_TCSS(x) (x)
  163. #define FSPI_FLSHA1CR2 0x80
  164. #define FSPI_FLSHA2CR2 0x84
  165. #define FSPI_FLSHB1CR2 0x88
  166. #define FSPI_FLSHB2CR2 0x8C
  167. #define FSPI_FLSHXCR2_CLRINSP BIT(24)
  168. #define FSPI_FLSHXCR2_AWRWAIT BIT(16)
  169. #define FSPI_FLSHXCR2_AWRSEQN_SHIFT 13
  170. #define FSPI_FLSHXCR2_AWRSEQI_SHIFT 8
  171. #define FSPI_FLSHXCR2_ARDSEQN_SHIFT 5
  172. #define FSPI_FLSHXCR2_ARDSEQI_SHIFT 0
  173. #define FSPI_IPCR0 0xA0
  174. #define FSPI_IPCR1 0xA4
  175. #define FSPI_IPCR1_IPAREN BIT(31)
  176. #define FSPI_IPCR1_SEQNUM_SHIFT 24
  177. #define FSPI_IPCR1_SEQID_SHIFT 16
  178. #define FSPI_IPCR1_IDATSZ(x) (x)
  179. #define FSPI_IPCMD 0xB0
  180. #define FSPI_IPCMD_TRG BIT(0)
  181. #define FSPI_DLPR 0xB4
  182. #define FSPI_IPRXFCR 0xB8
  183. #define FSPI_IPRXFCR_CLR BIT(0)
  184. #define FSPI_IPRXFCR_DMA_EN BIT(1)
  185. #define FSPI_IPRXFCR_WMRK(x) ((x) << 2)
  186. #define FSPI_IPTXFCR 0xBC
  187. #define FSPI_IPTXFCR_CLR BIT(0)
  188. #define FSPI_IPTXFCR_DMA_EN BIT(1)
  189. #define FSPI_IPTXFCR_WMRK(x) ((x) << 2)
  190. #define FSPI_DLLACR 0xC0
  191. #define FSPI_DLLACR_OVRDEN BIT(8)
  192. #define FSPI_DLLBCR 0xC4
  193. #define FSPI_DLLBCR_OVRDEN BIT(8)
  194. #define FSPI_STS0 0xE0
  195. #define FSPI_STS0_DLPHB(x) ((x) << 8)
  196. #define FSPI_STS0_DLPHA(x) ((x) << 4)
  197. #define FSPI_STS0_CMD_SRC(x) ((x) << 2)
  198. #define FSPI_STS0_ARB_IDLE BIT(1)
  199. #define FSPI_STS0_SEQ_IDLE BIT(0)
  200. #define FSPI_STS1 0xE4
  201. #define FSPI_STS1_IP_ERRCD(x) ((x) << 24)
  202. #define FSPI_STS1_IP_ERRID(x) ((x) << 16)
  203. #define FSPI_STS1_AHB_ERRCD(x) ((x) << 8)
  204. #define FSPI_STS1_AHB_ERRID(x) (x)
  205. #define FSPI_AHBSPNST 0xEC
  206. #define FSPI_AHBSPNST_DATLFT(x) ((x) << 16)
  207. #define FSPI_AHBSPNST_BUFID(x) ((x) << 1)
  208. #define FSPI_AHBSPNST_ACTIVE BIT(0)
  209. #define FSPI_IPRXFSTS 0xF0
  210. #define FSPI_IPRXFSTS_RDCNTR(x) ((x) << 16)
  211. #define FSPI_IPRXFSTS_FILL(x) (x)
  212. #define FSPI_IPTXFSTS 0xF4
  213. #define FSPI_IPTXFSTS_WRCNTR(x) ((x) << 16)
  214. #define FSPI_IPTXFSTS_FILL(x) (x)
  215. #define FSPI_RFDR 0x100
  216. #define FSPI_TFDR 0x180
  217. #define FSPI_LUT_BASE 0x200
  218. #define FSPI_LUT_OFFSET (SEQID_LUT * 4 * 4)
  219. #define FSPI_LUT_REG(idx) \
  220. (FSPI_LUT_BASE + FSPI_LUT_OFFSET + (idx) * 4)
  221. /* register map end */
  222. /* Instruction set for the LUT register. */
  223. #define LUT_STOP 0x00
  224. #define LUT_CMD 0x01
  225. #define LUT_ADDR 0x02
  226. #define LUT_CADDR_SDR 0x03
  227. #define LUT_MODE 0x04
  228. #define LUT_MODE2 0x05
  229. #define LUT_MODE4 0x06
  230. #define LUT_MODE8 0x07
  231. #define LUT_NXP_WRITE 0x08
  232. #define LUT_NXP_READ 0x09
  233. #define LUT_LEARN_SDR 0x0A
  234. #define LUT_DATSZ_SDR 0x0B
  235. #define LUT_DUMMY 0x0C
  236. #define LUT_DUMMY_RWDS_SDR 0x0D
  237. #define LUT_JMP_ON_CS 0x1F
  238. #define LUT_CMD_DDR 0x21
  239. #define LUT_ADDR_DDR 0x22
  240. #define LUT_CADDR_DDR 0x23
  241. #define LUT_MODE_DDR 0x24
  242. #define LUT_MODE2_DDR 0x25
  243. #define LUT_MODE4_DDR 0x26
  244. #define LUT_MODE8_DDR 0x27
  245. #define LUT_WRITE_DDR 0x28
  246. #define LUT_READ_DDR 0x29
  247. #define LUT_LEARN_DDR 0x2A
  248. #define LUT_DATSZ_DDR 0x2B
  249. #define LUT_DUMMY_DDR 0x2C
  250. #define LUT_DUMMY_RWDS_DDR 0x2D
  251. /*
  252. * Calculate number of required PAD bits for LUT register.
  253. *
  254. * The pad stands for the number of IO lines [0:7].
  255. * For example, the octal read needs eight IO lines,
  256. * so you should use LUT_PAD(8). This macro
  257. * returns 3 i.e. use eight (2^3) IP lines for read.
  258. */
  259. #define LUT_PAD(x) (fls(x) - 1)
  260. /*
  261. * Macro for constructing the LUT entries with the following
  262. * register layout:
  263. *
  264. * ---------------------------------------------------
  265. * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 |
  266. * ---------------------------------------------------
  267. */
  268. #define PAD_SHIFT 8
  269. #define INSTR_SHIFT 10
  270. #define OPRND_SHIFT 16
  271. /* Macros for constructing the LUT register. */
  272. #define LUT_DEF(idx, ins, pad, opr) \
  273. ((((ins) << INSTR_SHIFT) | ((pad) << PAD_SHIFT) | \
  274. (opr)) << (((idx) % 2) * OPRND_SHIFT))
  275. #define POLL_TOUT 5000
  276. #define NXP_FSPI_MAX_CHIPSELECT 4
  277. #define NXP_FSPI_MIN_IOMAP SZ_4M
  278. #define DCFG_RCWSR1 0x100
  279. #define SYS_PLL_RAT GENMASK(6, 2)
  280. /* Access flash memory using IP bus only */
  281. #define FSPI_QUIRK_USE_IP_ONLY BIT(0)
  282. struct nxp_fspi_devtype_data {
  283. unsigned int rxfifo;
  284. unsigned int txfifo;
  285. unsigned int ahb_buf_size;
  286. unsigned int quirks;
  287. bool little_endian;
  288. };
  289. static struct nxp_fspi_devtype_data lx2160a_data = {
  290. .rxfifo = SZ_512, /* (64 * 64 bits) */
  291. .txfifo = SZ_1K, /* (128 * 64 bits) */
  292. .ahb_buf_size = SZ_2K, /* (256 * 64 bits) */
  293. .quirks = 0,
  294. .little_endian = true, /* little-endian */
  295. };
  296. static struct nxp_fspi_devtype_data imx8mm_data = {
  297. .rxfifo = SZ_512, /* (64 * 64 bits) */
  298. .txfifo = SZ_1K, /* (128 * 64 bits) */
  299. .ahb_buf_size = SZ_2K, /* (256 * 64 bits) */
  300. .quirks = 0,
  301. .little_endian = true, /* little-endian */
  302. };
  303. static struct nxp_fspi_devtype_data imx8qxp_data = {
  304. .rxfifo = SZ_512, /* (64 * 64 bits) */
  305. .txfifo = SZ_1K, /* (128 * 64 bits) */
  306. .ahb_buf_size = SZ_2K, /* (256 * 64 bits) */
  307. .quirks = 0,
  308. .little_endian = true, /* little-endian */
  309. };
  310. static struct nxp_fspi_devtype_data imx8dxl_data = {
  311. .rxfifo = SZ_512, /* (64 * 64 bits) */
  312. .txfifo = SZ_1K, /* (128 * 64 bits) */
  313. .ahb_buf_size = SZ_2K, /* (256 * 64 bits) */
  314. .quirks = FSPI_QUIRK_USE_IP_ONLY,
  315. .little_endian = true, /* little-endian */
  316. };
  317. struct nxp_fspi {
  318. void __iomem *iobase;
  319. void __iomem *ahb_addr;
  320. u32 memmap_phy;
  321. u32 memmap_phy_size;
  322. u32 memmap_start;
  323. u32 memmap_len;
  324. struct clk *clk, *clk_en;
  325. struct device *dev;
  326. struct completion c;
  327. struct nxp_fspi_devtype_data *devtype_data;
  328. struct mutex lock;
  329. struct pm_qos_request pm_qos_req;
  330. int selected;
  331. };
  332. static inline int needs_ip_only(struct nxp_fspi *f)
  333. {
  334. return f->devtype_data->quirks & FSPI_QUIRK_USE_IP_ONLY;
  335. }
  336. /*
  337. * R/W functions for big- or little-endian registers:
  338. * The FSPI controller's endianness is independent of
  339. * the CPU core's endianness. So far, although the CPU
  340. * core is little-endian the FSPI controller can use
  341. * big-endian or little-endian.
  342. */
  343. static void fspi_writel(struct nxp_fspi *f, u32 val, void __iomem *addr)
  344. {
  345. if (f->devtype_data->little_endian)
  346. iowrite32(val, addr);
  347. else
  348. iowrite32be(val, addr);
  349. }
  350. static u32 fspi_readl(struct nxp_fspi *f, void __iomem *addr)
  351. {
  352. if (f->devtype_data->little_endian)
  353. return ioread32(addr);
  354. else
  355. return ioread32be(addr);
  356. }
  357. static irqreturn_t nxp_fspi_irq_handler(int irq, void *dev_id)
  358. {
  359. struct nxp_fspi *f = dev_id;
  360. u32 reg;
  361. /* clear interrupt */
  362. reg = fspi_readl(f, f->iobase + FSPI_INTR);
  363. fspi_writel(f, FSPI_INTR_IPCMDDONE, f->iobase + FSPI_INTR);
  364. if (reg & FSPI_INTR_IPCMDDONE)
  365. complete(&f->c);
  366. return IRQ_HANDLED;
  367. }
  368. static int nxp_fspi_check_buswidth(struct nxp_fspi *f, u8 width)
  369. {
  370. switch (width) {
  371. case 1:
  372. case 2:
  373. case 4:
  374. case 8:
  375. return 0;
  376. }
  377. return -ENOTSUPP;
  378. }
  379. static bool nxp_fspi_supports_op(struct spi_mem *mem,
  380. const struct spi_mem_op *op)
  381. {
  382. struct nxp_fspi *f = spi_controller_get_devdata(mem->spi->master);
  383. int ret;
  384. ret = nxp_fspi_check_buswidth(f, op->cmd.buswidth);
  385. if (op->addr.nbytes)
  386. ret |= nxp_fspi_check_buswidth(f, op->addr.buswidth);
  387. if (op->dummy.nbytes)
  388. ret |= nxp_fspi_check_buswidth(f, op->dummy.buswidth);
  389. if (op->data.nbytes)
  390. ret |= nxp_fspi_check_buswidth(f, op->data.buswidth);
  391. if (ret)
  392. return false;
  393. /*
  394. * The number of address bytes should be equal to or less than 4 bytes.
  395. */
  396. if (op->addr.nbytes > 4)
  397. return false;
  398. /*
  399. * If requested address value is greater than controller assigned
  400. * memory mapped space, return error as it didn't fit in the range
  401. * of assigned address space.
  402. */
  403. if (op->addr.val >= f->memmap_phy_size)
  404. return false;
  405. /* Max 64 dummy clock cycles supported */
  406. if (op->dummy.buswidth &&
  407. (op->dummy.nbytes * 8 / op->dummy.buswidth > 64))
  408. return false;
  409. /* Max data length, check controller limits and alignment */
  410. if (op->data.dir == SPI_MEM_DATA_IN &&
  411. (op->data.nbytes > f->devtype_data->ahb_buf_size ||
  412. (op->data.nbytes > f->devtype_data->rxfifo - 4 &&
  413. !IS_ALIGNED(op->data.nbytes, 8))))
  414. return false;
  415. if (op->data.dir == SPI_MEM_DATA_OUT &&
  416. op->data.nbytes > f->devtype_data->txfifo)
  417. return false;
  418. return spi_mem_default_supports_op(mem, op);
  419. }
  420. /* Instead of busy looping invoke readl_poll_timeout functionality. */
  421. static int fspi_readl_poll_tout(struct nxp_fspi *f, void __iomem *base,
  422. u32 mask, u32 delay_us,
  423. u32 timeout_us, bool c)
  424. {
  425. u32 reg;
  426. if (!f->devtype_data->little_endian)
  427. mask = (u32)cpu_to_be32(mask);
  428. if (c)
  429. return readl_poll_timeout(base, reg, (reg & mask),
  430. delay_us, timeout_us);
  431. else
  432. return readl_poll_timeout(base, reg, !(reg & mask),
  433. delay_us, timeout_us);
  434. }
  435. /*
  436. * If the slave device content being changed by Write/Erase, need to
  437. * invalidate the AHB buffer. This can be achieved by doing the reset
  438. * of controller after setting MCR0[SWRESET] bit.
  439. */
  440. static inline void nxp_fspi_invalid(struct nxp_fspi *f)
  441. {
  442. u32 reg;
  443. int ret;
  444. reg = fspi_readl(f, f->iobase + FSPI_MCR0);
  445. fspi_writel(f, reg | FSPI_MCR0_SWRST, f->iobase + FSPI_MCR0);
  446. /* w1c register, wait unit clear */
  447. ret = fspi_readl_poll_tout(f, f->iobase + FSPI_MCR0,
  448. FSPI_MCR0_SWRST, 0, POLL_TOUT, false);
  449. WARN_ON(ret);
  450. }
  451. static void nxp_fspi_prepare_lut(struct nxp_fspi *f,
  452. const struct spi_mem_op *op)
  453. {
  454. void __iomem *base = f->iobase;
  455. u32 lutval[4] = {};
  456. int lutidx = 1, i;
  457. /* cmd */
  458. lutval[0] |= LUT_DEF(0, LUT_CMD, LUT_PAD(op->cmd.buswidth),
  459. op->cmd.opcode);
  460. /* addr bytes */
  461. if (op->addr.nbytes) {
  462. lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_ADDR,
  463. LUT_PAD(op->addr.buswidth),
  464. op->addr.nbytes * 8);
  465. lutidx++;
  466. }
  467. /* dummy bytes, if needed */
  468. if (op->dummy.nbytes) {
  469. lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_DUMMY,
  470. /*
  471. * Due to FlexSPI controller limitation number of PAD for dummy
  472. * buswidth needs to be programmed as equal to data buswidth.
  473. */
  474. LUT_PAD(op->data.buswidth),
  475. op->dummy.nbytes * 8 /
  476. op->dummy.buswidth);
  477. lutidx++;
  478. }
  479. /* read/write data bytes */
  480. if (op->data.nbytes) {
  481. lutval[lutidx / 2] |= LUT_DEF(lutidx,
  482. op->data.dir == SPI_MEM_DATA_IN ?
  483. LUT_NXP_READ : LUT_NXP_WRITE,
  484. LUT_PAD(op->data.buswidth),
  485. 0);
  486. lutidx++;
  487. }
  488. /* stop condition. */
  489. lutval[lutidx / 2] |= LUT_DEF(lutidx, LUT_STOP, 0, 0);
  490. /* unlock LUT */
  491. fspi_writel(f, FSPI_LUTKEY_VALUE, f->iobase + FSPI_LUTKEY);
  492. fspi_writel(f, FSPI_LCKER_UNLOCK, f->iobase + FSPI_LCKCR);
  493. /* fill LUT */
  494. for (i = 0; i < ARRAY_SIZE(lutval); i++)
  495. fspi_writel(f, lutval[i], base + FSPI_LUT_REG(i));
  496. dev_dbg(f->dev, "CMD[%x] lutval[0:%x \t 1:%x \t 2:%x \t 3:%x], size: 0x%08x\n",
  497. op->cmd.opcode, lutval[0], lutval[1], lutval[2], lutval[3], op->data.nbytes);
  498. /* lock LUT */
  499. fspi_writel(f, FSPI_LUTKEY_VALUE, f->iobase + FSPI_LUTKEY);
  500. fspi_writel(f, FSPI_LCKER_LOCK, f->iobase + FSPI_LCKCR);
  501. }
  502. static int nxp_fspi_clk_prep_enable(struct nxp_fspi *f)
  503. {
  504. int ret;
  505. if (is_acpi_node(dev_fwnode(f->dev)))
  506. return 0;
  507. ret = clk_prepare_enable(f->clk_en);
  508. if (ret)
  509. return ret;
  510. ret = clk_prepare_enable(f->clk);
  511. if (ret) {
  512. clk_disable_unprepare(f->clk_en);
  513. return ret;
  514. }
  515. return 0;
  516. }
  517. static int nxp_fspi_clk_disable_unprep(struct nxp_fspi *f)
  518. {
  519. if (is_acpi_node(dev_fwnode(f->dev)))
  520. return 0;
  521. clk_disable_unprepare(f->clk);
  522. clk_disable_unprepare(f->clk_en);
  523. return 0;
  524. }
  525. /*
  526. * In FlexSPI controller, flash access is based on value of FSPI_FLSHXXCR0
  527. * register and start base address of the slave device.
  528. *
  529. * (Higher address)
  530. * -------- <-- FLSHB2CR0
  531. * | B2 |
  532. * | |
  533. * B2 start address --> -------- <-- FLSHB1CR0
  534. * | B1 |
  535. * | |
  536. * B1 start address --> -------- <-- FLSHA2CR0
  537. * | A2 |
  538. * | |
  539. * A2 start address --> -------- <-- FLSHA1CR0
  540. * | A1 |
  541. * | |
  542. * A1 start address --> -------- (Lower address)
  543. *
  544. *
  545. * Start base address defines the starting address range for given CS and
  546. * FSPI_FLSHXXCR0 defines the size of the slave device connected at given CS.
  547. *
  548. * But, different targets are having different combinations of number of CS,
  549. * some targets only have single CS or two CS covering controller's full
  550. * memory mapped space area.
  551. * Thus, implementation is being done as independent of the size and number
  552. * of the connected slave device.
  553. * Assign controller memory mapped space size as the size to the connected
  554. * slave device.
  555. * Mark FLSHxxCR0 as zero initially and then assign value only to the selected
  556. * chip-select Flash configuration register.
  557. *
  558. * For e.g. to access CS2 (B1), FLSHB1CR0 register would be equal to the
  559. * memory mapped size of the controller.
  560. * Value for rest of the CS FLSHxxCR0 register would be zero.
  561. *
  562. */
  563. static void nxp_fspi_select_mem(struct nxp_fspi *f, struct spi_device *spi)
  564. {
  565. unsigned long rate = spi->max_speed_hz;
  566. int ret;
  567. uint64_t size_kb;
  568. /*
  569. * Return, if previously selected slave device is same as current
  570. * requested slave device.
  571. */
  572. if (f->selected == spi->chip_select)
  573. return;
  574. /* Reset FLSHxxCR0 registers */
  575. fspi_writel(f, 0, f->iobase + FSPI_FLSHA1CR0);
  576. fspi_writel(f, 0, f->iobase + FSPI_FLSHA2CR0);
  577. fspi_writel(f, 0, f->iobase + FSPI_FLSHB1CR0);
  578. fspi_writel(f, 0, f->iobase + FSPI_FLSHB2CR0);
  579. /* Assign controller memory mapped space as size, KBytes, of flash. */
  580. size_kb = FSPI_FLSHXCR0_SZ(f->memmap_phy_size);
  581. fspi_writel(f, size_kb, f->iobase + FSPI_FLSHA1CR0 +
  582. 4 * spi->chip_select);
  583. dev_dbg(f->dev, "Slave device [CS:%x] selected\n", spi->chip_select);
  584. nxp_fspi_clk_disable_unprep(f);
  585. ret = clk_set_rate(f->clk, rate);
  586. if (ret)
  587. return;
  588. ret = nxp_fspi_clk_prep_enable(f);
  589. if (ret)
  590. return;
  591. f->selected = spi->chip_select;
  592. }
  593. static int nxp_fspi_read_ahb(struct nxp_fspi *f, const struct spi_mem_op *op)
  594. {
  595. u32 start = op->addr.val;
  596. u32 len = op->data.nbytes;
  597. /* if necessary, ioremap before AHB read */
  598. if ((!f->ahb_addr) || start < f->memmap_start ||
  599. start + len > f->memmap_start + f->memmap_len) {
  600. if (f->ahb_addr)
  601. iounmap(f->ahb_addr);
  602. f->memmap_start = start;
  603. f->memmap_len = len > NXP_FSPI_MIN_IOMAP ?
  604. len : NXP_FSPI_MIN_IOMAP;
  605. f->ahb_addr = ioremap(f->memmap_phy + f->memmap_start,
  606. f->memmap_len);
  607. if (!f->ahb_addr) {
  608. dev_err(f->dev, "failed to alloc memory\n");
  609. return -ENOMEM;
  610. }
  611. }
  612. /* Read out the data directly from the AHB buffer. */
  613. memcpy_fromio(op->data.buf.in,
  614. f->ahb_addr + start - f->memmap_start, len);
  615. return 0;
  616. }
  617. static void nxp_fspi_fill_txfifo(struct nxp_fspi *f,
  618. const struct spi_mem_op *op)
  619. {
  620. void __iomem *base = f->iobase;
  621. int i, ret;
  622. u8 *buf = (u8 *) op->data.buf.out;
  623. /* clear the TX FIFO. */
  624. fspi_writel(f, FSPI_IPTXFCR_CLR, base + FSPI_IPTXFCR);
  625. /*
  626. * Default value of water mark level is 8 bytes, hence in single
  627. * write request controller can write max 8 bytes of data.
  628. */
  629. for (i = 0; i < ALIGN_DOWN(op->data.nbytes, 8); i += 8) {
  630. /* Wait for TXFIFO empty */
  631. ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
  632. FSPI_INTR_IPTXWE, 0,
  633. POLL_TOUT, true);
  634. WARN_ON(ret);
  635. fspi_writel(f, *(u32 *) (buf + i), base + FSPI_TFDR);
  636. fspi_writel(f, *(u32 *) (buf + i + 4), base + FSPI_TFDR + 4);
  637. fspi_writel(f, FSPI_INTR_IPTXWE, base + FSPI_INTR);
  638. }
  639. if (i < op->data.nbytes) {
  640. u32 data = 0;
  641. int j;
  642. /* Wait for TXFIFO empty */
  643. ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
  644. FSPI_INTR_IPTXWE, 0,
  645. POLL_TOUT, true);
  646. WARN_ON(ret);
  647. for (j = 0; j < ALIGN(op->data.nbytes - i, 4); j += 4) {
  648. memcpy(&data, buf + i + j, 4);
  649. fspi_writel(f, data, base + FSPI_TFDR + j);
  650. }
  651. fspi_writel(f, FSPI_INTR_IPTXWE, base + FSPI_INTR);
  652. }
  653. }
  654. static void nxp_fspi_read_rxfifo(struct nxp_fspi *f,
  655. const struct spi_mem_op *op)
  656. {
  657. void __iomem *base = f->iobase;
  658. int i, ret;
  659. int len = op->data.nbytes;
  660. u8 *buf = (u8 *) op->data.buf.in;
  661. /*
  662. * Default value of water mark level is 8 bytes, hence in single
  663. * read request controller can read max 8 bytes of data.
  664. */
  665. for (i = 0; i < ALIGN_DOWN(len, 8); i += 8) {
  666. /* Wait for RXFIFO available */
  667. ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
  668. FSPI_INTR_IPRXWA, 0,
  669. POLL_TOUT, true);
  670. WARN_ON(ret);
  671. *(u32 *)(buf + i) = fspi_readl(f, base + FSPI_RFDR);
  672. *(u32 *)(buf + i + 4) = fspi_readl(f, base + FSPI_RFDR + 4);
  673. /* move the FIFO pointer */
  674. fspi_writel(f, FSPI_INTR_IPRXWA, base + FSPI_INTR);
  675. }
  676. if (i < len) {
  677. u32 tmp;
  678. int size, j;
  679. buf = op->data.buf.in + i;
  680. /* Wait for RXFIFO available */
  681. ret = fspi_readl_poll_tout(f, f->iobase + FSPI_INTR,
  682. FSPI_INTR_IPRXWA, 0,
  683. POLL_TOUT, true);
  684. WARN_ON(ret);
  685. len = op->data.nbytes - i;
  686. for (j = 0; j < op->data.nbytes - i; j += 4) {
  687. tmp = fspi_readl(f, base + FSPI_RFDR + j);
  688. size = min(len, 4);
  689. memcpy(buf + j, &tmp, size);
  690. len -= size;
  691. }
  692. }
  693. /* invalid the RXFIFO */
  694. fspi_writel(f, FSPI_IPRXFCR_CLR, base + FSPI_IPRXFCR);
  695. /* move the FIFO pointer */
  696. fspi_writel(f, FSPI_INTR_IPRXWA, base + FSPI_INTR);
  697. }
  698. static int nxp_fspi_do_op(struct nxp_fspi *f, const struct spi_mem_op *op)
  699. {
  700. void __iomem *base = f->iobase;
  701. int seqnum = 0;
  702. int err = 0;
  703. u32 reg;
  704. reg = fspi_readl(f, base + FSPI_IPRXFCR);
  705. /* invalid RXFIFO first */
  706. reg &= ~FSPI_IPRXFCR_DMA_EN;
  707. reg = reg | FSPI_IPRXFCR_CLR;
  708. fspi_writel(f, reg, base + FSPI_IPRXFCR);
  709. init_completion(&f->c);
  710. fspi_writel(f, op->addr.val, base + FSPI_IPCR0);
  711. /*
  712. * Always start the sequence at the same index since we update
  713. * the LUT at each exec_op() call. And also specify the DATA
  714. * length, since it's has not been specified in the LUT.
  715. */
  716. fspi_writel(f, op->data.nbytes |
  717. (SEQID_LUT << FSPI_IPCR1_SEQID_SHIFT) |
  718. (seqnum << FSPI_IPCR1_SEQNUM_SHIFT),
  719. base + FSPI_IPCR1);
  720. /* Trigger the LUT now. */
  721. fspi_writel(f, FSPI_IPCMD_TRG, base + FSPI_IPCMD);
  722. /* Wait for the interrupt. */
  723. if (!wait_for_completion_timeout(&f->c, msecs_to_jiffies(1000)))
  724. err = -ETIMEDOUT;
  725. /* Invoke IP data read, if request is of data read. */
  726. if (!err && op->data.nbytes && op->data.dir == SPI_MEM_DATA_IN)
  727. nxp_fspi_read_rxfifo(f, op);
  728. return err;
  729. }
  730. static int nxp_fspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
  731. {
  732. struct nxp_fspi *f = spi_controller_get_devdata(mem->spi->master);
  733. int err = 0;
  734. mutex_lock(&f->lock);
  735. /* Wait for controller being ready. */
  736. err = fspi_readl_poll_tout(f, f->iobase + FSPI_STS0,
  737. FSPI_STS0_ARB_IDLE, 1, POLL_TOUT, true);
  738. WARN_ON(err);
  739. nxp_fspi_select_mem(f, mem->spi);
  740. nxp_fspi_prepare_lut(f, op);
  741. /*
  742. * If we have large chunks of data, we read them through the AHB bus by
  743. * accessing the mapped memory. In all other cases we use IP commands
  744. * to access the flash. Read via AHB bus may be corrupted due to
  745. * existence of an errata and therefore discard AHB read in such cases.
  746. */
  747. if (op->data.nbytes > (f->devtype_data->rxfifo - 4) &&
  748. op->data.dir == SPI_MEM_DATA_IN &&
  749. !needs_ip_only(f)) {
  750. err = nxp_fspi_read_ahb(f, op);
  751. } else {
  752. if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT)
  753. nxp_fspi_fill_txfifo(f, op);
  754. err = nxp_fspi_do_op(f, op);
  755. }
  756. /* Invalidate the data in the AHB buffer. */
  757. nxp_fspi_invalid(f);
  758. mutex_unlock(&f->lock);
  759. return err;
  760. }
  761. static int nxp_fspi_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
  762. {
  763. struct nxp_fspi *f = spi_controller_get_devdata(mem->spi->master);
  764. if (op->data.dir == SPI_MEM_DATA_OUT) {
  765. if (op->data.nbytes > f->devtype_data->txfifo)
  766. op->data.nbytes = f->devtype_data->txfifo;
  767. } else {
  768. if (op->data.nbytes > f->devtype_data->ahb_buf_size)
  769. op->data.nbytes = f->devtype_data->ahb_buf_size;
  770. else if (op->data.nbytes > (f->devtype_data->rxfifo - 4))
  771. op->data.nbytes = ALIGN_DOWN(op->data.nbytes, 8);
  772. }
  773. /* Limit data bytes to RX FIFO in case of IP read only */
  774. if (op->data.dir == SPI_MEM_DATA_IN &&
  775. needs_ip_only(f) &&
  776. op->data.nbytes > f->devtype_data->rxfifo)
  777. op->data.nbytes = f->devtype_data->rxfifo;
  778. return 0;
  779. }
  780. static void erratum_err050568(struct nxp_fspi *f)
  781. {
  782. const struct soc_device_attribute ls1028a_soc_attr[] = {
  783. { .family = "QorIQ LS1028A" },
  784. { /* sentinel */ }
  785. };
  786. struct regmap *map;
  787. u32 val, sys_pll_ratio;
  788. int ret;
  789. /* Check for LS1028A family */
  790. if (!soc_device_match(ls1028a_soc_attr)) {
  791. dev_dbg(f->dev, "Errata applicable only for LS1028A\n");
  792. return;
  793. }
  794. map = syscon_regmap_lookup_by_compatible("fsl,ls1028a-dcfg");
  795. if (IS_ERR(map)) {
  796. dev_err(f->dev, "No syscon regmap\n");
  797. goto err;
  798. }
  799. ret = regmap_read(map, DCFG_RCWSR1, &val);
  800. if (ret < 0)
  801. goto err;
  802. sys_pll_ratio = FIELD_GET(SYS_PLL_RAT, val);
  803. dev_dbg(f->dev, "val: 0x%08x, sys_pll_ratio: %d\n", val, sys_pll_ratio);
  804. /* Use IP bus only if platform clock is 300MHz */
  805. if (sys_pll_ratio == 3)
  806. f->devtype_data->quirks |= FSPI_QUIRK_USE_IP_ONLY;
  807. return;
  808. err:
  809. dev_err(f->dev, "Errata cannot be executed. Read via IP bus may not work\n");
  810. }
  811. static int nxp_fspi_default_setup(struct nxp_fspi *f)
  812. {
  813. void __iomem *base = f->iobase;
  814. int ret, i;
  815. u32 reg;
  816. /* disable and unprepare clock to avoid glitch pass to controller */
  817. nxp_fspi_clk_disable_unprep(f);
  818. /* the default frequency, we will change it later if necessary. */
  819. ret = clk_set_rate(f->clk, 20000000);
  820. if (ret)
  821. return ret;
  822. ret = nxp_fspi_clk_prep_enable(f);
  823. if (ret)
  824. return ret;
  825. /*
  826. * ERR050568: Flash access by FlexSPI AHB command may not work with
  827. * platform frequency equal to 300 MHz on LS1028A.
  828. * LS1028A reuses LX2160A compatible entry. Make errata applicable for
  829. * Layerscape LS1028A platform.
  830. */
  831. if (of_device_is_compatible(f->dev->of_node, "nxp,lx2160a-fspi"))
  832. erratum_err050568(f);
  833. /* Reset the module */
  834. /* w1c register, wait unit clear */
  835. ret = fspi_readl_poll_tout(f, f->iobase + FSPI_MCR0,
  836. FSPI_MCR0_SWRST, 0, POLL_TOUT, false);
  837. WARN_ON(ret);
  838. /* Disable the module */
  839. fspi_writel(f, FSPI_MCR0_MDIS, base + FSPI_MCR0);
  840. /* Reset the DLL register to default value */
  841. fspi_writel(f, FSPI_DLLACR_OVRDEN, base + FSPI_DLLACR);
  842. fspi_writel(f, FSPI_DLLBCR_OVRDEN, base + FSPI_DLLBCR);
  843. /* enable module */
  844. fspi_writel(f, FSPI_MCR0_AHB_TIMEOUT(0xFF) |
  845. FSPI_MCR0_IP_TIMEOUT(0xFF) | (u32) FSPI_MCR0_OCTCOMB_EN,
  846. base + FSPI_MCR0);
  847. /*
  848. * Disable same device enable bit and configure all slave devices
  849. * independently.
  850. */
  851. reg = fspi_readl(f, f->iobase + FSPI_MCR2);
  852. reg = reg & ~(FSPI_MCR2_SAMEDEVICEEN);
  853. fspi_writel(f, reg, base + FSPI_MCR2);
  854. /* AHB configuration for access buffer 0~7. */
  855. for (i = 0; i < 7; i++)
  856. fspi_writel(f, 0, base + FSPI_AHBRX_BUF0CR0 + 4 * i);
  857. /*
  858. * Set ADATSZ with the maximum AHB buffer size to improve the read
  859. * performance.
  860. */
  861. fspi_writel(f, (f->devtype_data->ahb_buf_size / 8 |
  862. FSPI_AHBRXBUF0CR7_PREF), base + FSPI_AHBRX_BUF7CR0);
  863. /* prefetch and no start address alignment limitation */
  864. fspi_writel(f, FSPI_AHBCR_PREF_EN | FSPI_AHBCR_RDADDROPT,
  865. base + FSPI_AHBCR);
  866. /* Reset the FLSHxCR1 registers. */
  867. reg = FSPI_FLSHXCR1_TCSH(0x3) | FSPI_FLSHXCR1_TCSS(0x3);
  868. fspi_writel(f, reg, base + FSPI_FLSHA1CR1);
  869. fspi_writel(f, reg, base + FSPI_FLSHA2CR1);
  870. fspi_writel(f, reg, base + FSPI_FLSHB1CR1);
  871. fspi_writel(f, reg, base + FSPI_FLSHB2CR1);
  872. /* AHB Read - Set lut sequence ID for all CS. */
  873. fspi_writel(f, SEQID_LUT, base + FSPI_FLSHA1CR2);
  874. fspi_writel(f, SEQID_LUT, base + FSPI_FLSHA2CR2);
  875. fspi_writel(f, SEQID_LUT, base + FSPI_FLSHB1CR2);
  876. fspi_writel(f, SEQID_LUT, base + FSPI_FLSHB2CR2);
  877. f->selected = -1;
  878. /* enable the interrupt */
  879. fspi_writel(f, FSPI_INTEN_IPCMDDONE, base + FSPI_INTEN);
  880. return 0;
  881. }
  882. static const char *nxp_fspi_get_name(struct spi_mem *mem)
  883. {
  884. struct nxp_fspi *f = spi_controller_get_devdata(mem->spi->master);
  885. struct device *dev = &mem->spi->dev;
  886. const char *name;
  887. // Set custom name derived from the platform_device of the controller.
  888. if (of_get_available_child_count(f->dev->of_node) == 1)
  889. return dev_name(f->dev);
  890. name = devm_kasprintf(dev, GFP_KERNEL,
  891. "%s-%d", dev_name(f->dev),
  892. mem->spi->chip_select);
  893. if (!name) {
  894. dev_err(dev, "failed to get memory for custom flash name\n");
  895. return ERR_PTR(-ENOMEM);
  896. }
  897. return name;
  898. }
  899. static const struct spi_controller_mem_ops nxp_fspi_mem_ops = {
  900. .adjust_op_size = nxp_fspi_adjust_op_size,
  901. .supports_op = nxp_fspi_supports_op,
  902. .exec_op = nxp_fspi_exec_op,
  903. .get_name = nxp_fspi_get_name,
  904. };
  905. static int nxp_fspi_probe(struct platform_device *pdev)
  906. {
  907. struct spi_controller *ctlr;
  908. struct device *dev = &pdev->dev;
  909. struct device_node *np = dev->of_node;
  910. struct resource *res;
  911. struct nxp_fspi *f;
  912. int ret;
  913. u32 reg;
  914. ctlr = spi_alloc_master(&pdev->dev, sizeof(*f));
  915. if (!ctlr)
  916. return -ENOMEM;
  917. ctlr->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL |
  918. SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL;
  919. f = spi_controller_get_devdata(ctlr);
  920. f->dev = dev;
  921. f->devtype_data = (struct nxp_fspi_devtype_data *)device_get_match_data(dev);
  922. if (!f->devtype_data) {
  923. ret = -ENODEV;
  924. goto err_put_ctrl;
  925. }
  926. platform_set_drvdata(pdev, f);
  927. /* find the resources - configuration register address space */
  928. if (is_acpi_node(dev_fwnode(f->dev)))
  929. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  930. else
  931. res = platform_get_resource_byname(pdev,
  932. IORESOURCE_MEM, "fspi_base");
  933. f->iobase = devm_ioremap_resource(dev, res);
  934. if (IS_ERR(f->iobase)) {
  935. ret = PTR_ERR(f->iobase);
  936. goto err_put_ctrl;
  937. }
  938. /* find the resources - controller memory mapped space */
  939. if (is_acpi_node(dev_fwnode(f->dev)))
  940. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  941. else
  942. res = platform_get_resource_byname(pdev,
  943. IORESOURCE_MEM, "fspi_mmap");
  944. if (!res) {
  945. ret = -ENODEV;
  946. goto err_put_ctrl;
  947. }
  948. /* assign memory mapped starting address and mapped size. */
  949. f->memmap_phy = res->start;
  950. f->memmap_phy_size = resource_size(res);
  951. /* find the clocks */
  952. if (dev_of_node(&pdev->dev)) {
  953. f->clk_en = devm_clk_get(dev, "fspi_en");
  954. if (IS_ERR(f->clk_en)) {
  955. ret = PTR_ERR(f->clk_en);
  956. goto err_put_ctrl;
  957. }
  958. f->clk = devm_clk_get(dev, "fspi");
  959. if (IS_ERR(f->clk)) {
  960. ret = PTR_ERR(f->clk);
  961. goto err_put_ctrl;
  962. }
  963. ret = nxp_fspi_clk_prep_enable(f);
  964. if (ret) {
  965. dev_err(dev, "can not enable the clock\n");
  966. goto err_put_ctrl;
  967. }
  968. }
  969. /* Clear potential interrupts */
  970. reg = fspi_readl(f, f->iobase + FSPI_INTR);
  971. if (reg)
  972. fspi_writel(f, reg, f->iobase + FSPI_INTR);
  973. /* find the irq */
  974. ret = platform_get_irq(pdev, 0);
  975. if (ret < 0)
  976. goto err_disable_clk;
  977. ret = devm_request_irq(dev, ret,
  978. nxp_fspi_irq_handler, 0, pdev->name, f);
  979. if (ret) {
  980. dev_err(dev, "failed to request irq: %d\n", ret);
  981. goto err_disable_clk;
  982. }
  983. mutex_init(&f->lock);
  984. ctlr->bus_num = -1;
  985. ctlr->num_chipselect = NXP_FSPI_MAX_CHIPSELECT;
  986. ctlr->mem_ops = &nxp_fspi_mem_ops;
  987. nxp_fspi_default_setup(f);
  988. ctlr->dev.of_node = np;
  989. ret = devm_spi_register_controller(&pdev->dev, ctlr);
  990. if (ret)
  991. goto err_destroy_mutex;
  992. return 0;
  993. err_destroy_mutex:
  994. mutex_destroy(&f->lock);
  995. err_disable_clk:
  996. nxp_fspi_clk_disable_unprep(f);
  997. err_put_ctrl:
  998. spi_controller_put(ctlr);
  999. dev_err(dev, "NXP FSPI probe failed\n");
  1000. return ret;
  1001. }
  1002. static int nxp_fspi_remove(struct platform_device *pdev)
  1003. {
  1004. struct nxp_fspi *f = platform_get_drvdata(pdev);
  1005. /* disable the hardware */
  1006. fspi_writel(f, FSPI_MCR0_MDIS, f->iobase + FSPI_MCR0);
  1007. nxp_fspi_clk_disable_unprep(f);
  1008. mutex_destroy(&f->lock);
  1009. if (f->ahb_addr)
  1010. iounmap(f->ahb_addr);
  1011. return 0;
  1012. }
  1013. static int nxp_fspi_suspend(struct device *dev)
  1014. {
  1015. return 0;
  1016. }
  1017. static int nxp_fspi_resume(struct device *dev)
  1018. {
  1019. struct nxp_fspi *f = dev_get_drvdata(dev);
  1020. nxp_fspi_default_setup(f);
  1021. return 0;
  1022. }
  1023. static const struct of_device_id nxp_fspi_dt_ids[] = {
  1024. { .compatible = "nxp,lx2160a-fspi", .data = (void *)&lx2160a_data, },
  1025. { .compatible = "nxp,imx8mm-fspi", .data = (void *)&imx8mm_data, },
  1026. { .compatible = "nxp,imx8mp-fspi", .data = (void *)&imx8mm_data, },
  1027. { .compatible = "nxp,imx8qxp-fspi", .data = (void *)&imx8qxp_data, },
  1028. { .compatible = "nxp,imx8dxl-fspi", .data = (void *)&imx8dxl_data, },
  1029. { /* sentinel */ }
  1030. };
  1031. MODULE_DEVICE_TABLE(of, nxp_fspi_dt_ids);
  1032. #ifdef CONFIG_ACPI
  1033. static const struct acpi_device_id nxp_fspi_acpi_ids[] = {
  1034. { "NXP0009", .driver_data = (kernel_ulong_t)&lx2160a_data, },
  1035. {}
  1036. };
  1037. MODULE_DEVICE_TABLE(acpi, nxp_fspi_acpi_ids);
  1038. #endif
  1039. static const struct dev_pm_ops nxp_fspi_pm_ops = {
  1040. .suspend = nxp_fspi_suspend,
  1041. .resume = nxp_fspi_resume,
  1042. };
  1043. static struct platform_driver nxp_fspi_driver = {
  1044. .driver = {
  1045. .name = "nxp-fspi",
  1046. .of_match_table = nxp_fspi_dt_ids,
  1047. .acpi_match_table = ACPI_PTR(nxp_fspi_acpi_ids),
  1048. .pm = &nxp_fspi_pm_ops,
  1049. },
  1050. .probe = nxp_fspi_probe,
  1051. .remove = nxp_fspi_remove,
  1052. };
  1053. module_platform_driver(nxp_fspi_driver);
  1054. MODULE_DESCRIPTION("NXP FSPI Controller Driver");
  1055. MODULE_AUTHOR("NXP Semiconductor");
  1056. MODULE_AUTHOR("Yogesh Narayan Gaur <[email protected]>");
  1057. MODULE_AUTHOR("Boris Brezillon <[email protected]>");
  1058. MODULE_AUTHOR("Frieder Schrempf <[email protected]>");
  1059. MODULE_LICENSE("GPL v2");