spi-lantiq-ssc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /*
  2. * Copyright (C) 2011-2015 Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
  3. * Copyright (C) 2016 Hauke Mehrtens <hauke@hauke-m.de>
  4. *
  5. * This program is free software; you can distribute it and/or modify it
  6. * under the terms of the GNU General Public License (Version 2) as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/of_device.h>
  12. #include <linux/clk.h>
  13. #include <linux/io.h>
  14. #include <linux/delay.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/sched.h>
  17. #include <linux/completion.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/err.h>
  20. #include <linux/gpio.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/spi/spi.h>
  23. #ifdef CONFIG_LANTIQ
  24. #include <lantiq_soc.h>
  25. #endif
  26. #define LTQ_SPI_RX_IRQ_NAME "spi_rx"
  27. #define LTQ_SPI_TX_IRQ_NAME "spi_tx"
  28. #define LTQ_SPI_ERR_IRQ_NAME "spi_err"
  29. #define LTQ_SPI_FRM_IRQ_NAME "spi_frm"
  30. #define LTQ_SPI_CLC 0x00
  31. #define LTQ_SPI_PISEL 0x04
  32. #define LTQ_SPI_ID 0x08
  33. #define LTQ_SPI_CON 0x10
  34. #define LTQ_SPI_STAT 0x14
  35. #define LTQ_SPI_WHBSTATE 0x18
  36. #define LTQ_SPI_TB 0x20
  37. #define LTQ_SPI_RB 0x24
  38. #define LTQ_SPI_RXFCON 0x30
  39. #define LTQ_SPI_TXFCON 0x34
  40. #define LTQ_SPI_FSTAT 0x38
  41. #define LTQ_SPI_BRT 0x40
  42. #define LTQ_SPI_BRSTAT 0x44
  43. #define LTQ_SPI_SFCON 0x60
  44. #define LTQ_SPI_SFSTAT 0x64
  45. #define LTQ_SPI_GPOCON 0x70
  46. #define LTQ_SPI_GPOSTAT 0x74
  47. #define LTQ_SPI_FPGO 0x78
  48. #define LTQ_SPI_RXREQ 0x80
  49. #define LTQ_SPI_RXCNT 0x84
  50. #define LTQ_SPI_DMACON 0xec
  51. #define LTQ_SPI_IRNEN 0xf4
  52. #define LTQ_SPI_IRNICR 0xf8
  53. #define LTQ_SPI_IRNCR 0xfc
  54. #define LTQ_SPI_CLC_SMC_S 16 /* Clock divider for sleep mode */
  55. #define LTQ_SPI_CLC_SMC_M (0xFF << LTQ_SPI_CLC_SMC_S)
  56. #define LTQ_SPI_CLC_RMC_S 8 /* Clock divider for normal run mode */
  57. #define LTQ_SPI_CLC_RMC_M (0xFF << LTQ_SPI_CLC_RMC_S)
  58. #define LTQ_SPI_CLC_DISS BIT(1) /* Disable status bit */
  59. #define LTQ_SPI_CLC_DISR BIT(0) /* Disable request bit */
  60. #define LTQ_SPI_ID_TXFS_S 24 /* Implemented TX FIFO size */
  61. #define LTQ_SPI_ID_TXFS_M (0x3F << LTQ_SPI_ID_TXFS_S)
  62. #define LTQ_SPI_ID_RXFS_S 16 /* Implemented RX FIFO size */
  63. #define LTQ_SPI_ID_RXFS_M (0x3F << LTQ_SPI_ID_RXFS_S)
  64. #define LTQ_SPI_ID_MOD_S 8 /* Module ID */
  65. #define LTQ_SPI_ID_MOD_M (0xff << LTQ_SPI_ID_MOD_S)
  66. #define LTQ_SPI_ID_CFG_S 5 /* DMA interface support */
  67. #define LTQ_SPI_ID_CFG_M (1 << LTQ_SPI_ID_CFG_S)
  68. #define LTQ_SPI_ID_REV_M 0x1F /* Hardware revision number */
  69. #define LTQ_SPI_CON_BM_S 16 /* Data width selection */
  70. #define LTQ_SPI_CON_BM_M (0x1F << LTQ_SPI_CON_BM_S)
  71. #define LTQ_SPI_CON_EM BIT(24) /* Echo mode */
  72. #define LTQ_SPI_CON_IDLE BIT(23) /* Idle bit value */
  73. #define LTQ_SPI_CON_ENBV BIT(22) /* Enable byte valid control */
  74. #define LTQ_SPI_CON_RUEN BIT(12) /* Receive underflow error enable */
  75. #define LTQ_SPI_CON_TUEN BIT(11) /* Transmit underflow error enable */
  76. #define LTQ_SPI_CON_AEN BIT(10) /* Abort error enable */
  77. #define LTQ_SPI_CON_REN BIT(9) /* Receive overflow error enable */
  78. #define LTQ_SPI_CON_TEN BIT(8) /* Transmit overflow error enable */
  79. #define LTQ_SPI_CON_LB BIT(7) /* Loopback control */
  80. #define LTQ_SPI_CON_PO BIT(6) /* Clock polarity control */
  81. #define LTQ_SPI_CON_PH BIT(5) /* Clock phase control */
  82. #define LTQ_SPI_CON_HB BIT(4) /* Heading control */
  83. #define LTQ_SPI_CON_RXOFF BIT(1) /* Switch receiver off */
  84. #define LTQ_SPI_CON_TXOFF BIT(0) /* Switch transmitter off */
  85. #define LTQ_SPI_STAT_RXBV_S 28
  86. #define LTQ_SPI_STAT_RXBV_M (0x7 << LTQ_SPI_STAT_RXBV_S)
  87. #define LTQ_SPI_STAT_BSY BIT(13) /* Busy flag */
  88. #define LTQ_SPI_STAT_RUE BIT(12) /* Receive underflow error flag */
  89. #define LTQ_SPI_STAT_TUE BIT(11) /* Transmit underflow error flag */
  90. #define LTQ_SPI_STAT_AE BIT(10) /* Abort error flag */
  91. #define LTQ_SPI_STAT_RE BIT(9) /* Receive error flag */
  92. #define LTQ_SPI_STAT_TE BIT(8) /* Transmit error flag */
  93. #define LTQ_SPI_STAT_ME BIT(7) /* Mode error flag */
  94. #define LTQ_SPI_STAT_MS BIT(1) /* Master/slave select bit */
  95. #define LTQ_SPI_STAT_EN BIT(0) /* Enable bit */
  96. #define LTQ_SPI_STAT_ERRORS (LTQ_SPI_STAT_ME | LTQ_SPI_STAT_TE | \
  97. LTQ_SPI_STAT_RE | LTQ_SPI_STAT_AE | \
  98. LTQ_SPI_STAT_TUE | LTQ_SPI_STAT_RUE)
  99. #define LTQ_SPI_WHBSTATE_SETTUE BIT(15) /* Set transmit underflow error flag */
  100. #define LTQ_SPI_WHBSTATE_SETAE BIT(14) /* Set abort error flag */
  101. #define LTQ_SPI_WHBSTATE_SETRE BIT(13) /* Set receive error flag */
  102. #define LTQ_SPI_WHBSTATE_SETTE BIT(12) /* Set transmit error flag */
  103. #define LTQ_SPI_WHBSTATE_CLRTUE BIT(11) /* Clear transmit underflow error flag */
  104. #define LTQ_SPI_WHBSTATE_CLRAE BIT(10) /* Clear abort error flag */
  105. #define LTQ_SPI_WHBSTATE_CLRRE BIT(9) /* Clear receive error flag */
  106. #define LTQ_SPI_WHBSTATE_CLRTE BIT(8) /* Clear transmit error flag */
  107. #define LTQ_SPI_WHBSTATE_SETME BIT(7) /* Set mode error flag */
  108. #define LTQ_SPI_WHBSTATE_CLRME BIT(6) /* Clear mode error flag */
  109. #define LTQ_SPI_WHBSTATE_SETRUE BIT(5) /* Set receive underflow error flag */
  110. #define LTQ_SPI_WHBSTATE_CLRRUE BIT(4) /* Clear receive underflow error flag */
  111. #define LTQ_SPI_WHBSTATE_SETMS BIT(3) /* Set master select bit */
  112. #define LTQ_SPI_WHBSTATE_CLRMS BIT(2) /* Clear master select bit */
  113. #define LTQ_SPI_WHBSTATE_SETEN BIT(1) /* Set enable bit (operational mode) */
  114. #define LTQ_SPI_WHBSTATE_CLREN BIT(0) /* Clear enable bit (config mode */
  115. #define LTQ_SPI_WHBSTATE_CLR_ERRORS (LTQ_SPI_WHBSTATE_CLRRUE | \
  116. LTQ_SPI_WHBSTATE_CLRME | \
  117. LTQ_SPI_WHBSTATE_CLRTE | \
  118. LTQ_SPI_WHBSTATE_CLRRE | \
  119. LTQ_SPI_WHBSTATE_CLRAE | \
  120. LTQ_SPI_WHBSTATE_CLRTUE)
  121. #define LTQ_SPI_RXFCON_RXFITL_S 8 /* FIFO interrupt trigger level */
  122. #define LTQ_SPI_RXFCON_RXFITL_M (0x3F << LTQ_SPI_RXFCON_RXFITL_S)
  123. #define LTQ_SPI_RXFCON_RXFLU BIT(1) /* FIFO flush */
  124. #define LTQ_SPI_RXFCON_RXFEN BIT(0) /* FIFO enable */
  125. #define LTQ_SPI_TXFCON_TXFITL_S 8 /* FIFO interrupt trigger level */
  126. #define LTQ_SPI_TXFCON_TXFITL_M (0x3F << LTQ_SPI_TXFCON_TXFITL_S)
  127. #define LTQ_SPI_TXFCON_TXFLU BIT(1) /* FIFO flush */
  128. #define LTQ_SPI_TXFCON_TXFEN BIT(0) /* FIFO enable */
  129. #define LTQ_SPI_FSTAT_RXFFL_S 0
  130. #define LTQ_SPI_FSTAT_RXFFL_M (0x3f << LTQ_SPI_FSTAT_RXFFL_S)
  131. #define LTQ_SPI_FSTAT_TXFFL_S 8
  132. #define LTQ_SPI_FSTAT_TXFFL_M (0x3f << LTQ_SPI_FSTAT_TXFFL_S)
  133. #define LTQ_SPI_GPOCON_ISCSBN_S 8
  134. #define LTQ_SPI_GPOCON_INVOUTN_S 0
  135. #define LTQ_SPI_FGPO_SETOUTN_S 8
  136. #define LTQ_SPI_FGPO_CLROUTN_S 0
  137. #define LTQ_SPI_RXREQ_RXCNT_M 0xFFFF /* Receive count value */
  138. #define LTQ_SPI_RXCNT_TODO_M 0xFFFF /* Recevie to-do value */
  139. #define LTQ_SPI_IRNEN_TFI BIT(4) /* TX finished interrupt */
  140. #define LTQ_SPI_IRNEN_F BIT(3) /* Frame end interrupt request */
  141. #define LTQ_SPI_IRNEN_E BIT(2) /* Error end interrupt request */
  142. #define LTQ_SPI_IRNEN_T_XWAY BIT(1) /* Transmit end interrupt request */
  143. #define LTQ_SPI_IRNEN_R_XWAY BIT(0) /* Receive end interrupt request */
  144. #define LTQ_SPI_IRNEN_R_XRX BIT(1) /* Transmit end interrupt request */
  145. #define LTQ_SPI_IRNEN_T_XRX BIT(0) /* Receive end interrupt request */
  146. #define LTQ_SPI_IRNEN_ALL 0x1F
  147. struct lantiq_ssc_hwcfg {
  148. unsigned int irnen_r;
  149. unsigned int irnen_t;
  150. };
  151. struct lantiq_ssc_spi {
  152. struct spi_master *master;
  153. struct device *dev;
  154. void __iomem *regbase;
  155. struct clk *spi_clk;
  156. struct clk *fpi_clk;
  157. const struct lantiq_ssc_hwcfg *hwcfg;
  158. spinlock_t lock;
  159. struct workqueue_struct *wq;
  160. struct work_struct work;
  161. const u8 *tx;
  162. u8 *rx;
  163. unsigned int tx_todo;
  164. unsigned int rx_todo;
  165. unsigned int bits_per_word;
  166. unsigned int speed_hz;
  167. unsigned int tx_fifo_size;
  168. unsigned int rx_fifo_size;
  169. unsigned int base_cs;
  170. unsigned int fdx_tx_level;
  171. };
  172. static u32 lantiq_ssc_readl(const struct lantiq_ssc_spi *spi, u32 reg)
  173. {
  174. return __raw_readl(spi->regbase + reg);
  175. }
  176. static void lantiq_ssc_writel(const struct lantiq_ssc_spi *spi, u32 val,
  177. u32 reg)
  178. {
  179. __raw_writel(val, spi->regbase + reg);
  180. }
  181. static void lantiq_ssc_maskl(const struct lantiq_ssc_spi *spi, u32 clr,
  182. u32 set, u32 reg)
  183. {
  184. u32 val = __raw_readl(spi->regbase + reg);
  185. val &= ~clr;
  186. val |= set;
  187. __raw_writel(val, spi->regbase + reg);
  188. }
  189. static unsigned int tx_fifo_level(const struct lantiq_ssc_spi *spi)
  190. {
  191. u32 fstat = lantiq_ssc_readl(spi, LTQ_SPI_FSTAT);
  192. return (fstat & LTQ_SPI_FSTAT_TXFFL_M) >> LTQ_SPI_FSTAT_TXFFL_S;
  193. }
  194. static unsigned int rx_fifo_level(const struct lantiq_ssc_spi *spi)
  195. {
  196. u32 fstat = lantiq_ssc_readl(spi, LTQ_SPI_FSTAT);
  197. return fstat & LTQ_SPI_FSTAT_RXFFL_M;
  198. }
  199. static unsigned int tx_fifo_free(const struct lantiq_ssc_spi *spi)
  200. {
  201. return spi->tx_fifo_size - tx_fifo_level(spi);
  202. }
  203. static void rx_fifo_reset(const struct lantiq_ssc_spi *spi)
  204. {
  205. u32 val = spi->rx_fifo_size << LTQ_SPI_RXFCON_RXFITL_S;
  206. val |= LTQ_SPI_RXFCON_RXFEN | LTQ_SPI_RXFCON_RXFLU;
  207. lantiq_ssc_writel(spi, val, LTQ_SPI_RXFCON);
  208. }
  209. static void tx_fifo_reset(const struct lantiq_ssc_spi *spi)
  210. {
  211. u32 val = 1 << LTQ_SPI_TXFCON_TXFITL_S;
  212. val |= LTQ_SPI_TXFCON_TXFEN | LTQ_SPI_TXFCON_TXFLU;
  213. lantiq_ssc_writel(spi, val, LTQ_SPI_TXFCON);
  214. }
  215. static void rx_fifo_flush(const struct lantiq_ssc_spi *spi)
  216. {
  217. lantiq_ssc_maskl(spi, 0, LTQ_SPI_RXFCON_RXFLU, LTQ_SPI_RXFCON);
  218. }
  219. static void tx_fifo_flush(const struct lantiq_ssc_spi *spi)
  220. {
  221. lantiq_ssc_maskl(spi, 0, LTQ_SPI_TXFCON_TXFLU, LTQ_SPI_TXFCON);
  222. }
  223. static void hw_enter_config_mode(const struct lantiq_ssc_spi *spi)
  224. {
  225. lantiq_ssc_writel(spi, LTQ_SPI_WHBSTATE_CLREN, LTQ_SPI_WHBSTATE);
  226. }
  227. static void hw_enter_active_mode(const struct lantiq_ssc_spi *spi)
  228. {
  229. lantiq_ssc_writel(spi, LTQ_SPI_WHBSTATE_SETEN, LTQ_SPI_WHBSTATE);
  230. }
  231. static void hw_setup_speed_hz(const struct lantiq_ssc_spi *spi,
  232. unsigned int max_speed_hz)
  233. {
  234. u32 spi_clk, brt;
  235. /*
  236. * SPI module clock is derived from FPI bus clock dependent on
  237. * divider value in CLC.RMS which is always set to 1.
  238. *
  239. * f_SPI
  240. * baudrate = --------------
  241. * 2 * (BR + 1)
  242. */
  243. spi_clk = clk_get_rate(spi->fpi_clk) / 2;
  244. if (max_speed_hz > spi_clk)
  245. brt = 0;
  246. else
  247. brt = spi_clk / max_speed_hz - 1;
  248. if (brt > 0xFFFF)
  249. brt = 0xFFFF;
  250. dev_dbg(spi->dev, "spi_clk %u, max_speed_hz %u, brt %u\n",
  251. spi_clk, max_speed_hz, brt);
  252. lantiq_ssc_writel(spi, brt, LTQ_SPI_BRT);
  253. }
  254. static void hw_setup_bits_per_word(const struct lantiq_ssc_spi *spi,
  255. unsigned int bits_per_word)
  256. {
  257. u32 bm;
  258. /* CON.BM value = bits_per_word - 1 */
  259. bm = (bits_per_word - 1) << LTQ_SPI_CON_BM_S;
  260. lantiq_ssc_maskl(spi, LTQ_SPI_CON_BM_M, bm, LTQ_SPI_CON);
  261. }
  262. static void hw_setup_clock_mode(const struct lantiq_ssc_spi *spi,
  263. unsigned int mode)
  264. {
  265. u32 con_set = 0, con_clr = 0;
  266. /*
  267. * SPI mode mapping in CON register:
  268. * Mode CPOL CPHA CON.PO CON.PH
  269. * 0 0 0 0 1
  270. * 1 0 1 0 0
  271. * 2 1 0 1 1
  272. * 3 1 1 1 0
  273. */
  274. if (mode & SPI_CPHA)
  275. con_clr |= LTQ_SPI_CON_PH;
  276. else
  277. con_set |= LTQ_SPI_CON_PH;
  278. if (mode & SPI_CPOL)
  279. con_set |= LTQ_SPI_CON_PO | LTQ_SPI_CON_IDLE;
  280. else
  281. con_clr |= LTQ_SPI_CON_PO | LTQ_SPI_CON_IDLE;
  282. /* Set heading control */
  283. if (mode & SPI_LSB_FIRST)
  284. con_clr |= LTQ_SPI_CON_HB;
  285. else
  286. con_set |= LTQ_SPI_CON_HB;
  287. /* Set loopback mode */
  288. if (mode & SPI_LOOP)
  289. con_set |= LTQ_SPI_CON_LB;
  290. else
  291. con_clr |= LTQ_SPI_CON_LB;
  292. lantiq_ssc_maskl(spi, con_clr, con_set, LTQ_SPI_CON);
  293. }
  294. static void lantiq_ssc_hw_init(const struct lantiq_ssc_spi *spi)
  295. {
  296. const struct lantiq_ssc_hwcfg *hwcfg = spi->hwcfg;
  297. /*
  298. * Set clock divider for run mode to 1 to
  299. * run at same frequency as FPI bus
  300. */
  301. lantiq_ssc_writel(spi, 1 << LTQ_SPI_CLC_RMC_S, LTQ_SPI_CLC);
  302. /* Put controller into config mode */
  303. hw_enter_config_mode(spi);
  304. /* Clear error flags */
  305. lantiq_ssc_maskl(spi, 0, LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
  306. /* Enable error checking, disable TX/RX */
  307. lantiq_ssc_writel(spi, LTQ_SPI_CON_RUEN | LTQ_SPI_CON_AEN |
  308. LTQ_SPI_CON_TEN | LTQ_SPI_CON_REN | LTQ_SPI_CON_TXOFF |
  309. LTQ_SPI_CON_RXOFF, LTQ_SPI_CON);
  310. /* Setup default SPI mode */
  311. hw_setup_bits_per_word(spi, spi->bits_per_word);
  312. hw_setup_clock_mode(spi, SPI_MODE_0);
  313. /* Enable master mode and clear error flags */
  314. lantiq_ssc_writel(spi, LTQ_SPI_WHBSTATE_SETMS |
  315. LTQ_SPI_WHBSTATE_CLR_ERRORS,
  316. LTQ_SPI_WHBSTATE);
  317. /* Reset GPIO/CS registers */
  318. lantiq_ssc_writel(spi, 0, LTQ_SPI_GPOCON);
  319. lantiq_ssc_writel(spi, 0xFF00, LTQ_SPI_FPGO);
  320. /* Enable and flush FIFOs */
  321. rx_fifo_reset(spi);
  322. tx_fifo_reset(spi);
  323. /* Enable interrupts */
  324. lantiq_ssc_writel(spi, hwcfg->irnen_t | hwcfg->irnen_r |
  325. LTQ_SPI_IRNEN_E, LTQ_SPI_IRNEN);
  326. }
  327. static int lantiq_ssc_setup(struct spi_device *spidev)
  328. {
  329. struct spi_master *master = spidev->master;
  330. struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
  331. unsigned int cs = spidev->chip_select;
  332. u32 gpocon;
  333. /* GPIOs are used for CS */
  334. if (gpio_is_valid(spidev->cs_gpio))
  335. return 0;
  336. dev_dbg(spi->dev, "using internal chipselect %u\n", cs);
  337. if (cs < spi->base_cs) {
  338. dev_err(spi->dev,
  339. "chipselect %i too small (min %i)\n", cs, spi->base_cs);
  340. return -EINVAL;
  341. }
  342. /* set GPO pin to CS mode */
  343. gpocon = 1 << ((cs - spi->base_cs) + LTQ_SPI_GPOCON_ISCSBN_S);
  344. /* invert GPO pin */
  345. if (spidev->mode & SPI_CS_HIGH)
  346. gpocon |= 1 << (cs - spi->base_cs);
  347. lantiq_ssc_maskl(spi, 0, gpocon, LTQ_SPI_GPOCON);
  348. return 0;
  349. }
  350. static int lantiq_ssc_prepare_message(struct spi_master *master,
  351. struct spi_message *message)
  352. {
  353. struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
  354. hw_enter_config_mode(spi);
  355. hw_setup_clock_mode(spi, message->spi->mode);
  356. hw_enter_active_mode(spi);
  357. return 0;
  358. }
  359. static void hw_setup_transfer(struct lantiq_ssc_spi *spi,
  360. struct spi_device *spidev, struct spi_transfer *t)
  361. {
  362. unsigned int speed_hz = t->speed_hz;
  363. unsigned int bits_per_word = t->bits_per_word;
  364. u32 con;
  365. if (bits_per_word != spi->bits_per_word ||
  366. speed_hz != spi->speed_hz) {
  367. hw_enter_config_mode(spi);
  368. hw_setup_speed_hz(spi, speed_hz);
  369. hw_setup_bits_per_word(spi, bits_per_word);
  370. hw_enter_active_mode(spi);
  371. spi->speed_hz = speed_hz;
  372. spi->bits_per_word = bits_per_word;
  373. }
  374. /* Configure transmitter and receiver */
  375. con = lantiq_ssc_readl(spi, LTQ_SPI_CON);
  376. if (t->tx_buf)
  377. con &= ~LTQ_SPI_CON_TXOFF;
  378. else
  379. con |= LTQ_SPI_CON_TXOFF;
  380. if (t->rx_buf)
  381. con &= ~LTQ_SPI_CON_RXOFF;
  382. else
  383. con |= LTQ_SPI_CON_RXOFF;
  384. lantiq_ssc_writel(spi, con, LTQ_SPI_CON);
  385. }
  386. static int lantiq_ssc_unprepare_message(struct spi_master *master,
  387. struct spi_message *message)
  388. {
  389. struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
  390. flush_workqueue(spi->wq);
  391. /* Disable transmitter and receiver while idle */
  392. lantiq_ssc_maskl(spi, 0, LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF,
  393. LTQ_SPI_CON);
  394. return 0;
  395. }
  396. static void tx_fifo_write(struct lantiq_ssc_spi *spi)
  397. {
  398. const u8 *tx8;
  399. const u16 *tx16;
  400. const u32 *tx32;
  401. u32 data;
  402. unsigned int tx_free = tx_fifo_free(spi);
  403. spi->fdx_tx_level = 0;
  404. while (spi->tx_todo && tx_free) {
  405. switch (spi->bits_per_word) {
  406. case 2 ... 8:
  407. tx8 = spi->tx;
  408. data = *tx8;
  409. spi->tx_todo--;
  410. spi->tx++;
  411. break;
  412. case 16:
  413. tx16 = (u16 *) spi->tx;
  414. data = *tx16;
  415. spi->tx_todo -= 2;
  416. spi->tx += 2;
  417. break;
  418. case 32:
  419. tx32 = (u32 *) spi->tx;
  420. data = *tx32;
  421. spi->tx_todo -= 4;
  422. spi->tx += 4;
  423. break;
  424. default:
  425. WARN_ON(1);
  426. data = 0;
  427. break;
  428. }
  429. lantiq_ssc_writel(spi, data, LTQ_SPI_TB);
  430. tx_free--;
  431. spi->fdx_tx_level++;
  432. }
  433. }
  434. static void rx_fifo_read_full_duplex(struct lantiq_ssc_spi *spi)
  435. {
  436. u8 *rx8;
  437. u16 *rx16;
  438. u32 *rx32;
  439. u32 data;
  440. unsigned int rx_fill = rx_fifo_level(spi);
  441. /*
  442. * Wait until all expected data to be shifted in.
  443. * Otherwise, rx overrun may occur.
  444. */
  445. while (rx_fill != spi->fdx_tx_level)
  446. rx_fill = rx_fifo_level(spi);
  447. while (rx_fill) {
  448. data = lantiq_ssc_readl(spi, LTQ_SPI_RB);
  449. switch (spi->bits_per_word) {
  450. case 2 ... 8:
  451. rx8 = spi->rx;
  452. *rx8 = data;
  453. spi->rx_todo--;
  454. spi->rx++;
  455. break;
  456. case 16:
  457. rx16 = (u16 *) spi->rx;
  458. *rx16 = data;
  459. spi->rx_todo -= 2;
  460. spi->rx += 2;
  461. break;
  462. case 32:
  463. rx32 = (u32 *) spi->rx;
  464. *rx32 = data;
  465. spi->rx_todo -= 4;
  466. spi->rx += 4;
  467. break;
  468. default:
  469. WARN_ON(1);
  470. break;
  471. }
  472. rx_fill--;
  473. }
  474. }
  475. static void rx_fifo_read_half_duplex(struct lantiq_ssc_spi *spi)
  476. {
  477. u32 data, *rx32;
  478. u8 *rx8;
  479. unsigned int rxbv, shift;
  480. unsigned int rx_fill = rx_fifo_level(spi);
  481. /*
  482. * In RX-only mode the bits per word value is ignored by HW. A value
  483. * of 32 is used instead. Thus all 4 bytes per FIFO must be read.
  484. * If remaining RX bytes are less than 4, the FIFO must be read
  485. * differently. The amount of received and valid bytes is indicated
  486. * by STAT.RXBV register value.
  487. */
  488. while (rx_fill) {
  489. if (spi->rx_todo < 4) {
  490. rxbv = (lantiq_ssc_readl(spi, LTQ_SPI_STAT) &
  491. LTQ_SPI_STAT_RXBV_M) >> LTQ_SPI_STAT_RXBV_S;
  492. data = lantiq_ssc_readl(spi, LTQ_SPI_RB);
  493. shift = (rxbv - 1) * 8;
  494. rx8 = spi->rx;
  495. while (rxbv) {
  496. *rx8++ = (data >> shift) & 0xFF;
  497. rxbv--;
  498. shift -= 8;
  499. spi->rx_todo--;
  500. spi->rx++;
  501. }
  502. } else {
  503. data = lantiq_ssc_readl(spi, LTQ_SPI_RB);
  504. rx32 = (u32 *) spi->rx;
  505. *rx32++ = data;
  506. spi->rx_todo -= 4;
  507. spi->rx += 4;
  508. }
  509. rx_fill--;
  510. }
  511. }
  512. static void rx_request(struct lantiq_ssc_spi *spi)
  513. {
  514. unsigned int rxreq, rxreq_max;
  515. /*
  516. * To avoid receive overflows at high clocks it is better to request
  517. * only the amount of bytes that fits into all FIFOs. This value
  518. * depends on the FIFO size implemented in hardware.
  519. */
  520. rxreq = spi->rx_todo;
  521. rxreq_max = spi->rx_fifo_size * 4;
  522. if (rxreq > rxreq_max)
  523. rxreq = rxreq_max;
  524. lantiq_ssc_writel(spi, rxreq, LTQ_SPI_RXREQ);
  525. }
  526. static irqreturn_t lantiq_ssc_xmit_interrupt(int irq, void *data)
  527. {
  528. struct lantiq_ssc_spi *spi = data;
  529. if (spi->tx) {
  530. if (spi->rx && spi->rx_todo)
  531. rx_fifo_read_full_duplex(spi);
  532. if (spi->tx_todo)
  533. tx_fifo_write(spi);
  534. else if (!tx_fifo_level(spi))
  535. goto completed;
  536. } else if (spi->rx) {
  537. if (spi->rx_todo) {
  538. rx_fifo_read_half_duplex(spi);
  539. if (spi->rx_todo)
  540. rx_request(spi);
  541. else
  542. goto completed;
  543. } else {
  544. goto completed;
  545. }
  546. }
  547. return IRQ_HANDLED;
  548. completed:
  549. queue_work(spi->wq, &spi->work);
  550. return IRQ_HANDLED;
  551. }
  552. static irqreturn_t lantiq_ssc_err_interrupt(int irq, void *data)
  553. {
  554. struct lantiq_ssc_spi *spi = data;
  555. u32 stat = lantiq_ssc_readl(spi, LTQ_SPI_STAT);
  556. if (!(stat & LTQ_SPI_STAT_ERRORS))
  557. return IRQ_NONE;
  558. if (stat & LTQ_SPI_STAT_RUE)
  559. dev_err(spi->dev, "receive underflow error\n");
  560. if (stat & LTQ_SPI_STAT_TUE)
  561. dev_err(spi->dev, "transmit underflow error\n");
  562. if (stat & LTQ_SPI_STAT_AE)
  563. dev_err(spi->dev, "abort error\n");
  564. if (stat & LTQ_SPI_STAT_RE)
  565. dev_err(spi->dev, "receive overflow error\n");
  566. if (stat & LTQ_SPI_STAT_TE)
  567. dev_err(spi->dev, "transmit overflow error\n");
  568. if (stat & LTQ_SPI_STAT_ME)
  569. dev_err(spi->dev, "mode error\n");
  570. /* Clear error flags */
  571. lantiq_ssc_maskl(spi, 0, LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
  572. /* set bad status so it can be retried */
  573. if (spi->master->cur_msg)
  574. spi->master->cur_msg->status = -EIO;
  575. queue_work(spi->wq, &spi->work);
  576. return IRQ_HANDLED;
  577. }
  578. static int transfer_start(struct lantiq_ssc_spi *spi, struct spi_device *spidev,
  579. struct spi_transfer *t)
  580. {
  581. unsigned long flags;
  582. spin_lock_irqsave(&spi->lock, flags);
  583. spi->tx = t->tx_buf;
  584. spi->rx = t->rx_buf;
  585. if (t->tx_buf) {
  586. spi->tx_todo = t->len;
  587. /* initially fill TX FIFO */
  588. tx_fifo_write(spi);
  589. }
  590. if (spi->rx) {
  591. spi->rx_todo = t->len;
  592. /* start shift clock in RX-only mode */
  593. if (!spi->tx)
  594. rx_request(spi);
  595. }
  596. spin_unlock_irqrestore(&spi->lock, flags);
  597. return t->len;
  598. }
  599. /*
  600. * The driver only gets an interrupt when the FIFO is empty, but there
  601. * is an additional shift register from which the data is written to
  602. * the wire. We get the last interrupt when the controller starts to
  603. * write the last word to the wire, not when it is finished. Do busy
  604. * waiting till it finishes.
  605. */
  606. static void lantiq_ssc_bussy_work(struct work_struct *work)
  607. {
  608. struct lantiq_ssc_spi *spi;
  609. unsigned long long timeout = 8LL * 1000LL;
  610. unsigned long end;
  611. spi = container_of(work, typeof(*spi), work);
  612. do_div(timeout, spi->speed_hz);
  613. timeout += timeout + 100; /* some tolerance */
  614. end = jiffies + msecs_to_jiffies(timeout);
  615. do {
  616. u32 stat = lantiq_ssc_readl(spi, LTQ_SPI_STAT);
  617. if (!(stat & LTQ_SPI_STAT_BSY)) {
  618. spi_finalize_current_transfer(spi->master);
  619. return;
  620. }
  621. cond_resched();
  622. } while (!time_after_eq(jiffies, end));
  623. if (spi->master->cur_msg)
  624. spi->master->cur_msg->status = -EIO;
  625. spi_finalize_current_transfer(spi->master);
  626. }
  627. static void lantiq_ssc_handle_err(struct spi_master *master,
  628. struct spi_message *message)
  629. {
  630. struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
  631. /* flush FIFOs on timeout */
  632. rx_fifo_flush(spi);
  633. tx_fifo_flush(spi);
  634. }
  635. static void lantiq_ssc_set_cs(struct spi_device *spidev, bool enable)
  636. {
  637. struct lantiq_ssc_spi *spi = spi_master_get_devdata(spidev->master);
  638. unsigned int cs = spidev->chip_select;
  639. u32 fgpo;
  640. if (!!(spidev->mode & SPI_CS_HIGH) == enable)
  641. fgpo = (1 << (cs - spi->base_cs));
  642. else
  643. fgpo = (1 << (cs - spi->base_cs + LTQ_SPI_FGPO_SETOUTN_S));
  644. lantiq_ssc_writel(spi, fgpo, LTQ_SPI_FPGO);
  645. }
  646. static int lantiq_ssc_transfer_one(struct spi_master *master,
  647. struct spi_device *spidev,
  648. struct spi_transfer *t)
  649. {
  650. struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
  651. hw_setup_transfer(spi, spidev, t);
  652. return transfer_start(spi, spidev, t);
  653. }
  654. static const struct lantiq_ssc_hwcfg lantiq_ssc_xway = {
  655. .irnen_r = LTQ_SPI_IRNEN_R_XWAY,
  656. .irnen_t = LTQ_SPI_IRNEN_T_XWAY,
  657. };
  658. static const struct lantiq_ssc_hwcfg lantiq_ssc_xrx = {
  659. .irnen_r = LTQ_SPI_IRNEN_R_XRX,
  660. .irnen_t = LTQ_SPI_IRNEN_T_XRX,
  661. };
  662. static const struct of_device_id lantiq_ssc_match[] = {
  663. { .compatible = "lantiq,ase-spi", .data = &lantiq_ssc_xway, },
  664. { .compatible = "lantiq,falcon-spi", .data = &lantiq_ssc_xrx, },
  665. { .compatible = "lantiq,xrx100-spi", .data = &lantiq_ssc_xrx, },
  666. {},
  667. };
  668. MODULE_DEVICE_TABLE(of, lantiq_ssc_match);
  669. static int lantiq_ssc_probe(struct platform_device *pdev)
  670. {
  671. struct device *dev = &pdev->dev;
  672. struct spi_master *master;
  673. struct resource *res;
  674. struct lantiq_ssc_spi *spi;
  675. const struct lantiq_ssc_hwcfg *hwcfg;
  676. const struct of_device_id *match;
  677. int err, rx_irq, tx_irq, err_irq;
  678. u32 id, supports_dma, revision;
  679. unsigned int num_cs;
  680. match = of_match_device(lantiq_ssc_match, dev);
  681. if (!match) {
  682. dev_err(dev, "no device match\n");
  683. return -EINVAL;
  684. }
  685. hwcfg = match->data;
  686. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  687. if (!res) {
  688. dev_err(dev, "failed to get resources\n");
  689. return -ENXIO;
  690. }
  691. rx_irq = platform_get_irq_byname(pdev, LTQ_SPI_RX_IRQ_NAME);
  692. if (rx_irq < 0) {
  693. dev_err(dev, "failed to get %s\n", LTQ_SPI_RX_IRQ_NAME);
  694. return -ENXIO;
  695. }
  696. tx_irq = platform_get_irq_byname(pdev, LTQ_SPI_TX_IRQ_NAME);
  697. if (tx_irq < 0) {
  698. dev_err(dev, "failed to get %s\n", LTQ_SPI_TX_IRQ_NAME);
  699. return -ENXIO;
  700. }
  701. err_irq = platform_get_irq_byname(pdev, LTQ_SPI_ERR_IRQ_NAME);
  702. if (err_irq < 0) {
  703. dev_err(dev, "failed to get %s\n", LTQ_SPI_ERR_IRQ_NAME);
  704. return -ENXIO;
  705. }
  706. master = spi_alloc_master(dev, sizeof(struct lantiq_ssc_spi));
  707. if (!master)
  708. return -ENOMEM;
  709. spi = spi_master_get_devdata(master);
  710. spi->master = master;
  711. spi->dev = dev;
  712. spi->hwcfg = hwcfg;
  713. platform_set_drvdata(pdev, spi);
  714. spi->regbase = devm_ioremap_resource(dev, res);
  715. if (IS_ERR(spi->regbase)) {
  716. err = PTR_ERR(spi->regbase);
  717. goto err_master_put;
  718. }
  719. err = devm_request_irq(dev, rx_irq, lantiq_ssc_xmit_interrupt,
  720. 0, LTQ_SPI_RX_IRQ_NAME, spi);
  721. if (err)
  722. goto err_master_put;
  723. err = devm_request_irq(dev, tx_irq, lantiq_ssc_xmit_interrupt,
  724. 0, LTQ_SPI_TX_IRQ_NAME, spi);
  725. if (err)
  726. goto err_master_put;
  727. err = devm_request_irq(dev, err_irq, lantiq_ssc_err_interrupt,
  728. 0, LTQ_SPI_ERR_IRQ_NAME, spi);
  729. if (err)
  730. goto err_master_put;
  731. spi->spi_clk = devm_clk_get(dev, "gate");
  732. if (IS_ERR(spi->spi_clk)) {
  733. err = PTR_ERR(spi->spi_clk);
  734. goto err_master_put;
  735. }
  736. err = clk_prepare_enable(spi->spi_clk);
  737. if (err)
  738. goto err_master_put;
  739. /*
  740. * Use the old clk_get_fpi() function on Lantiq platform, till it
  741. * supports common clk.
  742. */
  743. #if defined(CONFIG_LANTIQ) && !defined(CONFIG_COMMON_CLK)
  744. spi->fpi_clk = clk_get_fpi();
  745. #else
  746. spi->fpi_clk = clk_get(dev, "freq");
  747. #endif
  748. if (IS_ERR(spi->fpi_clk)) {
  749. err = PTR_ERR(spi->fpi_clk);
  750. goto err_clk_disable;
  751. }
  752. num_cs = 8;
  753. of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs);
  754. spi->base_cs = 1;
  755. of_property_read_u32(pdev->dev.of_node, "base-cs", &spi->base_cs);
  756. spin_lock_init(&spi->lock);
  757. spi->bits_per_word = 8;
  758. spi->speed_hz = 0;
  759. master->dev.of_node = pdev->dev.of_node;
  760. master->num_chipselect = num_cs;
  761. master->setup = lantiq_ssc_setup;
  762. master->set_cs = lantiq_ssc_set_cs;
  763. master->handle_err = lantiq_ssc_handle_err;
  764. master->prepare_message = lantiq_ssc_prepare_message;
  765. master->unprepare_message = lantiq_ssc_unprepare_message;
  766. master->transfer_one = lantiq_ssc_transfer_one;
  767. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH |
  768. SPI_LOOP;
  769. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(2, 8) |
  770. SPI_BPW_MASK(16) | SPI_BPW_MASK(32);
  771. spi->wq = alloc_ordered_workqueue(dev_name(dev), 0);
  772. if (!spi->wq) {
  773. err = -ENOMEM;
  774. goto err_clk_put;
  775. }
  776. INIT_WORK(&spi->work, lantiq_ssc_bussy_work);
  777. id = lantiq_ssc_readl(spi, LTQ_SPI_ID);
  778. spi->tx_fifo_size = (id & LTQ_SPI_ID_TXFS_M) >> LTQ_SPI_ID_TXFS_S;
  779. spi->rx_fifo_size = (id & LTQ_SPI_ID_RXFS_M) >> LTQ_SPI_ID_RXFS_S;
  780. supports_dma = (id & LTQ_SPI_ID_CFG_M) >> LTQ_SPI_ID_CFG_S;
  781. revision = id & LTQ_SPI_ID_REV_M;
  782. lantiq_ssc_hw_init(spi);
  783. dev_info(dev,
  784. "Lantiq SSC SPI controller (Rev %i, TXFS %u, RXFS %u, DMA %u)\n",
  785. revision, spi->tx_fifo_size, spi->rx_fifo_size, supports_dma);
  786. err = devm_spi_register_master(dev, master);
  787. if (err) {
  788. dev_err(dev, "failed to register spi_master\n");
  789. goto err_wq_destroy;
  790. }
  791. return 0;
  792. err_wq_destroy:
  793. destroy_workqueue(spi->wq);
  794. err_clk_put:
  795. clk_put(spi->fpi_clk);
  796. err_clk_disable:
  797. clk_disable_unprepare(spi->spi_clk);
  798. err_master_put:
  799. spi_master_put(master);
  800. return err;
  801. }
  802. static int lantiq_ssc_remove(struct platform_device *pdev)
  803. {
  804. struct lantiq_ssc_spi *spi = platform_get_drvdata(pdev);
  805. lantiq_ssc_writel(spi, 0, LTQ_SPI_IRNEN);
  806. lantiq_ssc_writel(spi, 0, LTQ_SPI_CLC);
  807. rx_fifo_flush(spi);
  808. tx_fifo_flush(spi);
  809. hw_enter_config_mode(spi);
  810. destroy_workqueue(spi->wq);
  811. clk_disable_unprepare(spi->spi_clk);
  812. clk_put(spi->fpi_clk);
  813. return 0;
  814. }
  815. static struct platform_driver lantiq_ssc_driver = {
  816. .probe = lantiq_ssc_probe,
  817. .remove = lantiq_ssc_remove,
  818. .driver = {
  819. .name = "spi-lantiq-ssc",
  820. .of_match_table = lantiq_ssc_match,
  821. },
  822. };
  823. module_platform_driver(lantiq_ssc_driver);
  824. MODULE_DESCRIPTION("Lantiq SSC SPI controller driver");
  825. MODULE_AUTHOR("Daniel Schwierzeck <daniel.schwierzeck@gmail.com>");
  826. MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
  827. MODULE_LICENSE("GPL");
  828. MODULE_ALIAS("platform:spi-lantiq-ssc");