[realview] add more missing config symbols
[openwrt.git] / package / uboot-ar71xx / files / drivers / spi / ar71xx_spi.c
1 /*
2 * (C) Copyright 2010
3 * Michael Kurz <michi.kurz@googlemail.com>
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24 #include <common.h>
25 #include <malloc.h>
26 #include <spi.h>
27
28 #include <asm/addrspace.h>
29 #include <asm/types.h>
30 #include <asm/ar71xx.h>
31
32 /*-----------------------------------------------------------------------
33 * Definitions
34 */
35
36 #ifdef DEBUG_SPI
37 #define PRINTD(fmt,args...) printf (fmt ,##args)
38 #else
39 #define PRINTD(fmt,args...)
40 #endif
41
42 struct ar71xx_spi_slave {
43 struct spi_slave slave;
44 unsigned int mode;
45 };
46
47 static inline struct ar71xx_spi_slave *to_ar71xx_spi(struct spi_slave *slave)
48 {
49 return container_of(slave, struct ar71xx_spi_slave, slave);
50 }
51
52 /*=====================================================================*/
53 /* Public Functions */
54 /*=====================================================================*/
55
56 /*-----------------------------------------------------------------------
57 * Initialization
58 */
59
60 void spi_init()
61 {
62 PRINTD("ar71xx_spi: spi_init");
63
64 // Init SPI Hardware, disable remap, set clock
65 __raw_writel(0x43, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_CTRL));
66
67 PRINTD(" ---> out\n");
68 }
69
70 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
71 unsigned int max_hz, unsigned int mode)
72 {
73 struct ar71xx_spi_slave *ss;
74
75 PRINTD("ar71xx_spi: spi_setup_slave");
76
77 if ((bus != 0) || (cs > 2))
78 return NULL;
79
80 ss = malloc(sizeof(struct ar71xx_spi_slave));
81 if (!ss)
82 return NULL;
83
84 ss->slave.bus = bus;
85 ss->slave.cs = cs;
86 ss->mode = mode;
87
88 /* TODO: Use max_hz to limit the SCK rate */
89
90 PRINTD(" ---> out\n");
91
92 return &ss->slave;
93 }
94
95 void spi_free_slave(struct spi_slave *slave)
96 {
97 struct ar71xx_spi_slave *ss = to_ar71xx_spi(slave);
98
99 free(ss);
100 }
101
102 int spi_claim_bus(struct spi_slave *slave)
103 {
104
105 return 0;
106 }
107
108 void spi_release_bus(struct spi_slave *slave)
109 {
110
111 }
112
113 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
114 void *din, unsigned long flags)
115 {
116 struct ar71xx_spi_slave *ss = to_ar71xx_spi(slave);
117 uint8_t *rx = din;
118 const uint8_t *tx = dout;
119 uint8_t curbyte, curbitlen, restbits;
120 uint32_t bytes = bitlen / 8;
121 uint32_t out;
122 uint32_t in;
123
124 PRINTD("ar71xx_spi: spi_xfer: slave:%p bitlen:%08x dout:%p din:%p flags:%08x\n", slave, bitlen, dout, din, flags);
125
126 if (flags & SPI_XFER_BEGIN) {
127 __raw_writel(SPI_FS_GPIO, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_FS));
128 __raw_writel(SPI_IOC_CS_ALL, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_IOC));
129 }
130
131 restbits = (bitlen % 8);
132 if (restbits != 0)
133 bytes++;
134
135 // enable chip select
136 out = SPI_IOC_CS_ALL & ~(SPI_IOC_CS(slave->cs));
137
138 while (bytes--) {
139
140 curbyte = 0;
141 if (tx) {
142 curbyte = *tx++;
143 }
144
145 if (restbits != 0) {
146 curbitlen = restbits;
147 curbyte <<= 8 - restbits;
148 } else {
149 curbitlen = 8;
150 }
151
152 PRINTD("ar71xx_spi: sending: data:%02x length:%d\n", curbyte, curbitlen);
153
154 /* clock starts at inactive polarity */
155 for (curbyte <<= (8 - curbitlen); curbitlen; curbitlen--) {
156
157 if (curbyte & (1 << 7))
158 out |= SPI_IOC_DO;
159 else
160 out &= ~(SPI_IOC_DO);
161
162 /* setup MSB (to slave) on trailing edge */
163 __raw_writel(out, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_IOC));
164
165 __raw_writel(out | SPI_IOC_CLK, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_IOC));
166
167 curbyte <<= 1;
168 }
169
170 in = __raw_readl(KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_RDS));
171 PRINTD("ar71xx_spi: received:%02x\n", in);
172
173 if (rx) {
174 if (restbits == 0) {
175 *rx++ = in;
176 } else {
177 *rx++ = (in << (8 - restbits));
178 }
179 }
180 }
181
182 if (flags & SPI_XFER_END) {
183 __raw_writel(SPI_IOC_CS(slave->cs), KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_IOC));
184 __raw_writel(SPI_IOC_CS_ALL, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_IOC));
185 __raw_writel(0, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_FS));
186 }
187
188 PRINTD(" ---> out\n");
189
190 return 0;
191 }
This page took 0.052651 seconds and 5 git commands to generate.