wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004, Li-Pro.Net <www.li-pro.net> |
| 3 | * Stephan Linz <linz@li-pro.net> |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <linux/ctype.h> |
| 26 | |
| 27 | #if defined(CONFIG_NIOS_SPI) |
| 28 | #include <nios-io.h> |
| 29 | #include <spi.h> |
| 30 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 31 | #if !defined(CONFIG_SYS_NIOS_SPIBASE) |
| 32 | #error "*** CONFIG_SYS_NIOS_SPIBASE not defined ***" |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 35 | #if !defined(CONFIG_SYS_NIOS_SPIBITS) |
| 36 | #error "*** CONFIG_SYS_NIOS_SPIBITS not defined ***" |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 39 | #if (CONFIG_SYS_NIOS_SPIBITS != 8) && (CONFIG_SYS_NIOS_SPIBITS != 16) |
| 40 | #error "*** CONFIG_SYS_NIOS_SPIBITS should be either 8 or 16 ***" |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 43 | static nios_spi_t *spi = (nios_spi_t *)CONFIG_SYS_NIOS_SPIBASE; |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 44 | |
| 45 | /* Warning: |
| 46 | * You cannot enable DEBUG for early system initalization, i. e. when |
| 47 | * this driver is used to read environment parameters like "baudrate" |
| 48 | * from EEPROM which are used to initialize the serial port which is |
| 49 | * needed to print the debug messages... |
| 50 | */ |
| 51 | #undef DEBUG |
| 52 | |
| 53 | #ifdef DEBUG |
| 54 | |
| 55 | #define DPRINT(a) printf a; |
| 56 | /* ----------------------------------------------- |
| 57 | * Helper functions to peek into tx and rx buffers |
| 58 | * ----------------------------------------------- */ |
| 59 | static const char * const hex_digit = "0123456789ABCDEF"; |
| 60 | |
| 61 | static char quickhex (int i) |
| 62 | { |
| 63 | return hex_digit[i]; |
| 64 | } |
| 65 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 66 | static void memdump (const void *pv, int num) |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 67 | { |
| 68 | int i; |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 69 | const unsigned char *pc = (const unsigned char *) pv; |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 70 | |
| 71 | for (i = 0; i < num; i++) |
| 72 | printf ("%c%c ", quickhex (pc[i] >> 4), quickhex (pc[i] & 0x0f)); |
| 73 | printf ("\t"); |
| 74 | for (i = 0; i < num; i++) |
| 75 | printf ("%c", isprint (pc[i]) ? pc[i] : '.'); |
| 76 | printf ("\n"); |
| 77 | } |
| 78 | #else /* !DEBUG */ |
| 79 | |
| 80 | #define DPRINT(a) |
| 81 | #define memdump(p,n) |
| 82 | |
| 83 | #endif /* DEBUG */ |
| 84 | |
| 85 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 86 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 87 | unsigned int max_hz, unsigned int mode) |
| 88 | { |
| 89 | struct spi_slave *slave; |
| 90 | |
| 91 | if (!spi_cs_is_valid(bus, cs)) |
| 92 | return NULL; |
| 93 | |
| 94 | slave = malloc(sizeof(struct spi_slave)); |
| 95 | if (!slave) |
| 96 | return NULL; |
| 97 | |
| 98 | slave->bus = bus; |
| 99 | slave->cs = cs; |
| 100 | |
| 101 | /* TODO: Add support for different modes and speeds */ |
| 102 | |
| 103 | return slave; |
| 104 | } |
| 105 | |
| 106 | void spi_free_slave(struct spi_slave *slave) |
| 107 | { |
| 108 | free(slave); |
| 109 | } |
| 110 | |
| 111 | int spi_claim_bus(struct spi_slave *slave) |
| 112 | { |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | void spi_release_bus(struct spi_slave *slave) |
| 117 | { |
| 118 | |
| 119 | } |
| 120 | |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 121 | /* |
| 122 | * SPI transfer: |
| 123 | * |
| 124 | * See include/spi.h and http://www.altera.com/literature/ds/ds_nios_spi.pdf |
| 125 | * for more informations. |
| 126 | */ |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 127 | int spi_xfer(struct spi_slave *slave, int bitlen, const void *dout, |
| 128 | void *din, unsigned long flags) |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 129 | { |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 130 | const u8 *txd = dout; |
| 131 | u8 *rxd = din; |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 132 | int j; |
| 133 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 134 | DPRINT(("spi_xfer: slave %u:%u dout %08X din %08X bitlen %d\n", |
| 135 | slave->bus, slave->cs, *(uint *)dout, *(uint *)din, bitlen)); |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 136 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 137 | memdump(dout, (bitlen + 7) / 8); |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 138 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 139 | if (flags & SPI_XFER_BEGIN) |
| 140 | spi_cs_activate(slave); |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 141 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 142 | if (!(flags & SPI_XFER_END) || bitlen > CONFIG_SYS_NIOS_SPIBITS) { |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 143 | /* leave chip select active */ |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 144 | spi->control |= NIOS_SPI_SSO; |
| 145 | } |
| 146 | |
| 147 | for ( j = 0; /* count each byte in */ |
| 148 | j < ((bitlen + 7) / 8); /* dout[] and din[] */ |
| 149 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 150 | #if (CONFIG_SYS_NIOS_SPIBITS == 8) |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 151 | j++) { |
| 152 | |
| 153 | while ((spi->status & NIOS_SPI_TRDY) == 0) |
| 154 | ; |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 155 | spi->txdata = (unsigned)(txd[j]); |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 156 | |
| 157 | while ((spi->status & NIOS_SPI_RRDY) == 0) |
| 158 | ; |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 159 | rxd[j] = (unsigned char)(spi->rxdata & 0xff); |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 160 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 161 | #elif (CONFIG_SYS_NIOS_SPIBITS == 16) |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 162 | j++, j++) { |
| 163 | |
| 164 | while ((spi->status & NIOS_SPI_TRDY) == 0) |
| 165 | ; |
| 166 | if ((j+1) < ((bitlen + 7) / 8)) |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 167 | spi->txdata = (unsigned)((txd[j] << 8) | txd[j+1]); |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 168 | else |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 169 | spi->txdata = (unsigned)(txd[j] << 8); |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 170 | |
| 171 | while ((spi->status & NIOS_SPI_RRDY) == 0) |
| 172 | ; |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 173 | rxd[j] = (unsigned char)((spi->rxdata >> 8) & 0xff); |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 174 | if ((j+1) < ((bitlen + 7) / 8)) |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 175 | rxd[j+1] = (unsigned char)(spi->rxdata & 0xff); |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 176 | |
| 177 | #else |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 178 | #error "*** unsupported value of CONFIG_SYS_NIOS_SPIBITS ***" |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 179 | #endif |
| 180 | |
| 181 | } |
| 182 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 183 | if (bitlen > CONFIG_SYS_NIOS_SPIBITS && (flags & SPI_XFER_END)) { |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 184 | spi->control &= ~NIOS_SPI_SSO; |
| 185 | } |
| 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 | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 189 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 190 | memdump(din, (bitlen + 7) / 8); |
wdenk | ec4c544 | 2004-02-09 23:12:24 +0000 | [diff] [blame] | 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | #endif /* CONFIG_NIOS_SPI */ |