1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <common.h>
25#include <spi.h>
26
27#define SPI_EEPROM_WREN 0x06
28#define SPI_EEPROM_RDSR 0x05
29#define SPI_EEPROM_READ 0x03
30#define SPI_EEPROM_WRITE 0x02
31
32#ifndef CONFIG_DEFAULT_SPI_BUS
33#define CONFIG_DEFAULT_SPI_BUS 0
34#endif
35
36#ifndef CONFIG_DEFAULT_SPI_MODE
37#define CONFIG_DEFAULT_SPI_MODE SPI_MODE_0
38#endif
39
40ssize_t spi_read (uchar *addr, int alen, uchar *buffer, int len)
41{
42 struct spi_slave *slave;
43 u8 cmd = SPI_EEPROM_READ;
44
45 slave = spi_setup_slave(CONFIG_DEFAULT_SPI_BUS, 1, 1000000,
46 CONFIG_DEFAULT_SPI_MODE);
47 if(!slave)
48 return 0;
49
50 spi_claim_bus(slave);
51
52
53 if(spi_xfer(slave, 8, &cmd, NULL, SPI_XFER_BEGIN))
54 return -1;
55
56
57
58
59
60 if (alen == 3)
61 addr++;
62
63
64 if(spi_xfer(slave, 16, addr, NULL, 0))
65 return -1;
66 if(spi_xfer(slave, 8 * len, NULL, buffer, SPI_XFER_END))
67 return -1;
68
69 spi_release_bus(slave);
70 spi_free_slave(slave);
71 return len;
72}
73
74ssize_t spi_write (uchar *addr, int alen, uchar *buffer, int len)
75{
76 struct spi_slave *slave;
77 char buf[3];
78 ulong start;
79
80 slave = spi_setup_slave(CONFIG_DEFAULT_SPI_BUS, 1, 1000000,
81 CONFIG_DEFAULT_SPI_MODE);
82 if (!slave)
83 return 0;
84
85 spi_claim_bus(slave);
86
87 buf[0] = SPI_EEPROM_WREN;
88 if(spi_xfer(slave, 8, buf, NULL, SPI_XFER_BEGIN | SPI_XFER_END))
89 return -1;
90
91 buf[0] = SPI_EEPROM_WRITE;
92
93
94 if (alen == 3) {
95 alen--;
96 addr++;
97 }
98
99 memcpy(buf + 1, addr, alen);
100
101 if(spi_xfer(slave, 24, buf, NULL, SPI_XFER_BEGIN))
102 return -1;
103 if(spi_xfer(slave, len * 8, buffer, NULL, SPI_XFER_END))
104 return -1;
105
106 start = get_timer(0);
107 do {
108 buf[0] = SPI_EEPROM_RDSR;
109 buf[1] = 0;
110 spi_xfer(slave, 16, buf, buf, SPI_XFER_BEGIN | SPI_XFER_END);
111
112 if (!(buf[1] & 1))
113 break;
114
115 } while (get_timer(start) < CONFIG_SYS_SPI_WRITE_TOUT);
116
117 if (buf[1] & 1)
118 printf ("*** spi_write: Time out while writing!\n");
119
120 spi_release_bus(slave);
121 spi_free_slave(slave);
122 return len;
123}
124