spi-lantiq-ssc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. };
  171. static u32 lantiq_ssc_readl(const struct lantiq_ssc_spi *spi, u32 reg)
  172. {
  173. return __raw_readl(spi->regbase + reg);
  174. }
  175. static void lantiq_ssc_writel(const struct lantiq_ssc_spi *spi, u32 val,
  176. u32 reg)
  177. {
  178. __raw_writel(val, spi->regbase + reg);
  179. }
  180. static void lantiq_ssc_maskl(const struct lantiq_ssc_spi *spi, u32 clr,
  181. u32 set, u32 reg)
  182. {
  183. u32 val = __raw_readl(spi->regbase + reg);
  184. val &= ~clr;
  185. val |= set;
  186. __raw_writel(val, spi->regbase + reg);
  187. }
  188. static unsigned int tx_fifo_level(const struct lantiq_ssc_spi *spi)
  189. {
  190. u32 fstat = lantiq_ssc_readl(spi, LTQ_SPI_FSTAT);
  191. return (fstat & LTQ_SPI_FSTAT_TXFFL_M) >> LTQ_SPI_FSTAT_TXFFL_S;
  192. }
  193. static unsigned int rx_fifo_level(const struct lantiq_ssc_spi *spi)
  194. {
  195. u32 fstat = lantiq_ssc_readl(spi, LTQ_SPI_FSTAT);
  196. return fstat & LTQ_SPI_FSTAT_RXFFL_M;
  197. }
  198. static unsigned int tx_fifo_free(const struct lantiq_ssc_spi *spi)
  199. {
  200. return spi->tx_fifo_size - tx_fifo_level(spi);
  201. }
  202. static void rx_fifo_reset(const struct lantiq_ssc_spi *spi)
  203. {
  204. u32 val = spi->rx_fifo_size << LTQ_SPI_RXFCON_RXFITL_S;
  205. val |= LTQ_SPI_RXFCON_RXFEN | LTQ_SPI_RXFCON_RXFLU;
  206. lantiq_ssc_writel(spi, val, LTQ_SPI_RXFCON);
  207. }
  208. static void tx_fifo_reset(const struct lantiq_ssc_spi *spi)
  209. {
  210. u32 val = 1 << LTQ_SPI_TXFCON_TXFITL_S;
  211. val |= LTQ_SPI_TXFCON_TXFEN | LTQ_SPI_TXFCON_TXFLU;
  212. lantiq_ssc_writel(spi, val, LTQ_SPI_TXFCON);
  213. }
  214. static void rx_fifo_flush(const struct lantiq_ssc_spi *spi)
  215. {
  216. lantiq_ssc_maskl(spi, 0, LTQ_SPI_RXFCON_RXFLU, LTQ_SPI_RXFCON);
  217. }
  218. static void tx_fifo_flush(const struct lantiq_ssc_spi *spi)
  219. {
  220. lantiq_ssc_maskl(spi, 0, LTQ_SPI_TXFCON_TXFLU, LTQ_SPI_TXFCON);
  221. }
  222. static void hw_enter_config_mode(const struct lantiq_ssc_spi *spi)
  223. {
  224. lantiq_ssc_writel(spi, LTQ_SPI_WHBSTATE_CLREN, LTQ_SPI_WHBSTATE);
  225. }
  226. static void hw_enter_active_mode(const struct lantiq_ssc_spi *spi)
  227. {
  228. lantiq_ssc_writel(spi, LTQ_SPI_WHBSTATE_SETEN, LTQ_SPI_WHBSTATE);
  229. }
  230. static void hw_setup_speed_hz(const struct lantiq_ssc_spi *spi,
  231. unsigned int max_speed_hz)
  232. {
  233. u32 spi_clk, brt;
  234. /*
  235. * SPI module clock is derived from FPI bus clock dependent on
  236. * divider value in CLC.RMS which is always set to 1.
  237. *
  238. * f_SPI
  239. * baudrate = --------------
  240. * 2 * (BR + 1)
  241. */
  242. spi_clk = clk_get_rate(spi->fpi_clk) / 2;
  243. if (max_speed_hz > spi_clk)
  244. brt = 0;
  245. else
  246. brt = spi_clk / max_speed_hz - 1;
  247. if (brt > 0xFFFF)
  248. brt = 0xFFFF;
  249. dev_dbg(spi->dev, "spi_clk %u, max_speed_hz %u, brt %u\n",
  250. spi_clk, max_speed_hz, brt);
  251. lantiq_ssc_writel(spi, brt, LTQ_SPI_BRT);
  252. }
  253. static void hw_setup_bits_per_word(const struct lantiq_ssc_spi *spi,
  254. unsigned int bits_per_word)
  255. {
  256. u32 bm;
  257. /* CON.BM value = bits_per_word - 1 */
  258. bm = (bits_per_word - 1) << LTQ_SPI_CON_BM_S;
  259. lantiq_ssc_maskl(spi, LTQ_SPI_CON_BM_M, bm, LTQ_SPI_CON);
  260. }
  261. static void hw_setup_clock_mode(const struct lantiq_ssc_spi *spi,
  262. unsigned int mode)
  263. {
  264. u32 con_set = 0, con_clr = 0;
  265. /*
  266. * SPI mode mapping in CON register:
  267. * Mode CPOL CPHA CON.PO CON.PH
  268. * 0 0 0 0 1
  269. * 1 0 1 0 0
  270. * 2 1 0 1 1
  271. * 3 1 1 1 0
  272. */
  273. if (mode & SPI_CPHA)
  274. con_clr |= LTQ_SPI_CON_PH;
  275. else
  276. con_set |= LTQ_SPI_CON_PH;
  277. if (mode & SPI_CPOL)
  278. con_set |= LTQ_SPI_CON_PO | LTQ_SPI_CON_IDLE;
  279. else
  280. con_clr |= LTQ_SPI_CON_PO | LTQ_SPI_CON_IDLE;
  281. /* Set heading control */
  282. if (mode & SPI_LSB_FIRST)
  283. con_clr |= LTQ_SPI_CON_HB;
  284. else
  285. con_set |= LTQ_SPI_CON_HB;
  286. /* Set loopback mode */
  287. if (mode & SPI_LOOP)
  288. con_set |= LTQ_SPI_CON_LB;
  289. else
  290. con_clr |= LTQ_SPI_CON_LB;
  291. lantiq_ssc_maskl(spi, con_clr, con_set, LTQ_SPI_CON);
  292. }
  293. static void lantiq_ssc_hw_init(const struct lantiq_ssc_spi *spi)
  294. {
  295. const struct lantiq_ssc_hwcfg *hwcfg = spi->hwcfg;
  296. /*
  297. * Set clock divider for run mode to 1 to
  298. * run at same frequency as FPI bus
  299. */
  300. lantiq_ssc_writel(spi, 1 << LTQ_SPI_CLC_RMC_S, LTQ_SPI_CLC);
  301. /* Put controller into config mode */
  302. hw_enter_config_mode(spi);
  303. /* Clear error flags */
  304. lantiq_ssc_maskl(spi, 0, LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
  305. /* Enable error checking, disable TX/RX */
  306. lantiq_ssc_writel(spi, LTQ_SPI_CON_RUEN | LTQ_SPI_CON_AEN |
  307. LTQ_SPI_CON_TEN | LTQ_SPI_CON_REN | LTQ_SPI_CON_TXOFF |
  308. LTQ_SPI_CON_RXOFF, LTQ_SPI_CON);
  309. /* Setup default SPI mode */
  310. hw_setup_bits_per_word(spi, spi->bits_per_word);
  311. hw_setup_clock_mode(spi, SPI_MODE_0);
  312. /* Enable master mode and clear error flags */
  313. lantiq_ssc_writel(spi, LTQ_SPI_WHBSTATE_SETMS |
  314. LTQ_SPI_WHBSTATE_CLR_ERRORS,
  315. LTQ_SPI_WHBSTATE);
  316. /* Reset GPIO/CS registers */
  317. lantiq_ssc_writel(spi, 0, LTQ_SPI_GPOCON);
  318. lantiq_ssc_writel(spi, 0xFF00, LTQ_SPI_FPGO);
  319. /* Enable and flush FIFOs */
  320. rx_fifo_reset(spi);
  321. tx_fifo_reset(spi);
  322. /* Enable interrupts */
  323. lantiq_ssc_writel(spi, hwcfg->irnen_t | hwcfg->irnen_r |
  324. LTQ_SPI_IRNEN_E, LTQ_SPI_IRNEN);
  325. }
  326. static int lantiq_ssc_setup(struct spi_device *spidev)
  327. {
  328. struct spi_master *master = spidev->master;
  329. struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
  330. unsigned int cs = spidev->chip_select;
  331. u32 gpocon;
  332. /* GPIOs are used for CS */
  333. if (gpio_is_valid(spidev->cs_gpio))
  334. return 0;
  335. dev_dbg(spi->dev, "using internal chipselect %u\n", cs);
  336. if (cs < spi->base_cs) {
  337. dev_err(spi->dev,
  338. "chipselect %i too small (min %i)\n", cs, spi->base_cs);
  339. return -EINVAL;
  340. }
  341. /* set GPO pin to CS mode */
  342. gpocon = 1 << ((cs - spi->base_cs) + LTQ_SPI_GPOCON_ISCSBN_S);
  343. /* invert GPO pin */
  344. if (spidev->mode & SPI_CS_HIGH)
  345. gpocon |= 1 << (cs - spi->base_cs);
  346. lantiq_ssc_maskl(spi, 0, gpocon, LTQ_SPI_GPOCON);
  347. return 0;
  348. }
  349. static int lantiq_ssc_prepare_message(struct spi_master *master,
  350. struct spi_message *message)
  351. {
  352. struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
  353. hw_enter_config_mode(spi);
  354. hw_setup_clock_mode(spi, message->spi->mode);
  355. hw_enter_active_mode(spi);
  356. return 0;
  357. }
  358. static void hw_setup_transfer(struct lantiq_ssc_spi *spi,
  359. struct spi_device *spidev, struct spi_transfer *t)
  360. {
  361. unsigned int speed_hz = t->speed_hz;
  362. unsigned int bits_per_word = t->bits_per_word;
  363. u32 con;
  364. if (bits_per_word != spi->bits_per_word ||
  365. speed_hz != spi->speed_hz) {
  366. hw_enter_config_mode(spi);
  367. hw_setup_speed_hz(spi, speed_hz);
  368. hw_setup_bits_per_word(spi, bits_per_word);
  369. hw_enter_active_mode(spi);
  370. spi->speed_hz = speed_hz;
  371. spi->bits_per_word = bits_per_word;
  372. }
  373. /* Configure transmitter and receiver */
  374. con = lantiq_ssc_readl(spi, LTQ_SPI_CON);
  375. if (t->tx_buf)
  376. con &= ~LTQ_SPI_CON_TXOFF;
  377. else
  378. con |= LTQ_SPI_CON_TXOFF;
  379. if (t->rx_buf)
  380. con &= ~LTQ_SPI_CON_RXOFF;
  381. else
  382. con |= LTQ_SPI_CON_RXOFF;
  383. lantiq_ssc_writel(spi, con, LTQ_SPI_CON);
  384. }
  385. static int lantiq_ssc_unprepare_message(struct spi_master *master,
  386. struct spi_message *message)
  387. {
  388. struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
  389. flush_workqueue(spi->wq);
  390. /* Disable transmitter and receiver while idle */
  391. lantiq_ssc_maskl(spi, 0, LTQ_SPI_CON_TXOFF | LTQ_SPI_CON_RXOFF,
  392. LTQ_SPI_CON);
  393. return 0;
  394. }
  395. static void tx_fifo_write(struct lantiq_ssc_spi *spi)
  396. {
  397. const u8 *tx8;
  398. const u16 *tx16;
  399. const u32 *tx32;
  400. u32 data;
  401. unsigned int tx_free = tx_fifo_free(spi);
  402. while (spi->tx_todo && tx_free) {
  403. switch (spi->bits_per_word) {
  404. case 2 ... 8:
  405. tx8 = spi->tx;
  406. data = *tx8;
  407. spi->tx_todo--;
  408. spi->tx++;
  409. break;
  410. case 16:
  411. tx16 = (u16 *) spi->tx;
  412. data = *tx16;
  413. spi->tx_todo -= 2;
  414. spi->tx += 2;
  415. break;
  416. case 32:
  417. tx32 = (u32 *) spi->tx;
  418. data = *tx32;
  419. spi->tx_todo -= 4;
  420. spi->tx += 4;
  421. break;
  422. default:
  423. WARN_ON(1);
  424. data = 0;
  425. break;
  426. }
  427. lantiq_ssc_writel(spi, data, LTQ_SPI_TB);
  428. tx_free--;
  429. }
  430. }
  431. static void rx_fifo_read_full_duplex(struct lantiq_ssc_spi *spi)
  432. {
  433. u8 *rx8;
  434. u16 *rx16;
  435. u32 *rx32;
  436. u32 data;
  437. unsigned int rx_fill = rx_fifo_level(spi);
  438. while (rx_fill) {
  439. data = lantiq_ssc_readl(spi, LTQ_SPI_RB);
  440. switch (spi->bits_per_word) {
  441. case 2 ... 8:
  442. rx8 = spi->rx;
  443. *rx8 = data;
  444. spi->rx_todo--;
  445. spi->rx++;
  446. break;
  447. case 16:
  448. rx16 = (u16 *) spi->rx;
  449. *rx16 = data;
  450. spi->rx_todo -= 2;
  451. spi->rx += 2;
  452. break;
  453. case 32:
  454. rx32 = (u32 *) spi->rx;
  455. *rx32 = data;
  456. spi->rx_todo -= 4;
  457. spi->rx += 4;
  458. break;
  459. default:
  460. WARN_ON(1);
  461. break;
  462. }
  463. rx_fill--;
  464. }
  465. }
  466. static void rx_fifo_read_half_duplex(struct lantiq_ssc_spi *spi)
  467. {
  468. u32 data, *rx32;
  469. u8 *rx8;
  470. unsigned int rxbv, shift;
  471. unsigned int rx_fill = rx_fifo_level(spi);
  472. /*
  473. * In RX-only mode the bits per word value is ignored by HW. A value
  474. * of 32 is used instead. Thus all 4 bytes per FIFO must be read.
  475. * If remaining RX bytes are less than 4, the FIFO must be read
  476. * differently. The amount of received and valid bytes is indicated
  477. * by STAT.RXBV register value.
  478. */
  479. while (rx_fill) {
  480. if (spi->rx_todo < 4) {
  481. rxbv = (lantiq_ssc_readl(spi, LTQ_SPI_STAT) &
  482. LTQ_SPI_STAT_RXBV_M) >> LTQ_SPI_STAT_RXBV_S;
  483. data = lantiq_ssc_readl(spi, LTQ_SPI_RB);
  484. shift = (rxbv - 1) * 8;
  485. rx8 = spi->rx;
  486. while (rxbv) {
  487. *rx8++ = (data >> shift) & 0xFF;
  488. rxbv--;
  489. shift -= 8;
  490. spi->rx_todo--;
  491. spi->rx++;
  492. }
  493. } else {
  494. data = lantiq_ssc_readl(spi, LTQ_SPI_RB);
  495. rx32 = (u32 *) spi->rx;
  496. *rx32++ = data;
  497. spi->rx_todo -= 4;
  498. spi->rx += 4;
  499. }
  500. rx_fill--;
  501. }
  502. }
  503. static void rx_request(struct lantiq_ssc_spi *spi)
  504. {
  505. unsigned int rxreq, rxreq_max;
  506. /*
  507. * To avoid receive overflows at high clocks it is better to request
  508. * only the amount of bytes that fits into all FIFOs. This value
  509. * depends on the FIFO size implemented in hardware.
  510. */
  511. rxreq = spi->rx_todo;
  512. rxreq_max = spi->rx_fifo_size * 4;
  513. if (rxreq > rxreq_max)
  514. rxreq = rxreq_max;
  515. lantiq_ssc_writel(spi, rxreq, LTQ_SPI_RXREQ);
  516. }
  517. static irqreturn_t lantiq_ssc_xmit_interrupt(int irq, void *data)
  518. {
  519. struct lantiq_ssc_spi *spi = data;
  520. if (spi->tx) {
  521. if (spi->rx && spi->rx_todo)
  522. rx_fifo_read_full_duplex(spi);
  523. if (spi->tx_todo)
  524. tx_fifo_write(spi);
  525. else if (!tx_fifo_level(spi))
  526. goto completed;
  527. } else if (spi->rx) {
  528. if (spi->rx_todo) {
  529. rx_fifo_read_half_duplex(spi);
  530. if (spi->rx_todo)
  531. rx_request(spi);
  532. else
  533. goto completed;
  534. } else {
  535. goto completed;
  536. }
  537. }
  538. return IRQ_HANDLED;
  539. completed:
  540. queue_work(spi->wq, &spi->work);
  541. return IRQ_HANDLED;
  542. }
  543. static irqreturn_t lantiq_ssc_err_interrupt(int irq, void *data)
  544. {
  545. struct lantiq_ssc_spi *spi = data;
  546. u32 stat = lantiq_ssc_readl(spi, LTQ_SPI_STAT);
  547. if (!(stat & LTQ_SPI_STAT_ERRORS))
  548. return IRQ_NONE;
  549. if (stat & LTQ_SPI_STAT_RUE)
  550. dev_err(spi->dev, "receive underflow error\n");
  551. if (stat & LTQ_SPI_STAT_TUE)
  552. dev_err(spi->dev, "transmit underflow error\n");
  553. if (stat & LTQ_SPI_STAT_AE)
  554. dev_err(spi->dev, "abort error\n");
  555. if (stat & LTQ_SPI_STAT_RE)
  556. dev_err(spi->dev, "receive overflow error\n");
  557. if (stat & LTQ_SPI_STAT_TE)
  558. dev_err(spi->dev, "transmit overflow error\n");
  559. if (stat & LTQ_SPI_STAT_ME)
  560. dev_err(spi->dev, "mode error\n");
  561. /* Clear error flags */
  562. lantiq_ssc_maskl(spi, 0, LTQ_SPI_WHBSTATE_CLR_ERRORS, LTQ_SPI_WHBSTATE);
  563. /* set bad status so it can be retried */
  564. if (spi->master->cur_msg)
  565. spi->master->cur_msg->status = -EIO;
  566. queue_work(spi->wq, &spi->work);
  567. return IRQ_HANDLED;
  568. }
  569. static int transfer_start(struct lantiq_ssc_spi *spi, struct spi_device *spidev,
  570. struct spi_transfer *t)
  571. {
  572. unsigned long flags;
  573. spin_lock_irqsave(&spi->lock, flags);
  574. spi->tx = t->tx_buf;
  575. spi->rx = t->rx_buf;
  576. if (t->tx_buf) {
  577. spi->tx_todo = t->len;
  578. /* initially fill TX FIFO */
  579. tx_fifo_write(spi);
  580. }
  581. if (spi->rx) {
  582. spi->rx_todo = t->len;
  583. /* start shift clock in RX-only mode */
  584. if (!spi->tx)
  585. rx_request(spi);
  586. }
  587. spin_unlock_irqrestore(&spi->lock, flags);
  588. return t->len;
  589. }
  590. /*
  591. * The driver only gets an interrupt when the FIFO is empty, but there
  592. * is an additional shift register from which the data is written to
  593. * the wire. We get the last interrupt when the controller starts to
  594. * write the last word to the wire, not when it is finished. Do busy
  595. * waiting till it finishes.
  596. */
  597. static void lantiq_ssc_bussy_work(struct work_struct *work)
  598. {
  599. struct lantiq_ssc_spi *spi;
  600. unsigned long long timeout = 8LL * 1000LL;
  601. unsigned long end;
  602. spi = container_of(work, typeof(*spi), work);
  603. do_div(timeout, spi->speed_hz);
  604. timeout += timeout + 100; /* some tolerance */
  605. end = jiffies + msecs_to_jiffies(timeout);
  606. do {
  607. u32 stat = lantiq_ssc_readl(spi, LTQ_SPI_STAT);
  608. if (!(stat & LTQ_SPI_STAT_BSY)) {
  609. spi_finalize_current_transfer(spi->master);
  610. return;
  611. }
  612. cond_resched();
  613. } while (!time_after_eq(jiffies, end));
  614. if (spi->master->cur_msg)
  615. spi->master->cur_msg->status = -EIO;
  616. spi_finalize_current_transfer(spi->master);
  617. }
  618. static void lantiq_ssc_handle_err(struct spi_master *master,
  619. struct spi_message *message)
  620. {
  621. struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
  622. /* flush FIFOs on timeout */
  623. rx_fifo_flush(spi);
  624. tx_fifo_flush(spi);
  625. }
  626. static void lantiq_ssc_set_cs(struct spi_device *spidev, bool enable)
  627. {
  628. struct lantiq_ssc_spi *spi = spi_master_get_devdata(spidev->master);
  629. unsigned int cs = spidev->chip_select;
  630. u32 fgpo;
  631. if (!!(spidev->mode & SPI_CS_HIGH) == enable)
  632. fgpo = (1 << (cs - spi->base_cs));
  633. else
  634. fgpo = (1 << (cs - spi->base_cs + LTQ_SPI_FGPO_SETOUTN_S));
  635. lantiq_ssc_writel(spi, fgpo, LTQ_SPI_FPGO);
  636. }
  637. static int lantiq_ssc_transfer_one(struct spi_master *master,
  638. struct spi_device *spidev,
  639. struct spi_transfer *t)
  640. {
  641. struct lantiq_ssc_spi *spi = spi_master_get_devdata(master);
  642. hw_setup_transfer(spi, spidev, t);
  643. return transfer_start(spi, spidev, t);
  644. }
  645. static const struct lantiq_ssc_hwcfg lantiq_ssc_xway = {
  646. .irnen_r = LTQ_SPI_IRNEN_R_XWAY,
  647. .irnen_t = LTQ_SPI_IRNEN_T_XWAY,
  648. };
  649. static const struct lantiq_ssc_hwcfg lantiq_ssc_xrx = {
  650. .irnen_r = LTQ_SPI_IRNEN_R_XRX,
  651. .irnen_t = LTQ_SPI_IRNEN_T_XRX,
  652. };
  653. static const struct of_device_id lantiq_ssc_match[] = {
  654. { .compatible = "lantiq,ase-spi", .data = &lantiq_ssc_xway, },
  655. { .compatible = "lantiq,falcon-spi", .data = &lantiq_ssc_xrx, },
  656. { .compatible = "lantiq,xrx100-spi", .data = &lantiq_ssc_xrx, },
  657. {},
  658. };
  659. MODULE_DEVICE_TABLE(of, lantiq_ssc_match);
  660. static int lantiq_ssc_probe(struct platform_device *pdev)
  661. {
  662. struct device *dev = &pdev->dev;
  663. struct spi_master *master;
  664. struct resource *res;
  665. struct lantiq_ssc_spi *spi;
  666. const struct lantiq_ssc_hwcfg *hwcfg;
  667. const struct of_device_id *match;
  668. int err, rx_irq, tx_irq, err_irq;
  669. u32 id, supports_dma, revision;
  670. unsigned int num_cs;
  671. match = of_match_device(lantiq_ssc_match, dev);
  672. if (!match) {
  673. dev_err(dev, "no device match\n");
  674. return -EINVAL;
  675. }
  676. hwcfg = match->data;
  677. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  678. if (!res) {
  679. dev_err(dev, "failed to get resources\n");
  680. return -ENXIO;
  681. }
  682. rx_irq = platform_get_irq_byname(pdev, LTQ_SPI_RX_IRQ_NAME);
  683. if (rx_irq < 0) {
  684. dev_err(dev, "failed to get %s\n", LTQ_SPI_RX_IRQ_NAME);
  685. return -ENXIO;
  686. }
  687. tx_irq = platform_get_irq_byname(pdev, LTQ_SPI_TX_IRQ_NAME);
  688. if (tx_irq < 0) {
  689. dev_err(dev, "failed to get %s\n", LTQ_SPI_TX_IRQ_NAME);
  690. return -ENXIO;
  691. }
  692. err_irq = platform_get_irq_byname(pdev, LTQ_SPI_ERR_IRQ_NAME);
  693. if (err_irq < 0) {
  694. dev_err(dev, "failed to get %s\n", LTQ_SPI_ERR_IRQ_NAME);
  695. return -ENXIO;
  696. }
  697. master = spi_alloc_master(dev, sizeof(struct lantiq_ssc_spi));
  698. if (!master)
  699. return -ENOMEM;
  700. spi = spi_master_get_devdata(master);
  701. spi->master = master;
  702. spi->dev = dev;
  703. spi->hwcfg = hwcfg;
  704. platform_set_drvdata(pdev, spi);
  705. spi->regbase = devm_ioremap_resource(dev, res);
  706. if (IS_ERR(spi->regbase)) {
  707. err = PTR_ERR(spi->regbase);
  708. goto err_master_put;
  709. }
  710. err = devm_request_irq(dev, rx_irq, lantiq_ssc_xmit_interrupt,
  711. 0, LTQ_SPI_RX_IRQ_NAME, spi);
  712. if (err)
  713. goto err_master_put;
  714. err = devm_request_irq(dev, tx_irq, lantiq_ssc_xmit_interrupt,
  715. 0, LTQ_SPI_TX_IRQ_NAME, spi);
  716. if (err)
  717. goto err_master_put;
  718. err = devm_request_irq(dev, err_irq, lantiq_ssc_err_interrupt,
  719. 0, LTQ_SPI_ERR_IRQ_NAME, spi);
  720. if (err)
  721. goto err_master_put;
  722. spi->spi_clk = devm_clk_get(dev, "gate");
  723. if (IS_ERR(spi->spi_clk)) {
  724. err = PTR_ERR(spi->spi_clk);
  725. goto err_master_put;
  726. }
  727. err = clk_prepare_enable(spi->spi_clk);
  728. if (err)
  729. goto err_master_put;
  730. /*
  731. * Use the old clk_get_fpi() function on Lantiq platform, till it
  732. * supports common clk.
  733. */
  734. #if defined(CONFIG_LANTIQ) && !defined(CONFIG_COMMON_CLK)
  735. spi->fpi_clk = clk_get_fpi();
  736. #else
  737. spi->fpi_clk = clk_get(dev, "freq");
  738. #endif
  739. if (IS_ERR(spi->fpi_clk)) {
  740. err = PTR_ERR(spi->fpi_clk);
  741. goto err_clk_disable;
  742. }
  743. num_cs = 8;
  744. of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs);
  745. spi->base_cs = 1;
  746. of_property_read_u32(pdev->dev.of_node, "base-cs", &spi->base_cs);
  747. spin_lock_init(&spi->lock);
  748. spi->bits_per_word = 8;
  749. spi->speed_hz = 0;
  750. master->dev.of_node = pdev->dev.of_node;
  751. master->num_chipselect = num_cs;
  752. master->setup = lantiq_ssc_setup;
  753. master->set_cs = lantiq_ssc_set_cs;
  754. master->handle_err = lantiq_ssc_handle_err;
  755. master->prepare_message = lantiq_ssc_prepare_message;
  756. master->unprepare_message = lantiq_ssc_unprepare_message;
  757. master->transfer_one = lantiq_ssc_transfer_one;
  758. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH |
  759. SPI_LOOP;
  760. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(2, 8) |
  761. SPI_BPW_MASK(16) | SPI_BPW_MASK(32);
  762. spi->wq = alloc_ordered_workqueue(dev_name(dev), 0);
  763. if (!spi->wq) {
  764. err = -ENOMEM;
  765. goto err_clk_put;
  766. }
  767. INIT_WORK(&spi->work, lantiq_ssc_bussy_work);
  768. id = lantiq_ssc_readl(spi, LTQ_SPI_ID);
  769. spi->tx_fifo_size = (id & LTQ_SPI_ID_TXFS_M) >> LTQ_SPI_ID_TXFS_S;
  770. spi->rx_fifo_size = (id & LTQ_SPI_ID_RXFS_M) >> LTQ_SPI_ID_RXFS_S;
  771. supports_dma = (id & LTQ_SPI_ID_CFG_M) >> LTQ_SPI_ID_CFG_S;
  772. revision = id & LTQ_SPI_ID_REV_M;
  773. lantiq_ssc_hw_init(spi);
  774. dev_info(dev,
  775. "Lantiq SSC SPI controller (Rev %i, TXFS %u, RXFS %u, DMA %u)\n",
  776. revision, spi->tx_fifo_size, spi->rx_fifo_size, supports_dma);
  777. err = devm_spi_register_master(dev, master);
  778. if (err) {
  779. dev_err(dev, "failed to register spi_master\n");
  780. goto err_wq_destroy;
  781. }
  782. return 0;
  783. err_wq_destroy:
  784. destroy_workqueue(spi->wq);
  785. err_clk_put:
  786. clk_put(spi->fpi_clk);
  787. err_clk_disable:
  788. clk_disable_unprepare(spi->spi_clk);
  789. err_master_put:
  790. spi_master_put(master);
  791. return err;
  792. }
  793. static int lantiq_ssc_remove(struct platform_device *pdev)
  794. {
  795. struct lantiq_ssc_spi *spi = platform_get_drvdata(pdev);
  796. lantiq_ssc_writel(spi, 0, LTQ_SPI_IRNEN);
  797. lantiq_ssc_writel(spi, 0, LTQ_SPI_CLC);
  798. rx_fifo_flush(spi);
  799. tx_fifo_flush(spi);
  800. hw_enter_config_mode(spi);
  801. destroy_workqueue(spi->wq);
  802. clk_disable_unprepare(spi->spi_clk);
  803. clk_put(spi->fpi_clk);
  804. return 0;
  805. }
  806. static struct platform_driver lantiq_ssc_driver = {
  807. .probe = lantiq_ssc_probe,
  808. .remove = lantiq_ssc_remove,
  809. .driver = {
  810. .name = "spi-lantiq-ssc",
  811. .of_match_table = lantiq_ssc_match,
  812. },
  813. };
  814. module_platform_driver(lantiq_ssc_driver);
  815. MODULE_DESCRIPTION("Lantiq SSC SPI controller driver");
  816. MODULE_AUTHOR("Daniel Schwierzeck <daniel.schwierzeck@gmail.com>");
  817. MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
  818. MODULE_LICENSE("GPL");
  819. MODULE_ALIAS("platform:spi-lantiq-ssc");