spi-imx.c 35 KB

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