]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/soft_spi.c
a5b62f28ea35d6341feb7ebd95705b69480eeef5
[karo-tx-uboot.git] / common / soft_spi.c
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
30 #if defined(CONFIG_SOFT_SPI)
31
32 #define DEBUG_SPI
33
34
35 /*-----------------------------------------------------------------------
36  * Definitions
37  */
38
39 #ifdef DEBUG_SPI
40 #define PRINTD(fmt,args...)     printf (fmt ,##args)
41 #else
42 #define PRINTD(fmt,args...)
43 #endif
44
45
46
47 /*=====================================================================*/
48 /*                         Public Functions                            */
49 /*=====================================================================*/
50
51 /*-----------------------------------------------------------------------
52  * Initialization
53  */
54 void spi_init (void)
55 {
56 #ifdef  SPI_INIT
57         volatile immap_t *immr = (immap_t *)CFG_IMMR;
58
59         SPI_INIT;
60 #endif
61 }
62
63
64 /*-----------------------------------------------------------------------
65  * SPI transfer
66  *
67  * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
68  * "bitlen" bits in the SPI MISO port.  That's just the way SPI works.
69  *
70  * The source of the outgoing bits is the "dout" parameter and the
71  * destination of the input bits is the "din" parameter.  Note that "dout"
72  * and "din" can point to the same memory location, in which case the
73  * input data overwrites the output data (since both are buffered by
74  * temporary variables, this is OK).
75  *
76  * If the chipsel() function is not NULL, it is called with a parameter
77  * of '1' (chip select active) at the start of the transfer and again with
78  * a parameter of '0' at the end of the transfer.
79  *
80  * If the chipsel() function _is_ NULL, it the responsibility of the
81  * caller to make the appropriate chip select active before calling
82  * spi_xfer() and making it inactive after spi_xfer() returns.
83  */
84 int  spi_xfer(spi_chipsel_type chipsel, int bitlen, uchar *dout, uchar *din)
85 {
86         volatile immap_t *immr = (immap_t *)CFG_IMMR;
87         uchar tmpdin  = 0;
88         uchar tmpdout = 0;
89         int   j;
90
91         PRINTD("spi_xfer: chipsel %08X dout %08X din %08X bitlen %d\n",
92                 (int)chipsel, *(uint *)dout, *(uint *)din, bitlen);
93
94         if(chipsel != NULL) {
95                 (*chipsel)(1);  /* select the target chip */
96         }
97
98         for(j = 0; j < bitlen; j++) {
99                 /*
100                  * Check if it is time to work on a new byte.
101                  */
102                 if((j % 8) == 0) {
103                         tmpdout = *dout++;
104                         if(j != 0) {
105                                 *din++ = tmpdin;
106                         }
107                         tmpdin  = 0;
108                 }
109                 SPI_SCL(0);
110                 SPI_SDA(tmpdout & 0x80);
111                 SPI_DELAY;
112                 SPI_SCL(1);
113                 SPI_DELAY;
114                 tmpdin  <<= 1;
115                 tmpdin   |= SPI_READ;
116                 tmpdout <<= 1;
117         }
118         /*
119          * If the number of bits isn't a multiple of 8, shift the last
120          * bits over to left-justify them.  Then store the last byte
121          * read in.
122          */
123         if((bitlen % 8) != 0)
124                 tmpdin <<= 8 - (bitlen % 8);
125         *din++ = tmpdin;
126
127         SPI_SCL(0);             /* SPI wants the clock left low for idle */
128
129         if(chipsel != NULL) {
130                 (*chipsel)(0);  /* deselect the target chip */
131
132         }
133
134         return(0);
135 }
136
137 #endif  /* CONFIG_SOFT_SPI */
138