spi-imx.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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 = NULL, *desc_rx = NULL;
  801. int ret;
  802. unsigned long transfer_timeout;
  803. unsigned long timeout;
  804. struct spi_master *master = spi_imx->bitbang.master;
  805. struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;
  806. if (tx) {
  807. desc_tx = dmaengine_prep_slave_sg(master->dma_tx,
  808. tx->sgl, tx->nents, DMA_MEM_TO_DEV,
  809. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  810. if (!desc_tx)
  811. return -EINVAL;
  812. desc_tx->callback = spi_imx_dma_tx_callback;
  813. desc_tx->callback_param = (void *)spi_imx;
  814. dmaengine_submit(desc_tx);
  815. }
  816. if (rx) {
  817. desc_rx = dmaengine_prep_slave_sg(master->dma_rx,
  818. rx->sgl, rx->nents, DMA_DEV_TO_MEM,
  819. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  820. if (!desc_rx) {
  821. dmaengine_terminate_all(master->dma_tx);
  822. return -EINVAL;
  823. }
  824. desc_rx->callback = spi_imx_dma_rx_callback;
  825. desc_rx->callback_param = (void *)spi_imx;
  826. dmaengine_submit(desc_rx);
  827. }
  828. reinit_completion(&spi_imx->dma_rx_completion);
  829. reinit_completion(&spi_imx->dma_tx_completion);
  830. /*
  831. * Set these order to avoid potential RX overflow. The overflow may
  832. * happen if we enable SPI HW before starting RX DMA due to rescheduling
  833. * for another task and/or interrupt.
  834. * So RX DMA enabled first to make sure data would be read out from FIFO
  835. * ASAP. TX DMA enabled next to start filling TX FIFO with new data.
  836. * And finaly SPI HW enabled to start actual data transfer.
  837. */
  838. dma_async_issue_pending(master->dma_rx);
  839. dma_async_issue_pending(master->dma_tx);
  840. transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
  841. /* Wait SDMA to finish the data transfer.*/
  842. timeout = wait_for_completion_timeout(&spi_imx->dma_tx_completion,
  843. transfer_timeout);
  844. if (!timeout) {
  845. dev_err(spi_imx->dev, "I/O Error in DMA TX\n");
  846. dmaengine_terminate_all(master->dma_tx);
  847. dmaengine_terminate_all(master->dma_rx);
  848. } else {
  849. timeout = wait_for_completion_timeout(
  850. &spi_imx->dma_rx_completion, transfer_timeout);
  851. if (!timeout) {
  852. dev_err(spi_imx->dev, "I/O Error in DMA RX\n");
  853. spi_imx->devtype_data->reset(spi_imx);
  854. dmaengine_terminate_all(master->dma_rx);
  855. }
  856. }
  857. if (!timeout)
  858. ret = -ETIMEDOUT;
  859. else
  860. ret = transfer->len;
  861. return ret;
  862. }
  863. static int spi_imx_pio_transfer(struct spi_device *spi,
  864. struct spi_transfer *transfer)
  865. {
  866. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  867. spi_imx->tx_buf = transfer->tx_buf;
  868. spi_imx->rx_buf = transfer->rx_buf;
  869. spi_imx->count = transfer->len;
  870. spi_imx->txfifo = 0;
  871. reinit_completion(&spi_imx->xfer_done);
  872. spi_imx_push(spi_imx);
  873. spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE);
  874. wait_for_completion(&spi_imx->xfer_done);
  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. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  889. int gpio = spi_imx->chipselect[spi->chip_select];
  890. dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__,
  891. spi->mode, spi->bits_per_word, spi->max_speed_hz);
  892. if (gpio_is_valid(gpio))
  893. gpio_direction_output(gpio, spi->mode & SPI_CS_HIGH ? 0 : 1);
  894. spi_imx_chipselect(spi, BITBANG_CS_INACTIVE);
  895. return 0;
  896. }
  897. static void spi_imx_cleanup(struct spi_device *spi)
  898. {
  899. }
  900. static int
  901. spi_imx_prepare_message(struct spi_master *master, struct spi_message *msg)
  902. {
  903. struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
  904. int ret;
  905. ret = clk_enable(spi_imx->clk_per);
  906. if (ret)
  907. return ret;
  908. ret = clk_enable(spi_imx->clk_ipg);
  909. if (ret) {
  910. clk_disable(spi_imx->clk_per);
  911. return ret;
  912. }
  913. return 0;
  914. }
  915. static int
  916. spi_imx_unprepare_message(struct spi_master *master, struct spi_message *msg)
  917. {
  918. struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
  919. clk_disable(spi_imx->clk_ipg);
  920. clk_disable(spi_imx->clk_per);
  921. return 0;
  922. }
  923. static int spi_imx_probe(struct platform_device *pdev)
  924. {
  925. struct device_node *np = pdev->dev.of_node;
  926. const struct of_device_id *of_id =
  927. of_match_device(spi_imx_dt_ids, &pdev->dev);
  928. struct spi_imx_master *mxc_platform_info =
  929. dev_get_platdata(&pdev->dev);
  930. struct spi_master *master;
  931. struct spi_imx_data *spi_imx;
  932. struct resource *res;
  933. int i, ret, num_cs, irq;
  934. if (!np && !mxc_platform_info) {
  935. dev_err(&pdev->dev, "can't get the platform data\n");
  936. return -EINVAL;
  937. }
  938. ret = of_property_read_u32(np, "fsl,spi-num-chipselects", &num_cs);
  939. if (ret < 0) {
  940. if (mxc_platform_info)
  941. num_cs = mxc_platform_info->num_chipselect;
  942. else
  943. return ret;
  944. }
  945. master = spi_alloc_master(&pdev->dev,
  946. sizeof(struct spi_imx_data) + sizeof(int) * num_cs);
  947. if (!master)
  948. return -ENOMEM;
  949. platform_set_drvdata(pdev, master);
  950. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
  951. master->bus_num = pdev->id;
  952. master->num_chipselect = num_cs;
  953. spi_imx = spi_master_get_devdata(master);
  954. spi_imx->bitbang.master = master;
  955. spi_imx->dev = &pdev->dev;
  956. spi_imx->devtype_data = of_id ? of_id->data :
  957. (struct spi_imx_devtype_data *)pdev->id_entry->driver_data;
  958. for (i = 0; i < master->num_chipselect; i++) {
  959. int cs_gpio = of_get_named_gpio(np, "cs-gpios", i);
  960. if (!gpio_is_valid(cs_gpio) && mxc_platform_info)
  961. cs_gpio = mxc_platform_info->chipselect[i];
  962. spi_imx->chipselect[i] = cs_gpio;
  963. if (!gpio_is_valid(cs_gpio))
  964. continue;
  965. ret = devm_gpio_request(&pdev->dev, spi_imx->chipselect[i],
  966. DRIVER_NAME);
  967. if (ret) {
  968. dev_err(&pdev->dev, "can't get cs gpios\n");
  969. goto out_master_put;
  970. }
  971. }
  972. spi_imx->bitbang.chipselect = spi_imx_chipselect;
  973. spi_imx->bitbang.setup_transfer = spi_imx_setupxfer;
  974. spi_imx->bitbang.txrx_bufs = spi_imx_transfer;
  975. spi_imx->bitbang.master->setup = spi_imx_setup;
  976. spi_imx->bitbang.master->cleanup = spi_imx_cleanup;
  977. spi_imx->bitbang.master->prepare_message = spi_imx_prepare_message;
  978. spi_imx->bitbang.master->unprepare_message = spi_imx_unprepare_message;
  979. spi_imx->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  980. if (is_imx51_ecspi(spi_imx))
  981. spi_imx->bitbang.master->mode_bits |= SPI_LOOP;
  982. init_completion(&spi_imx->xfer_done);
  983. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  984. spi_imx->base = devm_ioremap_resource(&pdev->dev, res);
  985. if (IS_ERR(spi_imx->base)) {
  986. ret = PTR_ERR(spi_imx->base);
  987. goto out_master_put;
  988. }
  989. spi_imx->base_phys = res->start;
  990. irq = platform_get_irq(pdev, 0);
  991. if (irq < 0) {
  992. ret = irq;
  993. goto out_master_put;
  994. }
  995. ret = devm_request_irq(&pdev->dev, irq, spi_imx_isr, 0,
  996. dev_name(&pdev->dev), spi_imx);
  997. if (ret) {
  998. dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
  999. goto out_master_put;
  1000. }
  1001. spi_imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1002. if (IS_ERR(spi_imx->clk_ipg)) {
  1003. ret = PTR_ERR(spi_imx->clk_ipg);
  1004. goto out_master_put;
  1005. }
  1006. spi_imx->clk_per = devm_clk_get(&pdev->dev, "per");
  1007. if (IS_ERR(spi_imx->clk_per)) {
  1008. ret = PTR_ERR(spi_imx->clk_per);
  1009. goto out_master_put;
  1010. }
  1011. ret = clk_prepare_enable(spi_imx->clk_per);
  1012. if (ret)
  1013. goto out_master_put;
  1014. ret = clk_prepare_enable(spi_imx->clk_ipg);
  1015. if (ret)
  1016. goto out_put_per;
  1017. spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per);
  1018. /*
  1019. * Only validated on i.mx6 now, can remove the constrain if validated on
  1020. * other chips.
  1021. */
  1022. if (is_imx51_ecspi(spi_imx)) {
  1023. ret = spi_imx_sdma_init(&pdev->dev, spi_imx, master);
  1024. if (ret == -EPROBE_DEFER)
  1025. goto out_clk_put;
  1026. if (ret < 0)
  1027. dev_err(&pdev->dev, "dma setup error %d, use pio\n",
  1028. ret);
  1029. }
  1030. spi_imx->devtype_data->reset(spi_imx);
  1031. spi_imx->devtype_data->intctrl(spi_imx, 0);
  1032. master->dev.of_node = pdev->dev.of_node;
  1033. ret = spi_bitbang_start(&spi_imx->bitbang);
  1034. if (ret) {
  1035. dev_err(&pdev->dev, "bitbang start failed with %d\n", ret);
  1036. goto out_clk_put;
  1037. }
  1038. dev_info(&pdev->dev, "probed\n");
  1039. clk_disable(spi_imx->clk_ipg);
  1040. clk_disable(spi_imx->clk_per);
  1041. return ret;
  1042. out_clk_put:
  1043. clk_disable_unprepare(spi_imx->clk_ipg);
  1044. out_put_per:
  1045. clk_disable_unprepare(spi_imx->clk_per);
  1046. out_master_put:
  1047. spi_master_put(master);
  1048. return ret;
  1049. }
  1050. static int spi_imx_remove(struct platform_device *pdev)
  1051. {
  1052. struct spi_master *master = platform_get_drvdata(pdev);
  1053. struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
  1054. spi_bitbang_stop(&spi_imx->bitbang);
  1055. writel(0, spi_imx->base + MXC_CSPICTRL);
  1056. clk_unprepare(spi_imx->clk_ipg);
  1057. clk_unprepare(spi_imx->clk_per);
  1058. spi_imx_sdma_exit(spi_imx);
  1059. spi_master_put(master);
  1060. return 0;
  1061. }
  1062. static struct platform_driver spi_imx_driver = {
  1063. .driver = {
  1064. .name = DRIVER_NAME,
  1065. .of_match_table = spi_imx_dt_ids,
  1066. },
  1067. .id_table = spi_imx_devtype,
  1068. .probe = spi_imx_probe,
  1069. .remove = spi_imx_remove,
  1070. };
  1071. module_platform_driver(spi_imx_driver);
  1072. MODULE_DESCRIPTION("SPI Master Controller driver");
  1073. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  1074. MODULE_LICENSE("GPL");
  1075. MODULE_ALIAS("platform:" DRIVER_NAME);