spi-imx.c 33 KB

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