1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2006 Ben Warren, Qstreams Networks Inc.
4 * With help from the common/soft_spi and arch/powerpc/cpu/mpc8260 drivers
11 #include <asm/mpc8xxx_spi.h>
13 #define SPI_EV_NE (0x80000000 >> 22) /* Receiver Not Empty */
14 #define SPI_EV_NF (0x80000000 >> 23) /* Transmitter Not Full */
16 #define SPI_MODE_LOOP (0x80000000 >> 1) /* Loopback mode */
17 #define SPI_MODE_REV (0x80000000 >> 5) /* Reverse mode - MSB first */
18 #define SPI_MODE_MS (0x80000000 >> 6) /* Always master */
19 #define SPI_MODE_EN (0x80000000 >> 7) /* Enable interface */
21 #define SPI_TIMEOUT 1000
23 struct spi_slave *spi_setup_slave(uint bus, uint cs, uint max_hz, uint mode)
25 struct spi_slave *slave;
27 if (!spi_cs_is_valid(bus, cs))
30 slave = spi_alloc_slave_base(bus, cs);
35 * TODO: Some of the code in spi_init() should probably move
36 * here, or into spi_claim_bus() below.
42 void spi_free_slave(struct spi_slave *slave)
49 volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi;
52 * SPI pins on the MPC83xx are not muxed, so all we do is initialize
55 spi->mode = SPI_MODE_REV | SPI_MODE_MS | SPI_MODE_EN;
56 /* Use SYSCLK / 8 (16.67MHz typ.) */
57 spi->mode = (spi->mode & 0xfff0ffff) | BIT(16);
58 /* Clear all SPI events */
59 spi->event = 0xffffffff;
60 /* Mask all SPI interrupts */
61 spi->mask = 0x00000000;
62 /* LST bit doesn't do anything, so disregard */
66 int spi_claim_bus(struct spi_slave *slave)
71 void spi_release_bus(struct spi_slave *slave)
75 int spi_xfer(struct spi_slave *slave, uint bitlen, const void *dout, void *din,
78 volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi;
79 uint tmpdout, tmpdin, event;
80 int num_blks = DIV_ROUND_UP(bitlen, 32);
84 debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
85 slave->bus, slave->cs, *(uint *) dout, *(uint *) din, bitlen);
87 if (flags & SPI_XFER_BEGIN)
88 spi_cs_activate(slave);
90 /* Clear all SPI events */
91 spi->event = 0xffffffff;
93 /* Handle data in 32-bit chunks */
96 char_size = (bitlen >= 32 ? 32 : bitlen);
98 /* Shift data so it's msb-justified */
99 tmpdout = *(u32 *) dout >> (32 - char_size);
101 /* The LEN field of the SPMODE register is set as follows:
105 * 4 < len <= 16 len - 1
109 spi->mode &= ~SPI_MODE_EN;
113 spi->mode = (spi->mode & 0xff0fffff) |
116 spi->mode = (spi->mode & 0xff0fffff) |
117 ((bitlen - 1) << 20);
119 spi->mode = (spi->mode & 0xff0fffff);
120 /* Set up the next iteration if sending > 32 bits */
125 spi->mode |= SPI_MODE_EN;
127 /* Write the data out */
130 debug("*** spi_xfer: ... %08x written\n", tmpdout);
133 * Wait for SPI transmit to get out
134 * or time out (1 second = 1000 ms)
135 * The NE event must be read and cleared first
137 for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
139 if (event & SPI_EV_NE) {
141 spi->event |= SPI_EV_NE;
144 *(u32 *) din = (tmpdin << (32 - char_size));
145 if (char_size == 32) {
146 /* Advance output buffer by 32 bits */
151 * Only bail when we've had both NE and NF events.
152 * This will cause timeouts on RO devices, so maybe
153 * in the future put an arbitrary delay after writing
154 * the device. Arbitrary delays suck, though...
156 if (is_read && (event & SPI_EV_NF))
159 if (tm >= SPI_TIMEOUT)
160 puts("*** spi_xfer: Time out during SPI transfer");
162 debug("*** spi_xfer: transfer ended. Value=%08x\n", tmpdin);
165 if (flags & SPI_XFER_END)
166 spi_cs_deactivate(slave);