]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/spi/soft_spi.c
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,
32 /*-----------------------------------------------------------------------
37 #define PRINTD(fmt,args...) printf (fmt ,##args)
39 #define PRINTD(fmt,args...)
42 struct soft_spi_slave
{
43 struct spi_slave slave
;
47 static inline struct soft_spi_slave
*to_soft_spi(struct spi_slave
*slave
)
49 return container_of(slave
, struct soft_spi_slave
, slave
);
52 /*=====================================================================*/
53 /* Public Functions */
54 /*=====================================================================*/
56 /*-----------------------------------------------------------------------
62 volatile immap_t
*immr
= (immap_t
*)CONFIG_SYS_IMMR
;
68 struct spi_slave
*spi_setup_slave(unsigned int bus
, unsigned int cs
,
69 unsigned int max_hz
, unsigned int mode
)
71 struct soft_spi_slave
*ss
;
73 if (!spi_cs_is_valid(bus
, cs
))
76 ss
= malloc(sizeof(struct soft_spi_slave
));
84 /* TODO: Use max_hz to limit the SCK rate */
89 void spi_free_slave(struct spi_slave
*slave
)
91 struct soft_spi_slave
*ss
= to_soft_spi(slave
);
96 int spi_claim_bus(struct spi_slave
*slave
)
98 #ifdef CONFIG_SYS_IMMR
99 volatile immap_t
*immr
= (immap_t
*)CONFIG_SYS_IMMR
;
101 struct soft_spi_slave
*ss
= to_soft_spi(slave
);
104 * Make sure the SPI clock is in idle state as defined for
107 if (ss
->mode
& SPI_CPOL
)
115 void spi_release_bus(struct spi_slave
*slave
)
120 /*-----------------------------------------------------------------------
123 * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
124 * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
126 * The source of the outgoing bits is the "dout" parameter and the
127 * destination of the input bits is the "din" parameter. Note that "dout"
128 * and "din" can point to the same memory location, in which case the
129 * input data overwrites the output data (since both are buffered by
130 * temporary variables, this is OK).
132 int spi_xfer(struct spi_slave
*slave
, unsigned int bitlen
,
133 const void *dout
, void *din
, unsigned long flags
)
135 #ifdef CONFIG_SYS_IMMR
136 volatile immap_t
*immr
= (immap_t
*)CONFIG_SYS_IMMR
;
138 struct soft_spi_slave
*ss
= to_soft_spi(slave
);
141 const u8
*txd
= dout
;
143 int cpol
= ss
->mode
& SPI_CPOL
;
144 int cpha
= ss
->mode
& SPI_CPHA
;
147 PRINTD("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
148 slave
->bus
, slave
->cs
, *(uint
*)txd
, *(uint
*)rxd
, bitlen
);
150 if (flags
& SPI_XFER_BEGIN
)
151 spi_cs_activate(slave
);
153 for(j
= 0; j
< bitlen
; j
++) {
155 * Check if it is time to work on a new byte.
167 SPI_SDA(tmpdout
& 0x80);
181 * If the number of bits isn't a multiple of 8, shift the last
182 * bits over to left-justify them. Then store the last byte
185 if((bitlen
% 8) != 0)
186 tmpdin
<<= 8 - (bitlen
% 8);
189 if (flags
& SPI_XFER_END
)
190 spi_cs_deactivate(slave
);