0100-spi-add-support-for-Lantiq-SPI-controller.patch 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. From 0175fc559debc22fe8d17e9b8ffd1452e0a4667d Mon Sep 17 00:00:00 2001
  2. From: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
  3. Date: Tue, 16 Dec 2014 15:40:32 +0100
  4. Subject: [PATCH 1/2] spi: add support for Lantiq SPI controller
  5. Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
  6. ---
  7. drivers/spi/Kconfig | 7 +
  8. drivers/spi/Makefile | 1 +
  9. drivers/spi/spi-lantiq.c | 1089 ++++++++++++++++++++++++++++++++++++++++++++++
  10. 3 files changed, 1097 insertions(+)
  11. create mode 100644 drivers/spi/spi-lantiq.c
  12. --- a/drivers/spi/Kconfig
  13. +++ b/drivers/spi/Kconfig
  14. @@ -355,6 +355,13 @@ config SPI_MT65XX
  15. say Y or M here.If you are not sure, say N.
  16. SPI drivers for Mediatek MT65XX and MT81XX series ARM SoCs.
  17. +config SPI_LANTIQ
  18. + tristate "Lantiq SPI controller"
  19. + depends on LANTIQ && (SOC_TYPE_XWAY || SOC_FALCON)
  20. + help
  21. + This driver supports the Lantiq SPI controller in master
  22. + mode.
  23. +
  24. config SPI_OC_TINY
  25. tristate "OpenCores tiny SPI"
  26. depends on GPIOLIB || COMPILE_TEST
  27. --- a/drivers/spi/Makefile
  28. +++ b/drivers/spi/Makefile
  29. @@ -45,6 +45,7 @@ obj-$(CONFIG_SPI_GPIO) += spi-gpio.o
  30. obj-$(CONFIG_SPI_GPIO_OLD) += spi_gpio_old.o
  31. obj-$(CONFIG_SPI_IMG_SPFI) += spi-img-spfi.o
  32. obj-$(CONFIG_SPI_IMX) += spi-imx.o
  33. +obj-$(CONFIG_SPI_LANTIQ) += spi-lantiq.o
  34. obj-$(CONFIG_SPI_LM70_LLP) += spi-lm70llp.o
  35. obj-$(CONFIG_SPI_MESON_SPIFC) += spi-meson-spifc.o
  36. obj-$(CONFIG_SPI_MPC512x_PSC) += spi-mpc512x-psc.o
  37. --- /dev/null
  38. +++ b/drivers/spi/spi-lantiq.c
  39. @@ -0,0 +1,1091 @@
  40. +/*
  41. + * Copyright (C) 2011-2015 Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
  42. + *
  43. + * This program is free software; you can distribute it and/or modify it
  44. + * under the terms of the GNU General Public License (Version 2) as
  45. + * published by the Free Software Foundation.
  46. + */
  47. +
  48. +#include <linux/kernel.h>
  49. +#include <linux/module.h>
  50. +#include <linux/of_device.h>
  51. +#include <linux/io.h>
  52. +#include <linux/delay.h>
  53. +#include <linux/interrupt.h>
  54. +#include <linux/sched.h>
  55. +#include <linux/completion.h>
  56. +#include <linux/spinlock.h>
  57. +#include <linux/err.h>
  58. +#include <linux/gpio.h>
  59. +#include <linux/pm_runtime.h>
  60. +#include <linux/spi/spi.h>
  61. +
  62. +#include <lantiq_soc.h>
  63. +
  64. +#define SPI_RX_IRQ_NAME "spi_rx"
  65. +#define SPI_TX_IRQ_NAME "spi_tx"
  66. +#define SPI_ERR_IRQ_NAME "spi_err"
  67. +#define SPI_FRM_IRQ_NAME "spi_frm"
  68. +
  69. +#define SPI_CLC 0x00
  70. +#define SPI_PISEL 0x04
  71. +#define SPI_ID 0x08
  72. +#define SPI_CON 0x10
  73. +#define SPI_STAT 0x14
  74. +#define SPI_WHBSTATE 0x18
  75. +#define SPI_TB 0x20
  76. +#define SPI_RB 0x24
  77. +#define SPI_RXFCON 0x30
  78. +#define SPI_TXFCON 0x34
  79. +#define SPI_FSTAT 0x38
  80. +#define SPI_BRT 0x40
  81. +#define SPI_BRSTAT 0x44
  82. +#define SPI_SFCON 0x60
  83. +#define SPI_SFSTAT 0x64
  84. +#define SPI_GPOCON 0x70
  85. +#define SPI_GPOSTAT 0x74
  86. +#define SPI_FPGO 0x78
  87. +#define SPI_RXREQ 0x80
  88. +#define SPI_RXCNT 0x84
  89. +#define SPI_DMACON 0xec
  90. +#define SPI_IRNEN 0xf4
  91. +#define SPI_IRNICR 0xf8
  92. +#define SPI_IRNCR 0xfc
  93. +
  94. +#define SPI_CLC_SMC_S 16 /* Clock divider for sleep mode */
  95. +#define SPI_CLC_SMC_M (0xFF << SPI_CLC_SMC_S)
  96. +#define SPI_CLC_RMC_S 8 /* Clock divider for normal run mode */
  97. +#define SPI_CLC_RMC_M (0xFF << SPI_CLC_RMC_S)
  98. +#define SPI_CLC_DISS BIT(1) /* Disable status bit */
  99. +#define SPI_CLC_DISR BIT(0) /* Disable request bit */
  100. +
  101. +#define SPI_ID_TXFS_S 24 /* Implemented TX FIFO size */
  102. +#define SPI_ID_TXFS_M (0x3F << SPI_ID_TXFS_S)
  103. +#define SPI_ID_RXFS_S 16 /* Implemented RX FIFO size */
  104. +#define SPI_ID_RXFS_M (0x3F << SPI_ID_RXFS_S)
  105. +#define SPI_ID_REV_M 0x1F /* Hardware revision number */
  106. +#define SPI_ID_CFG BIT(5) /* DMA interface support */
  107. +
  108. +#define SPI_CON_BM_S 16 /* Data width selection */
  109. +#define SPI_CON_BM_M (0x1F << SPI_CON_BM_S)
  110. +#define SPI_CON_EM BIT(24) /* Echo mode */
  111. +#define SPI_CON_IDLE BIT(23) /* Idle bit value */
  112. +#define SPI_CON_ENBV BIT(22) /* Enable byte valid control */
  113. +#define SPI_CON_RUEN BIT(12) /* Receive underflow error enable */
  114. +#define SPI_CON_TUEN BIT(11) /* Transmit underflow error enable */
  115. +#define SPI_CON_AEN BIT(10) /* Abort error enable */
  116. +#define SPI_CON_REN BIT(9) /* Receive overflow error enable */
  117. +#define SPI_CON_TEN BIT(8) /* Transmit overflow error enable */
  118. +#define SPI_CON_LB BIT(7) /* Loopback control */
  119. +#define SPI_CON_PO BIT(6) /* Clock polarity control */
  120. +#define SPI_CON_PH BIT(5) /* Clock phase control */
  121. +#define SPI_CON_HB BIT(4) /* Heading control */
  122. +#define SPI_CON_RXOFF BIT(1) /* Switch receiver off */
  123. +#define SPI_CON_TXOFF BIT(0) /* Switch transmitter off */
  124. +
  125. +#define SPI_STAT_RXBV_S 28
  126. +#define SPI_STAT_RXBV_M (0x7 << SPI_STAT_RXBV_S)
  127. +#define SPI_STAT_BSY BIT(13) /* Busy flag */
  128. +#define SPI_STAT_RUE BIT(12) /* Receive underflow error flag */
  129. +#define SPI_STAT_TUE BIT(11) /* Transmit underflow error flag */
  130. +#define SPI_STAT_AE BIT(10) /* Abort error flag */
  131. +#define SPI_STAT_RE BIT(9) /* Receive error flag */
  132. +#define SPI_STAT_TE BIT(8) /* Transmit error flag */
  133. +#define SPI_STAT_ME BIT(7) /* Mode error flag */
  134. +#define SPI_STAT_MS BIT(1) /* Master/slave select bit */
  135. +#define SPI_STAT_EN BIT(0) /* Enable bit */
  136. +
  137. +#define SPI_WHBSTATE_SETTUE BIT(15) /* Set transmit underflow error flag */
  138. +#define SPI_WHBSTATE_SETAE BIT(14) /* Set abort error flag */
  139. +#define SPI_WHBSTATE_SETRE BIT(13) /* Set receive error flag */
  140. +#define SPI_WHBSTATE_SETTE BIT(12) /* Set transmit error flag */
  141. +#define SPI_WHBSTATE_CLRTUE BIT(11) /* Clear transmit underflow error flag */
  142. +#define SPI_WHBSTATE_CLRAE BIT(10) /* Clear abort error flag */
  143. +#define SPI_WHBSTATE_CLRRE BIT(9) /* Clear receive error flag */
  144. +#define SPI_WHBSTATE_CLRTE BIT(8) /* Clear transmit error flag */
  145. +#define SPI_WHBSTATE_SETME BIT(7) /* Set mode error flag */
  146. +#define SPI_WHBSTATE_CLRME BIT(6) /* Clear mode error flag */
  147. +#define SPI_WHBSTATE_SETRUE BIT(5) /* Set receive underflow error flag */
  148. +#define SPI_WHBSTATE_CLRRUE BIT(4) /* Clear receive underflow error flag */
  149. +#define SPI_WHBSTATE_SETMS BIT(3) /* Set master select bit */
  150. +#define SPI_WHBSTATE_CLRMS BIT(2) /* Clear master select bit */
  151. +#define SPI_WHBSTATE_SETEN BIT(1) /* Set enable bit (operational mode) */
  152. +#define SPI_WHBSTATE_CLREN BIT(0) /* Clear enable bit (config mode */
  153. +#define SPI_WHBSTATE_CLR_ERRORS 0x0F50
  154. +
  155. +#define SPI_RXFCON_RXFITL_S 8 /* FIFO interrupt trigger level */
  156. +#define SPI_RXFCON_RXFITL_M (0x3F << SPI_RXFCON_RXFITL_S)
  157. +#define SPI_RXFCON_RXFLU BIT(1) /* FIFO flush */
  158. +#define SPI_RXFCON_RXFEN BIT(0) /* FIFO enable */
  159. +
  160. +#define SPI_TXFCON_TXFITL_S 8 /* FIFO interrupt trigger level */
  161. +#define SPI_TXFCON_TXFITL_M (0x3F << SPI_TXFCON_TXFITL_S)
  162. +#define SPI_TXFCON_TXFLU BIT(1) /* FIFO flush */
  163. +#define SPI_TXFCON_TXFEN BIT(0) /* FIFO enable */
  164. +
  165. +#define SPI_FSTAT_RXFFL_S 0
  166. +#define SPI_FSTAT_RXFFL_M (0x3f << SPI_FSTAT_RXFFL_S)
  167. +#define SPI_FSTAT_TXFFL_S 8
  168. +#define SPI_FSTAT_TXFFL_M (0x3f << SPI_FSTAT_TXFFL_S)
  169. +
  170. +#define SPI_GPOCON_ISCSBN_S 8
  171. +#define SPI_GPOCON_INVOUTN_S 0
  172. +
  173. +#define SPI_FGPO_SETOUTN_S 8
  174. +#define SPI_FGPO_CLROUTN_S 0
  175. +
  176. +#define SPI_RXREQ_RXCNT_M 0xFFFF /* Receive count value */
  177. +#define SPI_RXCNT_TODO_M 0xFFFF /* Recevie to-do value */
  178. +
  179. +#define SPI_IRNEN_TFI BIT(4) /* TX finished interrupt */
  180. +#define SPI_IRNEN_F BIT(3) /* Frame end interrupt request */
  181. +#define SPI_IRNEN_E BIT(2) /* Error end interrupt request */
  182. +#define SPI_IRNEN_T_XWAY BIT(1) /* Transmit end interrupt request */
  183. +#define SPI_IRNEN_R_XWAY BIT(0) /* Receive end interrupt request */
  184. +#define SPI_IRNEN_R_XRX BIT(1) /* Transmit end interrupt request */
  185. +#define SPI_IRNEN_T_XRX BIT(0) /* Receive end interrupt request */
  186. +#define SPI_IRNEN_ALL 0x1F
  187. +
  188. +struct lantiq_spi_hwcfg {
  189. + unsigned int num_chipselect;
  190. + unsigned int irnen_r;
  191. + unsigned int irnen_t;
  192. +};
  193. +
  194. +struct lantiq_spi {
  195. + struct spi_master *master;
  196. + struct device *dev;
  197. + void __iomem *regbase;
  198. + struct clk *spi_clk;
  199. + struct clk *fpi_clk;
  200. + const struct lantiq_spi_hwcfg *hwcfg;
  201. +
  202. + spinlock_t lock;
  203. + struct completion xfer_complete;
  204. +
  205. + const u8 *tx;
  206. + u8 *rx;
  207. + unsigned int tx_todo;
  208. + unsigned int rx_todo;
  209. + unsigned int bits_per_word;
  210. + unsigned int speed_hz;
  211. + int status;
  212. + unsigned long timeout;
  213. + unsigned int cs_delay;
  214. +};
  215. +
  216. +struct lantiq_spi_cstate {
  217. + int cs_gpio;
  218. +};
  219. +
  220. +static u32 lantiq_spi_readl(const struct lantiq_spi *spi, u32 reg)
  221. +{
  222. + return readl_be(spi->regbase + reg);
  223. +}
  224. +
  225. +static void lantiq_spi_writel(const struct lantiq_spi *spi, u32 val, u32 reg)
  226. +{
  227. + writel_be(val, spi->regbase + reg);
  228. +}
  229. +
  230. +static void lantiq_spi_maskl(const struct lantiq_spi *spi, u32 clr, u32 set,
  231. + u32 reg)
  232. +{
  233. + u32 val = readl_be(spi->regbase + reg);
  234. + val &= ~clr;
  235. + val |= set;
  236. + writel_be(val, spi->regbase + reg);
  237. +}
  238. +
  239. +static int supports_dma(const struct lantiq_spi *spi)
  240. +{
  241. + u32 id = lantiq_spi_readl(spi, SPI_ID);
  242. + return id & SPI_ID_CFG;
  243. +}
  244. +
  245. +static unsigned int tx_fifo_size(const struct lantiq_spi *spi)
  246. +{
  247. + u32 id = lantiq_spi_readl(spi, SPI_ID);
  248. + return (id & SPI_ID_TXFS_M) >> SPI_ID_TXFS_S;
  249. +}
  250. +
  251. +static unsigned int rx_fifo_size(const struct lantiq_spi *spi)
  252. +{
  253. + u32 id = lantiq_spi_readl(spi, SPI_ID);
  254. + return (id & SPI_ID_RXFS_M) >> SPI_ID_RXFS_S;
  255. +}
  256. +
  257. +static unsigned int tx_fifo_level(const struct lantiq_spi *spi)
  258. +{
  259. + u32 fstat = lantiq_spi_readl(spi, SPI_FSTAT);
  260. + return (fstat & SPI_FSTAT_TXFFL_M) >> SPI_FSTAT_TXFFL_S;
  261. +}
  262. +
  263. +static unsigned int rx_fifo_level(const struct lantiq_spi *spi)
  264. +{
  265. + u32 fstat = lantiq_spi_readl(spi, SPI_FSTAT);
  266. + return fstat & SPI_FSTAT_RXFFL_M;
  267. +}
  268. +
  269. +static unsigned int tx_fifo_free(const struct lantiq_spi *spi)
  270. +{
  271. + return tx_fifo_size(spi) - tx_fifo_level(spi);
  272. +}
  273. +
  274. +static void rx_fifo_reset(const struct lantiq_spi *spi)
  275. +{
  276. + u32 val = rx_fifo_size(spi) << SPI_RXFCON_RXFITL_S;
  277. + val |= SPI_RXFCON_RXFEN | SPI_RXFCON_RXFLU;
  278. + lantiq_spi_writel(spi, val, SPI_RXFCON);
  279. +}
  280. +
  281. +static void tx_fifo_reset(const struct lantiq_spi *spi)
  282. +{
  283. + u32 val = 1 << SPI_TXFCON_TXFITL_S;
  284. + val |= SPI_TXFCON_TXFEN | SPI_TXFCON_TXFLU;
  285. + lantiq_spi_writel(spi, val, SPI_TXFCON);
  286. +}
  287. +
  288. +static void rx_fifo_flush(const struct lantiq_spi *spi)
  289. +{
  290. + lantiq_spi_maskl(spi, 0, SPI_RXFCON_RXFLU, SPI_RXFCON);
  291. +}
  292. +
  293. +static void tx_fifo_flush(const struct lantiq_spi *spi)
  294. +{
  295. + lantiq_spi_maskl(spi, 0, SPI_TXFCON_TXFLU, SPI_TXFCON);
  296. +}
  297. +
  298. +static int hw_is_busy(const struct lantiq_spi *spi)
  299. +{
  300. + u32 stat = lantiq_spi_readl(spi, SPI_STAT);
  301. + return stat & SPI_STAT_BSY;
  302. +}
  303. +
  304. +static void hw_enter_config_mode(const struct lantiq_spi *spi)
  305. +{
  306. + lantiq_spi_writel(spi, SPI_WHBSTATE_CLREN, SPI_WHBSTATE);
  307. +}
  308. +
  309. +static void hw_enter_active_mode(const struct lantiq_spi *spi)
  310. +{
  311. + lantiq_spi_writel(spi, SPI_WHBSTATE_SETEN, SPI_WHBSTATE);
  312. +}
  313. +
  314. +static void hw_setup_speed_hz(const struct lantiq_spi *spi,
  315. + unsigned int max_speed_hz)
  316. +{
  317. + u32 spi_clk, brt;
  318. +
  319. + /*
  320. + * SPI module clock is derived from FPI bus clock dependent on
  321. + * divider value in CLC.RMS which is always set to 1.
  322. + *
  323. + * f_SPI
  324. + * baudrate = --------------
  325. + * 2 * (BR + 1)
  326. + */
  327. + spi_clk = clk_get_rate(spi->fpi_clk) / 2;
  328. +
  329. + if (max_speed_hz > spi_clk)
  330. + brt = 0;
  331. + else
  332. + brt = spi_clk / max_speed_hz - 1;
  333. +
  334. + if (brt > 0xFFFF)
  335. + brt = 0xFFFF;
  336. +
  337. + dev_dbg(spi->dev, "spi_clk %u, max_speed_hz %u, brt %u\n",
  338. + spi_clk, max_speed_hz, brt);
  339. +
  340. + lantiq_spi_writel(spi, brt, SPI_BRT);
  341. +}
  342. +
  343. +static void hw_setup_bits_per_word(const struct lantiq_spi *spi,
  344. + unsigned int bits_per_word)
  345. +{
  346. + u32 bm;
  347. +
  348. + /* CON.BM value = bits_per_word - 1 */
  349. + bm = (bits_per_word - 1) << SPI_CON_BM_S;
  350. +
  351. + lantiq_spi_maskl(spi, SPI_CON_BM_M, bm, SPI_CON);
  352. +}
  353. +
  354. +static void hw_setup_clock_mode(const struct lantiq_spi *spi,
  355. + unsigned int mode)
  356. +{
  357. + u32 con_set = 0, con_clr = 0;
  358. +
  359. + /*
  360. + * SPI mode mapping in CON register:
  361. + * Mode CPOL CPHA CON.PO CON.PH
  362. + * 0 0 0 0 1
  363. + * 1 0 1 0 0
  364. + * 2 1 0 1 1
  365. + * 3 1 1 1 0
  366. + */
  367. + if (mode & SPI_CPHA)
  368. + con_clr |= SPI_CON_PH;
  369. + else
  370. + con_set |= SPI_CON_PH;
  371. +
  372. + if (mode & SPI_CPOL)
  373. + con_set |= SPI_CON_PO | SPI_CON_IDLE;
  374. + else
  375. + con_clr |= SPI_CON_PO | SPI_CON_IDLE;
  376. +
  377. + /* Set heading control */
  378. + if (mode & SPI_LSB_FIRST)
  379. + con_clr |= SPI_CON_HB;
  380. + else
  381. + con_set |= SPI_CON_HB;
  382. +
  383. + /* Set loopback mode */
  384. + if (mode & SPI_LOOP)
  385. + con_set |= SPI_CON_LB;
  386. + else
  387. + con_clr |= SPI_CON_LB;
  388. +
  389. + lantiq_spi_maskl(spi, con_clr, con_set, SPI_CON);
  390. +}
  391. +
  392. +static void lantiq_spi_hw_init(const struct lantiq_spi *spi)
  393. +{
  394. + /*
  395. + * Set clock divider for run mode to 1 to
  396. + * run at same frequency as FPI bus
  397. + */
  398. + lantiq_spi_writel(spi, 1 << SPI_CLC_RMC_S, SPI_CLC);
  399. +
  400. + /* Put controller into config mode */
  401. + hw_enter_config_mode(spi);
  402. +
  403. + /* Disable all interrupts */
  404. + lantiq_spi_writel(spi, 0, SPI_IRNEN);
  405. +
  406. + /* Clear error flags */
  407. + lantiq_spi_maskl(spi, 0, SPI_WHBSTATE_CLR_ERRORS, SPI_WHBSTATE);
  408. +
  409. + /* Enable error checking, disable TX/RX */
  410. + lantiq_spi_writel(spi, SPI_CON_RUEN | SPI_CON_AEN | SPI_CON_TEN |
  411. + SPI_CON_REN | SPI_CON_TXOFF | SPI_CON_RXOFF, SPI_CON);
  412. +
  413. + /* Setup default SPI mode */
  414. + hw_setup_bits_per_word(spi, spi->bits_per_word);
  415. + hw_setup_clock_mode(spi, SPI_MODE_0);
  416. +
  417. + /* Enable master mode and clear error flags */
  418. + lantiq_spi_writel(spi, SPI_WHBSTATE_SETMS | SPI_WHBSTATE_CLR_ERRORS,
  419. + SPI_WHBSTATE);
  420. +
  421. + /* Reset GPIO/CS registers */
  422. + lantiq_spi_writel(spi, 0, SPI_GPOCON);
  423. + lantiq_spi_writel(spi, 0xFF00, SPI_FPGO);
  424. +
  425. + /* Enable and flush FIFOs */
  426. + rx_fifo_reset(spi);
  427. + tx_fifo_reset(spi);
  428. +}
  429. +
  430. +static void hw_chipselect_set(struct lantiq_spi *spi, unsigned int cs)
  431. +{
  432. + u32 fgpo = (1 << (cs - 1 + SPI_FGPO_SETOUTN_S));
  433. + lantiq_spi_writel(spi, fgpo, SPI_FPGO);
  434. +}
  435. +
  436. +static void hw_chipselect_clear(struct lantiq_spi *spi, unsigned int cs)
  437. +{
  438. + u32 fgpo = (1 << (cs - 1));
  439. + lantiq_spi_writel(spi, fgpo, SPI_FPGO);
  440. +}
  441. +
  442. +static void hw_chipselect_init(struct lantiq_spi *spi, unsigned int cs,
  443. + unsigned int cs_high)
  444. +{
  445. + u32 gpocon;
  446. +
  447. + /* set GPO pin to CS mode */
  448. + gpocon = 1 << ((cs - 1) + SPI_GPOCON_ISCSBN_S);
  449. +
  450. + /* invert GPO pin */
  451. + if (cs_high)
  452. + gpocon |= 1 << (cs - 1);
  453. +
  454. + lantiq_spi_maskl(spi, 0, gpocon, SPI_GPOCON);
  455. +}
  456. +
  457. +static void chipselect_enable(struct spi_device *spidev)
  458. +{
  459. + struct lantiq_spi *spi = spi_master_get_devdata(spidev->master);
  460. + struct lantiq_spi_cstate *cstate = spi_get_ctldata(spidev);
  461. +
  462. + if (cstate->cs_gpio >= 0)
  463. + gpio_set_value(cstate->cs_gpio, spidev->mode & SPI_CS_HIGH);
  464. + else
  465. + hw_chipselect_clear(spi, spidev->chip_select);
  466. +
  467. + /* CS setup/recovery time */
  468. + if (spi->cs_delay)
  469. + ndelay(spi->cs_delay);
  470. +}
  471. +
  472. +static void chipselect_disable(struct spi_device *spidev)
  473. +{
  474. + struct lantiq_spi *spi = spi_master_get_devdata(spidev->master);
  475. + struct lantiq_spi_cstate *cstate = spi_get_ctldata(spidev);
  476. +
  477. + /* CS hold time */
  478. + if (spi->cs_delay)
  479. + ndelay(spi->cs_delay);
  480. +
  481. + if (cstate->cs_gpio >= 0)
  482. + gpio_set_value(cstate->cs_gpio, !(spidev->mode & SPI_CS_HIGH));
  483. + else
  484. + hw_chipselect_set(spi, spidev->chip_select);
  485. +
  486. + /* CS setup/recovery time */
  487. + if (spi->cs_delay)
  488. + ndelay(spi->cs_delay);
  489. +}
  490. +
  491. +static int lantiq_spi_setup(struct spi_device *spidev)
  492. +{
  493. + struct lantiq_spi *spi = spi_master_get_devdata(spidev->master);
  494. + struct lantiq_spi_cstate *cstate = spi_get_ctldata(spidev);
  495. + int err;
  496. +
  497. + if (!cstate) {
  498. + cstate = kzalloc(sizeof(*cstate), GFP_KERNEL);
  499. + if (!cstate)
  500. + return -ENOMEM;
  501. +
  502. + spi_set_ctldata(spidev, cstate);
  503. + cstate->cs_gpio = -ENOENT;
  504. +
  505. + if (spidev->cs_gpio >= 0) {
  506. + dev_dbg(spi->dev, "using chipselect %u on GPIO %d\n",
  507. + spidev->chip_select, spidev->cs_gpio);
  508. +
  509. + err = gpio_request(spidev->cs_gpio, dev_name(spi->dev));
  510. + if (err)
  511. + return err;
  512. +
  513. + gpio_direction_output(spidev->cs_gpio,
  514. + !(spidev->mode & SPI_CS_HIGH));
  515. +
  516. + cstate->cs_gpio = spidev->cs_gpio;
  517. + } else {
  518. + dev_dbg(spi->dev, "using internal chipselect %u\n",
  519. + spidev->chip_select);
  520. +
  521. + hw_chipselect_init(spi, spidev->chip_select,
  522. + spidev->mode & SPI_CS_HIGH);
  523. + hw_chipselect_set(spi, spidev->chip_select);
  524. + }
  525. + }
  526. +
  527. + return 0;
  528. +}
  529. +
  530. +static void lantiq_spi_cleanup(struct spi_device *spidev)
  531. +{
  532. + struct lantiq_spi_cstate *cstate = spi_get_ctldata(spidev);
  533. +
  534. + if (cstate->cs_gpio >= 0)
  535. + gpio_free(cstate->cs_gpio);
  536. +
  537. + kfree(cstate);
  538. + spi_set_ctldata(spidev, NULL);
  539. +}
  540. +
  541. +static void hw_setup_message(const struct lantiq_spi *spi,
  542. + struct spi_device *spidev)
  543. +{
  544. + const struct lantiq_spi_hwcfg *hwcfg = spi->hwcfg;
  545. +
  546. + hw_enter_config_mode(spi);
  547. + hw_setup_clock_mode(spi, spidev->mode);
  548. + hw_enter_active_mode(spi);
  549. +
  550. + /* Enable interrupts */
  551. + lantiq_spi_writel(spi, hwcfg->irnen_t | hwcfg->irnen_r | SPI_IRNEN_E,
  552. + SPI_IRNEN);
  553. +}
  554. +
  555. +static void hw_setup_transfer(struct lantiq_spi *spi, struct spi_device *spidev,
  556. + struct spi_transfer *t)
  557. +{
  558. + unsigned int speed_hz, bits_per_word;
  559. + u32 con;
  560. +
  561. + if (t->speed_hz)
  562. + speed_hz = t->speed_hz;
  563. + else
  564. + speed_hz = spidev->max_speed_hz;
  565. +
  566. + if (t->bits_per_word)
  567. + bits_per_word = t->bits_per_word;
  568. + else
  569. + bits_per_word = spidev->bits_per_word;
  570. +
  571. + if (bits_per_word != spi->bits_per_word ||
  572. + speed_hz != spi->speed_hz) {
  573. + hw_enter_config_mode(spi);
  574. + hw_setup_speed_hz(spi, speed_hz);
  575. + hw_setup_bits_per_word(spi, bits_per_word);
  576. + hw_enter_active_mode(spi);
  577. +
  578. + spi->speed_hz = speed_hz;
  579. + spi->bits_per_word = bits_per_word;
  580. + }
  581. +
  582. + /* Configure transmitter and receiver */
  583. + con = lantiq_spi_readl(spi, SPI_CON);
  584. + if (t->tx_buf)
  585. + con &= ~SPI_CON_TXOFF;
  586. + else
  587. + con |= SPI_CON_TXOFF;
  588. +
  589. + if (t->rx_buf)
  590. + con &= ~SPI_CON_RXOFF;
  591. + else
  592. + con |= SPI_CON_RXOFF;
  593. +
  594. + lantiq_spi_writel(spi, con, SPI_CON);
  595. +}
  596. +
  597. +static void hw_finish_message(const struct lantiq_spi *spi)
  598. +{
  599. + /* Disable interrupts */
  600. + lantiq_spi_writel(spi, 0, SPI_IRNEN);
  601. +
  602. + /* Disable transmitter and receiver */
  603. + lantiq_spi_maskl(spi, 0, SPI_CON_TXOFF | SPI_CON_RXOFF, SPI_CON);
  604. +}
  605. +
  606. +static void tx_fifo_write(struct lantiq_spi *spi)
  607. +{
  608. + const u8 *tx8;
  609. + const u16 *tx16;
  610. + const u32 *tx32;
  611. + u32 data;
  612. + unsigned int tx_free = tx_fifo_free(spi);
  613. +
  614. + while (spi->tx_todo && tx_free) {
  615. + switch (spi->bits_per_word) {
  616. + case 8:
  617. + tx8 = spi->tx;
  618. + data = *tx8;
  619. + spi->tx_todo--;
  620. + spi->tx++;
  621. + break;
  622. + case 16:
  623. + tx16 = (u16 *) spi->tx;
  624. + data = *tx16;
  625. + spi->tx_todo -= 2;
  626. + spi->tx += 2;
  627. + break;
  628. + case 32:
  629. + tx32 = (u32 *) spi->tx;
  630. + data = *tx32;
  631. + spi->tx_todo -= 4;
  632. + spi->tx += 4;
  633. + break;
  634. + default:
  635. + BUG();
  636. + }
  637. +
  638. + lantiq_spi_writel(spi, data, SPI_TB);
  639. + tx_free--;
  640. + }
  641. +}
  642. +
  643. +static void rx_fifo_read_full_duplex(struct lantiq_spi *spi)
  644. +{
  645. + u8 *rx8;
  646. + u16 *rx16;
  647. + u32 *rx32;
  648. + u32 data;
  649. + unsigned int rx_fill = rx_fifo_level(spi);
  650. +
  651. + while (rx_fill) {
  652. + data = lantiq_spi_readl(spi, SPI_RB);
  653. +
  654. + switch (spi->bits_per_word) {
  655. + case 8:
  656. + rx8 = spi->rx;
  657. + *rx8 = data;
  658. + spi->rx_todo--;
  659. + spi->rx++;
  660. + break;
  661. + case 16:
  662. + rx16 = (u16 *) spi->rx;
  663. + *rx16 = data;
  664. + spi->rx_todo -= 2;
  665. + spi->rx += 2;
  666. + break;
  667. + case 32:
  668. + rx32 = (u32 *) spi->rx;
  669. + *rx32 = data;
  670. + spi->rx_todo -= 4;
  671. + spi->rx += 4;
  672. + break;
  673. + default:
  674. + BUG();
  675. + }
  676. +
  677. + rx_fill--;
  678. + }
  679. +}
  680. +
  681. +static void rx_fifo_read_half_duplex(struct lantiq_spi *spi)
  682. +{
  683. + u32 data, *rx32;
  684. + u8 *rx8;
  685. + unsigned int rxbv, shift;
  686. + unsigned int rx_fill = rx_fifo_level(spi);
  687. +
  688. + /*
  689. + * In RX-only mode the bits per word value is ignored by HW. A value
  690. + * of 32 is used instead. Thus all 4 bytes per FIFO must be read.
  691. + * If remaining RX bytes are less than 4, the FIFO must be read
  692. + * differently. The amount of received and valid bytes is indicated
  693. + * by STAT.RXBV register value.
  694. + */
  695. + while (rx_fill) {
  696. + if (spi->rx_todo < 4) {
  697. + rxbv = (lantiq_spi_readl(spi, SPI_STAT) &
  698. + SPI_STAT_RXBV_M) >> SPI_STAT_RXBV_S;
  699. + data = lantiq_spi_readl(spi, SPI_RB);
  700. +
  701. + shift = (rxbv - 1) * 8;
  702. + rx8 = spi->rx;
  703. +
  704. + while (rxbv) {
  705. + *rx8++ = (data >> shift) & 0xFF;
  706. + rxbv--;
  707. + shift -= 8;
  708. + spi->rx_todo--;
  709. + spi->rx++;
  710. + }
  711. + } else {
  712. + data = lantiq_spi_readl(spi, SPI_RB);
  713. + rx32 = (u32 *) spi->rx;
  714. +
  715. + *rx32++ = data;
  716. + spi->rx_todo -= 4;
  717. + spi->rx += 4;
  718. + }
  719. + rx_fill--;
  720. + }
  721. +}
  722. +
  723. +static void rx_request(struct lantiq_spi *spi)
  724. +{
  725. + unsigned int rxreq, rxreq_max;
  726. +
  727. + /*
  728. + * To avoid receive overflows at high clocks it is better to request
  729. + * only the amount of bytes that fits into all FIFOs. This value
  730. + * depends on the FIFO size implemented in hardware.
  731. + */
  732. + rxreq = spi->rx_todo;
  733. + rxreq_max = rx_fifo_size(spi) * 4;
  734. + if (rxreq > rxreq_max)
  735. + rxreq = rxreq_max;
  736. +
  737. + lantiq_spi_writel(spi, rxreq, SPI_RXREQ);
  738. +}
  739. +
  740. +static irqreturn_t lantiq_spi_xmit_interrupt(int irq, void *data)
  741. +{
  742. + struct lantiq_spi *spi = data;
  743. +
  744. + /* handle possible interrupts after device initialization */
  745. + if (!spi->rx && !spi->tx)
  746. + return IRQ_HANDLED;
  747. +
  748. + if (spi->tx) {
  749. + if (spi->rx && spi->rx_todo)
  750. + rx_fifo_read_full_duplex(spi);
  751. +
  752. + if (spi->tx_todo)
  753. + tx_fifo_write(spi);
  754. + else
  755. + goto completed;
  756. + } else if (spi->rx) {
  757. + if (spi->rx_todo) {
  758. + rx_fifo_read_half_duplex(spi);
  759. +
  760. + if (spi->rx_todo)
  761. + rx_request(spi);
  762. + else
  763. + goto completed;
  764. + } else
  765. + goto completed;
  766. + }
  767. +
  768. + return IRQ_HANDLED;
  769. +
  770. +completed:
  771. + spi->status = 0;
  772. + complete(&spi->xfer_complete);
  773. +
  774. + return IRQ_HANDLED;
  775. +}
  776. +
  777. +static irqreturn_t lantiq_spi_err_interrupt(int irq, void *data)
  778. +{
  779. + struct lantiq_spi *spi = data;
  780. + u32 stat = lantiq_spi_readl(spi, SPI_STAT);
  781. +
  782. + if (stat & SPI_STAT_RUE)
  783. + dev_err(spi->dev, "receive underflow error\n");
  784. + if (stat & SPI_STAT_TUE)
  785. + dev_err(spi->dev, "transmit underflow error\n");
  786. + if (stat & SPI_STAT_RE)
  787. + dev_err(spi->dev, "receive overflow error\n");
  788. + if (stat & SPI_STAT_TE)
  789. + dev_err(spi->dev, "transmit overflow error\n");
  790. + if (stat & SPI_STAT_ME)
  791. + dev_err(spi->dev, "mode error\n");
  792. +
  793. + /* Disable all interrupts */
  794. + lantiq_spi_writel(spi, 0, SPI_IRNEN);
  795. +
  796. + /* Clear error flags */
  797. + lantiq_spi_maskl(spi, 0, SPI_WHBSTATE_CLR_ERRORS, SPI_WHBSTATE);
  798. +
  799. + /* flush FIFOs */
  800. + rx_fifo_flush(spi);
  801. + tx_fifo_flush(spi);
  802. +
  803. + /* set bad status so it can be retried */
  804. + spi->status = -EIO;
  805. + complete(&spi->xfer_complete);
  806. +
  807. + return IRQ_HANDLED;
  808. +}
  809. +
  810. +static int transfer_start(struct lantiq_spi *spi, struct spi_device *spidev,
  811. + struct spi_transfer *t)
  812. +{
  813. + unsigned long flags;
  814. +
  815. + spin_lock_irqsave(&spi->lock, flags);
  816. +
  817. + spi->tx = t->tx_buf;
  818. + spi->rx = t->rx_buf;
  819. + spi->status = -EINPROGRESS;
  820. +
  821. + if (t->tx_buf) {
  822. + spi->tx_todo = t->len;
  823. +
  824. + /* initially fill TX FIFO */
  825. + tx_fifo_write(spi);
  826. + }
  827. +
  828. + if (spi->rx) {
  829. + spi->rx_todo = t->len;
  830. +
  831. + /* start shift clock in RX-only mode */
  832. + if (!spi->tx)
  833. + rx_request(spi);
  834. + }
  835. +
  836. + spin_unlock_irqrestore(&spi->lock, flags);
  837. +
  838. + return 0;
  839. +}
  840. +
  841. +static int transfer_wait_finished(struct lantiq_spi *spi)
  842. +{
  843. + unsigned long timeout;
  844. +
  845. + /* wait for completion by interrupt */
  846. + timeout = wait_for_completion_timeout(&spi->xfer_complete,
  847. + msecs_to_jiffies(spi->timeout));
  848. + if (!timeout)
  849. + return -EIO;
  850. +
  851. + /* make sure that HW is idle */
  852. + timeout = jiffies + msecs_to_jiffies(spi->timeout);
  853. + do {
  854. + if (!hw_is_busy(spi))
  855. + return 0;
  856. +
  857. + cond_resched();
  858. + } while (!time_after_eq(jiffies, timeout));
  859. +
  860. + /* flush FIFOs on timeout */
  861. + rx_fifo_flush(spi);
  862. + tx_fifo_flush(spi);
  863. +
  864. + return -EIO;
  865. +}
  866. +
  867. +static int lantiq_spi_transfer_one_message(struct spi_master *master,
  868. + struct spi_message *msg)
  869. +{
  870. + struct lantiq_spi *spi = spi_master_get_devdata(master);
  871. + struct spi_device *spidev = msg->spi;
  872. + struct spi_transfer *t;
  873. + int status;
  874. + unsigned int cs_change = 1;
  875. +
  876. + hw_setup_message(spi, spidev);
  877. +
  878. + list_for_each_entry(t, &msg->transfers, transfer_list) {
  879. + reinit_completion(&spi->xfer_complete);
  880. +
  881. + hw_setup_transfer(spi, spidev, t);
  882. +
  883. + if (cs_change)
  884. + chipselect_enable(spidev);
  885. + cs_change = t->cs_change;
  886. +
  887. + status = transfer_start(spi, spidev, t);
  888. + if (status) {
  889. + dev_err(spi->dev, "failed to start transfer\n");
  890. + goto done;
  891. + }
  892. +
  893. + status = transfer_wait_finished(spi);
  894. + if (status) {
  895. + dev_err(spi->dev, "transfer timeout\n");
  896. + goto done;
  897. + }
  898. +
  899. + status = spi->status;
  900. + if (status) {
  901. + dev_err(spi->dev, "transfer failed\n");
  902. + goto done;
  903. + }
  904. +
  905. + msg->actual_length += t->len;
  906. +
  907. + if (t->delay_usecs)
  908. + udelay(t->delay_usecs);
  909. +
  910. + if (cs_change)
  911. + chipselect_disable(spidev);
  912. + }
  913. +
  914. +done:
  915. + msg->status = status;
  916. +
  917. + if (!(status == 0 && cs_change))
  918. + chipselect_disable(spidev);
  919. +
  920. + spi_finalize_current_message(master);
  921. + hw_finish_message(spi);
  922. +
  923. + return status;
  924. +}
  925. +
  926. +static int lantiq_spi_prepare_transfer(struct spi_master *master)
  927. +{
  928. + struct lantiq_spi *spi = spi_master_get_devdata(master);
  929. +
  930. + pm_runtime_get_sync(spi->dev);
  931. +
  932. + return 0;
  933. +}
  934. +
  935. +static int lantiq_spi_unprepare_transfer(struct spi_master *master)
  936. +{
  937. + struct lantiq_spi *spi = spi_master_get_devdata(master);
  938. +
  939. + pm_runtime_put(spi->dev);
  940. +
  941. + return 0;
  942. +}
  943. +
  944. +static const struct lantiq_spi_hwcfg spi_xway = {
  945. + .num_chipselect = 3,
  946. + .irnen_r = SPI_IRNEN_R_XWAY,
  947. + .irnen_t = SPI_IRNEN_T_XWAY,
  948. +};
  949. +
  950. +static const struct lantiq_spi_hwcfg spi_xrx = {
  951. + .num_chipselect = 6,
  952. + .irnen_r = SPI_IRNEN_R_XRX,
  953. + .irnen_t = SPI_IRNEN_T_XRX,
  954. +};
  955. +
  956. +static const struct of_device_id lantiq_spi_match[] = {
  957. + { .compatible = "lantiq,spi-xway", .data = &spi_xway, }, /* DEPRECATED */
  958. + { .compatible = "lantiq,ase-spi", .data = &spi_xway, },
  959. + { .compatible = "lantiq,xrx100-spi", .data = &spi_xrx, },
  960. + { .compatible = "lantiq,xrx200-spi", .data = &spi_xrx, },
  961. + { .compatible = "lantiq,xrx330-spi", .data = &spi_xrx, },
  962. + {},
  963. +};
  964. +MODULE_DEVICE_TABLE(of, lantiq_spi_match);
  965. +
  966. +static int lantiq_spi_probe(struct platform_device *pdev)
  967. +{
  968. + struct spi_master *master;
  969. + struct resource *res;
  970. + struct lantiq_spi *spi;
  971. + const struct lantiq_spi_hwcfg *hwcfg;
  972. + const struct of_device_id *match;
  973. + int err, rx_irq, tx_irq, err_irq;
  974. +
  975. + match = of_match_device(lantiq_spi_match, &pdev->dev);
  976. + if (!match) {
  977. + dev_err(&pdev->dev, "no device match\n");
  978. + return -EINVAL;
  979. + }
  980. + hwcfg = match->data;
  981. +
  982. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  983. + if (!res) {
  984. + dev_err(&pdev->dev, "failed to get resources\n");
  985. + return -ENXIO;
  986. + }
  987. +
  988. + rx_irq = platform_get_irq_byname(pdev, SPI_RX_IRQ_NAME);
  989. + if (rx_irq < 0) {
  990. + dev_err(&pdev->dev, "failed to get %s\n", SPI_RX_IRQ_NAME);
  991. + return -ENXIO;
  992. + }
  993. +
  994. + tx_irq = platform_get_irq_byname(pdev, SPI_TX_IRQ_NAME);
  995. + if (tx_irq < 0) {
  996. + dev_err(&pdev->dev, "failed to get %s\n", SPI_TX_IRQ_NAME);
  997. + return -ENXIO;
  998. + }
  999. +
  1000. + err_irq = platform_get_irq_byname(pdev, SPI_ERR_IRQ_NAME);
  1001. + if (err_irq < 0) {
  1002. + dev_err(&pdev->dev, "failed to get %s\n", SPI_ERR_IRQ_NAME);
  1003. + return -ENXIO;
  1004. + }
  1005. +
  1006. + master = spi_alloc_master(&pdev->dev, sizeof(struct lantiq_spi));
  1007. + if (!master)
  1008. + return -ENOMEM;
  1009. +
  1010. + spi = spi_master_get_devdata(master);
  1011. + spi->master = master;
  1012. + spi->dev = &pdev->dev;
  1013. + spi->hwcfg = hwcfg;
  1014. + platform_set_drvdata(pdev, spi);
  1015. +
  1016. + spi->regbase = devm_ioremap_resource(&pdev->dev, res);
  1017. + if (IS_ERR(spi->regbase)) {
  1018. + err = PTR_ERR(spi->regbase);
  1019. + goto err_master_put;
  1020. + }
  1021. +
  1022. + err = devm_request_irq(&pdev->dev, rx_irq, lantiq_spi_xmit_interrupt, 0,
  1023. + SPI_RX_IRQ_NAME, spi);
  1024. + if (err)
  1025. + goto err_master_put;
  1026. +
  1027. + err = devm_request_irq(&pdev->dev, tx_irq, lantiq_spi_xmit_interrupt, 0,
  1028. + SPI_TX_IRQ_NAME, spi);
  1029. + if (err)
  1030. + goto err_master_put;
  1031. +
  1032. + err = devm_request_irq(&pdev->dev, err_irq, lantiq_spi_err_interrupt, 0,
  1033. + SPI_ERR_IRQ_NAME, spi);
  1034. + if (err)
  1035. + goto err_master_put;
  1036. +
  1037. + spi->spi_clk = clk_get(&pdev->dev, NULL);
  1038. + if (IS_ERR(spi->spi_clk)) {
  1039. + err = PTR_ERR(spi->spi_clk);
  1040. + goto err_master_put;
  1041. + }
  1042. + clk_prepare_enable(spi->spi_clk);
  1043. +
  1044. + spi->fpi_clk = clk_get_fpi();
  1045. + if (IS_ERR(spi->fpi_clk)) {
  1046. + err = PTR_ERR(spi->fpi_clk);
  1047. + goto err_clk_disable;
  1048. + }
  1049. +
  1050. + init_completion(&spi->xfer_complete);
  1051. + spin_lock_init(&spi->lock);
  1052. + spi->timeout = 2000;
  1053. + spi->cs_delay = 100;
  1054. + spi->bits_per_word = 8;
  1055. + spi->speed_hz = 0;
  1056. +
  1057. + master->dev.of_node = pdev->dev.of_node;
  1058. + master->bus_num = 0;
  1059. + master->num_chipselect = hwcfg->num_chipselect;
  1060. + master->setup = lantiq_spi_setup;
  1061. + master->cleanup = lantiq_spi_cleanup;
  1062. + master->prepare_transfer_hardware = lantiq_spi_prepare_transfer;
  1063. + master->transfer_one_message = lantiq_spi_transfer_one_message;
  1064. + master->unprepare_transfer_hardware = lantiq_spi_unprepare_transfer;
  1065. + master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH |
  1066. + SPI_LOOP;
  1067. +
  1068. + lantiq_spi_hw_init(spi);
  1069. +
  1070. + err = spi_register_master(master);
  1071. + if (err) {
  1072. + dev_err(&pdev->dev, "failed to register spi_master\n");
  1073. + goto err_clk_put;
  1074. + }
  1075. +
  1076. + dev_info(&pdev->dev,
  1077. + "Lantiq SPI controller (TXFS %u, RXFS %u, DMA %u)\n",
  1078. + tx_fifo_size(spi), rx_fifo_size(spi), supports_dma(spi));
  1079. +
  1080. + return 0;
  1081. +
  1082. +err_clk_put:
  1083. + clk_put(spi->fpi_clk);
  1084. +err_clk_disable:
  1085. + clk_disable_unprepare(spi->spi_clk);
  1086. + clk_put(spi->spi_clk);
  1087. +err_master_put:
  1088. + platform_set_drvdata(pdev, NULL);
  1089. + spi_master_put(master);
  1090. +
  1091. + return err;
  1092. +}
  1093. +
  1094. +static int lantiq_spi_remove(struct platform_device *pdev)
  1095. +{
  1096. + struct lantiq_spi *spi = platform_get_drvdata(pdev);
  1097. + struct spi_master *master = spi->master;
  1098. +
  1099. + spi_unregister_master(master);
  1100. +
  1101. + lantiq_spi_writel(spi, 0, SPI_IRNEN);
  1102. + rx_fifo_flush(spi);
  1103. + tx_fifo_flush(spi);
  1104. + hw_enter_config_mode(spi);
  1105. +
  1106. + clk_disable_unprepare(spi->spi_clk);
  1107. + clk_put(spi->spi_clk);
  1108. + clk_put(spi->fpi_clk);
  1109. +
  1110. + platform_set_drvdata(pdev, NULL);
  1111. + spi_master_put(master);
  1112. +
  1113. + return 0;
  1114. +}
  1115. +
  1116. +static struct platform_driver lantiq_spi_driver = {
  1117. + .probe = lantiq_spi_probe,
  1118. + .remove = lantiq_spi_remove,
  1119. + .driver = {
  1120. + .name = "spi-lantiq",
  1121. + .owner = THIS_MODULE,
  1122. + .of_match_table = lantiq_spi_match,
  1123. + },
  1124. +};
  1125. +module_platform_driver(lantiq_spi_driver);
  1126. +
  1127. +MODULE_DESCRIPTION("Lantiq SPI controller driver");
  1128. +MODULE_AUTHOR("Daniel Schwierzeck <daniel.schwierzeck@gmail.com>");
  1129. +MODULE_LICENSE("GPL");
  1130. +MODULE_ALIAS("platform:spi-lantiq");