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