blob: f37146b7939b75093376f8f86755015cd3a53d1a [file] [log] [blame]
wdenkec4c5442004-02-09 23:12:24 +00001/*
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
31#if !defined(CFG_NIOS_SPIBASE)
32#error "*** CFG_NIOS_SPIBASE not defined ***"
33#endif
34
35#if !defined(CFG_NIOS_SPIBITS)
36#error "*** CFG_NIOS_SPIBITS not defined ***"
37#endif
38
39#if (CFG_NIOS_SPIBITS != 8) && (CFG_NIOS_SPIBITS != 16)
40#error "*** CFG_NIOS_SPIBITS should be either 8 or 16 ***"
41#endif
42
43static nios_spi_t *spi = (nios_spi_t *)CFG_NIOS_SPIBASE;
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 * ----------------------------------------------- */
59static const char * const hex_digit = "0123456789ABCDEF";
60
61static char quickhex (int i)
62{
63 return hex_digit[i];
64}
65
66static void memdump (void *pv, int num)
67{
68 int i;
69 unsigned char *pc = (unsigned char *) pv;
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
86/*
87 * SPI transfer:
88 *
89 * See include/spi.h and http://www.altera.com/literature/ds/ds_nios_spi.pdf
90 * for more informations.
91 */
92int spi_xfer(spi_chipsel_type chipsel, int bitlen, uchar *dout, uchar *din)
93{
94 int j;
95
96 DPRINT(("spi_xfer: chipsel %08X dout %08X din %08X bitlen %d\n",
97 (int)chipsel, *(uint *)dout, *(uint *)din, bitlen));
98
99 memdump((void*)dout, (bitlen + 7) / 8);
100
101 if(chipsel != NULL) {
102 chipsel(1); /* select the target chip */
103 }
104
105 if (bitlen > CFG_NIOS_SPIBITS) { /* leave chip select active */
106 spi->control |= NIOS_SPI_SSO;
107 }
108
109 for ( j = 0; /* count each byte in */
110 j < ((bitlen + 7) / 8); /* dout[] and din[] */
111
112#if (CFG_NIOS_SPIBITS == 8)
113 j++) {
114
115 while ((spi->status & NIOS_SPI_TRDY) == 0)
116 ;
117 spi->txdata = (unsigned)(dout[j]);
118
119 while ((spi->status & NIOS_SPI_RRDY) == 0)
120 ;
121 din[j] = (unsigned char)(spi->rxdata & 0xff);
122
123#elif (CFG_NIOS_SPIBITS == 16)
124 j++, j++) {
125
126 while ((spi->status & NIOS_SPI_TRDY) == 0)
127 ;
128 if ((j+1) < ((bitlen + 7) / 8))
129 spi->txdata = (unsigned)((dout[j] << 8) | dout[j+1]);
130 else
131 spi->txdata = (unsigned)(dout[j] << 8);
132
133 while ((spi->status & NIOS_SPI_RRDY) == 0)
134 ;
135 din[j] = (unsigned char)((spi->rxdata >> 8) & 0xff);
136 if ((j+1) < ((bitlen + 7) / 8))
137 din[j+1] = (unsigned char)(spi->rxdata & 0xff);
138
139#else
140#error "*** unsupported value of CFG_NIOS_SPIBITS ***"
141#endif
142
143 }
144
145 if (bitlen > CFG_NIOS_SPIBITS) {
146 spi->control &= ~NIOS_SPI_SSO;
147 }
148
149 if(chipsel != NULL) {
150 chipsel(0); /* deselect the target chip */
151 }
152
153 memdump((void*)din, (bitlen + 7) / 8);
154
155 return 0;
156}
157
158#endif /* CONFIG_NIOS_SPI */