blob: 567071457d0333635b9b9f315cb4f8b954969faa [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>
Stefano Babicd77fe992010-07-06 17:05:06 +020026#include <mxc_gpio.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020027
28#ifdef CONFIG_MX27
29/* i.MX27 has a completely wrong register layout and register definitions in the
30 * datasheet, the correct one is in the Freescale's Linux driver */
31
32#error "i.MX27 CSPI not supported due to drastic differences in register definisions" \
33"See linux mxc_spi driver from Freescale for details."
34
Stefano Babic6e6f4552010-04-04 22:43:38 +020035#elif defined(CONFIG_MX31)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020036
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +010037#include <asm/arch/mx31.h>
38
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020039#define MXC_CSPIRXDATA 0x00
40#define MXC_CSPITXDATA 0x04
41#define MXC_CSPICTRL 0x08
42#define MXC_CSPIINT 0x0C
43#define MXC_CSPIDMA 0x10
44#define MXC_CSPISTAT 0x14
45#define MXC_CSPIPERIOD 0x18
46#define MXC_CSPITEST 0x1C
47#define MXC_CSPIRESET 0x00
48
49#define MXC_CSPICTRL_EN (1 << 0)
50#define MXC_CSPICTRL_MODE (1 << 1)
51#define MXC_CSPICTRL_XCH (1 << 2)
52#define MXC_CSPICTRL_SMC (1 << 3)
53#define MXC_CSPICTRL_POL (1 << 4)
54#define MXC_CSPICTRL_PHA (1 << 5)
55#define MXC_CSPICTRL_SSCTL (1 << 6)
Wolfgang Denka1be4762008-05-20 16:00:29 +020056#define MXC_CSPICTRL_SSPOL (1 << 7)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020057#define MXC_CSPICTRL_CHIPSELECT(x) (((x) & 0x3) << 24)
58#define MXC_CSPICTRL_BITCOUNT(x) (((x) & 0x1f) << 8)
59#define MXC_CSPICTRL_DATARATE(x) (((x) & 0x7) << 16)
Stefano Babic6e6f4552010-04-04 22:43:38 +020060#define MXC_CSPICTRL_TC (1 << 8)
61#define MXC_CSPICTRL_RXOVF (1 << 6)
62#define MXC_CSPICTRL_MAXBITS 0x1f
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020063
64#define MXC_CSPIPERIOD_32KHZ (1 << 15)
Stefano Babic125f82a2010-08-20 12:05:03 +020065#define MAX_SPI_BYTES 4
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020066
67static unsigned long spi_bases[] = {
68 0x43fa4000,
69 0x50010000,
70 0x53f84000,
71};
72
Stefano Babicdcd73cd2011-01-19 22:46:30 +000073#define mxc_get_clock(x) mx31_get_ipg_clk()
74
Stefano Babic6e6f4552010-04-04 22:43:38 +020075#elif defined(CONFIG_MX51)
76#include <asm/arch/imx-regs.h>
77#include <asm/arch/clock.h>
78
79#define MXC_CSPIRXDATA 0x00
80#define MXC_CSPITXDATA 0x04
81#define MXC_CSPICTRL 0x08
82#define MXC_CSPICON 0x0C
83#define MXC_CSPIINT 0x10
84#define MXC_CSPIDMA 0x14
85#define MXC_CSPISTAT 0x18
86#define MXC_CSPIPERIOD 0x1C
87#define MXC_CSPIRESET 0x00
88#define MXC_CSPICTRL_EN (1 << 0)
89#define MXC_CSPICTRL_MODE (1 << 1)
90#define MXC_CSPICTRL_XCH (1 << 2)
91#define MXC_CSPICTRL_CHIPSELECT(x) (((x) & 0x3) << 12)
92#define MXC_CSPICTRL_BITCOUNT(x) (((x) & 0xfff) << 20)
93#define MXC_CSPICTRL_PREDIV(x) (((x) & 0xF) << 12)
94#define MXC_CSPICTRL_POSTDIV(x) (((x) & 0xF) << 8)
95#define MXC_CSPICTRL_SELCHAN(x) (((x) & 0x3) << 18)
96#define MXC_CSPICTRL_MAXBITS 0xfff
97#define MXC_CSPICTRL_TC (1 << 7)
98#define MXC_CSPICTRL_RXOVF (1 << 6)
99
100#define MXC_CSPIPERIOD_32KHZ (1 << 15)
Stefano Babic125f82a2010-08-20 12:05:03 +0200101#define MAX_SPI_BYTES 32
Stefano Babic6e6f4552010-04-04 22:43:38 +0200102
103/* Bit position inside CTRL register to be associated with SS */
104#define MXC_CSPICTRL_CHAN 18
105
106/* Bit position inside CON register to be associated with SS */
107#define MXC_CSPICON_POL 4
108#define MXC_CSPICON_PHA 0
109#define MXC_CSPICON_SSPOL 12
110
111static unsigned long spi_bases[] = {
112 CSPI1_BASE_ADDR,
113 CSPI2_BASE_ADDR,
114 CSPI3_BASE_ADDR,
115};
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000116
117#elif defined(CONFIG_MX35)
118
119#include <asm/arch/imx-regs.h>
120#include <asm/arch/clock.h>
121
122#define MXC_CSPIRXDATA 0x00
123#define MXC_CSPITXDATA 0x04
124#define MXC_CSPICTRL 0x08
125#define MXC_CSPIINT 0x0C
126#define MXC_CSPIDMA 0x10
127#define MXC_CSPISTAT 0x14
128#define MXC_CSPIPERIOD 0x18
129#define MXC_CSPITEST 0x1C
130#define MXC_CSPIRESET 0x00
131
132#define MXC_CSPICTRL_EN (1 << 0)
133#define MXC_CSPICTRL_MODE (1 << 1)
134#define MXC_CSPICTRL_XCH (1 << 2)
135#define MXC_CSPICTRL_SMC (1 << 3)
136#define MXC_CSPICTRL_POL (1 << 4)
137#define MXC_CSPICTRL_PHA (1 << 5)
138#define MXC_CSPICTRL_SSCTL (1 << 6)
139#define MXC_CSPICTRL_SSPOL (1 << 7)
140#define MXC_CSPICTRL_CHIPSELECT(x) (((x) & 0x3) << 12)
141#define MXC_CSPICTRL_BITCOUNT(x) (((x) & 0xfff) << 20)
142#define MXC_CSPICTRL_DATARATE(x) (((x) & 0x7) << 16)
143#define MXC_CSPICTRL_TC (1 << 7)
144#define MXC_CSPICTRL_RXOVF (1 << 6)
145#define MXC_CSPICTRL_MAXBITS 0xfff
146
147#define MXC_CSPIPERIOD_32KHZ (1 << 15)
148#define MAX_SPI_BYTES 4
149
150static unsigned long spi_bases[] = {
151 0x43fa4000,
152 0x50010000,
153};
154
Stefano Babic6e6f4552010-04-04 22:43:38 +0200155#else
156#error "Unsupported architecture"
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200157#endif
158
Stefano Babicd77fe992010-07-06 17:05:06 +0200159#define OUT MXC_GPIO_DIRECTION_OUT
160
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200161struct mxc_spi_slave {
162 struct spi_slave slave;
163 unsigned long base;
164 u32 ctrl_reg;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200165#if defined(CONFIG_MX51)
166 u32 cfg_reg;
167#endif
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100168 int gpio;
Stefano Babicd77fe992010-07-06 17:05:06 +0200169 int ss_pol;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200170};
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200171
172static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
173{
174 return container_of(slave, struct mxc_spi_slave, slave);
175}
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200176
177static inline u32 reg_read(unsigned long addr)
178{
179 return *(volatile unsigned long*)addr;
180}
181
182static inline void reg_write(unsigned long addr, u32 val)
183{
184 *(volatile unsigned long*)addr = val;
185}
Stefano Babic6e6f4552010-04-04 22:43:38 +0200186
187void spi_cs_activate(struct spi_slave *slave)
188{
189 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
190 if (mxcs->gpio > 0)
Stefano Babicd77fe992010-07-06 17:05:06 +0200191 mxc_gpio_set(mxcs->gpio, mxcs->ss_pol);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200192}
193
194void spi_cs_deactivate(struct spi_slave *slave)
195{
196 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
197 if (mxcs->gpio > 0)
198 mxc_gpio_set(mxcs->gpio,
Stefano Babicd77fe992010-07-06 17:05:06 +0200199 !(mxcs->ss_pol));
Stefano Babic6e6f4552010-04-04 22:43:38 +0200200}
201
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000202#if defined(CONFIG_MX31) || defined(CONFIG_MX35)
203static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
204 unsigned int max_hz, unsigned int mode)
205{
206 unsigned int ctrl_reg;
207
208 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
209 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
210 MXC_CSPICTRL_DATARATE(7) | /* FIXME: calculate data rate */
211 MXC_CSPICTRL_EN |
212#ifdef CONFIG_MX35
213 MXC_CSPICTRL_SSCTL |
214#endif
215 MXC_CSPICTRL_MODE;
216
217 if (mode & SPI_CPHA)
218 ctrl_reg |= MXC_CSPICTRL_PHA;
219 if (mode & SPI_CPOL)
220 ctrl_reg |= MXC_CSPICTRL_POL;
221 if (mode & SPI_CS_HIGH)
222 ctrl_reg |= MXC_CSPICTRL_SSPOL;
223 mxcs->ctrl_reg = ctrl_reg;
224
225 return 0;
226}
227#endif
228
229#if defined(CONFIG_MX51)
230static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
Stefano Babic6e6f4552010-04-04 22:43:38 +0200231 unsigned int max_hz, unsigned int mode)
232{
233 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
234 s32 pre_div = 0, post_div = 0, i, reg_ctrl, reg_config;
235 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0;
236
237 if (max_hz == 0) {
238 printf("Error: desired clock is 0\n");
239 return -1;
240 }
241
242 reg_ctrl = reg_read(mxcs->base + MXC_CSPICTRL);
243
244 /* Reset spi */
245 reg_write(mxcs->base + MXC_CSPICTRL, 0);
246 reg_write(mxcs->base + MXC_CSPICTRL, (reg_ctrl | 0x1));
247
248 /*
249 * The following computation is taken directly from Freescale's code.
250 */
251 if (clk_src > max_hz) {
252 pre_div = clk_src / max_hz;
253 if (pre_div > 16) {
254 post_div = pre_div / 16;
255 pre_div = 15;
256 }
257 if (post_div != 0) {
258 for (i = 0; i < 16; i++) {
259 if ((1 << i) >= post_div)
260 break;
261 }
262 if (i == 16) {
263 printf("Error: no divider for the freq: %d\n",
264 max_hz);
265 return -1;
266 }
267 post_div = i;
268 }
269 }
270
271 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
272 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
273 MXC_CSPICTRL_SELCHAN(cs);
274 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
275 MXC_CSPICTRL_PREDIV(pre_div);
276 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
277 MXC_CSPICTRL_POSTDIV(post_div);
278
279 /* always set to master mode */
280 reg_ctrl |= 1 << (cs + 4);
281
282 /* We need to disable SPI before changing registers */
283 reg_ctrl &= ~MXC_CSPICTRL_EN;
284
285 if (mode & SPI_CS_HIGH)
286 ss_pol = 1;
287
Stefano Babic4c596992010-08-23 20:41:19 +0200288 if (mode & SPI_CPOL)
Stefano Babic6e6f4552010-04-04 22:43:38 +0200289 sclkpol = 1;
290
291 if (mode & SPI_CPHA)
292 sclkpha = 1;
293
294 reg_config = reg_read(mxcs->base + MXC_CSPICON);
295
296 /*
297 * Configuration register setup
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000298 * The MX51 supports different setup for each SS
Stefano Babic6e6f4552010-04-04 22:43:38 +0200299 */
300 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
301 (ss_pol << (cs + MXC_CSPICON_SSPOL));
302 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
303 (sclkpol << (cs + MXC_CSPICON_POL));
304 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
305 (sclkpha << (cs + MXC_CSPICON_PHA));
306
307 debug("reg_ctrl = 0x%x\n", reg_ctrl);
308 reg_write(mxcs->base + MXC_CSPICTRL, reg_ctrl);
309 debug("reg_config = 0x%x\n", reg_config);
310 reg_write(mxcs->base + MXC_CSPICON, reg_config);
311
312 /* save config register and control register */
313 mxcs->ctrl_reg = reg_ctrl;
314 mxcs->cfg_reg = reg_config;
315
316 /* clear interrupt reg */
317 reg_write(mxcs->base + MXC_CSPIINT, 0);
318 reg_write(mxcs->base + MXC_CSPISTAT,
319 MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
320
321 return 0;
322}
323#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200324
Stefano Babic125f82a2010-08-20 12:05:03 +0200325int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen,
326 const u8 *dout, u8 *din, unsigned long flags)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200327{
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200328 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
Stefano Babic125f82a2010-08-20 12:05:03 +0200329 int nbytes = (bitlen + 7) / 8;
330 u32 data, cnt, i;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200331
Stefano Babic125f82a2010-08-20 12:05:03 +0200332 debug("%s: bitlen %d dout 0x%x din 0x%x\n",
333 __func__, bitlen, (u32)dout, (u32)din);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200334
Stefano Babic6e6f4552010-04-04 22:43:38 +0200335 mxcs->ctrl_reg = (mxcs->ctrl_reg &
336 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100337 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200338
Stefano Babic6e6f4552010-04-04 22:43:38 +0200339 reg_write(mxcs->base + MXC_CSPICTRL, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
340#ifdef CONFIG_MX51
341 reg_write(mxcs->base + MXC_CSPICON, mxcs->cfg_reg);
342#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200343
Stefano Babic6e6f4552010-04-04 22:43:38 +0200344 /* Clear interrupt register */
345 reg_write(mxcs->base + MXC_CSPISTAT,
346 MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100347
Stefano Babic125f82a2010-08-20 12:05:03 +0200348 /*
349 * The SPI controller works only with words,
350 * check if less than a word is sent.
351 * Access to the FIFO is only 32 bit
352 */
353 if (bitlen % 32) {
354 data = 0;
355 cnt = (bitlen % 32) / 8;
356 if (dout) {
357 for (i = 0; i < cnt; i++) {
358 data = (data << 8) | (*dout++ & 0xFF);
359 }
360 }
361 debug("Sending SPI 0x%x\n", data);
362
363 reg_write(mxcs->base + MXC_CSPITXDATA, data);
364 nbytes -= cnt;
365 }
366
367 data = 0;
368
369 while (nbytes > 0) {
370 data = 0;
371 if (dout) {
372 /* Buffer is not 32-bit aligned */
373 if ((unsigned long)dout & 0x03) {
374 data = 0;
375 for (i = 0; i < 4; i++, data <<= 8) {
376 data = (data << 8) | (*dout++ & 0xFF);
377 }
378 } else {
379 data = *(u32 *)dout;
380 data = cpu_to_be32(data);
381 }
382 dout += 4;
383 }
384 debug("Sending SPI 0x%x\n", data);
385 reg_write(mxcs->base + MXC_CSPITXDATA, data);
386 nbytes -= 4;
387 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200388
Stefano Babic6e6f4552010-04-04 22:43:38 +0200389 /* FIFO is written, now starts the transfer setting the XCH bit */
390 reg_write(mxcs->base + MXC_CSPICTRL, mxcs->ctrl_reg |
391 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200392
Stefano Babic6e6f4552010-04-04 22:43:38 +0200393 /* Wait until the TC (Transfer completed) bit is set */
394 while ((reg_read(mxcs->base + MXC_CSPISTAT) & MXC_CSPICTRL_TC) == 0)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200395 ;
396
Stefano Babic6e6f4552010-04-04 22:43:38 +0200397 /* Transfer completed, clear any pending request */
398 reg_write(mxcs->base + MXC_CSPISTAT,
399 MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
400
Stefano Babic125f82a2010-08-20 12:05:03 +0200401 nbytes = (bitlen + 7) / 8;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200402
Stefano Babic125f82a2010-08-20 12:05:03 +0200403 cnt = nbytes % 32;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100404
Stefano Babic125f82a2010-08-20 12:05:03 +0200405 if (bitlen % 32) {
406 data = reg_read(mxcs->base + MXC_CSPIRXDATA);
407 cnt = (bitlen % 32) / 8;
408 debug("SPI Rx unaligned: 0x%x\n", data);
409 if (din) {
410 for (i = 0; i < cnt; i++, data >>= 8) {
411 *din++ = data & 0xFF;
412 }
413 }
414 nbytes -= cnt;
415 }
416
417 while (nbytes > 0) {
418 u32 tmp;
419 tmp = reg_read(mxcs->base + MXC_CSPIRXDATA);
420 data = cpu_to_be32(tmp);
421 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
422 cnt = min(nbytes, sizeof(data));
423 if (din) {
424 memcpy(din, &data, cnt);
425 din += cnt;
426 }
427 nbytes -= cnt;
428 }
429
430 return 0;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200431
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200432}
433
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200434int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
435 void *din, unsigned long flags)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200436{
Stefano Babic125f82a2010-08-20 12:05:03 +0200437 int n_bytes = (bitlen + 7) / 8;
438 int n_bits;
439 int ret;
440 u32 blk_size;
441 u8 *p_outbuf = (u8 *)dout;
442 u8 *p_inbuf = (u8 *)din;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200443
Stefano Babic125f82a2010-08-20 12:05:03 +0200444 if (!slave)
445 return -1;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200446
Stefano Babic125f82a2010-08-20 12:05:03 +0200447 if (flags & SPI_XFER_BEGIN)
448 spi_cs_activate(slave);
Magnus Lilja1858a9a2010-02-09 22:05:39 +0100449
Stefano Babic125f82a2010-08-20 12:05:03 +0200450 while (n_bytes > 0) {
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100451
Stefano Babic125f82a2010-08-20 12:05:03 +0200452 if (n_bytes < MAX_SPI_BYTES)
453 blk_size = n_bytes;
454 else
455 blk_size = MAX_SPI_BYTES;
456
457 n_bits = blk_size * 8;
458
459 ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0);
460
461 if (ret)
462 return ret;
463 if (dout)
464 p_outbuf += blk_size;
465 if (din)
466 p_inbuf += blk_size;
467 n_bytes -= blk_size;
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100468 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200469
Stefano Babic125f82a2010-08-20 12:05:03 +0200470 if (flags & SPI_XFER_END) {
471 spi_cs_deactivate(slave);
472 }
473
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200474 return 0;
475}
476
477void spi_init(void)
478{
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100479}
480
481static int decode_cs(struct mxc_spi_slave *mxcs, unsigned int cs)
482{
483 int ret;
484
485 /*
486 * Some SPI devices require active chip-select over multiple
487 * transactions, we achieve this using a GPIO. Still, the SPI
488 * controller has to be configured to use one of its own chipselects.
489 * To use this feature you have to call spi_setup_slave() with
490 * cs = internal_cs | (gpio << 8), and you have to use some unused
491 * on this SPI controller cs between 0 and 3.
492 */
493 if (cs > 3) {
494 mxcs->gpio = cs >> 8;
495 cs &= 3;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200496 ret = mxc_gpio_direction(mxcs->gpio, OUT);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100497 if (ret) {
498 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
499 return -EINVAL;
500 }
501 } else {
502 mxcs->gpio = -1;
503 }
504
505 return cs;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200506}
507
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200508struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
509 unsigned int max_hz, unsigned int mode)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200510{
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200511 struct mxc_spi_slave *mxcs;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100512 int ret;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200513
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100514 if (bus >= ARRAY_SIZE(spi_bases))
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200515 return NULL;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200516
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100517 mxcs = malloc(sizeof(struct mxc_spi_slave));
Stefano Babic125f82a2010-08-20 12:05:03 +0200518 if (!mxcs) {
519 puts("mxc_spi: SPI Slave not allocated !\n");
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100520 return NULL;
Stefano Babic125f82a2010-08-20 12:05:03 +0200521 }
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100522
523 ret = decode_cs(mxcs, cs);
524 if (ret < 0) {
525 free(mxcs);
526 return NULL;
527 }
528
529 cs = ret;
530
Stefano Babic6e6f4552010-04-04 22:43:38 +0200531 mxcs->slave.bus = bus;
532 mxcs->slave.cs = cs;
533 mxcs->base = spi_bases[bus];
Stefano Babicd77fe992010-07-06 17:05:06 +0200534 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200535
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000536 ret = spi_cfg_mxc(mxcs, cs, max_hz, mode);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200537 if (ret) {
538 printf("mxc_spi: cannot setup SPI controller\n");
539 free(mxcs);
540 return NULL;
541 }
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200542 return &mxcs->slave;
543}
544
545void spi_free_slave(struct spi_slave *slave)
546{
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100547 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
548
549 free(mxcs);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200550}
551
552int spi_claim_bus(struct spi_slave *slave)
553{
554 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
555
556 reg_write(mxcs->base + MXC_CSPIRESET, 1);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200557 udelay(1);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200558 reg_write(mxcs->base + MXC_CSPICTRL, mxcs->ctrl_reg);
559 reg_write(mxcs->base + MXC_CSPIPERIOD,
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200560 MXC_CSPIPERIOD_32KHZ);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200561 reg_write(mxcs->base + MXC_CSPIINT, 0);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200562
563 return 0;
564}
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200565
566void spi_release_bus(struct spi_slave *slave)
567{
568 /* TODO: Shut the controller down */
569}