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 | |
Mario Six | 10f300a | 2019-04-29 01:58:41 +0530 | [diff] [blame] | 13 | enum { |
| 14 | SPI_EV_NE = BIT(31 - 22), /* Receiver Not Empty */ |
| 15 | SPI_EV_NF = BIT(31 - 23), /* Transmitter Not Full */ |
| 16 | }; |
| 17 | |
| 18 | enum { |
| 19 | SPI_MODE_LOOP = BIT(31 - 1), /* Loopback mode */ |
| 20 | SPI_MODE_CI = BIT(31 - 2), /* Clock invert */ |
| 21 | SPI_MODE_CP = BIT(31 - 3), /* Clock phase */ |
| 22 | SPI_MODE_DIV16 = BIT(31 - 4), /* Divide clock source by 16 */ |
| 23 | SPI_MODE_REV = BIT(31 - 5), /* Reverse mode - MSB first */ |
| 24 | SPI_MODE_MS = BIT(31 - 6), /* Always master */ |
| 25 | SPI_MODE_EN = BIT(31 - 7), /* Enable interface */ |
| 26 | |
| 27 | SPI_MODE_LEN_MASK = 0xf00000, |
| 28 | SPI_MODE_PM_MASK = 0xf0000, |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 29 | |
Mario Six | 10f300a | 2019-04-29 01:58:41 +0530 | [diff] [blame] | 30 | SPI_COM_LST = BIT(31 - 9), |
| 31 | }; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 32 | |
Mario Six | eb2533a | 2019-04-29 01:58:47 +0530 | [diff] [blame^] | 33 | static inline u32 to_prescale_mod(u32 val) |
| 34 | { |
| 35 | return (min(val, (u32)15) << 16); |
| 36 | } |
| 37 | |
| 38 | static void set_char_len(spi8xxx_t *spi, u32 val) |
| 39 | { |
| 40 | clrsetbits_be32(&spi->mode, SPI_MODE_LEN_MASK, (val << 20)); |
| 41 | } |
| 42 | |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 43 | #define SPI_TIMEOUT 1000 |
| 44 | |
Mario Six | 4f4936e | 2019-04-29 01:58:36 +0530 | [diff] [blame] | 45 | struct spi_slave *spi_setup_slave(uint bus, uint cs, uint max_hz, uint mode) |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 46 | { |
| 47 | struct spi_slave *slave; |
| 48 | |
| 49 | if (!spi_cs_is_valid(bus, cs)) |
| 50 | return NULL; |
| 51 | |
Simon Glass | d034a95 | 2013-03-18 19:23:40 +0000 | [diff] [blame] | 52 | slave = spi_alloc_slave_base(bus, cs); |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 53 | if (!slave) |
| 54 | return NULL; |
| 55 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 56 | /* |
| 57 | * TODO: Some of the code in spi_init() should probably move |
| 58 | * here, or into spi_claim_bus() below. |
| 59 | */ |
| 60 | |
| 61 | return slave; |
| 62 | } |
| 63 | |
| 64 | void spi_free_slave(struct spi_slave *slave) |
| 65 | { |
| 66 | free(slave); |
| 67 | } |
| 68 | |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 69 | void spi_init(void) |
| 70 | { |
Mario Six | dee9949 | 2019-04-29 01:58:42 +0530 | [diff] [blame] | 71 | spi8xxx_t *spi = &((immap_t *)(CONFIG_SYS_IMMR))->spi; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 72 | |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 73 | /* |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 74 | * SPI pins on the MPC83xx are not muxed, so all we do is initialize |
| 75 | * some registers |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 76 | */ |
Mario Six | dee9949 | 2019-04-29 01:58:42 +0530 | [diff] [blame] | 77 | out_be32(&spi->mode, SPI_MODE_REV | SPI_MODE_MS | SPI_MODE_EN); |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 78 | /* Use SYSCLK / 8 (16.67MHz typ.) */ |
Mario Six | eb2533a | 2019-04-29 01:58:47 +0530 | [diff] [blame^] | 79 | clrsetbits_be32(&spi->mode, SPI_MODE_PM_MASK, to_prescale_mod(1)); |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 80 | /* Clear all SPI events */ |
Mario Six | dee9949 | 2019-04-29 01:58:42 +0530 | [diff] [blame] | 81 | setbits_be32(&spi->event, 0xffffffff); |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 82 | /* Mask all SPI interrupts */ |
Mario Six | dee9949 | 2019-04-29 01:58:42 +0530 | [diff] [blame] | 83 | clrbits_be32(&spi->mask, 0xffffffff); |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 84 | /* LST bit doesn't do anything, so disregard */ |
Mario Six | dee9949 | 2019-04-29 01:58:42 +0530 | [diff] [blame] | 85 | out_be32(&spi->com, 0); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 86 | } |
| 87 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 88 | int spi_claim_bus(struct spi_slave *slave) |
| 89 | { |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | void spi_release_bus(struct spi_slave *slave) |
| 94 | { |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Mario Six | 4f4936e | 2019-04-29 01:58:36 +0530 | [diff] [blame] | 97 | int spi_xfer(struct spi_slave *slave, uint bitlen, const void *dout, void *din, |
| 98 | ulong flags) |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 99 | { |
Mario Six | dee9949 | 2019-04-29 01:58:42 +0530 | [diff] [blame] | 100 | spi8xxx_t *spi = &((immap_t *)(CONFIG_SYS_IMMR))->spi; |
Mario Six | 8d684ec | 2019-04-29 01:58:46 +0530 | [diff] [blame] | 101 | u32 tmpdin; |
Mario Six | 56edb8b | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 102 | int num_blks = DIV_ROUND_UP(bitlen, 32); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 103 | |
Mario Six | f9d5ca2 | 2019-04-29 01:58:40 +0530 | [diff] [blame] | 104 | debug("%s: slave %u:%u dout %08X din %08X bitlen %u\n", __func__, |
Mario Six | 6a644f0 | 2019-04-29 01:58:39 +0530 | [diff] [blame] | 105 | slave->bus, slave->cs, *(uint *)dout, *(uint *)din, bitlen); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 106 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 107 | if (flags & SPI_XFER_BEGIN) |
| 108 | spi_cs_activate(slave); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 109 | |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 110 | /* Clear all SPI events */ |
Mario Six | dee9949 | 2019-04-29 01:58:42 +0530 | [diff] [blame] | 111 | setbits_be32(&spi->event, 0xffffffff); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 112 | |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 113 | /* Handle data in 32-bit chunks */ |
Mario Six | 56edb8b | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 114 | while (num_blks--) { |
Mario Six | 8d684ec | 2019-04-29 01:58:46 +0530 | [diff] [blame] | 115 | int tm; |
| 116 | u32 tmpdout = 0; |
| 117 | uchar char_size = (bitlen >= 32 ? 32 : bitlen); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 118 | |
| 119 | /* Shift data so it's msb-justified */ |
Mario Six | 6a644f0 | 2019-04-29 01:58:39 +0530 | [diff] [blame] | 120 | tmpdout = *(u32 *)dout >> (32 - char_size); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 121 | |
| 122 | /* The LEN field of the SPMODE register is set as follows: |
| 123 | * |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 124 | * Bit length setting |
| 125 | * len <= 4 3 |
| 126 | * 4 < len <= 16 len - 1 |
| 127 | * len > 16 0 |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 128 | */ |
| 129 | |
Mario Six | dee9949 | 2019-04-29 01:58:42 +0530 | [diff] [blame] | 130 | clrbits_be32(&spi->mode, SPI_MODE_EN); |
Ira W. Snyder | 5261b00 | 2012-09-12 14:17:31 -0700 | [diff] [blame] | 131 | |
Mario Six | eb2533a | 2019-04-29 01:58:47 +0530 | [diff] [blame^] | 132 | if (bitlen <= 4) |
| 133 | set_char_len(spi, 3); |
| 134 | else if (bitlen <= 16) |
| 135 | set_char_len(spi, bitlen - 1); |
| 136 | else |
| 137 | set_char_len(spi, 0); |
| 138 | |
| 139 | if (bitlen > 16) { |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 140 | /* Set up the next iteration if sending > 32 bits */ |
| 141 | bitlen -= 32; |
| 142 | dout += 4; |
| 143 | } |
| 144 | |
Mario Six | dee9949 | 2019-04-29 01:58:42 +0530 | [diff] [blame] | 145 | setbits_be32(&spi->mode, SPI_MODE_EN); |
Ira W. Snyder | 5261b00 | 2012-09-12 14:17:31 -0700 | [diff] [blame] | 146 | |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 147 | /* Write the data out */ |
Mario Six | dee9949 | 2019-04-29 01:58:42 +0530 | [diff] [blame] | 148 | out_be32(&spi->tx, tmpdout); |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 149 | |
Mario Six | f9d5ca2 | 2019-04-29 01:58:40 +0530 | [diff] [blame] | 150 | debug("*** %s: ... %08x written\n", __func__, tmpdout); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 151 | |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 152 | /* |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 153 | * Wait for SPI transmit to get out |
| 154 | * or time out (1 second = 1000 ms) |
| 155 | * The NE event must be read and cleared first |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 156 | */ |
Mario Six | 4b671e1 | 2019-04-29 01:58:44 +0530 | [diff] [blame] | 157 | for (tm = 0; tm < SPI_TIMEOUT; ++tm) { |
Mario Six | 8d684ec | 2019-04-29 01:58:46 +0530 | [diff] [blame] | 158 | u32 event = in_be32(&spi->event); |
Mario Six | 4b671e1 | 2019-04-29 01:58:44 +0530 | [diff] [blame] | 159 | bool have_ne = event & SPI_EV_NE; |
| 160 | bool have_nf = event & SPI_EV_NF; |
| 161 | |
Mario Six | 2afedfe | 2019-04-29 01:58:45 +0530 | [diff] [blame] | 162 | if (!have_ne) |
| 163 | continue; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 164 | |
Mario Six | 2afedfe | 2019-04-29 01:58:45 +0530 | [diff] [blame] | 165 | tmpdin = in_be32(&spi->rx); |
| 166 | setbits_be32(&spi->event, SPI_EV_NE); |
| 167 | |
| 168 | *(u32 *)din = (tmpdin << (32 - char_size)); |
| 169 | if (char_size == 32) { |
| 170 | /* Advance output buffer by 32 bits */ |
| 171 | din += 4; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 172 | } |
Mario Six | 2afedfe | 2019-04-29 01:58:45 +0530 | [diff] [blame] | 173 | |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 174 | /* |
| 175 | * Only bail when we've had both NE and NF events. |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 176 | * This will cause timeouts on RO devices, so maybe |
| 177 | * in the future put an arbitrary delay after writing |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 178 | * the device. Arbitrary delays suck, though... |
| 179 | */ |
Mario Six | 2afedfe | 2019-04-29 01:58:45 +0530 | [diff] [blame] | 180 | if (have_nf) |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 181 | break; |
| 182 | } |
Mario Six | 2afedfe | 2019-04-29 01:58:45 +0530 | [diff] [blame] | 183 | |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 184 | if (tm >= SPI_TIMEOUT) |
Mario Six | f9d5ca2 | 2019-04-29 01:58:40 +0530 | [diff] [blame] | 185 | debug("*** %s: Time out during SPI transfer\n", |
| 186 | __func__); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 187 | |
Mario Six | f9d5ca2 | 2019-04-29 01:58:40 +0530 | [diff] [blame] | 188 | debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 191 | if (flags & SPI_XFER_END) |
| 192 | spi_cs_deactivate(slave); |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 193 | |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 194 | return 0; |
| 195 | } |