spi-imx.c 34 KB

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