spi_imx.c 23 KB

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