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(unsigned int bus, unsigned int cs,
24 unsigned int max_hz, unsigned int mode)
26 struct spi_slave *slave;
28 if (!spi_cs_is_valid(bus, cs))
31 slave = spi_alloc_slave_base(bus, cs);
36 * TODO: Some of the code in spi_init() should probably move
37 * here, or into spi_claim_bus() below.
43 void spi_free_slave(struct spi_slave *slave)
50 volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi;
53 * SPI pins on the MPC83xx are not muxed, so all we do is initialize
56 spi->mode = SPI_MODE_REV | SPI_MODE_MS | SPI_MODE_EN;
57 spi->mode = (spi->mode & 0xfff0ffff) | BIT(16); /* Use SYSCLK / 8
59 spi->event = 0xffffffff; /* Clear all SPI events */
60 spi->mask = 0x00000000; /* Mask all SPI interrupts */
61 spi->com = 0; /* LST bit doesn't do anything, so disregard */
64 int spi_claim_bus(struct spi_slave *slave)
69 void spi_release_bus(struct spi_slave *slave)
74 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
75 void *din, unsigned long flags)
77 volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi;
78 unsigned int tmpdout, tmpdin, event;
79 int numBlks = DIV_ROUND_UP(bitlen, 32);
81 unsigned char charSize = 32;
83 debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
84 slave->bus, slave->cs, *(uint *) dout, *(uint *) din, bitlen);
86 if (flags & SPI_XFER_BEGIN)
87 spi_cs_activate(slave);
89 spi->event = 0xffffffff; /* Clear all SPI events */
91 /* handle data in 32-bit chunks */
94 charSize = (bitlen >= 32 ? 32 : bitlen);
96 /* Shift data so it's msb-justified */
97 tmpdout = *(u32 *) dout >> (32 - charSize);
99 /* The LEN field of the SPMODE register is set as follows:
103 * 4 < len <= 16 len - 1
107 spi->mode &= ~SPI_MODE_EN;
111 spi->mode = (spi->mode & 0xff0fffff) |
114 spi->mode = (spi->mode & 0xff0fffff) |
115 ((bitlen - 1) << 20);
117 spi->mode = (spi->mode & 0xff0fffff);
118 /* Set up the next iteration if sending > 32 bits */
123 spi->mode |= SPI_MODE_EN;
125 spi->tx = tmpdout; /* Write the data out */
126 debug("*** spi_xfer: ... %08x written\n", tmpdout);
129 * Wait for SPI transmit to get out
130 * or time out (1 second = 1000 ms)
131 * The NE event must be read and cleared first
133 for (tm = 0, isRead = 0; tm < SPI_TIMEOUT; ++tm) {
135 if (event & SPI_EV_NE) {
137 spi->event |= SPI_EV_NE;
140 *(u32 *) din = (tmpdin << (32 - charSize));
141 if (charSize == 32) {
142 /* Advance output buffer by 32 bits */
147 * Only bail when we've had both NE and NF events.
148 * This will cause timeouts on RO devices, so maybe
149 * in the future put an arbitrary delay after writing
150 * the device. Arbitrary delays suck, though...
152 if (isRead && (event & SPI_EV_NF))
155 if (tm >= SPI_TIMEOUT)
156 puts("*** spi_xfer: Time out during SPI transfer");
158 debug("*** spi_xfer: transfer ended. Value=%08x\n", tmpdin);
161 if (flags & SPI_XFER_END)
162 spi_cs_deactivate(slave);