spi-imx.c 39 KB

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