spi-imx.c 34 KB

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