Commit | Line | Data |
---|---|---|
c942fddf | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
e0c9905e SS |
2 | /* |
3 | * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs | |
8083d6b8 | 4 | * Copyright (C) 2013, 2021 Intel Corporation |
e0c9905e SS |
5 | */ |
6 | ||
40917709 | 7 | #include <linux/atomic.h> |
8b136baa | 8 | #include <linux/bitops.h> |
40917709 | 9 | #include <linux/bug.h> |
5ce25705 AS |
10 | #include <linux/clk.h> |
11 | #include <linux/delay.h> | |
e0c9905e | 12 | #include <linux/device.h> |
0e476871 | 13 | #include <linux/dmaengine.h> |
cbfd6a21 | 14 | #include <linux/err.h> |
5ce25705 | 15 | #include <linux/gpio/consumer.h> |
e0c9905e | 16 | #include <linux/interrupt.h> |
40917709 | 17 | #include <linux/io.h> |
5ce25705 | 18 | #include <linux/ioport.h> |
40917709 AS |
19 | #include <linux/math64.h> |
20 | #include <linux/minmax.h> | |
40917709 | 21 | #include <linux/module.h> |
5ce25705 | 22 | #include <linux/pm_runtime.h> |
f2faa3ec | 23 | #include <linux/property.h> |
5ce25705 | 24 | #include <linux/slab.h> |
40917709 | 25 | #include <linux/types.h> |
0e476871 | 26 | |
e0c9905e | 27 | #include <linux/spi/spi.h> |
e0c9905e | 28 | |
e47f9230 | 29 | #include "internals.h" |
cd7bed00 | 30 | #include "spi-pxa2xx.h" |
e0c9905e | 31 | |
f1f640a9 VS |
32 | #define TIMOUT_DFLT 1000 |
33 | ||
b97c74bd | 34 | /* |
8083d6b8 AS |
35 | * For testing SSCR1 changes that require SSP restart, basically |
36 | * everything except the service and interrupt enables, the PXA270 developer | |
b97c74bd | 37 | * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this |
8083d6b8 AS |
38 | * list, but the PXA255 developer manual says all bits without really meaning |
39 | * the service and interrupt enables. | |
b97c74bd NF |
40 | */ |
41 | #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \ | |
8d94cc50 | 42 | | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \ |
b97c74bd NF |
43 | | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \ |
44 | | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \ | |
45 | | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \ | |
46 | | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) | |
8d94cc50 | 47 | |
e5262d05 WC |
48 | #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \ |
49 | | QUARK_X1000_SSCR1_EFWR \ | |
50 | | QUARK_X1000_SSCR1_RFT \ | |
51 | | QUARK_X1000_SSCR1_TFT \ | |
52 | | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) | |
53 | ||
7c7289a4 AS |
54 | #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \ |
55 | | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \ | |
56 | | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \ | |
57 | | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \ | |
58 | | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \ | |
59 | | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) | |
60 | ||
b5ec3986 AS |
61 | struct chip_data { |
62 | u32 cr1; | |
63 | u32 dds_rate; | |
64 | u32 threshold; | |
65 | u16 lpss_rx_threshold; | |
66 | u16 lpss_tx_threshold; | |
67 | }; | |
68 | ||
624ea72e JN |
69 | #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24) |
70 | #define LPSS_CS_CONTROL_SW_MODE BIT(0) | |
71 | #define LPSS_CS_CONTROL_CS_HIGH BIT(1) | |
8b136baa JN |
72 | #define LPSS_CAPS_CS_EN_SHIFT 9 |
73 | #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT) | |
a0d2642e | 74 | |
683f65de | 75 | #define LPSS_PRIV_CLOCK_GATE 0x38 |
78b435c9 AS |
76 | #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3 |
77 | #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3 | |
78 | #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_OFF 0x0 | |
683f65de | 79 | |
dccf7369 JN |
80 | struct lpss_config { |
81 | /* LPSS offset from drv_data->ioaddr */ | |
82 | unsigned offset; | |
83 | /* Register offsets from drv_data->lpss_base or -1 */ | |
84 | int reg_general; | |
85 | int reg_ssp; | |
86 | int reg_cs_ctrl; | |
8b136baa | 87 | int reg_capabilities; |
dccf7369 JN |
88 | /* FIFO thresholds */ |
89 | u32 rx_threshold; | |
90 | u32 tx_threshold_lo; | |
91 | u32 tx_threshold_hi; | |
c1e4a53c MW |
92 | /* Chip select control */ |
93 | unsigned cs_sel_shift; | |
94 | unsigned cs_sel_mask; | |
683f65de EG |
95 | /* Quirks */ |
96 | unsigned cs_clk_stays_gated : 1; | |
dccf7369 JN |
97 | }; |
98 | ||
99 | /* Keep these sorted with enum pxa_ssp_type */ | |
100 | static const struct lpss_config lpss_platforms[] = { | |
101 | { /* LPSS_LPT_SSP */ | |
102 | .offset = 0x800, | |
103 | .reg_general = 0x08, | |
104 | .reg_ssp = 0x0c, | |
105 | .reg_cs_ctrl = 0x18, | |
8b136baa | 106 | .reg_capabilities = -1, |
dccf7369 JN |
107 | .rx_threshold = 64, |
108 | .tx_threshold_lo = 160, | |
109 | .tx_threshold_hi = 224, | |
110 | }, | |
111 | { /* LPSS_BYT_SSP */ | |
112 | .offset = 0x400, | |
113 | .reg_general = 0x08, | |
114 | .reg_ssp = 0x0c, | |
115 | .reg_cs_ctrl = 0x18, | |
8b136baa | 116 | .reg_capabilities = -1, |
dccf7369 JN |
117 | .rx_threshold = 64, |
118 | .tx_threshold_lo = 160, | |
119 | .tx_threshold_hi = 224, | |
120 | }, | |
30f3a6ab MW |
121 | { /* LPSS_BSW_SSP */ |
122 | .offset = 0x400, | |
123 | .reg_general = 0x08, | |
124 | .reg_ssp = 0x0c, | |
125 | .reg_cs_ctrl = 0x18, | |
126 | .reg_capabilities = -1, | |
127 | .rx_threshold = 64, | |
128 | .tx_threshold_lo = 160, | |
129 | .tx_threshold_hi = 224, | |
130 | .cs_sel_shift = 2, | |
131 | .cs_sel_mask = 1 << 2, | |
30f3a6ab | 132 | }, |
34cadd9c JN |
133 | { /* LPSS_SPT_SSP */ |
134 | .offset = 0x200, | |
135 | .reg_general = -1, | |
136 | .reg_ssp = 0x20, | |
137 | .reg_cs_ctrl = 0x24, | |
66ec246e | 138 | .reg_capabilities = -1, |
34cadd9c JN |
139 | .rx_threshold = 1, |
140 | .tx_threshold_lo = 32, | |
141 | .tx_threshold_hi = 56, | |
142 | }, | |
b7c08cf8 JN |
143 | { /* LPSS_BXT_SSP */ |
144 | .offset = 0x200, | |
145 | .reg_general = -1, | |
146 | .reg_ssp = 0x20, | |
147 | .reg_cs_ctrl = 0x24, | |
148 | .reg_capabilities = 0xfc, | |
149 | .rx_threshold = 1, | |
150 | .tx_threshold_lo = 16, | |
151 | .tx_threshold_hi = 48, | |
c1e4a53c MW |
152 | .cs_sel_shift = 8, |
153 | .cs_sel_mask = 3 << 8, | |
6eefaee4 | 154 | .cs_clk_stays_gated = true, |
b7c08cf8 | 155 | }, |
fc0b2acc JN |
156 | { /* LPSS_CNL_SSP */ |
157 | .offset = 0x200, | |
158 | .reg_general = -1, | |
159 | .reg_ssp = 0x20, | |
160 | .reg_cs_ctrl = 0x24, | |
161 | .reg_capabilities = 0xfc, | |
162 | .rx_threshold = 1, | |
163 | .tx_threshold_lo = 32, | |
164 | .tx_threshold_hi = 56, | |
165 | .cs_sel_shift = 8, | |
166 | .cs_sel_mask = 3 << 8, | |
683f65de | 167 | .cs_clk_stays_gated = true, |
fc0b2acc | 168 | }, |
dccf7369 JN |
169 | }; |
170 | ||
171 | static inline const struct lpss_config | |
172 | *lpss_get_config(const struct driver_data *drv_data) | |
173 | { | |
174 | return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP]; | |
175 | } | |
176 | ||
a0d2642e MW |
177 | static bool is_lpss_ssp(const struct driver_data *drv_data) |
178 | { | |
03fbf488 JN |
179 | switch (drv_data->ssp_type) { |
180 | case LPSS_LPT_SSP: | |
181 | case LPSS_BYT_SSP: | |
30f3a6ab | 182 | case LPSS_BSW_SSP: |
34cadd9c | 183 | case LPSS_SPT_SSP: |
b7c08cf8 | 184 | case LPSS_BXT_SSP: |
fc0b2acc | 185 | case LPSS_CNL_SSP: |
03fbf488 JN |
186 | return true; |
187 | default: | |
188 | return false; | |
189 | } | |
a0d2642e MW |
190 | } |
191 | ||
e5262d05 WC |
192 | static bool is_quark_x1000_ssp(const struct driver_data *drv_data) |
193 | { | |
194 | return drv_data->ssp_type == QUARK_X1000_SSP; | |
195 | } | |
196 | ||
41c98841 AS |
197 | static bool is_mmp2_ssp(const struct driver_data *drv_data) |
198 | { | |
199 | return drv_data->ssp_type == MMP2_SSP; | |
200 | } | |
201 | ||
3fdb59cf AS |
202 | static bool is_mrfld_ssp(const struct driver_data *drv_data) |
203 | { | |
204 | return drv_data->ssp_type == MRFLD_SSP; | |
205 | } | |
206 | ||
1bed378c AS |
207 | static void pxa2xx_spi_update(const struct driver_data *drv_data, u32 reg, u32 mask, u32 value) |
208 | { | |
209 | if ((pxa2xx_spi_read(drv_data, reg) & mask) != value) | |
210 | pxa2xx_spi_write(drv_data, reg, value & mask); | |
211 | } | |
212 | ||
4fdb2424 WC |
213 | static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data) |
214 | { | |
215 | switch (drv_data->ssp_type) { | |
e5262d05 WC |
216 | case QUARK_X1000_SSP: |
217 | return QUARK_X1000_SSCR1_CHANGE_MASK; | |
7c7289a4 AS |
218 | case CE4100_SSP: |
219 | return CE4100_SSCR1_CHANGE_MASK; | |
4fdb2424 WC |
220 | default: |
221 | return SSCR1_CHANGE_MASK; | |
222 | } | |
223 | } | |
224 | ||
225 | static u32 | |
226 | pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data) | |
227 | { | |
228 | switch (drv_data->ssp_type) { | |
e5262d05 WC |
229 | case QUARK_X1000_SSP: |
230 | return RX_THRESH_QUARK_X1000_DFLT; | |
7c7289a4 AS |
231 | case CE4100_SSP: |
232 | return RX_THRESH_CE4100_DFLT; | |
4fdb2424 WC |
233 | default: |
234 | return RX_THRESH_DFLT; | |
235 | } | |
236 | } | |
237 | ||
238 | static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data) | |
239 | { | |
4fdb2424 WC |
240 | u32 mask; |
241 | ||
242 | switch (drv_data->ssp_type) { | |
e5262d05 WC |
243 | case QUARK_X1000_SSP: |
244 | mask = QUARK_X1000_SSSR_TFL_MASK; | |
245 | break; | |
7c7289a4 AS |
246 | case CE4100_SSP: |
247 | mask = CE4100_SSSR_TFL_MASK; | |
248 | break; | |
4fdb2424 WC |
249 | default: |
250 | mask = SSSR_TFL_MASK; | |
251 | break; | |
252 | } | |
253 | ||
6d380132 | 254 | return read_SSSR_bits(drv_data, mask) == mask; |
4fdb2424 WC |
255 | } |
256 | ||
257 | static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data, | |
258 | u32 *sccr1_reg) | |
259 | { | |
260 | u32 mask; | |
261 | ||
262 | switch (drv_data->ssp_type) { | |
e5262d05 WC |
263 | case QUARK_X1000_SSP: |
264 | mask = QUARK_X1000_SSCR1_RFT; | |
265 | break; | |
7c7289a4 AS |
266 | case CE4100_SSP: |
267 | mask = CE4100_SSCR1_RFT; | |
268 | break; | |
4fdb2424 WC |
269 | default: |
270 | mask = SSCR1_RFT; | |
271 | break; | |
272 | } | |
273 | *sccr1_reg &= ~mask; | |
274 | } | |
275 | ||
276 | static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data, | |
277 | u32 *sccr1_reg, u32 threshold) | |
278 | { | |
279 | switch (drv_data->ssp_type) { | |
e5262d05 WC |
280 | case QUARK_X1000_SSP: |
281 | *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold); | |
282 | break; | |
7c7289a4 AS |
283 | case CE4100_SSP: |
284 | *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold); | |
285 | break; | |
4fdb2424 WC |
286 | default: |
287 | *sccr1_reg |= SSCR1_RxTresh(threshold); | |
288 | break; | |
289 | } | |
290 | } | |
291 | ||
292 | static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data, | |
293 | u32 clk_div, u8 bits) | |
294 | { | |
295 | switch (drv_data->ssp_type) { | |
e5262d05 WC |
296 | case QUARK_X1000_SSP: |
297 | return clk_div | |
298 | | QUARK_X1000_SSCR0_Motorola | |
0c8ccd8b | 299 | | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits); |
4fdb2424 WC |
300 | default: |
301 | return clk_div | |
302 | | SSCR0_Motorola | |
303 | | SSCR0_DataSize(bits > 16 ? bits - 16 : bits) | |
4fdb2424 WC |
304 | | (bits > 16 ? SSCR0_EDSS : 0); |
305 | } | |
306 | } | |
307 | ||
a0d2642e MW |
308 | /* |
309 | * Read and write LPSS SSP private registers. Caller must first check that | |
310 | * is_lpss_ssp() returns true before these can be called. | |
311 | */ | |
312 | static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset) | |
313 | { | |
314 | WARN_ON(!drv_data->lpss_base); | |
315 | return readl(drv_data->lpss_base + offset); | |
316 | } | |
317 | ||
318 | static void __lpss_ssp_write_priv(struct driver_data *drv_data, | |
319 | unsigned offset, u32 value) | |
320 | { | |
321 | WARN_ON(!drv_data->lpss_base); | |
322 | writel(value, drv_data->lpss_base + offset); | |
323 | } | |
324 | ||
78b435c9 AS |
325 | static bool __lpss_ssp_update_priv(struct driver_data *drv_data, unsigned int offset, |
326 | u32 mask, u32 value) | |
327 | { | |
328 | u32 new, curr; | |
329 | ||
330 | curr = __lpss_ssp_read_priv(drv_data, offset); | |
331 | new = (curr & ~mask) | (value & mask); | |
332 | if (new == curr) | |
333 | return false; | |
334 | ||
335 | __lpss_ssp_write_priv(drv_data, offset, new); | |
336 | return true; | |
337 | } | |
338 | ||
a0d2642e MW |
339 | /* |
340 | * lpss_ssp_setup - perform LPSS SSP specific setup | |
341 | * @drv_data: pointer to the driver private data | |
342 | * | |
343 | * Perform LPSS SSP specific setup. This function must be called first if | |
344 | * one is going to use LPSS SSP private registers. | |
345 | */ | |
346 | static void lpss_ssp_setup(struct driver_data *drv_data) | |
347 | { | |
dccf7369 JN |
348 | const struct lpss_config *config; |
349 | u32 value; | |
a0d2642e | 350 | |
dccf7369 | 351 | config = lpss_get_config(drv_data); |
9e43c9a8 | 352 | drv_data->lpss_base = drv_data->ssp->mmio_base + config->offset; |
a0d2642e MW |
353 | |
354 | /* Enable software chip select control */ | |
78b435c9 AS |
355 | value = LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH; |
356 | __lpss_ssp_update_priv(drv_data, config->reg_cs_ctrl, value, value); | |
0054e28d MW |
357 | |
358 | /* Enable multiblock DMA transfers */ | |
51eea52d | 359 | if (drv_data->controller_info->enable_dma) { |
78b435c9 | 360 | __lpss_ssp_update_priv(drv_data, config->reg_ssp, BIT(0), BIT(0)); |
1de70612 | 361 | |
82ba2c2a | 362 | if (config->reg_general >= 0) { |
78b435c9 AS |
363 | value = LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE; |
364 | __lpss_ssp_update_priv(drv_data, config->reg_general, value, value); | |
82ba2c2a | 365 | } |
1de70612 | 366 | } |
a0d2642e MW |
367 | } |
368 | ||
d5898e19 | 369 | static void lpss_ssp_select_cs(struct spi_device *spi, |
c1e4a53c MW |
370 | const struct lpss_config *config) |
371 | { | |
d5898e19 JN |
372 | struct driver_data *drv_data = |
373 | spi_controller_get_devdata(spi->controller); | |
78b435c9 | 374 | u32 cs; |
c1e4a53c | 375 | |
78b435c9 AS |
376 | cs = spi_get_chipselect(spi, 0) << config->cs_sel_shift; |
377 | if (!__lpss_ssp_update_priv(drv_data, config->reg_cs_ctrl, config->cs_sel_mask, cs)) | |
c1e4a53c MW |
378 | return; |
379 | ||
78b435c9 AS |
380 | /* |
381 | * When switching another chip select output active the output must be | |
382 | * selected first and wait 2 ssp_clk cycles before changing state to | |
383 | * active. Otherwise a short glitch will occur on the previous chip | |
384 | * select since output select is latched but state control is not. | |
385 | */ | |
386 | ndelay(1000000000 / (drv_data->controller->max_speed_hz / 2)); | |
c1e4a53c MW |
387 | } |
388 | ||
d5898e19 | 389 | static void lpss_ssp_cs_control(struct spi_device *spi, bool enable) |
a0d2642e | 390 | { |
d5898e19 JN |
391 | struct driver_data *drv_data = |
392 | spi_controller_get_devdata(spi->controller); | |
dccf7369 | 393 | const struct lpss_config *config; |
78b435c9 | 394 | u32 mask; |
a0d2642e | 395 | |
dccf7369 JN |
396 | config = lpss_get_config(drv_data); |
397 | ||
c1e4a53c | 398 | if (enable) |
d5898e19 | 399 | lpss_ssp_select_cs(spi, config); |
c1e4a53c | 400 | |
78b435c9 | 401 | mask = LPSS_CS_CONTROL_CS_HIGH; |
aff2355d | 402 | __lpss_ssp_update_priv(drv_data, config->reg_cs_ctrl, mask, enable ? 0 : mask); |
683f65de | 403 | if (config->cs_clk_stays_gated) { |
683f65de EG |
404 | /* |
405 | * Changing CS alone when dynamic clock gating is on won't | |
406 | * actually flip CS at that time. This ruins SPI transfers | |
407 | * that specify delays, or have no data. Toggle the clock mode | |
408 | * to force on briefly to poke the CS pin to move. | |
409 | */ | |
78b435c9 AS |
410 | mask = LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK; |
411 | if (__lpss_ssp_update_priv(drv_data, LPSS_PRIV_CLOCK_GATE, mask, | |
412 | LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON)) | |
413 | __lpss_ssp_update_priv(drv_data, LPSS_PRIV_CLOCK_GATE, mask, | |
414 | LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_OFF); | |
683f65de | 415 | } |
a0d2642e MW |
416 | } |
417 | ||
d5898e19 | 418 | static void cs_assert(struct spi_device *spi) |
a7bb3909 | 419 | { |
d5898e19 JN |
420 | struct driver_data *drv_data = |
421 | spi_controller_get_devdata(spi->controller); | |
a7bb3909 | 422 | |
2a8626a9 | 423 | if (drv_data->ssp_type == CE4100_SSP) { |
9e264f3f | 424 | pxa2xx_spi_write(drv_data, SSSR, spi_get_chipselect(spi, 0)); |
2a8626a9 SAS |
425 | return; |
426 | } | |
427 | ||
7566bcc7 | 428 | if (is_lpss_ssp(drv_data)) |
d5898e19 | 429 | lpss_ssp_cs_control(spi, true); |
a7bb3909 EM |
430 | } |
431 | ||
d5898e19 | 432 | static void cs_deassert(struct spi_device *spi) |
a7bb3909 | 433 | { |
d5898e19 JN |
434 | struct driver_data *drv_data = |
435 | spi_controller_get_devdata(spi->controller); | |
104e51af | 436 | unsigned long timeout; |
a7bb3909 | 437 | |
2a8626a9 SAS |
438 | if (drv_data->ssp_type == CE4100_SSP) |
439 | return; | |
440 | ||
104e51af JN |
441 | /* Wait until SSP becomes idle before deasserting the CS */ |
442 | timeout = jiffies + msecs_to_jiffies(10); | |
443 | while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY && | |
444 | !time_after(jiffies, timeout)) | |
445 | cpu_relax(); | |
446 | ||
7566bcc7 | 447 | if (is_lpss_ssp(drv_data)) |
d5898e19 JN |
448 | lpss_ssp_cs_control(spi, false); |
449 | } | |
450 | ||
451 | static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level) | |
452 | { | |
453 | if (level) | |
454 | cs_deassert(spi); | |
455 | else | |
456 | cs_assert(spi); | |
a7bb3909 EM |
457 | } |
458 | ||
cd7bed00 | 459 | int pxa2xx_spi_flush(struct driver_data *drv_data) |
e0c9905e SS |
460 | { |
461 | unsigned long limit = loops_per_jiffy << 1; | |
462 | ||
e0c9905e | 463 | do { |
6d380132 | 464 | while (read_SSSR_bits(drv_data, SSSR_RNE)) |
c039dd27 JN |
465 | pxa2xx_spi_read(drv_data, SSDR); |
466 | } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit); | |
2a8626a9 | 467 | write_SSSR_CS(drv_data, SSSR_ROR); |
e0c9905e SS |
468 | |
469 | return limit; | |
470 | } | |
471 | ||
29d7e05c LR |
472 | static void pxa2xx_spi_off(struct driver_data *drv_data) |
473 | { | |
41c98841 AS |
474 | /* On MMP, disabling SSE seems to corrupt the Rx FIFO */ |
475 | if (is_mmp2_ssp(drv_data)) | |
29d7e05c LR |
476 | return; |
477 | ||
0c8ccd8b | 478 | pxa_ssp_disable(drv_data->ssp); |
29d7e05c LR |
479 | } |
480 | ||
8d94cc50 | 481 | static int null_writer(struct driver_data *drv_data) |
e0c9905e | 482 | { |
9708c121 | 483 | u8 n_bytes = drv_data->n_bytes; |
e0c9905e | 484 | |
4fdb2424 | 485 | if (pxa2xx_spi_txfifo_full(drv_data) |
8d94cc50 SS |
486 | || (drv_data->tx == drv_data->tx_end)) |
487 | return 0; | |
488 | ||
c039dd27 | 489 | pxa2xx_spi_write(drv_data, SSDR, 0); |
8d94cc50 SS |
490 | drv_data->tx += n_bytes; |
491 | ||
492 | return 1; | |
e0c9905e SS |
493 | } |
494 | ||
8d94cc50 | 495 | static int null_reader(struct driver_data *drv_data) |
e0c9905e | 496 | { |
9708c121 | 497 | u8 n_bytes = drv_data->n_bytes; |
e0c9905e | 498 | |
6d380132 | 499 | while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) { |
c039dd27 | 500 | pxa2xx_spi_read(drv_data, SSDR); |
e0c9905e SS |
501 | drv_data->rx += n_bytes; |
502 | } | |
8d94cc50 SS |
503 | |
504 | return drv_data->rx == drv_data->rx_end; | |
e0c9905e SS |
505 | } |
506 | ||
8d94cc50 | 507 | static int u8_writer(struct driver_data *drv_data) |
e0c9905e | 508 | { |
4fdb2424 | 509 | if (pxa2xx_spi_txfifo_full(drv_data) |
8d94cc50 SS |
510 | || (drv_data->tx == drv_data->tx_end)) |
511 | return 0; | |
512 | ||
c039dd27 | 513 | pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx)); |
8d94cc50 SS |
514 | ++drv_data->tx; |
515 | ||
516 | return 1; | |
e0c9905e SS |
517 | } |
518 | ||
8d94cc50 | 519 | static int u8_reader(struct driver_data *drv_data) |
e0c9905e | 520 | { |
6d380132 | 521 | while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) { |
c039dd27 | 522 | *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); |
e0c9905e SS |
523 | ++drv_data->rx; |
524 | } | |
8d94cc50 SS |
525 | |
526 | return drv_data->rx == drv_data->rx_end; | |
e0c9905e SS |
527 | } |
528 | ||
8d94cc50 | 529 | static int u16_writer(struct driver_data *drv_data) |
e0c9905e | 530 | { |
4fdb2424 | 531 | if (pxa2xx_spi_txfifo_full(drv_data) |
8d94cc50 SS |
532 | || (drv_data->tx == drv_data->tx_end)) |
533 | return 0; | |
534 | ||
c039dd27 | 535 | pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx)); |
8d94cc50 SS |
536 | drv_data->tx += 2; |
537 | ||
538 | return 1; | |
e0c9905e SS |
539 | } |
540 | ||
8d94cc50 | 541 | static int u16_reader(struct driver_data *drv_data) |
e0c9905e | 542 | { |
6d380132 | 543 | while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) { |
c039dd27 | 544 | *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); |
e0c9905e SS |
545 | drv_data->rx += 2; |
546 | } | |
8d94cc50 SS |
547 | |
548 | return drv_data->rx == drv_data->rx_end; | |
e0c9905e | 549 | } |
8d94cc50 SS |
550 | |
551 | static int u32_writer(struct driver_data *drv_data) | |
e0c9905e | 552 | { |
4fdb2424 | 553 | if (pxa2xx_spi_txfifo_full(drv_data) |
8d94cc50 SS |
554 | || (drv_data->tx == drv_data->tx_end)) |
555 | return 0; | |
556 | ||
c039dd27 | 557 | pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx)); |
8d94cc50 SS |
558 | drv_data->tx += 4; |
559 | ||
560 | return 1; | |
e0c9905e SS |
561 | } |
562 | ||
8d94cc50 | 563 | static int u32_reader(struct driver_data *drv_data) |
e0c9905e | 564 | { |
6d380132 | 565 | while (read_SSSR_bits(drv_data, SSSR_RNE) && drv_data->rx < drv_data->rx_end) { |
c039dd27 | 566 | *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR); |
e0c9905e SS |
567 | drv_data->rx += 4; |
568 | } | |
8d94cc50 SS |
569 | |
570 | return drv_data->rx == drv_data->rx_end; | |
e0c9905e SS |
571 | } |
572 | ||
579d3bb2 SAS |
573 | static void reset_sccr1(struct driver_data *drv_data) |
574 | { | |
e3aa9acc AS |
575 | u32 mask = drv_data->int_cr1 | drv_data->dma_cr1, threshold; |
576 | struct chip_data *chip; | |
577 | ||
578 | if (drv_data->controller->cur_msg) { | |
579 | chip = spi_get_ctldata(drv_data->controller->cur_msg->spi); | |
580 | threshold = chip->threshold; | |
581 | } else { | |
582 | threshold = 0; | |
583 | } | |
579d3bb2 | 584 | |
152bc19e AS |
585 | switch (drv_data->ssp_type) { |
586 | case QUARK_X1000_SSP: | |
e0a6512d | 587 | mask |= QUARK_X1000_SSCR1_RFT; |
152bc19e | 588 | break; |
7c7289a4 | 589 | case CE4100_SSP: |
e0a6512d | 590 | mask |= CE4100_SSCR1_RFT; |
7c7289a4 | 591 | break; |
152bc19e | 592 | default: |
e0a6512d | 593 | mask |= SSCR1_RFT; |
152bc19e AS |
594 | break; |
595 | } | |
e0a6512d | 596 | |
e3aa9acc | 597 | pxa2xx_spi_update(drv_data, SSCR1, mask, threshold); |
579d3bb2 SAS |
598 | } |
599 | ||
ab77fe89 | 600 | static void int_stop_and_reset(struct driver_data *drv_data) |
e0c9905e | 601 | { |
ab77fe89 | 602 | /* Clear and disable interrupts */ |
2a8626a9 | 603 | write_SSSR_CS(drv_data, drv_data->clear_sr); |
579d3bb2 | 604 | reset_sccr1(drv_data); |
ab77fe89 AS |
605 | if (pxa25x_ssp_comp(drv_data)) |
606 | return; | |
607 | ||
608 | pxa2xx_spi_write(drv_data, SSTO, 0); | |
609 | } | |
610 | ||
4761d2e7 | 611 | static void int_error_stop(struct driver_data *drv_data, const char *msg, int err) |
ab77fe89 AS |
612 | { |
613 | int_stop_and_reset(drv_data); | |
cd7bed00 | 614 | pxa2xx_spi_flush(drv_data); |
29d7e05c | 615 | pxa2xx_spi_off(drv_data); |
e0c9905e | 616 | |
c3dce24c | 617 | dev_err(drv_data->ssp->dev, "%s\n", msg); |
e0c9905e | 618 | |
4761d2e7 | 619 | drv_data->controller->cur_msg->status = err; |
51eea52d | 620 | spi_finalize_current_transfer(drv_data->controller); |
8d94cc50 | 621 | } |
5daa3ba0 | 622 | |
8d94cc50 SS |
623 | static void int_transfer_complete(struct driver_data *drv_data) |
624 | { | |
ab77fe89 | 625 | int_stop_and_reset(drv_data); |
e0c9905e | 626 | |
51eea52d | 627 | spi_finalize_current_transfer(drv_data->controller); |
8d94cc50 | 628 | } |
e0c9905e | 629 | |
8d94cc50 SS |
630 | static irqreturn_t interrupt_transfer(struct driver_data *drv_data) |
631 | { | |
6d380132 | 632 | u32 irq_status; |
e0c9905e | 633 | |
6d380132 AS |
634 | irq_status = read_SSSR_bits(drv_data, drv_data->mask_sr); |
635 | if (!(pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE)) | |
636 | irq_status &= ~SSSR_TFS; | |
e0c9905e | 637 | |
8d94cc50 | 638 | if (irq_status & SSSR_ROR) { |
8083d6b8 | 639 | int_error_stop(drv_data, "interrupt_transfer: FIFO overrun", -EIO); |
8d94cc50 SS |
640 | return IRQ_HANDLED; |
641 | } | |
e0c9905e | 642 | |
ec93cb6f | 643 | if (irq_status & SSSR_TUR) { |
8083d6b8 | 644 | int_error_stop(drv_data, "interrupt_transfer: FIFO underrun", -EIO); |
ec93cb6f LR |
645 | return IRQ_HANDLED; |
646 | } | |
647 | ||
8d94cc50 | 648 | if (irq_status & SSSR_TINT) { |
c039dd27 | 649 | pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT); |
8d94cc50 SS |
650 | if (drv_data->read(drv_data)) { |
651 | int_transfer_complete(drv_data); | |
652 | return IRQ_HANDLED; | |
653 | } | |
654 | } | |
e0c9905e | 655 | |
8083d6b8 | 656 | /* Drain Rx FIFO, Fill Tx FIFO and prevent overruns */ |
8d94cc50 SS |
657 | do { |
658 | if (drv_data->read(drv_data)) { | |
659 | int_transfer_complete(drv_data); | |
660 | return IRQ_HANDLED; | |
661 | } | |
662 | } while (drv_data->write(drv_data)); | |
e0c9905e | 663 | |
8d94cc50 SS |
664 | if (drv_data->read(drv_data)) { |
665 | int_transfer_complete(drv_data); | |
666 | return IRQ_HANDLED; | |
667 | } | |
e0c9905e | 668 | |
8d94cc50 | 669 | if (drv_data->tx == drv_data->tx_end) { |
579d3bb2 SAS |
670 | u32 bytes_left; |
671 | u32 sccr1_reg; | |
672 | ||
c039dd27 | 673 | sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1); |
579d3bb2 SAS |
674 | sccr1_reg &= ~SSCR1_TIE; |
675 | ||
676 | /* | |
8083d6b8 AS |
677 | * PXA25x_SSP has no timeout, set up Rx threshold for |
678 | * the remaining Rx bytes. | |
579d3bb2 | 679 | */ |
2a8626a9 | 680 | if (pxa25x_ssp_comp(drv_data)) { |
4fdb2424 | 681 | u32 rx_thre; |
579d3bb2 | 682 | |
4fdb2424 | 683 | pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg); |
579d3bb2 SAS |
684 | |
685 | bytes_left = drv_data->rx_end - drv_data->rx; | |
686 | switch (drv_data->n_bytes) { | |
687 | case 4: | |
2c183376 GS |
688 | bytes_left >>= 2; |
689 | break; | |
579d3bb2 SAS |
690 | case 2: |
691 | bytes_left >>= 1; | |
2c183376 | 692 | break; |
8d94cc50 | 693 | } |
579d3bb2 | 694 | |
4fdb2424 WC |
695 | rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data); |
696 | if (rx_thre > bytes_left) | |
697 | rx_thre = bytes_left; | |
579d3bb2 | 698 | |
4fdb2424 | 699 | pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre); |
e0c9905e | 700 | } |
c039dd27 | 701 | pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg); |
e0c9905e SS |
702 | } |
703 | ||
5daa3ba0 SS |
704 | /* We did something */ |
705 | return IRQ_HANDLED; | |
e0c9905e SS |
706 | } |
707 | ||
b0312482 JK |
708 | static void handle_bad_msg(struct driver_data *drv_data) |
709 | { | |
3bbdc083 | 710 | int_stop_and_reset(drv_data); |
29d7e05c | 711 | pxa2xx_spi_off(drv_data); |
b0312482 | 712 | |
c3dce24c | 713 | dev_err(drv_data->ssp->dev, "bad message state in interrupt handler\n"); |
b0312482 JK |
714 | } |
715 | ||
7d12e780 | 716 | static irqreturn_t ssp_int(int irq, void *dev_id) |
e0c9905e | 717 | { |
c7bec5ab | 718 | struct driver_data *drv_data = dev_id; |
7d94a505 | 719 | u32 sccr1_reg; |
49cbb1e0 SAS |
720 | u32 mask = drv_data->mask_sr; |
721 | u32 status; | |
722 | ||
7d94a505 MW |
723 | /* |
724 | * The IRQ might be shared with other peripherals so we must first | |
725 | * check that are we RPM suspended or not. If we are we assume that | |
726 | * the IRQ was not for us (we shouldn't be RPM suspended when the | |
727 | * interrupt is enabled). | |
728 | */ | |
c3dce24c | 729 | if (pm_runtime_suspended(drv_data->ssp->dev)) |
7d94a505 MW |
730 | return IRQ_NONE; |
731 | ||
269e4a41 MW |
732 | /* |
733 | * If the device is not yet in RPM suspended state and we get an | |
734 | * interrupt that is meant for another device, check if status bits | |
735 | * are all set to one. That means that the device is already | |
736 | * powered off. | |
737 | */ | |
c039dd27 | 738 | status = pxa2xx_spi_read(drv_data, SSSR); |
269e4a41 MW |
739 | if (status == ~0) |
740 | return IRQ_NONE; | |
741 | ||
c039dd27 | 742 | sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1); |
49cbb1e0 SAS |
743 | |
744 | /* Ignore possible writes if we don't need to write */ | |
745 | if (!(sccr1_reg & SSCR1_TIE)) | |
746 | mask &= ~SSSR_TFS; | |
747 | ||
02bc933e TJN |
748 | /* Ignore RX timeout interrupt if it is disabled */ |
749 | if (!(sccr1_reg & SSCR1_TINTE)) | |
750 | mask &= ~SSSR_TINT; | |
751 | ||
49cbb1e0 SAS |
752 | if (!(status & mask)) |
753 | return IRQ_NONE; | |
e0c9905e | 754 | |
e51e9b93 JK |
755 | pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1); |
756 | pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg); | |
5daa3ba0 | 757 | |
51eea52d | 758 | if (!drv_data->controller->cur_msg) { |
b0312482 | 759 | handle_bad_msg(drv_data); |
e0c9905e SS |
760 | /* Never fail */ |
761 | return IRQ_HANDLED; | |
762 | } | |
763 | ||
764 | return drv_data->transfer_handler(drv_data); | |
765 | } | |
766 | ||
e5262d05 | 767 | /* |
9df461ec AS |
768 | * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply |
769 | * input frequency by fractions of 2^24. It also has a divider by 5. | |
770 | * | |
771 | * There are formulas to get baud rate value for given input frequency and | |
772 | * divider parameters, such as DDS_CLK_RATE and SCR: | |
773 | * | |
774 | * Fsys = 200MHz | |
775 | * | |
776 | * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1) | |
777 | * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2) | |
778 | * | |
779 | * DDS_CLK_RATE either 2^n or 2^n / 5. | |
780 | * SCR is in range 0 .. 255 | |
781 | * | |
782 | * Divisor = 5^i * 2^j * 2 * k | |
783 | * i = [0, 1] i = 1 iff j = 0 or j > 3 | |
784 | * j = [0, 23] j = 0 iff i = 1 | |
785 | * k = [1, 256] | |
786 | * Special case: j = 0, i = 1: Divisor = 2 / 5 | |
787 | * | |
788 | * Accordingly to the specification the recommended values for DDS_CLK_RATE | |
789 | * are: | |
790 | * Case 1: 2^n, n = [0, 23] | |
791 | * Case 2: 2^24 * 2 / 5 (0x666666) | |
792 | * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333) | |
793 | * | |
794 | * In all cases the lowest possible value is better. | |
795 | * | |
796 | * The function calculates parameters for all cases and chooses the one closest | |
797 | * to the asked baud rate. | |
e5262d05 | 798 | */ |
9df461ec AS |
799 | static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds) |
800 | { | |
801 | unsigned long xtal = 200000000; | |
802 | unsigned long fref = xtal / 2; /* mandatory division by 2, | |
803 | see (2) */ | |
804 | /* case 3 */ | |
805 | unsigned long fref1 = fref / 2; /* case 1 */ | |
806 | unsigned long fref2 = fref * 2 / 5; /* case 2 */ | |
807 | unsigned long scale; | |
808 | unsigned long q, q1, q2; | |
809 | long r, r1, r2; | |
810 | u32 mul; | |
811 | ||
812 | /* Case 1 */ | |
813 | ||
814 | /* Set initial value for DDS_CLK_RATE */ | |
815 | mul = (1 << 24) >> 1; | |
816 | ||
817 | /* Calculate initial quot */ | |
3ad48062 | 818 | q1 = DIV_ROUND_UP(fref1, rate); |
9df461ec AS |
819 | |
820 | /* Scale q1 if it's too big */ | |
821 | if (q1 > 256) { | |
822 | /* Scale q1 to range [1, 512] */ | |
823 | scale = fls_long(q1 - 1); | |
824 | if (scale > 9) { | |
825 | q1 >>= scale - 9; | |
826 | mul >>= scale - 9; | |
e5262d05 | 827 | } |
9df461ec AS |
828 | |
829 | /* Round the result if we have a remainder */ | |
830 | q1 += q1 & 1; | |
831 | } | |
832 | ||
833 | /* Decrease DDS_CLK_RATE as much as we can without loss in precision */ | |
834 | scale = __ffs(q1); | |
835 | q1 >>= scale; | |
836 | mul >>= scale; | |
837 | ||
838 | /* Get the remainder */ | |
839 | r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate); | |
840 | ||
841 | /* Case 2 */ | |
842 | ||
3ad48062 | 843 | q2 = DIV_ROUND_UP(fref2, rate); |
9df461ec AS |
844 | r2 = abs(fref2 / q2 - rate); |
845 | ||
846 | /* | |
847 | * Choose the best between two: less remainder we have the better. We | |
848 | * can't go case 2 if q2 is greater than 256 since SCR register can | |
849 | * hold only values 0 .. 255. | |
850 | */ | |
851 | if (r2 >= r1 || q2 > 256) { | |
852 | /* case 1 is better */ | |
853 | r = r1; | |
854 | q = q1; | |
855 | } else { | |
856 | /* case 2 is better */ | |
857 | r = r2; | |
858 | q = q2; | |
859 | mul = (1 << 24) * 2 / 5; | |
e5262d05 WC |
860 | } |
861 | ||
3ad48062 | 862 | /* Check case 3 only if the divisor is big enough */ |
9df461ec AS |
863 | if (fref / rate >= 80) { |
864 | u64 fssp; | |
865 | u32 m; | |
866 | ||
867 | /* Calculate initial quot */ | |
3ad48062 | 868 | q1 = DIV_ROUND_UP(fref, rate); |
9df461ec AS |
869 | m = (1 << 24) / q1; |
870 | ||
871 | /* Get the remainder */ | |
872 | fssp = (u64)fref * m; | |
873 | do_div(fssp, 1 << 24); | |
874 | r1 = abs(fssp - rate); | |
875 | ||
876 | /* Choose this one if it suits better */ | |
877 | if (r1 < r) { | |
878 | /* case 3 is better */ | |
879 | q = 1; | |
880 | mul = m; | |
881 | } | |
882 | } | |
e5262d05 | 883 | |
9df461ec AS |
884 | *dds = mul; |
885 | return q - 1; | |
e5262d05 WC |
886 | } |
887 | ||
3343b7a6 | 888 | static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate) |
2f1a74e5 | 889 | { |
51eea52d | 890 | unsigned long ssp_clk = drv_data->controller->max_speed_hz; |
3343b7a6 MW |
891 | const struct ssp_device *ssp = drv_data->ssp; |
892 | ||
893 | rate = min_t(int, ssp_clk, rate); | |
2f1a74e5 | 894 | |
29f21337 FS |
895 | /* |
896 | * Calculate the divisor for the SCR (Serial Clock Rate), avoiding | |
8083d6b8 | 897 | * that the SSP transmission rate can be greater than the device rate. |
29f21337 | 898 | */ |
2a8626a9 | 899 | if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP) |
29f21337 | 900 | return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff; |
2f1a74e5 | 901 | else |
29f21337 | 902 | return (DIV_ROUND_UP(ssp_clk, rate) - 1) & 0xfff; |
2f1a74e5 | 903 | } |
904 | ||
e5262d05 | 905 | static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data, |
d2c2f6a4 | 906 | int rate) |
e5262d05 | 907 | { |
96579a4e | 908 | struct chip_data *chip = |
51eea52d | 909 | spi_get_ctldata(drv_data->controller->cur_msg->spi); |
025ffe88 | 910 | unsigned int clk_div; |
e5262d05 WC |
911 | |
912 | switch (drv_data->ssp_type) { | |
913 | case QUARK_X1000_SSP: | |
9df461ec | 914 | clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate); |
eecacf73 | 915 | break; |
e5262d05 | 916 | default: |
025ffe88 | 917 | clk_div = ssp_get_clk_div(drv_data, rate); |
eecacf73 | 918 | break; |
e5262d05 | 919 | } |
025ffe88 | 920 | return clk_div << 8; |
e5262d05 WC |
921 | } |
922 | ||
51eea52d | 923 | static bool pxa2xx_spi_can_dma(struct spi_controller *controller, |
b6ced294 JN |
924 | struct spi_device *spi, |
925 | struct spi_transfer *xfer) | |
926 | { | |
5c5de36d | 927 | struct driver_data *drv_data = spi_controller_get_devdata(controller); |
b6ced294 | 928 | |
5c5de36d | 929 | return drv_data->controller_info->enable_dma && |
b6ced294 | 930 | xfer->len <= MAX_DMA_LEN && |
5c5de36d | 931 | xfer->len >= drv_data->controller_info->dma_burst_size; |
b6ced294 JN |
932 | } |
933 | ||
51eea52d | 934 | static int pxa2xx_spi_transfer_one(struct spi_controller *controller, |
71293a60 | 935 | struct spi_device *spi, |
936 | struct spi_transfer *transfer) | |
e0c9905e | 937 | { |
51eea52d | 938 | struct driver_data *drv_data = spi_controller_get_devdata(controller); |
20f4c379 | 939 | struct chip_data *chip = spi_get_ctldata(spi); |
96579a4e | 940 | u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data); |
5c5de36d | 941 | u32 dma_thresh; |
bffc967e JN |
942 | u32 clk_div; |
943 | u8 bits; | |
944 | u32 speed; | |
9708c121 | 945 | u32 cr0; |
8d94cc50 | 946 | u32 cr1; |
7d1f1bf6 | 947 | int err; |
b6ced294 | 948 | int dma_mapped; |
e0c9905e | 949 | |
cd7bed00 | 950 | /* Check if we can DMA this transfer */ |
5c5de36d | 951 | if (transfer->len > MAX_DMA_LEN && drv_data->controller_info->enable_dma) { |
8083d6b8 | 952 | /* Warn ... we force this to PIO mode */ |
20f4c379 | 953 | dev_warn_ratelimited(&spi->dev, |
684a3ac7 AS |
954 | "DMA disabled for transfer length %u greater than %d\n", |
955 | transfer->len, MAX_DMA_LEN); | |
8d94cc50 SS |
956 | } |
957 | ||
e0c9905e | 958 | /* Setup the transfer state based on the type of transfer */ |
cd7bed00 | 959 | if (pxa2xx_spi_flush(drv_data) == 0) { |
748fbadf | 960 | dev_err(&spi->dev, "Flush failed\n"); |
d5898e19 | 961 | return -EIO; |
e0c9905e | 962 | } |
e0c9905e SS |
963 | drv_data->tx = (void *)transfer->tx_buf; |
964 | drv_data->tx_end = drv_data->tx + transfer->len; | |
965 | drv_data->rx = transfer->rx_buf; | |
966 | drv_data->rx_end = drv_data->rx + transfer->len; | |
9708c121 SS |
967 | |
968 | /* Change speed and bit per word on a per transfer */ | |
196b0e2c JN |
969 | bits = transfer->bits_per_word; |
970 | speed = transfer->speed_hz; | |
971 | ||
d2c2f6a4 | 972 | clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed); |
196b0e2c JN |
973 | |
974 | if (bits <= 8) { | |
975 | drv_data->n_bytes = 1; | |
44ec41b7 AS |
976 | drv_data->read = drv_data->rx ? u8_reader : null_reader; |
977 | drv_data->write = drv_data->tx ? u8_writer : null_writer; | |
196b0e2c JN |
978 | } else if (bits <= 16) { |
979 | drv_data->n_bytes = 2; | |
44ec41b7 AS |
980 | drv_data->read = drv_data->rx ? u16_reader : null_reader; |
981 | drv_data->write = drv_data->tx ? u16_writer : null_writer; | |
196b0e2c JN |
982 | } else if (bits <= 32) { |
983 | drv_data->n_bytes = 4; | |
44ec41b7 AS |
984 | drv_data->read = drv_data->rx ? u32_reader : null_reader; |
985 | drv_data->write = drv_data->tx ? u32_writer : null_writer; | |
9708c121 SS |
986 | } |
987 | ||
5c5de36d | 988 | dma_thresh = SSCR1_RxTresh(RX_THRESH_DFLT) | SSCR1_TxTresh(TX_THRESH_DFLT); |
e47f9230 | 989 | dma_mapped = spi_xfer_is_dma_mapped(controller, spi, transfer); |
b6ced294 | 990 | if (dma_mapped) { |
e0c9905e | 991 | /* Ensure we have the correct interrupt handler */ |
cd7bed00 MW |
992 | drv_data->transfer_handler = pxa2xx_spi_dma_transfer; |
993 | ||
d5898e19 JN |
994 | err = pxa2xx_spi_dma_prepare(drv_data, transfer); |
995 | if (err) | |
996 | return err; | |
e0c9905e | 997 | |
8d94cc50 SS |
998 | /* Clear status and start DMA engine */ |
999 | cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1; | |
c039dd27 | 1000 | pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr); |
cd7bed00 MW |
1001 | |
1002 | pxa2xx_spi_dma_start(drv_data); | |
e0c9905e SS |
1003 | } else { |
1004 | /* Ensure we have the correct interrupt handler */ | |
1005 | drv_data->transfer_handler = interrupt_transfer; | |
1006 | ||
8d94cc50 SS |
1007 | /* Clear status */ |
1008 | cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1; | |
2a8626a9 | 1009 | write_SSSR_CS(drv_data, drv_data->clear_sr); |
8d94cc50 SS |
1010 | } |
1011 | ||
ee03672d JN |
1012 | /* NOTE: PXA25x_SSP _could_ use external clocking ... */ |
1013 | cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits); | |
1014 | if (!pxa25x_ssp_comp(drv_data)) | |
20f4c379 | 1015 | dev_dbg(&spi->dev, "%u Hz actual, %s\n", |
51eea52d | 1016 | controller->max_speed_hz |
ee03672d | 1017 | / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)), |
b6ced294 | 1018 | dma_mapped ? "DMA" : "PIO"); |
ee03672d | 1019 | else |
20f4c379 | 1020 | dev_dbg(&spi->dev, "%u Hz actual, %s\n", |
51eea52d | 1021 | controller->max_speed_hz / 2 |
ee03672d | 1022 | / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)), |
b6ced294 | 1023 | dma_mapped ? "DMA" : "PIO"); |
ee03672d | 1024 | |
a0d2642e | 1025 | if (is_lpss_ssp(drv_data)) { |
1bed378c AS |
1026 | pxa2xx_spi_update(drv_data, SSIRF, GENMASK(7, 0), chip->lpss_rx_threshold); |
1027 | pxa2xx_spi_update(drv_data, SSITF, GENMASK(15, 0), chip->lpss_tx_threshold); | |
a0d2642e MW |
1028 | } |
1029 | ||
3fdb59cf | 1030 | if (is_mrfld_ssp(drv_data)) { |
70252440 | 1031 | u32 mask = SFIFOTT_RFT | SFIFOTT_TFT; |
3fdb59cf AS |
1032 | u32 thresh = 0; |
1033 | ||
1034 | thresh |= SFIFOTT_RxThresh(chip->lpss_rx_threshold); | |
1035 | thresh |= SFIFOTT_TxThresh(chip->lpss_tx_threshold); | |
1036 | ||
70252440 | 1037 | pxa2xx_spi_update(drv_data, SFIFOTT, mask, thresh); |
3fdb59cf AS |
1038 | } |
1039 | ||
1bed378c AS |
1040 | if (is_quark_x1000_ssp(drv_data)) |
1041 | pxa2xx_spi_update(drv_data, DDS_RATE, GENMASK(23, 0), chip->dds_rate); | |
e5262d05 | 1042 | |
0c8ccd8b AS |
1043 | /* Stop the SSP */ |
1044 | if (!is_mmp2_ssp(drv_data)) | |
1045 | pxa_ssp_disable(drv_data->ssp); | |
1046 | ||
1047 | if (!pxa25x_ssp_comp(drv_data)) | |
35bf074b | 1048 | pxa2xx_spi_write(drv_data, SSTO, TIMOUT_DFLT); |
0c8ccd8b | 1049 | |
8083d6b8 | 1050 | /* First set CR1 without interrupt and service enables */ |
1bed378c AS |
1051 | pxa2xx_spi_update(drv_data, SSCR1, change_mask, cr1); |
1052 | ||
8083d6b8 | 1053 | /* See if we need to reload the configuration registers */ |
1bed378c | 1054 | pxa2xx_spi_update(drv_data, SSCR0, GENMASK(31, 0), cr0); |
b97c74bd | 1055 | |
0c8ccd8b AS |
1056 | /* Restart the SSP */ |
1057 | pxa_ssp_enable(drv_data->ssp); | |
1058 | ||
41c98841 | 1059 | if (is_mmp2_ssp(drv_data)) { |
6d380132 | 1060 | u8 tx_level = read_SSSR_bits(drv_data, SSSR_TFL_MASK) >> 8; |
82391856 LR |
1061 | |
1062 | if (tx_level) { | |
8083d6b8 | 1063 | /* On MMP2, flipping SSE doesn't to empty Tx FIFO. */ |
684a3ac7 | 1064 | dev_warn(&spi->dev, "%u bytes of garbage in Tx FIFO!\n", tx_level); |
82391856 LR |
1065 | if (tx_level > transfer->len) |
1066 | tx_level = transfer->len; | |
1067 | drv_data->tx += tx_level; | |
1068 | } | |
1069 | } | |
1070 | ||
60ba4431 | 1071 | if (spi_controller_is_target(controller)) { |
ec93cb6f LR |
1072 | while (drv_data->write(drv_data)) |
1073 | ; | |
77d33897 LR |
1074 | if (drv_data->gpiod_ready) { |
1075 | gpiod_set_value(drv_data->gpiod_ready, 1); | |
1076 | udelay(1); | |
1077 | gpiod_set_value(drv_data->gpiod_ready, 0); | |
1078 | } | |
ec93cb6f LR |
1079 | } |
1080 | ||
d5898e19 JN |
1081 | /* |
1082 | * Release the data by enabling service requests and interrupts, | |
8083d6b8 | 1083 | * without changing any mode bits. |
d5898e19 | 1084 | */ |
c039dd27 | 1085 | pxa2xx_spi_write(drv_data, SSCR1, cr1); |
d5898e19 JN |
1086 | |
1087 | return 1; | |
e0c9905e SS |
1088 | } |
1089 | ||
60ba4431 | 1090 | static int pxa2xx_spi_target_abort(struct spi_controller *controller) |
ec93cb6f | 1091 | { |
51eea52d | 1092 | struct driver_data *drv_data = spi_controller_get_devdata(controller); |
ec93cb6f | 1093 | |
4761d2e7 | 1094 | int_error_stop(drv_data, "transfer aborted", -EINTR); |
ec93cb6f LR |
1095 | |
1096 | return 0; | |
1097 | } | |
1098 | ||
51eea52d | 1099 | static void pxa2xx_spi_handle_err(struct spi_controller *controller, |
d5898e19 | 1100 | struct spi_message *msg) |
e0c9905e | 1101 | { |
51eea52d | 1102 | struct driver_data *drv_data = spi_controller_get_devdata(controller); |
e0c9905e | 1103 | |
3bbdc083 AS |
1104 | int_stop_and_reset(drv_data); |
1105 | ||
d5898e19 | 1106 | /* Disable the SSP */ |
29d7e05c | 1107 | pxa2xx_spi_off(drv_data); |
e0c9905e | 1108 | |
d5898e19 JN |
1109 | /* |
1110 | * Stop the DMA if running. Note DMA callback handler may have unset | |
1111 | * the dma_running already, which is fine as stopping is not needed | |
1112 | * then but we shouldn't rely this flag for anything else than | |
1113 | * stopping. For instance to differentiate between PIO and DMA | |
1114 | * transfers. | |
1115 | */ | |
1116 | if (atomic_read(&drv_data->dma_running)) | |
1117 | pxa2xx_spi_dma_stop(drv_data); | |
e0c9905e SS |
1118 | } |
1119 | ||
51eea52d | 1120 | static int pxa2xx_spi_unprepare_transfer(struct spi_controller *controller) |
7d94a505 | 1121 | { |
51eea52d | 1122 | struct driver_data *drv_data = spi_controller_get_devdata(controller); |
7d94a505 MW |
1123 | |
1124 | /* Disable the SSP now */ | |
29d7e05c | 1125 | pxa2xx_spi_off(drv_data); |
7d94a505 | 1126 | |
7d94a505 MW |
1127 | return 0; |
1128 | } | |
1129 | ||
e0c9905e SS |
1130 | static int setup(struct spi_device *spi) |
1131 | { | |
e0c9905e | 1132 | struct chip_data *chip; |
dccf7369 | 1133 | const struct lpss_config *config; |
3cc7b0e3 JN |
1134 | struct driver_data *drv_data = |
1135 | spi_controller_get_devdata(spi->controller); | |
a0d2642e MW |
1136 | uint tx_thres, tx_hi_thres, rx_thres; |
1137 | ||
e5262d05 WC |
1138 | switch (drv_data->ssp_type) { |
1139 | case QUARK_X1000_SSP: | |
1140 | tx_thres = TX_THRESH_QUARK_X1000_DFLT; | |
1141 | tx_hi_thres = 0; | |
1142 | rx_thres = RX_THRESH_QUARK_X1000_DFLT; | |
1143 | break; | |
3fdb59cf AS |
1144 | case MRFLD_SSP: |
1145 | tx_thres = TX_THRESH_MRFLD_DFLT; | |
1146 | tx_hi_thres = 0; | |
1147 | rx_thres = RX_THRESH_MRFLD_DFLT; | |
1148 | break; | |
7c7289a4 AS |
1149 | case CE4100_SSP: |
1150 | tx_thres = TX_THRESH_CE4100_DFLT; | |
1151 | tx_hi_thres = 0; | |
1152 | rx_thres = RX_THRESH_CE4100_DFLT; | |
1153 | break; | |
03fbf488 JN |
1154 | case LPSS_LPT_SSP: |
1155 | case LPSS_BYT_SSP: | |
30f3a6ab | 1156 | case LPSS_BSW_SSP: |
34cadd9c | 1157 | case LPSS_SPT_SSP: |
b7c08cf8 | 1158 | case LPSS_BXT_SSP: |
fc0b2acc | 1159 | case LPSS_CNL_SSP: |
dccf7369 JN |
1160 | config = lpss_get_config(drv_data); |
1161 | tx_thres = config->tx_threshold_lo; | |
1162 | tx_hi_thres = config->tx_threshold_hi; | |
1163 | rx_thres = config->rx_threshold; | |
e5262d05 WC |
1164 | break; |
1165 | default: | |
a0d2642e | 1166 | tx_hi_thres = 0; |
60ba4431 | 1167 | if (spi_controller_is_target(drv_data->controller)) { |
ec93cb6f LR |
1168 | tx_thres = 1; |
1169 | rx_thres = 2; | |
1170 | } else { | |
1171 | tx_thres = TX_THRESH_DFLT; | |
1172 | rx_thres = RX_THRESH_DFLT; | |
1173 | } | |
e5262d05 | 1174 | break; |
a0d2642e | 1175 | } |
e0c9905e | 1176 | |
df3431fd AS |
1177 | if (drv_data->ssp_type == CE4100_SSP) { |
1178 | if (spi_get_chipselect(spi, 0) > 4) { | |
1179 | dev_err(&spi->dev, "failed setup: cs number must not be > 4.\n"); | |
1180 | return -EINVAL; | |
1181 | } | |
1182 | } | |
1183 | ||
8083d6b8 | 1184 | /* Only allocate on the first setup */ |
e0c9905e | 1185 | chip = spi_get_ctldata(spi); |
8d94cc50 | 1186 | if (!chip) { |
e0c9905e | 1187 | chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); |
9deae459 | 1188 | if (!chip) |
e0c9905e | 1189 | return -ENOMEM; |
e0c9905e SS |
1190 | } |
1191 | ||
8393961c | 1192 | chip->cr1 = 0; |
60ba4431 | 1193 | if (spi_controller_is_target(drv_data->controller)) { |
ec93cb6f LR |
1194 | chip->cr1 |= SSCR1_SCFR; |
1195 | chip->cr1 |= SSCR1_SCLKDIR; | |
1196 | chip->cr1 |= SSCR1_SFRMDIR; | |
1197 | chip->cr1 |= SSCR1_SPH; | |
1198 | } | |
e0c9905e | 1199 | |
3fdb59cf AS |
1200 | if (is_lpss_ssp(drv_data)) { |
1201 | chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres); | |
1202 | chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres) | | |
1203 | SSITF_TxHiThresh(tx_hi_thres); | |
1204 | } | |
1205 | ||
1206 | if (is_mrfld_ssp(drv_data)) { | |
1207 | chip->lpss_rx_threshold = rx_thres; | |
1208 | chip->lpss_tx_threshold = tx_thres; | |
1209 | } | |
a0d2642e | 1210 | |
e5262d05 WC |
1211 | switch (drv_data->ssp_type) { |
1212 | case QUARK_X1000_SSP: | |
1213 | chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres) | |
1214 | & QUARK_X1000_SSCR1_RFT) | |
1215 | | (QUARK_X1000_SSCR1_TxTresh(tx_thres) | |
1216 | & QUARK_X1000_SSCR1_TFT); | |
1217 | break; | |
7c7289a4 AS |
1218 | case CE4100_SSP: |
1219 | chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) | | |
1220 | (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT); | |
1221 | break; | |
e5262d05 WC |
1222 | default: |
1223 | chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) | | |
1224 | (SSCR1_TxTresh(tx_thres) & SSCR1_TFT); | |
1225 | break; | |
1226 | } | |
1227 | ||
7f6ee1ad | 1228 | chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH); |
eb743ec6 AS |
1229 | chip->cr1 |= ((spi->mode & SPI_CPHA) ? SSCR1_SPH : 0) | |
1230 | ((spi->mode & SPI_CPOL) ? SSCR1_SPO : 0); | |
e0c9905e | 1231 | |
b833172f MW |
1232 | if (spi->mode & SPI_LOOP) |
1233 | chip->cr1 |= SSCR1_LBM; | |
1234 | ||
e0c9905e SS |
1235 | spi_set_ctldata(spi, chip); |
1236 | ||
31455bbd | 1237 | return 0; |
e0c9905e SS |
1238 | } |
1239 | ||
0ffa0285 | 1240 | static void cleanup(struct spi_device *spi) |
e0c9905e | 1241 | { |
0ffa0285 | 1242 | struct chip_data *chip = spi_get_ctldata(spi); |
a7bb3909 | 1243 | |
e0c9905e SS |
1244 | kfree(chip); |
1245 | } | |
1246 | ||
51eea52d | 1247 | static int pxa2xx_spi_fw_translate_cs(struct spi_controller *controller, |
3cc7b0e3 | 1248 | unsigned int cs) |
0c27d9cf | 1249 | { |
51eea52d | 1250 | struct driver_data *drv_data = spi_controller_get_devdata(controller); |
0c27d9cf | 1251 | |
75bfdcca AS |
1252 | switch (drv_data->ssp_type) { |
1253 | /* | |
1254 | * For some of Intel Atoms the ACPI DeviceSelection used by the Windows | |
1255 | * driver starts from 1 instead of 0 so translate it here to match what | |
1256 | * Linux expects. | |
1257 | */ | |
1258 | case LPSS_BYT_SSP: | |
1259 | case LPSS_BSW_SSP: | |
1260 | return cs - 1; | |
0c27d9cf | 1261 | |
75bfdcca AS |
1262 | default: |
1263 | return cs; | |
0c27d9cf | 1264 | } |
0c27d9cf MW |
1265 | } |
1266 | ||
b2662a16 DV |
1267 | static size_t pxa2xx_spi_max_dma_transfer_size(struct spi_device *spi) |
1268 | { | |
1269 | return MAX_DMA_LEN; | |
1270 | } | |
1271 | ||
9a8fc292 AS |
1272 | int pxa2xx_spi_probe(struct device *dev, struct ssp_device *ssp, |
1273 | struct pxa2xx_spi_controller *platform_info) | |
e0c9905e | 1274 | { |
51eea52d | 1275 | struct spi_controller *controller; |
65a00a20 | 1276 | struct driver_data *drv_data; |
8b136baa | 1277 | const struct lpss_config *config; |
778c12e6 | 1278 | int status; |
c039dd27 | 1279 | u32 tmp; |
e0c9905e | 1280 | |
60ba4431 YY |
1281 | if (platform_info->is_target) |
1282 | controller = devm_spi_alloc_target(dev, sizeof(*drv_data)); | |
ec93cb6f | 1283 | else |
60ba4431 | 1284 | controller = devm_spi_alloc_host(dev, sizeof(*drv_data)); |
a2fca8f2 AS |
1285 | if (!controller) |
1286 | return dev_err_probe(dev, -ENOMEM, "cannot alloc spi_controller\n"); | |
ec93cb6f | 1287 | |
51eea52d LR |
1288 | drv_data = spi_controller_get_devdata(controller); |
1289 | drv_data->controller = controller; | |
1290 | drv_data->controller_info = platform_info; | |
2f1a74e5 | 1291 | drv_data->ssp = ssp; |
e0c9905e | 1292 | |
12baee68 | 1293 | device_set_node(&controller->dev, dev_fwnode(dev)); |
94acf807 | 1294 | |
8083d6b8 | 1295 | /* The spi->mode bits understood by this driver: */ |
51eea52d LR |
1296 | controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP; |
1297 | ||
1298 | controller->bus_num = ssp->port_id; | |
1299 | controller->dma_alignment = DMA_ALIGNMENT; | |
1300 | controller->cleanup = cleanup; | |
1301 | controller->setup = setup; | |
1302 | controller->set_cs = pxa2xx_spi_set_cs; | |
1303 | controller->transfer_one = pxa2xx_spi_transfer_one; | |
60ba4431 | 1304 | controller->target_abort = pxa2xx_spi_target_abort; |
51eea52d LR |
1305 | controller->handle_err = pxa2xx_spi_handle_err; |
1306 | controller->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer; | |
1307 | controller->fw_translate_cs = pxa2xx_spi_fw_translate_cs; | |
1308 | controller->auto_runtime_pm = true; | |
1309 | controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX; | |
e0c9905e | 1310 | |
2f1a74e5 | 1311 | drv_data->ssp_type = ssp->type; |
e0c9905e | 1312 | |
2a8626a9 | 1313 | if (pxa25x_ssp_comp(drv_data)) { |
e5262d05 WC |
1314 | switch (drv_data->ssp_type) { |
1315 | case QUARK_X1000_SSP: | |
51eea52d | 1316 | controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); |
e5262d05 WC |
1317 | break; |
1318 | default: | |
51eea52d | 1319 | controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16); |
e5262d05 WC |
1320 | break; |
1321 | } | |
1322 | ||
e0c9905e SS |
1323 | drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE; |
1324 | drv_data->dma_cr1 = 0; | |
1325 | drv_data->clear_sr = SSSR_ROR; | |
1326 | drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR; | |
1327 | } else { | |
51eea52d | 1328 | controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); |
e0c9905e | 1329 | drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE; |
5928808e | 1330 | drv_data->dma_cr1 = DEFAULT_DMA_CR1; |
e0c9905e | 1331 | drv_data->clear_sr = SSSR_ROR | SSSR_TINT; |
ec93cb6f LR |
1332 | drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS |
1333 | | SSSR_ROR | SSSR_TUR; | |
e0c9905e SS |
1334 | } |
1335 | ||
49cbb1e0 SAS |
1336 | status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev), |
1337 | drv_data); | |
a2fca8f2 AS |
1338 | if (status < 0) |
1339 | return dev_err_probe(dev, status, "cannot get IRQ %d\n", ssp->irq); | |
e0c9905e SS |
1340 | |
1341 | /* Setup DMA if requested */ | |
e0c9905e | 1342 | if (platform_info->enable_dma) { |
cd7bed00 MW |
1343 | status = pxa2xx_spi_dma_setup(drv_data); |
1344 | if (status) { | |
8b57b11b | 1345 | dev_warn(dev, "no DMA channels available, using PIO\n"); |
cd7bed00 | 1346 | platform_info->enable_dma = false; |
b6ced294 | 1347 | } else { |
51eea52d | 1348 | controller->can_dma = pxa2xx_spi_can_dma; |
bf9f742c | 1349 | controller->max_dma_len = MAX_DMA_LEN; |
b2662a16 DV |
1350 | controller->max_transfer_size = |
1351 | pxa2xx_spi_max_dma_transfer_size; | |
9b328f5f AS |
1352 | |
1353 | dev_dbg(dev, "DMA burst size set to %u\n", platform_info->dma_burst_size); | |
e0c9905e | 1354 | } |
e0c9905e SS |
1355 | } |
1356 | ||
1357 | /* Enable SOC clock */ | |
62bbc864 TJ |
1358 | status = clk_prepare_enable(ssp->clk); |
1359 | if (status) | |
1360 | goto out_error_dma_irq_alloc; | |
3343b7a6 | 1361 | |
51eea52d | 1362 | controller->max_speed_hz = clk_get_rate(ssp->clk); |
23cdddb2 JN |
1363 | /* |
1364 | * Set minimum speed for all other platforms than Intel Quark which is | |
1365 | * able do under 1 Hz transfers. | |
1366 | */ | |
1367 | if (!pxa25x_ssp_comp(drv_data)) | |
1368 | controller->min_speed_hz = | |
1369 | DIV_ROUND_UP(controller->max_speed_hz, 4096); | |
1370 | else if (!is_quark_x1000_ssp(drv_data)) | |
1371 | controller->min_speed_hz = | |
1372 | DIV_ROUND_UP(controller->max_speed_hz, 512); | |
e0c9905e | 1373 | |
0c8ccd8b AS |
1374 | pxa_ssp_disable(ssp); |
1375 | ||
e0c9905e | 1376 | /* Load default SSP configuration */ |
e5262d05 WC |
1377 | switch (drv_data->ssp_type) { |
1378 | case QUARK_X1000_SSP: | |
7c7289a4 AS |
1379 | tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) | |
1380 | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT); | |
c039dd27 | 1381 | pxa2xx_spi_write(drv_data, SSCR1, tmp); |
e5262d05 | 1382 | |
8083d6b8 | 1383 | /* Using the Motorola SPI protocol and use 8 bit frame */ |
7c7289a4 AS |
1384 | tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8); |
1385 | pxa2xx_spi_write(drv_data, SSCR0, tmp); | |
e5262d05 | 1386 | break; |
7c7289a4 AS |
1387 | case CE4100_SSP: |
1388 | tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) | | |
1389 | CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT); | |
1390 | pxa2xx_spi_write(drv_data, SSCR1, tmp); | |
1391 | tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8); | |
1392 | pxa2xx_spi_write(drv_data, SSCR0, tmp); | |
a2dd8af0 | 1393 | break; |
e5262d05 | 1394 | default: |
ec93cb6f | 1395 | |
60ba4431 | 1396 | if (spi_controller_is_target(controller)) { |
ec93cb6f LR |
1397 | tmp = SSCR1_SCFR | |
1398 | SSCR1_SCLKDIR | | |
1399 | SSCR1_SFRMDIR | | |
1400 | SSCR1_RxTresh(2) | | |
1401 | SSCR1_TxTresh(1) | | |
1402 | SSCR1_SPH; | |
1403 | } else { | |
1404 | tmp = SSCR1_RxTresh(RX_THRESH_DFLT) | | |
1405 | SSCR1_TxTresh(TX_THRESH_DFLT); | |
1406 | } | |
c039dd27 | 1407 | pxa2xx_spi_write(drv_data, SSCR1, tmp); |
ec93cb6f | 1408 | tmp = SSCR0_Motorola | SSCR0_DataSize(8); |
60ba4431 | 1409 | if (!spi_controller_is_target(controller)) |
ec93cb6f | 1410 | tmp |= SSCR0_SCR(2); |
c039dd27 | 1411 | pxa2xx_spi_write(drv_data, SSCR0, tmp); |
e5262d05 WC |
1412 | break; |
1413 | } | |
1414 | ||
2a8626a9 | 1415 | if (!pxa25x_ssp_comp(drv_data)) |
c039dd27 | 1416 | pxa2xx_spi_write(drv_data, SSTO, 0); |
e5262d05 WC |
1417 | |
1418 | if (!is_quark_x1000_ssp(drv_data)) | |
c039dd27 | 1419 | pxa2xx_spi_write(drv_data, SSPSP, 0); |
e0c9905e | 1420 | |
8b136baa JN |
1421 | if (is_lpss_ssp(drv_data)) { |
1422 | lpss_ssp_setup(drv_data); | |
1423 | config = lpss_get_config(drv_data); | |
1424 | if (config->reg_capabilities >= 0) { | |
1425 | tmp = __lpss_ssp_read_priv(drv_data, | |
1426 | config->reg_capabilities); | |
1427 | tmp &= LPSS_CAPS_CS_EN_MASK; | |
1428 | tmp >>= LPSS_CAPS_CS_EN_SHIFT; | |
1429 | platform_info->num_chipselect = ffz(tmp); | |
1430 | } | |
1431 | } | |
51eea52d | 1432 | controller->num_chipselect = platform_info->num_chipselect; |
778c12e6 | 1433 | controller->use_gpio_descriptors = true; |
6ac5a435 | 1434 | |
60ba4431 | 1435 | if (platform_info->is_target) { |
77d33897 LR |
1436 | drv_data->gpiod_ready = devm_gpiod_get_optional(dev, |
1437 | "ready", GPIOD_OUT_LOW); | |
1438 | if (IS_ERR(drv_data->gpiod_ready)) { | |
1439 | status = PTR_ERR(drv_data->gpiod_ready); | |
1440 | goto out_error_clock_enabled; | |
1441 | } | |
1442 | } | |
1443 | ||
e0c9905e | 1444 | /* Register with the SPI framework */ |
c65174fd | 1445 | dev_set_drvdata(dev, drv_data); |
32e5b572 | 1446 | status = spi_register_controller(controller); |
eb743ec6 | 1447 | if (status) { |
d5449432 | 1448 | dev_err_probe(dev, status, "problem registering SPI controller\n"); |
e17465f7 | 1449 | goto out_error_clock_enabled; |
e0c9905e SS |
1450 | } |
1451 | ||
1452 | return status; | |
1453 | ||
12742045 | 1454 | out_error_clock_enabled: |
3343b7a6 | 1455 | clk_disable_unprepare(ssp->clk); |
62bbc864 TJ |
1456 | |
1457 | out_error_dma_irq_alloc: | |
cd7bed00 | 1458 | pxa2xx_spi_dma_release(drv_data); |
2f1a74e5 | 1459 | free_irq(ssp->irq, drv_data); |
e0c9905e | 1460 | |
e0c9905e SS |
1461 | return status; |
1462 | } | |
cdd30ebb | 1463 | EXPORT_SYMBOL_NS_GPL(pxa2xx_spi_probe, "SPI_PXA2xx"); |
e0c9905e | 1464 | |
3d8f037f | 1465 | void pxa2xx_spi_remove(struct device *dev) |
e0c9905e | 1466 | { |
c65174fd | 1467 | struct driver_data *drv_data = dev_get_drvdata(dev); |
3d24b2a4 | 1468 | struct ssp_device *ssp = drv_data->ssp; |
e0c9905e | 1469 | |
32e5b572 LW |
1470 | spi_unregister_controller(drv_data->controller); |
1471 | ||
e0c9905e | 1472 | /* Disable the SSP at the peripheral and SOC level */ |
0c8ccd8b | 1473 | pxa_ssp_disable(ssp); |
3343b7a6 | 1474 | clk_disable_unprepare(ssp->clk); |
e0c9905e SS |
1475 | |
1476 | /* Release DMA */ | |
51eea52d | 1477 | if (drv_data->controller_info->enable_dma) |
cd7bed00 | 1478 | pxa2xx_spi_dma_release(drv_data); |
e0c9905e SS |
1479 | |
1480 | /* Release IRQ */ | |
2f1a74e5 | 1481 | free_irq(ssp->irq, drv_data); |
e0c9905e | 1482 | } |
cdd30ebb | 1483 | EXPORT_SYMBOL_NS_GPL(pxa2xx_spi_remove, "SPI_PXA2xx"); |
e0c9905e | 1484 | |
86d2593a | 1485 | static int pxa2xx_spi_suspend(struct device *dev) |
e0c9905e | 1486 | { |
86d2593a | 1487 | struct driver_data *drv_data = dev_get_drvdata(dev); |
2f1a74e5 | 1488 | struct ssp_device *ssp = drv_data->ssp; |
bffc967e | 1489 | int status; |
e0c9905e | 1490 | |
51eea52d | 1491 | status = spi_controller_suspend(drv_data->controller); |
eb743ec6 | 1492 | if (status) |
e0c9905e | 1493 | return status; |
0c8ccd8b AS |
1494 | |
1495 | pxa_ssp_disable(ssp); | |
2b9375b9 DES |
1496 | |
1497 | if (!pm_runtime_suspended(dev)) | |
1498 | clk_disable_unprepare(ssp->clk); | |
e0c9905e SS |
1499 | |
1500 | return 0; | |
1501 | } | |
1502 | ||
86d2593a | 1503 | static int pxa2xx_spi_resume(struct device *dev) |
e0c9905e | 1504 | { |
86d2593a | 1505 | struct driver_data *drv_data = dev_get_drvdata(dev); |
2f1a74e5 | 1506 | struct ssp_device *ssp = drv_data->ssp; |
bffc967e | 1507 | int status; |
e0c9905e SS |
1508 | |
1509 | /* Enable the SSP clock */ | |
62bbc864 TJ |
1510 | if (!pm_runtime_suspended(dev)) { |
1511 | status = clk_prepare_enable(ssp->clk); | |
1512 | if (status) | |
1513 | return status; | |
1514 | } | |
e0c9905e SS |
1515 | |
1516 | /* Start the queue running */ | |
51eea52d | 1517 | return spi_controller_resume(drv_data->controller); |
e0c9905e | 1518 | } |
7d94a505 | 1519 | |
7d94a505 MW |
1520 | static int pxa2xx_spi_runtime_suspend(struct device *dev) |
1521 | { | |
1522 | struct driver_data *drv_data = dev_get_drvdata(dev); | |
1523 | ||
1524 | clk_disable_unprepare(drv_data->ssp->clk); | |
1525 | return 0; | |
1526 | } | |
1527 | ||
1528 | static int pxa2xx_spi_runtime_resume(struct device *dev) | |
1529 | { | |
1530 | struct driver_data *drv_data = dev_get_drvdata(dev); | |
1531 | ||
d294e99c | 1532 | return clk_prepare_enable(drv_data->ssp->clk); |
7d94a505 | 1533 | } |
86d2593a | 1534 | |
3d8f037f | 1535 | EXPORT_NS_GPL_DEV_PM_OPS(pxa2xx_spi_pm_ops, SPI_PXA2xx) = { |
6c3c438c AS |
1536 | SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume) |
1537 | RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend, pxa2xx_spi_runtime_resume, NULL) | |
86d2593a | 1538 | }; |
e0c9905e | 1539 | |
3d8f037f AS |
1540 | MODULE_AUTHOR("Stephen Street"); |
1541 | MODULE_DESCRIPTION("PXA2xx SSP SPI Controller core driver"); | |
1542 | MODULE_LICENSE("GPL"); |