spi-imx.c 34 KB

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