3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
5 * Influenced by code from:
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
30 #if defined(CONFIG_SOFT_SPI)
32 /*-----------------------------------------------------------------------
37 #define PRINTD(fmt,args...) printf (fmt ,##args)
39 #define PRINTD(fmt,args...)
44 /*=====================================================================*/
45 /* Public Functions */
46 /*=====================================================================*/
48 /*-----------------------------------------------------------------------
54 volatile immap_t *immr = (immap_t *)CFG_IMMR;
61 /*-----------------------------------------------------------------------
64 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
65 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
67 * The source of the outgoing bits is the "dout" parameter and the
68 * destination of the input bits is the "din" parameter. Note that "dout"
69 * and "din" can point to the same memory location, in which case the
70 * input data overwrites the output data (since both are buffered by
71 * temporary variables, this is OK).
73 * If the chipsel() function is not NULL, it is called with a parameter
74 * of '1' (chip select active) at the start of the transfer and again with
75 * a parameter of '0' at the end of the transfer.
77 * If the chipsel() function _is_ NULL, it the responsibility of the
78 * caller to make the appropriate chip select active before calling
79 * spi_xfer() and making it inactive after spi_xfer() returns.
81 int spi_xfer(spi_chipsel_type chipsel, int bitlen, uchar *dout, uchar *din)
83 volatile immap_t *immr = (immap_t *)CFG_IMMR;
88 PRINTD("spi_xfer: chipsel %08X dout %08X din %08X bitlen %d\n",
89 (int)chipsel, *(uint *)dout, *(uint *)din, bitlen);
92 (*chipsel)(1); /* select the target chip */
95 for(j = 0; j < bitlen; j++) {
97 * Check if it is time to work on a new byte.
107 SPI_SDA(tmpdout & 0x80);
116 * If the number of bits isn't a multiple of 8, shift the last
117 * bits over to left-justify them. Then store the last byte
120 if((bitlen % 8) != 0)
121 tmpdin <<= 8 - (bitlen % 8);
124 SPI_SCL(0); /* SPI wants the clock left low for idle */
126 if(chipsel != NULL) {
127 (*chipsel)(0); /* deselect the target chip */
134 #endif /* CONFIG_SOFT_SPI */