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