blob: e15a63caca9f342741a72670b79ab4ca5c94b32f [file] [log] [blame]
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +02001/*
2 * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17 * MA 02111-1307 USA
18 *
19 */
20
21#include <common.h>
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020022#include <malloc.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020023#include <spi.h>
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +010024#include <asm/errno.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020025#include <asm/io.h>
26
27#ifdef CONFIG_MX27
28/* i.MX27 has a completely wrong register layout and register definitions in the
29 * datasheet, the correct one is in the Freescale's Linux driver */
30
31#error "i.MX27 CSPI not supported due to drastic differences in register definisions" \
32"See linux mxc_spi driver from Freescale for details."
33
Stefano Babic6e6f4552010-04-04 22:43:38 +020034#elif defined(CONFIG_MX31)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020035
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +010036#include <asm/arch/mx31.h>
37
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020038#define MXC_CSPIRXDATA 0x00
39#define MXC_CSPITXDATA 0x04
40#define MXC_CSPICTRL 0x08
41#define MXC_CSPIINT 0x0C
42#define MXC_CSPIDMA 0x10
43#define MXC_CSPISTAT 0x14
44#define MXC_CSPIPERIOD 0x18
45#define MXC_CSPITEST 0x1C
46#define MXC_CSPIRESET 0x00
47
48#define MXC_CSPICTRL_EN (1 << 0)
49#define MXC_CSPICTRL_MODE (1 << 1)
50#define MXC_CSPICTRL_XCH (1 << 2)
51#define MXC_CSPICTRL_SMC (1 << 3)
52#define MXC_CSPICTRL_POL (1 << 4)
53#define MXC_CSPICTRL_PHA (1 << 5)
54#define MXC_CSPICTRL_SSCTL (1 << 6)
Wolfgang Denka1be4762008-05-20 16:00:29 +020055#define MXC_CSPICTRL_SSPOL (1 << 7)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020056#define MXC_CSPICTRL_CHIPSELECT(x) (((x) & 0x3) << 24)
57#define MXC_CSPICTRL_BITCOUNT(x) (((x) & 0x1f) << 8)
58#define MXC_CSPICTRL_DATARATE(x) (((x) & 0x7) << 16)
Stefano Babic6e6f4552010-04-04 22:43:38 +020059#define MXC_CSPICTRL_TC (1 << 8)
60#define MXC_CSPICTRL_RXOVF (1 << 6)
61#define MXC_CSPICTRL_MAXBITS 0x1f
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020062
63#define MXC_CSPIPERIOD_32KHZ (1 << 15)
64
65static unsigned long spi_bases[] = {
66 0x43fa4000,
67 0x50010000,
68 0x53f84000,
69};
70
Stefano Babic6e6f4552010-04-04 22:43:38 +020071#define OUT MX31_GPIO_DIRECTION_OUT
72#define mxc_gpio_direction mx31_gpio_direction
73#define mxc_gpio_set mx31_gpio_set
74#elif defined(CONFIG_MX51)
75#include <asm/arch/imx-regs.h>
76#include <asm/arch/clock.h>
77
78#define MXC_CSPIRXDATA 0x00
79#define MXC_CSPITXDATA 0x04
80#define MXC_CSPICTRL 0x08
81#define MXC_CSPICON 0x0C
82#define MXC_CSPIINT 0x10
83#define MXC_CSPIDMA 0x14
84#define MXC_CSPISTAT 0x18
85#define MXC_CSPIPERIOD 0x1C
86#define MXC_CSPIRESET 0x00
87#define MXC_CSPICTRL_EN (1 << 0)
88#define MXC_CSPICTRL_MODE (1 << 1)
89#define MXC_CSPICTRL_XCH (1 << 2)
90#define MXC_CSPICTRL_CHIPSELECT(x) (((x) & 0x3) << 12)
91#define MXC_CSPICTRL_BITCOUNT(x) (((x) & 0xfff) << 20)
92#define MXC_CSPICTRL_PREDIV(x) (((x) & 0xF) << 12)
93#define MXC_CSPICTRL_POSTDIV(x) (((x) & 0xF) << 8)
94#define MXC_CSPICTRL_SELCHAN(x) (((x) & 0x3) << 18)
95#define MXC_CSPICTRL_MAXBITS 0xfff
96#define MXC_CSPICTRL_TC (1 << 7)
97#define MXC_CSPICTRL_RXOVF (1 << 6)
98
99#define MXC_CSPIPERIOD_32KHZ (1 << 15)
100
101/* Bit position inside CTRL register to be associated with SS */
102#define MXC_CSPICTRL_CHAN 18
103
104/* Bit position inside CON register to be associated with SS */
105#define MXC_CSPICON_POL 4
106#define MXC_CSPICON_PHA 0
107#define MXC_CSPICON_SSPOL 12
108
109static unsigned long spi_bases[] = {
110 CSPI1_BASE_ADDR,
111 CSPI2_BASE_ADDR,
112 CSPI3_BASE_ADDR,
113};
114#define mxc_gpio_direction(gpio, dir) (0)
115#define mxc_gpio_set(gpio, value) {}
116#define OUT 1
117#else
118#error "Unsupported architecture"
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200119#endif
120
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200121struct mxc_spi_slave {
122 struct spi_slave slave;
123 unsigned long base;
124 u32 ctrl_reg;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200125#if defined(CONFIG_MX51)
126 u32 cfg_reg;
127#endif
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100128 int gpio;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200129};
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200130
131static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
132{
133 return container_of(slave, struct mxc_spi_slave, slave);
134}
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200135
136static inline u32 reg_read(unsigned long addr)
137{
138 return *(volatile unsigned long*)addr;
139}
140
141static inline void reg_write(unsigned long addr, u32 val)
142{
143 *(volatile unsigned long*)addr = val;
144}
Stefano Babic6e6f4552010-04-04 22:43:38 +0200145
146void spi_cs_activate(struct spi_slave *slave)
147{
148 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
149 if (mxcs->gpio > 0)
150 mxc_gpio_set(mxcs->gpio, mxcs->ctrl_reg & MXC_CSPICTRL_SSPOL);
151}
152
153void spi_cs_deactivate(struct spi_slave *slave)
154{
155 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
156 if (mxcs->gpio > 0)
157 mxc_gpio_set(mxcs->gpio,
158 !(mxcs->ctrl_reg & MXC_CSPICTRL_SSPOL));
159}
160
161#ifdef CONFIG_MX51
162static s32 spi_cfg(struct mxc_spi_slave *mxcs, unsigned int cs,
163 unsigned int max_hz, unsigned int mode)
164{
165 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
166 s32 pre_div = 0, post_div = 0, i, reg_ctrl, reg_config;
167 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0;
168
169 if (max_hz == 0) {
170 printf("Error: desired clock is 0\n");
171 return -1;
172 }
173
174 reg_ctrl = reg_read(mxcs->base + MXC_CSPICTRL);
175
176 /* Reset spi */
177 reg_write(mxcs->base + MXC_CSPICTRL, 0);
178 reg_write(mxcs->base + MXC_CSPICTRL, (reg_ctrl | 0x1));
179
180 /*
181 * The following computation is taken directly from Freescale's code.
182 */
183 if (clk_src > max_hz) {
184 pre_div = clk_src / max_hz;
185 if (pre_div > 16) {
186 post_div = pre_div / 16;
187 pre_div = 15;
188 }
189 if (post_div != 0) {
190 for (i = 0; i < 16; i++) {
191 if ((1 << i) >= post_div)
192 break;
193 }
194 if (i == 16) {
195 printf("Error: no divider for the freq: %d\n",
196 max_hz);
197 return -1;
198 }
199 post_div = i;
200 }
201 }
202
203 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
204 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
205 MXC_CSPICTRL_SELCHAN(cs);
206 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
207 MXC_CSPICTRL_PREDIV(pre_div);
208 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
209 MXC_CSPICTRL_POSTDIV(post_div);
210
211 /* always set to master mode */
212 reg_ctrl |= 1 << (cs + 4);
213
214 /* We need to disable SPI before changing registers */
215 reg_ctrl &= ~MXC_CSPICTRL_EN;
216
217 if (mode & SPI_CS_HIGH)
218 ss_pol = 1;
219
220 if (!(mode & SPI_CPOL))
221 sclkpol = 1;
222
223 if (mode & SPI_CPHA)
224 sclkpha = 1;
225
226 reg_config = reg_read(mxcs->base + MXC_CSPICON);
227
228 /*
229 * Configuration register setup
230 * The MX51 has support different setup for each SS
231 */
232 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
233 (ss_pol << (cs + MXC_CSPICON_SSPOL));
234 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
235 (sclkpol << (cs + MXC_CSPICON_POL));
236 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
237 (sclkpha << (cs + MXC_CSPICON_PHA));
238
239 debug("reg_ctrl = 0x%x\n", reg_ctrl);
240 reg_write(mxcs->base + MXC_CSPICTRL, reg_ctrl);
241 debug("reg_config = 0x%x\n", reg_config);
242 reg_write(mxcs->base + MXC_CSPICON, reg_config);
243
244 /* save config register and control register */
245 mxcs->ctrl_reg = reg_ctrl;
246 mxcs->cfg_reg = reg_config;
247
248 /* clear interrupt reg */
249 reg_write(mxcs->base + MXC_CSPIINT, 0);
250 reg_write(mxcs->base + MXC_CSPISTAT,
251 MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
252
253 return 0;
254}
255#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200256
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100257static u32 spi_xchg_single(struct spi_slave *slave, u32 data, int bitlen,
258 unsigned long flags)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200259{
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200260 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200261
262 if (flags & SPI_XFER_BEGIN)
263 spi_cs_activate(slave);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200264
Stefano Babic6e6f4552010-04-04 22:43:38 +0200265 mxcs->ctrl_reg = (mxcs->ctrl_reg &
266 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100267 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200268
Stefano Babic6e6f4552010-04-04 22:43:38 +0200269 reg_write(mxcs->base + MXC_CSPICTRL, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
270#ifdef CONFIG_MX51
271 reg_write(mxcs->base + MXC_CSPICON, mxcs->cfg_reg);
272#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200273
Stefano Babic6e6f4552010-04-04 22:43:38 +0200274 /* Clear interrupt register */
275 reg_write(mxcs->base + MXC_CSPISTAT,
276 MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100277
Stefano Babic6e6f4552010-04-04 22:43:38 +0200278 debug("Sending SPI 0x%x\n", data);
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100279 reg_write(mxcs->base + MXC_CSPITXDATA, data);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200280
Stefano Babic6e6f4552010-04-04 22:43:38 +0200281 /* FIFO is written, now starts the transfer setting the XCH bit */
282 reg_write(mxcs->base + MXC_CSPICTRL, mxcs->ctrl_reg |
283 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200284
Stefano Babic6e6f4552010-04-04 22:43:38 +0200285 /* Wait until the TC (Transfer completed) bit is set */
286 while ((reg_read(mxcs->base + MXC_CSPISTAT) & MXC_CSPICTRL_TC) == 0)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200287 ;
288
Stefano Babic6e6f4552010-04-04 22:43:38 +0200289 /* Transfer completed, clear any pending request */
290 reg_write(mxcs->base + MXC_CSPISTAT,
291 MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
292
293 data = reg_read(mxcs->base + MXC_CSPIRXDATA);
294 debug("SPI Rx: 0x%x\n", data);
295
296 if (flags & SPI_XFER_END)
297 spi_cs_deactivate(slave);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100298
Stefano Babic6e6f4552010-04-04 22:43:38 +0200299 return data;
300
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200301}
302
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200303int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
304 void *din, unsigned long flags)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200305{
306 int n_blks = (bitlen + 31) / 32;
307 u32 *out_l, *in_l;
308 int i;
309
310 if ((int)dout & 3 || (int)din & 3) {
311 printf("Error: unaligned buffers in: %p, out: %p\n", din, dout);
312 return 1;
313 }
314
Magnus Lilja1858a9a2010-02-09 22:05:39 +0100315 /* This driver is currently partly broken, alert the user */
316 if (bitlen > 16 && (bitlen % 32)) {
317 printf("Error: SPI transfer with bitlen=%d is broken.\n",
318 bitlen);
319 return 1;
320 }
321
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200322 for (i = 0, in_l = (u32 *)din, out_l = (u32 *)dout;
323 i < n_blks;
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100324 i++, in_l++, out_l++, bitlen -= 32) {
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100325 u32 data = spi_xchg_single(slave, *out_l, bitlen, flags);
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100326
327 /* Check if we're only transfering 8 or 16 bits */
328 if (!i) {
329 if (bitlen < 9)
330 *(u8 *)din = data;
331 else if (bitlen < 17)
332 *(u16 *)din = data;
Magnus Lilja1858a9a2010-02-09 22:05:39 +0100333 else
334 *in_l = data;
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100335 }
336 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200337
338 return 0;
339}
340
341void spi_init(void)
342{
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100343}
344
345static int decode_cs(struct mxc_spi_slave *mxcs, unsigned int cs)
346{
347 int ret;
348
349 /*
350 * Some SPI devices require active chip-select over multiple
351 * transactions, we achieve this using a GPIO. Still, the SPI
352 * controller has to be configured to use one of its own chipselects.
353 * To use this feature you have to call spi_setup_slave() with
354 * cs = internal_cs | (gpio << 8), and you have to use some unused
355 * on this SPI controller cs between 0 and 3.
356 */
357 if (cs > 3) {
358 mxcs->gpio = cs >> 8;
359 cs &= 3;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200360 ret = mxc_gpio_direction(mxcs->gpio, OUT);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100361 if (ret) {
362 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
363 return -EINVAL;
364 }
365 } else {
366 mxcs->gpio = -1;
367 }
368
369 return cs;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200370}
371
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200372struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
373 unsigned int max_hz, unsigned int mode)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200374{
375 unsigned int ctrl_reg;
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200376 struct mxc_spi_slave *mxcs;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100377 int ret;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200378
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100379 if (bus >= ARRAY_SIZE(spi_bases))
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200380 return NULL;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200381
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100382 mxcs = malloc(sizeof(struct mxc_spi_slave));
383 if (!mxcs)
384 return NULL;
385
386 ret = decode_cs(mxcs, cs);
387 if (ret < 0) {
388 free(mxcs);
389 return NULL;
390 }
391
392 cs = ret;
393
Stefano Babic6e6f4552010-04-04 22:43:38 +0200394 mxcs->slave.bus = bus;
395 mxcs->slave.cs = cs;
396 mxcs->base = spi_bases[bus];
397
398#ifdef CONFIG_MX51
399 /* Can be used for i.MX31 too ? */
400 ctrl_reg = 0;
401 ret = spi_cfg(mxcs, cs, max_hz, mode);
402 if (ret) {
403 printf("mxc_spi: cannot setup SPI controller\n");
404 free(mxcs);
405 return NULL;
406 }
407#else
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200408 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200409 MXC_CSPICTRL_BITCOUNT(31) |
410 MXC_CSPICTRL_DATARATE(7) | /* FIXME: calculate data rate */
411 MXC_CSPICTRL_EN |
412 MXC_CSPICTRL_MODE;
413
414 if (mode & SPI_CPHA)
415 ctrl_reg |= MXC_CSPICTRL_PHA;
416 if (!(mode & SPI_CPOL))
417 ctrl_reg |= MXC_CSPICTRL_POL;
418 if (mode & SPI_CS_HIGH)
419 ctrl_reg |= MXC_CSPICTRL_SSPOL;
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200420 mxcs->ctrl_reg = ctrl_reg;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200421#endif
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200422 return &mxcs->slave;
423}
424
425void spi_free_slave(struct spi_slave *slave)
426{
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100427 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
428
429 free(mxcs);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200430}
431
432int spi_claim_bus(struct spi_slave *slave)
433{
434 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
435
436 reg_write(mxcs->base + MXC_CSPIRESET, 1);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200437 udelay(1);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200438 reg_write(mxcs->base + MXC_CSPICTRL, mxcs->ctrl_reg);
439 reg_write(mxcs->base + MXC_CSPIPERIOD,
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200440 MXC_CSPIPERIOD_32KHZ);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200441 reg_write(mxcs->base + MXC_CSPIINT, 0);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200442
443 return 0;
444}
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200445
446void spi_release_bus(struct spi_slave *slave)
447{
448 /* TODO: Shut the controller down */
449}