spi-imx.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
  3. // Copyright (C) 2008 Juergen Beisert
  4. #include <linux/bits.h>
  5. #include <linux/clk.h>
  6. #include <linux/completion.h>
  7. #include <linux/delay.h>
  8. #include <linux/dmaengine.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/err.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/io.h>
  13. #include <linux/irq.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/pinctrl/consumer.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/slab.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/types.h>
  22. #include <linux/of.h>
  23. #include <linux/property.h>
  24. #include <linux/dma/imx-dma.h>
  25. #define DRIVER_NAME "spi_imx"
  26. static bool use_dma = true;
  27. module_param(use_dma, bool, 0644);
  28. MODULE_PARM_DESC(use_dma, "Enable usage of DMA when available (default)");
  29. /* define polling limits */
  30. static unsigned int polling_limit_us = 30;
  31. module_param(polling_limit_us, uint, 0664);
  32. MODULE_PARM_DESC(polling_limit_us,
  33. "time in us to run a transfer in polling mode\n");
  34. #define MXC_RPM_TIMEOUT 2000 /* 2000ms */
  35. #define MXC_CSPIRXDATA 0x00
  36. #define MXC_CSPITXDATA 0x04
  37. #define MXC_CSPICTRL 0x08
  38. #define MXC_CSPIINT 0x0c
  39. #define MXC_RESET 0x1c
  40. /* generic defines to abstract from the different register layouts */
  41. #define MXC_INT_RR (1 << 0) /* Receive data ready interrupt */
  42. #define MXC_INT_TE (1 << 1) /* Transmit FIFO empty interrupt */
  43. #define MXC_INT_RDR BIT(4) /* Receive date threshold interrupt */
  44. /* The maximum bytes that a sdma BD can transfer. */
  45. #define MAX_SDMA_BD_BYTES (1 << 15)
  46. #define MX51_ECSPI_CTRL_MAX_BURST 512
  47. /* The maximum bytes that IMX53_ECSPI can transfer in target mode.*/
  48. #define MX53_MAX_TRANSFER_BYTES 512
  49. enum spi_imx_devtype {
  50. IMX1_CSPI,
  51. IMX21_CSPI,
  52. IMX27_CSPI,
  53. IMX31_CSPI,
  54. IMX35_CSPI, /* CSPI on all i.mx except above */
  55. IMX51_ECSPI, /* ECSPI on i.mx51 */
  56. IMX53_ECSPI, /* ECSPI on i.mx53 and later */
  57. };
  58. struct spi_imx_data;
  59. struct spi_imx_devtype_data {
  60. void (*intctrl)(struct spi_imx_data *spi_imx, int enable);
  61. int (*prepare_message)(struct spi_imx_data *spi_imx, struct spi_message *msg);
  62. int (*prepare_transfer)(struct spi_imx_data *spi_imx, struct spi_device *spi);
  63. void (*trigger)(struct spi_imx_data *spi_imx);
  64. int (*rx_available)(struct spi_imx_data *spi_imx);
  65. void (*reset)(struct spi_imx_data *spi_imx);
  66. void (*setup_wml)(struct spi_imx_data *spi_imx);
  67. void (*disable)(struct spi_imx_data *spi_imx);
  68. bool has_dmamode;
  69. bool has_targetmode;
  70. unsigned int fifo_size;
  71. bool dynamic_burst;
  72. /*
  73. * ERR009165 fixed or not:
  74. * https://www.nxp.com/docs/en/errata/IMX6DQCE.pdf
  75. */
  76. bool tx_glitch_fixed;
  77. enum spi_imx_devtype devtype;
  78. };
  79. struct spi_imx_data {
  80. struct spi_controller *controller;
  81. struct device *dev;
  82. struct completion xfer_done;
  83. void __iomem *base;
  84. unsigned long base_phys;
  85. struct clk *clk_per;
  86. struct clk *clk_ipg;
  87. unsigned long spi_clk;
  88. unsigned int spi_bus_clk;
  89. unsigned int bits_per_word;
  90. unsigned int spi_drctl;
  91. unsigned int count, remainder;
  92. void (*tx)(struct spi_imx_data *spi_imx);
  93. void (*rx)(struct spi_imx_data *spi_imx);
  94. void *rx_buf;
  95. const void *tx_buf;
  96. unsigned int txfifo; /* number of words pushed in tx FIFO */
  97. unsigned int dynamic_burst;
  98. bool rx_only;
  99. /* Target mode */
  100. bool target_mode;
  101. bool target_aborted;
  102. unsigned int target_burst;
  103. /* DMA */
  104. bool usedma;
  105. u32 wml;
  106. struct completion dma_rx_completion;
  107. struct completion dma_tx_completion;
  108. const struct spi_imx_devtype_data *devtype_data;
  109. };
  110. static inline int is_imx27_cspi(struct spi_imx_data *d)
  111. {
  112. return d->devtype_data->devtype == IMX27_CSPI;
  113. }
  114. static inline int is_imx35_cspi(struct spi_imx_data *d)
  115. {
  116. return d->devtype_data->devtype == IMX35_CSPI;
  117. }
  118. static inline int is_imx51_ecspi(struct spi_imx_data *d)
  119. {
  120. return d->devtype_data->devtype == IMX51_ECSPI;
  121. }
  122. static inline int is_imx53_ecspi(struct spi_imx_data *d)
  123. {
  124. return d->devtype_data->devtype == IMX53_ECSPI;
  125. }
  126. #define MXC_SPI_BUF_RX(type) \
  127. static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx) \
  128. { \
  129. unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); \
  130. \
  131. if (spi_imx->rx_buf) { \
  132. *(type *)spi_imx->rx_buf = val; \
  133. spi_imx->rx_buf += sizeof(type); \
  134. } \
  135. \
  136. spi_imx->remainder -= sizeof(type); \
  137. }
  138. #define MXC_SPI_BUF_TX(type) \
  139. static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx) \
  140. { \
  141. type val = 0; \
  142. \
  143. if (spi_imx->tx_buf) { \
  144. val = *(type *)spi_imx->tx_buf; \
  145. spi_imx->tx_buf += sizeof(type); \
  146. } \
  147. \
  148. spi_imx->count -= sizeof(type); \
  149. \
  150. writel(val, spi_imx->base + MXC_CSPITXDATA); \
  151. }
  152. MXC_SPI_BUF_RX(u8)
  153. MXC_SPI_BUF_TX(u8)
  154. MXC_SPI_BUF_RX(u16)
  155. MXC_SPI_BUF_TX(u16)
  156. MXC_SPI_BUF_RX(u32)
  157. MXC_SPI_BUF_TX(u32)
  158. /* First entry is reserved, second entry is valid only if SDHC_SPIEN is set
  159. * (which is currently not the case in this driver)
  160. */
  161. static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
  162. 256, 384, 512, 768, 1024};
  163. /* MX21, MX27 */
  164. static unsigned int spi_imx_clkdiv_1(unsigned int fin,
  165. unsigned int fspi, unsigned int max, unsigned int *fres)
  166. {
  167. int i;
  168. for (i = 2; i < max; i++)
  169. if (fspi * mxc_clkdivs[i] >= fin)
  170. break;
  171. *fres = fin / mxc_clkdivs[i];
  172. return i;
  173. }
  174. /* MX1, MX31, MX35, MX51 CSPI */
  175. static unsigned int spi_imx_clkdiv_2(unsigned int fin,
  176. unsigned int fspi, unsigned int *fres)
  177. {
  178. int i, div = 4;
  179. for (i = 0; i < 7; i++) {
  180. if (fspi * div >= fin)
  181. goto out;
  182. div <<= 1;
  183. }
  184. out:
  185. *fres = fin / div;
  186. return i;
  187. }
  188. static int spi_imx_bytes_per_word(const int bits_per_word)
  189. {
  190. if (bits_per_word <= 8)
  191. return 1;
  192. else if (bits_per_word <= 16)
  193. return 2;
  194. else
  195. return 4;
  196. }
  197. static bool spi_imx_can_dma(struct spi_controller *controller, struct spi_device *spi,
  198. struct spi_transfer *transfer)
  199. {
  200. struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
  201. if (!use_dma || controller->fallback)
  202. return false;
  203. if (!controller->dma_rx)
  204. return false;
  205. if (spi_imx->target_mode)
  206. return false;
  207. if (transfer->len < spi_imx->devtype_data->fifo_size)
  208. return false;
  209. spi_imx->dynamic_burst = 0;
  210. return true;
  211. }
  212. /*
  213. * Note the number of natively supported chip selects for MX51 is 4. Some
  214. * devices may have less actual SS pins but the register map supports 4. When
  215. * using gpio chip selects the cs values passed into the macros below can go
  216. * outside the range 0 - 3. We therefore need to limit the cs value to avoid
  217. * corrupting bits outside the allocated locations.
  218. *
  219. * The simplest way to do this is to just mask the cs bits to 2 bits. This
  220. * still allows all 4 native chip selects to work as well as gpio chip selects
  221. * (which can use any of the 4 chip select configurations).
  222. */
  223. #define MX51_ECSPI_CTRL 0x08
  224. #define MX51_ECSPI_CTRL_ENABLE (1 << 0)
  225. #define MX51_ECSPI_CTRL_XCH (1 << 2)
  226. #define MX51_ECSPI_CTRL_SMC (1 << 3)
  227. #define MX51_ECSPI_CTRL_MODE_MASK (0xf << 4)
  228. #define MX51_ECSPI_CTRL_DRCTL(drctl) ((drctl) << 16)
  229. #define MX51_ECSPI_CTRL_POSTDIV_OFFSET 8
  230. #define MX51_ECSPI_CTRL_PREDIV_OFFSET 12
  231. #define MX51_ECSPI_CTRL_CS(cs) ((cs & 3) << 18)
  232. #define MX51_ECSPI_CTRL_BL_OFFSET 20
  233. #define MX51_ECSPI_CTRL_BL_MASK (0xfff << 20)
  234. #define MX51_ECSPI_CONFIG 0x0c
  235. #define MX51_ECSPI_CONFIG_SCLKPHA(cs) (1 << ((cs & 3) + 0))
  236. #define MX51_ECSPI_CONFIG_SCLKPOL(cs) (1 << ((cs & 3) + 4))
  237. #define MX51_ECSPI_CONFIG_SBBCTRL(cs) (1 << ((cs & 3) + 8))
  238. #define MX51_ECSPI_CONFIG_SSBPOL(cs) (1 << ((cs & 3) + 12))
  239. #define MX51_ECSPI_CONFIG_DATACTL(cs) (1 << ((cs & 3) + 16))
  240. #define MX51_ECSPI_CONFIG_SCLKCTL(cs) (1 << ((cs & 3) + 20))
  241. #define MX51_ECSPI_INT 0x10
  242. #define MX51_ECSPI_INT_TEEN (1 << 0)
  243. #define MX51_ECSPI_INT_RREN (1 << 3)
  244. #define MX51_ECSPI_INT_RDREN (1 << 4)
  245. #define MX51_ECSPI_DMA 0x14
  246. #define MX51_ECSPI_DMA_TX_WML(wml) ((wml) & 0x3f)
  247. #define MX51_ECSPI_DMA_RX_WML(wml) (((wml) & 0x3f) << 16)
  248. #define MX51_ECSPI_DMA_RXT_WML(wml) (((wml) & 0x3f) << 24)
  249. #define MX51_ECSPI_DMA_TEDEN (1 << 7)
  250. #define MX51_ECSPI_DMA_RXDEN (1 << 23)
  251. #define MX51_ECSPI_DMA_RXTDEN (1 << 31)
  252. #define MX51_ECSPI_STAT 0x18
  253. #define MX51_ECSPI_STAT_RR (1 << 3)
  254. #define MX51_ECSPI_TESTREG 0x20
  255. #define MX51_ECSPI_TESTREG_LBC BIT(31)
  256. static void spi_imx_buf_rx_swap_u32(struct spi_imx_data *spi_imx)
  257. {
  258. unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA);
  259. if (spi_imx->rx_buf) {
  260. #ifdef __LITTLE_ENDIAN
  261. unsigned int bytes_per_word;
  262. bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
  263. if (bytes_per_word == 1)
  264. swab32s(&val);
  265. else if (bytes_per_word == 2)
  266. swahw32s(&val);
  267. #endif
  268. *(u32 *)spi_imx->rx_buf = val;
  269. spi_imx->rx_buf += sizeof(u32);
  270. }
  271. spi_imx->remainder -= sizeof(u32);
  272. }
  273. static void spi_imx_buf_rx_swap(struct spi_imx_data *spi_imx)
  274. {
  275. int unaligned;
  276. u32 val;
  277. unaligned = spi_imx->remainder % 4;
  278. if (!unaligned) {
  279. spi_imx_buf_rx_swap_u32(spi_imx);
  280. return;
  281. }
  282. if (spi_imx_bytes_per_word(spi_imx->bits_per_word) == 2) {
  283. spi_imx_buf_rx_u16(spi_imx);
  284. return;
  285. }
  286. val = readl(spi_imx->base + MXC_CSPIRXDATA);
  287. while (unaligned--) {
  288. if (spi_imx->rx_buf) {
  289. *(u8 *)spi_imx->rx_buf = (val >> (8 * unaligned)) & 0xff;
  290. spi_imx->rx_buf++;
  291. }
  292. spi_imx->remainder--;
  293. }
  294. }
  295. static void spi_imx_buf_tx_swap_u32(struct spi_imx_data *spi_imx)
  296. {
  297. u32 val = 0;
  298. #ifdef __LITTLE_ENDIAN
  299. unsigned int bytes_per_word;
  300. #endif
  301. if (spi_imx->tx_buf) {
  302. val = *(u32 *)spi_imx->tx_buf;
  303. spi_imx->tx_buf += sizeof(u32);
  304. }
  305. spi_imx->count -= sizeof(u32);
  306. #ifdef __LITTLE_ENDIAN
  307. bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
  308. if (bytes_per_word == 1)
  309. swab32s(&val);
  310. else if (bytes_per_word == 2)
  311. swahw32s(&val);
  312. #endif
  313. writel(val, spi_imx->base + MXC_CSPITXDATA);
  314. }
  315. static void spi_imx_buf_tx_swap(struct spi_imx_data *spi_imx)
  316. {
  317. int unaligned;
  318. u32 val = 0;
  319. unaligned = spi_imx->count % 4;
  320. if (!unaligned) {
  321. spi_imx_buf_tx_swap_u32(spi_imx);
  322. return;
  323. }
  324. if (spi_imx_bytes_per_word(spi_imx->bits_per_word) == 2) {
  325. spi_imx_buf_tx_u16(spi_imx);
  326. return;
  327. }
  328. while (unaligned--) {
  329. if (spi_imx->tx_buf) {
  330. val |= *(u8 *)spi_imx->tx_buf << (8 * unaligned);
  331. spi_imx->tx_buf++;
  332. }
  333. spi_imx->count--;
  334. }
  335. writel(val, spi_imx->base + MXC_CSPITXDATA);
  336. }
  337. static void mx53_ecspi_rx_target(struct spi_imx_data *spi_imx)
  338. {
  339. u32 val = be32_to_cpu(readl(spi_imx->base + MXC_CSPIRXDATA));
  340. if (spi_imx->rx_buf) {
  341. int n_bytes = spi_imx->target_burst % sizeof(val);
  342. if (!n_bytes)
  343. n_bytes = sizeof(val);
  344. memcpy(spi_imx->rx_buf,
  345. ((u8 *)&val) + sizeof(val) - n_bytes, n_bytes);
  346. spi_imx->rx_buf += n_bytes;
  347. spi_imx->target_burst -= n_bytes;
  348. }
  349. spi_imx->remainder -= sizeof(u32);
  350. }
  351. static void mx53_ecspi_tx_target(struct spi_imx_data *spi_imx)
  352. {
  353. u32 val = 0;
  354. int n_bytes = spi_imx->count % sizeof(val);
  355. if (!n_bytes)
  356. n_bytes = sizeof(val);
  357. if (spi_imx->tx_buf) {
  358. memcpy(((u8 *)&val) + sizeof(val) - n_bytes,
  359. spi_imx->tx_buf, n_bytes);
  360. val = cpu_to_be32(val);
  361. spi_imx->tx_buf += n_bytes;
  362. }
  363. spi_imx->count -= n_bytes;
  364. writel(val, spi_imx->base + MXC_CSPITXDATA);
  365. }
  366. /* MX51 eCSPI */
  367. static unsigned int mx51_ecspi_clkdiv(struct spi_imx_data *spi_imx,
  368. unsigned int fspi, unsigned int *fres)
  369. {
  370. /*
  371. * there are two 4-bit dividers, the pre-divider divides by
  372. * $pre, the post-divider by 2^$post
  373. */
  374. unsigned int pre, post;
  375. unsigned int fin = spi_imx->spi_clk;
  376. fspi = min(fspi, fin);
  377. post = fls(fin) - fls(fspi);
  378. if (fin > fspi << post)
  379. post++;
  380. /* now we have: (fin <= fspi << post) with post being minimal */
  381. post = max(4U, post) - 4;
  382. if (unlikely(post > 0xf)) {
  383. dev_err(spi_imx->dev, "cannot set clock freq: %u (base freq: %u)\n",
  384. fspi, fin);
  385. return 0xff;
  386. }
  387. pre = DIV_ROUND_UP(fin, fspi << post) - 1;
  388. dev_dbg(spi_imx->dev, "%s: fin: %u, fspi: %u, post: %u, pre: %u\n",
  389. __func__, fin, fspi, post, pre);
  390. /* Resulting frequency for the SCLK line. */
  391. *fres = (fin / (pre + 1)) >> post;
  392. return (pre << MX51_ECSPI_CTRL_PREDIV_OFFSET) |
  393. (post << MX51_ECSPI_CTRL_POSTDIV_OFFSET);
  394. }
  395. static void mx51_ecspi_intctrl(struct spi_imx_data *spi_imx, int enable)
  396. {
  397. unsigned int val = 0;
  398. if (enable & MXC_INT_TE)
  399. val |= MX51_ECSPI_INT_TEEN;
  400. if (enable & MXC_INT_RR)
  401. val |= MX51_ECSPI_INT_RREN;
  402. if (enable & MXC_INT_RDR)
  403. val |= MX51_ECSPI_INT_RDREN;
  404. writel(val, spi_imx->base + MX51_ECSPI_INT);
  405. }
  406. static void mx51_ecspi_trigger(struct spi_imx_data *spi_imx)
  407. {
  408. u32 reg;
  409. reg = readl(spi_imx->base + MX51_ECSPI_CTRL);
  410. reg |= MX51_ECSPI_CTRL_XCH;
  411. writel(reg, spi_imx->base + MX51_ECSPI_CTRL);
  412. }
  413. static void mx51_ecspi_disable(struct spi_imx_data *spi_imx)
  414. {
  415. u32 ctrl;
  416. ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
  417. ctrl &= ~MX51_ECSPI_CTRL_ENABLE;
  418. writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
  419. }
  420. static int mx51_ecspi_channel(const struct spi_device *spi)
  421. {
  422. if (!spi_get_csgpiod(spi, 0))
  423. return spi_get_chipselect(spi, 0);
  424. return spi->controller->unused_native_cs;
  425. }
  426. static int mx51_ecspi_prepare_message(struct spi_imx_data *spi_imx,
  427. struct spi_message *msg)
  428. {
  429. struct spi_device *spi = msg->spi;
  430. struct spi_transfer *xfer;
  431. u32 ctrl = MX51_ECSPI_CTRL_ENABLE;
  432. u32 min_speed_hz = ~0U;
  433. u32 testreg, delay;
  434. u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
  435. u32 current_cfg = cfg;
  436. int channel = mx51_ecspi_channel(spi);
  437. /* set Host or Target mode */
  438. if (spi_imx->target_mode)
  439. ctrl &= ~MX51_ECSPI_CTRL_MODE_MASK;
  440. else
  441. ctrl |= MX51_ECSPI_CTRL_MODE_MASK;
  442. /*
  443. * Enable SPI_RDY handling (falling edge/level triggered).
  444. */
  445. if (spi->mode & SPI_READY)
  446. ctrl |= MX51_ECSPI_CTRL_DRCTL(spi_imx->spi_drctl);
  447. /* set chip select to use */
  448. ctrl |= MX51_ECSPI_CTRL_CS(channel);
  449. /*
  450. * The ctrl register must be written first, with the EN bit set other
  451. * registers must not be written to.
  452. */
  453. writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
  454. testreg = readl(spi_imx->base + MX51_ECSPI_TESTREG);
  455. if (spi->mode & SPI_LOOP)
  456. testreg |= MX51_ECSPI_TESTREG_LBC;
  457. else
  458. testreg &= ~MX51_ECSPI_TESTREG_LBC;
  459. writel(testreg, spi_imx->base + MX51_ECSPI_TESTREG);
  460. /*
  461. * eCSPI burst completion by Chip Select signal in Target mode
  462. * is not functional for imx53 Soc, config SPI burst completed when
  463. * BURST_LENGTH + 1 bits are received
  464. */
  465. if (spi_imx->target_mode && is_imx53_ecspi(spi_imx))
  466. cfg &= ~MX51_ECSPI_CONFIG_SBBCTRL(channel);
  467. else
  468. cfg |= MX51_ECSPI_CONFIG_SBBCTRL(channel);
  469. if (spi->mode & SPI_CPOL) {
  470. cfg |= MX51_ECSPI_CONFIG_SCLKPOL(channel);
  471. cfg |= MX51_ECSPI_CONFIG_SCLKCTL(channel);
  472. } else {
  473. cfg &= ~MX51_ECSPI_CONFIG_SCLKPOL(channel);
  474. cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL(channel);
  475. }
  476. if (spi->mode & SPI_MOSI_IDLE_LOW)
  477. cfg |= MX51_ECSPI_CONFIG_DATACTL(channel);
  478. else
  479. cfg &= ~MX51_ECSPI_CONFIG_DATACTL(channel);
  480. if (spi->mode & SPI_CS_HIGH)
  481. cfg |= MX51_ECSPI_CONFIG_SSBPOL(channel);
  482. else
  483. cfg &= ~MX51_ECSPI_CONFIG_SSBPOL(channel);
  484. if (cfg == current_cfg)
  485. return 0;
  486. writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
  487. /*
  488. * Wait until the changes in the configuration register CONFIGREG
  489. * propagate into the hardware. It takes exactly one tick of the
  490. * SCLK clock, but we will wait two SCLK clock just to be sure. The
  491. * effect of the delay it takes for the hardware to apply changes
  492. * is noticable if the SCLK clock run very slow. In such a case, if
  493. * the polarity of SCLK should be inverted, the GPIO ChipSelect might
  494. * be asserted before the SCLK polarity changes, which would disrupt
  495. * the SPI communication as the device on the other end would consider
  496. * the change of SCLK polarity as a clock tick already.
  497. *
  498. * Because spi_imx->spi_bus_clk is only set in prepare_message
  499. * callback, iterate over all the transfers in spi_message, find the
  500. * one with lowest bus frequency, and use that bus frequency for the
  501. * delay calculation. In case all transfers have speed_hz == 0, then
  502. * min_speed_hz is ~0 and the resulting delay is zero.
  503. */
  504. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  505. if (!xfer->speed_hz)
  506. continue;
  507. min_speed_hz = min(xfer->speed_hz, min_speed_hz);
  508. }
  509. delay = (2 * 1000000) / min_speed_hz;
  510. if (likely(delay < 10)) /* SCLK is faster than 200 kHz */
  511. udelay(delay);
  512. else /* SCLK is _very_ slow */
  513. usleep_range(delay, delay + 10);
  514. return 0;
  515. }
  516. static void mx51_configure_cpha(struct spi_imx_data *spi_imx,
  517. struct spi_device *spi)
  518. {
  519. bool cpha = (spi->mode & SPI_CPHA);
  520. bool flip_cpha = (spi->mode & SPI_RX_CPHA_FLIP) && spi_imx->rx_only;
  521. u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
  522. int channel = mx51_ecspi_channel(spi);
  523. /* Flip cpha logical value iff flip_cpha */
  524. cpha ^= flip_cpha;
  525. if (cpha)
  526. cfg |= MX51_ECSPI_CONFIG_SCLKPHA(channel);
  527. else
  528. cfg &= ~MX51_ECSPI_CONFIG_SCLKPHA(channel);
  529. writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
  530. }
  531. static int mx51_ecspi_prepare_transfer(struct spi_imx_data *spi_imx,
  532. struct spi_device *spi)
  533. {
  534. u32 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
  535. u32 clk;
  536. /* Clear BL field and set the right value */
  537. ctrl &= ~MX51_ECSPI_CTRL_BL_MASK;
  538. if (spi_imx->target_mode && is_imx53_ecspi(spi_imx))
  539. ctrl |= (spi_imx->target_burst * 8 - 1)
  540. << MX51_ECSPI_CTRL_BL_OFFSET;
  541. else {
  542. ctrl |= (spi_imx->bits_per_word - 1)
  543. << MX51_ECSPI_CTRL_BL_OFFSET;
  544. }
  545. /* set clock speed */
  546. ctrl &= ~(0xf << MX51_ECSPI_CTRL_POSTDIV_OFFSET |
  547. 0xf << MX51_ECSPI_CTRL_PREDIV_OFFSET);
  548. ctrl |= mx51_ecspi_clkdiv(spi_imx, spi_imx->spi_bus_clk, &clk);
  549. spi_imx->spi_bus_clk = clk;
  550. mx51_configure_cpha(spi_imx, spi);
  551. /*
  552. * ERR009165: work in XHC mode instead of SMC as PIO on the chips
  553. * before i.mx6ul.
  554. */
  555. if (spi_imx->usedma && spi_imx->devtype_data->tx_glitch_fixed)
  556. ctrl |= MX51_ECSPI_CTRL_SMC;
  557. else
  558. ctrl &= ~MX51_ECSPI_CTRL_SMC;
  559. writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
  560. return 0;
  561. }
  562. static void mx51_setup_wml(struct spi_imx_data *spi_imx)
  563. {
  564. u32 tx_wml = 0;
  565. if (spi_imx->devtype_data->tx_glitch_fixed)
  566. tx_wml = spi_imx->wml;
  567. /*
  568. * Configure the DMA register: setup the watermark
  569. * and enable DMA request.
  570. */
  571. writel(MX51_ECSPI_DMA_RX_WML(spi_imx->wml - 1) |
  572. MX51_ECSPI_DMA_TX_WML(tx_wml) |
  573. MX51_ECSPI_DMA_RXT_WML(spi_imx->wml) |
  574. MX51_ECSPI_DMA_TEDEN | MX51_ECSPI_DMA_RXDEN |
  575. MX51_ECSPI_DMA_RXTDEN, spi_imx->base + MX51_ECSPI_DMA);
  576. }
  577. static int mx51_ecspi_rx_available(struct spi_imx_data *spi_imx)
  578. {
  579. return readl(spi_imx->base + MX51_ECSPI_STAT) & MX51_ECSPI_STAT_RR;
  580. }
  581. static void mx51_ecspi_reset(struct spi_imx_data *spi_imx)
  582. {
  583. /* drain receive buffer */
  584. while (mx51_ecspi_rx_available(spi_imx))
  585. readl(spi_imx->base + MXC_CSPIRXDATA);
  586. }
  587. #define MX31_INTREG_TEEN (1 << 0)
  588. #define MX31_INTREG_RREN (1 << 3)
  589. #define MX31_CSPICTRL_ENABLE (1 << 0)
  590. #define MX31_CSPICTRL_HOST (1 << 1)
  591. #define MX31_CSPICTRL_XCH (1 << 2)
  592. #define MX31_CSPICTRL_SMC (1 << 3)
  593. #define MX31_CSPICTRL_POL (1 << 4)
  594. #define MX31_CSPICTRL_PHA (1 << 5)
  595. #define MX31_CSPICTRL_SSCTL (1 << 6)
  596. #define MX31_CSPICTRL_SSPOL (1 << 7)
  597. #define MX31_CSPICTRL_BC_SHIFT 8
  598. #define MX35_CSPICTRL_BL_SHIFT 20
  599. #define MX31_CSPICTRL_CS_SHIFT 24
  600. #define MX35_CSPICTRL_CS_SHIFT 12
  601. #define MX31_CSPICTRL_DR_SHIFT 16
  602. #define MX31_CSPI_DMAREG 0x10
  603. #define MX31_DMAREG_RH_DEN (1<<4)
  604. #define MX31_DMAREG_TH_DEN (1<<1)
  605. #define MX31_CSPISTATUS 0x14
  606. #define MX31_STATUS_RR (1 << 3)
  607. #define MX31_CSPI_TESTREG 0x1C
  608. #define MX31_TEST_LBC (1 << 14)
  609. /* These functions also work for the i.MX35, but be aware that
  610. * the i.MX35 has a slightly different register layout for bits
  611. * we do not use here.
  612. */
  613. static void mx31_intctrl(struct spi_imx_data *spi_imx, int enable)
  614. {
  615. unsigned int val = 0;
  616. if (enable & MXC_INT_TE)
  617. val |= MX31_INTREG_TEEN;
  618. if (enable & MXC_INT_RR)
  619. val |= MX31_INTREG_RREN;
  620. writel(val, spi_imx->base + MXC_CSPIINT);
  621. }
  622. static void mx31_trigger(struct spi_imx_data *spi_imx)
  623. {
  624. unsigned int reg;
  625. reg = readl(spi_imx->base + MXC_CSPICTRL);
  626. reg |= MX31_CSPICTRL_XCH;
  627. writel(reg, spi_imx->base + MXC_CSPICTRL);
  628. }
  629. static int mx31_prepare_message(struct spi_imx_data *spi_imx,
  630. struct spi_message *msg)
  631. {
  632. return 0;
  633. }
  634. static int mx31_prepare_transfer(struct spi_imx_data *spi_imx,
  635. struct spi_device *spi)
  636. {
  637. unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_HOST;
  638. unsigned int clk;
  639. reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->spi_bus_clk, &clk) <<
  640. MX31_CSPICTRL_DR_SHIFT;
  641. spi_imx->spi_bus_clk = clk;
  642. if (is_imx35_cspi(spi_imx)) {
  643. reg |= (spi_imx->bits_per_word - 1) << MX35_CSPICTRL_BL_SHIFT;
  644. reg |= MX31_CSPICTRL_SSCTL;
  645. } else {
  646. reg |= (spi_imx->bits_per_word - 1) << MX31_CSPICTRL_BC_SHIFT;
  647. }
  648. if (spi->mode & SPI_CPHA)
  649. reg |= MX31_CSPICTRL_PHA;
  650. if (spi->mode & SPI_CPOL)
  651. reg |= MX31_CSPICTRL_POL;
  652. if (spi->mode & SPI_CS_HIGH)
  653. reg |= MX31_CSPICTRL_SSPOL;
  654. if (!spi_get_csgpiod(spi, 0))
  655. reg |= (spi_get_chipselect(spi, 0)) <<
  656. (is_imx35_cspi(spi_imx) ? MX35_CSPICTRL_CS_SHIFT :
  657. MX31_CSPICTRL_CS_SHIFT);
  658. if (spi_imx->usedma)
  659. reg |= MX31_CSPICTRL_SMC;
  660. writel(reg, spi_imx->base + MXC_CSPICTRL);
  661. reg = readl(spi_imx->base + MX31_CSPI_TESTREG);
  662. if (spi->mode & SPI_LOOP)
  663. reg |= MX31_TEST_LBC;
  664. else
  665. reg &= ~MX31_TEST_LBC;
  666. writel(reg, spi_imx->base + MX31_CSPI_TESTREG);
  667. if (spi_imx->usedma) {
  668. /*
  669. * configure DMA requests when RXFIFO is half full and
  670. * when TXFIFO is half empty
  671. */
  672. writel(MX31_DMAREG_RH_DEN | MX31_DMAREG_TH_DEN,
  673. spi_imx->base + MX31_CSPI_DMAREG);
  674. }
  675. return 0;
  676. }
  677. static int mx31_rx_available(struct spi_imx_data *spi_imx)
  678. {
  679. return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR;
  680. }
  681. static void mx31_reset(struct spi_imx_data *spi_imx)
  682. {
  683. /* drain receive buffer */
  684. while (readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR)
  685. readl(spi_imx->base + MXC_CSPIRXDATA);
  686. }
  687. #define MX21_INTREG_RR (1 << 4)
  688. #define MX21_INTREG_TEEN (1 << 9)
  689. #define MX21_INTREG_RREN (1 << 13)
  690. #define MX21_CSPICTRL_POL (1 << 5)
  691. #define MX21_CSPICTRL_PHA (1 << 6)
  692. #define MX21_CSPICTRL_SSPOL (1 << 8)
  693. #define MX21_CSPICTRL_XCH (1 << 9)
  694. #define MX21_CSPICTRL_ENABLE (1 << 10)
  695. #define MX21_CSPICTRL_HOST (1 << 11)
  696. #define MX21_CSPICTRL_DR_SHIFT 14
  697. #define MX21_CSPICTRL_CS_SHIFT 19
  698. static void mx21_intctrl(struct spi_imx_data *spi_imx, int enable)
  699. {
  700. unsigned int val = 0;
  701. if (enable & MXC_INT_TE)
  702. val |= MX21_INTREG_TEEN;
  703. if (enable & MXC_INT_RR)
  704. val |= MX21_INTREG_RREN;
  705. writel(val, spi_imx->base + MXC_CSPIINT);
  706. }
  707. static void mx21_trigger(struct spi_imx_data *spi_imx)
  708. {
  709. unsigned int reg;
  710. reg = readl(spi_imx->base + MXC_CSPICTRL);
  711. reg |= MX21_CSPICTRL_XCH;
  712. writel(reg, spi_imx->base + MXC_CSPICTRL);
  713. }
  714. static int mx21_prepare_message(struct spi_imx_data *spi_imx,
  715. struct spi_message *msg)
  716. {
  717. return 0;
  718. }
  719. static int mx21_prepare_transfer(struct spi_imx_data *spi_imx,
  720. struct spi_device *spi)
  721. {
  722. unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_HOST;
  723. unsigned int max = is_imx27_cspi(spi_imx) ? 16 : 18;
  724. unsigned int clk;
  725. reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, spi_imx->spi_bus_clk, max, &clk)
  726. << MX21_CSPICTRL_DR_SHIFT;
  727. spi_imx->spi_bus_clk = clk;
  728. reg |= spi_imx->bits_per_word - 1;
  729. if (spi->mode & SPI_CPHA)
  730. reg |= MX21_CSPICTRL_PHA;
  731. if (spi->mode & SPI_CPOL)
  732. reg |= MX21_CSPICTRL_POL;
  733. if (spi->mode & SPI_CS_HIGH)
  734. reg |= MX21_CSPICTRL_SSPOL;
  735. if (!spi_get_csgpiod(spi, 0))
  736. reg |= spi_get_chipselect(spi, 0) << MX21_CSPICTRL_CS_SHIFT;
  737. writel(reg, spi_imx->base + MXC_CSPICTRL);
  738. return 0;
  739. }
  740. static int mx21_rx_available(struct spi_imx_data *spi_imx)
  741. {
  742. return readl(spi_imx->base + MXC_CSPIINT) & MX21_INTREG_RR;
  743. }
  744. static void mx21_reset(struct spi_imx_data *spi_imx)
  745. {
  746. writel(1, spi_imx->base + MXC_RESET);
  747. }
  748. #define MX1_INTREG_RR (1 << 3)
  749. #define MX1_INTREG_TEEN (1 << 8)
  750. #define MX1_INTREG_RREN (1 << 11)
  751. #define MX1_CSPICTRL_POL (1 << 4)
  752. #define MX1_CSPICTRL_PHA (1 << 5)
  753. #define MX1_CSPICTRL_XCH (1 << 8)
  754. #define MX1_CSPICTRL_ENABLE (1 << 9)
  755. #define MX1_CSPICTRL_HOST (1 << 10)
  756. #define MX1_CSPICTRL_DR_SHIFT 13
  757. static void mx1_intctrl(struct spi_imx_data *spi_imx, int enable)
  758. {
  759. unsigned int val = 0;
  760. if (enable & MXC_INT_TE)
  761. val |= MX1_INTREG_TEEN;
  762. if (enable & MXC_INT_RR)
  763. val |= MX1_INTREG_RREN;
  764. writel(val, spi_imx->base + MXC_CSPIINT);
  765. }
  766. static void mx1_trigger(struct spi_imx_data *spi_imx)
  767. {
  768. unsigned int reg;
  769. reg = readl(spi_imx->base + MXC_CSPICTRL);
  770. reg |= MX1_CSPICTRL_XCH;
  771. writel(reg, spi_imx->base + MXC_CSPICTRL);
  772. }
  773. static int mx1_prepare_message(struct spi_imx_data *spi_imx,
  774. struct spi_message *msg)
  775. {
  776. return 0;
  777. }
  778. static int mx1_prepare_transfer(struct spi_imx_data *spi_imx,
  779. struct spi_device *spi)
  780. {
  781. unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_HOST;
  782. unsigned int clk;
  783. reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->spi_bus_clk, &clk) <<
  784. MX1_CSPICTRL_DR_SHIFT;
  785. spi_imx->spi_bus_clk = clk;
  786. reg |= spi_imx->bits_per_word - 1;
  787. if (spi->mode & SPI_CPHA)
  788. reg |= MX1_CSPICTRL_PHA;
  789. if (spi->mode & SPI_CPOL)
  790. reg |= MX1_CSPICTRL_POL;
  791. writel(reg, spi_imx->base + MXC_CSPICTRL);
  792. return 0;
  793. }
  794. static int mx1_rx_available(struct spi_imx_data *spi_imx)
  795. {
  796. return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR;
  797. }
  798. static void mx1_reset(struct spi_imx_data *spi_imx)
  799. {
  800. writel(1, spi_imx->base + MXC_RESET);
  801. }
  802. static struct spi_imx_devtype_data imx1_cspi_devtype_data = {
  803. .intctrl = mx1_intctrl,
  804. .prepare_message = mx1_prepare_message,
  805. .prepare_transfer = mx1_prepare_transfer,
  806. .trigger = mx1_trigger,
  807. .rx_available = mx1_rx_available,
  808. .reset = mx1_reset,
  809. .fifo_size = 8,
  810. .has_dmamode = false,
  811. .dynamic_burst = false,
  812. .has_targetmode = false,
  813. .devtype = IMX1_CSPI,
  814. };
  815. static struct spi_imx_devtype_data imx21_cspi_devtype_data = {
  816. .intctrl = mx21_intctrl,
  817. .prepare_message = mx21_prepare_message,
  818. .prepare_transfer = mx21_prepare_transfer,
  819. .trigger = mx21_trigger,
  820. .rx_available = mx21_rx_available,
  821. .reset = mx21_reset,
  822. .fifo_size = 8,
  823. .has_dmamode = false,
  824. .dynamic_burst = false,
  825. .has_targetmode = false,
  826. .devtype = IMX21_CSPI,
  827. };
  828. static struct spi_imx_devtype_data imx27_cspi_devtype_data = {
  829. /* i.mx27 cspi shares the functions with i.mx21 one */
  830. .intctrl = mx21_intctrl,
  831. .prepare_message = mx21_prepare_message,
  832. .prepare_transfer = mx21_prepare_transfer,
  833. .trigger = mx21_trigger,
  834. .rx_available = mx21_rx_available,
  835. .reset = mx21_reset,
  836. .fifo_size = 8,
  837. .has_dmamode = false,
  838. .dynamic_burst = false,
  839. .has_targetmode = false,
  840. .devtype = IMX27_CSPI,
  841. };
  842. static struct spi_imx_devtype_data imx31_cspi_devtype_data = {
  843. .intctrl = mx31_intctrl,
  844. .prepare_message = mx31_prepare_message,
  845. .prepare_transfer = mx31_prepare_transfer,
  846. .trigger = mx31_trigger,
  847. .rx_available = mx31_rx_available,
  848. .reset = mx31_reset,
  849. .fifo_size = 8,
  850. .has_dmamode = false,
  851. .dynamic_burst = false,
  852. .has_targetmode = false,
  853. .devtype = IMX31_CSPI,
  854. };
  855. static struct spi_imx_devtype_data imx35_cspi_devtype_data = {
  856. /* i.mx35 and later cspi shares the functions with i.mx31 one */
  857. .intctrl = mx31_intctrl,
  858. .prepare_message = mx31_prepare_message,
  859. .prepare_transfer = mx31_prepare_transfer,
  860. .trigger = mx31_trigger,
  861. .rx_available = mx31_rx_available,
  862. .reset = mx31_reset,
  863. .fifo_size = 8,
  864. .has_dmamode = false,
  865. .dynamic_burst = false,
  866. .has_targetmode = false,
  867. .devtype = IMX35_CSPI,
  868. };
  869. static struct spi_imx_devtype_data imx51_ecspi_devtype_data = {
  870. .intctrl = mx51_ecspi_intctrl,
  871. .prepare_message = mx51_ecspi_prepare_message,
  872. .prepare_transfer = mx51_ecspi_prepare_transfer,
  873. .trigger = mx51_ecspi_trigger,
  874. .rx_available = mx51_ecspi_rx_available,
  875. .reset = mx51_ecspi_reset,
  876. .setup_wml = mx51_setup_wml,
  877. .fifo_size = 64,
  878. .has_dmamode = true,
  879. .dynamic_burst = true,
  880. .has_targetmode = true,
  881. .disable = mx51_ecspi_disable,
  882. .devtype = IMX51_ECSPI,
  883. };
  884. static struct spi_imx_devtype_data imx53_ecspi_devtype_data = {
  885. .intctrl = mx51_ecspi_intctrl,
  886. .prepare_message = mx51_ecspi_prepare_message,
  887. .prepare_transfer = mx51_ecspi_prepare_transfer,
  888. .trigger = mx51_ecspi_trigger,
  889. .rx_available = mx51_ecspi_rx_available,
  890. .reset = mx51_ecspi_reset,
  891. .fifo_size = 64,
  892. .has_dmamode = true,
  893. .has_targetmode = true,
  894. .disable = mx51_ecspi_disable,
  895. .devtype = IMX53_ECSPI,
  896. };
  897. static struct spi_imx_devtype_data imx6ul_ecspi_devtype_data = {
  898. .intctrl = mx51_ecspi_intctrl,
  899. .prepare_message = mx51_ecspi_prepare_message,
  900. .prepare_transfer = mx51_ecspi_prepare_transfer,
  901. .trigger = mx51_ecspi_trigger,
  902. .rx_available = mx51_ecspi_rx_available,
  903. .reset = mx51_ecspi_reset,
  904. .setup_wml = mx51_setup_wml,
  905. .fifo_size = 64,
  906. .has_dmamode = true,
  907. .dynamic_burst = true,
  908. .has_targetmode = true,
  909. .tx_glitch_fixed = true,
  910. .disable = mx51_ecspi_disable,
  911. .devtype = IMX51_ECSPI,
  912. };
  913. static const struct of_device_id spi_imx_dt_ids[] = {
  914. { .compatible = "fsl,imx1-cspi", .data = &imx1_cspi_devtype_data, },
  915. { .compatible = "fsl,imx21-cspi", .data = &imx21_cspi_devtype_data, },
  916. { .compatible = "fsl,imx27-cspi", .data = &imx27_cspi_devtype_data, },
  917. { .compatible = "fsl,imx31-cspi", .data = &imx31_cspi_devtype_data, },
  918. { .compatible = "fsl,imx35-cspi", .data = &imx35_cspi_devtype_data, },
  919. { .compatible = "fsl,imx51-ecspi", .data = &imx51_ecspi_devtype_data, },
  920. { .compatible = "fsl,imx53-ecspi", .data = &imx53_ecspi_devtype_data, },
  921. { .compatible = "fsl,imx6ul-ecspi", .data = &imx6ul_ecspi_devtype_data, },
  922. { /* sentinel */ }
  923. };
  924. MODULE_DEVICE_TABLE(of, spi_imx_dt_ids);
  925. static void spi_imx_set_burst_len(struct spi_imx_data *spi_imx, int n_bits)
  926. {
  927. u32 ctrl;
  928. ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
  929. ctrl &= ~MX51_ECSPI_CTRL_BL_MASK;
  930. ctrl |= ((n_bits - 1) << MX51_ECSPI_CTRL_BL_OFFSET);
  931. writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
  932. }
  933. static void spi_imx_push(struct spi_imx_data *spi_imx)
  934. {
  935. unsigned int burst_len;
  936. /*
  937. * Reload the FIFO when the remaining bytes to be transferred in the
  938. * current burst is 0. This only applies when bits_per_word is a
  939. * multiple of 8.
  940. */
  941. if (!spi_imx->remainder) {
  942. if (spi_imx->dynamic_burst) {
  943. /* We need to deal unaligned data first */
  944. burst_len = spi_imx->count % MX51_ECSPI_CTRL_MAX_BURST;
  945. if (!burst_len)
  946. burst_len = MX51_ECSPI_CTRL_MAX_BURST;
  947. spi_imx_set_burst_len(spi_imx, burst_len * 8);
  948. spi_imx->remainder = burst_len;
  949. } else {
  950. spi_imx->remainder = spi_imx_bytes_per_word(spi_imx->bits_per_word);
  951. }
  952. }
  953. while (spi_imx->txfifo < spi_imx->devtype_data->fifo_size) {
  954. if (!spi_imx->count)
  955. break;
  956. if (spi_imx->dynamic_burst &&
  957. spi_imx->txfifo >= DIV_ROUND_UP(spi_imx->remainder, 4))
  958. break;
  959. spi_imx->tx(spi_imx);
  960. spi_imx->txfifo++;
  961. }
  962. if (!spi_imx->target_mode)
  963. spi_imx->devtype_data->trigger(spi_imx);
  964. }
  965. static irqreturn_t spi_imx_isr(int irq, void *dev_id)
  966. {
  967. struct spi_imx_data *spi_imx = dev_id;
  968. while (spi_imx->txfifo &&
  969. spi_imx->devtype_data->rx_available(spi_imx)) {
  970. spi_imx->rx(spi_imx);
  971. spi_imx->txfifo--;
  972. }
  973. if (spi_imx->count) {
  974. spi_imx_push(spi_imx);
  975. return IRQ_HANDLED;
  976. }
  977. if (spi_imx->txfifo) {
  978. /* No data left to push, but still waiting for rx data,
  979. * enable receive data available interrupt.
  980. */
  981. spi_imx->devtype_data->intctrl(
  982. spi_imx, MXC_INT_RR);
  983. return IRQ_HANDLED;
  984. }
  985. spi_imx->devtype_data->intctrl(spi_imx, 0);
  986. complete(&spi_imx->xfer_done);
  987. return IRQ_HANDLED;
  988. }
  989. static int spi_imx_dma_configure(struct spi_controller *controller)
  990. {
  991. int ret;
  992. enum dma_slave_buswidth buswidth;
  993. struct dma_slave_config rx = {}, tx = {};
  994. struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
  995. switch (spi_imx_bytes_per_word(spi_imx->bits_per_word)) {
  996. case 4:
  997. buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
  998. break;
  999. case 2:
  1000. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  1001. break;
  1002. case 1:
  1003. buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
  1004. break;
  1005. default:
  1006. return -EINVAL;
  1007. }
  1008. tx.direction = DMA_MEM_TO_DEV;
  1009. tx.dst_addr = spi_imx->base_phys + MXC_CSPITXDATA;
  1010. tx.dst_addr_width = buswidth;
  1011. tx.dst_maxburst = spi_imx->wml;
  1012. ret = dmaengine_slave_config(controller->dma_tx, &tx);
  1013. if (ret) {
  1014. dev_err(spi_imx->dev, "TX dma configuration failed with %d\n", ret);
  1015. return ret;
  1016. }
  1017. rx.direction = DMA_DEV_TO_MEM;
  1018. rx.src_addr = spi_imx->base_phys + MXC_CSPIRXDATA;
  1019. rx.src_addr_width = buswidth;
  1020. rx.src_maxburst = spi_imx->wml;
  1021. ret = dmaengine_slave_config(controller->dma_rx, &rx);
  1022. if (ret) {
  1023. dev_err(spi_imx->dev, "RX dma configuration failed with %d\n", ret);
  1024. return ret;
  1025. }
  1026. return 0;
  1027. }
  1028. static int spi_imx_setupxfer(struct spi_device *spi,
  1029. struct spi_transfer *t)
  1030. {
  1031. struct spi_imx_data *spi_imx = spi_controller_get_devdata(spi->controller);
  1032. if (!t)
  1033. return 0;
  1034. if (!t->speed_hz) {
  1035. if (!spi->max_speed_hz) {
  1036. dev_err(&spi->dev, "no speed_hz provided!\n");
  1037. return -EINVAL;
  1038. }
  1039. dev_dbg(&spi->dev, "using spi->max_speed_hz!\n");
  1040. spi_imx->spi_bus_clk = spi->max_speed_hz;
  1041. } else
  1042. spi_imx->spi_bus_clk = t->speed_hz;
  1043. spi_imx->bits_per_word = t->bits_per_word;
  1044. spi_imx->count = t->len;
  1045. /*
  1046. * Initialize the functions for transfer. To transfer non byte-aligned
  1047. * words, we have to use multiple word-size bursts, we can't use
  1048. * dynamic_burst in that case.
  1049. */
  1050. if (spi_imx->devtype_data->dynamic_burst && !spi_imx->target_mode &&
  1051. !(spi->mode & SPI_CS_WORD) &&
  1052. (spi_imx->bits_per_word == 8 ||
  1053. spi_imx->bits_per_word == 16 ||
  1054. spi_imx->bits_per_word == 32)) {
  1055. spi_imx->rx = spi_imx_buf_rx_swap;
  1056. spi_imx->tx = spi_imx_buf_tx_swap;
  1057. spi_imx->dynamic_burst = 1;
  1058. } else {
  1059. if (spi_imx->bits_per_word <= 8) {
  1060. spi_imx->rx = spi_imx_buf_rx_u8;
  1061. spi_imx->tx = spi_imx_buf_tx_u8;
  1062. } else if (spi_imx->bits_per_word <= 16) {
  1063. spi_imx->rx = spi_imx_buf_rx_u16;
  1064. spi_imx->tx = spi_imx_buf_tx_u16;
  1065. } else {
  1066. spi_imx->rx = spi_imx_buf_rx_u32;
  1067. spi_imx->tx = spi_imx_buf_tx_u32;
  1068. }
  1069. spi_imx->dynamic_burst = 0;
  1070. }
  1071. if (spi_imx_can_dma(spi_imx->controller, spi, t))
  1072. spi_imx->usedma = true;
  1073. else
  1074. spi_imx->usedma = false;
  1075. spi_imx->rx_only = ((t->tx_buf == NULL)
  1076. || (t->tx_buf == spi->controller->dummy_tx));
  1077. if (is_imx53_ecspi(spi_imx) && spi_imx->target_mode) {
  1078. spi_imx->rx = mx53_ecspi_rx_target;
  1079. spi_imx->tx = mx53_ecspi_tx_target;
  1080. spi_imx->target_burst = t->len;
  1081. }
  1082. spi_imx->devtype_data->prepare_transfer(spi_imx, spi);
  1083. return 0;
  1084. }
  1085. static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx)
  1086. {
  1087. struct spi_controller *controller = spi_imx->controller;
  1088. if (controller->dma_rx) {
  1089. dma_release_channel(controller->dma_rx);
  1090. controller->dma_rx = NULL;
  1091. }
  1092. if (controller->dma_tx) {
  1093. dma_release_channel(controller->dma_tx);
  1094. controller->dma_tx = NULL;
  1095. }
  1096. }
  1097. static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx,
  1098. struct spi_controller *controller)
  1099. {
  1100. int ret;
  1101. spi_imx->wml = spi_imx->devtype_data->fifo_size / 2;
  1102. /* Prepare for TX DMA: */
  1103. controller->dma_tx = dma_request_chan(dev, "tx");
  1104. if (IS_ERR(controller->dma_tx)) {
  1105. ret = PTR_ERR(controller->dma_tx);
  1106. dev_err_probe(dev, ret, "can't get the TX DMA channel!\n");
  1107. controller->dma_tx = NULL;
  1108. goto err;
  1109. }
  1110. /* Prepare for RX : */
  1111. controller->dma_rx = dma_request_chan(dev, "rx");
  1112. if (IS_ERR(controller->dma_rx)) {
  1113. ret = PTR_ERR(controller->dma_rx);
  1114. dev_err_probe(dev, ret, "can't get the RX DMA channel!\n");
  1115. controller->dma_rx = NULL;
  1116. goto err;
  1117. }
  1118. init_completion(&spi_imx->dma_rx_completion);
  1119. init_completion(&spi_imx->dma_tx_completion);
  1120. controller->can_dma = spi_imx_can_dma;
  1121. controller->max_dma_len = MAX_SDMA_BD_BYTES;
  1122. spi_imx->controller->flags = SPI_CONTROLLER_MUST_RX |
  1123. SPI_CONTROLLER_MUST_TX;
  1124. return 0;
  1125. err:
  1126. spi_imx_sdma_exit(spi_imx);
  1127. return ret;
  1128. }
  1129. static void spi_imx_dma_rx_callback(void *cookie)
  1130. {
  1131. struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
  1132. complete(&spi_imx->dma_rx_completion);
  1133. }
  1134. static void spi_imx_dma_tx_callback(void *cookie)
  1135. {
  1136. struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
  1137. complete(&spi_imx->dma_tx_completion);
  1138. }
  1139. static int spi_imx_calculate_timeout(struct spi_imx_data *spi_imx, int size)
  1140. {
  1141. unsigned long timeout = 0;
  1142. /* Time with actual data transfer and CS change delay related to HW */
  1143. timeout = (8 + 4) * size / spi_imx->spi_bus_clk;
  1144. /* Add extra second for scheduler related activities */
  1145. timeout += 1;
  1146. /* Double calculated timeout */
  1147. return msecs_to_jiffies(2 * timeout * MSEC_PER_SEC);
  1148. }
  1149. static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx,
  1150. struct spi_transfer *transfer)
  1151. {
  1152. struct dma_async_tx_descriptor *desc_tx, *desc_rx;
  1153. unsigned long transfer_timeout;
  1154. unsigned long time_left;
  1155. struct spi_controller *controller = spi_imx->controller;
  1156. struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;
  1157. struct scatterlist *last_sg = sg_last(rx->sgl, rx->nents);
  1158. unsigned int bytes_per_word, i;
  1159. int ret;
  1160. /* Get the right burst length from the last sg to ensure no tail data */
  1161. bytes_per_word = spi_imx_bytes_per_word(transfer->bits_per_word);
  1162. for (i = spi_imx->devtype_data->fifo_size / 2; i > 0; i--) {
  1163. if (!(sg_dma_len(last_sg) % (i * bytes_per_word)))
  1164. break;
  1165. }
  1166. /* Use 1 as wml in case no available burst length got */
  1167. if (i == 0)
  1168. i = 1;
  1169. spi_imx->wml = i;
  1170. ret = spi_imx_dma_configure(controller);
  1171. if (ret)
  1172. goto dma_failure_no_start;
  1173. if (!spi_imx->devtype_data->setup_wml) {
  1174. dev_err(spi_imx->dev, "No setup_wml()?\n");
  1175. ret = -EINVAL;
  1176. goto dma_failure_no_start;
  1177. }
  1178. spi_imx->devtype_data->setup_wml(spi_imx);
  1179. /*
  1180. * The TX DMA setup starts the transfer, so make sure RX is configured
  1181. * before TX.
  1182. */
  1183. desc_rx = dmaengine_prep_slave_sg(controller->dma_rx,
  1184. rx->sgl, rx->nents, DMA_DEV_TO_MEM,
  1185. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1186. if (!desc_rx) {
  1187. ret = -EINVAL;
  1188. goto dma_failure_no_start;
  1189. }
  1190. desc_rx->callback = spi_imx_dma_rx_callback;
  1191. desc_rx->callback_param = (void *)spi_imx;
  1192. dmaengine_submit(desc_rx);
  1193. reinit_completion(&spi_imx->dma_rx_completion);
  1194. dma_async_issue_pending(controller->dma_rx);
  1195. desc_tx = dmaengine_prep_slave_sg(controller->dma_tx,
  1196. tx->sgl, tx->nents, DMA_MEM_TO_DEV,
  1197. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1198. if (!desc_tx) {
  1199. dmaengine_terminate_all(controller->dma_tx);
  1200. dmaengine_terminate_all(controller->dma_rx);
  1201. return -EINVAL;
  1202. }
  1203. desc_tx->callback = spi_imx_dma_tx_callback;
  1204. desc_tx->callback_param = (void *)spi_imx;
  1205. dmaengine_submit(desc_tx);
  1206. reinit_completion(&spi_imx->dma_tx_completion);
  1207. dma_async_issue_pending(controller->dma_tx);
  1208. transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
  1209. /* Wait SDMA to finish the data transfer.*/
  1210. time_left = wait_for_completion_timeout(&spi_imx->dma_tx_completion,
  1211. transfer_timeout);
  1212. if (!time_left) {
  1213. dev_err(spi_imx->dev, "I/O Error in DMA TX\n");
  1214. dmaengine_terminate_all(controller->dma_tx);
  1215. dmaengine_terminate_all(controller->dma_rx);
  1216. return -ETIMEDOUT;
  1217. }
  1218. time_left = wait_for_completion_timeout(&spi_imx->dma_rx_completion,
  1219. transfer_timeout);
  1220. if (!time_left) {
  1221. dev_err(&controller->dev, "I/O Error in DMA RX\n");
  1222. spi_imx->devtype_data->reset(spi_imx);
  1223. dmaengine_terminate_all(controller->dma_rx);
  1224. return -ETIMEDOUT;
  1225. }
  1226. return 0;
  1227. /* fallback to pio */
  1228. dma_failure_no_start:
  1229. transfer->error |= SPI_TRANS_FAIL_NO_START;
  1230. return ret;
  1231. }
  1232. static int spi_imx_pio_transfer(struct spi_device *spi,
  1233. struct spi_transfer *transfer)
  1234. {
  1235. struct spi_imx_data *spi_imx = spi_controller_get_devdata(spi->controller);
  1236. unsigned long transfer_timeout;
  1237. unsigned long time_left;
  1238. spi_imx->tx_buf = transfer->tx_buf;
  1239. spi_imx->rx_buf = transfer->rx_buf;
  1240. spi_imx->count = transfer->len;
  1241. spi_imx->txfifo = 0;
  1242. spi_imx->remainder = 0;
  1243. reinit_completion(&spi_imx->xfer_done);
  1244. spi_imx_push(spi_imx);
  1245. spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE);
  1246. transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
  1247. time_left = wait_for_completion_timeout(&spi_imx->xfer_done,
  1248. transfer_timeout);
  1249. if (!time_left) {
  1250. dev_err(&spi->dev, "I/O Error in PIO\n");
  1251. spi_imx->devtype_data->reset(spi_imx);
  1252. return -ETIMEDOUT;
  1253. }
  1254. return 0;
  1255. }
  1256. static int spi_imx_poll_transfer(struct spi_device *spi,
  1257. struct spi_transfer *transfer)
  1258. {
  1259. struct spi_imx_data *spi_imx = spi_controller_get_devdata(spi->controller);
  1260. unsigned long timeout;
  1261. spi_imx->tx_buf = transfer->tx_buf;
  1262. spi_imx->rx_buf = transfer->rx_buf;
  1263. spi_imx->count = transfer->len;
  1264. spi_imx->txfifo = 0;
  1265. spi_imx->remainder = 0;
  1266. /* fill in the fifo before timeout calculations if we are
  1267. * interrupted here, then the data is getting transferred by
  1268. * the HW while we are interrupted
  1269. */
  1270. spi_imx_push(spi_imx);
  1271. timeout = spi_imx_calculate_timeout(spi_imx, transfer->len) + jiffies;
  1272. while (spi_imx->txfifo) {
  1273. /* RX */
  1274. while (spi_imx->txfifo &&
  1275. spi_imx->devtype_data->rx_available(spi_imx)) {
  1276. spi_imx->rx(spi_imx);
  1277. spi_imx->txfifo--;
  1278. }
  1279. /* TX */
  1280. if (spi_imx->count) {
  1281. spi_imx_push(spi_imx);
  1282. continue;
  1283. }
  1284. if (spi_imx->txfifo &&
  1285. time_after(jiffies, timeout)) {
  1286. dev_err_ratelimited(&spi->dev,
  1287. "timeout period reached: jiffies: %lu- falling back to interrupt mode\n",
  1288. jiffies - timeout);
  1289. /* fall back to interrupt mode */
  1290. return spi_imx_pio_transfer(spi, transfer);
  1291. }
  1292. }
  1293. return 0;
  1294. }
  1295. static int spi_imx_pio_transfer_target(struct spi_device *spi,
  1296. struct spi_transfer *transfer)
  1297. {
  1298. struct spi_imx_data *spi_imx = spi_controller_get_devdata(spi->controller);
  1299. int ret = 0;
  1300. if (is_imx53_ecspi(spi_imx) &&
  1301. transfer->len > MX53_MAX_TRANSFER_BYTES) {
  1302. dev_err(&spi->dev, "Transaction too big, max size is %d bytes\n",
  1303. MX53_MAX_TRANSFER_BYTES);
  1304. return -EMSGSIZE;
  1305. }
  1306. spi_imx->tx_buf = transfer->tx_buf;
  1307. spi_imx->rx_buf = transfer->rx_buf;
  1308. spi_imx->count = transfer->len;
  1309. spi_imx->txfifo = 0;
  1310. spi_imx->remainder = 0;
  1311. reinit_completion(&spi_imx->xfer_done);
  1312. spi_imx->target_aborted = false;
  1313. spi_imx_push(spi_imx);
  1314. spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE | MXC_INT_RDR);
  1315. if (wait_for_completion_interruptible(&spi_imx->xfer_done) ||
  1316. spi_imx->target_aborted) {
  1317. dev_dbg(&spi->dev, "interrupted\n");
  1318. ret = -EINTR;
  1319. }
  1320. /* ecspi has a HW issue when works in Target mode,
  1321. * after 64 words writtern to TXFIFO, even TXFIFO becomes empty,
  1322. * ECSPI_TXDATA keeps shift out the last word data,
  1323. * so we have to disable ECSPI when in target mode after the
  1324. * transfer completes
  1325. */
  1326. if (spi_imx->devtype_data->disable)
  1327. spi_imx->devtype_data->disable(spi_imx);
  1328. return ret;
  1329. }
  1330. static int spi_imx_transfer_one(struct spi_controller *controller,
  1331. struct spi_device *spi,
  1332. struct spi_transfer *transfer)
  1333. {
  1334. int ret;
  1335. struct spi_imx_data *spi_imx = spi_controller_get_devdata(spi->controller);
  1336. unsigned long hz_per_byte, byte_limit;
  1337. ret = spi_imx_setupxfer(spi, transfer);
  1338. if (ret < 0)
  1339. return ret;
  1340. transfer->effective_speed_hz = spi_imx->spi_bus_clk;
  1341. /* flush rxfifo before transfer */
  1342. while (spi_imx->devtype_data->rx_available(spi_imx))
  1343. readl(spi_imx->base + MXC_CSPIRXDATA);
  1344. if (spi_imx->target_mode)
  1345. return spi_imx_pio_transfer_target(spi, transfer);
  1346. /*
  1347. * If we decided in spi_imx_can_dma() that we want to do a DMA
  1348. * transfer, the SPI transfer has already been mapped, so we
  1349. * have to do the DMA transfer here.
  1350. */
  1351. if (spi_imx->usedma)
  1352. return spi_imx_dma_transfer(spi_imx, transfer);
  1353. /*
  1354. * Calculate the estimated time in us the transfer runs. Find
  1355. * the number of Hz per byte per polling limit.
  1356. */
  1357. hz_per_byte = polling_limit_us ? ((8 + 4) * USEC_PER_SEC) / polling_limit_us : 0;
  1358. byte_limit = hz_per_byte ? transfer->effective_speed_hz / hz_per_byte : 1;
  1359. /* run in polling mode for short transfers */
  1360. if (transfer->len < byte_limit)
  1361. return spi_imx_poll_transfer(spi, transfer);
  1362. return spi_imx_pio_transfer(spi, transfer);
  1363. }
  1364. static int spi_imx_setup(struct spi_device *spi)
  1365. {
  1366. dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__,
  1367. spi->mode, spi->bits_per_word, spi->max_speed_hz);
  1368. return 0;
  1369. }
  1370. static int
  1371. spi_imx_prepare_message(struct spi_controller *controller, struct spi_message *msg)
  1372. {
  1373. struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
  1374. int ret;
  1375. ret = pm_runtime_resume_and_get(spi_imx->dev);
  1376. if (ret < 0) {
  1377. dev_err(spi_imx->dev, "failed to enable clock\n");
  1378. return ret;
  1379. }
  1380. ret = spi_imx->devtype_data->prepare_message(spi_imx, msg);
  1381. if (ret) {
  1382. pm_runtime_mark_last_busy(spi_imx->dev);
  1383. pm_runtime_put_autosuspend(spi_imx->dev);
  1384. }
  1385. return ret;
  1386. }
  1387. static int
  1388. spi_imx_unprepare_message(struct spi_controller *controller, struct spi_message *msg)
  1389. {
  1390. struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
  1391. pm_runtime_mark_last_busy(spi_imx->dev);
  1392. pm_runtime_put_autosuspend(spi_imx->dev);
  1393. return 0;
  1394. }
  1395. static int spi_imx_target_abort(struct spi_controller *controller)
  1396. {
  1397. struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
  1398. spi_imx->target_aborted = true;
  1399. complete(&spi_imx->xfer_done);
  1400. return 0;
  1401. }
  1402. static int spi_imx_probe(struct platform_device *pdev)
  1403. {
  1404. struct device_node *np = pdev->dev.of_node;
  1405. struct spi_controller *controller;
  1406. struct spi_imx_data *spi_imx;
  1407. struct resource *res;
  1408. int ret, irq, spi_drctl;
  1409. const struct spi_imx_devtype_data *devtype_data =
  1410. of_device_get_match_data(&pdev->dev);
  1411. bool target_mode;
  1412. u32 val;
  1413. target_mode = devtype_data->has_targetmode &&
  1414. of_property_read_bool(np, "spi-slave");
  1415. if (target_mode)
  1416. controller = spi_alloc_target(&pdev->dev,
  1417. sizeof(struct spi_imx_data));
  1418. else
  1419. controller = spi_alloc_host(&pdev->dev,
  1420. sizeof(struct spi_imx_data));
  1421. if (!controller)
  1422. return -ENOMEM;
  1423. ret = of_property_read_u32(np, "fsl,spi-rdy-drctl", &spi_drctl);
  1424. if ((ret < 0) || (spi_drctl >= 0x3)) {
  1425. /* '11' is reserved */
  1426. spi_drctl = 0;
  1427. }
  1428. platform_set_drvdata(pdev, controller);
  1429. controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
  1430. controller->bus_num = np ? -1 : pdev->id;
  1431. controller->use_gpio_descriptors = true;
  1432. spi_imx = spi_controller_get_devdata(controller);
  1433. spi_imx->controller = controller;
  1434. spi_imx->dev = &pdev->dev;
  1435. spi_imx->target_mode = target_mode;
  1436. spi_imx->devtype_data = devtype_data;
  1437. /*
  1438. * Get number of chip selects from device properties. This can be
  1439. * coming from device tree or boardfiles, if it is not defined,
  1440. * a default value of 3 chip selects will be used, as all the legacy
  1441. * board files have <= 3 chip selects.
  1442. */
  1443. if (!device_property_read_u32(&pdev->dev, "num-cs", &val))
  1444. controller->num_chipselect = val;
  1445. else
  1446. controller->num_chipselect = 3;
  1447. controller->transfer_one = spi_imx_transfer_one;
  1448. controller->setup = spi_imx_setup;
  1449. controller->prepare_message = spi_imx_prepare_message;
  1450. controller->unprepare_message = spi_imx_unprepare_message;
  1451. controller->target_abort = spi_imx_target_abort;
  1452. controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_NO_CS |
  1453. SPI_MOSI_IDLE_LOW;
  1454. if (is_imx35_cspi(spi_imx) || is_imx51_ecspi(spi_imx) ||
  1455. is_imx53_ecspi(spi_imx))
  1456. controller->mode_bits |= SPI_LOOP | SPI_READY;
  1457. if (is_imx51_ecspi(spi_imx) || is_imx53_ecspi(spi_imx))
  1458. controller->mode_bits |= SPI_RX_CPHA_FLIP;
  1459. if (is_imx51_ecspi(spi_imx) &&
  1460. device_property_read_u32(&pdev->dev, "cs-gpios", NULL))
  1461. /*
  1462. * When using HW-CS implementing SPI_CS_WORD can be done by just
  1463. * setting the burst length to the word size. This is
  1464. * considerably faster than manually controlling the CS.
  1465. */
  1466. controller->mode_bits |= SPI_CS_WORD;
  1467. if (is_imx51_ecspi(spi_imx) || is_imx53_ecspi(spi_imx)) {
  1468. controller->max_native_cs = 4;
  1469. controller->flags |= SPI_CONTROLLER_GPIO_SS;
  1470. }
  1471. spi_imx->spi_drctl = spi_drctl;
  1472. init_completion(&spi_imx->xfer_done);
  1473. spi_imx->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  1474. if (IS_ERR(spi_imx->base)) {
  1475. ret = PTR_ERR(spi_imx->base);
  1476. goto out_controller_put;
  1477. }
  1478. spi_imx->base_phys = res->start;
  1479. irq = platform_get_irq(pdev, 0);
  1480. if (irq < 0) {
  1481. ret = irq;
  1482. goto out_controller_put;
  1483. }
  1484. ret = devm_request_irq(&pdev->dev, irq, spi_imx_isr, 0,
  1485. dev_name(&pdev->dev), spi_imx);
  1486. if (ret) {
  1487. dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
  1488. goto out_controller_put;
  1489. }
  1490. spi_imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1491. if (IS_ERR(spi_imx->clk_ipg)) {
  1492. ret = PTR_ERR(spi_imx->clk_ipg);
  1493. goto out_controller_put;
  1494. }
  1495. spi_imx->clk_per = devm_clk_get(&pdev->dev, "per");
  1496. if (IS_ERR(spi_imx->clk_per)) {
  1497. ret = PTR_ERR(spi_imx->clk_per);
  1498. goto out_controller_put;
  1499. }
  1500. ret = clk_prepare_enable(spi_imx->clk_per);
  1501. if (ret)
  1502. goto out_controller_put;
  1503. ret = clk_prepare_enable(spi_imx->clk_ipg);
  1504. if (ret)
  1505. goto out_put_per;
  1506. pm_runtime_set_autosuspend_delay(spi_imx->dev, MXC_RPM_TIMEOUT);
  1507. pm_runtime_use_autosuspend(spi_imx->dev);
  1508. pm_runtime_get_noresume(spi_imx->dev);
  1509. pm_runtime_set_active(spi_imx->dev);
  1510. pm_runtime_enable(spi_imx->dev);
  1511. spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per);
  1512. /*
  1513. * Only validated on i.mx35 and i.mx6 now, can remove the constraint
  1514. * if validated on other chips.
  1515. */
  1516. if (spi_imx->devtype_data->has_dmamode) {
  1517. ret = spi_imx_sdma_init(&pdev->dev, spi_imx, controller);
  1518. if (ret == -EPROBE_DEFER)
  1519. goto out_runtime_pm_put;
  1520. if (ret < 0)
  1521. dev_dbg(&pdev->dev, "dma setup error %d, use pio\n",
  1522. ret);
  1523. }
  1524. spi_imx->devtype_data->reset(spi_imx);
  1525. spi_imx->devtype_data->intctrl(spi_imx, 0);
  1526. controller->dev.of_node = pdev->dev.of_node;
  1527. ret = spi_register_controller(controller);
  1528. if (ret) {
  1529. dev_err_probe(&pdev->dev, ret, "register controller failed\n");
  1530. goto out_register_controller;
  1531. }
  1532. pm_runtime_mark_last_busy(spi_imx->dev);
  1533. pm_runtime_put_autosuspend(spi_imx->dev);
  1534. return ret;
  1535. out_register_controller:
  1536. if (spi_imx->devtype_data->has_dmamode)
  1537. spi_imx_sdma_exit(spi_imx);
  1538. out_runtime_pm_put:
  1539. pm_runtime_dont_use_autosuspend(spi_imx->dev);
  1540. pm_runtime_disable(spi_imx->dev);
  1541. pm_runtime_set_suspended(&pdev->dev);
  1542. clk_disable_unprepare(spi_imx->clk_ipg);
  1543. out_put_per:
  1544. clk_disable_unprepare(spi_imx->clk_per);
  1545. out_controller_put:
  1546. spi_controller_put(controller);
  1547. return ret;
  1548. }
  1549. static void spi_imx_remove(struct platform_device *pdev)
  1550. {
  1551. struct spi_controller *controller = platform_get_drvdata(pdev);
  1552. struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
  1553. int ret;
  1554. spi_unregister_controller(controller);
  1555. ret = pm_runtime_get_sync(spi_imx->dev);
  1556. if (ret >= 0)
  1557. writel(0, spi_imx->base + MXC_CSPICTRL);
  1558. else
  1559. dev_warn(spi_imx->dev, "failed to enable clock, skip hw disable\n");
  1560. pm_runtime_dont_use_autosuspend(spi_imx->dev);
  1561. pm_runtime_put_sync(spi_imx->dev);
  1562. pm_runtime_disable(spi_imx->dev);
  1563. spi_imx_sdma_exit(spi_imx);
  1564. }
  1565. static int spi_imx_runtime_resume(struct device *dev)
  1566. {
  1567. struct spi_controller *controller = dev_get_drvdata(dev);
  1568. struct spi_imx_data *spi_imx;
  1569. int ret;
  1570. spi_imx = spi_controller_get_devdata(controller);
  1571. ret = clk_prepare_enable(spi_imx->clk_per);
  1572. if (ret)
  1573. return ret;
  1574. ret = clk_prepare_enable(spi_imx->clk_ipg);
  1575. if (ret) {
  1576. clk_disable_unprepare(spi_imx->clk_per);
  1577. return ret;
  1578. }
  1579. return 0;
  1580. }
  1581. static int spi_imx_runtime_suspend(struct device *dev)
  1582. {
  1583. struct spi_controller *controller = dev_get_drvdata(dev);
  1584. struct spi_imx_data *spi_imx;
  1585. spi_imx = spi_controller_get_devdata(controller);
  1586. clk_disable_unprepare(spi_imx->clk_per);
  1587. clk_disable_unprepare(spi_imx->clk_ipg);
  1588. return 0;
  1589. }
  1590. static int spi_imx_suspend(struct device *dev)
  1591. {
  1592. pinctrl_pm_select_sleep_state(dev);
  1593. return 0;
  1594. }
  1595. static int spi_imx_resume(struct device *dev)
  1596. {
  1597. pinctrl_pm_select_default_state(dev);
  1598. return 0;
  1599. }
  1600. static const struct dev_pm_ops imx_spi_pm = {
  1601. RUNTIME_PM_OPS(spi_imx_runtime_suspend, spi_imx_runtime_resume, NULL)
  1602. SYSTEM_SLEEP_PM_OPS(spi_imx_suspend, spi_imx_resume)
  1603. };
  1604. static struct platform_driver spi_imx_driver = {
  1605. .driver = {
  1606. .name = DRIVER_NAME,
  1607. .of_match_table = spi_imx_dt_ids,
  1608. .pm = pm_ptr(&imx_spi_pm),
  1609. },
  1610. .probe = spi_imx_probe,
  1611. .remove_new = spi_imx_remove,
  1612. };
  1613. module_platform_driver(spi_imx_driver);
  1614. MODULE_DESCRIPTION("i.MX SPI Controller driver");
  1615. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  1616. MODULE_LICENSE("GPL");
  1617. MODULE_ALIAS("platform:" DRIVER_NAME);