Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2006 Ben Warren, Qstreams Networks Inc. |
Stefan Roese | 88fbf93 | 2010-04-15 16:07:28 +0200 | [diff] [blame] | 4 | * With help from the common/soft_spi and arch/powerpc/cpu/mpc8260 drivers |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Ben Warren | 3753140 | 2008-01-26 23:41:19 -0500 | [diff] [blame] | 8 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 9 | #include <malloc.h> |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 10 | #include <spi.h> |
| 11 | #include <asm/mpc8xxx_spi.h> |
| 12 | |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 13 | #define SPI_EV_NE (0x80000000 >> 22) /* Receiver Not Empty */ |
| 14 | #define SPI_EV_NF (0x80000000 >> 23) /* Transmitter Not Full */ |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 15 | |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 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 */ |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 20 | |
| 21 | #define SPI_TIMEOUT 1000 |
| 22 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 23 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 24 | unsigned int max_hz, unsigned int mode) |
| 25 | { |
| 26 | struct spi_slave *slave; |
| 27 | |
| 28 | if (!spi_cs_is_valid(bus, cs)) |
| 29 | return NULL; |
| 30 | |
Simon Glass | d034a95 | 2013-03-18 19:23:40 +0000 | [diff] [blame] | 31 | slave = spi_alloc_slave_base(bus, cs); |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 32 | if (!slave) |
| 33 | return NULL; |
| 34 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 35 | /* |
| 36 | * TODO: Some of the code in spi_init() should probably move |
| 37 | * here, or into spi_claim_bus() below. |
| 38 | */ |
| 39 | |
| 40 | return slave; |
| 41 | } |
| 42 | |
| 43 | void spi_free_slave(struct spi_slave *slave) |
| 44 | { |
| 45 | free(slave); |
| 46 | } |
| 47 | |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 48 | void spi_init(void) |
| 49 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 50 | volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 51 | |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 52 | /* |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 53 | * SPI pins on the MPC83xx are not muxed, so all we do is initialize |
| 54 | * some registers |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 55 | */ |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 56 | spi->mode = SPI_MODE_REV | SPI_MODE_MS | SPI_MODE_EN; |
Jagan Teki | f9c9f6a | 2015-10-23 01:38:07 +0530 | [diff] [blame] | 57 | spi->mode = (spi->mode & 0xfff0ffff) | BIT(16); /* Use SYSCLK / 8 |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 58 | (16.67MHz typ.) */ |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 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 */ |
| 62 | } |
| 63 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 64 | int spi_claim_bus(struct spi_slave *slave) |
| 65 | { |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | void spi_release_bus(struct spi_slave *slave) |
| 70 | { |
| 71 | |
| 72 | } |
| 73 | |
| 74 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, |
| 75 | void *din, unsigned long flags) |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 76 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 77 | volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 78 | unsigned int tmpdout, tmpdin, event; |
Axel Lin | 3db3928 | 2013-07-12 17:42:15 +0800 | [diff] [blame] | 79 | int numBlks = DIV_ROUND_UP(bitlen, 32); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 80 | int tm, isRead = 0; |
| 81 | unsigned char charSize = 32; |
| 82 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 83 | debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n", |
| 84 | slave->bus, slave->cs, *(uint *) dout, *(uint *) din, bitlen); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 85 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 86 | if (flags & SPI_XFER_BEGIN) |
| 87 | spi_cs_activate(slave); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 88 | |
| 89 | spi->event = 0xffffffff; /* Clear all SPI events */ |
| 90 | |
| 91 | /* handle data in 32-bit chunks */ |
| 92 | while (numBlks--) { |
| 93 | tmpdout = 0; |
| 94 | charSize = (bitlen >= 32 ? 32 : bitlen); |
| 95 | |
| 96 | /* Shift data so it's msb-justified */ |
| 97 | tmpdout = *(u32 *) dout >> (32 - charSize); |
| 98 | |
| 99 | /* The LEN field of the SPMODE register is set as follows: |
| 100 | * |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 101 | * Bit length setting |
| 102 | * len <= 4 3 |
| 103 | * 4 < len <= 16 len - 1 |
| 104 | * len > 16 0 |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 105 | */ |
| 106 | |
Ira W. Snyder | 5261b00 | 2012-09-12 14:17:31 -0700 | [diff] [blame] | 107 | spi->mode &= ~SPI_MODE_EN; |
| 108 | |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 109 | if (bitlen <= 16) { |
| 110 | if (bitlen <= 4) |
| 111 | spi->mode = (spi->mode & 0xff0fffff) | |
Wolfgang Denk | ec7fbf5 | 2013-10-04 17:43:24 +0200 | [diff] [blame] | 112 | (3 << 20); |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 113 | else |
| 114 | spi->mode = (spi->mode & 0xff0fffff) | |
Wolfgang Denk | ec7fbf5 | 2013-10-04 17:43:24 +0200 | [diff] [blame] | 115 | ((bitlen - 1) << 20); |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 116 | } else { |
| 117 | spi->mode = (spi->mode & 0xff0fffff); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 118 | /* Set up the next iteration if sending > 32 bits */ |
| 119 | bitlen -= 32; |
| 120 | dout += 4; |
| 121 | } |
| 122 | |
Ira W. Snyder | 5261b00 | 2012-09-12 14:17:31 -0700 | [diff] [blame] | 123 | spi->mode |= SPI_MODE_EN; |
| 124 | |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 125 | spi->tx = tmpdout; /* Write the data out */ |
| 126 | debug("*** spi_xfer: ... %08x written\n", tmpdout); |
| 127 | |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 128 | /* |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 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 |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 132 | */ |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 133 | for (tm = 0, isRead = 0; tm < SPI_TIMEOUT; ++tm) { |
| 134 | event = spi->event; |
| 135 | if (event & SPI_EV_NE) { |
| 136 | tmpdin = spi->rx; |
| 137 | spi->event |= SPI_EV_NE; |
| 138 | isRead = 1; |
| 139 | |
| 140 | *(u32 *) din = (tmpdin << (32 - charSize)); |
| 141 | if (charSize == 32) { |
| 142 | /* Advance output buffer by 32 bits */ |
| 143 | din += 4; |
| 144 | } |
| 145 | } |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 146 | /* |
| 147 | * Only bail when we've had both NE and NF events. |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 148 | * This will cause timeouts on RO devices, so maybe |
| 149 | * in the future put an arbitrary delay after writing |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 150 | * the device. Arbitrary delays suck, though... |
| 151 | */ |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 152 | if (isRead && (event & SPI_EV_NF)) |
| 153 | break; |
| 154 | } |
| 155 | if (tm >= SPI_TIMEOUT) |
| 156 | puts("*** spi_xfer: Time out during SPI transfer"); |
| 157 | |
| 158 | debug("*** spi_xfer: transfer ended. Value=%08x\n", tmpdin); |
| 159 | } |
| 160 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 161 | if (flags & SPI_XFER_END) |
| 162 | spi_cs_deactivate(slave); |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 163 | |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 164 | return 0; |
| 165 | } |