spi-imx.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. /*
  2. * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
  3. * Copyright (C) 2008 Juergen Beisert
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the
  16. * Free Software Foundation
  17. * 51 Franklin Street, Fifth Floor
  18. * Boston, MA 02110-1301, USA.
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/completion.h>
  22. #include <linux/delay.h>
  23. #include <linux/dmaengine.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/err.h>
  26. #include <linux/gpio.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/io.h>
  29. #include <linux/irq.h>
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/slab.h>
  34. #include <linux/spi/spi.h>
  35. #include <linux/spi/spi_bitbang.h>
  36. #include <linux/types.h>
  37. #include <linux/of.h>
  38. #include <linux/of_device.h>
  39. #include <linux/of_gpio.h>
  40. #include <linux/platform_data/dma-imx.h>
  41. #include <linux/platform_data/spi-imx.h>
  42. #define DRIVER_NAME "spi_imx"
  43. #define MXC_CSPIRXDATA 0x00
  44. #define MXC_CSPITXDATA 0x04
  45. #define MXC_CSPICTRL 0x08
  46. #define MXC_CSPIINT 0x0c
  47. #define MXC_RESET 0x1c
  48. /* generic defines to abstract from the different register layouts */
  49. #define MXC_INT_RR (1 << 0) /* Receive data ready interrupt */
  50. #define MXC_INT_TE (1 << 1) /* Transmit FIFO empty interrupt */
  51. /* The maximum bytes that a sdma BD can transfer.*/
  52. #define MAX_SDMA_BD_BYTES (1 << 15)
  53. #define IMX_DMA_TIMEOUT (msecs_to_jiffies(3000))
  54. struct spi_imx_config {
  55. unsigned int speed_hz;
  56. unsigned int bpw;
  57. unsigned int mode;
  58. u8 cs;
  59. };
  60. enum spi_imx_devtype {
  61. IMX1_CSPI,
  62. IMX21_CSPI,
  63. IMX27_CSPI,
  64. IMX31_CSPI,
  65. IMX35_CSPI, /* CSPI on all i.mx except above */
  66. IMX51_ECSPI, /* ECSPI on i.mx51 and later */
  67. };
  68. struct spi_imx_data;
  69. struct spi_imx_devtype_data {
  70. void (*intctrl)(struct spi_imx_data *, int);
  71. int (*config)(struct spi_imx_data *, struct spi_imx_config *);
  72. void (*trigger)(struct spi_imx_data *);
  73. int (*rx_available)(struct spi_imx_data *);
  74. void (*reset)(struct spi_imx_data *);
  75. enum spi_imx_devtype devtype;
  76. };
  77. struct spi_imx_data {
  78. struct spi_bitbang bitbang;
  79. struct completion xfer_done;
  80. void __iomem *base;
  81. struct clk *clk_per;
  82. struct clk *clk_ipg;
  83. unsigned long spi_clk;
  84. unsigned int count;
  85. void (*tx)(struct spi_imx_data *);
  86. void (*rx)(struct spi_imx_data *);
  87. void *rx_buf;
  88. const void *tx_buf;
  89. unsigned int txfifo; /* number of words pushed in tx FIFO */
  90. /* DMA */
  91. unsigned int dma_is_inited;
  92. unsigned int dma_finished;
  93. bool usedma;
  94. u32 rx_wml;
  95. u32 tx_wml;
  96. u32 rxt_wml;
  97. struct completion dma_rx_completion;
  98. struct completion dma_tx_completion;
  99. const struct spi_imx_devtype_data *devtype_data;
  100. int chipselect[0];
  101. };
  102. static inline int is_imx27_cspi(struct spi_imx_data *d)
  103. {
  104. return d->devtype_data->devtype == IMX27_CSPI;
  105. }
  106. static inline int is_imx35_cspi(struct spi_imx_data *d)
  107. {
  108. return d->devtype_data->devtype == IMX35_CSPI;
  109. }
  110. static inline unsigned spi_imx_get_fifosize(struct spi_imx_data *d)
  111. {
  112. return (d->devtype_data->devtype == IMX51_ECSPI) ? 64 : 8;
  113. }
  114. #define MXC_SPI_BUF_RX(type) \
  115. static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx) \
  116. { \
  117. unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); \
  118. \
  119. if (spi_imx->rx_buf) { \
  120. *(type *)spi_imx->rx_buf = val; \
  121. spi_imx->rx_buf += sizeof(type); \
  122. } \
  123. }
  124. #define MXC_SPI_BUF_TX(type) \
  125. static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx) \
  126. { \
  127. type val = 0; \
  128. \
  129. if (spi_imx->tx_buf) { \
  130. val = *(type *)spi_imx->tx_buf; \
  131. spi_imx->tx_buf += sizeof(type); \
  132. } \
  133. \
  134. spi_imx->count -= sizeof(type); \
  135. \
  136. writel(val, spi_imx->base + MXC_CSPITXDATA); \
  137. }
  138. MXC_SPI_BUF_RX(u8)
  139. MXC_SPI_BUF_TX(u8)
  140. MXC_SPI_BUF_RX(u16)
  141. MXC_SPI_BUF_TX(u16)
  142. MXC_SPI_BUF_RX(u32)
  143. MXC_SPI_BUF_TX(u32)
  144. /* First entry is reserved, second entry is valid only if SDHC_SPIEN is set
  145. * (which is currently not the case in this driver)
  146. */
  147. static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
  148. 256, 384, 512, 768, 1024};
  149. /* MX21, MX27 */
  150. static unsigned int spi_imx_clkdiv_1(unsigned int fin,
  151. unsigned int fspi, unsigned int max)
  152. {
  153. int i;
  154. for (i = 2; i < max; i++)
  155. if (fspi * mxc_clkdivs[i] >= fin)
  156. return i;
  157. return max;
  158. }
  159. /* MX1, MX31, MX35, MX51 CSPI */
  160. static unsigned int spi_imx_clkdiv_2(unsigned int fin,
  161. unsigned int fspi)
  162. {
  163. int i, div = 4;
  164. for (i = 0; i < 7; i++) {
  165. if (fspi * div >= fin)
  166. return i;
  167. div <<= 1;
  168. }
  169. return 7;
  170. }
  171. static bool spi_imx_can_dma(struct spi_master *master, struct spi_device *spi,
  172. struct spi_transfer *transfer)
  173. {
  174. struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
  175. if (spi_imx->dma_is_inited && (transfer->len > spi_imx->rx_wml)
  176. && (transfer->len > spi_imx->tx_wml))
  177. return true;
  178. return false;
  179. }
  180. #define MX51_ECSPI_CTRL 0x08
  181. #define MX51_ECSPI_CTRL_ENABLE (1 << 0)
  182. #define MX51_ECSPI_CTRL_XCH (1 << 2)
  183. #define MX51_ECSPI_CTRL_SMC (1 << 3)
  184. #define MX51_ECSPI_CTRL_MODE_MASK (0xf << 4)
  185. #define MX51_ECSPI_CTRL_POSTDIV_OFFSET 8
  186. #define MX51_ECSPI_CTRL_PREDIV_OFFSET 12
  187. #define MX51_ECSPI_CTRL_CS(cs) ((cs) << 18)
  188. #define MX51_ECSPI_CTRL_BL_OFFSET 20
  189. #define MX51_ECSPI_CONFIG 0x0c
  190. #define MX51_ECSPI_CONFIG_SCLKPHA(cs) (1 << ((cs) + 0))
  191. #define MX51_ECSPI_CONFIG_SCLKPOL(cs) (1 << ((cs) + 4))
  192. #define MX51_ECSPI_CONFIG_SBBCTRL(cs) (1 << ((cs) + 8))
  193. #define MX51_ECSPI_CONFIG_SSBPOL(cs) (1 << ((cs) + 12))
  194. #define MX51_ECSPI_CONFIG_SCLKCTL(cs) (1 << ((cs) + 20))
  195. #define MX51_ECSPI_INT 0x10
  196. #define MX51_ECSPI_INT_TEEN (1 << 0)
  197. #define MX51_ECSPI_INT_RREN (1 << 3)
  198. #define MX51_ECSPI_DMA 0x14
  199. #define MX51_ECSPI_DMA_TX_WML_OFFSET 0
  200. #define MX51_ECSPI_DMA_TX_WML_MASK 0x3F
  201. #define MX51_ECSPI_DMA_RX_WML_OFFSET 16
  202. #define MX51_ECSPI_DMA_RX_WML_MASK (0x3F << 16)
  203. #define MX51_ECSPI_DMA_RXT_WML_OFFSET 24
  204. #define MX51_ECSPI_DMA_RXT_WML_MASK (0x3F << 24)
  205. #define MX51_ECSPI_DMA_TEDEN_OFFSET 7
  206. #define MX51_ECSPI_DMA_RXDEN_OFFSET 23
  207. #define MX51_ECSPI_DMA_RXTDEN_OFFSET 31
  208. #define MX51_ECSPI_STAT 0x18
  209. #define MX51_ECSPI_STAT_RR (1 << 3)
  210. /* MX51 eCSPI */
  211. static unsigned int mx51_ecspi_clkdiv(unsigned int fin, unsigned int fspi,
  212. unsigned int *fres)
  213. {
  214. /*
  215. * there are two 4-bit dividers, the pre-divider divides by
  216. * $pre, the post-divider by 2^$post
  217. */
  218. unsigned int pre, post;
  219. if (unlikely(fspi > fin))
  220. return 0;
  221. post = fls(fin) - fls(fspi);
  222. if (fin > fspi << post)
  223. post++;
  224. /* now we have: (fin <= fspi << post) with post being minimal */
  225. post = max(4U, post) - 4;
  226. if (unlikely(post > 0xf)) {
  227. pr_err("%s: cannot set clock freq: %u (base freq: %u)\n",
  228. __func__, fspi, fin);
  229. return 0xff;
  230. }
  231. pre = DIV_ROUND_UP(fin, fspi << post) - 1;
  232. pr_debug("%s: fin: %u, fspi: %u, post: %u, pre: %u\n",
  233. __func__, fin, fspi, post, pre);
  234. /* Resulting frequency for the SCLK line. */
  235. *fres = (fin / (pre + 1)) >> post;
  236. return (pre << MX51_ECSPI_CTRL_PREDIV_OFFSET) |
  237. (post << MX51_ECSPI_CTRL_POSTDIV_OFFSET);
  238. }
  239. static void __maybe_unused mx51_ecspi_intctrl(struct spi_imx_data *spi_imx, int enable)
  240. {
  241. unsigned val = 0;
  242. if (enable & MXC_INT_TE)
  243. val |= MX51_ECSPI_INT_TEEN;
  244. if (enable & MXC_INT_RR)
  245. val |= MX51_ECSPI_INT_RREN;
  246. writel(val, spi_imx->base + MX51_ECSPI_INT);
  247. }
  248. static void __maybe_unused mx51_ecspi_trigger(struct spi_imx_data *spi_imx)
  249. {
  250. u32 reg = readl(spi_imx->base + MX51_ECSPI_CTRL);
  251. if (!spi_imx->usedma)
  252. reg |= MX51_ECSPI_CTRL_XCH;
  253. else if (!spi_imx->dma_finished)
  254. reg |= MX51_ECSPI_CTRL_SMC;
  255. else
  256. reg &= ~MX51_ECSPI_CTRL_SMC;
  257. writel(reg, spi_imx->base + MX51_ECSPI_CTRL);
  258. }
  259. static int __maybe_unused mx51_ecspi_config(struct spi_imx_data *spi_imx,
  260. struct spi_imx_config *config)
  261. {
  262. u32 ctrl = MX51_ECSPI_CTRL_ENABLE, cfg = 0, dma = 0;
  263. u32 tx_wml_cfg, rx_wml_cfg, rxt_wml_cfg;
  264. u32 clk = config->speed_hz, delay;
  265. /*
  266. * The hardware seems to have a race condition when changing modes. The
  267. * current assumption is that the selection of the channel arrives
  268. * earlier in the hardware than the mode bits when they are written at
  269. * the same time.
  270. * So set master mode for all channels as we do not support slave mode.
  271. */
  272. ctrl |= MX51_ECSPI_CTRL_MODE_MASK;
  273. /* set clock speed */
  274. ctrl |= mx51_ecspi_clkdiv(spi_imx->spi_clk, config->speed_hz, &clk);
  275. /* set chip select to use */
  276. ctrl |= MX51_ECSPI_CTRL_CS(config->cs);
  277. ctrl |= (config->bpw - 1) << MX51_ECSPI_CTRL_BL_OFFSET;
  278. cfg |= MX51_ECSPI_CONFIG_SBBCTRL(config->cs);
  279. if (config->mode & SPI_CPHA)
  280. cfg |= MX51_ECSPI_CONFIG_SCLKPHA(config->cs);
  281. if (config->mode & SPI_CPOL) {
  282. cfg |= MX51_ECSPI_CONFIG_SCLKPOL(config->cs);
  283. cfg |= MX51_ECSPI_CONFIG_SCLKCTL(config->cs);
  284. }
  285. if (config->mode & SPI_CS_HIGH)
  286. cfg |= MX51_ECSPI_CONFIG_SSBPOL(config->cs);
  287. writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
  288. writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
  289. /*
  290. * Wait until the changes in the configuration register CONFIGREG
  291. * propagate into the hardware. It takes exactly one tick of the
  292. * SCLK clock, but we will wait two SCLK clock just to be sure. The
  293. * effect of the delay it takes for the hardware to apply changes
  294. * is noticable if the SCLK clock run very slow. In such a case, if
  295. * the polarity of SCLK should be inverted, the GPIO ChipSelect might
  296. * be asserted before the SCLK polarity changes, which would disrupt
  297. * the SPI communication as the device on the other end would consider
  298. * the change of SCLK polarity as a clock tick already.
  299. */
  300. delay = (2 * 1000000) / clk;
  301. if (likely(delay < 10)) /* SCLK is faster than 100 kHz */
  302. udelay(delay);
  303. else /* SCLK is _very_ slow */
  304. usleep_range(delay, delay + 10);
  305. /*
  306. * Configure the DMA register: setup the watermark
  307. * and enable DMA request.
  308. */
  309. if (spi_imx->dma_is_inited) {
  310. dma = readl(spi_imx->base + MX51_ECSPI_DMA);
  311. spi_imx->rxt_wml = spi_imx_get_fifosize(spi_imx) / 2;
  312. rx_wml_cfg = spi_imx->rx_wml << MX51_ECSPI_DMA_RX_WML_OFFSET;
  313. tx_wml_cfg = spi_imx->tx_wml << MX51_ECSPI_DMA_TX_WML_OFFSET;
  314. rxt_wml_cfg = spi_imx->rxt_wml << MX51_ECSPI_DMA_RXT_WML_OFFSET;
  315. dma = (dma & ~MX51_ECSPI_DMA_TX_WML_MASK
  316. & ~MX51_ECSPI_DMA_RX_WML_MASK
  317. & ~MX51_ECSPI_DMA_RXT_WML_MASK)
  318. | rx_wml_cfg | tx_wml_cfg | rxt_wml_cfg
  319. |(1 << MX51_ECSPI_DMA_TEDEN_OFFSET)
  320. |(1 << MX51_ECSPI_DMA_RXDEN_OFFSET)
  321. |(1 << MX51_ECSPI_DMA_RXTDEN_OFFSET);
  322. writel(dma, spi_imx->base + MX51_ECSPI_DMA);
  323. }
  324. return 0;
  325. }
  326. static int __maybe_unused mx51_ecspi_rx_available(struct spi_imx_data *spi_imx)
  327. {
  328. return readl(spi_imx->base + MX51_ECSPI_STAT) & MX51_ECSPI_STAT_RR;
  329. }
  330. static void __maybe_unused mx51_ecspi_reset(struct spi_imx_data *spi_imx)
  331. {
  332. /* drain receive buffer */
  333. while (mx51_ecspi_rx_available(spi_imx))
  334. readl(spi_imx->base + MXC_CSPIRXDATA);
  335. }
  336. #define MX31_INTREG_TEEN (1 << 0)
  337. #define MX31_INTREG_RREN (1 << 3)
  338. #define MX31_CSPICTRL_ENABLE (1 << 0)
  339. #define MX31_CSPICTRL_MASTER (1 << 1)
  340. #define MX31_CSPICTRL_XCH (1 << 2)
  341. #define MX31_CSPICTRL_POL (1 << 4)
  342. #define MX31_CSPICTRL_PHA (1 << 5)
  343. #define MX31_CSPICTRL_SSCTL (1 << 6)
  344. #define MX31_CSPICTRL_SSPOL (1 << 7)
  345. #define MX31_CSPICTRL_BC_SHIFT 8
  346. #define MX35_CSPICTRL_BL_SHIFT 20
  347. #define MX31_CSPICTRL_CS_SHIFT 24
  348. #define MX35_CSPICTRL_CS_SHIFT 12
  349. #define MX31_CSPICTRL_DR_SHIFT 16
  350. #define MX31_CSPISTATUS 0x14
  351. #define MX31_STATUS_RR (1 << 3)
  352. /* These functions also work for the i.MX35, but be aware that
  353. * the i.MX35 has a slightly different register layout for bits
  354. * we do not use here.
  355. */
  356. static void __maybe_unused mx31_intctrl(struct spi_imx_data *spi_imx, int enable)
  357. {
  358. unsigned int val = 0;
  359. if (enable & MXC_INT_TE)
  360. val |= MX31_INTREG_TEEN;
  361. if (enable & MXC_INT_RR)
  362. val |= MX31_INTREG_RREN;
  363. writel(val, spi_imx->base + MXC_CSPIINT);
  364. }
  365. static void __maybe_unused mx31_trigger(struct spi_imx_data *spi_imx)
  366. {
  367. unsigned int reg;
  368. reg = readl(spi_imx->base + MXC_CSPICTRL);
  369. reg |= MX31_CSPICTRL_XCH;
  370. writel(reg, spi_imx->base + MXC_CSPICTRL);
  371. }
  372. static int __maybe_unused mx31_config(struct spi_imx_data *spi_imx,
  373. struct spi_imx_config *config)
  374. {
  375. unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER;
  376. int cs = spi_imx->chipselect[config->cs];
  377. reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) <<
  378. MX31_CSPICTRL_DR_SHIFT;
  379. if (is_imx35_cspi(spi_imx)) {
  380. reg |= (config->bpw - 1) << MX35_CSPICTRL_BL_SHIFT;
  381. reg |= MX31_CSPICTRL_SSCTL;
  382. } else {
  383. reg |= (config->bpw - 1) << MX31_CSPICTRL_BC_SHIFT;
  384. }
  385. if (config->mode & SPI_CPHA)
  386. reg |= MX31_CSPICTRL_PHA;
  387. if (config->mode & SPI_CPOL)
  388. reg |= MX31_CSPICTRL_POL;
  389. if (config->mode & SPI_CS_HIGH)
  390. reg |= MX31_CSPICTRL_SSPOL;
  391. if (cs < 0)
  392. reg |= (cs + 32) <<
  393. (is_imx35_cspi(spi_imx) ? MX35_CSPICTRL_CS_SHIFT :
  394. MX31_CSPICTRL_CS_SHIFT);
  395. writel(reg, spi_imx->base + MXC_CSPICTRL);
  396. return 0;
  397. }
  398. static int __maybe_unused mx31_rx_available(struct spi_imx_data *spi_imx)
  399. {
  400. return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR;
  401. }
  402. static void __maybe_unused mx31_reset(struct spi_imx_data *spi_imx)
  403. {
  404. /* drain receive buffer */
  405. while (readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR)
  406. readl(spi_imx->base + MXC_CSPIRXDATA);
  407. }
  408. #define MX21_INTREG_RR (1 << 4)
  409. #define MX21_INTREG_TEEN (1 << 9)
  410. #define MX21_INTREG_RREN (1 << 13)
  411. #define MX21_CSPICTRL_POL (1 << 5)
  412. #define MX21_CSPICTRL_PHA (1 << 6)
  413. #define MX21_CSPICTRL_SSPOL (1 << 8)
  414. #define MX21_CSPICTRL_XCH (1 << 9)
  415. #define MX21_CSPICTRL_ENABLE (1 << 10)
  416. #define MX21_CSPICTRL_MASTER (1 << 11)
  417. #define MX21_CSPICTRL_DR_SHIFT 14
  418. #define MX21_CSPICTRL_CS_SHIFT 19
  419. static void __maybe_unused mx21_intctrl(struct spi_imx_data *spi_imx, int enable)
  420. {
  421. unsigned int val = 0;
  422. if (enable & MXC_INT_TE)
  423. val |= MX21_INTREG_TEEN;
  424. if (enable & MXC_INT_RR)
  425. val |= MX21_INTREG_RREN;
  426. writel(val, spi_imx->base + MXC_CSPIINT);
  427. }
  428. static void __maybe_unused mx21_trigger(struct spi_imx_data *spi_imx)
  429. {
  430. unsigned int reg;
  431. reg = readl(spi_imx->base + MXC_CSPICTRL);
  432. reg |= MX21_CSPICTRL_XCH;
  433. writel(reg, spi_imx->base + MXC_CSPICTRL);
  434. }
  435. static int __maybe_unused mx21_config(struct spi_imx_data *spi_imx,
  436. struct spi_imx_config *config)
  437. {
  438. unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_MASTER;
  439. int cs = spi_imx->chipselect[config->cs];
  440. unsigned int max = is_imx27_cspi(spi_imx) ? 16 : 18;
  441. reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, config->speed_hz, max) <<
  442. MX21_CSPICTRL_DR_SHIFT;
  443. reg |= config->bpw - 1;
  444. if (config->mode & SPI_CPHA)
  445. reg |= MX21_CSPICTRL_PHA;
  446. if (config->mode & SPI_CPOL)
  447. reg |= MX21_CSPICTRL_POL;
  448. if (config->mode & SPI_CS_HIGH)
  449. reg |= MX21_CSPICTRL_SSPOL;
  450. if (cs < 0)
  451. reg |= (cs + 32) << MX21_CSPICTRL_CS_SHIFT;
  452. writel(reg, spi_imx->base + MXC_CSPICTRL);
  453. return 0;
  454. }
  455. static int __maybe_unused mx21_rx_available(struct spi_imx_data *spi_imx)
  456. {
  457. return readl(spi_imx->base + MXC_CSPIINT) & MX21_INTREG_RR;
  458. }
  459. static void __maybe_unused mx21_reset(struct spi_imx_data *spi_imx)
  460. {
  461. writel(1, spi_imx->base + MXC_RESET);
  462. }
  463. #define MX1_INTREG_RR (1 << 3)
  464. #define MX1_INTREG_TEEN (1 << 8)
  465. #define MX1_INTREG_RREN (1 << 11)
  466. #define MX1_CSPICTRL_POL (1 << 4)
  467. #define MX1_CSPICTRL_PHA (1 << 5)
  468. #define MX1_CSPICTRL_XCH (1 << 8)
  469. #define MX1_CSPICTRL_ENABLE (1 << 9)
  470. #define MX1_CSPICTRL_MASTER (1 << 10)
  471. #define MX1_CSPICTRL_DR_SHIFT 13
  472. static void __maybe_unused mx1_intctrl(struct spi_imx_data *spi_imx, int enable)
  473. {
  474. unsigned int val = 0;
  475. if (enable & MXC_INT_TE)
  476. val |= MX1_INTREG_TEEN;
  477. if (enable & MXC_INT_RR)
  478. val |= MX1_INTREG_RREN;
  479. writel(val, spi_imx->base + MXC_CSPIINT);
  480. }
  481. static void __maybe_unused mx1_trigger(struct spi_imx_data *spi_imx)
  482. {
  483. unsigned int reg;
  484. reg = readl(spi_imx->base + MXC_CSPICTRL);
  485. reg |= MX1_CSPICTRL_XCH;
  486. writel(reg, spi_imx->base + MXC_CSPICTRL);
  487. }
  488. static int __maybe_unused mx1_config(struct spi_imx_data *spi_imx,
  489. struct spi_imx_config *config)
  490. {
  491. unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER;
  492. reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) <<
  493. MX1_CSPICTRL_DR_SHIFT;
  494. reg |= config->bpw - 1;
  495. if (config->mode & SPI_CPHA)
  496. reg |= MX1_CSPICTRL_PHA;
  497. if (config->mode & SPI_CPOL)
  498. reg |= MX1_CSPICTRL_POL;
  499. writel(reg, spi_imx->base + MXC_CSPICTRL);
  500. return 0;
  501. }
  502. static int __maybe_unused mx1_rx_available(struct spi_imx_data *spi_imx)
  503. {
  504. return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR;
  505. }
  506. static void __maybe_unused mx1_reset(struct spi_imx_data *spi_imx)
  507. {
  508. writel(1, spi_imx->base + MXC_RESET);
  509. }
  510. static struct spi_imx_devtype_data imx1_cspi_devtype_data = {
  511. .intctrl = mx1_intctrl,
  512. .config = mx1_config,
  513. .trigger = mx1_trigger,
  514. .rx_available = mx1_rx_available,
  515. .reset = mx1_reset,
  516. .devtype = IMX1_CSPI,
  517. };
  518. static struct spi_imx_devtype_data imx21_cspi_devtype_data = {
  519. .intctrl = mx21_intctrl,
  520. .config = mx21_config,
  521. .trigger = mx21_trigger,
  522. .rx_available = mx21_rx_available,
  523. .reset = mx21_reset,
  524. .devtype = IMX21_CSPI,
  525. };
  526. static struct spi_imx_devtype_data imx27_cspi_devtype_data = {
  527. /* i.mx27 cspi shares the functions with i.mx21 one */
  528. .intctrl = mx21_intctrl,
  529. .config = mx21_config,
  530. .trigger = mx21_trigger,
  531. .rx_available = mx21_rx_available,
  532. .reset = mx21_reset,
  533. .devtype = IMX27_CSPI,
  534. };
  535. static struct spi_imx_devtype_data imx31_cspi_devtype_data = {
  536. .intctrl = mx31_intctrl,
  537. .config = mx31_config,
  538. .trigger = mx31_trigger,
  539. .rx_available = mx31_rx_available,
  540. .reset = mx31_reset,
  541. .devtype = IMX31_CSPI,
  542. };
  543. static struct spi_imx_devtype_data imx35_cspi_devtype_data = {
  544. /* i.mx35 and later cspi shares the functions with i.mx31 one */
  545. .intctrl = mx31_intctrl,
  546. .config = mx31_config,
  547. .trigger = mx31_trigger,
  548. .rx_available = mx31_rx_available,
  549. .reset = mx31_reset,
  550. .devtype = IMX35_CSPI,
  551. };
  552. static struct spi_imx_devtype_data imx51_ecspi_devtype_data = {
  553. .intctrl = mx51_ecspi_intctrl,
  554. .config = mx51_ecspi_config,
  555. .trigger = mx51_ecspi_trigger,
  556. .rx_available = mx51_ecspi_rx_available,
  557. .reset = mx51_ecspi_reset,
  558. .devtype = IMX51_ECSPI,
  559. };
  560. static const struct platform_device_id spi_imx_devtype[] = {
  561. {
  562. .name = "imx1-cspi",
  563. .driver_data = (kernel_ulong_t) &imx1_cspi_devtype_data,
  564. }, {
  565. .name = "imx21-cspi",
  566. .driver_data = (kernel_ulong_t) &imx21_cspi_devtype_data,
  567. }, {
  568. .name = "imx27-cspi",
  569. .driver_data = (kernel_ulong_t) &imx27_cspi_devtype_data,
  570. }, {
  571. .name = "imx31-cspi",
  572. .driver_data = (kernel_ulong_t) &imx31_cspi_devtype_data,
  573. }, {
  574. .name = "imx35-cspi",
  575. .driver_data = (kernel_ulong_t) &imx35_cspi_devtype_data,
  576. }, {
  577. .name = "imx51-ecspi",
  578. .driver_data = (kernel_ulong_t) &imx51_ecspi_devtype_data,
  579. }, {
  580. /* sentinel */
  581. }
  582. };
  583. static const struct of_device_id spi_imx_dt_ids[] = {
  584. { .compatible = "fsl,imx1-cspi", .data = &imx1_cspi_devtype_data, },
  585. { .compatible = "fsl,imx21-cspi", .data = &imx21_cspi_devtype_data, },
  586. { .compatible = "fsl,imx27-cspi", .data = &imx27_cspi_devtype_data, },
  587. { .compatible = "fsl,imx31-cspi", .data = &imx31_cspi_devtype_data, },
  588. { .compatible = "fsl,imx35-cspi", .data = &imx35_cspi_devtype_data, },
  589. { .compatible = "fsl,imx51-ecspi", .data = &imx51_ecspi_devtype_data, },
  590. { /* sentinel */ }
  591. };
  592. MODULE_DEVICE_TABLE(of, spi_imx_dt_ids);
  593. static void spi_imx_chipselect(struct spi_device *spi, int is_active)
  594. {
  595. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  596. int gpio = spi_imx->chipselect[spi->chip_select];
  597. int active = is_active != BITBANG_CS_INACTIVE;
  598. int dev_is_lowactive = !(spi->mode & SPI_CS_HIGH);
  599. if (!gpio_is_valid(gpio))
  600. return;
  601. gpio_set_value(gpio, dev_is_lowactive ^ active);
  602. }
  603. static void spi_imx_push(struct spi_imx_data *spi_imx)
  604. {
  605. while (spi_imx->txfifo < spi_imx_get_fifosize(spi_imx)) {
  606. if (!spi_imx->count)
  607. break;
  608. spi_imx->tx(spi_imx);
  609. spi_imx->txfifo++;
  610. }
  611. spi_imx->devtype_data->trigger(spi_imx);
  612. }
  613. static irqreturn_t spi_imx_isr(int irq, void *dev_id)
  614. {
  615. struct spi_imx_data *spi_imx = dev_id;
  616. while (spi_imx->devtype_data->rx_available(spi_imx)) {
  617. spi_imx->rx(spi_imx);
  618. spi_imx->txfifo--;
  619. }
  620. if (spi_imx->count) {
  621. spi_imx_push(spi_imx);
  622. return IRQ_HANDLED;
  623. }
  624. if (spi_imx->txfifo) {
  625. /* No data left to push, but still waiting for rx data,
  626. * enable receive data available interrupt.
  627. */
  628. spi_imx->devtype_data->intctrl(
  629. spi_imx, MXC_INT_RR);
  630. return IRQ_HANDLED;
  631. }
  632. spi_imx->devtype_data->intctrl(spi_imx, 0);
  633. complete(&spi_imx->xfer_done);
  634. return IRQ_HANDLED;
  635. }
  636. static int spi_imx_setupxfer(struct spi_device *spi,
  637. struct spi_transfer *t)
  638. {
  639. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  640. struct spi_imx_config config;
  641. config.bpw = t ? t->bits_per_word : spi->bits_per_word;
  642. config.speed_hz = t ? t->speed_hz : spi->max_speed_hz;
  643. config.mode = spi->mode;
  644. config.cs = spi->chip_select;
  645. if (!config.speed_hz)
  646. config.speed_hz = spi->max_speed_hz;
  647. if (!config.bpw)
  648. config.bpw = spi->bits_per_word;
  649. /* Initialize the functions for transfer */
  650. if (config.bpw <= 8) {
  651. spi_imx->rx = spi_imx_buf_rx_u8;
  652. spi_imx->tx = spi_imx_buf_tx_u8;
  653. } else if (config.bpw <= 16) {
  654. spi_imx->rx = spi_imx_buf_rx_u16;
  655. spi_imx->tx = spi_imx_buf_tx_u16;
  656. } else {
  657. spi_imx->rx = spi_imx_buf_rx_u32;
  658. spi_imx->tx = spi_imx_buf_tx_u32;
  659. }
  660. spi_imx->devtype_data->config(spi_imx, &config);
  661. return 0;
  662. }
  663. static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx)
  664. {
  665. struct spi_master *master = spi_imx->bitbang.master;
  666. if (master->dma_rx) {
  667. dma_release_channel(master->dma_rx);
  668. master->dma_rx = NULL;
  669. }
  670. if (master->dma_tx) {
  671. dma_release_channel(master->dma_tx);
  672. master->dma_tx = NULL;
  673. }
  674. spi_imx->dma_is_inited = 0;
  675. }
  676. static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx,
  677. struct spi_master *master,
  678. const struct resource *res)
  679. {
  680. struct dma_slave_config slave_config = {};
  681. int ret;
  682. /* use pio mode for i.mx6dl chip TKT238285 */
  683. if (of_machine_is_compatible("fsl,imx6dl"))
  684. return 0;
  685. /* Prepare for TX DMA: */
  686. master->dma_tx = dma_request_slave_channel(dev, "tx");
  687. if (!master->dma_tx) {
  688. dev_err(dev, "cannot get the TX DMA channel!\n");
  689. ret = -EINVAL;
  690. goto err;
  691. }
  692. slave_config.direction = DMA_MEM_TO_DEV;
  693. slave_config.dst_addr = res->start + MXC_CSPITXDATA;
  694. slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  695. slave_config.dst_maxburst = spi_imx_get_fifosize(spi_imx) / 2;
  696. ret = dmaengine_slave_config(master->dma_tx, &slave_config);
  697. if (ret) {
  698. dev_err(dev, "error in TX dma configuration.\n");
  699. goto err;
  700. }
  701. /* Prepare for RX : */
  702. master->dma_rx = dma_request_slave_channel(dev, "rx");
  703. if (!master->dma_rx) {
  704. dev_dbg(dev, "cannot get the DMA channel.\n");
  705. ret = -EINVAL;
  706. goto err;
  707. }
  708. slave_config.direction = DMA_DEV_TO_MEM;
  709. slave_config.src_addr = res->start + MXC_CSPIRXDATA;
  710. slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  711. slave_config.src_maxburst = spi_imx_get_fifosize(spi_imx) / 2;
  712. ret = dmaengine_slave_config(master->dma_rx, &slave_config);
  713. if (ret) {
  714. dev_err(dev, "error in RX dma configuration.\n");
  715. goto err;
  716. }
  717. init_completion(&spi_imx->dma_rx_completion);
  718. init_completion(&spi_imx->dma_tx_completion);
  719. master->can_dma = spi_imx_can_dma;
  720. master->max_dma_len = MAX_SDMA_BD_BYTES;
  721. spi_imx->bitbang.master->flags = SPI_MASTER_MUST_RX |
  722. SPI_MASTER_MUST_TX;
  723. spi_imx->tx_wml = spi_imx_get_fifosize(spi_imx) / 2;
  724. spi_imx->rx_wml = spi_imx_get_fifosize(spi_imx) / 2;
  725. spi_imx->dma_is_inited = 1;
  726. return 0;
  727. err:
  728. spi_imx_sdma_exit(spi_imx);
  729. return ret;
  730. }
  731. static void spi_imx_dma_rx_callback(void *cookie)
  732. {
  733. struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
  734. complete(&spi_imx->dma_rx_completion);
  735. }
  736. static void spi_imx_dma_tx_callback(void *cookie)
  737. {
  738. struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
  739. complete(&spi_imx->dma_tx_completion);
  740. }
  741. static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx,
  742. struct spi_transfer *transfer)
  743. {
  744. struct dma_async_tx_descriptor *desc_tx = NULL, *desc_rx = NULL;
  745. int ret;
  746. unsigned long timeout;
  747. u32 dma;
  748. int left;
  749. struct spi_master *master = spi_imx->bitbang.master;
  750. struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;
  751. if (tx) {
  752. desc_tx = dmaengine_prep_slave_sg(master->dma_tx,
  753. tx->sgl, tx->nents, DMA_MEM_TO_DEV,
  754. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  755. if (!desc_tx)
  756. goto no_dma;
  757. desc_tx->callback = spi_imx_dma_tx_callback;
  758. desc_tx->callback_param = (void *)spi_imx;
  759. dmaengine_submit(desc_tx);
  760. }
  761. if (rx) {
  762. desc_rx = dmaengine_prep_slave_sg(master->dma_rx,
  763. rx->sgl, rx->nents, DMA_DEV_TO_MEM,
  764. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  765. if (!desc_rx)
  766. goto no_dma;
  767. desc_rx->callback = spi_imx_dma_rx_callback;
  768. desc_rx->callback_param = (void *)spi_imx;
  769. dmaengine_submit(desc_rx);
  770. }
  771. reinit_completion(&spi_imx->dma_rx_completion);
  772. reinit_completion(&spi_imx->dma_tx_completion);
  773. /* Trigger the cspi module. */
  774. spi_imx->dma_finished = 0;
  775. dma = readl(spi_imx->base + MX51_ECSPI_DMA);
  776. dma = dma & (~MX51_ECSPI_DMA_RXT_WML_MASK);
  777. /* Change RX_DMA_LENGTH trigger dma fetch tail data */
  778. left = transfer->len % spi_imx->rxt_wml;
  779. if (left)
  780. writel(dma | (left << MX51_ECSPI_DMA_RXT_WML_OFFSET),
  781. spi_imx->base + MX51_ECSPI_DMA);
  782. spi_imx->devtype_data->trigger(spi_imx);
  783. dma_async_issue_pending(master->dma_tx);
  784. dma_async_issue_pending(master->dma_rx);
  785. /* Wait SDMA to finish the data transfer.*/
  786. timeout = wait_for_completion_timeout(&spi_imx->dma_tx_completion,
  787. IMX_DMA_TIMEOUT);
  788. if (!timeout) {
  789. pr_warn("%s %s: I/O Error in DMA TX\n",
  790. dev_driver_string(&master->dev),
  791. dev_name(&master->dev));
  792. dmaengine_terminate_all(master->dma_tx);
  793. } else {
  794. timeout = wait_for_completion_timeout(
  795. &spi_imx->dma_rx_completion, IMX_DMA_TIMEOUT);
  796. if (!timeout) {
  797. pr_warn("%s %s: I/O Error in DMA RX\n",
  798. dev_driver_string(&master->dev),
  799. dev_name(&master->dev));
  800. spi_imx->devtype_data->reset(spi_imx);
  801. dmaengine_terminate_all(master->dma_rx);
  802. }
  803. writel(dma |
  804. spi_imx->rxt_wml << MX51_ECSPI_DMA_RXT_WML_OFFSET,
  805. spi_imx->base + MX51_ECSPI_DMA);
  806. }
  807. spi_imx->dma_finished = 1;
  808. spi_imx->devtype_data->trigger(spi_imx);
  809. if (!timeout)
  810. ret = -ETIMEDOUT;
  811. else
  812. ret = transfer->len;
  813. return ret;
  814. no_dma:
  815. pr_warn_once("%s %s: DMA not available, falling back to PIO\n",
  816. dev_driver_string(&master->dev),
  817. dev_name(&master->dev));
  818. return -EAGAIN;
  819. }
  820. static int spi_imx_pio_transfer(struct spi_device *spi,
  821. struct spi_transfer *transfer)
  822. {
  823. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  824. spi_imx->tx_buf = transfer->tx_buf;
  825. spi_imx->rx_buf = transfer->rx_buf;
  826. spi_imx->count = transfer->len;
  827. spi_imx->txfifo = 0;
  828. reinit_completion(&spi_imx->xfer_done);
  829. spi_imx_push(spi_imx);
  830. spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE);
  831. wait_for_completion(&spi_imx->xfer_done);
  832. return transfer->len;
  833. }
  834. static int spi_imx_transfer(struct spi_device *spi,
  835. struct spi_transfer *transfer)
  836. {
  837. int ret;
  838. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  839. if (spi_imx->bitbang.master->can_dma &&
  840. spi_imx_can_dma(spi_imx->bitbang.master, spi, transfer)) {
  841. spi_imx->usedma = true;
  842. ret = spi_imx_dma_transfer(spi_imx, transfer);
  843. if (ret != -EAGAIN)
  844. return ret;
  845. }
  846. spi_imx->usedma = false;
  847. return spi_imx_pio_transfer(spi, transfer);
  848. }
  849. static int spi_imx_setup(struct spi_device *spi)
  850. {
  851. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  852. int gpio = spi_imx->chipselect[spi->chip_select];
  853. dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__,
  854. spi->mode, spi->bits_per_word, spi->max_speed_hz);
  855. if (gpio_is_valid(gpio))
  856. gpio_direction_output(gpio, spi->mode & SPI_CS_HIGH ? 0 : 1);
  857. spi_imx_chipselect(spi, BITBANG_CS_INACTIVE);
  858. return 0;
  859. }
  860. static void spi_imx_cleanup(struct spi_device *spi)
  861. {
  862. }
  863. static int
  864. spi_imx_prepare_message(struct spi_master *master, struct spi_message *msg)
  865. {
  866. struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
  867. int ret;
  868. ret = clk_enable(spi_imx->clk_per);
  869. if (ret)
  870. return ret;
  871. ret = clk_enable(spi_imx->clk_ipg);
  872. if (ret) {
  873. clk_disable(spi_imx->clk_per);
  874. return ret;
  875. }
  876. return 0;
  877. }
  878. static int
  879. spi_imx_unprepare_message(struct spi_master *master, struct spi_message *msg)
  880. {
  881. struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
  882. clk_disable(spi_imx->clk_ipg);
  883. clk_disable(spi_imx->clk_per);
  884. return 0;
  885. }
  886. static int spi_imx_probe(struct platform_device *pdev)
  887. {
  888. struct device_node *np = pdev->dev.of_node;
  889. const struct of_device_id *of_id =
  890. of_match_device(spi_imx_dt_ids, &pdev->dev);
  891. struct spi_imx_master *mxc_platform_info =
  892. dev_get_platdata(&pdev->dev);
  893. struct spi_master *master;
  894. struct spi_imx_data *spi_imx;
  895. struct resource *res;
  896. int i, ret, num_cs, irq;
  897. if (!np && !mxc_platform_info) {
  898. dev_err(&pdev->dev, "can't get the platform data\n");
  899. return -EINVAL;
  900. }
  901. ret = of_property_read_u32(np, "fsl,spi-num-chipselects", &num_cs);
  902. if (ret < 0) {
  903. if (mxc_platform_info)
  904. num_cs = mxc_platform_info->num_chipselect;
  905. else
  906. return ret;
  907. }
  908. master = spi_alloc_master(&pdev->dev,
  909. sizeof(struct spi_imx_data) + sizeof(int) * num_cs);
  910. if (!master)
  911. return -ENOMEM;
  912. platform_set_drvdata(pdev, master);
  913. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
  914. master->bus_num = pdev->id;
  915. master->num_chipselect = num_cs;
  916. spi_imx = spi_master_get_devdata(master);
  917. spi_imx->bitbang.master = master;
  918. for (i = 0; i < master->num_chipselect; i++) {
  919. int cs_gpio = of_get_named_gpio(np, "cs-gpios", i);
  920. if (!gpio_is_valid(cs_gpio) && mxc_platform_info)
  921. cs_gpio = mxc_platform_info->chipselect[i];
  922. spi_imx->chipselect[i] = cs_gpio;
  923. if (!gpio_is_valid(cs_gpio))
  924. continue;
  925. ret = devm_gpio_request(&pdev->dev, spi_imx->chipselect[i],
  926. DRIVER_NAME);
  927. if (ret) {
  928. dev_err(&pdev->dev, "can't get cs gpios\n");
  929. goto out_master_put;
  930. }
  931. }
  932. spi_imx->bitbang.chipselect = spi_imx_chipselect;
  933. spi_imx->bitbang.setup_transfer = spi_imx_setupxfer;
  934. spi_imx->bitbang.txrx_bufs = spi_imx_transfer;
  935. spi_imx->bitbang.master->setup = spi_imx_setup;
  936. spi_imx->bitbang.master->cleanup = spi_imx_cleanup;
  937. spi_imx->bitbang.master->prepare_message = spi_imx_prepare_message;
  938. spi_imx->bitbang.master->unprepare_message = spi_imx_unprepare_message;
  939. spi_imx->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  940. init_completion(&spi_imx->xfer_done);
  941. spi_imx->devtype_data = of_id ? of_id->data :
  942. (struct spi_imx_devtype_data *) pdev->id_entry->driver_data;
  943. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  944. spi_imx->base = devm_ioremap_resource(&pdev->dev, res);
  945. if (IS_ERR(spi_imx->base)) {
  946. ret = PTR_ERR(spi_imx->base);
  947. goto out_master_put;
  948. }
  949. irq = platform_get_irq(pdev, 0);
  950. if (irq < 0) {
  951. ret = irq;
  952. goto out_master_put;
  953. }
  954. ret = devm_request_irq(&pdev->dev, irq, spi_imx_isr, 0,
  955. dev_name(&pdev->dev), spi_imx);
  956. if (ret) {
  957. dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
  958. goto out_master_put;
  959. }
  960. spi_imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  961. if (IS_ERR(spi_imx->clk_ipg)) {
  962. ret = PTR_ERR(spi_imx->clk_ipg);
  963. goto out_master_put;
  964. }
  965. spi_imx->clk_per = devm_clk_get(&pdev->dev, "per");
  966. if (IS_ERR(spi_imx->clk_per)) {
  967. ret = PTR_ERR(spi_imx->clk_per);
  968. goto out_master_put;
  969. }
  970. ret = clk_prepare_enable(spi_imx->clk_per);
  971. if (ret)
  972. goto out_master_put;
  973. ret = clk_prepare_enable(spi_imx->clk_ipg);
  974. if (ret)
  975. goto out_put_per;
  976. spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per);
  977. /*
  978. * Only validated on i.mx6 now, can remove the constrain if validated on
  979. * other chips.
  980. */
  981. if (spi_imx->devtype_data == &imx51_ecspi_devtype_data
  982. && spi_imx_sdma_init(&pdev->dev, spi_imx, master, res))
  983. dev_err(&pdev->dev, "dma setup error,use pio instead\n");
  984. spi_imx->devtype_data->reset(spi_imx);
  985. spi_imx->devtype_data->intctrl(spi_imx, 0);
  986. master->dev.of_node = pdev->dev.of_node;
  987. ret = spi_bitbang_start(&spi_imx->bitbang);
  988. if (ret) {
  989. dev_err(&pdev->dev, "bitbang start failed with %d\n", ret);
  990. goto out_clk_put;
  991. }
  992. dev_info(&pdev->dev, "probed\n");
  993. clk_disable(spi_imx->clk_ipg);
  994. clk_disable(spi_imx->clk_per);
  995. return ret;
  996. out_clk_put:
  997. clk_disable_unprepare(spi_imx->clk_ipg);
  998. out_put_per:
  999. clk_disable_unprepare(spi_imx->clk_per);
  1000. out_master_put:
  1001. spi_master_put(master);
  1002. return ret;
  1003. }
  1004. static int spi_imx_remove(struct platform_device *pdev)
  1005. {
  1006. struct spi_master *master = platform_get_drvdata(pdev);
  1007. struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
  1008. spi_bitbang_stop(&spi_imx->bitbang);
  1009. writel(0, spi_imx->base + MXC_CSPICTRL);
  1010. clk_unprepare(spi_imx->clk_ipg);
  1011. clk_unprepare(spi_imx->clk_per);
  1012. spi_imx_sdma_exit(spi_imx);
  1013. spi_master_put(master);
  1014. return 0;
  1015. }
  1016. static struct platform_driver spi_imx_driver = {
  1017. .driver = {
  1018. .name = DRIVER_NAME,
  1019. .of_match_table = spi_imx_dt_ids,
  1020. },
  1021. .id_table = spi_imx_devtype,
  1022. .probe = spi_imx_probe,
  1023. .remove = spi_imx_remove,
  1024. };
  1025. module_platform_driver(spi_imx_driver);
  1026. MODULE_DESCRIPTION("SPI Master Controller driver");
  1027. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  1028. MODULE_LICENSE("GPL");
  1029. MODULE_ALIAS("platform:" DRIVER_NAME);