spi-imx.c 35 KB

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