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 | |
Kim Phillips | 2956acd | 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 | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 15 | |
Kim Phillips | 2956acd | 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 | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 20 | |
| 21 | #define SPI_TIMEOUT 1000 |
| 22 | |
Mario Six | d896b7b | 2019-04-29 01:58:36 +0530 | [diff] [blame] | 23 | 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] | 24 | { |
| 25 | struct spi_slave *slave; |
| 26 | |
| 27 | if (!spi_cs_is_valid(bus, cs)) |
| 28 | return NULL; |
| 29 | |
Simon Glass | d3504fe | 2013-03-18 19:23:40 +0000 | [diff] [blame] | 30 | slave = spi_alloc_slave_base(bus, cs); |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 31 | if (!slave) |
| 32 | return NULL; |
| 33 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 34 | /* |
| 35 | * TODO: Some of the code in spi_init() should probably move |
| 36 | * here, or into spi_claim_bus() below. |
| 37 | */ |
| 38 | |
| 39 | return slave; |
| 40 | } |
| 41 | |
| 42 | void spi_free_slave(struct spi_slave *slave) |
| 43 | { |
| 44 | free(slave); |
| 45 | } |
| 46 | |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 47 | void spi_init(void) |
| 48 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 49 | volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi; |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 50 | |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 51 | /* |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 52 | * SPI pins on the MPC83xx are not muxed, so all we do is initialize |
| 53 | * some registers |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 54 | */ |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 55 | spi->mode = SPI_MODE_REV | SPI_MODE_MS | SPI_MODE_EN; |
Mario Six | d93fe31 | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 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 */ |
| 63 | spi->com = 0; |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 66 | int spi_claim_bus(struct spi_slave *slave) |
| 67 | { |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | void spi_release_bus(struct spi_slave *slave) |
| 72 | { |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Mario Six | d896b7b | 2019-04-29 01:58:36 +0530 | [diff] [blame] | 75 | int spi_xfer(struct spi_slave *slave, uint bitlen, const void *dout, void *din, |
| 76 | ulong flags) |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 77 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 78 | volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi; |
Mario Six | d896b7b | 2019-04-29 01:58:36 +0530 | [diff] [blame] | 79 | uint tmpdout, tmpdin, event; |
Mario Six | 01ac1e1 | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 80 | int num_blks = DIV_ROUND_UP(bitlen, 32); |
| 81 | int tm, is_read = 0; |
| 82 | uchar char_size = 32; |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 83 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 84 | debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n", |
Mario Six | 6f3ac07 | 2019-04-29 01:58:39 +0530 | [diff] [blame^] | 85 | slave->bus, slave->cs, *(uint *)dout, *(uint *)din, bitlen); |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 86 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 87 | if (flags & SPI_XFER_BEGIN) |
| 88 | spi_cs_activate(slave); |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 89 | |
Mario Six | d93fe31 | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 90 | /* Clear all SPI events */ |
| 91 | spi->event = 0xffffffff; |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 92 | |
Mario Six | d93fe31 | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 93 | /* Handle data in 32-bit chunks */ |
Mario Six | 01ac1e1 | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 94 | while (num_blks--) { |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 95 | tmpdout = 0; |
Mario Six | 01ac1e1 | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 96 | char_size = (bitlen >= 32 ? 32 : bitlen); |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 97 | |
| 98 | /* Shift data so it's msb-justified */ |
Mario Six | 6f3ac07 | 2019-04-29 01:58:39 +0530 | [diff] [blame^] | 99 | tmpdout = *(u32 *)dout >> (32 - char_size); |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 100 | |
| 101 | /* The LEN field of the SPMODE register is set as follows: |
| 102 | * |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 103 | * Bit length setting |
| 104 | * len <= 4 3 |
| 105 | * 4 < len <= 16 len - 1 |
| 106 | * len > 16 0 |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 107 | */ |
| 108 | |
Ira W. Snyder | f138ca1 | 2012-09-12 14:17:31 -0700 | [diff] [blame] | 109 | spi->mode &= ~SPI_MODE_EN; |
| 110 | |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 111 | if (bitlen <= 16) { |
| 112 | if (bitlen <= 4) |
| 113 | spi->mode = (spi->mode & 0xff0fffff) | |
Wolfgang Denk | 93e1459 | 2013-10-04 17:43:24 +0200 | [diff] [blame] | 114 | (3 << 20); |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 115 | else |
| 116 | spi->mode = (spi->mode & 0xff0fffff) | |
Wolfgang Denk | 93e1459 | 2013-10-04 17:43:24 +0200 | [diff] [blame] | 117 | ((bitlen - 1) << 20); |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 118 | } else { |
| 119 | spi->mode = (spi->mode & 0xff0fffff); |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 120 | /* Set up the next iteration if sending > 32 bits */ |
| 121 | bitlen -= 32; |
| 122 | dout += 4; |
| 123 | } |
| 124 | |
Ira W. Snyder | f138ca1 | 2012-09-12 14:17:31 -0700 | [diff] [blame] | 125 | spi->mode |= SPI_MODE_EN; |
| 126 | |
Mario Six | d93fe31 | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 127 | /* Write the data out */ |
| 128 | spi->tx = tmpdout; |
| 129 | |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 130 | debug("*** spi_xfer: ... %08x written\n", tmpdout); |
| 131 | |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 132 | /* |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 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 |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 136 | */ |
Mario Six | 01ac1e1 | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 137 | for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) { |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 138 | event = spi->event; |
| 139 | if (event & SPI_EV_NE) { |
| 140 | tmpdin = spi->rx; |
| 141 | spi->event |= SPI_EV_NE; |
Mario Six | 01ac1e1 | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 142 | is_read = 1; |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 143 | |
Mario Six | 6f3ac07 | 2019-04-29 01:58:39 +0530 | [diff] [blame^] | 144 | *(u32 *)din = (tmpdin << (32 - char_size)); |
Mario Six | 01ac1e1 | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 145 | if (char_size == 32) { |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 146 | /* Advance output buffer by 32 bits */ |
| 147 | din += 4; |
| 148 | } |
| 149 | } |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 150 | /* |
| 151 | * Only bail when we've had both NE and NF events. |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 152 | * This will cause timeouts on RO devices, so maybe |
| 153 | * in the future put an arbitrary delay after writing |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 154 | * the device. Arbitrary delays suck, though... |
| 155 | */ |
Mario Six | 01ac1e1 | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 156 | if (is_read && (event & SPI_EV_NF)) |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | if (tm >= SPI_TIMEOUT) |
| 160 | puts("*** spi_xfer: Time out during SPI transfer"); |
| 161 | |
| 162 | debug("*** spi_xfer: transfer ended. Value=%08x\n", tmpdin); |
| 163 | } |
| 164 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 165 | if (flags & SPI_XFER_END) |
| 166 | spi_cs_deactivate(slave); |
Kim Phillips | 2956acd | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 167 | |
Ben Warren | 04a9e11 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 168 | return 0; |
| 169 | } |