]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/spi/atmel_spi.c
2 * Copyright (C) 2007 Atmel Corporation
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 #include <asm/arch/clk.h>
29 #include <asm/arch/hardware.h>
31 #include "atmel_spi.h"
38 struct spi_slave
*spi_setup_slave(unsigned int bus
, unsigned int cs
,
39 unsigned int max_hz
, unsigned int mode
)
41 struct atmel_spi_slave
*as
;
46 if (!spi_cs_is_valid(bus
, cs
))
51 regs
= (void *)ATMEL_BASE_SPI0
;
53 #ifdef ATMEL_BASE_SPI1
55 regs
= (void *)ATMEL_BASE_SPI1
;
58 #ifdef ATMEL_BASE_SPI2
60 regs
= (void *)ATMEL_BASE_SPI2
;
63 #ifdef ATMEL_BASE_SPI3
65 regs
= (void *)ATMEL_BASE_SPI3
;
73 scbr
= (get_spi_clk_rate(bus
) + max_hz
- 1) / max_hz
;
74 if (scbr
> ATMEL_SPI_CSRx_SCBR_MAX
)
75 /* Too low max SCK rate */
80 csrx
= ATMEL_SPI_CSRx_SCBR(scbr
);
81 csrx
|= ATMEL_SPI_CSRx_BITS(ATMEL_SPI_BITS_8
);
82 if (!(mode
& SPI_CPHA
))
83 csrx
|= ATMEL_SPI_CSRx_NCPHA
;
85 csrx
|= ATMEL_SPI_CSRx_CPOL
;
87 as
= malloc(sizeof(struct atmel_spi_slave
));
94 as
->mr
= ATMEL_SPI_MR_MSTR
| ATMEL_SPI_MR_MODFDIS
95 | ATMEL_SPI_MR_PCS(~(1 << cs
) & 0xf);
96 spi_writel(as
, CSR(cs
), csrx
);
101 void spi_free_slave(struct spi_slave
*slave
)
103 struct atmel_spi_slave
*as
= to_atmel_spi(slave
);
108 int spi_claim_bus(struct spi_slave
*slave
)
110 struct atmel_spi_slave
*as
= to_atmel_spi(slave
);
112 /* Enable the SPI hardware */
113 spi_writel(as
, CR
, ATMEL_SPI_CR_SPIEN
);
116 * Select the slave. This should set SCK to the correct
117 * initial state, etc.
119 spi_writel(as
, MR
, as
->mr
);
124 void spi_release_bus(struct spi_slave
*slave
)
126 struct atmel_spi_slave
*as
= to_atmel_spi(slave
);
128 /* Disable the SPI hardware */
129 spi_writel(as
, CR
, ATMEL_SPI_CR_SPIDIS
);
132 int spi_xfer(struct spi_slave
*slave
, unsigned int bitlen
,
133 const void *dout
, void *din
, unsigned long flags
)
135 struct atmel_spi_slave
*as
= to_atmel_spi(slave
);
140 const u8
*txp
= dout
;
145 /* Finish any previously submitted transfers */
149 * TODO: The controller can do non-multiple-of-8 bit
150 * transfers, but this driver currently doesn't support it.
152 * It's also not clear how such transfers are supposed to be
153 * represented as a stream of bytes...this is a limitation of
154 * the current SPI interface.
157 /* Errors always terminate an ongoing transfer */
158 flags
|= SPI_XFER_END
;
165 * The controller can do automatic CS control, but it is
166 * somewhat quirky, and it doesn't really buy us much anyway
167 * in the context of U-Boot.
169 if (flags
& SPI_XFER_BEGIN
) {
170 spi_cs_activate(slave
);
172 * sometimes the RDR is not empty when we get here,
173 * in theory that should not happen, but it DOES happen.
174 * Read it here to be on the safe side.
175 * That also clears the OVRES flag. Required if the
176 * following loop exits due to OVRES!
181 for (len_tx
= 0, len_rx
= 0; len_rx
< len
; ) {
182 status
= spi_readl(as
, SR
);
184 if (status
& ATMEL_SPI_SR_OVRES
)
187 if (len_tx
< len
&& (status
& ATMEL_SPI_SR_TDRE
)) {
192 spi_writel(as
, TDR
, value
);
195 if (status
& ATMEL_SPI_SR_RDRF
) {
196 value
= spi_readl(as
, RDR
);
204 if (flags
& SPI_XFER_END
) {
206 * Wait until the transfer is completely done before
210 status
= spi_readl(as
, SR
);
211 } while (!(status
& ATMEL_SPI_SR_TXEMPTY
));
213 spi_cs_deactivate(slave
);