Debian Codegraph @

src/soc/qualcomm/ipq40xx/spi.c

raw symbols SCIP
1 /* SPDX-License-Identifier: BSD-3-Clause */
2
3 #include <device/mmio.h>
4 #include <console/console.h>
5 #include <delay.h>
6 #include <gpio.h>
7 #include <soc/iomap.h>
8 #include <soc/spi.h>
9
10 static const struct blsp_spi spi_reg[] = {
11 /* BLSP0 registers for SPI interface */
12 {
13 BLSP0_SPI_CONFIG_REG,
14 BLSP0_SPI_IO_CONTROL_REG,
15 BLSP0_SPI_ERROR_FLAGS_REG,
16 BLSP0_SPI_ERROR_FLAGS_EN_REG,
17 BLSP0_QUP_CONFIG_REG,
18 BLSP0_QUP_ERROR_FLAGS_REG,
19 BLSP0_QUP_ERROR_FLAGS_EN_REG,
20 BLSP0_QUP_OPERATIONAL_REG,
21 BLSP0_QUP_IO_MODES_REG,
22 BLSP0_QUP_STATE_REG,
23 BLSP0_QUP_INPUT_FIFOc_REG(0),
24 BLSP0_QUP_OUTPUT_FIFOc_REG(0),
25 BLSP0_QUP_MX_INPUT_COUNT_REG,
26 BLSP0_QUP_MX_OUTPUT_COUNT_REG,
27 BLSP0_QUP_SW_RESET_REG,
28 0,
29 0,
30 BLSP0_QUP_OPERATIONAL_MASK,
31 BLSP0_SPI_DEASSERT_WAIT_REG,
32 },
33 /* BLSP1 registers for SPI interface */
34 {
35 BLSP1_SPI_CONFIG_REG,
36 BLSP1_SPI_IO_CONTROL_REG,
37 BLSP1_SPI_ERROR_FLAGS_REG,
38 BLSP1_SPI_ERROR_FLAGS_EN_REG,
39 BLSP1_QUP_CONFIG_REG,
40 BLSP1_QUP_ERROR_FLAGS_REG,
41 BLSP1_QUP_ERROR_FLAGS_EN_REG,
42 BLSP1_QUP_OPERATIONAL_REG,
43 BLSP1_QUP_IO_MODES_REG,
44 BLSP1_QUP_STATE_REG,
45 BLSP1_QUP_INPUT_FIFOc_REG(0),
46 BLSP1_QUP_OUTPUT_FIFOc_REG(0),
47 BLSP1_QUP_MX_INPUT_COUNT_REG,
48 BLSP1_QUP_MX_OUTPUT_COUNT_REG,
49 BLSP1_QUP_SW_RESET_REG,
50 0,
51 0,
52 BLSP1_QUP_OPERATIONAL_MASK,
53 BLSP1_SPI_DEASSERT_WAIT_REG,
54 },
55 };
56
57 static int check_bit_state(void *reg_addr, int mask,
58 int val, int us_delay)
59 {
60 unsigned int count = TIMEOUT_CNT;
61
62 while ((read32(reg_addr) & mask) != val) {
63 count--;
64 if (count == 0)
65 return -ETIMEDOUT;
66 udelay(us_delay);
67 }
68
69 return SUCCESS;
70 }
71
72 /*
73 * Check whether QUPn State is valid
74 */
75 static int check_qup_state_valid(struct ipq_spi_slave *ds)
76 {
77 return check_bit_state(ds->regs->qup_state, QUP_STATE_VALID_MASK,
78 QUP_STATE_VALID, 1);
79 }
80
81 /*
82 * Configure QUPn Core state
83 */
84 static int config_spi_state(struct ipq_spi_slave *ds, unsigned int state)
85 {
86 uint32_t val;
87 int ret = SUCCESS;
88
89 ret = check_qup_state_valid(ds);
90 if (ret != SUCCESS)
91 return ret;
92
93 switch (state) {
94 case QUP_STATE_RUN:
95 /* Set the state to RUN */
96 val = ((read32(ds->regs->qup_state) & ~QUP_STATE_MASK)
97 | QUP_STATE_RUN);
98 write32(ds->regs->qup_state, val);
99 ret = check_qup_state_valid(ds);
100 break;
101 case QUP_STATE_RESET:
102 /* Set the state to RESET */
103 val = ((read32(ds->regs->qup_state) & ~QUP_STATE_MASK)
104 | QUP_STATE_RESET);
105 write32(ds->regs->qup_state, val);
106 ret = check_qup_state_valid(ds);
107 break;
108 default:
109 printk(BIOS_ERR, "unsupported QUP SPI state : %d\n", state);
110 ret = -EINVAL;
111 break;
112 }
113
114 return ret;
115 }
116
117 /*
118 * Set QUPn SPI Mode
119 */
120 static void spi_set_mode(struct ipq_spi_slave *ds, unsigned int mode)
121 {
122 unsigned int clk_idle_state;
123 unsigned int input_first_mode;
124 uint32_t val;
125
126 switch (mode) {
127 case SPI_MODE0:
128 clk_idle_state = 0;
129 input_first_mode = SPI_CONFIG_INPUT_FIRST;
130 break;
131 case SPI_MODE1:
132 clk_idle_state = 0;
133 input_first_mode = 0;
134 break;
135 case SPI_MODE2:
136 clk_idle_state = 1;
137 input_first_mode = SPI_CONFIG_INPUT_FIRST;
138 break;
139 case SPI_MODE3:
140 clk_idle_state = 1;
141 input_first_mode = 0;
142 break;
143 default:
144 printk(BIOS_ERR, "unsupported spi mode : %d\n", mode);
145 return;
146 }
147
148 val = read32(ds->regs->spi_config);
149 val |= input_first_mode;
150 write32(ds->regs->spi_config, val);
151
152 val = read32(ds->regs->io_control);
153 if (clk_idle_state)
154 val |= SPI_IO_CTRL_CLOCK_IDLE_HIGH;
155 else
156 val &= ~SPI_IO_CTRL_CLOCK_IDLE_HIGH;
157
158 write32(ds->regs->io_control, val);
159 }
160
161 /*
162 * Reset entire QUP and all mini cores
163 */
164 static void spi_reset(struct ipq_spi_slave *ds)
165 {
166 write32(ds->regs->qup_sw_reset, 0x1);
167 udelay(5);
168 check_qup_state_valid(ds);
169 }
170
171 static struct ipq_spi_slave spi_slave_pool[2];
172
173 static struct ipq_spi_slave *to_ipq_spi(const struct spi_slave *slave)
174 {
175 struct ipq_spi_slave *ds;
176 size_t i;
177
178 for (i = 0; i < ARRAY_SIZE(spi_slave_pool); i++) {
179 ds = spi_slave_pool + i;
180
181 if (!ds->allocated)
182 continue;
183
184 if ((ds->slave.bus == slave->bus) &&
185 (ds->slave.cs == slave->cs))
186 return ds;
187 }
188
189 return NULL;
190 }
191
192 /*
193 * BLSP QUPn SPI Hardware Initialisation
194 */
195 static int spi_hw_init(struct ipq_spi_slave *ds)
196 {
197 int ret;
198
199 ds->initialized = 0;
200
201 /* QUPn module configuration */
202 spi_reset(ds);
203
204 /* Set the QUPn state */
205 ret = config_spi_state(ds, QUP_STATE_RESET);
206 if (ret)
207 return ret;
208
209 /*
210 * Configure Mini core to SPI core with Input Output enabled,
211 * SPI master, N = 8 bits
212 */
213 clrsetbits32(ds->regs->qup_config, QUP_CONFIG_MINI_CORE_MSK |
214 QUP_CONF_INPUT_MSK |
215 QUP_CONF_OUTPUT_MSK |
216 QUP_CONF_N_MASK,
217 QUP_CONFIG_MINI_CORE_SPI |
218 QUP_CONF_INPUT_ENA |
219 QUP_CONF_OUTPUT_ENA |
220 QUP_CONF_N_SPI_8_BIT_WORD);
221
222 /*
223 * Configure Input first SPI protocol,
224 * SPI master mode and no loopback
225 */
226 clrsetbits32(ds->regs->spi_config, SPI_CONFIG_LOOP_BACK_MSK |
227 SPI_CONFIG_NO_SLAVE_OPER_MSK,
228 SPI_CONFIG_NO_LOOP_BACK |
229 SPI_CONFIG_NO_SLAVE_OPER);
230
231 /*
232 * Configure SPI IO Control Register
233 * CLK_ALWAYS_ON = 0
234 * MX_CS_MODE = 0
235 * NO_TRI_STATE = 1
236 */
237 write32(ds->regs->io_control, SPI_IO_CTRL_CLK_ALWAYS_ON |
238 SPI_IO_CTRL_NO_TRI_STATE);
239
240 /*
241 * Configure SPI IO Modes.
242 * OUTPUT_BIT_SHIFT_EN = 1
243 * INPUT_MODE = Block Mode
244 * OUTPUT MODE = Block Mode
245 */
246 clrsetbits32(ds->regs->qup_io_modes,
247 QUP_IO_MODES_OUTPUT_BIT_SHIFT_MSK |
248 QUP_IO_MODES_INPUT_MODE_MSK |
249 QUP_IO_MODES_OUTPUT_MODE_MSK,
250 QUP_IO_MODES_OUTPUT_BIT_SHIFT_EN |
251 QUP_IO_MODES_INPUT_BLOCK_MODE |
252 QUP_IO_MODES_OUTPUT_BLOCK_MODE);
253
254 spi_set_mode(ds, ds->mode);
255
256 /* Disable Error mask */
257 write32(ds->regs->error_flags_en, 0);
258 write32(ds->regs->qup_error_flags_en, 0);
259
260 write32(ds->regs->qup_deassert_wait, 0);
261
262 ds->initialized = 1;
263
264 return SUCCESS;
265 }
266
267 static int spi_ctrlr_claim_bus(const struct spi_slave *slave)
268 {
269 struct ipq_spi_slave *ds = to_ipq_spi(slave);
270 unsigned int ret;
271
272 ret = spi_hw_init(ds);
273 if (ret)
274 return -EIO;
275
276 return SUCCESS;
277 }
278
279 static void spi_ctrlr_release_bus(const struct spi_slave *slave)
280 {
281 struct ipq_spi_slave *ds = to_ipq_spi(slave);
282
283 /* Reset the SPI hardware */
284 spi_reset(ds);
285 ds->initialized = 0;
286 }
287
288 static void write_force_cs(const struct spi_slave *slave, int assert)
289 {
290 struct ipq_spi_slave *ds = to_ipq_spi(slave);
291
292 if (assert)
293 clrsetbits32(ds->regs->io_control,
294 SPI_IO_CTRL_FORCE_CS_MSK, SPI_IO_CTRL_FORCE_CS_EN);
295 else
296 clrsetbits32(ds->regs->io_control,
297 SPI_IO_CTRL_FORCE_CS_MSK, SPI_IO_CTRL_FORCE_CS_DIS);
298 }
299
300 /*
301 * Function to write data to OUTPUT FIFO
302 */
303 static void spi_write_byte(struct ipq_spi_slave *ds, unsigned char data)
304 {
305 /* Wait for space in the FIFO */
306 while ((read32(ds->regs->qup_operational) & OUTPUT_FIFO_FULL))
307 udelay(1);
308
309 /* Write the byte of data */
310 write32(ds->regs->qup_output_fifo, data);
311 }
312
313 /*
314 * Function to read data from Input FIFO
315 */
316 static unsigned char spi_read_byte(struct ipq_spi_slave *ds)
317 {
318 /* Wait for Data in FIFO */
319 while (!(read32(ds->regs->qup_operational) & INPUT_FIFO_NOT_EMPTY))
320 udelay(1);
321
322 /* Read a byte of data */
323 return read32(ds->regs->qup_input_fifo) & 0xff;
324 }
325
326 /*
327 * Function to check whether Input or Output FIFO
328 * has data to be serviced
329 */
330 static int check_fifo_status(void *reg_addr)
331 {
332 unsigned int count = TIMEOUT_CNT;
333 unsigned int status_flag;
334 unsigned int val;
335
336 do {
337 val = read32(reg_addr);
338 count--;
339 if (count == 0)
340 return -ETIMEDOUT;
341 status_flag = ((val & OUTPUT_SERVICE_FLAG) |
342 (val & INPUT_SERVICE_FLAG));
343 } while (!status_flag);
344
345 return SUCCESS;
346 }
347
348 /*
349 * Function to configure Input and Output enable/disable
350 */
351 static void enable_io_config(struct ipq_spi_slave *ds,
352 uint32_t write_cnt, uint32_t read_cnt)
353 {
354 if (write_cnt) {
355 clrsetbits32(ds->regs->qup_config,
356 QUP_CONF_OUTPUT_MSK, QUP_CONF_OUTPUT_ENA);
357 } else {
358 clrsetbits32(ds->regs->qup_config,
359 QUP_CONF_OUTPUT_MSK, QUP_CONF_NO_OUTPUT);
360 }
361
362 if (read_cnt) {
363 clrsetbits32(ds->regs->qup_config,
364 QUP_CONF_INPUT_MSK, QUP_CONF_INPUT_ENA);
365 } else {
366 clrsetbits32(ds->regs->qup_config,
367 QUP_CONF_INPUT_MSK, QUP_CONF_NO_INPUT);
368 }
369 }
370
371 /*
372 * Function to read bytes number of data from the Input FIFO
373 */
374 static int __blsp_spi_read(struct ipq_spi_slave *ds, u8 *data_buffer,
375 unsigned int bytes)
376 {
377 uint32_t val;
378 unsigned int i;
379 unsigned int fifo_count;
380 int ret = SUCCESS;
381 int state_config;
382
383 /* Configure no of bytes to read */
384 state_config = config_spi_state(ds, QUP_STATE_RESET);
385 if (state_config)
386 return state_config;
387
388 /* Configure input and output enable */
389 enable_io_config(ds, 0, bytes);
390
391 write32(ds->regs->qup_mx_input_count, bytes);
392
393 state_config = config_spi_state(ds, QUP_STATE_RUN);
394 if (state_config)
395 return state_config;
396
397 while (bytes) {
398 ret = check_fifo_status(ds->regs->qup_operational);
399 if (ret != SUCCESS)
400 goto out;
401
402 val = read32(ds->regs->qup_operational);
403 if (val & INPUT_SERVICE_FLAG) {
404 /*
405 * acknowledge to hw that software will
406 * read input data
407 */
408 val &= INPUT_SERVICE_FLAG;
409 write32(ds->regs->qup_operational, val);
410
411 fifo_count = ((bytes > SPI_INPUT_BLOCK_SIZE) ?
412 SPI_INPUT_BLOCK_SIZE : bytes);
413
414 for (i = 0; i < fifo_count; i++) {
415 *data_buffer = spi_read_byte(ds);
416 data_buffer++;
417 bytes--;
418 }
419 }
420 }
421
422 out:
423 /*
424 * Put the SPI Core back in the Reset State
425 * to end the transfer
426 */
427 (void)config_spi_state(ds, QUP_STATE_RESET);
428 return ret;
429 }
430
431 static int blsp_spi_read(struct ipq_spi_slave *ds, u8 *data_buffer,
432 unsigned int bytes)
433 {
434 int length, ret;
435
436 while (bytes) {
437 length = (bytes < MAX_COUNT_SIZE) ? bytes : MAX_COUNT_SIZE;
438
439 ret = __blsp_spi_read(ds, data_buffer, length);
440 if (ret != SUCCESS)
441 return ret;
442
443 data_buffer += length;
444 bytes -= length;
445 }
446
447 return 0;
448 }
449
450 /*
451 * Function to write data to the Output FIFO
452 */
453 static int __blsp_spi_write(struct ipq_spi_slave *ds, const u8 *cmd_buffer,
454 unsigned int bytes)
455 {
456 uint32_t val;
457 unsigned int i;
458 unsigned int write_len = bytes;
459 unsigned int read_len = bytes;
460 unsigned int fifo_count;
461 int ret = SUCCESS;
462 int state_config;
463
464 state_config = config_spi_state(ds, QUP_STATE_RESET);
465 if (state_config)
466 return state_config;
467
468 /* No of bytes to be written in Output FIFO */
469 write32(ds->regs->qup_mx_output_count, bytes);
470 write32(ds->regs->qup_mx_input_count, bytes);
471 state_config = config_spi_state(ds, QUP_STATE_RUN);
472 if (state_config)
473 return state_config;
474
475 /* Configure input and output enable */
476 enable_io_config(ds, write_len, read_len);
477
478 /*
479 * read_len considered to ensure that we read the dummy data for the
480 * write we performed. This is needed to ensure with WR-RD transaction
481 * to get the actual data on the subsequent read cycle that happens
482 */
483 while (write_len || read_len) {
484 ret = check_fifo_status(ds->regs->qup_operational);
485 if (ret != SUCCESS)
486 goto out;
487
488 val = read32(ds->regs->qup_operational);
489 if (val & OUTPUT_SERVICE_FLAG) {
490 /*
491 * acknowledge to hw that software will write
492 * expected output data
493 */
494 val &= OUTPUT_SERVICE_FLAG;
495 write32(ds->regs->qup_operational, val);
496
497 if (write_len > SPI_OUTPUT_BLOCK_SIZE)
498 fifo_count = SPI_OUTPUT_BLOCK_SIZE;
499 else
500 fifo_count = write_len;
501
502 for (i = 0; i < fifo_count; i++) {
503 /* Write actual data to output FIFO */
504 spi_write_byte(ds, *cmd_buffer);
505 cmd_buffer++;
506 write_len--;
507 }
508 }
509 if (val & INPUT_SERVICE_FLAG) {
510 /*
511 * acknowledge to hw that software
512 * will read input data
513 */
514 val &= INPUT_SERVICE_FLAG;
515 write32(ds->regs->qup_operational, val);
516
517 if (read_len > SPI_INPUT_BLOCK_SIZE)
518 fifo_count = SPI_INPUT_BLOCK_SIZE;
519 else
520 fifo_count = read_len;
521
522 for (i = 0; i < fifo_count; i++) {
523 /* Read dummy data for the data written */
524 (void)spi_read_byte(ds);
525
526 /* Decrement the read count after reading the
527 * dummy data from the device. This is to make
528 * sure we read dummy data before we write the
529 * data to fifo
530 */
531 read_len--;
532 }
533 }
534 }
535
536 out:
537 /*
538 * Put the SPI Core back in the Reset State
539 * to end the transfer
540 */
541 (void)config_spi_state(ds, QUP_STATE_RESET);
542
543 return ret;
544 }
545
546 static int blsp_spi_write(struct ipq_spi_slave *ds, u8 *cmd_buffer,
547 unsigned int bytes)
548 {
549 int length, ret;
550
551 while (bytes) {
552 length = (bytes < MAX_COUNT_SIZE) ? bytes : MAX_COUNT_SIZE;
553
554 ret = __blsp_spi_write(ds, cmd_buffer, length);
555 if (ret != SUCCESS)
556 return ret;
557
558 cmd_buffer += length;
559 bytes -= length;
560 }
561
562 return 0;
563 }
564
565 /*
566 * This function is invoked with either tx_buf or rx_buf.
567 * Calling this function with both null does a chip select change.
568 */
569 static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout,
570 size_t out_bytes, void *din, size_t in_bytes)
571 {
572 struct ipq_spi_slave *ds = to_ipq_spi(slave);
573 u8 *txp = (u8 *)dout;
574 u8 *rxp = (u8 *)din;
575 int ret;
576
577 /* Driver implementation does not support full duplex. */
578 if (dout && din)
579 return -1;
580
581 ret = config_spi_state(ds, QUP_STATE_RESET);
582 if (ret != SUCCESS)
583 return ret;
584
585 write_force_cs(slave, 1);
586
587 if (dout != NULL) {
588 ret = blsp_spi_write(ds, txp, (unsigned int)out_bytes);
589 if (ret != SUCCESS)
590 goto out;
591 }
592
593 if (din != NULL) {
594 ret = blsp_spi_read(ds, rxp, in_bytes);
595 if (ret != SUCCESS)
596 goto out;
597 }
598
599 out:
600 write_force_cs(slave, 0);
601
602 /*
603 * Put the SPI Core back in the Reset State
604 * to end the transfer
605 */
606 (void)config_spi_state(ds, QUP_STATE_RESET);
607
608 return ret;
609 }
610
611 static int spi_ctrlr_setup(const struct spi_slave *slave)
612 {
613 struct ipq_spi_slave *ds = NULL;
614 int i;
615 int bus = slave->bus;
616 int cs = slave->cs;
617
618 if ((bus < BLSP0_SPI) || (bus > BLSP1_SPI)
619 || ((bus == BLSP0_SPI) && (cs > 2))
620 || ((bus == BLSP1_SPI) && (cs > 0))) {
621 printk(BIOS_ERR,
622 "SPI error: unsupported bus %d (Supported buses 0, 1 and 2) "
623 "or chipselect\n", bus);
624 return -1;
625 }
626
627 for (i = 0; i < ARRAY_SIZE(spi_slave_pool); i++) {
628 if (spi_slave_pool[i].allocated)
629 continue;
630 ds = spi_slave_pool + i;
631
632 ds->slave.bus = bus;
633 ds->slave.cs = cs;
634 ds->regs = &spi_reg[bus];
635
636 /*
637 * TODO(vbendeb):
638 * hardcoded frequency and mode - we might need to find a way
639 * to configure this
640 */
641 ds->freq = 10000000;
642 ds->mode = SPI_MODE3;
643 ds->allocated = 1;
644
645 return 0;
646 }
647
648 printk(BIOS_ERR, "SPI error: all %d pools busy\n", i);
649 return -1;
650 }
651
652 static const struct spi_ctrlr spi_ctrlr = {
653 .setup = spi_ctrlr_setup,
654 .claim_bus = spi_ctrlr_claim_bus,
655 .release_bus = spi_ctrlr_release_bus,
656 .xfer = spi_ctrlr_xfer,
657 .max_xfer_size = MAX_PACKET_COUNT,
658 };
659
660 const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
661 {
662 .ctrlr = &spi_ctrlr,
663 .bus_start = BLSP0_SPI,
664 .bus_end = BLSP1_SPI,
665 },
666 };
667
668 const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map);