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 | * |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
| 28 | #include <spi.h> |
| 29 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 30 | #include <malloc.h> |
| 31 | |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 32 | /*----------------------------------------------------------------------- |
| 33 | * Definitions |
| 34 | */ |
| 35 | |
| 36 | #ifdef DEBUG_SPI |
| 37 | #define PRINTD(fmt,args...) printf (fmt ,##args) |
| 38 | #else |
| 39 | #define PRINTD(fmt,args...) |
| 40 | #endif |
| 41 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 42 | struct soft_spi_slave { |
| 43 | struct spi_slave slave; |
| 44 | unsigned int mode; |
| 45 | }; |
| 46 | |
| 47 | static inline struct soft_spi_slave *to_soft_spi(struct spi_slave *slave) |
| 48 | { |
| 49 | return container_of(slave, struct soft_spi_slave, slave); |
| 50 | } |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 51 | |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 52 | /*=====================================================================*/ |
| 53 | /* Public Functions */ |
| 54 | /*=====================================================================*/ |
| 55 | |
| 56 | /*----------------------------------------------------------------------- |
| 57 | * Initialization |
| 58 | */ |
| 59 | void spi_init (void) |
| 60 | { |
| 61 | #ifdef SPI_INIT |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 62 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 63 | |
| 64 | SPI_INIT; |
| 65 | #endif |
| 66 | } |
| 67 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 68 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 69 | unsigned int max_hz, unsigned int mode) |
| 70 | { |
| 71 | struct soft_spi_slave *ss; |
| 72 | |
| 73 | if (!spi_cs_is_valid(bus, cs)) |
| 74 | return NULL; |
| 75 | |
Simon Glass | d3504fe | 2013-03-18 19:23:40 +0000 | [diff] [blame] | 76 | ss = spi_alloc_slave(struct soft_spi_slave, bus, cs); |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 77 | if (!ss) |
| 78 | return NULL; |
| 79 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 80 | ss->mode = mode; |
| 81 | |
| 82 | /* TODO: Use max_hz to limit the SCK rate */ |
| 83 | |
| 84 | return &ss->slave; |
| 85 | } |
| 86 | |
| 87 | void spi_free_slave(struct spi_slave *slave) |
| 88 | { |
| 89 | struct soft_spi_slave *ss = to_soft_spi(slave); |
| 90 | |
| 91 | free(ss); |
| 92 | } |
| 93 | |
| 94 | int spi_claim_bus(struct spi_slave *slave) |
| 95 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 96 | #ifdef CONFIG_SYS_IMMR |
| 97 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 98 | #endif |
| 99 | struct soft_spi_slave *ss = to_soft_spi(slave); |
| 100 | |
| 101 | /* |
| 102 | * Make sure the SPI clock is in idle state as defined for |
| 103 | * this slave. |
| 104 | */ |
| 105 | if (ss->mode & SPI_CPOL) |
| 106 | SPI_SCL(1); |
| 107 | else |
| 108 | SPI_SCL(0); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | void spi_release_bus(struct spi_slave *slave) |
| 114 | { |
| 115 | /* Nothing to do */ |
| 116 | } |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 117 | |
| 118 | /*----------------------------------------------------------------------- |
| 119 | * SPI transfer |
| 120 | * |
| 121 | * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks |
| 122 | * "bitlen" bits in the SPI MISO port. That's just the way SPI works. |
| 123 | * |
| 124 | * The source of the outgoing bits is the "dout" parameter and the |
| 125 | * destination of the input bits is the "din" parameter. Note that "dout" |
| 126 | * and "din" can point to the same memory location, in which case the |
| 127 | * input data overwrites the output data (since both are buffered by |
| 128 | * temporary variables, this is OK). |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 129 | */ |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 130 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, |
| 131 | const void *dout, void *din, unsigned long flags) |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 132 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 133 | #ifdef CONFIG_SYS_IMMR |
| 134 | volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR; |
John Otken | d4024bb | 2007-07-26 17:49:11 +0200 | [diff] [blame] | 135 | #endif |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 136 | struct soft_spi_slave *ss = to_soft_spi(slave); |
| 137 | uchar tmpdin = 0; |
| 138 | uchar tmpdout = 0; |
| 139 | const u8 *txd = dout; |
| 140 | u8 *rxd = din; |
| 141 | int cpol = ss->mode & SPI_CPOL; |
| 142 | int cpha = ss->mode & SPI_CPHA; |
| 143 | unsigned int j; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 144 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 145 | PRINTD("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n", |
| 146 | slave->bus, slave->cs, *(uint *)txd, *(uint *)rxd, bitlen); |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 147 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 148 | if (flags & SPI_XFER_BEGIN) |
| 149 | spi_cs_activate(slave); |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 150 | |
| 151 | for(j = 0; j < bitlen; j++) { |
| 152 | /* |
| 153 | * Check if it is time to work on a new byte. |
| 154 | */ |
| 155 | if((j % 8) == 0) { |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 156 | tmpdout = *txd++; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 157 | if(j != 0) { |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 158 | *rxd++ = tmpdin; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 159 | } |
| 160 | tmpdin = 0; |
| 161 | } |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 162 | |
| 163 | if (!cpha) |
| 164 | SPI_SCL(!cpol); |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 165 | SPI_SDA(tmpdout & 0x80); |
| 166 | SPI_DELAY; |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 167 | if (cpha) |
| 168 | SPI_SCL(!cpol); |
| 169 | else |
| 170 | SPI_SCL(cpol); |
| 171 | tmpdin <<= 1; |
| 172 | tmpdin |= SPI_READ; |
| 173 | tmpdout <<= 1; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 174 | SPI_DELAY; |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 175 | if (cpha) |
| 176 | SPI_SCL(cpol); |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 177 | } |
| 178 | /* |
| 179 | * If the number of bits isn't a multiple of 8, shift the last |
| 180 | * bits over to left-justify them. Then store the last byte |
| 181 | * read in. |
| 182 | */ |
| 183 | if((bitlen % 8) != 0) |
| 184 | tmpdin <<= 8 - (bitlen % 8); |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 185 | *rxd++ = tmpdin; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 186 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 187 | if (flags & SPI_XFER_END) |
| 188 | spi_cs_deactivate(slave); |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 189 | |
| 190 | return(0); |
| 191 | } |