blob: b237c7c0feac747eeaf8649e65ab8fba3407bdbb [file] [log] [blame]
Ajay Bhargavb1b08202011-10-03 14:00:57 +05301/*
2 * (C) Copyright 2011
3 * eInfochips Ltd. <www.einfochips.com>
4 * Written-by: Ajay Bhargav <ajay.bhargav@einfochips.com>
5 *
6 * (C) Copyright 2009
7 * Marvell Semiconductor <www.marvell.com>
8 * Based on SSP driver
9 * Written-by: Lei Wen <leiwen@marvell.com>
10 *
11 * See file CREDITS for list of people who contributed to this
12 * project.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27 * MA 02110-1301 USA
28 */
29
30
31#include <common.h>
32#include <malloc.h>
33#include <spi.h>
34
35#include <asm/io.h>
36#include <asm/arch/spi.h>
37#include <asm/gpio.h>
38
39#define to_armd_spi_slave(s) container_of(s, struct armd_spi_slave, slave)
40
41struct armd_spi_slave {
42 struct spi_slave slave;
43 struct ssp_reg *spi_reg;
44 u32 cr0, cr1;
45 u32 int_cr1;
46 u32 clear_sr;
47 const void *tx;
48 void *rx;
49 int gpio_cs_inverted;
50};
51
52static int spi_armd_write(struct armd_spi_slave *pss)
53{
54 int wait_timeout = SSP_FLUSH_NUM;
55 while (--wait_timeout && !(readl(&pss->spi_reg->sssr) & SSSR_TNF))
56 ;
57 if (!wait_timeout) {
58 debug("%s: timeout error\n", __func__);
59 return -1;
60 }
61
62 if (pss->tx != NULL) {
63 writel(*(u8 *)pss->tx, &pss->spi_reg->ssdr);
64 ++pss->tx;
65 } else {
66 writel(0, &pss->spi_reg->ssdr);
67 }
68 return 0;
69}
70
71static int spi_armd_read(struct armd_spi_slave *pss)
72{
73 int wait_timeout = SSP_FLUSH_NUM;
74 while (--wait_timeout && !(readl(&pss->spi_reg->sssr) & SSSR_RNE))
75 ;
76 if (!wait_timeout) {
77 debug("%s: timeout error\n", __func__);
78 return -1;
79 }
80
81 if (pss->rx != NULL) {
82 *(u8 *)pss->rx = readl(&pss->spi_reg->ssdr);
83 ++pss->rx;
84 } else {
85 readl(&pss->spi_reg->ssdr);
86 }
87 return 0;
88}
89
90static int spi_armd_flush(struct armd_spi_slave *pss)
91{
92 unsigned long limit = SSP_FLUSH_NUM;
93
94 do {
95 while (readl(&pss->spi_reg->sssr) & SSSR_RNE)
96 readl(&pss->spi_reg->ssdr);
97 } while ((readl(&pss->spi_reg->sssr) & SSSR_BSY) && limit--);
98
99 writel(SSSR_ROR, &pss->spi_reg->sssr);
100
101 return limit;
102}
103
104void spi_cs_activate(struct spi_slave *slave)
105{
106 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
107
108 gpio_set_value(slave->cs, pss->gpio_cs_inverted);
109}
110
111void spi_cs_deactivate(struct spi_slave *slave)
112{
113 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
114
115 gpio_set_value(slave->cs, !pss->gpio_cs_inverted);
116}
117
118struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
119 unsigned int max_hz, unsigned int mode)
120{
121 struct armd_spi_slave *pss;
122
Simon Glassd034a952013-03-18 19:23:40 +0000123 pss = spi_alloc_slave(struct armd_spi_slave, bus, cs);
Ajay Bhargavb1b08202011-10-03 14:00:57 +0530124 if (!pss)
125 return NULL;
126
Ajay Bhargavb1b08202011-10-03 14:00:57 +0530127 pss->spi_reg = (struct ssp_reg *)SSP_REG_BASE(CONFIG_SYS_SSP_PORT);
128
129 pss->cr0 = SSCR0_MOTO | SSCR0_DATASIZE(DEFAULT_WORD_LEN) | SSCR0_SSE;
130
131 pss->cr1 = (SSCR1_RXTRESH(RX_THRESH_DEF) & SSCR1_RFT) |
132 (SSCR1_TXTRESH(TX_THRESH_DEF) & SSCR1_TFT);
133 pss->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
134 pss->cr1 |= (((mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
135 | (((mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
136
137 pss->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
138 pss->clear_sr = SSSR_ROR | SSSR_TINT;
139
140 pss->gpio_cs_inverted = mode & SPI_CS_HIGH;
141 gpio_set_value(cs, !pss->gpio_cs_inverted);
142
143 return &pss->slave;
144}
145
146void spi_free_slave(struct spi_slave *slave)
147{
148 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
149
150 free(pss);
151}
152
153int spi_claim_bus(struct spi_slave *slave)
154{
155 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
156
157 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
158 if (spi_armd_flush(pss) == 0)
159 return -1;
160
161 return 0;
162}
163
164void spi_release_bus(struct spi_slave *slave)
165{
166}
167
168int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
169 void *din, unsigned long flags)
170{
171 struct armd_spi_slave *pss = to_armd_spi_slave(slave);
172 uint bytes = bitlen / 8;
173 unsigned long limit;
174 int ret = 0;
175
176 if (bitlen == 0)
177 goto done;
178
179 /* we can only do 8 bit transfers */
180 if (bitlen % 8) {
181 flags |= SPI_XFER_END;
182 goto done;
183 }
184
Axel Linb3814442013-06-11 21:57:31 +0800185 pss->tx = dout;
186 pss->rx = din;
Ajay Bhargavb1b08202011-10-03 14:00:57 +0530187
188 if (flags & SPI_XFER_BEGIN) {
189 spi_cs_activate(slave);
190 writel(pss->cr1 | pss->int_cr1, &pss->spi_reg->sscr1);
191 writel(TIMEOUT_DEF, &pss->spi_reg->ssto);
192 writel(pss->cr0, &pss->spi_reg->sscr0);
193 }
194
195 while (bytes--) {
196 limit = SSP_FLUSH_NUM;
197 ret = spi_armd_write(pss);
198 if (ret)
199 break;
200
201 while ((readl(&pss->spi_reg->sssr) & SSSR_BSY) && limit--)
202 udelay(1);
203
204 ret = spi_armd_read(pss);
205 if (ret)
206 break;
207 }
208
209 done:
210 if (flags & SPI_XFER_END) {
211 /* Stop SSP */
212 writel(pss->clear_sr, &pss->spi_reg->sssr);
213 clrbits_le32(&pss->spi_reg->sscr1, pss->int_cr1);
214 writel(0, &pss->spi_reg->ssto);
215 spi_cs_deactivate(slave);
216 }
217
218 return ret;
219}