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