1 // SPDX-License-Identifier: GPL-2.0+
3 * eSPI controller driver.
5 * Copyright 2010-2011 Freescale Semiconductor, Inc.
7 * Author: Mingkai Hu (Mingkai.hu@freescale.com)
8 * Chuanhua Han (chuanhua.han@nxp.com)
13 #include <linux/bitops.h>
14 #include <linux/delay.h>
18 #include <asm/global_data.h>
19 #include <asm/immap_85xx.h>
23 #include <dm/platform_data/fsl_espi.h>
25 struct fsl_spi_slave {
26 struct spi_slave slave;
37 unsigned int max_transfer_length;
40 #define to_fsl_spi_slave(s) container_of(s, struct fsl_spi_slave, slave)
41 #define US_PER_SECOND 1000000UL
43 /* default SCK frequency, unit: HZ */
44 #define FSL_ESPI_DEFAULT_SCK_FREQ 10000000
46 #define ESPI_MAX_CS_NUM 4
47 #define ESPI_FIFO_WIDTH_BIT 32
49 #define ESPI_EV_RNE BIT(9)
50 #define ESPI_EV_TNF BIT(8)
51 #define ESPI_EV_DON BIT(14)
52 #define ESPI_EV_TXE BIT(15)
53 #define ESPI_EV_RFCNT_SHIFT 24
54 #define ESPI_EV_RFCNT_MASK (0x3f << ESPI_EV_RFCNT_SHIFT)
56 #define ESPI_MODE_EN BIT(31) /* Enable interface */
57 #define ESPI_MODE_TXTHR(x) ((x) << 8) /* Tx FIFO threshold */
58 #define ESPI_MODE_RXTHR(x) ((x) << 0) /* Rx FIFO threshold */
60 #define ESPI_COM_CS(x) ((x) << 30)
61 #define ESPI_COM_TRANLEN(x) ((x) << 0)
63 #define ESPI_CSMODE_CI_INACTIVEHIGH BIT(31)
64 #define ESPI_CSMODE_CP_BEGIN_EDGCLK BIT(30)
65 #define ESPI_CSMODE_REV_MSB_FIRST BIT(29)
66 #define ESPI_CSMODE_DIV16 BIT(28)
67 #define ESPI_CSMODE_PM(x) ((x) << 24)
68 #define ESPI_CSMODE_POL_ASSERTED_LOW BIT(20)
69 #define ESPI_CSMODE_LEN(x) ((x) << 16)
70 #define ESPI_CSMODE_CSBEF(x) ((x) << 12)
71 #define ESPI_CSMODE_CSAFT(x) ((x) << 8)
72 #define ESPI_CSMODE_CSCG(x) ((x) << 3)
74 #define ESPI_CSMODE_INIT_VAL (ESPI_CSMODE_POL_ASSERTED_LOW | \
75 ESPI_CSMODE_CSBEF(0) | ESPI_CSMODE_CSAFT(0) | \
78 #define ESPI_MAX_DATA_TRANSFER_LEN 0xFFF0
80 void fsl_spi_cs_activate(struct spi_slave *slave, uint cs)
82 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
83 ccsr_espi_t *espi = fsl->espi;
85 size_t data_len = fsl->data_len;
87 com &= ~(ESPI_COM_CS(0x3) | ESPI_COM_TRANLEN(0xFFFF));
88 com |= ESPI_COM_CS(cs);
89 com |= ESPI_COM_TRANLEN(data_len - 1);
90 out_be32(&espi->com, com);
93 void fsl_spi_cs_deactivate(struct spi_slave *slave)
95 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
96 ccsr_espi_t *espi = fsl->espi;
98 /* clear the RXCNT and TXCNT */
99 out_be32(&espi->mode, in_be32(&espi->mode) & (~ESPI_MODE_EN));
100 out_be32(&espi->mode, in_be32(&espi->mode) | ESPI_MODE_EN);
103 static void fsl_espi_tx(struct fsl_spi_slave *fsl, const void *dout)
105 ccsr_espi_t *espi = fsl->espi;
106 unsigned int tmpdout, event;
110 tmpdout = *(u32 *)dout;
114 out_be32(&espi->tx, tmpdout);
115 out_be32(&espi->event, ESPI_EV_TNF);
116 debug("***spi_xfer:...%08x written\n", tmpdout);
118 tmp_tx_timeout = fsl->tx_timeout;
119 /* Wait for eSPI transmit to go out */
120 while (tmp_tx_timeout--) {
121 event = in_be32(&espi->event);
122 if (event & ESPI_EV_DON || event & ESPI_EV_TXE) {
123 out_be32(&espi->event, ESPI_EV_TXE);
129 if (tmp_tx_timeout < 0)
130 debug("***spi_xfer:...Tx timeout! event = %08x\n", event);
133 static int fsl_espi_rx(struct fsl_spi_slave *fsl, void *din,
136 ccsr_espi_t *espi = fsl->espi;
137 unsigned int tmpdin, rx_times;
138 unsigned char *buf, *p_cursor;
143 rx_times = DIV_ROUND_UP(bytes, 4);
144 buf = (unsigned char *)malloc(4 * rx_times);
146 debug("SF: Failed to malloc memory.\n");
151 tmpdin = in_be32(&espi->rx);
152 debug("***spi_xfer:...%08x readed\n", tmpdin);
153 *(u32 *)p_cursor = tmpdin;
158 memcpy(din, buf, bytes);
161 out_be32(&espi->event, ESPI_EV_RNE);
166 void espi_release_bus(struct fsl_spi_slave *fsl)
168 /* Disable the SPI hardware */
169 out_be32(&fsl->espi->mode,
170 in_be32(&fsl->espi->mode) & (~ESPI_MODE_EN));
173 int espi_xfer(struct fsl_spi_slave *fsl, uint cs, unsigned int bitlen,
174 const void *data_out, void *data_in, unsigned long flags)
176 struct spi_slave *slave = &fsl->slave;
177 ccsr_espi_t *espi = fsl->espi;
178 unsigned int event, rx_bytes;
179 const void *dout = NULL;
182 int num_blks, num_chunks, max_tran_len, tran_len;
184 unsigned char *buffer = NULL;
186 u8 *cmd_buf = fsl->cmd_buf;
187 size_t cmd_len = fsl->cmd_len;
188 size_t data_len = bitlen / 8;
189 size_t rx_offset = 0;
192 max_tran_len = fsl->max_transfer_length;
196 fsl->cmd_len = cmd_len;
197 memcpy(cmd_buf, data_out, cmd_len);
202 fsl_spi_cs_deactivate(slave);
205 buf_len = 2 * cmd_len + min(data_len, (size_t)max_tran_len);
206 len = cmd_len + data_len;
208 buffer = (unsigned char *)malloc(buf_len);
210 debug("SF: Failed to malloc memory.\n");
213 memcpy(buffer, cmd_buf, cmd_len);
215 memcpy(buffer + cmd_len, data_out, data_len);
217 case SPI_XFER_BEGIN | SPI_XFER_END:
219 buffer = (unsigned char *)malloc(len * 2);
221 debug("SF: Failed to malloc memory.\n");
224 memcpy(buffer, data_out, len);
230 debug("spi_xfer: data_out %08X(%p) data_in %08X(%p) len %u\n",
231 *(uint *)data_out, data_out, *(uint *)data_in, data_in, len);
233 num_chunks = DIV_ROUND_UP(data_len, max_tran_len);
234 while (num_chunks--) {
236 din = buffer + rx_offset;
238 tran_len = min(data_len, (size_t)max_tran_len);
239 num_blks = DIV_ROUND_UP(tran_len + cmd_len, 4);
240 num_bytes = (tran_len + cmd_len) % 4;
241 fsl->data_len = tran_len + cmd_len;
242 fsl_spi_cs_activate(slave, cs);
244 /* Clear all eSPI events */
245 out_be32(&espi->event , 0xffffffff);
246 /* handle data in 32-bit chunks */
248 event = in_be32(&espi->event);
249 if (event & ESPI_EV_TNF) {
250 fsl_espi_tx(fsl, dout);
251 /* Set up the next iteration */
258 event = in_be32(&espi->event);
259 if (event & ESPI_EV_RNE) {
260 rf_cnt = ((event & ESPI_EV_RFCNT_MASK)
261 >> ESPI_EV_RFCNT_SHIFT);
264 else if (num_blks == 1 && rf_cnt == num_bytes)
265 rx_bytes = num_bytes;
268 if (fsl_espi_rx(fsl, din, rx_bytes)
272 din = (unsigned char *)din
278 memcpy(data_in, buffer + 2 * cmd_len, tran_len);
279 if (*buffer == 0x0b) {
281 data_len -= tran_len;
282 *(int *)buffer += tran_len;
285 fsl_spi_cs_deactivate(slave);
292 void espi_claim_bus(struct fsl_spi_slave *fsl, unsigned int cs)
294 ccsr_espi_t *espi = fsl->espi;
295 unsigned char pm = fsl->pm;
296 unsigned int mode = fsl->mode;
297 unsigned int div16 = fsl->div16;
300 /* Enable eSPI interface */
301 out_be32(&espi->mode, ESPI_MODE_RXTHR(3)
302 | ESPI_MODE_TXTHR(4) | ESPI_MODE_EN);
304 out_be32(&espi->event, 0xffffffff); /* Clear all eSPI events */
305 out_be32(&espi->mask, 0x00000000); /* Mask all eSPI interrupts */
307 /* Init CS mode interface */
308 for (i = 0; i < ESPI_MAX_CS_NUM; i++)
309 out_be32(&espi->csmode[i], ESPI_CSMODE_INIT_VAL);
311 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs]) &
312 ~(ESPI_CSMODE_PM(0xF) | ESPI_CSMODE_DIV16
313 | ESPI_CSMODE_CI_INACTIVEHIGH | ESPI_CSMODE_CP_BEGIN_EDGCLK
314 | ESPI_CSMODE_REV_MSB_FIRST | ESPI_CSMODE_LEN(0xF)));
316 /* Set eSPI BRG clock source */
317 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
318 | ESPI_CSMODE_PM(pm) | div16);
322 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
323 | ESPI_CSMODE_CP_BEGIN_EDGCLK);
325 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
326 | ESPI_CSMODE_CI_INACTIVEHIGH);
328 /* Character bit order: msb first */
329 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
330 | ESPI_CSMODE_REV_MSB_FIRST);
332 /* Character length in bits, between 0x3~0xf, i.e. 4bits~16bits */
333 out_be32(&espi->csmode[cs], in_be32(&espi->csmode[cs])
334 | ESPI_CSMODE_LEN(7));
337 void espi_setup_slave(struct fsl_spi_slave *fsl)
341 unsigned long spibrg = 0;
342 unsigned long spi_freq = 0;
343 unsigned char pm = 0;
345 max_hz = fsl->speed_hz;
347 get_sys_info(&sysinfo);
348 spibrg = sysinfo.freq_systembus / 2;
350 if ((spibrg / max_hz) > 32) {
351 fsl->div16 = ESPI_CSMODE_DIV16;
352 pm = spibrg / (max_hz * 16 * 2);
355 debug("max_hz is too low: %d Hz, %ld Hz is used.\n",
356 max_hz, spibrg / (32 * 16));
359 pm = spibrg / (max_hz * 2);
366 spi_freq = spibrg / ((pm + 1) * 2 * 16);
368 spi_freq = spibrg / ((pm + 1) * 2);
370 /* set tx_timeout to 10 times of one espi FIFO entry go out */
371 fsl->tx_timeout = DIV_ROUND_UP((US_PER_SECOND * ESPI_FIFO_WIDTH_BIT
372 * 10), spi_freq);/* Set eSPI BRG clock source */
375 #if !CONFIG_IS_ENABLED(DM_SPI)
376 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
378 return bus == 0 && cs < ESPI_MAX_CS_NUM;
381 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
382 unsigned int max_hz, unsigned int mode)
384 struct fsl_spi_slave *fsl;
386 if (!spi_cs_is_valid(bus, cs))
389 fsl = spi_alloc_slave(struct fsl_spi_slave, bus, cs);
393 fsl->espi = (void *)(CONFIG_SYS_MPC85xx_ESPI_ADDR);
395 fsl->max_transfer_length = ESPI_MAX_DATA_TRANSFER_LEN;
396 fsl->speed_hz = max_hz;
398 espi_setup_slave(fsl);
403 void spi_free_slave(struct spi_slave *slave)
405 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
410 int spi_claim_bus(struct spi_slave *slave)
412 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
414 espi_claim_bus(fsl, slave->cs);
419 void spi_release_bus(struct spi_slave *slave)
421 struct fsl_spi_slave *fsl = to_fsl_spi_slave(slave);
423 espi_release_bus(fsl);
426 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
427 void *din, unsigned long flags)
429 struct fsl_spi_slave *fsl = (struct fsl_spi_slave *)slave;
431 return espi_xfer(fsl, slave->cs, bitlen, dout, din, flags);
434 static void __espi_set_speed(struct fsl_spi_slave *fsl)
436 espi_setup_slave(fsl);
438 /* Set eSPI BRG clock source */
439 out_be32(&fsl->espi->csmode[fsl->cs],
440 in_be32(&fsl->espi->csmode[fsl->cs])
441 | ESPI_CSMODE_PM(fsl->pm) | fsl->div16);
444 static void __espi_set_mode(struct fsl_spi_slave *fsl)
447 if (fsl->mode & SPI_CPHA)
448 out_be32(&fsl->espi->csmode[fsl->cs],
449 in_be32(&fsl->espi->csmode[fsl->cs])
450 | ESPI_CSMODE_CP_BEGIN_EDGCLK);
451 if (fsl->mode & SPI_CPOL)
452 out_be32(&fsl->espi->csmode[fsl->cs],
453 in_be32(&fsl->espi->csmode[fsl->cs])
454 | ESPI_CSMODE_CI_INACTIVEHIGH);
457 static int fsl_espi_claim_bus(struct udevice *dev)
459 struct udevice *bus = dev->parent;
460 struct fsl_spi_slave *fsl = dev_get_priv(bus);
462 espi_claim_bus(fsl, fsl->cs);
467 static int fsl_espi_release_bus(struct udevice *dev)
469 struct udevice *bus = dev->parent;
470 struct fsl_spi_slave *fsl = dev_get_priv(bus);
472 espi_release_bus(fsl);
477 static int fsl_espi_xfer(struct udevice *dev, unsigned int bitlen,
478 const void *dout, void *din, unsigned long flags)
480 struct udevice *bus = dev->parent;
481 struct fsl_spi_slave *fsl = dev_get_priv(bus);
483 return espi_xfer(fsl, fsl->cs, bitlen, dout, din, flags);
486 static int fsl_espi_set_speed(struct udevice *bus, uint speed)
488 struct fsl_spi_slave *fsl = dev_get_priv(bus);
490 debug("%s speed %u\n", __func__, speed);
491 fsl->speed_hz = speed;
493 __espi_set_speed(fsl);
498 static int fsl_espi_set_mode(struct udevice *bus, uint mode)
500 struct fsl_spi_slave *fsl = dev_get_priv(bus);
502 debug("%s mode %u\n", __func__, mode);
505 __espi_set_mode(fsl);
510 static int fsl_espi_child_pre_probe(struct udevice *dev)
512 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
513 struct udevice *bus = dev->parent;
514 struct fsl_spi_slave *fsl = dev_get_priv(bus);
516 debug("%s cs %u\n", __func__, slave_plat->cs);
517 fsl->cs = slave_plat->cs;
522 static int fsl_espi_probe(struct udevice *bus)
524 struct fsl_espi_plat *plat = dev_get_plat(bus);
525 struct fsl_spi_slave *fsl = dev_get_priv(bus);
527 fsl->espi = (ccsr_espi_t *)((u32)plat->regs_addr);
528 fsl->max_transfer_length = ESPI_MAX_DATA_TRANSFER_LEN;
529 fsl->speed_hz = plat->speed_hz;
531 debug("%s probe done, bus-num %d.\n", bus->name, dev_seq(bus));
536 static const struct dm_spi_ops fsl_espi_ops = {
537 .claim_bus = fsl_espi_claim_bus,
538 .release_bus = fsl_espi_release_bus,
539 .xfer = fsl_espi_xfer,
540 .set_speed = fsl_espi_set_speed,
541 .set_mode = fsl_espi_set_mode,
544 #if CONFIG_IS_ENABLED(OF_REAL)
545 static int fsl_espi_of_to_plat(struct udevice *bus)
548 struct fsl_espi_plat *plat = dev_get_plat(bus);
549 const void *blob = gd->fdt_blob;
550 int node = dev_of_offset(bus);
552 addr = dev_read_addr(bus);
553 if (addr == FDT_ADDR_T_NONE)
556 plat->regs_addr = lower_32_bits(addr);
557 plat->speed_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
558 FSL_ESPI_DEFAULT_SCK_FREQ);
560 debug("ESPI: regs=%p, max-frequency=%d\n",
561 &plat->regs_addr, plat->speed_hz);
566 static const struct udevice_id fsl_espi_ids[] = {
567 { .compatible = "fsl,mpc8536-espi" },
572 U_BOOT_DRIVER(fsl_espi) = {
575 #if CONFIG_IS_ENABLED(OF_REAL)
576 .of_match = fsl_espi_ids,
577 .of_to_plat = fsl_espi_of_to_plat,
579 .ops = &fsl_espi_ops,
580 .plat_auto = sizeof(struct fsl_espi_plat),
581 .priv_auto = sizeof(struct fsl_spi_slave),
582 .probe = fsl_espi_probe,
583 .child_pre_probe = fsl_espi_child_pre_probe,