wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com. |
| 4 | * |
| 5 | * Influenced by code from: |
| 6 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 7 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <spi.h> |
| 13 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 14 | #include <malloc.h> |
| 15 | |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 16 | /*----------------------------------------------------------------------- |
| 17 | * Definitions |
| 18 | */ |
| 19 | |
| 20 | #ifdef DEBUG_SPI |
| 21 | #define PRINTD(fmt,args...) printf (fmt ,##args) |
| 22 | #else |
| 23 | #define PRINTD(fmt,args...) |
| 24 | #endif |
| 25 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 26 | struct soft_spi_slave { |
| 27 | struct spi_slave slave; |
| 28 | unsigned int mode; |
| 29 | }; |
| 30 | |
| 31 | static inline struct soft_spi_slave *to_soft_spi(struct spi_slave *slave) |
| 32 | { |
| 33 | return container_of(slave, struct soft_spi_slave, slave); |
| 34 | } |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 35 | |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 36 | /*=====================================================================*/ |
| 37 | /* Public Functions */ |
| 38 | /*=====================================================================*/ |
| 39 | |
| 40 | /*----------------------------------------------------------------------- |
| 41 | * Initialization |
| 42 | */ |
| 43 | void spi_init (void) |
| 44 | { |
| 45 | #ifdef SPI_INIT |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 46 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 47 | |
| 48 | SPI_INIT; |
| 49 | #endif |
| 50 | } |
| 51 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 52 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 53 | unsigned int max_hz, unsigned int mode) |
| 54 | { |
| 55 | struct soft_spi_slave *ss; |
| 56 | |
| 57 | if (!spi_cs_is_valid(bus, cs)) |
| 58 | return NULL; |
| 59 | |
Simon Glass | d034a95 | 2013-03-18 19:23:40 +0000 | [diff] [blame] | 60 | ss = spi_alloc_slave(struct soft_spi_slave, bus, cs); |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 61 | if (!ss) |
| 62 | return NULL; |
| 63 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 64 | ss->mode = mode; |
| 65 | |
| 66 | /* TODO: Use max_hz to limit the SCK rate */ |
| 67 | |
| 68 | return &ss->slave; |
| 69 | } |
| 70 | |
| 71 | void spi_free_slave(struct spi_slave *slave) |
| 72 | { |
| 73 | struct soft_spi_slave *ss = to_soft_spi(slave); |
| 74 | |
| 75 | free(ss); |
| 76 | } |
| 77 | |
| 78 | int spi_claim_bus(struct spi_slave *slave) |
| 79 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 80 | #ifdef CONFIG_SYS_IMMR |
| 81 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 82 | #endif |
| 83 | struct soft_spi_slave *ss = to_soft_spi(slave); |
| 84 | |
| 85 | /* |
| 86 | * Make sure the SPI clock is in idle state as defined for |
| 87 | * this slave. |
| 88 | */ |
| 89 | if (ss->mode & SPI_CPOL) |
| 90 | SPI_SCL(1); |
| 91 | else |
| 92 | SPI_SCL(0); |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | void spi_release_bus(struct spi_slave *slave) |
| 98 | { |
| 99 | /* Nothing to do */ |
| 100 | } |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 101 | |
| 102 | /*----------------------------------------------------------------------- |
| 103 | * SPI transfer |
| 104 | * |
| 105 | * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks |
| 106 | * "bitlen" bits in the SPI MISO port. That's just the way SPI works. |
| 107 | * |
| 108 | * The source of the outgoing bits is the "dout" parameter and the |
| 109 | * destination of the input bits is the "din" parameter. Note that "dout" |
| 110 | * and "din" can point to the same memory location, in which case the |
| 111 | * input data overwrites the output data (since both are buffered by |
| 112 | * temporary variables, this is OK). |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 113 | */ |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 114 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, |
| 115 | const void *dout, void *din, unsigned long flags) |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 116 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 117 | #ifdef CONFIG_SYS_IMMR |
| 118 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
John Otken | 9aa3677 | 2007-07-26 17:49:11 +0200 | [diff] [blame] | 119 | #endif |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 120 | struct soft_spi_slave *ss = to_soft_spi(slave); |
| 121 | uchar tmpdin = 0; |
| 122 | uchar tmpdout = 0; |
| 123 | const u8 *txd = dout; |
| 124 | u8 *rxd = din; |
| 125 | int cpol = ss->mode & SPI_CPOL; |
| 126 | int cpha = ss->mode & SPI_CPHA; |
| 127 | unsigned int j; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 128 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 129 | PRINTD("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n", |
| 130 | slave->bus, slave->cs, *(uint *)txd, *(uint *)rxd, bitlen); |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 131 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 132 | if (flags & SPI_XFER_BEGIN) |
| 133 | spi_cs_activate(slave); |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 134 | |
| 135 | for(j = 0; j < bitlen; j++) { |
| 136 | /* |
| 137 | * Check if it is time to work on a new byte. |
| 138 | */ |
Andrew Ruder | 6b622d1 | 2014-04-24 13:39:32 -0500 | [diff] [blame] | 139 | if ((j % 8) == 0) { |
| 140 | if (txd) |
| 141 | tmpdout = *txd++; |
| 142 | else |
| 143 | tmpdout = 0; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 144 | if(j != 0) { |
Andrew Ruder | 6b622d1 | 2014-04-24 13:39:32 -0500 | [diff] [blame] | 145 | if (rxd) |
| 146 | *rxd++ = tmpdin; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 147 | } |
| 148 | tmpdin = 0; |
| 149 | } |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 150 | |
| 151 | if (!cpha) |
| 152 | SPI_SCL(!cpol); |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 153 | SPI_SDA(tmpdout & 0x80); |
| 154 | SPI_DELAY; |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 155 | if (cpha) |
| 156 | SPI_SCL(!cpol); |
| 157 | else |
| 158 | SPI_SCL(cpol); |
| 159 | tmpdin <<= 1; |
| 160 | tmpdin |= SPI_READ; |
| 161 | tmpdout <<= 1; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 162 | SPI_DELAY; |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 163 | if (cpha) |
| 164 | SPI_SCL(cpol); |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 165 | } |
| 166 | /* |
| 167 | * If the number of bits isn't a multiple of 8, shift the last |
| 168 | * bits over to left-justify them. Then store the last byte |
| 169 | * read in. |
| 170 | */ |
Andrew Ruder | 6b622d1 | 2014-04-24 13:39:32 -0500 | [diff] [blame] | 171 | if (rxd) { |
| 172 | if ((bitlen % 8) != 0) |
| 173 | tmpdin <<= 8 - (bitlen % 8); |
| 174 | *rxd++ = tmpdin; |
| 175 | } |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 176 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 177 | if (flags & SPI_XFER_END) |
| 178 | spi_cs_deactivate(slave); |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 179 | |
| 180 | return(0); |
| 181 | } |