spi-imx.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  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/clk.h>
  5. #include <linux/completion.h>
  6. #include <linux/delay.h>
  7. #include <linux/dmaengine.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/err.h>
  10. #include <linux/gpio.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/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/spi/spi_bitbang.h>
  20. #include <linux/types.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/platform_data/dma-imx.h>
  25. #include <linux/platform_data/spi-imx.h>
  26. #define DRIVER_NAME "spi_imx"
  27. #define MXC_CSPIRXDATA 0x00
  28. #define MXC_CSPITXDATA 0x04
  29. #define MXC_CSPICTRL 0x08
  30. #define MXC_CSPIINT 0x0c
  31. #define MXC_RESET 0x1c
  32. /* generic defines to abstract from the different register layouts */
  33. #define MXC_INT_RR (1 << 0) /* Receive data ready interrupt */
  34. #define MXC_INT_TE (1 << 1) /* Transmit FIFO empty interrupt */
  35. #define MXC_INT_RDR BIT(4) /* Receive date threshold interrupt */
  36. /* The maximum bytes that a sdma BD can transfer.*/
  37. #define MAX_SDMA_BD_BYTES (1 << 15)
  38. #define MX51_ECSPI_CTRL_MAX_BURST 512
  39. /* The maximum bytes that IMX53_ECSPI can transfer in slave mode.*/
  40. #define MX53_MAX_TRANSFER_BYTES 512
  41. enum spi_imx_devtype {
  42. IMX1_CSPI,
  43. IMX21_CSPI,
  44. IMX27_CSPI,
  45. IMX31_CSPI,
  46. IMX35_CSPI, /* CSPI on all i.mx except above */
  47. IMX51_ECSPI, /* ECSPI on i.mx51 */
  48. IMX53_ECSPI, /* ECSPI on i.mx53 and later */
  49. };
  50. struct spi_imx_data;
  51. struct spi_imx_devtype_data {
  52. void (*intctrl)(struct spi_imx_data *, int);
  53. int (*config)(struct spi_device *);
  54. void (*trigger)(struct spi_imx_data *);
  55. int (*rx_available)(struct spi_imx_data *);
  56. void (*reset)(struct spi_imx_data *);
  57. void (*disable)(struct spi_imx_data *);
  58. bool has_dmamode;
  59. bool has_slavemode;
  60. unsigned int fifo_size;
  61. bool dynamic_burst;
  62. enum spi_imx_devtype devtype;
  63. };
  64. struct spi_imx_data {
  65. struct spi_bitbang bitbang;
  66. struct device *dev;
  67. struct completion xfer_done;
  68. void __iomem *base;
  69. unsigned long base_phys;
  70. struct clk *clk_per;
  71. struct clk *clk_ipg;
  72. unsigned long spi_clk;
  73. unsigned int spi_bus_clk;
  74. unsigned int speed_hz;
  75. unsigned int bits_per_word;
  76. unsigned int spi_drctl;
  77. unsigned int count, remainder;
  78. void (*tx)(struct spi_imx_data *);
  79. void (*rx)(struct spi_imx_data *);
  80. void *rx_buf;
  81. const void *tx_buf;
  82. unsigned int txfifo; /* number of words pushed in tx FIFO */
  83. unsigned int dynamic_burst;
  84. /* Slave mode */
  85. bool slave_mode;
  86. bool slave_aborted;
  87. unsigned int slave_burst;
  88. /* DMA */
  89. bool usedma;
  90. u32 wml;
  91. struct completion dma_rx_completion;
  92. struct completion dma_tx_completion;
  93. const struct spi_imx_devtype_data *devtype_data;
  94. };
  95. static inline int is_imx27_cspi(struct spi_imx_data *d)
  96. {
  97. return d->devtype_data->devtype == IMX27_CSPI;
  98. }
  99. static inline int is_imx35_cspi(struct spi_imx_data *d)
  100. {
  101. return d->devtype_data->devtype == IMX35_CSPI;
  102. }
  103. static inline int is_imx51_ecspi(struct spi_imx_data *d)
  104. {
  105. return d->devtype_data->devtype == IMX51_ECSPI;
  106. }
  107. static inline int is_imx53_ecspi(struct spi_imx_data *d)
  108. {
  109. return d->devtype_data->devtype == IMX53_ECSPI;
  110. }
  111. #define MXC_SPI_BUF_RX(type) \
  112. static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx) \
  113. { \
  114. unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); \
  115. \
  116. if (spi_imx->rx_buf) { \
  117. *(type *)spi_imx->rx_buf = val; \
  118. spi_imx->rx_buf += sizeof(type); \
  119. } \
  120. \
  121. spi_imx->remainder -= sizeof(type); \
  122. }
  123. #define MXC_SPI_BUF_TX(type) \
  124. static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx) \
  125. { \
  126. type val = 0; \
  127. \
  128. if (spi_imx->tx_buf) { \
  129. val = *(type *)spi_imx->tx_buf; \
  130. spi_imx->tx_buf += sizeof(type); \
  131. } \
  132. \
  133. spi_imx->count -= sizeof(type); \
  134. \
  135. writel(val, spi_imx->base + MXC_CSPITXDATA); \
  136. }
  137. MXC_SPI_BUF_RX(u8)
  138. MXC_SPI_BUF_TX(u8)
  139. MXC_SPI_BUF_RX(u16)
  140. MXC_SPI_BUF_TX(u16)
  141. MXC_SPI_BUF_RX(u32)
  142. MXC_SPI_BUF_TX(u32)
  143. /* First entry is reserved, second entry is valid only if SDHC_SPIEN is set
  144. * (which is currently not the case in this driver)
  145. */
  146. static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
  147. 256, 384, 512, 768, 1024};
  148. /* MX21, MX27 */
  149. static unsigned int spi_imx_clkdiv_1(unsigned int fin,
  150. unsigned int fspi, unsigned int max, unsigned int *fres)
  151. {
  152. int i;
  153. for (i = 2; i < max; i++)
  154. if (fspi * mxc_clkdivs[i] >= fin)
  155. break;
  156. *fres = fin / mxc_clkdivs[i];
  157. return i;
  158. }
  159. /* MX1, MX31, MX35, MX51 CSPI */
  160. static unsigned int spi_imx_clkdiv_2(unsigned int fin,
  161. unsigned int fspi, unsigned int *fres)
  162. {
  163. int i, div = 4;
  164. for (i = 0; i < 7; i++) {
  165. if (fspi * div >= fin)
  166. goto out;
  167. div <<= 1;
  168. }
  169. out:
  170. *fres = fin / div;
  171. return i;
  172. }
  173. static int spi_imx_bytes_per_word(const int bits_per_word)
  174. {
  175. if (bits_per_word <= 8)
  176. return 1;
  177. else if (bits_per_word <= 16)
  178. return 2;
  179. else
  180. return 4;
  181. }
  182. static bool spi_imx_can_dma(struct spi_master *master, struct spi_device *spi,
  183. struct spi_transfer *transfer)
  184. {
  185. struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
  186. unsigned int bytes_per_word, i;
  187. if (!master->dma_rx)
  188. return false;
  189. if (spi_imx->slave_mode)
  190. return false;
  191. bytes_per_word = spi_imx_bytes_per_word(transfer->bits_per_word);
  192. for (i = spi_imx->devtype_data->fifo_size / 2; i > 0; i--) {
  193. if (!(transfer->len % (i * bytes_per_word)))
  194. break;
  195. }
  196. spi_imx->wml = i;
  197. spi_imx->dynamic_burst = 0;
  198. return true;
  199. }
  200. #define MX51_ECSPI_CTRL 0x08
  201. #define MX51_ECSPI_CTRL_ENABLE (1 << 0)
  202. #define MX51_ECSPI_CTRL_XCH (1 << 2)
  203. #define MX51_ECSPI_CTRL_SMC (1 << 3)
  204. #define MX51_ECSPI_CTRL_MODE_MASK (0xf << 4)
  205. #define MX51_ECSPI_CTRL_DRCTL(drctl) ((drctl) << 16)
  206. #define MX51_ECSPI_CTRL_POSTDIV_OFFSET 8
  207. #define MX51_ECSPI_CTRL_PREDIV_OFFSET 12
  208. #define MX51_ECSPI_CTRL_CS(cs) ((cs) << 18)
  209. #define MX51_ECSPI_CTRL_BL_OFFSET 20
  210. #define MX51_ECSPI_CTRL_BL_MASK (0xfff << 20)
  211. #define MX51_ECSPI_CONFIG 0x0c
  212. #define MX51_ECSPI_CONFIG_SCLKPHA(cs) (1 << ((cs) + 0))
  213. #define MX51_ECSPI_CONFIG_SCLKPOL(cs) (1 << ((cs) + 4))
  214. #define MX51_ECSPI_CONFIG_SBBCTRL(cs) (1 << ((cs) + 8))
  215. #define MX51_ECSPI_CONFIG_SSBPOL(cs) (1 << ((cs) + 12))
  216. #define MX51_ECSPI_CONFIG_SCLKCTL(cs) (1 << ((cs) + 20))
  217. #define MX51_ECSPI_INT 0x10
  218. #define MX51_ECSPI_INT_TEEN (1 << 0)
  219. #define MX51_ECSPI_INT_RREN (1 << 3)
  220. #define MX51_ECSPI_INT_RDREN (1 << 4)
  221. #define MX51_ECSPI_DMA 0x14
  222. #define MX51_ECSPI_DMA_TX_WML(wml) ((wml) & 0x3f)
  223. #define MX51_ECSPI_DMA_RX_WML(wml) (((wml) & 0x3f) << 16)
  224. #define MX51_ECSPI_DMA_RXT_WML(wml) (((wml) & 0x3f) << 24)
  225. #define MX51_ECSPI_DMA_TEDEN (1 << 7)
  226. #define MX51_ECSPI_DMA_RXDEN (1 << 23)
  227. #define MX51_ECSPI_DMA_RXTDEN (1 << 31)
  228. #define MX51_ECSPI_STAT 0x18
  229. #define MX51_ECSPI_STAT_RR (1 << 3)
  230. #define MX51_ECSPI_TESTREG 0x20
  231. #define MX51_ECSPI_TESTREG_LBC BIT(31)
  232. static void spi_imx_buf_rx_swap_u32(struct spi_imx_data *spi_imx)
  233. {
  234. unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA);
  235. #ifdef __LITTLE_ENDIAN
  236. unsigned int bytes_per_word;
  237. #endif
  238. if (spi_imx->rx_buf) {
  239. #ifdef __LITTLE_ENDIAN
  240. bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
  241. if (bytes_per_word == 1)
  242. val = cpu_to_be32(val);
  243. else if (bytes_per_word == 2)
  244. val = (val << 16) | (val >> 16);
  245. #endif
  246. *(u32 *)spi_imx->rx_buf = val;
  247. spi_imx->rx_buf += sizeof(u32);
  248. }
  249. spi_imx->remainder -= sizeof(u32);
  250. }
  251. static void spi_imx_buf_rx_swap(struct spi_imx_data *spi_imx)
  252. {
  253. int unaligned;
  254. u32 val;
  255. unaligned = spi_imx->remainder % 4;
  256. if (!unaligned) {
  257. spi_imx_buf_rx_swap_u32(spi_imx);
  258. return;
  259. }
  260. if (spi_imx_bytes_per_word(spi_imx->bits_per_word) == 2) {
  261. spi_imx_buf_rx_u16(spi_imx);
  262. return;
  263. }
  264. val = readl(spi_imx->base + MXC_CSPIRXDATA);
  265. while (unaligned--) {
  266. if (spi_imx->rx_buf) {
  267. *(u8 *)spi_imx->rx_buf = (val >> (8 * unaligned)) & 0xff;
  268. spi_imx->rx_buf++;
  269. }
  270. spi_imx->remainder--;
  271. }
  272. }
  273. static void spi_imx_buf_tx_swap_u32(struct spi_imx_data *spi_imx)
  274. {
  275. u32 val = 0;
  276. #ifdef __LITTLE_ENDIAN
  277. unsigned int bytes_per_word;
  278. #endif
  279. if (spi_imx->tx_buf) {
  280. val = *(u32 *)spi_imx->tx_buf;
  281. spi_imx->tx_buf += sizeof(u32);
  282. }
  283. spi_imx->count -= sizeof(u32);
  284. #ifdef __LITTLE_ENDIAN
  285. bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
  286. if (bytes_per_word == 1)
  287. val = cpu_to_be32(val);
  288. else if (bytes_per_word == 2)
  289. val = (val << 16) | (val >> 16);
  290. #endif
  291. writel(val, spi_imx->base + MXC_CSPITXDATA);
  292. }
  293. static void spi_imx_buf_tx_swap(struct spi_imx_data *spi_imx)
  294. {
  295. int unaligned;
  296. u32 val = 0;
  297. unaligned = spi_imx->count % 4;
  298. if (!unaligned) {
  299. spi_imx_buf_tx_swap_u32(spi_imx);
  300. return;
  301. }
  302. if (spi_imx_bytes_per_word(spi_imx->bits_per_word) == 2) {
  303. spi_imx_buf_tx_u16(spi_imx);
  304. return;
  305. }
  306. while (unaligned--) {
  307. if (spi_imx->tx_buf) {
  308. val |= *(u8 *)spi_imx->tx_buf << (8 * unaligned);
  309. spi_imx->tx_buf++;
  310. }
  311. spi_imx->count--;
  312. }
  313. writel(val, spi_imx->base + MXC_CSPITXDATA);
  314. }
  315. static void mx53_ecspi_rx_slave(struct spi_imx_data *spi_imx)
  316. {
  317. u32 val = be32_to_cpu(readl(spi_imx->base + MXC_CSPIRXDATA));
  318. if (spi_imx->rx_buf) {
  319. int n_bytes = spi_imx->slave_burst % sizeof(val);
  320. if (!n_bytes)
  321. n_bytes = sizeof(val);
  322. memcpy(spi_imx->rx_buf,
  323. ((u8 *)&val) + sizeof(val) - n_bytes, n_bytes);
  324. spi_imx->rx_buf += n_bytes;
  325. spi_imx->slave_burst -= n_bytes;
  326. }
  327. spi_imx->remainder -= sizeof(u32);
  328. }
  329. static void mx53_ecspi_tx_slave(struct spi_imx_data *spi_imx)
  330. {
  331. u32 val = 0;
  332. int n_bytes = spi_imx->count % sizeof(val);
  333. if (!n_bytes)
  334. n_bytes = sizeof(val);
  335. if (spi_imx->tx_buf) {
  336. memcpy(((u8 *)&val) + sizeof(val) - n_bytes,
  337. spi_imx->tx_buf, n_bytes);
  338. val = cpu_to_be32(val);
  339. spi_imx->tx_buf += n_bytes;
  340. }
  341. spi_imx->count -= n_bytes;
  342. writel(val, spi_imx->base + MXC_CSPITXDATA);
  343. }
  344. /* MX51 eCSPI */
  345. static unsigned int mx51_ecspi_clkdiv(struct spi_imx_data *spi_imx,
  346. unsigned int fspi, unsigned int *fres)
  347. {
  348. /*
  349. * there are two 4-bit dividers, the pre-divider divides by
  350. * $pre, the post-divider by 2^$post
  351. */
  352. unsigned int pre, post;
  353. unsigned int fin = spi_imx->spi_clk;
  354. if (unlikely(fspi > fin))
  355. return 0;
  356. post = fls(fin) - fls(fspi);
  357. if (fin > fspi << post)
  358. post++;
  359. /* now we have: (fin <= fspi << post) with post being minimal */
  360. post = max(4U, post) - 4;
  361. if (unlikely(post > 0xf)) {
  362. dev_err(spi_imx->dev, "cannot set clock freq: %u (base freq: %u)\n",
  363. fspi, fin);
  364. return 0xff;
  365. }
  366. pre = DIV_ROUND_UP(fin, fspi << post) - 1;
  367. dev_dbg(spi_imx->dev, "%s: fin: %u, fspi: %u, post: %u, pre: %u\n",
  368. __func__, fin, fspi, post, pre);
  369. /* Resulting frequency for the SCLK line. */
  370. *fres = (fin / (pre + 1)) >> post;
  371. return (pre << MX51_ECSPI_CTRL_PREDIV_OFFSET) |
  372. (post << MX51_ECSPI_CTRL_POSTDIV_OFFSET);
  373. }
  374. static void mx51_ecspi_intctrl(struct spi_imx_data *spi_imx, int enable)
  375. {
  376. unsigned val = 0;
  377. if (enable & MXC_INT_TE)
  378. val |= MX51_ECSPI_INT_TEEN;
  379. if (enable & MXC_INT_RR)
  380. val |= MX51_ECSPI_INT_RREN;
  381. if (enable & MXC_INT_RDR)
  382. val |= MX51_ECSPI_INT_RDREN;
  383. writel(val, spi_imx->base + MX51_ECSPI_INT);
  384. }
  385. static void mx51_ecspi_trigger(struct spi_imx_data *spi_imx)
  386. {
  387. u32 reg;
  388. reg = readl(spi_imx->base + MX51_ECSPI_CTRL);
  389. reg |= MX51_ECSPI_CTRL_XCH;
  390. writel(reg, spi_imx->base + MX51_ECSPI_CTRL);
  391. }
  392. static void mx51_ecspi_disable(struct spi_imx_data *spi_imx)
  393. {
  394. u32 ctrl;
  395. ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
  396. ctrl &= ~MX51_ECSPI_CTRL_ENABLE;
  397. writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
  398. }
  399. static int mx51_ecspi_config(struct spi_device *spi)
  400. {
  401. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  402. u32 ctrl = MX51_ECSPI_CTRL_ENABLE;
  403. u32 clk = spi_imx->speed_hz, delay, reg;
  404. u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
  405. /* set Master or Slave mode */
  406. if (spi_imx->slave_mode)
  407. ctrl &= ~MX51_ECSPI_CTRL_MODE_MASK;
  408. else
  409. ctrl |= MX51_ECSPI_CTRL_MODE_MASK;
  410. /*
  411. * Enable SPI_RDY handling (falling edge/level triggered).
  412. */
  413. if (spi->mode & SPI_READY)
  414. ctrl |= MX51_ECSPI_CTRL_DRCTL(spi_imx->spi_drctl);
  415. /* set clock speed */
  416. ctrl |= mx51_ecspi_clkdiv(spi_imx, spi_imx->speed_hz, &clk);
  417. spi_imx->spi_bus_clk = clk;
  418. /* set chip select to use */
  419. ctrl |= MX51_ECSPI_CTRL_CS(spi->chip_select);
  420. if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx))
  421. ctrl |= (spi_imx->slave_burst * 8 - 1)
  422. << MX51_ECSPI_CTRL_BL_OFFSET;
  423. else
  424. ctrl |= (spi_imx->bits_per_word - 1)
  425. << MX51_ECSPI_CTRL_BL_OFFSET;
  426. /*
  427. * eCSPI burst completion by Chip Select signal in Slave mode
  428. * is not functional for imx53 Soc, config SPI burst completed when
  429. * BURST_LENGTH + 1 bits are received
  430. */
  431. if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx))
  432. cfg &= ~MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
  433. else
  434. cfg |= MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
  435. if (spi->mode & SPI_CPHA)
  436. cfg |= MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
  437. else
  438. cfg &= ~MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
  439. if (spi->mode & SPI_CPOL) {
  440. cfg |= MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
  441. cfg |= MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
  442. } else {
  443. cfg &= ~MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
  444. cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
  445. }
  446. if (spi->mode & SPI_CS_HIGH)
  447. cfg |= MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
  448. else
  449. cfg &= ~MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
  450. if (spi_imx->usedma)
  451. ctrl |= MX51_ECSPI_CTRL_SMC;
  452. /* CTRL register always go first to bring out controller from reset */
  453. writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
  454. reg = readl(spi_imx->base + MX51_ECSPI_TESTREG);
  455. if (spi->mode & SPI_LOOP)
  456. reg |= MX51_ECSPI_TESTREG_LBC;
  457. else
  458. reg &= ~MX51_ECSPI_TESTREG_LBC;
  459. writel(reg, spi_imx->base + MX51_ECSPI_TESTREG);
  460. writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
  461. /*
  462. * Wait until the changes in the configuration register CONFIGREG
  463. * propagate into the hardware. It takes exactly one tick of the
  464. * SCLK clock, but we will wait two SCLK clock just to be sure. The
  465. * effect of the delay it takes for the hardware to apply changes
  466. * is noticable if the SCLK clock run very slow. In such a case, if
  467. * the polarity of SCLK should be inverted, the GPIO ChipSelect might
  468. * be asserted before the SCLK polarity changes, which would disrupt
  469. * the SPI communication as the device on the other end would consider
  470. * the change of SCLK polarity as a clock tick already.
  471. */
  472. delay = (2 * 1000000) / clk;
  473. if (likely(delay < 10)) /* SCLK is faster than 100 kHz */
  474. udelay(delay);
  475. else /* SCLK is _very_ slow */
  476. usleep_range(delay, delay + 10);
  477. /*
  478. * Configure the DMA register: setup the watermark
  479. * and enable DMA request.
  480. */
  481. writel(MX51_ECSPI_DMA_RX_WML(spi_imx->wml) |
  482. MX51_ECSPI_DMA_TX_WML(spi_imx->wml) |
  483. MX51_ECSPI_DMA_RXT_WML(spi_imx->wml) |
  484. MX51_ECSPI_DMA_TEDEN | MX51_ECSPI_DMA_RXDEN |
  485. MX51_ECSPI_DMA_RXTDEN, spi_imx->base + MX51_ECSPI_DMA);
  486. return 0;
  487. }
  488. static int mx51_ecspi_rx_available(struct spi_imx_data *spi_imx)
  489. {
  490. return readl(spi_imx->base + MX51_ECSPI_STAT) & MX51_ECSPI_STAT_RR;
  491. }
  492. static void mx51_ecspi_reset(struct spi_imx_data *spi_imx)
  493. {
  494. /* drain receive buffer */
  495. while (mx51_ecspi_rx_available(spi_imx))
  496. readl(spi_imx->base + MXC_CSPIRXDATA);
  497. }
  498. #define MX31_INTREG_TEEN (1 << 0)
  499. #define MX31_INTREG_RREN (1 << 3)
  500. #define MX31_CSPICTRL_ENABLE (1 << 0)
  501. #define MX31_CSPICTRL_MASTER (1 << 1)
  502. #define MX31_CSPICTRL_XCH (1 << 2)
  503. #define MX31_CSPICTRL_SMC (1 << 3)
  504. #define MX31_CSPICTRL_POL (1 << 4)
  505. #define MX31_CSPICTRL_PHA (1 << 5)
  506. #define MX31_CSPICTRL_SSCTL (1 << 6)
  507. #define MX31_CSPICTRL_SSPOL (1 << 7)
  508. #define MX31_CSPICTRL_BC_SHIFT 8
  509. #define MX35_CSPICTRL_BL_SHIFT 20
  510. #define MX31_CSPICTRL_CS_SHIFT 24
  511. #define MX35_CSPICTRL_CS_SHIFT 12
  512. #define MX31_CSPICTRL_DR_SHIFT 16
  513. #define MX31_CSPI_DMAREG 0x10
  514. #define MX31_DMAREG_RH_DEN (1<<4)
  515. #define MX31_DMAREG_TH_DEN (1<<1)
  516. #define MX31_CSPISTATUS 0x14
  517. #define MX31_STATUS_RR (1 << 3)
  518. #define MX31_CSPI_TESTREG 0x1C
  519. #define MX31_TEST_LBC (1 << 14)
  520. /* These functions also work for the i.MX35, but be aware that
  521. * the i.MX35 has a slightly different register layout for bits
  522. * we do not use here.
  523. */
  524. static void mx31_intctrl(struct spi_imx_data *spi_imx, int enable)
  525. {
  526. unsigned int val = 0;
  527. if (enable & MXC_INT_TE)
  528. val |= MX31_INTREG_TEEN;
  529. if (enable & MXC_INT_RR)
  530. val |= MX31_INTREG_RREN;
  531. writel(val, spi_imx->base + MXC_CSPIINT);
  532. }
  533. static void mx31_trigger(struct spi_imx_data *spi_imx)
  534. {
  535. unsigned int reg;
  536. reg = readl(spi_imx->base + MXC_CSPICTRL);
  537. reg |= MX31_CSPICTRL_XCH;
  538. writel(reg, spi_imx->base + MXC_CSPICTRL);
  539. }
  540. static int mx31_config(struct spi_device *spi)
  541. {
  542. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  543. unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER;
  544. unsigned int clk;
  545. reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->speed_hz, &clk) <<
  546. MX31_CSPICTRL_DR_SHIFT;
  547. spi_imx->spi_bus_clk = clk;
  548. if (is_imx35_cspi(spi_imx)) {
  549. reg |= (spi_imx->bits_per_word - 1) << MX35_CSPICTRL_BL_SHIFT;
  550. reg |= MX31_CSPICTRL_SSCTL;
  551. } else {
  552. reg |= (spi_imx->bits_per_word - 1) << MX31_CSPICTRL_BC_SHIFT;
  553. }
  554. if (spi->mode & SPI_CPHA)
  555. reg |= MX31_CSPICTRL_PHA;
  556. if (spi->mode & SPI_CPOL)
  557. reg |= MX31_CSPICTRL_POL;
  558. if (spi->mode & SPI_CS_HIGH)
  559. reg |= MX31_CSPICTRL_SSPOL;
  560. if (!gpio_is_valid(spi->cs_gpio))
  561. reg |= (spi->chip_select) <<
  562. (is_imx35_cspi(spi_imx) ? MX35_CSPICTRL_CS_SHIFT :
  563. MX31_CSPICTRL_CS_SHIFT);
  564. if (spi_imx->usedma)
  565. reg |= MX31_CSPICTRL_SMC;
  566. writel(reg, spi_imx->base + MXC_CSPICTRL);
  567. reg = readl(spi_imx->base + MX31_CSPI_TESTREG);
  568. if (spi->mode & SPI_LOOP)
  569. reg |= MX31_TEST_LBC;
  570. else
  571. reg &= ~MX31_TEST_LBC;
  572. writel(reg, spi_imx->base + MX31_CSPI_TESTREG);
  573. if (spi_imx->usedma) {
  574. /* configure DMA requests when RXFIFO is half full and
  575. when TXFIFO is half empty */
  576. writel(MX31_DMAREG_RH_DEN | MX31_DMAREG_TH_DEN,
  577. spi_imx->base + MX31_CSPI_DMAREG);
  578. }
  579. return 0;
  580. }
  581. static int mx31_rx_available(struct spi_imx_data *spi_imx)
  582. {
  583. return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR;
  584. }
  585. static void mx31_reset(struct spi_imx_data *spi_imx)
  586. {
  587. /* drain receive buffer */
  588. while (readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR)
  589. readl(spi_imx->base + MXC_CSPIRXDATA);
  590. }
  591. #define MX21_INTREG_RR (1 << 4)
  592. #define MX21_INTREG_TEEN (1 << 9)
  593. #define MX21_INTREG_RREN (1 << 13)
  594. #define MX21_CSPICTRL_POL (1 << 5)
  595. #define MX21_CSPICTRL_PHA (1 << 6)
  596. #define MX21_CSPICTRL_SSPOL (1 << 8)
  597. #define MX21_CSPICTRL_XCH (1 << 9)
  598. #define MX21_CSPICTRL_ENABLE (1 << 10)
  599. #define MX21_CSPICTRL_MASTER (1 << 11)
  600. #define MX21_CSPICTRL_DR_SHIFT 14
  601. #define MX21_CSPICTRL_CS_SHIFT 19
  602. static void mx21_intctrl(struct spi_imx_data *spi_imx, int enable)
  603. {
  604. unsigned int val = 0;
  605. if (enable & MXC_INT_TE)
  606. val |= MX21_INTREG_TEEN;
  607. if (enable & MXC_INT_RR)
  608. val |= MX21_INTREG_RREN;
  609. writel(val, spi_imx->base + MXC_CSPIINT);
  610. }
  611. static void mx21_trigger(struct spi_imx_data *spi_imx)
  612. {
  613. unsigned int reg;
  614. reg = readl(spi_imx->base + MXC_CSPICTRL);
  615. reg |= MX21_CSPICTRL_XCH;
  616. writel(reg, spi_imx->base + MXC_CSPICTRL);
  617. }
  618. static int mx21_config(struct spi_device *spi)
  619. {
  620. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  621. unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_MASTER;
  622. unsigned int max = is_imx27_cspi(spi_imx) ? 16 : 18;
  623. unsigned int clk;
  624. reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, spi_imx->speed_hz, max, &clk)
  625. << MX21_CSPICTRL_DR_SHIFT;
  626. spi_imx->spi_bus_clk = clk;
  627. reg |= spi_imx->bits_per_word - 1;
  628. if (spi->mode & SPI_CPHA)
  629. reg |= MX21_CSPICTRL_PHA;
  630. if (spi->mode & SPI_CPOL)
  631. reg |= MX21_CSPICTRL_POL;
  632. if (spi->mode & SPI_CS_HIGH)
  633. reg |= MX21_CSPICTRL_SSPOL;
  634. if (!gpio_is_valid(spi->cs_gpio))
  635. reg |= spi->chip_select << MX21_CSPICTRL_CS_SHIFT;
  636. writel(reg, spi_imx->base + MXC_CSPICTRL);
  637. return 0;
  638. }
  639. static int mx21_rx_available(struct spi_imx_data *spi_imx)
  640. {
  641. return readl(spi_imx->base + MXC_CSPIINT) & MX21_INTREG_RR;
  642. }
  643. static void mx21_reset(struct spi_imx_data *spi_imx)
  644. {
  645. writel(1, spi_imx->base + MXC_RESET);
  646. }
  647. #define MX1_INTREG_RR (1 << 3)
  648. #define MX1_INTREG_TEEN (1 << 8)
  649. #define MX1_INTREG_RREN (1 << 11)
  650. #define MX1_CSPICTRL_POL (1 << 4)
  651. #define MX1_CSPICTRL_PHA (1 << 5)
  652. #define MX1_CSPICTRL_XCH (1 << 8)
  653. #define MX1_CSPICTRL_ENABLE (1 << 9)
  654. #define MX1_CSPICTRL_MASTER (1 << 10)
  655. #define MX1_CSPICTRL_DR_SHIFT 13
  656. static void mx1_intctrl(struct spi_imx_data *spi_imx, int enable)
  657. {
  658. unsigned int val = 0;
  659. if (enable & MXC_INT_TE)
  660. val |= MX1_INTREG_TEEN;
  661. if (enable & MXC_INT_RR)
  662. val |= MX1_INTREG_RREN;
  663. writel(val, spi_imx->base + MXC_CSPIINT);
  664. }
  665. static void mx1_trigger(struct spi_imx_data *spi_imx)
  666. {
  667. unsigned int reg;
  668. reg = readl(spi_imx->base + MXC_CSPICTRL);
  669. reg |= MX1_CSPICTRL_XCH;
  670. writel(reg, spi_imx->base + MXC_CSPICTRL);
  671. }
  672. static int mx1_config(struct spi_device *spi)
  673. {
  674. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  675. unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER;
  676. unsigned int clk;
  677. reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->speed_hz, &clk) <<
  678. MX1_CSPICTRL_DR_SHIFT;
  679. spi_imx->spi_bus_clk = clk;
  680. reg |= spi_imx->bits_per_word - 1;
  681. if (spi->mode & SPI_CPHA)
  682. reg |= MX1_CSPICTRL_PHA;
  683. if (spi->mode & SPI_CPOL)
  684. reg |= MX1_CSPICTRL_POL;
  685. writel(reg, spi_imx->base + MXC_CSPICTRL);
  686. return 0;
  687. }
  688. static int mx1_rx_available(struct spi_imx_data *spi_imx)
  689. {
  690. return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR;
  691. }
  692. static void mx1_reset(struct spi_imx_data *spi_imx)
  693. {
  694. writel(1, spi_imx->base + MXC_RESET);
  695. }
  696. static struct spi_imx_devtype_data imx1_cspi_devtype_data = {
  697. .intctrl = mx1_intctrl,
  698. .config = mx1_config,
  699. .trigger = mx1_trigger,
  700. .rx_available = mx1_rx_available,
  701. .reset = mx1_reset,
  702. .fifo_size = 8,
  703. .has_dmamode = false,
  704. .dynamic_burst = false,
  705. .has_slavemode = false,
  706. .devtype = IMX1_CSPI,
  707. };
  708. static struct spi_imx_devtype_data imx21_cspi_devtype_data = {
  709. .intctrl = mx21_intctrl,
  710. .config = mx21_config,
  711. .trigger = mx21_trigger,
  712. .rx_available = mx21_rx_available,
  713. .reset = mx21_reset,
  714. .fifo_size = 8,
  715. .has_dmamode = false,
  716. .dynamic_burst = false,
  717. .has_slavemode = false,
  718. .devtype = IMX21_CSPI,
  719. };
  720. static struct spi_imx_devtype_data imx27_cspi_devtype_data = {
  721. /* i.mx27 cspi shares the functions with i.mx21 one */
  722. .intctrl = mx21_intctrl,
  723. .config = mx21_config,
  724. .trigger = mx21_trigger,
  725. .rx_available = mx21_rx_available,
  726. .reset = mx21_reset,
  727. .fifo_size = 8,
  728. .has_dmamode = false,
  729. .dynamic_burst = false,
  730. .has_slavemode = false,
  731. .devtype = IMX27_CSPI,
  732. };
  733. static struct spi_imx_devtype_data imx31_cspi_devtype_data = {
  734. .intctrl = mx31_intctrl,
  735. .config = mx31_config,
  736. .trigger = mx31_trigger,
  737. .rx_available = mx31_rx_available,
  738. .reset = mx31_reset,
  739. .fifo_size = 8,
  740. .has_dmamode = false,
  741. .dynamic_burst = false,
  742. .has_slavemode = false,
  743. .devtype = IMX31_CSPI,
  744. };
  745. static struct spi_imx_devtype_data imx35_cspi_devtype_data = {
  746. /* i.mx35 and later cspi shares the functions with i.mx31 one */
  747. .intctrl = mx31_intctrl,
  748. .config = mx31_config,
  749. .trigger = mx31_trigger,
  750. .rx_available = mx31_rx_available,
  751. .reset = mx31_reset,
  752. .fifo_size = 8,
  753. .has_dmamode = true,
  754. .dynamic_burst = false,
  755. .has_slavemode = false,
  756. .devtype = IMX35_CSPI,
  757. };
  758. static struct spi_imx_devtype_data imx51_ecspi_devtype_data = {
  759. .intctrl = mx51_ecspi_intctrl,
  760. .config = mx51_ecspi_config,
  761. .trigger = mx51_ecspi_trigger,
  762. .rx_available = mx51_ecspi_rx_available,
  763. .reset = mx51_ecspi_reset,
  764. .fifo_size = 64,
  765. .has_dmamode = true,
  766. .dynamic_burst = true,
  767. .has_slavemode = true,
  768. .disable = mx51_ecspi_disable,
  769. .devtype = IMX51_ECSPI,
  770. };
  771. static struct spi_imx_devtype_data imx53_ecspi_devtype_data = {
  772. .intctrl = mx51_ecspi_intctrl,
  773. .config = mx51_ecspi_config,
  774. .trigger = mx51_ecspi_trigger,
  775. .rx_available = mx51_ecspi_rx_available,
  776. .reset = mx51_ecspi_reset,
  777. .fifo_size = 64,
  778. .has_dmamode = true,
  779. .has_slavemode = true,
  780. .disable = mx51_ecspi_disable,
  781. .devtype = IMX53_ECSPI,
  782. };
  783. static const struct platform_device_id spi_imx_devtype[] = {
  784. {
  785. .name = "imx1-cspi",
  786. .driver_data = (kernel_ulong_t) &imx1_cspi_devtype_data,
  787. }, {
  788. .name = "imx21-cspi",
  789. .driver_data = (kernel_ulong_t) &imx21_cspi_devtype_data,
  790. }, {
  791. .name = "imx27-cspi",
  792. .driver_data = (kernel_ulong_t) &imx27_cspi_devtype_data,
  793. }, {
  794. .name = "imx31-cspi",
  795. .driver_data = (kernel_ulong_t) &imx31_cspi_devtype_data,
  796. }, {
  797. .name = "imx35-cspi",
  798. .driver_data = (kernel_ulong_t) &imx35_cspi_devtype_data,
  799. }, {
  800. .name = "imx51-ecspi",
  801. .driver_data = (kernel_ulong_t) &imx51_ecspi_devtype_data,
  802. }, {
  803. .name = "imx53-ecspi",
  804. .driver_data = (kernel_ulong_t) &imx53_ecspi_devtype_data,
  805. }, {
  806. /* sentinel */
  807. }
  808. };
  809. static const struct of_device_id spi_imx_dt_ids[] = {
  810. { .compatible = "fsl,imx1-cspi", .data = &imx1_cspi_devtype_data, },
  811. { .compatible = "fsl,imx21-cspi", .data = &imx21_cspi_devtype_data, },
  812. { .compatible = "fsl,imx27-cspi", .data = &imx27_cspi_devtype_data, },
  813. { .compatible = "fsl,imx31-cspi", .data = &imx31_cspi_devtype_data, },
  814. { .compatible = "fsl,imx35-cspi", .data = &imx35_cspi_devtype_data, },
  815. { .compatible = "fsl,imx51-ecspi", .data = &imx51_ecspi_devtype_data, },
  816. { .compatible = "fsl,imx53-ecspi", .data = &imx53_ecspi_devtype_data, },
  817. { /* sentinel */ }
  818. };
  819. MODULE_DEVICE_TABLE(of, spi_imx_dt_ids);
  820. static void spi_imx_chipselect(struct spi_device *spi, int is_active)
  821. {
  822. int active = is_active != BITBANG_CS_INACTIVE;
  823. int dev_is_lowactive = !(spi->mode & SPI_CS_HIGH);
  824. if (spi->mode & SPI_NO_CS)
  825. return;
  826. if (!gpio_is_valid(spi->cs_gpio))
  827. return;
  828. gpio_set_value(spi->cs_gpio, dev_is_lowactive ^ active);
  829. }
  830. static void spi_imx_set_burst_len(struct spi_imx_data *spi_imx, int n_bits)
  831. {
  832. u32 ctrl;
  833. ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
  834. ctrl &= ~MX51_ECSPI_CTRL_BL_MASK;
  835. ctrl |= ((n_bits - 1) << MX51_ECSPI_CTRL_BL_OFFSET);
  836. writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
  837. }
  838. static void spi_imx_push(struct spi_imx_data *spi_imx)
  839. {
  840. unsigned int burst_len, fifo_words;
  841. if (spi_imx->dynamic_burst)
  842. fifo_words = 4;
  843. else
  844. fifo_words = spi_imx_bytes_per_word(spi_imx->bits_per_word);
  845. /*
  846. * Reload the FIFO when the remaining bytes to be transferred in the
  847. * current burst is 0. This only applies when bits_per_word is a
  848. * multiple of 8.
  849. */
  850. if (!spi_imx->remainder) {
  851. if (spi_imx->dynamic_burst) {
  852. /* We need to deal unaligned data first */
  853. burst_len = spi_imx->count % MX51_ECSPI_CTRL_MAX_BURST;
  854. if (!burst_len)
  855. burst_len = MX51_ECSPI_CTRL_MAX_BURST;
  856. spi_imx_set_burst_len(spi_imx, burst_len * 8);
  857. spi_imx->remainder = burst_len;
  858. } else {
  859. spi_imx->remainder = fifo_words;
  860. }
  861. }
  862. while (spi_imx->txfifo < spi_imx->devtype_data->fifo_size) {
  863. if (!spi_imx->count)
  864. break;
  865. if (spi_imx->dynamic_burst &&
  866. spi_imx->txfifo >= DIV_ROUND_UP(spi_imx->remainder,
  867. fifo_words))
  868. break;
  869. spi_imx->tx(spi_imx);
  870. spi_imx->txfifo++;
  871. }
  872. if (!spi_imx->slave_mode)
  873. spi_imx->devtype_data->trigger(spi_imx);
  874. }
  875. static irqreturn_t spi_imx_isr(int irq, void *dev_id)
  876. {
  877. struct spi_imx_data *spi_imx = dev_id;
  878. while (spi_imx->txfifo &&
  879. spi_imx->devtype_data->rx_available(spi_imx)) {
  880. spi_imx->rx(spi_imx);
  881. spi_imx->txfifo--;
  882. }
  883. if (spi_imx->count) {
  884. spi_imx_push(spi_imx);
  885. return IRQ_HANDLED;
  886. }
  887. if (spi_imx->txfifo) {
  888. /* No data left to push, but still waiting for rx data,
  889. * enable receive data available interrupt.
  890. */
  891. spi_imx->devtype_data->intctrl(
  892. spi_imx, MXC_INT_RR);
  893. return IRQ_HANDLED;
  894. }
  895. spi_imx->devtype_data->intctrl(spi_imx, 0);
  896. complete(&spi_imx->xfer_done);
  897. return IRQ_HANDLED;
  898. }
  899. static int spi_imx_dma_configure(struct spi_master *master)
  900. {
  901. int ret;
  902. enum dma_slave_buswidth buswidth;
  903. struct dma_slave_config rx = {}, tx = {};
  904. struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
  905. switch (spi_imx_bytes_per_word(spi_imx->bits_per_word)) {
  906. case 4:
  907. buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
  908. break;
  909. case 2:
  910. buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
  911. break;
  912. case 1:
  913. buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
  914. break;
  915. default:
  916. return -EINVAL;
  917. }
  918. tx.direction = DMA_MEM_TO_DEV;
  919. tx.dst_addr = spi_imx->base_phys + MXC_CSPITXDATA;
  920. tx.dst_addr_width = buswidth;
  921. tx.dst_maxburst = spi_imx->wml;
  922. ret = dmaengine_slave_config(master->dma_tx, &tx);
  923. if (ret) {
  924. dev_err(spi_imx->dev, "TX dma configuration failed with %d\n", ret);
  925. return ret;
  926. }
  927. rx.direction = DMA_DEV_TO_MEM;
  928. rx.src_addr = spi_imx->base_phys + MXC_CSPIRXDATA;
  929. rx.src_addr_width = buswidth;
  930. rx.src_maxburst = spi_imx->wml;
  931. ret = dmaengine_slave_config(master->dma_rx, &rx);
  932. if (ret) {
  933. dev_err(spi_imx->dev, "RX dma configuration failed with %d\n", ret);
  934. return ret;
  935. }
  936. return 0;
  937. }
  938. static int spi_imx_setupxfer(struct spi_device *spi,
  939. struct spi_transfer *t)
  940. {
  941. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  942. int ret;
  943. if (!t)
  944. return 0;
  945. spi_imx->bits_per_word = t->bits_per_word;
  946. spi_imx->speed_hz = t->speed_hz;
  947. /*
  948. * Initialize the functions for transfer. To transfer non byte-aligned
  949. * words, we have to use multiple word-size bursts, we can't use
  950. * dynamic_burst in that case.
  951. */
  952. if (spi_imx->devtype_data->dynamic_burst && !spi_imx->slave_mode &&
  953. (spi_imx->bits_per_word == 8 ||
  954. spi_imx->bits_per_word == 16 ||
  955. spi_imx->bits_per_word == 32)) {
  956. spi_imx->rx = spi_imx_buf_rx_swap;
  957. spi_imx->tx = spi_imx_buf_tx_swap;
  958. spi_imx->dynamic_burst = 1;
  959. } else {
  960. if (spi_imx->bits_per_word <= 8) {
  961. spi_imx->rx = spi_imx_buf_rx_u8;
  962. spi_imx->tx = spi_imx_buf_tx_u8;
  963. } else if (spi_imx->bits_per_word <= 16) {
  964. spi_imx->rx = spi_imx_buf_rx_u16;
  965. spi_imx->tx = spi_imx_buf_tx_u16;
  966. } else {
  967. spi_imx->rx = spi_imx_buf_rx_u32;
  968. spi_imx->tx = spi_imx_buf_tx_u32;
  969. }
  970. spi_imx->dynamic_burst = 0;
  971. }
  972. if (spi_imx_can_dma(spi_imx->bitbang.master, spi, t))
  973. spi_imx->usedma = 1;
  974. else
  975. spi_imx->usedma = 0;
  976. if (spi_imx->usedma) {
  977. ret = spi_imx_dma_configure(spi->master);
  978. if (ret)
  979. return ret;
  980. }
  981. if (is_imx53_ecspi(spi_imx) && spi_imx->slave_mode) {
  982. spi_imx->rx = mx53_ecspi_rx_slave;
  983. spi_imx->tx = mx53_ecspi_tx_slave;
  984. spi_imx->slave_burst = t->len;
  985. }
  986. spi_imx->devtype_data->config(spi);
  987. return 0;
  988. }
  989. static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx)
  990. {
  991. struct spi_master *master = spi_imx->bitbang.master;
  992. if (master->dma_rx) {
  993. dma_release_channel(master->dma_rx);
  994. master->dma_rx = NULL;
  995. }
  996. if (master->dma_tx) {
  997. dma_release_channel(master->dma_tx);
  998. master->dma_tx = NULL;
  999. }
  1000. }
  1001. static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx,
  1002. struct spi_master *master)
  1003. {
  1004. int ret;
  1005. /* use pio mode for i.mx6dl chip TKT238285 */
  1006. if (of_machine_is_compatible("fsl,imx6dl"))
  1007. return 0;
  1008. spi_imx->wml = spi_imx->devtype_data->fifo_size / 2;
  1009. /* Prepare for TX DMA: */
  1010. master->dma_tx = dma_request_slave_channel_reason(dev, "tx");
  1011. if (IS_ERR(master->dma_tx)) {
  1012. ret = PTR_ERR(master->dma_tx);
  1013. dev_dbg(dev, "can't get the TX DMA channel, error %d!\n", ret);
  1014. master->dma_tx = NULL;
  1015. goto err;
  1016. }
  1017. /* Prepare for RX : */
  1018. master->dma_rx = dma_request_slave_channel_reason(dev, "rx");
  1019. if (IS_ERR(master->dma_rx)) {
  1020. ret = PTR_ERR(master->dma_rx);
  1021. dev_dbg(dev, "can't get the RX DMA channel, error %d\n", ret);
  1022. master->dma_rx = NULL;
  1023. goto err;
  1024. }
  1025. init_completion(&spi_imx->dma_rx_completion);
  1026. init_completion(&spi_imx->dma_tx_completion);
  1027. master->can_dma = spi_imx_can_dma;
  1028. master->max_dma_len = MAX_SDMA_BD_BYTES;
  1029. spi_imx->bitbang.master->flags = SPI_MASTER_MUST_RX |
  1030. SPI_MASTER_MUST_TX;
  1031. return 0;
  1032. err:
  1033. spi_imx_sdma_exit(spi_imx);
  1034. return ret;
  1035. }
  1036. static void spi_imx_dma_rx_callback(void *cookie)
  1037. {
  1038. struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
  1039. complete(&spi_imx->dma_rx_completion);
  1040. }
  1041. static void spi_imx_dma_tx_callback(void *cookie)
  1042. {
  1043. struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
  1044. complete(&spi_imx->dma_tx_completion);
  1045. }
  1046. static int spi_imx_calculate_timeout(struct spi_imx_data *spi_imx, int size)
  1047. {
  1048. unsigned long timeout = 0;
  1049. /* Time with actual data transfer and CS change delay related to HW */
  1050. timeout = (8 + 4) * size / spi_imx->spi_bus_clk;
  1051. /* Add extra second for scheduler related activities */
  1052. timeout += 1;
  1053. /* Double calculated timeout */
  1054. return msecs_to_jiffies(2 * timeout * MSEC_PER_SEC);
  1055. }
  1056. static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx,
  1057. struct spi_transfer *transfer)
  1058. {
  1059. struct dma_async_tx_descriptor *desc_tx, *desc_rx;
  1060. unsigned long transfer_timeout;
  1061. unsigned long timeout;
  1062. struct spi_master *master = spi_imx->bitbang.master;
  1063. struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;
  1064. /*
  1065. * The TX DMA setup starts the transfer, so make sure RX is configured
  1066. * before TX.
  1067. */
  1068. desc_rx = dmaengine_prep_slave_sg(master->dma_rx,
  1069. rx->sgl, rx->nents, DMA_DEV_TO_MEM,
  1070. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1071. if (!desc_rx)
  1072. return -EINVAL;
  1073. desc_rx->callback = spi_imx_dma_rx_callback;
  1074. desc_rx->callback_param = (void *)spi_imx;
  1075. dmaengine_submit(desc_rx);
  1076. reinit_completion(&spi_imx->dma_rx_completion);
  1077. dma_async_issue_pending(master->dma_rx);
  1078. desc_tx = dmaengine_prep_slave_sg(master->dma_tx,
  1079. tx->sgl, tx->nents, DMA_MEM_TO_DEV,
  1080. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1081. if (!desc_tx) {
  1082. dmaengine_terminate_all(master->dma_tx);
  1083. return -EINVAL;
  1084. }
  1085. desc_tx->callback = spi_imx_dma_tx_callback;
  1086. desc_tx->callback_param = (void *)spi_imx;
  1087. dmaengine_submit(desc_tx);
  1088. reinit_completion(&spi_imx->dma_tx_completion);
  1089. dma_async_issue_pending(master->dma_tx);
  1090. transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
  1091. /* Wait SDMA to finish the data transfer.*/
  1092. timeout = wait_for_completion_timeout(&spi_imx->dma_tx_completion,
  1093. transfer_timeout);
  1094. if (!timeout) {
  1095. dev_err(spi_imx->dev, "I/O Error in DMA TX\n");
  1096. dmaengine_terminate_all(master->dma_tx);
  1097. dmaengine_terminate_all(master->dma_rx);
  1098. return -ETIMEDOUT;
  1099. }
  1100. timeout = wait_for_completion_timeout(&spi_imx->dma_rx_completion,
  1101. transfer_timeout);
  1102. if (!timeout) {
  1103. dev_err(&master->dev, "I/O Error in DMA RX\n");
  1104. spi_imx->devtype_data->reset(spi_imx);
  1105. dmaengine_terminate_all(master->dma_rx);
  1106. return -ETIMEDOUT;
  1107. }
  1108. return transfer->len;
  1109. }
  1110. static int spi_imx_pio_transfer(struct spi_device *spi,
  1111. struct spi_transfer *transfer)
  1112. {
  1113. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  1114. unsigned long transfer_timeout;
  1115. unsigned long timeout;
  1116. spi_imx->tx_buf = transfer->tx_buf;
  1117. spi_imx->rx_buf = transfer->rx_buf;
  1118. spi_imx->count = transfer->len;
  1119. spi_imx->txfifo = 0;
  1120. spi_imx->remainder = 0;
  1121. reinit_completion(&spi_imx->xfer_done);
  1122. spi_imx_push(spi_imx);
  1123. spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE);
  1124. transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
  1125. timeout = wait_for_completion_timeout(&spi_imx->xfer_done,
  1126. transfer_timeout);
  1127. if (!timeout) {
  1128. dev_err(&spi->dev, "I/O Error in PIO\n");
  1129. spi_imx->devtype_data->reset(spi_imx);
  1130. return -ETIMEDOUT;
  1131. }
  1132. return transfer->len;
  1133. }
  1134. static int spi_imx_pio_transfer_slave(struct spi_device *spi,
  1135. struct spi_transfer *transfer)
  1136. {
  1137. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  1138. int ret = transfer->len;
  1139. if (is_imx53_ecspi(spi_imx) &&
  1140. transfer->len > MX53_MAX_TRANSFER_BYTES) {
  1141. dev_err(&spi->dev, "Transaction too big, max size is %d bytes\n",
  1142. MX53_MAX_TRANSFER_BYTES);
  1143. return -EMSGSIZE;
  1144. }
  1145. spi_imx->tx_buf = transfer->tx_buf;
  1146. spi_imx->rx_buf = transfer->rx_buf;
  1147. spi_imx->count = transfer->len;
  1148. spi_imx->txfifo = 0;
  1149. spi_imx->remainder = 0;
  1150. reinit_completion(&spi_imx->xfer_done);
  1151. spi_imx->slave_aborted = false;
  1152. spi_imx_push(spi_imx);
  1153. spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE | MXC_INT_RDR);
  1154. if (wait_for_completion_interruptible(&spi_imx->xfer_done) ||
  1155. spi_imx->slave_aborted) {
  1156. dev_dbg(&spi->dev, "interrupted\n");
  1157. ret = -EINTR;
  1158. }
  1159. /* ecspi has a HW issue when works in Slave mode,
  1160. * after 64 words writtern to TXFIFO, even TXFIFO becomes empty,
  1161. * ECSPI_TXDATA keeps shift out the last word data,
  1162. * so we have to disable ECSPI when in slave mode after the
  1163. * transfer completes
  1164. */
  1165. if (spi_imx->devtype_data->disable)
  1166. spi_imx->devtype_data->disable(spi_imx);
  1167. return ret;
  1168. }
  1169. static int spi_imx_transfer(struct spi_device *spi,
  1170. struct spi_transfer *transfer)
  1171. {
  1172. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  1173. /* flush rxfifo before transfer */
  1174. while (spi_imx->devtype_data->rx_available(spi_imx))
  1175. readl(spi_imx->base + MXC_CSPIRXDATA);
  1176. if (spi_imx->slave_mode)
  1177. return spi_imx_pio_transfer_slave(spi, transfer);
  1178. if (spi_imx->usedma)
  1179. return spi_imx_dma_transfer(spi_imx, transfer);
  1180. else
  1181. return spi_imx_pio_transfer(spi, transfer);
  1182. }
  1183. static int spi_imx_setup(struct spi_device *spi)
  1184. {
  1185. dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__,
  1186. spi->mode, spi->bits_per_word, spi->max_speed_hz);
  1187. if (spi->mode & SPI_NO_CS)
  1188. return 0;
  1189. if (gpio_is_valid(spi->cs_gpio))
  1190. gpio_direction_output(spi->cs_gpio,
  1191. spi->mode & SPI_CS_HIGH ? 0 : 1);
  1192. spi_imx_chipselect(spi, BITBANG_CS_INACTIVE);
  1193. return 0;
  1194. }
  1195. static void spi_imx_cleanup(struct spi_device *spi)
  1196. {
  1197. }
  1198. static int
  1199. spi_imx_prepare_message(struct spi_master *master, struct spi_message *msg)
  1200. {
  1201. struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
  1202. int ret;
  1203. ret = clk_enable(spi_imx->clk_per);
  1204. if (ret)
  1205. return ret;
  1206. ret = clk_enable(spi_imx->clk_ipg);
  1207. if (ret) {
  1208. clk_disable(spi_imx->clk_per);
  1209. return ret;
  1210. }
  1211. return 0;
  1212. }
  1213. static int
  1214. spi_imx_unprepare_message(struct spi_master *master, struct spi_message *msg)
  1215. {
  1216. struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
  1217. clk_disable(spi_imx->clk_ipg);
  1218. clk_disable(spi_imx->clk_per);
  1219. return 0;
  1220. }
  1221. static int spi_imx_slave_abort(struct spi_master *master)
  1222. {
  1223. struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
  1224. spi_imx->slave_aborted = true;
  1225. complete(&spi_imx->xfer_done);
  1226. return 0;
  1227. }
  1228. static int spi_imx_probe(struct platform_device *pdev)
  1229. {
  1230. struct device_node *np = pdev->dev.of_node;
  1231. const struct of_device_id *of_id =
  1232. of_match_device(spi_imx_dt_ids, &pdev->dev);
  1233. struct spi_imx_master *mxc_platform_info =
  1234. dev_get_platdata(&pdev->dev);
  1235. struct spi_master *master;
  1236. struct spi_imx_data *spi_imx;
  1237. struct resource *res;
  1238. int i, ret, irq, spi_drctl;
  1239. const struct spi_imx_devtype_data *devtype_data = of_id ? of_id->data :
  1240. (struct spi_imx_devtype_data *)pdev->id_entry->driver_data;
  1241. bool slave_mode;
  1242. if (!np && !mxc_platform_info) {
  1243. dev_err(&pdev->dev, "can't get the platform data\n");
  1244. return -EINVAL;
  1245. }
  1246. slave_mode = devtype_data->has_slavemode &&
  1247. of_property_read_bool(np, "spi-slave");
  1248. if (slave_mode)
  1249. master = spi_alloc_slave(&pdev->dev,
  1250. sizeof(struct spi_imx_data));
  1251. else
  1252. master = spi_alloc_master(&pdev->dev,
  1253. sizeof(struct spi_imx_data));
  1254. if (!master)
  1255. return -ENOMEM;
  1256. ret = of_property_read_u32(np, "fsl,spi-rdy-drctl", &spi_drctl);
  1257. if ((ret < 0) || (spi_drctl >= 0x3)) {
  1258. /* '11' is reserved */
  1259. spi_drctl = 0;
  1260. }
  1261. platform_set_drvdata(pdev, master);
  1262. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
  1263. master->bus_num = np ? -1 : pdev->id;
  1264. spi_imx = spi_master_get_devdata(master);
  1265. spi_imx->bitbang.master = master;
  1266. spi_imx->dev = &pdev->dev;
  1267. spi_imx->slave_mode = slave_mode;
  1268. spi_imx->devtype_data = devtype_data;
  1269. /* Get number of chip selects, either platform data or OF */
  1270. if (mxc_platform_info) {
  1271. master->num_chipselect = mxc_platform_info->num_chipselect;
  1272. if (mxc_platform_info->chipselect) {
  1273. master->cs_gpios = devm_kcalloc(&master->dev,
  1274. master->num_chipselect, sizeof(int),
  1275. GFP_KERNEL);
  1276. if (!master->cs_gpios)
  1277. return -ENOMEM;
  1278. for (i = 0; i < master->num_chipselect; i++)
  1279. master->cs_gpios[i] = mxc_platform_info->chipselect[i];
  1280. }
  1281. } else {
  1282. u32 num_cs;
  1283. if (!of_property_read_u32(np, "num-cs", &num_cs))
  1284. master->num_chipselect = num_cs;
  1285. /* If not preset, default value of 1 is used */
  1286. }
  1287. spi_imx->bitbang.chipselect = spi_imx_chipselect;
  1288. spi_imx->bitbang.setup_transfer = spi_imx_setupxfer;
  1289. spi_imx->bitbang.txrx_bufs = spi_imx_transfer;
  1290. spi_imx->bitbang.master->setup = spi_imx_setup;
  1291. spi_imx->bitbang.master->cleanup = spi_imx_cleanup;
  1292. spi_imx->bitbang.master->prepare_message = spi_imx_prepare_message;
  1293. spi_imx->bitbang.master->unprepare_message = spi_imx_unprepare_message;
  1294. spi_imx->bitbang.master->slave_abort = spi_imx_slave_abort;
  1295. spi_imx->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
  1296. | SPI_NO_CS;
  1297. if (is_imx35_cspi(spi_imx) || is_imx51_ecspi(spi_imx) ||
  1298. is_imx53_ecspi(spi_imx))
  1299. spi_imx->bitbang.master->mode_bits |= SPI_LOOP | SPI_READY;
  1300. spi_imx->spi_drctl = spi_drctl;
  1301. init_completion(&spi_imx->xfer_done);
  1302. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1303. spi_imx->base = devm_ioremap_resource(&pdev->dev, res);
  1304. if (IS_ERR(spi_imx->base)) {
  1305. ret = PTR_ERR(spi_imx->base);
  1306. goto out_master_put;
  1307. }
  1308. spi_imx->base_phys = res->start;
  1309. irq = platform_get_irq(pdev, 0);
  1310. if (irq < 0) {
  1311. ret = irq;
  1312. goto out_master_put;
  1313. }
  1314. ret = devm_request_irq(&pdev->dev, irq, spi_imx_isr, 0,
  1315. dev_name(&pdev->dev), spi_imx);
  1316. if (ret) {
  1317. dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
  1318. goto out_master_put;
  1319. }
  1320. spi_imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1321. if (IS_ERR(spi_imx->clk_ipg)) {
  1322. ret = PTR_ERR(spi_imx->clk_ipg);
  1323. goto out_master_put;
  1324. }
  1325. spi_imx->clk_per = devm_clk_get(&pdev->dev, "per");
  1326. if (IS_ERR(spi_imx->clk_per)) {
  1327. ret = PTR_ERR(spi_imx->clk_per);
  1328. goto out_master_put;
  1329. }
  1330. ret = clk_prepare_enable(spi_imx->clk_per);
  1331. if (ret)
  1332. goto out_master_put;
  1333. ret = clk_prepare_enable(spi_imx->clk_ipg);
  1334. if (ret)
  1335. goto out_put_per;
  1336. spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per);
  1337. /*
  1338. * Only validated on i.mx35 and i.mx6 now, can remove the constraint
  1339. * if validated on other chips.
  1340. */
  1341. if (spi_imx->devtype_data->has_dmamode) {
  1342. ret = spi_imx_sdma_init(&pdev->dev, spi_imx, master);
  1343. if (ret == -EPROBE_DEFER)
  1344. goto out_clk_put;
  1345. if (ret < 0)
  1346. dev_err(&pdev->dev, "dma setup error %d, use pio\n",
  1347. ret);
  1348. }
  1349. spi_imx->devtype_data->reset(spi_imx);
  1350. spi_imx->devtype_data->intctrl(spi_imx, 0);
  1351. master->dev.of_node = pdev->dev.of_node;
  1352. ret = spi_bitbang_start(&spi_imx->bitbang);
  1353. if (ret) {
  1354. dev_err(&pdev->dev, "bitbang start failed with %d\n", ret);
  1355. goto out_clk_put;
  1356. }
  1357. /* Request GPIO CS lines, if any */
  1358. if (!spi_imx->slave_mode && master->cs_gpios) {
  1359. for (i = 0; i < master->num_chipselect; i++) {
  1360. if (!gpio_is_valid(master->cs_gpios[i]))
  1361. continue;
  1362. ret = devm_gpio_request(&pdev->dev,
  1363. master->cs_gpios[i],
  1364. DRIVER_NAME);
  1365. if (ret) {
  1366. dev_err(&pdev->dev, "Can't get CS GPIO %i\n",
  1367. master->cs_gpios[i]);
  1368. goto out_spi_bitbang;
  1369. }
  1370. }
  1371. }
  1372. dev_info(&pdev->dev, "probed\n");
  1373. clk_disable(spi_imx->clk_ipg);
  1374. clk_disable(spi_imx->clk_per);
  1375. return ret;
  1376. out_spi_bitbang:
  1377. spi_bitbang_stop(&spi_imx->bitbang);
  1378. out_clk_put:
  1379. clk_disable_unprepare(spi_imx->clk_ipg);
  1380. out_put_per:
  1381. clk_disable_unprepare(spi_imx->clk_per);
  1382. out_master_put:
  1383. spi_master_put(master);
  1384. return ret;
  1385. }
  1386. static int spi_imx_remove(struct platform_device *pdev)
  1387. {
  1388. struct spi_master *master = platform_get_drvdata(pdev);
  1389. struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
  1390. int ret;
  1391. spi_bitbang_stop(&spi_imx->bitbang);
  1392. ret = clk_enable(spi_imx->clk_per);
  1393. if (ret)
  1394. return ret;
  1395. ret = clk_enable(spi_imx->clk_ipg);
  1396. if (ret) {
  1397. clk_disable(spi_imx->clk_per);
  1398. return ret;
  1399. }
  1400. writel(0, spi_imx->base + MXC_CSPICTRL);
  1401. clk_disable_unprepare(spi_imx->clk_ipg);
  1402. clk_disable_unprepare(spi_imx->clk_per);
  1403. spi_imx_sdma_exit(spi_imx);
  1404. spi_master_put(master);
  1405. return 0;
  1406. }
  1407. static struct platform_driver spi_imx_driver = {
  1408. .driver = {
  1409. .name = DRIVER_NAME,
  1410. .of_match_table = spi_imx_dt_ids,
  1411. },
  1412. .id_table = spi_imx_devtype,
  1413. .probe = spi_imx_probe,
  1414. .remove = spi_imx_remove,
  1415. };
  1416. module_platform_driver(spi_imx_driver);
  1417. MODULE_DESCRIPTION("SPI Controller driver");
  1418. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  1419. MODULE_LICENSE("GPL");
  1420. MODULE_ALIAS("platform:" DRIVER_NAME);