Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2006 Ben Warren, Qstreams Networks Inc. |
Stefan Roese | a47a12b | 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 | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Ben Warren | 8931ab1 | 2008-01-26 23:41:19 -0500 | [diff] [blame] | 8 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 9 | #include <malloc.h> |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 10 | #include <spi.h> |
| 11 | #include <asm/mpc8xxx_spi.h> |
| 12 | |
Mario Six | 6ea9395 | 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 | }; |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 17 | |
Mario Six | 6ea9395 | 2019-04-29 01:58:41 +0530 | [diff] [blame] | 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, |
| 29 | |
| 30 | SPI_COM_LST = BIT(31 - 9), |
| 31 | }; |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 32 | |
| 33 | #define SPI_TIMEOUT 1000 |
| 34 | |
Mario Six | d896b7b | 2019-04-29 01:58:36 +0530 | [diff] [blame] | 35 | struct spi_slave *spi_setup_slave(uint bus, uint cs, uint max_hz, uint mode) |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 36 | { |
| 37 | struct spi_slave *slave; |
| 38 | |
| 39 | if (!spi_cs_is_valid(bus, cs)) |
| 40 | return NULL; |
| 41 | |
Simon Glass | d3504fe | 2013-03-18 19:23:40 +0000 | [diff] [blame] | 42 | slave = spi_alloc_slave_base(bus, cs); |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 43 | if (!slave) |
| 44 | return NULL; |
| 45 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 46 | /* |
| 47 | * TODO: Some of the code in spi_init() should probably move |
| 48 | * here, or into spi_claim_bus() below. |
| 49 | */ |
| 50 | |
| 51 | return slave; |
| 52 | } |
| 53 | |
| 54 | void spi_free_slave(struct spi_slave *slave) |
| 55 | { |
| 56 | free(slave); |
| 57 | } |
| 58 | |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 59 | void spi_init(void) |
| 60 | { |
Mario Six | 1a907e4 | 2019-04-29 01:58:42 +0530 | [diff] [blame^] | 61 | spi8xxx_t *spi = &((immap_t *)(CONFIG_SYS_IMMR))->spi; |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 62 | |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 63 | /* |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 64 | * SPI pins on the MPC83xx are not muxed, so all we do is initialize |
| 65 | * some registers |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 66 | */ |
Mario Six | 1a907e4 | 2019-04-29 01:58:42 +0530 | [diff] [blame^] | 67 | out_be32(&spi->mode, SPI_MODE_REV | SPI_MODE_MS | SPI_MODE_EN); |
Mario Six | d93fe31 | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 68 | /* Use SYSCLK / 8 (16.67MHz typ.) */ |
Mario Six | 1a907e4 | 2019-04-29 01:58:42 +0530 | [diff] [blame^] | 69 | clrsetbits_be32(&spi->mode, 0x000f0000, BIT(16)); |
Mario Six | d93fe31 | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 70 | /* Clear all SPI events */ |
Mario Six | 1a907e4 | 2019-04-29 01:58:42 +0530 | [diff] [blame^] | 71 | setbits_be32(&spi->event, 0xffffffff); |
Mario Six | d93fe31 | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 72 | /* Mask all SPI interrupts */ |
Mario Six | 1a907e4 | 2019-04-29 01:58:42 +0530 | [diff] [blame^] | 73 | clrbits_be32(&spi->mask, 0xffffffff); |
Mario Six | d93fe31 | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 74 | /* LST bit doesn't do anything, so disregard */ |
Mario Six | 1a907e4 | 2019-04-29 01:58:42 +0530 | [diff] [blame^] | 75 | out_be32(&spi->com, 0); |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 76 | } |
| 77 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 78 | int spi_claim_bus(struct spi_slave *slave) |
| 79 | { |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | void spi_release_bus(struct spi_slave *slave) |
| 84 | { |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Mario Six | d896b7b | 2019-04-29 01:58:36 +0530 | [diff] [blame] | 87 | int spi_xfer(struct spi_slave *slave, uint bitlen, const void *dout, void *din, |
| 88 | ulong flags) |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 89 | { |
Mario Six | 1a907e4 | 2019-04-29 01:58:42 +0530 | [diff] [blame^] | 90 | spi8xxx_t *spi = &((immap_t *)(CONFIG_SYS_IMMR))->spi; |
Mario Six | d896b7b | 2019-04-29 01:58:36 +0530 | [diff] [blame] | 91 | uint tmpdout, tmpdin, event; |
Mario Six | 01ac1e1 | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 92 | int num_blks = DIV_ROUND_UP(bitlen, 32); |
| 93 | int tm, is_read = 0; |
| 94 | uchar char_size = 32; |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 95 | |
Mario Six | fabe6c4 | 2019-04-29 01:58:40 +0530 | [diff] [blame] | 96 | debug("%s: slave %u:%u dout %08X din %08X bitlen %u\n", __func__, |
Mario Six | 6f3ac07 | 2019-04-29 01:58:39 +0530 | [diff] [blame] | 97 | slave->bus, slave->cs, *(uint *)dout, *(uint *)din, bitlen); |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 98 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 99 | if (flags & SPI_XFER_BEGIN) |
| 100 | spi_cs_activate(slave); |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 101 | |
Mario Six | d93fe31 | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 102 | /* Clear all SPI events */ |
Mario Six | 1a907e4 | 2019-04-29 01:58:42 +0530 | [diff] [blame^] | 103 | setbits_be32(&spi->event, 0xffffffff); |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 104 | |
Mario Six | d93fe31 | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 105 | /* Handle data in 32-bit chunks */ |
Mario Six | 01ac1e1 | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 106 | while (num_blks--) { |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 107 | tmpdout = 0; |
Mario Six | 01ac1e1 | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 108 | char_size = (bitlen >= 32 ? 32 : bitlen); |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 109 | |
| 110 | /* Shift data so it's msb-justified */ |
Mario Six | 6f3ac07 | 2019-04-29 01:58:39 +0530 | [diff] [blame] | 111 | tmpdout = *(u32 *)dout >> (32 - char_size); |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 112 | |
| 113 | /* The LEN field of the SPMODE register is set as follows: |
| 114 | * |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 115 | * Bit length setting |
| 116 | * len <= 4 3 |
| 117 | * 4 < len <= 16 len - 1 |
| 118 | * len > 16 0 |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 119 | */ |
| 120 | |
Mario Six | 1a907e4 | 2019-04-29 01:58:42 +0530 | [diff] [blame^] | 121 | clrbits_be32(&spi->mode, SPI_MODE_EN); |
Ira W. Snyder | f138ca1 | 2012-09-12 14:17:31 -0700 | [diff] [blame] | 122 | |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 123 | if (bitlen <= 16) { |
| 124 | if (bitlen <= 4) |
Mario Six | 1a907e4 | 2019-04-29 01:58:42 +0530 | [diff] [blame^] | 125 | clrsetbits_be32(&spi->mode, 0x00f00000, |
| 126 | (3 << 20)); |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 127 | else |
Mario Six | 1a907e4 | 2019-04-29 01:58:42 +0530 | [diff] [blame^] | 128 | clrsetbits_be32(&spi->mode, 0x00f00000, |
| 129 | ((bitlen - 1) << 20)); |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 130 | } else { |
Mario Six | 1a907e4 | 2019-04-29 01:58:42 +0530 | [diff] [blame^] | 131 | clrbits_be32(&spi->mode, 0x00f00000); |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 132 | /* Set up the next iteration if sending > 32 bits */ |
| 133 | bitlen -= 32; |
| 134 | dout += 4; |
| 135 | } |
| 136 | |
Mario Six | 1a907e4 | 2019-04-29 01:58:42 +0530 | [diff] [blame^] | 137 | setbits_be32(&spi->mode, SPI_MODE_EN); |
Ira W. Snyder | f138ca1 | 2012-09-12 14:17:31 -0700 | [diff] [blame] | 138 | |
Mario Six | d93fe31 | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 139 | /* Write the data out */ |
Mario Six | 1a907e4 | 2019-04-29 01:58:42 +0530 | [diff] [blame^] | 140 | out_be32(&spi->tx, tmpdout); |
Mario Six | d93fe31 | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 141 | |
Mario Six | fabe6c4 | 2019-04-29 01:58:40 +0530 | [diff] [blame] | 142 | debug("*** %s: ... %08x written\n", __func__, tmpdout); |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 143 | |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 144 | /* |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 145 | * Wait for SPI transmit to get out |
| 146 | * or time out (1 second = 1000 ms) |
| 147 | * The NE event must be read and cleared first |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 148 | */ |
Mario Six | 01ac1e1 | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 149 | for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) { |
Mario Six | 1a907e4 | 2019-04-29 01:58:42 +0530 | [diff] [blame^] | 150 | event = in_be32(&spi->event); |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 151 | if (event & SPI_EV_NE) { |
Mario Six | 1a907e4 | 2019-04-29 01:58:42 +0530 | [diff] [blame^] | 152 | tmpdin = in_be32(&spi->rx); |
| 153 | setbits_be32(&spi->event, SPI_EV_NE); |
Mario Six | 01ac1e1 | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 154 | is_read = 1; |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 155 | |
Mario Six | 6f3ac07 | 2019-04-29 01:58:39 +0530 | [diff] [blame] | 156 | *(u32 *)din = (tmpdin << (32 - char_size)); |
Mario Six | 01ac1e1 | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 157 | if (char_size == 32) { |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 158 | /* Advance output buffer by 32 bits */ |
| 159 | din += 4; |
| 160 | } |
| 161 | } |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 162 | /* |
| 163 | * Only bail when we've had both NE and NF events. |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 164 | * This will cause timeouts on RO devices, so maybe |
| 165 | * in the future put an arbitrary delay after writing |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 166 | * the device. Arbitrary delays suck, though... |
| 167 | */ |
Mario Six | 01ac1e1 | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 168 | if (is_read && (event & SPI_EV_NF)) |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 169 | break; |
| 170 | } |
| 171 | if (tm >= SPI_TIMEOUT) |
Mario Six | fabe6c4 | 2019-04-29 01:58:40 +0530 | [diff] [blame] | 172 | debug("*** %s: Time out during SPI transfer\n", |
| 173 | __func__); |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 174 | |
Mario Six | fabe6c4 | 2019-04-29 01:58:40 +0530 | [diff] [blame] | 175 | debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin); |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 176 | } |
| 177 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 178 | if (flags & SPI_XFER_END) |
| 179 | spi_cs_deactivate(slave); |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 180 | |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 181 | return 0; |
| 182 | } |