spi-imx.c 33 KB

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