spi-imx.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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/err.h>
  24. #include <linux/gpio.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/irq.h>
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/slab.h>
  33. #include <linux/spi/spi.h>
  34. #include <linux/spi/spi_bitbang.h>
  35. #include <linux/types.h>
  36. #include <linux/of.h>
  37. #include <linux/of_device.h>
  38. #include <linux/of_gpio.h>
  39. #include <mach/spi.h>
  40. #define DRIVER_NAME "spi_imx"
  41. #define MXC_CSPIRXDATA 0x00
  42. #define MXC_CSPITXDATA 0x04
  43. #define MXC_CSPICTRL 0x08
  44. #define MXC_CSPIINT 0x0c
  45. #define MXC_RESET 0x1c
  46. /* generic defines to abstract from the different register layouts */
  47. #define MXC_INT_RR (1 << 0) /* Receive data ready interrupt */
  48. #define MXC_INT_TE (1 << 1) /* Transmit FIFO empty interrupt */
  49. struct spi_imx_config {
  50. unsigned int speed_hz;
  51. unsigned int bpw;
  52. unsigned int mode;
  53. u8 cs;
  54. };
  55. enum spi_imx_devtype {
  56. IMX1_CSPI,
  57. IMX21_CSPI,
  58. IMX27_CSPI,
  59. IMX31_CSPI,
  60. IMX35_CSPI, /* CSPI on all i.mx except above */
  61. IMX51_ECSPI, /* ECSPI on i.mx51 and later */
  62. };
  63. struct spi_imx_data;
  64. struct spi_imx_devtype_data {
  65. void (*intctrl)(struct spi_imx_data *, int);
  66. int (*config)(struct spi_imx_data *, struct spi_imx_config *);
  67. void (*trigger)(struct spi_imx_data *);
  68. int (*rx_available)(struct spi_imx_data *);
  69. void (*reset)(struct spi_imx_data *);
  70. enum spi_imx_devtype devtype;
  71. };
  72. struct spi_imx_data {
  73. struct spi_bitbang bitbang;
  74. struct completion xfer_done;
  75. void __iomem *base;
  76. int irq;
  77. struct clk *clk;
  78. unsigned long spi_clk;
  79. unsigned int count;
  80. void (*tx)(struct spi_imx_data *);
  81. void (*rx)(struct spi_imx_data *);
  82. void *rx_buf;
  83. const void *tx_buf;
  84. unsigned int txfifo; /* number of words pushed in tx FIFO */
  85. struct spi_imx_devtype_data *devtype_data;
  86. int chipselect[0];
  87. };
  88. static inline int is_imx27_cspi(struct spi_imx_data *d)
  89. {
  90. return d->devtype_data->devtype == IMX27_CSPI;
  91. }
  92. static inline int is_imx35_cspi(struct spi_imx_data *d)
  93. {
  94. return d->devtype_data->devtype == IMX35_CSPI;
  95. }
  96. static inline unsigned spi_imx_get_fifosize(struct spi_imx_data *d)
  97. {
  98. return (d->devtype_data->devtype == IMX51_ECSPI) ? 64 : 8;
  99. }
  100. #define MXC_SPI_BUF_RX(type) \
  101. static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx) \
  102. { \
  103. unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); \
  104. \
  105. if (spi_imx->rx_buf) { \
  106. *(type *)spi_imx->rx_buf = val; \
  107. spi_imx->rx_buf += sizeof(type); \
  108. } \
  109. }
  110. #define MXC_SPI_BUF_TX(type) \
  111. static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx) \
  112. { \
  113. type val = 0; \
  114. \
  115. if (spi_imx->tx_buf) { \
  116. val = *(type *)spi_imx->tx_buf; \
  117. spi_imx->tx_buf += sizeof(type); \
  118. } \
  119. \
  120. spi_imx->count -= sizeof(type); \
  121. \
  122. writel(val, spi_imx->base + MXC_CSPITXDATA); \
  123. }
  124. MXC_SPI_BUF_RX(u8)
  125. MXC_SPI_BUF_TX(u8)
  126. MXC_SPI_BUF_RX(u16)
  127. MXC_SPI_BUF_TX(u16)
  128. MXC_SPI_BUF_RX(u32)
  129. MXC_SPI_BUF_TX(u32)
  130. /* First entry is reserved, second entry is valid only if SDHC_SPIEN is set
  131. * (which is currently not the case in this driver)
  132. */
  133. static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
  134. 256, 384, 512, 768, 1024};
  135. /* MX21, MX27 */
  136. static unsigned int spi_imx_clkdiv_1(unsigned int fin,
  137. unsigned int fspi, unsigned int max)
  138. {
  139. int i;
  140. for (i = 2; i < max; i++)
  141. if (fspi * mxc_clkdivs[i] >= fin)
  142. return i;
  143. return max;
  144. }
  145. /* MX1, MX31, MX35, MX51 CSPI */
  146. static unsigned int spi_imx_clkdiv_2(unsigned int fin,
  147. unsigned int fspi)
  148. {
  149. int i, div = 4;
  150. for (i = 0; i < 7; i++) {
  151. if (fspi * div >= fin)
  152. return i;
  153. div <<= 1;
  154. }
  155. return 7;
  156. }
  157. #define MX51_ECSPI_CTRL 0x08
  158. #define MX51_ECSPI_CTRL_ENABLE (1 << 0)
  159. #define MX51_ECSPI_CTRL_XCH (1 << 2)
  160. #define MX51_ECSPI_CTRL_MODE_MASK (0xf << 4)
  161. #define MX51_ECSPI_CTRL_POSTDIV_OFFSET 8
  162. #define MX51_ECSPI_CTRL_PREDIV_OFFSET 12
  163. #define MX51_ECSPI_CTRL_CS(cs) ((cs) << 18)
  164. #define MX51_ECSPI_CTRL_BL_OFFSET 20
  165. #define MX51_ECSPI_CONFIG 0x0c
  166. #define MX51_ECSPI_CONFIG_SCLKPHA(cs) (1 << ((cs) + 0))
  167. #define MX51_ECSPI_CONFIG_SCLKPOL(cs) (1 << ((cs) + 4))
  168. #define MX51_ECSPI_CONFIG_SBBCTRL(cs) (1 << ((cs) + 8))
  169. #define MX51_ECSPI_CONFIG_SSBPOL(cs) (1 << ((cs) + 12))
  170. #define MX51_ECSPI_INT 0x10
  171. #define MX51_ECSPI_INT_TEEN (1 << 0)
  172. #define MX51_ECSPI_INT_RREN (1 << 3)
  173. #define MX51_ECSPI_STAT 0x18
  174. #define MX51_ECSPI_STAT_RR (1 << 3)
  175. /* MX51 eCSPI */
  176. static unsigned int mx51_ecspi_clkdiv(unsigned int fin, unsigned int fspi)
  177. {
  178. /*
  179. * there are two 4-bit dividers, the pre-divider divides by
  180. * $pre, the post-divider by 2^$post
  181. */
  182. unsigned int pre, post;
  183. if (unlikely(fspi > fin))
  184. return 0;
  185. post = fls(fin) - fls(fspi);
  186. if (fin > fspi << post)
  187. post++;
  188. /* now we have: (fin <= fspi << post) with post being minimal */
  189. post = max(4U, post) - 4;
  190. if (unlikely(post > 0xf)) {
  191. pr_err("%s: cannot set clock freq: %u (base freq: %u)\n",
  192. __func__, fspi, fin);
  193. return 0xff;
  194. }
  195. pre = DIV_ROUND_UP(fin, fspi << post) - 1;
  196. pr_debug("%s: fin: %u, fspi: %u, post: %u, pre: %u\n",
  197. __func__, fin, fspi, post, pre);
  198. return (pre << MX51_ECSPI_CTRL_PREDIV_OFFSET) |
  199. (post << MX51_ECSPI_CTRL_POSTDIV_OFFSET);
  200. }
  201. static void __maybe_unused mx51_ecspi_intctrl(struct spi_imx_data *spi_imx, int enable)
  202. {
  203. unsigned val = 0;
  204. if (enable & MXC_INT_TE)
  205. val |= MX51_ECSPI_INT_TEEN;
  206. if (enable & MXC_INT_RR)
  207. val |= MX51_ECSPI_INT_RREN;
  208. writel(val, spi_imx->base + MX51_ECSPI_INT);
  209. }
  210. static void __maybe_unused mx51_ecspi_trigger(struct spi_imx_data *spi_imx)
  211. {
  212. u32 reg;
  213. reg = readl(spi_imx->base + MX51_ECSPI_CTRL);
  214. reg |= MX51_ECSPI_CTRL_XCH;
  215. writel(reg, spi_imx->base + MX51_ECSPI_CTRL);
  216. }
  217. static int __maybe_unused mx51_ecspi_config(struct spi_imx_data *spi_imx,
  218. struct spi_imx_config *config)
  219. {
  220. u32 ctrl = MX51_ECSPI_CTRL_ENABLE, cfg = 0;
  221. /*
  222. * The hardware seems to have a race condition when changing modes. The
  223. * current assumption is that the selection of the channel arrives
  224. * earlier in the hardware than the mode bits when they are written at
  225. * the same time.
  226. * So set master mode for all channels as we do not support slave mode.
  227. */
  228. ctrl |= MX51_ECSPI_CTRL_MODE_MASK;
  229. /* set clock speed */
  230. ctrl |= mx51_ecspi_clkdiv(spi_imx->spi_clk, config->speed_hz);
  231. /* set chip select to use */
  232. ctrl |= MX51_ECSPI_CTRL_CS(config->cs);
  233. ctrl |= (config->bpw - 1) << MX51_ECSPI_CTRL_BL_OFFSET;
  234. cfg |= MX51_ECSPI_CONFIG_SBBCTRL(config->cs);
  235. if (config->mode & SPI_CPHA)
  236. cfg |= MX51_ECSPI_CONFIG_SCLKPHA(config->cs);
  237. if (config->mode & SPI_CPOL)
  238. cfg |= MX51_ECSPI_CONFIG_SCLKPOL(config->cs);
  239. if (config->mode & SPI_CS_HIGH)
  240. cfg |= MX51_ECSPI_CONFIG_SSBPOL(config->cs);
  241. writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
  242. writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
  243. return 0;
  244. }
  245. static int __maybe_unused mx51_ecspi_rx_available(struct spi_imx_data *spi_imx)
  246. {
  247. return readl(spi_imx->base + MX51_ECSPI_STAT) & MX51_ECSPI_STAT_RR;
  248. }
  249. static void __maybe_unused mx51_ecspi_reset(struct spi_imx_data *spi_imx)
  250. {
  251. /* drain receive buffer */
  252. while (mx51_ecspi_rx_available(spi_imx))
  253. readl(spi_imx->base + MXC_CSPIRXDATA);
  254. }
  255. #define MX31_INTREG_TEEN (1 << 0)
  256. #define MX31_INTREG_RREN (1 << 3)
  257. #define MX31_CSPICTRL_ENABLE (1 << 0)
  258. #define MX31_CSPICTRL_MASTER (1 << 1)
  259. #define MX31_CSPICTRL_XCH (1 << 2)
  260. #define MX31_CSPICTRL_POL (1 << 4)
  261. #define MX31_CSPICTRL_PHA (1 << 5)
  262. #define MX31_CSPICTRL_SSCTL (1 << 6)
  263. #define MX31_CSPICTRL_SSPOL (1 << 7)
  264. #define MX31_CSPICTRL_BC_SHIFT 8
  265. #define MX35_CSPICTRL_BL_SHIFT 20
  266. #define MX31_CSPICTRL_CS_SHIFT 24
  267. #define MX35_CSPICTRL_CS_SHIFT 12
  268. #define MX31_CSPICTRL_DR_SHIFT 16
  269. #define MX31_CSPISTATUS 0x14
  270. #define MX31_STATUS_RR (1 << 3)
  271. /* These functions also work for the i.MX35, but be aware that
  272. * the i.MX35 has a slightly different register layout for bits
  273. * we do not use here.
  274. */
  275. static void __maybe_unused mx31_intctrl(struct spi_imx_data *spi_imx, int enable)
  276. {
  277. unsigned int val = 0;
  278. if (enable & MXC_INT_TE)
  279. val |= MX31_INTREG_TEEN;
  280. if (enable & MXC_INT_RR)
  281. val |= MX31_INTREG_RREN;
  282. writel(val, spi_imx->base + MXC_CSPIINT);
  283. }
  284. static void __maybe_unused mx31_trigger(struct spi_imx_data *spi_imx)
  285. {
  286. unsigned int reg;
  287. reg = readl(spi_imx->base + MXC_CSPICTRL);
  288. reg |= MX31_CSPICTRL_XCH;
  289. writel(reg, spi_imx->base + MXC_CSPICTRL);
  290. }
  291. static int __maybe_unused mx31_config(struct spi_imx_data *spi_imx,
  292. struct spi_imx_config *config)
  293. {
  294. unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER;
  295. int cs = spi_imx->chipselect[config->cs];
  296. reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) <<
  297. MX31_CSPICTRL_DR_SHIFT;
  298. if (is_imx35_cspi(spi_imx)) {
  299. reg |= (config->bpw - 1) << MX35_CSPICTRL_BL_SHIFT;
  300. reg |= MX31_CSPICTRL_SSCTL;
  301. } else {
  302. reg |= (config->bpw - 1) << MX31_CSPICTRL_BC_SHIFT;
  303. }
  304. if (config->mode & SPI_CPHA)
  305. reg |= MX31_CSPICTRL_PHA;
  306. if (config->mode & SPI_CPOL)
  307. reg |= MX31_CSPICTRL_POL;
  308. if (config->mode & SPI_CS_HIGH)
  309. reg |= MX31_CSPICTRL_SSPOL;
  310. if (cs < 0)
  311. reg |= (cs + 32) <<
  312. (is_imx35_cspi(spi_imx) ? MX35_CSPICTRL_CS_SHIFT :
  313. MX31_CSPICTRL_CS_SHIFT);
  314. writel(reg, spi_imx->base + MXC_CSPICTRL);
  315. return 0;
  316. }
  317. static int __maybe_unused mx31_rx_available(struct spi_imx_data *spi_imx)
  318. {
  319. return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR;
  320. }
  321. static void __maybe_unused mx31_reset(struct spi_imx_data *spi_imx)
  322. {
  323. /* drain receive buffer */
  324. while (readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR)
  325. readl(spi_imx->base + MXC_CSPIRXDATA);
  326. }
  327. #define MX21_INTREG_RR (1 << 4)
  328. #define MX21_INTREG_TEEN (1 << 9)
  329. #define MX21_INTREG_RREN (1 << 13)
  330. #define MX21_CSPICTRL_POL (1 << 5)
  331. #define MX21_CSPICTRL_PHA (1 << 6)
  332. #define MX21_CSPICTRL_SSPOL (1 << 8)
  333. #define MX21_CSPICTRL_XCH (1 << 9)
  334. #define MX21_CSPICTRL_ENABLE (1 << 10)
  335. #define MX21_CSPICTRL_MASTER (1 << 11)
  336. #define MX21_CSPICTRL_DR_SHIFT 14
  337. #define MX21_CSPICTRL_CS_SHIFT 19
  338. static void __maybe_unused mx21_intctrl(struct spi_imx_data *spi_imx, int enable)
  339. {
  340. unsigned int val = 0;
  341. if (enable & MXC_INT_TE)
  342. val |= MX21_INTREG_TEEN;
  343. if (enable & MXC_INT_RR)
  344. val |= MX21_INTREG_RREN;
  345. writel(val, spi_imx->base + MXC_CSPIINT);
  346. }
  347. static void __maybe_unused mx21_trigger(struct spi_imx_data *spi_imx)
  348. {
  349. unsigned int reg;
  350. reg = readl(spi_imx->base + MXC_CSPICTRL);
  351. reg |= MX21_CSPICTRL_XCH;
  352. writel(reg, spi_imx->base + MXC_CSPICTRL);
  353. }
  354. static int __maybe_unused mx21_config(struct spi_imx_data *spi_imx,
  355. struct spi_imx_config *config)
  356. {
  357. unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_MASTER;
  358. int cs = spi_imx->chipselect[config->cs];
  359. unsigned int max = is_imx27_cspi(spi_imx) ? 16 : 18;
  360. reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, config->speed_hz, max) <<
  361. MX21_CSPICTRL_DR_SHIFT;
  362. reg |= config->bpw - 1;
  363. if (config->mode & SPI_CPHA)
  364. reg |= MX21_CSPICTRL_PHA;
  365. if (config->mode & SPI_CPOL)
  366. reg |= MX21_CSPICTRL_POL;
  367. if (config->mode & SPI_CS_HIGH)
  368. reg |= MX21_CSPICTRL_SSPOL;
  369. if (cs < 0)
  370. reg |= (cs + 32) << MX21_CSPICTRL_CS_SHIFT;
  371. writel(reg, spi_imx->base + MXC_CSPICTRL);
  372. return 0;
  373. }
  374. static int __maybe_unused mx21_rx_available(struct spi_imx_data *spi_imx)
  375. {
  376. return readl(spi_imx->base + MXC_CSPIINT) & MX21_INTREG_RR;
  377. }
  378. static void __maybe_unused mx21_reset(struct spi_imx_data *spi_imx)
  379. {
  380. writel(1, spi_imx->base + MXC_RESET);
  381. }
  382. #define MX1_INTREG_RR (1 << 3)
  383. #define MX1_INTREG_TEEN (1 << 8)
  384. #define MX1_INTREG_RREN (1 << 11)
  385. #define MX1_CSPICTRL_POL (1 << 4)
  386. #define MX1_CSPICTRL_PHA (1 << 5)
  387. #define MX1_CSPICTRL_XCH (1 << 8)
  388. #define MX1_CSPICTRL_ENABLE (1 << 9)
  389. #define MX1_CSPICTRL_MASTER (1 << 10)
  390. #define MX1_CSPICTRL_DR_SHIFT 13
  391. static void __maybe_unused mx1_intctrl(struct spi_imx_data *spi_imx, int enable)
  392. {
  393. unsigned int val = 0;
  394. if (enable & MXC_INT_TE)
  395. val |= MX1_INTREG_TEEN;
  396. if (enable & MXC_INT_RR)
  397. val |= MX1_INTREG_RREN;
  398. writel(val, spi_imx->base + MXC_CSPIINT);
  399. }
  400. static void __maybe_unused mx1_trigger(struct spi_imx_data *spi_imx)
  401. {
  402. unsigned int reg;
  403. reg = readl(spi_imx->base + MXC_CSPICTRL);
  404. reg |= MX1_CSPICTRL_XCH;
  405. writel(reg, spi_imx->base + MXC_CSPICTRL);
  406. }
  407. static int __maybe_unused mx1_config(struct spi_imx_data *spi_imx,
  408. struct spi_imx_config *config)
  409. {
  410. unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER;
  411. reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) <<
  412. MX1_CSPICTRL_DR_SHIFT;
  413. reg |= config->bpw - 1;
  414. if (config->mode & SPI_CPHA)
  415. reg |= MX1_CSPICTRL_PHA;
  416. if (config->mode & SPI_CPOL)
  417. reg |= MX1_CSPICTRL_POL;
  418. writel(reg, spi_imx->base + MXC_CSPICTRL);
  419. return 0;
  420. }
  421. static int __maybe_unused mx1_rx_available(struct spi_imx_data *spi_imx)
  422. {
  423. return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR;
  424. }
  425. static void __maybe_unused mx1_reset(struct spi_imx_data *spi_imx)
  426. {
  427. writel(1, spi_imx->base + MXC_RESET);
  428. }
  429. static struct spi_imx_devtype_data imx1_cspi_devtype_data = {
  430. .intctrl = mx1_intctrl,
  431. .config = mx1_config,
  432. .trigger = mx1_trigger,
  433. .rx_available = mx1_rx_available,
  434. .reset = mx1_reset,
  435. .devtype = IMX1_CSPI,
  436. };
  437. static struct spi_imx_devtype_data imx21_cspi_devtype_data = {
  438. .intctrl = mx21_intctrl,
  439. .config = mx21_config,
  440. .trigger = mx21_trigger,
  441. .rx_available = mx21_rx_available,
  442. .reset = mx21_reset,
  443. .devtype = IMX21_CSPI,
  444. };
  445. static struct spi_imx_devtype_data imx27_cspi_devtype_data = {
  446. /* i.mx27 cspi shares the functions with i.mx21 one */
  447. .intctrl = mx21_intctrl,
  448. .config = mx21_config,
  449. .trigger = mx21_trigger,
  450. .rx_available = mx21_rx_available,
  451. .reset = mx21_reset,
  452. .devtype = IMX27_CSPI,
  453. };
  454. static struct spi_imx_devtype_data imx31_cspi_devtype_data = {
  455. .intctrl = mx31_intctrl,
  456. .config = mx31_config,
  457. .trigger = mx31_trigger,
  458. .rx_available = mx31_rx_available,
  459. .reset = mx31_reset,
  460. .devtype = IMX31_CSPI,
  461. };
  462. static struct spi_imx_devtype_data imx35_cspi_devtype_data = {
  463. /* i.mx35 and later cspi shares the functions with i.mx31 one */
  464. .intctrl = mx31_intctrl,
  465. .config = mx31_config,
  466. .trigger = mx31_trigger,
  467. .rx_available = mx31_rx_available,
  468. .reset = mx31_reset,
  469. .devtype = IMX35_CSPI,
  470. };
  471. static struct spi_imx_devtype_data imx51_ecspi_devtype_data = {
  472. .intctrl = mx51_ecspi_intctrl,
  473. .config = mx51_ecspi_config,
  474. .trigger = mx51_ecspi_trigger,
  475. .rx_available = mx51_ecspi_rx_available,
  476. .reset = mx51_ecspi_reset,
  477. .devtype = IMX51_ECSPI,
  478. };
  479. static struct platform_device_id spi_imx_devtype[] = {
  480. {
  481. .name = "imx1-cspi",
  482. .driver_data = (kernel_ulong_t) &imx1_cspi_devtype_data,
  483. }, {
  484. .name = "imx21-cspi",
  485. .driver_data = (kernel_ulong_t) &imx21_cspi_devtype_data,
  486. }, {
  487. .name = "imx27-cspi",
  488. .driver_data = (kernel_ulong_t) &imx27_cspi_devtype_data,
  489. }, {
  490. .name = "imx31-cspi",
  491. .driver_data = (kernel_ulong_t) &imx31_cspi_devtype_data,
  492. }, {
  493. .name = "imx35-cspi",
  494. .driver_data = (kernel_ulong_t) &imx35_cspi_devtype_data,
  495. }, {
  496. .name = "imx51-ecspi",
  497. .driver_data = (kernel_ulong_t) &imx51_ecspi_devtype_data,
  498. }, {
  499. /* sentinel */
  500. }
  501. };
  502. static const struct of_device_id spi_imx_dt_ids[] = {
  503. { .compatible = "fsl,imx1-cspi", .data = &imx1_cspi_devtype_data, },
  504. { .compatible = "fsl,imx21-cspi", .data = &imx21_cspi_devtype_data, },
  505. { .compatible = "fsl,imx27-cspi", .data = &imx27_cspi_devtype_data, },
  506. { .compatible = "fsl,imx31-cspi", .data = &imx31_cspi_devtype_data, },
  507. { .compatible = "fsl,imx35-cspi", .data = &imx35_cspi_devtype_data, },
  508. { .compatible = "fsl,imx51-ecspi", .data = &imx51_ecspi_devtype_data, },
  509. { /* sentinel */ }
  510. };
  511. static void spi_imx_chipselect(struct spi_device *spi, int is_active)
  512. {
  513. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  514. int gpio = spi_imx->chipselect[spi->chip_select];
  515. int active = is_active != BITBANG_CS_INACTIVE;
  516. int dev_is_lowactive = !(spi->mode & SPI_CS_HIGH);
  517. if (gpio < 0)
  518. return;
  519. gpio_set_value(gpio, dev_is_lowactive ^ active);
  520. }
  521. static void spi_imx_push(struct spi_imx_data *spi_imx)
  522. {
  523. while (spi_imx->txfifo < spi_imx_get_fifosize(spi_imx)) {
  524. if (!spi_imx->count)
  525. break;
  526. spi_imx->tx(spi_imx);
  527. spi_imx->txfifo++;
  528. }
  529. spi_imx->devtype_data->trigger(spi_imx);
  530. }
  531. static irqreturn_t spi_imx_isr(int irq, void *dev_id)
  532. {
  533. struct spi_imx_data *spi_imx = dev_id;
  534. while (spi_imx->devtype_data->rx_available(spi_imx)) {
  535. spi_imx->rx(spi_imx);
  536. spi_imx->txfifo--;
  537. }
  538. if (spi_imx->count) {
  539. spi_imx_push(spi_imx);
  540. return IRQ_HANDLED;
  541. }
  542. if (spi_imx->txfifo) {
  543. /* No data left to push, but still waiting for rx data,
  544. * enable receive data available interrupt.
  545. */
  546. spi_imx->devtype_data->intctrl(
  547. spi_imx, MXC_INT_RR);
  548. return IRQ_HANDLED;
  549. }
  550. spi_imx->devtype_data->intctrl(spi_imx, 0);
  551. complete(&spi_imx->xfer_done);
  552. return IRQ_HANDLED;
  553. }
  554. static int spi_imx_setupxfer(struct spi_device *spi,
  555. struct spi_transfer *t)
  556. {
  557. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  558. struct spi_imx_config config;
  559. config.bpw = t ? t->bits_per_word : spi->bits_per_word;
  560. config.speed_hz = t ? t->speed_hz : spi->max_speed_hz;
  561. config.mode = spi->mode;
  562. config.cs = spi->chip_select;
  563. if (!config.speed_hz)
  564. config.speed_hz = spi->max_speed_hz;
  565. if (!config.bpw)
  566. config.bpw = spi->bits_per_word;
  567. if (!config.speed_hz)
  568. config.speed_hz = spi->max_speed_hz;
  569. /* Initialize the functions for transfer */
  570. if (config.bpw <= 8) {
  571. spi_imx->rx = spi_imx_buf_rx_u8;
  572. spi_imx->tx = spi_imx_buf_tx_u8;
  573. } else if (config.bpw <= 16) {
  574. spi_imx->rx = spi_imx_buf_rx_u16;
  575. spi_imx->tx = spi_imx_buf_tx_u16;
  576. } else if (config.bpw <= 32) {
  577. spi_imx->rx = spi_imx_buf_rx_u32;
  578. spi_imx->tx = spi_imx_buf_tx_u32;
  579. } else
  580. BUG();
  581. spi_imx->devtype_data->config(spi_imx, &config);
  582. return 0;
  583. }
  584. static int spi_imx_transfer(struct spi_device *spi,
  585. struct spi_transfer *transfer)
  586. {
  587. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  588. spi_imx->tx_buf = transfer->tx_buf;
  589. spi_imx->rx_buf = transfer->rx_buf;
  590. spi_imx->count = transfer->len;
  591. spi_imx->txfifo = 0;
  592. init_completion(&spi_imx->xfer_done);
  593. spi_imx_push(spi_imx);
  594. spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE);
  595. wait_for_completion(&spi_imx->xfer_done);
  596. return transfer->len;
  597. }
  598. static int spi_imx_setup(struct spi_device *spi)
  599. {
  600. struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
  601. int gpio = spi_imx->chipselect[spi->chip_select];
  602. dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__,
  603. spi->mode, spi->bits_per_word, spi->max_speed_hz);
  604. if (gpio >= 0)
  605. gpio_direction_output(gpio, spi->mode & SPI_CS_HIGH ? 0 : 1);
  606. spi_imx_chipselect(spi, BITBANG_CS_INACTIVE);
  607. return 0;
  608. }
  609. static void spi_imx_cleanup(struct spi_device *spi)
  610. {
  611. }
  612. static int __devinit spi_imx_probe(struct platform_device *pdev)
  613. {
  614. struct device_node *np = pdev->dev.of_node;
  615. const struct of_device_id *of_id =
  616. of_match_device(spi_imx_dt_ids, &pdev->dev);
  617. struct spi_imx_master *mxc_platform_info =
  618. dev_get_platdata(&pdev->dev);
  619. struct spi_master *master;
  620. struct spi_imx_data *spi_imx;
  621. struct resource *res;
  622. int i, ret, num_cs;
  623. if (!np && !mxc_platform_info) {
  624. dev_err(&pdev->dev, "can't get the platform data\n");
  625. return -EINVAL;
  626. }
  627. ret = of_property_read_u32(np, "fsl,spi-num-chipselects", &num_cs);
  628. if (ret < 0) {
  629. if (mxc_platform_info)
  630. num_cs = mxc_platform_info->num_chipselect;
  631. else
  632. return ret;
  633. }
  634. master = spi_alloc_master(&pdev->dev,
  635. sizeof(struct spi_imx_data) + sizeof(int) * num_cs);
  636. if (!master)
  637. return -ENOMEM;
  638. platform_set_drvdata(pdev, master);
  639. master->bus_num = pdev->id;
  640. master->num_chipselect = num_cs;
  641. spi_imx = spi_master_get_devdata(master);
  642. spi_imx->bitbang.master = spi_master_get(master);
  643. for (i = 0; i < master->num_chipselect; i++) {
  644. int cs_gpio = of_get_named_gpio(np, "cs-gpios", i);
  645. if (cs_gpio < 0 && mxc_platform_info)
  646. cs_gpio = mxc_platform_info->chipselect[i];
  647. spi_imx->chipselect[i] = cs_gpio;
  648. if (cs_gpio < 0)
  649. continue;
  650. ret = gpio_request(spi_imx->chipselect[i], DRIVER_NAME);
  651. if (ret) {
  652. dev_err(&pdev->dev, "can't get cs gpios\n");
  653. goto out_gpio_free;
  654. }
  655. }
  656. spi_imx->bitbang.chipselect = spi_imx_chipselect;
  657. spi_imx->bitbang.setup_transfer = spi_imx_setupxfer;
  658. spi_imx->bitbang.txrx_bufs = spi_imx_transfer;
  659. spi_imx->bitbang.master->setup = spi_imx_setup;
  660. spi_imx->bitbang.master->cleanup = spi_imx_cleanup;
  661. spi_imx->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  662. init_completion(&spi_imx->xfer_done);
  663. spi_imx->devtype_data = of_id ? of_id->data :
  664. (struct spi_imx_devtype_data *) pdev->id_entry->driver_data;
  665. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  666. if (!res) {
  667. dev_err(&pdev->dev, "can't get platform resource\n");
  668. ret = -ENOMEM;
  669. goto out_gpio_free;
  670. }
  671. if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
  672. dev_err(&pdev->dev, "request_mem_region failed\n");
  673. ret = -EBUSY;
  674. goto out_gpio_free;
  675. }
  676. spi_imx->base = ioremap(res->start, resource_size(res));
  677. if (!spi_imx->base) {
  678. ret = -EINVAL;
  679. goto out_release_mem;
  680. }
  681. spi_imx->irq = platform_get_irq(pdev, 0);
  682. if (spi_imx->irq < 0) {
  683. ret = -EINVAL;
  684. goto out_iounmap;
  685. }
  686. ret = request_irq(spi_imx->irq, spi_imx_isr, 0, DRIVER_NAME, spi_imx);
  687. if (ret) {
  688. dev_err(&pdev->dev, "can't get irq%d: %d\n", spi_imx->irq, ret);
  689. goto out_iounmap;
  690. }
  691. spi_imx->clk = clk_get(&pdev->dev, NULL);
  692. if (IS_ERR(spi_imx->clk)) {
  693. dev_err(&pdev->dev, "unable to get clock\n");
  694. ret = PTR_ERR(spi_imx->clk);
  695. goto out_free_irq;
  696. }
  697. clk_enable(spi_imx->clk);
  698. spi_imx->spi_clk = clk_get_rate(spi_imx->clk);
  699. spi_imx->devtype_data->reset(spi_imx);
  700. spi_imx->devtype_data->intctrl(spi_imx, 0);
  701. master->dev.of_node = pdev->dev.of_node;
  702. ret = spi_bitbang_start(&spi_imx->bitbang);
  703. if (ret) {
  704. dev_err(&pdev->dev, "bitbang start failed with %d\n", ret);
  705. goto out_clk_put;
  706. }
  707. dev_info(&pdev->dev, "probed\n");
  708. return ret;
  709. out_clk_put:
  710. clk_disable(spi_imx->clk);
  711. clk_put(spi_imx->clk);
  712. out_free_irq:
  713. free_irq(spi_imx->irq, spi_imx);
  714. out_iounmap:
  715. iounmap(spi_imx->base);
  716. out_release_mem:
  717. release_mem_region(res->start, resource_size(res));
  718. out_gpio_free:
  719. while (--i >= 0) {
  720. if (spi_imx->chipselect[i] >= 0)
  721. gpio_free(spi_imx->chipselect[i]);
  722. }
  723. spi_master_put(master);
  724. kfree(master);
  725. platform_set_drvdata(pdev, NULL);
  726. return ret;
  727. }
  728. static int __devexit spi_imx_remove(struct platform_device *pdev)
  729. {
  730. struct spi_master *master = platform_get_drvdata(pdev);
  731. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  732. struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
  733. int i;
  734. spi_bitbang_stop(&spi_imx->bitbang);
  735. writel(0, spi_imx->base + MXC_CSPICTRL);
  736. clk_disable(spi_imx->clk);
  737. clk_put(spi_imx->clk);
  738. free_irq(spi_imx->irq, spi_imx);
  739. iounmap(spi_imx->base);
  740. for (i = 0; i < master->num_chipselect; i++)
  741. if (spi_imx->chipselect[i] >= 0)
  742. gpio_free(spi_imx->chipselect[i]);
  743. spi_master_put(master);
  744. release_mem_region(res->start, resource_size(res));
  745. platform_set_drvdata(pdev, NULL);
  746. return 0;
  747. }
  748. static struct platform_driver spi_imx_driver = {
  749. .driver = {
  750. .name = DRIVER_NAME,
  751. .owner = THIS_MODULE,
  752. .of_match_table = spi_imx_dt_ids,
  753. },
  754. .id_table = spi_imx_devtype,
  755. .probe = spi_imx_probe,
  756. .remove = __devexit_p(spi_imx_remove),
  757. };
  758. module_platform_driver(spi_imx_driver);
  759. MODULE_DESCRIPTION("SPI Master Controller driver");
  760. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  761. MODULE_LICENSE("GPL");