blob: ed6278b86fdef4eac9851a7e1dc3802658c27fc6 [file] [log] [blame]
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +02001/*
2 * Copyright (C) 2007 Atmel Corporation
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +02005 */
6#include <common.h>
7#include <spi.h>
8#include <malloc.h>
9
10#include <asm/io.h>
11
12#include <asm/arch/clk.h>
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010013#include <asm/arch/hardware.h>
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +020014
15#include "atmel_spi.h"
16
Bo Shen6fa8b782013-04-16 23:42:42 +000017static int spi_has_wdrbt(struct atmel_spi_slave *slave)
18{
19 unsigned int ver;
20
21 ver = spi_readl(slave, VERSION);
22
23 return (ATMEL_SPI_VERSION_REV(ver) >= 0x210);
24}
25
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +020026void spi_init()
27{
28
29}
30
31struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
32 unsigned int max_hz, unsigned int mode)
33{
34 struct atmel_spi_slave *as;
35 unsigned int scbr;
36 u32 csrx;
37 void *regs;
38
Reinhard Meyer1e6ef502010-08-09 13:37:59 +020039 if (!spi_cs_is_valid(bus, cs))
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +020040 return NULL;
41
42 switch (bus) {
43 case 0:
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010044 regs = (void *)ATMEL_BASE_SPI0;
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +020045 break;
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010046#ifdef ATMEL_BASE_SPI1
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +020047 case 1:
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010048 regs = (void *)ATMEL_BASE_SPI1;
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +020049 break;
50#endif
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010051#ifdef ATMEL_BASE_SPI2
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +020052 case 2:
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010053 regs = (void *)ATMEL_BASE_SPI2;
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +020054 break;
55#endif
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010056#ifdef ATMEL_BASE_SPI3
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +020057 case 3:
Reinhard Meyer7f619cb2010-11-03 16:32:56 +010058 regs = (void *)ATMEL_BASE_SPI3;
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +020059 break;
60#endif
61 default:
62 return NULL;
63 }
64
65
66 scbr = (get_spi_clk_rate(bus) + max_hz - 1) / max_hz;
67 if (scbr > ATMEL_SPI_CSRx_SCBR_MAX)
68 /* Too low max SCK rate */
69 return NULL;
70 if (scbr < 1)
71 scbr = 1;
72
73 csrx = ATMEL_SPI_CSRx_SCBR(scbr);
74 csrx |= ATMEL_SPI_CSRx_BITS(ATMEL_SPI_BITS_8);
75 if (!(mode & SPI_CPHA))
76 csrx |= ATMEL_SPI_CSRx_NCPHA;
77 if (mode & SPI_CPOL)
78 csrx |= ATMEL_SPI_CSRx_CPOL;
79
Simon Glassd034a952013-03-18 19:23:40 +000080 as = spi_alloc_slave(struct atmel_spi_slave, bus, cs);
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +020081 if (!as)
82 return NULL;
83
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +020084 as->regs = regs;
85 as->mr = ATMEL_SPI_MR_MSTR | ATMEL_SPI_MR_MODFDIS
86 | ATMEL_SPI_MR_PCS(~(1 << cs) & 0xf);
Bo Shen6fa8b782013-04-16 23:42:42 +000087 if (spi_has_wdrbt(as))
88 as->mr |= ATMEL_SPI_MR_WDRBT;
89
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +020090 spi_writel(as, CSR(cs), csrx);
91
92 return &as->slave;
93}
94
95void spi_free_slave(struct spi_slave *slave)
96{
97 struct atmel_spi_slave *as = to_atmel_spi(slave);
98
99 free(as);
100}
101
102int spi_claim_bus(struct spi_slave *slave)
103{
104 struct atmel_spi_slave *as = to_atmel_spi(slave);
105
106 /* Enable the SPI hardware */
107 spi_writel(as, CR, ATMEL_SPI_CR_SPIEN);
108
109 /*
110 * Select the slave. This should set SCK to the correct
111 * initial state, etc.
112 */
113 spi_writel(as, MR, as->mr);
114
115 return 0;
116}
117
118void spi_release_bus(struct spi_slave *slave)
119{
120 struct atmel_spi_slave *as = to_atmel_spi(slave);
121
122 /* Disable the SPI hardware */
123 spi_writel(as, CR, ATMEL_SPI_CR_SPIDIS);
124}
125
126int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
127 const void *dout, void *din, unsigned long flags)
128{
129 struct atmel_spi_slave *as = to_atmel_spi(slave);
130 unsigned int len_tx;
131 unsigned int len_rx;
132 unsigned int len;
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +0200133 u32 status;
134 const u8 *txp = dout;
135 u8 *rxp = din;
136 u8 value;
137
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +0200138 if (bitlen == 0)
139 /* Finish any previously submitted transfers */
140 goto out;
141
142 /*
143 * TODO: The controller can do non-multiple-of-8 bit
144 * transfers, but this driver currently doesn't support it.
145 *
146 * It's also not clear how such transfers are supposed to be
147 * represented as a stream of bytes...this is a limitation of
148 * the current SPI interface.
149 */
150 if (bitlen % 8) {
151 /* Errors always terminate an ongoing transfer */
152 flags |= SPI_XFER_END;
153 goto out;
154 }
155
156 len = bitlen / 8;
157
158 /*
159 * The controller can do automatic CS control, but it is
160 * somewhat quirky, and it doesn't really buy us much anyway
161 * in the context of U-Boot.
162 */
Reinhard Meyer1e6ef502010-08-09 13:37:59 +0200163 if (flags & SPI_XFER_BEGIN) {
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +0200164 spi_cs_activate(slave);
Reinhard Meyer1e6ef502010-08-09 13:37:59 +0200165 /*
166 * sometimes the RDR is not empty when we get here,
167 * in theory that should not happen, but it DOES happen.
168 * Read it here to be on the safe side.
169 * That also clears the OVRES flag. Required if the
170 * following loop exits due to OVRES!
171 */
172 spi_readl(as, RDR);
173 }
Hans-Christian Egtvedt9b4381b2008-05-16 11:10:32 +0200174
175 for (len_tx = 0, len_rx = 0; len_rx < len; ) {
176 status = spi_readl(as, SR);
177
178 if (status & ATMEL_SPI_SR_OVRES)
179 return -1;
180
181 if (len_tx < len && (status & ATMEL_SPI_SR_TDRE)) {
182 if (txp)
183 value = *txp++;
184 else
185 value = 0;
186 spi_writel(as, TDR, value);
187 len_tx++;
188 }
189 if (status & ATMEL_SPI_SR_RDRF) {
190 value = spi_readl(as, RDR);
191 if (rxp)
192 *rxp++ = value;
193 len_rx++;
194 }
195 }
196
197out:
198 if (flags & SPI_XFER_END) {
199 /*
200 * Wait until the transfer is completely done before
201 * we deactivate CS.
202 */
203 do {
204 status = spi_readl(as, SR);
205 } while (!(status & ATMEL_SPI_SR_TXEMPTY));
206
207 spi_cs_deactivate(slave);
208 }
209
210 return 0;
211}