]>
Commit | Line | Data |
---|---|---|
367ea426 RM |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* | |
3 | * Driver for Qualcomm QUP SPI controller | |
4 | * FIFO and Block modes supported, no DMA | |
5 | * mode support | |
6 | * | |
7 | * Copyright (c) 2020 Sartura Ltd. | |
8 | * | |
9 | * Author: Robert Marko <[email protected]> | |
10 | * Author: Luka Kovacic <[email protected]> | |
11 | * | |
1be82afa | 12 | * Based on stock U-Boot and Linux drivers |
367ea426 RM |
13 | */ |
14 | ||
15 | #include <asm/gpio.h> | |
16 | #include <asm/io.h> | |
17 | #include <clk.h> | |
367ea426 RM |
18 | #include <dm.h> |
19 | #include <errno.h> | |
20 | #include <linux/delay.h> | |
21 | #include <spi.h> | |
22 | ||
23 | #define QUP_CONFIG 0x0000 | |
24 | #define QUP_STATE 0x0004 | |
25 | #define QUP_IO_M_MODES 0x0008 | |
26 | #define QUP_SW_RESET 0x000c | |
27 | #define QUP_OPERATIONAL 0x0018 | |
28 | #define QUP_ERROR_FLAGS 0x001c | |
29 | #define QUP_ERROR_FLAGS_EN 0x0020 | |
30 | #define QUP_OPERATIONAL_MASK 0x0028 | |
31 | #define QUP_HW_VERSION 0x0030 | |
32 | #define QUP_MX_OUTPUT_CNT 0x0100 | |
33 | #define QUP_OUTPUT_FIFO 0x0110 | |
34 | #define QUP_MX_WRITE_CNT 0x0150 | |
35 | #define QUP_MX_INPUT_CNT 0x0200 | |
36 | #define QUP_MX_READ_CNT 0x0208 | |
37 | #define QUP_INPUT_FIFO 0x0218 | |
38 | ||
39 | #define SPI_CONFIG 0x0300 | |
40 | #define SPI_IO_CONTROL 0x0304 | |
41 | #define SPI_ERROR_FLAGS 0x0308 | |
42 | #define SPI_ERROR_FLAGS_EN 0x030c | |
43 | ||
44 | /* QUP_CONFIG fields */ | |
45 | #define QUP_CONFIG_SPI_MODE BIT(8) | |
46 | #define QUP_CONFIG_CLOCK_AUTO_GATE BIT(13) | |
47 | #define QUP_CONFIG_NO_INPUT BIT(7) | |
48 | #define QUP_CONFIG_NO_OUTPUT BIT(6) | |
49 | #define QUP_CONFIG_N 0x001f | |
50 | ||
51 | /* QUP_STATE fields */ | |
52 | #define QUP_STATE_VALID BIT(2) | |
53 | #define QUP_STATE_RESET 0 | |
54 | #define QUP_STATE_RUN 1 | |
55 | #define QUP_STATE_PAUSE 3 | |
56 | #define QUP_STATE_MASK 3 | |
57 | #define QUP_STATE_CLEAR 2 | |
58 | ||
59 | /* QUP_IO_M_MODES fields */ | |
60 | #define QUP_IO_M_PACK_EN BIT(15) | |
61 | #define QUP_IO_M_UNPACK_EN BIT(14) | |
62 | #define QUP_IO_M_INPUT_MODE_MASK_SHIFT 12 | |
63 | #define QUP_IO_M_OUTPUT_MODE_MASK_SHIFT 10 | |
64 | #define QUP_IO_M_INPUT_MODE_MASK (3 << QUP_IO_M_INPUT_MODE_MASK_SHIFT) | |
65 | #define QUP_IO_M_OUTPUT_MODE_MASK (3 << QUP_IO_M_OUTPUT_MODE_MASK_SHIFT) | |
66 | ||
67 | #define QUP_IO_M_OUTPUT_BLOCK_SIZE(x) (((x) & (0x03 << 0)) >> 0) | |
68 | #define QUP_IO_M_OUTPUT_FIFO_SIZE(x) (((x) & (0x07 << 2)) >> 2) | |
69 | #define QUP_IO_M_INPUT_BLOCK_SIZE(x) (((x) & (0x03 << 5)) >> 5) | |
70 | #define QUP_IO_M_INPUT_FIFO_SIZE(x) (((x) & (0x07 << 7)) >> 7) | |
71 | ||
72 | #define QUP_IO_M_MODE_FIFO 0 | |
73 | #define QUP_IO_M_MODE_BLOCK 1 | |
74 | #define QUP_IO_M_MODE_DMOV 2 | |
75 | #define QUP_IO_M_MODE_BAM 3 | |
76 | ||
77 | /* QUP_OPERATIONAL fields */ | |
78 | #define QUP_OP_IN_BLOCK_READ_REQ BIT(13) | |
79 | #define QUP_OP_OUT_BLOCK_WRITE_REQ BIT(12) | |
80 | #define QUP_OP_MAX_INPUT_DONE_FLAG BIT(11) | |
81 | #define QUP_OP_MAX_OUTPUT_DONE_FLAG BIT(10) | |
82 | #define QUP_OP_IN_SERVICE_FLAG BIT(9) | |
83 | #define QUP_OP_OUT_SERVICE_FLAG BIT(8) | |
84 | #define QUP_OP_IN_FIFO_FULL BIT(7) | |
85 | #define QUP_OP_OUT_FIFO_FULL BIT(6) | |
86 | #define QUP_OP_IN_FIFO_NOT_EMPTY BIT(5) | |
87 | #define QUP_OP_OUT_FIFO_NOT_EMPTY BIT(4) | |
88 | ||
89 | /* QUP_ERROR_FLAGS and QUP_ERROR_FLAGS_EN fields */ | |
90 | #define QUP_ERROR_OUTPUT_OVER_RUN BIT(5) | |
91 | #define QUP_ERROR_INPUT_UNDER_RUN BIT(4) | |
92 | #define QUP_ERROR_OUTPUT_UNDER_RUN BIT(3) | |
93 | #define QUP_ERROR_INPUT_OVER_RUN BIT(2) | |
94 | ||
95 | /* SPI_CONFIG fields */ | |
96 | #define SPI_CONFIG_HS_MODE BIT(10) | |
97 | #define SPI_CONFIG_INPUT_FIRST BIT(9) | |
98 | #define SPI_CONFIG_LOOPBACK BIT(8) | |
99 | ||
100 | /* SPI_IO_CONTROL fields */ | |
101 | #define SPI_IO_C_FORCE_CS BIT(11) | |
102 | #define SPI_IO_C_CLK_IDLE_HIGH BIT(10) | |
103 | #define SPI_IO_C_MX_CS_MODE BIT(8) | |
104 | #define SPI_IO_C_CS_N_POLARITY_0 BIT(4) | |
105 | #define SPI_IO_C_CS_SELECT(x) (((x) & 3) << 2) | |
106 | #define SPI_IO_C_CS_SELECT_MASK 0x000c | |
107 | #define SPI_IO_C_TRISTATE_CS BIT(1) | |
108 | #define SPI_IO_C_NO_TRI_STATE BIT(0) | |
109 | ||
110 | /* SPI_ERROR_FLAGS and SPI_ERROR_FLAGS_EN fields */ | |
111 | #define SPI_ERROR_CLK_OVER_RUN BIT(1) | |
112 | #define SPI_ERROR_CLK_UNDER_RUN BIT(0) | |
113 | ||
114 | #define SPI_NUM_CHIPSELECTS 4 | |
115 | ||
116 | #define SPI_DELAY_THRESHOLD 1 | |
117 | #define SPI_DELAY_RETRY 10 | |
118 | ||
119 | #define SPI_RESET_STATE 0 | |
120 | #define SPI_RUN_STATE 1 | |
121 | #define SPI_CORE_RESET 0 | |
122 | #define SPI_CORE_RUNNING 1 | |
123 | ||
124 | #define DUMMY_DATA_VAL 0 | |
125 | #define TIMEOUT_CNT 100 | |
126 | ||
127 | #define QUP_STATE_VALID_BIT 2 | |
128 | #define QUP_CONFIG_MINI_CORE_MSK (0x0F << 8) | |
129 | #define QUP_CONFIG_MINI_CORE_SPI BIT(8) | |
130 | #define QUP_CONF_INPUT_MSK BIT(7) | |
131 | #define QUP_CONF_INPUT_ENA (0 << 7) | |
132 | #define QUP_CONF_NO_INPUT BIT(7) | |
133 | #define QUP_CONF_OUTPUT_MSK BIT(6) | |
134 | #define QUP_CONF_OUTPUT_ENA (0 << 6) | |
135 | #define QUP_CONF_NO_OUTPUT BIT(6) | |
136 | #define QUP_STATE_RUN_STATE 0x1 | |
137 | #define QUP_STATE_RESET_STATE 0x0 | |
138 | #define QUP_STATE_PAUSE_STATE 0x3 | |
139 | #define SPI_BIT_WORD_MSK 0x1F | |
140 | #define SPI_8_BIT_WORD 0x07 | |
141 | #define LOOP_BACK_MSK BIT(8) | |
142 | #define NO_LOOP_BACK (0 << 8) | |
143 | #define SLAVE_OPERATION_MSK BIT(5) | |
144 | #define SLAVE_OPERATION (0 << 5) | |
145 | #define CLK_ALWAYS_ON (0 << 9) | |
146 | #define MX_CS_MODE BIT(8) | |
147 | #define CS_POLARITY_MASK BIT(4) | |
148 | #define NO_TRI_STATE BIT(0) | |
149 | #define FORCE_CS_MSK BIT(11) | |
150 | #define FORCE_CS_EN BIT(11) | |
151 | #define FORCE_CS_DIS (0 << 11) | |
152 | #define OUTPUT_BIT_SHIFT_MSK BIT(16) | |
153 | #define OUTPUT_BIT_SHIFT_EN BIT(16) | |
154 | #define INPUT_BLOCK_MODE_MSK (0x03 << 12) | |
155 | #define INPUT_BLOCK_MODE (0x01 << 12) | |
156 | #define OUTPUT_BLOCK_MODE_MSK (0x03 << 10) | |
157 | #define OUTPUT_BLOCK_MODE (0x01 << 10) | |
158 | #define INPUT_BAM_MODE (0x3 << 12) | |
159 | #define OUTPUT_BAM_MODE (0x3 << 10) | |
160 | #define PACK_EN (0x1 << 15) | |
161 | #define UNPACK_EN (0x1 << 14) | |
162 | #define PACK_EN_MSK (0x1 << 15) | |
163 | #define UNPACK_EN_MSK (0x1 << 14) | |
164 | #define OUTPUT_SERVICE_MSK (0x1 << 8) | |
165 | #define INPUT_SERVICE_MSK (0x1 << 9) | |
166 | #define OUTPUT_SERVICE_DIS (0x1 << 8) | |
167 | #define INPUT_SERVICE_DIS (0x1 << 9) | |
168 | #define BLSP0_SPI_DEASSERT_WAIT_REG 0x0310 | |
169 | #define QUP_DATA_AVAILABLE_FOR_READ BIT(5) | |
170 | #define SPI_INPUT_BLOCK_SIZE 4 | |
171 | #define SPI_OUTPUT_BLOCK_SIZE 4 | |
172 | #define SPI_BITLEN_MSK 0x07 | |
173 | #define MAX_COUNT_SIZE 0xffff | |
174 | ||
175 | struct qup_spi_priv { | |
176 | phys_addr_t base; | |
177 | struct clk clk; | |
178 | u32 num_cs; | |
179 | struct gpio_desc cs_gpios[SPI_NUM_CHIPSELECTS]; | |
180 | bool cs_high; | |
181 | u32 core_state; | |
182 | }; | |
183 | ||
184 | static int qup_spi_set_cs(struct udevice *dev, unsigned int cs, bool enable) | |
185 | { | |
186 | struct qup_spi_priv *priv = dev_get_priv(dev); | |
187 | ||
188 | debug("%s: cs=%d enable=%d\n", __func__, cs, enable); | |
189 | ||
190 | if (cs >= SPI_NUM_CHIPSELECTS) | |
191 | return -ENODEV; | |
192 | ||
193 | if (!dm_gpio_is_valid(&priv->cs_gpios[cs])) | |
194 | return -EINVAL; | |
195 | ||
196 | if (priv->cs_high) | |
197 | enable = !enable; | |
198 | ||
199 | return dm_gpio_set_value(&priv->cs_gpios[cs], enable ? 1 : 0); | |
200 | } | |
201 | ||
202 | /* | |
203 | * Function to write data to OUTPUT FIFO | |
204 | */ | |
205 | static void qup_spi_write_byte(struct udevice *dev, unsigned char data) | |
206 | { | |
207 | struct udevice *bus = dev_get_parent(dev); | |
208 | struct qup_spi_priv *priv = dev_get_priv(bus); | |
209 | /* Wait for space in the FIFO */ | |
210 | while ((readl(priv->base + QUP_OPERATIONAL) & QUP_OP_OUT_FIFO_FULL)) | |
211 | udelay(1); | |
212 | ||
213 | /* Write the byte of data */ | |
214 | writel(data, priv->base + QUP_OUTPUT_FIFO); | |
215 | } | |
216 | ||
217 | /* | |
218 | * Function to read data from Input FIFO | |
219 | */ | |
220 | static unsigned char qup_spi_read_byte(struct udevice *dev) | |
221 | { | |
222 | struct udevice *bus = dev_get_parent(dev); | |
223 | struct qup_spi_priv *priv = dev_get_priv(bus); | |
224 | /* Wait for Data in FIFO */ | |
225 | while (!(readl(priv->base + QUP_OPERATIONAL) & QUP_DATA_AVAILABLE_FOR_READ)) { | |
226 | printf("Stuck at FIFO data wait\n"); | |
227 | udelay(1); | |
228 | } | |
229 | ||
230 | /* Read a byte of data */ | |
231 | return readl(priv->base + QUP_INPUT_FIFO) & 0xff; | |
232 | } | |
233 | ||
234 | /* | |
d466f620 | 235 | * Function to check whether Input or Output FIFO |
367ea426 RM |
236 | * has data to be serviced |
237 | */ | |
238 | static int qup_spi_check_fifo_status(struct udevice *dev, u32 reg_addr) | |
239 | { | |
240 | struct udevice *bus = dev_get_parent(dev); | |
241 | struct qup_spi_priv *priv = dev_get_priv(bus); | |
242 | unsigned int count = TIMEOUT_CNT; | |
243 | unsigned int status_flag; | |
244 | unsigned int val; | |
245 | ||
246 | do { | |
247 | val = readl(priv->base + reg_addr); | |
248 | count--; | |
249 | if (count == 0) | |
250 | return -ETIMEDOUT; | |
251 | ||
252 | status_flag = ((val & QUP_OP_OUT_SERVICE_FLAG) | (val & QUP_OP_IN_SERVICE_FLAG)); | |
253 | } while (!status_flag); | |
254 | ||
255 | return 0; | |
256 | } | |
257 | ||
258 | /* | |
259 | * Function to configure Input and Output enable/disable | |
260 | */ | |
261 | static void qup_spi_enable_io_config(struct udevice *dev, u32 write_cnt, u32 read_cnt) | |
262 | { | |
263 | struct udevice *bus = dev_get_parent(dev); | |
264 | struct qup_spi_priv *priv = dev_get_priv(bus); | |
265 | ||
266 | if (write_cnt) { | |
267 | clrsetbits_le32(priv->base + QUP_CONFIG, | |
268 | QUP_CONF_OUTPUT_MSK, QUP_CONF_OUTPUT_ENA); | |
269 | } else { | |
270 | clrsetbits_le32(priv->base + QUP_CONFIG, | |
271 | QUP_CONF_OUTPUT_MSK, QUP_CONF_NO_OUTPUT); | |
272 | } | |
273 | ||
274 | if (read_cnt) { | |
275 | clrsetbits_le32(priv->base + QUP_CONFIG, | |
276 | QUP_CONF_INPUT_MSK, QUP_CONF_INPUT_ENA); | |
277 | } else { | |
278 | clrsetbits_le32(priv->base + QUP_CONFIG, | |
279 | QUP_CONF_INPUT_MSK, QUP_CONF_NO_INPUT); | |
280 | } | |
281 | } | |
282 | ||
283 | static int check_bit_state(struct udevice *dev, u32 reg_addr, int bit_num, int val, | |
284 | int us_delay) | |
285 | { | |
286 | struct udevice *bus = dev_get_parent(dev); | |
287 | struct qup_spi_priv *priv = dev_get_priv(bus); | |
288 | unsigned int count = TIMEOUT_CNT; | |
289 | unsigned int bit_val = ((readl(priv->base + reg_addr) >> bit_num) & 0x01); | |
290 | ||
291 | while (bit_val != val) { | |
292 | count--; | |
293 | if (count == 0) | |
294 | return -ETIMEDOUT; | |
295 | udelay(us_delay); | |
296 | bit_val = ((readl(priv->base + reg_addr) >> bit_num) & 0x01); | |
297 | } | |
298 | ||
299 | return 0; | |
300 | } | |
301 | ||
302 | /* | |
303 | * Check whether QUPn State is valid | |
304 | */ | |
305 | static int check_qup_state_valid(struct udevice *dev) | |
306 | { | |
307 | return check_bit_state(dev, QUP_STATE, QUP_STATE_VALID, 1, 1); | |
308 | } | |
309 | ||
310 | /* | |
311 | * Configure QUPn Core state | |
312 | */ | |
313 | static int qup_spi_config_spi_state(struct udevice *dev, unsigned int state) | |
314 | { | |
315 | struct udevice *bus = dev_get_parent(dev); | |
316 | struct qup_spi_priv *priv = dev_get_priv(bus); | |
317 | u32 val; | |
318 | int ret; | |
319 | ||
320 | ret = check_qup_state_valid(dev); | |
321 | if (ret != 0) | |
322 | return ret; | |
323 | ||
324 | switch (state) { | |
325 | case SPI_RUN_STATE: | |
326 | /* Set the state to RUN */ | |
327 | val = ((readl(priv->base + QUP_STATE) & ~QUP_STATE_MASK) | |
328 | | QUP_STATE_RUN); | |
329 | writel(val, priv->base + QUP_STATE); | |
330 | ret = check_qup_state_valid(dev); | |
331 | if (ret != 0) | |
332 | return ret; | |
333 | priv->core_state = SPI_CORE_RUNNING; | |
334 | break; | |
335 | case SPI_RESET_STATE: | |
336 | /* Set the state to RESET */ | |
337 | val = ((readl(priv->base + QUP_STATE) & ~QUP_STATE_MASK) | |
338 | | QUP_STATE_RESET); | |
339 | writel(val, priv->base + QUP_STATE); | |
340 | ret = check_qup_state_valid(dev); | |
341 | if (ret != 0) | |
342 | return ret; | |
343 | priv->core_state = SPI_CORE_RESET; | |
344 | break; | |
345 | default: | |
346 | printf("Unsupported QUP SPI state: %d\n", state); | |
347 | ret = -EINVAL; | |
348 | break; | |
349 | } | |
350 | return ret; | |
351 | } | |
352 | ||
353 | /* | |
354 | * Function to read bytes number of data from the Input FIFO | |
355 | */ | |
356 | static int __qup_spi_blsp_spi_read(struct udevice *dev, u8 *data_buffer, unsigned int bytes) | |
357 | { | |
358 | struct udevice *bus = dev_get_parent(dev); | |
359 | struct qup_spi_priv *priv = dev_get_priv(bus); | |
360 | u32 val; | |
361 | unsigned int i; | |
362 | unsigned int read_bytes = bytes; | |
363 | unsigned int fifo_count; | |
364 | int ret = 0; | |
365 | int state_config; | |
366 | ||
367 | /* Configure no of bytes to read */ | |
368 | state_config = qup_spi_config_spi_state(dev, SPI_RESET_STATE); | |
369 | if (state_config) | |
370 | return state_config; | |
371 | ||
372 | /* Configure input and output enable */ | |
373 | qup_spi_enable_io_config(dev, 0, read_bytes); | |
374 | ||
375 | writel(bytes, priv->base + QUP_MX_INPUT_CNT); | |
376 | ||
377 | state_config = qup_spi_config_spi_state(dev, SPI_RUN_STATE); | |
378 | if (state_config) | |
379 | return state_config; | |
380 | ||
381 | while (read_bytes) { | |
382 | ret = qup_spi_check_fifo_status(dev, QUP_OPERATIONAL); | |
383 | if (ret != 0) | |
384 | goto out; | |
385 | ||
386 | val = readl(priv->base + QUP_OPERATIONAL); | |
387 | if (val & QUP_OP_IN_SERVICE_FLAG) { | |
388 | /* | |
389 | * acknowledge to hw that software will | |
390 | * read input data | |
391 | */ | |
392 | val &= QUP_OP_IN_SERVICE_FLAG; | |
393 | writel(val, priv->base + QUP_OPERATIONAL); | |
394 | ||
395 | fifo_count = ((read_bytes > SPI_INPUT_BLOCK_SIZE) ? | |
396 | SPI_INPUT_BLOCK_SIZE : read_bytes); | |
397 | ||
398 | for (i = 0; i < fifo_count; i++) { | |
399 | *data_buffer = qup_spi_read_byte(dev); | |
400 | data_buffer++; | |
401 | read_bytes--; | |
402 | } | |
403 | } | |
404 | } | |
405 | ||
406 | out: | |
407 | /* | |
408 | * Put the SPI Core back in the Reset State | |
409 | * to end the transfer | |
410 | */ | |
411 | (void)qup_spi_config_spi_state(dev, SPI_RESET_STATE); | |
412 | ||
413 | return ret; | |
414 | } | |
415 | ||
416 | static int qup_spi_blsp_spi_read(struct udevice *dev, u8 *data_buffer, unsigned int bytes) | |
417 | { | |
418 | int length, ret; | |
419 | ||
420 | while (bytes) { | |
421 | length = (bytes < MAX_COUNT_SIZE) ? bytes : MAX_COUNT_SIZE; | |
422 | ||
423 | ret = __qup_spi_blsp_spi_read(dev, data_buffer, length); | |
424 | if (ret != 0) | |
425 | return ret; | |
426 | ||
427 | data_buffer += length; | |
428 | bytes -= length; | |
429 | } | |
430 | ||
431 | return 0; | |
432 | } | |
433 | ||
434 | /* | |
435 | * Function to write data to the Output FIFO | |
436 | */ | |
437 | static int __qup_blsp_spi_write(struct udevice *dev, const u8 *cmd_buffer, unsigned int bytes) | |
438 | { | |
439 | struct udevice *bus = dev_get_parent(dev); | |
440 | struct qup_spi_priv *priv = dev_get_priv(bus); | |
441 | u32 val; | |
442 | unsigned int i; | |
443 | unsigned int write_len = bytes; | |
444 | unsigned int read_len = bytes; | |
445 | unsigned int fifo_count; | |
446 | int ret = 0; | |
447 | int state_config; | |
448 | ||
449 | state_config = qup_spi_config_spi_state(dev, SPI_RESET_STATE); | |
450 | if (state_config) | |
451 | return state_config; | |
452 | ||
453 | writel(bytes, priv->base + QUP_MX_OUTPUT_CNT); | |
454 | writel(bytes, priv->base + QUP_MX_INPUT_CNT); | |
455 | state_config = qup_spi_config_spi_state(dev, SPI_RUN_STATE); | |
456 | if (state_config) | |
457 | return state_config; | |
458 | ||
459 | /* Configure input and output enable */ | |
460 | qup_spi_enable_io_config(dev, write_len, read_len); | |
461 | ||
462 | /* | |
463 | * read_len considered to ensure that we read the dummy data for the | |
464 | * write we performed. This is needed to ensure with WR-RD transaction | |
465 | * to get the actual data on the subsequent read cycle that happens | |
466 | */ | |
467 | while (write_len || read_len) { | |
468 | ret = qup_spi_check_fifo_status(dev, QUP_OPERATIONAL); | |
469 | if (ret != 0) | |
470 | goto out; | |
471 | ||
472 | val = readl(priv->base + QUP_OPERATIONAL); | |
473 | if (val & QUP_OP_OUT_SERVICE_FLAG) { | |
474 | /* | |
475 | * acknowledge to hw that software will write | |
476 | * expected output data | |
477 | */ | |
478 | val &= QUP_OP_OUT_SERVICE_FLAG; | |
479 | writel(val, priv->base + QUP_OPERATIONAL); | |
480 | ||
481 | if (write_len > SPI_OUTPUT_BLOCK_SIZE) | |
482 | fifo_count = SPI_OUTPUT_BLOCK_SIZE; | |
483 | else | |
484 | fifo_count = write_len; | |
485 | ||
486 | for (i = 0; i < fifo_count; i++) { | |
487 | /* Write actual data to output FIFO */ | |
488 | qup_spi_write_byte(dev, *cmd_buffer); | |
489 | cmd_buffer++; | |
490 | write_len--; | |
491 | } | |
492 | } | |
493 | if (val & QUP_OP_IN_SERVICE_FLAG) { | |
494 | /* | |
495 | * acknowledge to hw that software | |
496 | * will read input data | |
497 | */ | |
498 | val &= QUP_OP_IN_SERVICE_FLAG; | |
499 | writel(val, priv->base + QUP_OPERATIONAL); | |
500 | ||
501 | if (read_len > SPI_INPUT_BLOCK_SIZE) | |
502 | fifo_count = SPI_INPUT_BLOCK_SIZE; | |
503 | else | |
504 | fifo_count = read_len; | |
505 | ||
506 | for (i = 0; i < fifo_count; i++) { | |
507 | /* Read dummy data for the data written */ | |
508 | (void)qup_spi_read_byte(dev); | |
509 | ||
510 | /* Decrement the write count after reading the | |
511 | * dummy data from the device. This is to make | |
512 | * sure we read dummy data before we write the | |
513 | * data to fifo | |
514 | */ | |
515 | read_len--; | |
516 | } | |
517 | } | |
518 | } | |
519 | out: | |
520 | /* | |
521 | * Put the SPI Core back in the Reset State | |
522 | * to end the transfer | |
523 | */ | |
524 | (void)qup_spi_config_spi_state(dev, SPI_RESET_STATE); | |
525 | ||
526 | return ret; | |
527 | } | |
528 | ||
529 | static int qup_spi_blsp_spi_write(struct udevice *dev, const u8 *cmd_buffer, unsigned int bytes) | |
530 | { | |
531 | int length, ret; | |
532 | ||
533 | while (bytes) { | |
534 | length = (bytes < MAX_COUNT_SIZE) ? bytes : MAX_COUNT_SIZE; | |
535 | ||
536 | ret = __qup_blsp_spi_write(dev, cmd_buffer, length); | |
537 | if (ret != 0) | |
538 | return ret; | |
539 | ||
540 | cmd_buffer += length; | |
541 | bytes -= length; | |
542 | } | |
543 | ||
544 | return 0; | |
545 | } | |
546 | ||
547 | static int qup_spi_set_speed(struct udevice *dev, uint speed) | |
548 | { | |
549 | return 0; | |
550 | } | |
551 | ||
552 | static int qup_spi_set_mode(struct udevice *dev, uint mode) | |
553 | { | |
554 | struct qup_spi_priv *priv = dev_get_priv(dev); | |
555 | unsigned int clk_idle_state; | |
556 | unsigned int input_first_mode; | |
557 | u32 val; | |
558 | ||
559 | switch (mode) { | |
560 | case SPI_MODE_0: | |
561 | clk_idle_state = 0; | |
562 | input_first_mode = SPI_CONFIG_INPUT_FIRST; | |
563 | break; | |
564 | case SPI_MODE_1: | |
565 | clk_idle_state = 0; | |
566 | input_first_mode = 0; | |
567 | break; | |
568 | case SPI_MODE_2: | |
569 | clk_idle_state = 1; | |
570 | input_first_mode = SPI_CONFIG_INPUT_FIRST; | |
571 | break; | |
572 | case SPI_MODE_3: | |
573 | clk_idle_state = 1; | |
574 | input_first_mode = 0; | |
575 | break; | |
576 | default: | |
577 | printf("Unsupported spi mode: %d\n", mode); | |
578 | return -EINVAL; | |
579 | } | |
580 | ||
581 | if (mode & SPI_CS_HIGH) | |
582 | priv->cs_high = true; | |
583 | else | |
584 | priv->cs_high = false; | |
585 | ||
586 | val = readl(priv->base + SPI_CONFIG); | |
587 | val |= input_first_mode; | |
588 | writel(val, priv->base + SPI_CONFIG); | |
589 | ||
590 | val = readl(priv->base + SPI_IO_CONTROL); | |
591 | if (clk_idle_state) | |
592 | val |= SPI_IO_C_CLK_IDLE_HIGH; | |
593 | else | |
594 | val &= ~SPI_IO_C_CLK_IDLE_HIGH; | |
595 | ||
596 | writel(val, priv->base + SPI_IO_CONTROL); | |
597 | ||
598 | return 0; | |
599 | } | |
600 | ||
601 | static void qup_spi_reset(struct udevice *dev) | |
602 | { | |
603 | struct udevice *bus = dev_get_parent(dev); | |
604 | struct qup_spi_priv *priv = dev_get_priv(bus); | |
605 | ||
606 | /* Driver may not be probed yet */ | |
607 | if (!priv) | |
608 | return; | |
609 | ||
610 | writel(0x1, priv->base + QUP_SW_RESET); | |
611 | udelay(5); | |
612 | } | |
613 | ||
614 | static int qup_spi_hw_init(struct udevice *dev) | |
615 | { | |
616 | struct udevice *bus = dev_get_parent(dev); | |
617 | struct qup_spi_priv *priv = dev_get_priv(bus); | |
618 | int ret; | |
619 | ||
620 | /* QUPn module configuration */ | |
621 | qup_spi_reset(dev); | |
622 | ||
623 | /* Set the QUPn state */ | |
624 | ret = qup_spi_config_spi_state(dev, SPI_RESET_STATE); | |
625 | if (ret) | |
626 | return ret; | |
627 | ||
628 | /* | |
629 | * Configure Mini core to SPI core with Input Output enabled, | |
630 | * SPI master, N = 8 bits | |
631 | */ | |
632 | clrsetbits_le32(priv->base + QUP_CONFIG, (QUP_CONFIG_MINI_CORE_MSK | | |
633 | QUP_CONF_INPUT_MSK | | |
634 | QUP_CONF_OUTPUT_MSK | | |
635 | SPI_BIT_WORD_MSK), | |
636 | (QUP_CONFIG_MINI_CORE_SPI | | |
637 | QUP_CONF_INPUT_ENA | | |
638 | QUP_CONF_OUTPUT_ENA | | |
639 | SPI_8_BIT_WORD)); | |
640 | ||
641 | /* | |
642 | * Configure Input first SPI protocol, | |
643 | * SPI master mode and no loopback | |
644 | */ | |
645 | clrsetbits_le32(priv->base + SPI_CONFIG, (LOOP_BACK_MSK | | |
646 | SLAVE_OPERATION_MSK), | |
647 | (NO_LOOP_BACK | | |
648 | SLAVE_OPERATION)); | |
649 | ||
650 | /* | |
651 | * Configure SPI IO Control Register | |
652 | * CLK_ALWAYS_ON = 0 | |
653 | * MX_CS_MODE = 0 | |
654 | * NO_TRI_STATE = 1 | |
655 | */ | |
656 | writel((CLK_ALWAYS_ON | NO_TRI_STATE), priv->base + SPI_IO_CONTROL); | |
657 | ||
658 | /* | |
659 | * Configure SPI IO Modes. | |
660 | * OUTPUT_BIT_SHIFT_EN = 1 | |
661 | * INPUT_MODE = Block Mode | |
662 | * OUTPUT MODE = Block Mode | |
663 | */ | |
664 | ||
665 | clrsetbits_le32(priv->base + QUP_IO_M_MODES, (OUTPUT_BIT_SHIFT_MSK | | |
666 | INPUT_BLOCK_MODE_MSK | | |
667 | OUTPUT_BLOCK_MODE_MSK), | |
668 | (OUTPUT_BIT_SHIFT_EN | | |
669 | INPUT_BLOCK_MODE | | |
670 | OUTPUT_BLOCK_MODE)); | |
671 | ||
672 | /* Disable Error mask */ | |
673 | writel(0, priv->base + SPI_ERROR_FLAGS_EN); | |
674 | writel(0, priv->base + QUP_ERROR_FLAGS_EN); | |
675 | writel(0, priv->base + BLSP0_SPI_DEASSERT_WAIT_REG); | |
676 | ||
677 | return ret; | |
678 | } | |
679 | ||
680 | static int qup_spi_claim_bus(struct udevice *dev) | |
681 | { | |
682 | int ret; | |
683 | ||
684 | ret = qup_spi_hw_init(dev); | |
685 | if (ret) | |
686 | return -EIO; | |
687 | ||
688 | return 0; | |
689 | } | |
690 | ||
691 | static int qup_spi_release_bus(struct udevice *dev) | |
692 | { | |
693 | /* Reset the SPI hardware */ | |
694 | qup_spi_reset(dev); | |
695 | ||
696 | return 0; | |
697 | } | |
698 | ||
699 | static int qup_spi_xfer(struct udevice *dev, unsigned int bitlen, | |
700 | const void *dout, void *din, unsigned long flags) | |
701 | { | |
702 | struct udevice *bus = dev_get_parent(dev); | |
8a8d24bd | 703 | struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev); |
367ea426 RM |
704 | unsigned int len; |
705 | const u8 *txp = dout; | |
706 | u8 *rxp = din; | |
707 | int ret = 0; | |
708 | ||
709 | if (bitlen & SPI_BITLEN_MSK) { | |
710 | printf("Invalid bit length\n"); | |
711 | return -EINVAL; | |
712 | } | |
713 | ||
714 | len = bitlen >> 3; | |
715 | ||
716 | if (flags & SPI_XFER_BEGIN) { | |
717 | ret = qup_spi_hw_init(dev); | |
718 | if (ret != 0) | |
719 | return ret; | |
720 | ||
34da258b | 721 | ret = qup_spi_set_cs(bus, slave_plat->cs[0], false); |
367ea426 RM |
722 | if (ret != 0) |
723 | return ret; | |
724 | } | |
725 | ||
726 | if (dout != NULL) { | |
727 | ret = qup_spi_blsp_spi_write(dev, txp, len); | |
728 | if (ret != 0) | |
729 | return ret; | |
730 | } | |
731 | ||
732 | if (din != NULL) { | |
733 | ret = qup_spi_blsp_spi_read(dev, rxp, len); | |
734 | if (ret != 0) | |
735 | return ret; | |
736 | } | |
737 | ||
738 | if (flags & SPI_XFER_END) { | |
34da258b | 739 | ret = qup_spi_set_cs(bus, slave_plat->cs[0], true); |
367ea426 RM |
740 | if (ret != 0) |
741 | return ret; | |
742 | } | |
743 | ||
744 | return ret; | |
745 | } | |
746 | ||
747 | static int qup_spi_probe(struct udevice *dev) | |
748 | { | |
749 | struct qup_spi_priv *priv = dev_get_priv(dev); | |
750 | int ret; | |
751 | ||
752 | priv->base = dev_read_addr(dev); | |
753 | if (priv->base == FDT_ADDR_T_NONE) | |
754 | return -EINVAL; | |
755 | ||
756 | ret = clk_get_by_index(dev, 0, &priv->clk); | |
757 | if (ret) | |
758 | return ret; | |
759 | ||
760 | ret = clk_enable(&priv->clk); | |
761 | if (ret < 0) | |
762 | return ret; | |
763 | ||
764 | priv->num_cs = dev_read_u32_default(dev, "num-cs", 1); | |
765 | ||
766 | ret = gpio_request_list_by_name(dev, "cs-gpios", priv->cs_gpios, | |
767 | priv->num_cs, GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE); | |
768 | if (ret < 0) { | |
769 | printf("Can't get %s cs gpios: %d\n", dev->name, ret); | |
770 | return -EINVAL; | |
771 | } | |
772 | ||
773 | return 0; | |
774 | } | |
775 | ||
776 | static const struct dm_spi_ops qup_spi_ops = { | |
777 | .claim_bus = qup_spi_claim_bus, | |
778 | .release_bus = qup_spi_release_bus, | |
779 | .xfer = qup_spi_xfer, | |
780 | .set_speed = qup_spi_set_speed, | |
781 | .set_mode = qup_spi_set_mode, | |
782 | /* | |
783 | * cs_info is not needed, since we require all chip selects to be | |
784 | * in the device tree explicitly | |
785 | */ | |
786 | }; | |
787 | ||
788 | static const struct udevice_id qup_spi_ids[] = { | |
789 | { .compatible = "qcom,spi-qup-v1.1.1", }, | |
790 | { .compatible = "qcom,spi-qup-v2.1.1", }, | |
791 | { .compatible = "qcom,spi-qup-v2.2.1", }, | |
792 | { } | |
793 | }; | |
794 | ||
795 | U_BOOT_DRIVER(spi_qup) = { | |
796 | .name = "spi_qup", | |
797 | .id = UCLASS_SPI, | |
798 | .of_match = qup_spi_ids, | |
799 | .ops = &qup_spi_ops, | |
41575d8e | 800 | .priv_auto = sizeof(struct qup_spi_priv), |
367ea426 RM |
801 | .probe = qup_spi_probe, |
802 | }; |