1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/*
* Copyright (C) 2008 Sekhar Nori, Texas Instruments, Inc <www.ti.com>
*
* Driver for SPI controller on DaVinci. Based on atmel_spi.c
* by Atmel Corporation
*
* Copyright (C) 2007 Atmel Corporation
*
* See file CREDITS for list of people who contributed to this
* project.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
#include <common.h>
#include <spi.h>
#include <malloc.h>
#include <asm/io.h>
#include <asm/arch/hardware.h>
#include "davinci_spi.h"
static unsigned int data1_reg_val;
void spi_init()
{
/* do nothing */
}
struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
unsigned int max_hz, unsigned int mode)
{
struct davinci_spi_slave *ds;
void *regs;
unsigned int fmt0;
ds = malloc(sizeof(struct davinci_spi_slave));
if (!ds)
return NULL;
ds->slave.bus = bus;
ds->slave.cs = cs;
ds->regs = CFG_SPI_BASE;
ds->freq = max_hz;
return &ds->slave;
}
void spi_free_slave(struct spi_slave *slave)
{
struct davinci_spi_slave *ds = to_davinci_spi(slave);
free(ds);
}
int spi_claim_bus(struct spi_slave *slave)
{
struct davinci_spi_slave *ds = to_davinci_spi(slave);
unsigned int scalar;
/* Enable the SPI hardware */
spi_writel(ds, GCR0, 0);
udelay(1000);
spi_writel(ds, GCR0, 1);
/* Set master mode, powered up and not activated */
spi_writel(ds, GCR1, 0x3);
/* CS, CLK, SIMO and SOMI are functional pins */
spi_writel(ds, PC0, (1 << 0) | (1 << 9) | (1 << 10) | (1 << 11));
/* setup format */
scalar = ((CFG_SPI_CLK / ds->freq) - 1 ) & 0xFF;
spi_writel(ds, FMT0, 8 | /* character length */
(scalar << 8) |
(1 << 16) | /* clock signal delayed by half clk cycle */
(0 << 17) | /* clock low in idle state - Mode 0 */
(0 << 20)); /* MSB shifted out first */
/* hold cs active at end of transfer until explicitly de-asserted */
data1_reg_val = (1 << 28) | (slave->cs << 16);
spi_writel(ds, DAT1, data1_reg_val);
/* including a minor delay. No science here. Should be good even with
* no delay
*/
spi_writel(ds, DELAY, (50 << 24) | (50 << 16));
/* default chip select register */
spi_writel(ds, DEF, 1);
/* no interrupts */
spi_writel(ds, INT0, 0);
spi_writel(ds, LVL, 0);
/* enable SPI */
spi_writel(ds, GCR1, spi_readl(ds, GCR1) | (1 << 24));
return 0;
}
void spi_release_bus(struct spi_slave *slave)
{
struct davinci_spi_slave *ds = to_davinci_spi(slave);
/* Disable the SPI hardware */
spi_writel(ds, GCR0, 0);
}
int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
const void *dout, void *din, unsigned long flags)
{
struct davinci_spi_slave *ds = to_davinci_spi(slave);
unsigned int len_tx;
unsigned int len_rx;
unsigned int len;
int ret, i;
u32 status;
const u8 *txp = dout;
u8 *rxp = din;
u8 value, dummy = 0;
ret = 0;
if (bitlen == 0)
/* Finish any previously submitted transfers */
goto out;
/*
* It's not clear how non-8-bit-aligned transfers are supposed to be
* represented as a stream of bytes...this is a limitation of
* the current SPI interface - here we terminate on receiving such a
* transfer request.
*/
if (bitlen % 8) {
/* Errors always terminate an ongoing transfer */
flags |= SPI_XFER_END;
goto out;
}
len = bitlen / 8;
/* do an empty read to clear the current contents */
spi_readl(ds, BUF);
/* keep writing and reading 1 byte until done */
for (i = 0; i < len; i++) {
/* wait till TXFULL is asserted */
while(spi_readl(ds, BUF) & (1 << 29));
/* write the data */
data1_reg_val &= ~0xFFFF;
if(txp) {
data1_reg_val |= *txp & 0xFF;
txp++;
}
/* write to DAT1 is required to keep the serial transfer going */
/* we just terminate when we reach the end */
if((i == (len -1)) && (flags & SPI_XFER_END)) {
spi_writel(ds, DAT1, data1_reg_val & ~(1 << 28)); /* clear CS hold */
} else {
spi_writel(ds, DAT1, data1_reg_val);
}
/* read the data - wait for data availability */
while(spi_readl(ds, BUF) & (1 << 31));
if(rxp) {
*rxp = spi_readl(ds, BUF) & 0xFF;
rxp++;
} else {
spi_readl(ds, BUF); /* simply drop the read character */
}
}
return 0;
out:
if (flags & SPI_XFER_END) {
spi_writel(ds, DAT1, data1_reg_val & ~(1 << 28));
}
return 0;
}
#ifdef CONFIG_CMD_EEPROM
/* ------------------------------------------------------------------------ *
* SPI ROM Definitions *
* ------------------------------------------------------------------------ */
#define SPIROM_SIZE 0x00008000
#define SPIROM_BASE 0x00000000
#define SPIROM_PAGESIZE 32
#define SPIROM_PAGEMASK 0xffffffc0
/* ------------------------------------------------------------------------ *
* SPI ROM Commands *
* ------------------------------------------------------------------------ */
#define SPIROM_CMD_WRSR 0x01
#define SPIROM_CMD_WRITE 0x02
#define SPIROM_CMD_READ 0x03
#define SPIROM_CMD_WRDI 0x04
#define SPIROM_CMD_RDSR 0x05
#define SPIROM_CMD_WREN 0x06
static struct spi_slave *slave;
void spi_init_f(void)
{
slave = spi_setup_slave(0, 0, 1*1024*1024, 0);
spi_claim_bus(slave);
}
static char spirombuf[3];
/* ------------------------------------------------------------------------ *
* spirom_status( ) *
* ------------------------------------------------------------------------ */
static unsigned char spi_get_status( )
{
/* Issue read status command */
spirombuf[0] = SPIROM_CMD_RDSR;
spirombuf[1] = 0;
spi_xfer(slave, (2)*8, spirombuf, spirombuf, SPI_XFER_BEGIN | SPI_XFER_END);
return spirombuf[1];
}
ssize_t spi_write(uchar *addr, int alen, uchar *buffer, int len)
{
spirombuf[0] = SPIROM_CMD_WREN;
spi_xfer(slave, 1*8, spirombuf, NULL, SPI_XFER_BEGIN | SPI_XFER_END);
/* Create command block for program operation */
spirombuf[0] = SPIROM_CMD_WRITE;
spirombuf[1] = addr[0];
spirombuf[2] = addr[1];
spi_xfer(slave, 3 * 8, spirombuf, NULL, SPI_XFER_BEGIN);
spi_xfer(slave, len * 8, buffer, NULL, SPI_XFER_END);
/* Wait while busy */
while( (spi_get_status( ) & 0x01 ) );
return len;
}
ssize_t spi_read(uchar *addr, int alen, uchar *buffer, int len)
{
spirombuf[0] = 0x3;
spirombuf[1] = addr[0];
spirombuf[2] = addr[1];
spi_xfer(slave, 3*8, spirombuf, NULL, SPI_XFER_BEGIN);
spi_xfer(slave, len*8, NULL, buffer, SPI_XFER_END);
return len;
}
#endif
|