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 spi_claim_bus(slave);
48
49
50 if(spi_xfer(slave, 8, &cmd, NULL, SPI_XFER_BEGIN))
51 return -1;
52
53
54
55
56
57 if (alen == 3)
58 addr++;
59
60
61 if(spi_xfer(slave, 16, addr, NULL, 0))
62 return -1;
63 if(spi_xfer(slave, 8 * len, NULL, buffer, SPI_XFER_END))
64 return -1;
65
66 spi_release_bus(slave);
67 spi_free_slave(slave);
68 return len;
69}
70
71ssize_t spi_write (uchar *addr, int alen, uchar *buffer, int len)
72{
73 struct spi_slave *slave;
74 char buf[3];
75
76 slave = spi_setup_slave(CONFIG_DEFAULT_SPI_BUS, 1, 1000000,
77 CONFIG_DEFAULT_SPI_MODE);
78 spi_claim_bus(slave);
79
80 buf[0] = SPI_EEPROM_WREN;
81 if(spi_xfer(slave, 8, buf, NULL, SPI_XFER_BEGIN | SPI_XFER_END))
82 return -1;
83
84 buf[0] = SPI_EEPROM_WRITE;
85
86
87 if (alen == 3) {
88 alen--;
89 addr++;
90 }
91
92 memcpy(buf + 1, addr, alen);
93
94 if(spi_xfer(slave, 24, buf, NULL, SPI_XFER_BEGIN))
95 return -1;
96 if(spi_xfer(slave, len * 8, buffer, NULL, SPI_XFER_END))
97 return -1;
98
99 reset_timer_masked();
100 do {
101 buf[0] = SPI_EEPROM_RDSR;
102 buf[1] = 0;
103 spi_xfer(slave, 16, buf, buf, SPI_XFER_BEGIN | SPI_XFER_END);
104
105 if (!(buf[1] & 1))
106 break;
107
108 } while (get_timer_masked() < CONFIG_SYS_SPI_WRITE_TOUT);
109
110 if (buf[1] & 1)
111 printf ("*** spi_write: Time out while writing!\n");
112
113 spi_release_bus(slave);
114 spi_free_slave(slave);
115 return len;
116}
117