blob: 95dd03f7b3ff5ab43d33a37a2a8c1f495e2ae82a [file] [log] [blame]
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +02001/*
2 * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de>
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +02005 */
6
7#include <common.h>
Haavard Skinnemoend255bb02008-05-16 11:10:31 +02008#include <malloc.h>
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +02009#include <spi.h>
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +010010#include <asm/errno.h>
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020011#include <asm/io.h>
Stefano Babicd8e0ca82011-08-21 10:45:44 +020012#include <asm/gpio.h>
Stefano Babic86271112011-03-14 15:43:56 +010013#include <asm/arch/imx-regs.h>
14#include <asm/arch/clock.h>
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020015
16#ifdef CONFIG_MX27
17/* i.MX27 has a completely wrong register layout and register definitions in the
18 * datasheet, the correct one is in the Freescale's Linux driver */
19
Helmut Raiger61a58a12011-06-15 01:45:45 +000020#error "i.MX27 CSPI not supported due to drastic differences in register definitions" \
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020021"See linux mxc_spi driver from Freescale for details."
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020022#endif
23
Eric Nelson08c61a52012-01-31 07:52:03 +000024static unsigned long spi_bases[] = {
25 MXC_SPI_BASE_ADDRESSES
26};
27
Stefano Babicc4ea1422010-07-06 17:05:06 +020028#define OUT MXC_GPIO_DIRECTION_OUT
29
Stefano Babicac87c172011-01-19 22:46:33 +000030#define reg_read readl
31#define reg_write(a, v) writel(v, a)
32
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020033struct mxc_spi_slave {
34 struct spi_slave slave;
35 unsigned long base;
36 u32 ctrl_reg;
Eric Nelson08c61a52012-01-31 07:52:03 +000037#if defined(MXC_ECSPI)
Stefano Babicd205ddc2010-04-04 22:43:38 +020038 u32 cfg_reg;
39#endif
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +010040 int gpio;
Stefano Babicc4ea1422010-07-06 17:05:06 +020041 int ss_pol;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020042};
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020043
44static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
45{
46 return container_of(slave, struct mxc_spi_slave, slave);
47}
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020048
Stefano Babicd205ddc2010-04-04 22:43:38 +020049void spi_cs_activate(struct spi_slave *slave)
50{
51 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
52 if (mxcs->gpio > 0)
Stefano Babicd8e0ca82011-08-21 10:45:44 +020053 gpio_set_value(mxcs->gpio, mxcs->ss_pol);
Stefano Babicd205ddc2010-04-04 22:43:38 +020054}
55
56void spi_cs_deactivate(struct spi_slave *slave)
57{
58 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
59 if (mxcs->gpio > 0)
Stefano Babicd8e0ca82011-08-21 10:45:44 +020060 gpio_set_value(mxcs->gpio,
Stefano Babicc4ea1422010-07-06 17:05:06 +020061 !(mxcs->ss_pol));
Stefano Babicd205ddc2010-04-04 22:43:38 +020062}
63
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +000064u32 get_cspi_div(u32 div)
65{
66 int i;
67
68 for (i = 0; i < 8; i++) {
69 if (div <= (4 << i))
70 return i;
71 }
72 return i;
73}
74
Eric Nelson08c61a52012-01-31 07:52:03 +000075#ifdef MXC_CSPI
Stefano Babicc9d59c72011-01-19 22:46:30 +000076static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
77 unsigned int max_hz, unsigned int mode)
78{
79 unsigned int ctrl_reg;
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +000080 u32 clk_src;
81 u32 div;
82
83 clk_src = mxc_get_clock(MXC_CSPI_CLK);
84
Benoît Thébaudeaucd200402012-08-10 08:51:50 +000085 div = DIV_ROUND_UP(clk_src, max_hz);
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +000086 div = get_cspi_div(div);
87
88 debug("clk %d Hz, div %d, real clk %d Hz\n",
89 max_hz, div, clk_src / (4 << div));
Stefano Babicc9d59c72011-01-19 22:46:30 +000090
91 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
92 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +000093 MXC_CSPICTRL_DATARATE(div) |
Stefano Babicc9d59c72011-01-19 22:46:30 +000094 MXC_CSPICTRL_EN |
95#ifdef CONFIG_MX35
96 MXC_CSPICTRL_SSCTL |
97#endif
98 MXC_CSPICTRL_MODE;
99
100 if (mode & SPI_CPHA)
101 ctrl_reg |= MXC_CSPICTRL_PHA;
102 if (mode & SPI_CPOL)
103 ctrl_reg |= MXC_CSPICTRL_POL;
104 if (mode & SPI_CS_HIGH)
105 ctrl_reg |= MXC_CSPICTRL_SSPOL;
106 mxcs->ctrl_reg = ctrl_reg;
107
108 return 0;
109}
110#endif
111
Eric Nelson08c61a52012-01-31 07:52:03 +0000112#ifdef MXC_ECSPI
Stefano Babicc9d59c72011-01-19 22:46:30 +0000113static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
Stefano Babicd205ddc2010-04-04 22:43:38 +0200114 unsigned int max_hz, unsigned int mode)
115{
116 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
Dirk Behme9a309032013-05-11 07:25:54 +0200117 s32 reg_ctrl, reg_config;
118 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, pre_div = 0, post_div = 0;
Stefano Babicac87c172011-01-19 22:46:33 +0000119 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200120
121 if (max_hz == 0) {
122 printf("Error: desired clock is 0\n");
123 return -1;
124 }
125
Fabio Estevam0f1411b2013-04-09 13:06:25 +0000126 /*
127 * Reset SPI and set all CSs to master mode, if toggling
128 * between slave and master mode we might see a glitch
129 * on the clock line
130 */
131 reg_ctrl = MXC_CSPICTRL_MODE_MASK;
132 reg_write(&regs->ctrl, reg_ctrl);
133 reg_ctrl |= MXC_CSPICTRL_EN;
134 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200135
Stefano Babicd205ddc2010-04-04 22:43:38 +0200136 if (clk_src > max_hz) {
Dirk Behme9a309032013-05-11 07:25:54 +0200137 pre_div = (clk_src - 1) / max_hz;
138 /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
139 post_div = fls(pre_div);
140 if (post_div > 4) {
141 post_div -= 4;
142 if (post_div >= 16) {
Stefano Babicd205ddc2010-04-04 22:43:38 +0200143 printf("Error: no divider for the freq: %d\n",
144 max_hz);
145 return -1;
146 }
Dirk Behme9a309032013-05-11 07:25:54 +0200147 pre_div >>= post_div;
148 } else {
149 post_div = 0;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200150 }
151 }
152
153 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
154 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
155 MXC_CSPICTRL_SELCHAN(cs);
156 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
157 MXC_CSPICTRL_PREDIV(pre_div);
158 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
159 MXC_CSPICTRL_POSTDIV(post_div);
160
Stefano Babicd205ddc2010-04-04 22:43:38 +0200161 /* We need to disable SPI before changing registers */
162 reg_ctrl &= ~MXC_CSPICTRL_EN;
163
164 if (mode & SPI_CS_HIGH)
165 ss_pol = 1;
166
Stefano Babic9f481e92010-08-23 20:41:19 +0200167 if (mode & SPI_CPOL)
Stefano Babicd205ddc2010-04-04 22:43:38 +0200168 sclkpol = 1;
169
170 if (mode & SPI_CPHA)
171 sclkpha = 1;
172
Stefano Babicac87c172011-01-19 22:46:33 +0000173 reg_config = reg_read(&regs->cfg);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200174
175 /*
176 * Configuration register setup
Stefano Babicc9d59c72011-01-19 22:46:30 +0000177 * The MX51 supports different setup for each SS
Stefano Babicd205ddc2010-04-04 22:43:38 +0200178 */
179 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
180 (ss_pol << (cs + MXC_CSPICON_SSPOL));
181 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
182 (sclkpol << (cs + MXC_CSPICON_POL));
183 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
184 (sclkpha << (cs + MXC_CSPICON_PHA));
185
186 debug("reg_ctrl = 0x%x\n", reg_ctrl);
Stefano Babicac87c172011-01-19 22:46:33 +0000187 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200188 debug("reg_config = 0x%x\n", reg_config);
Stefano Babicac87c172011-01-19 22:46:33 +0000189 reg_write(&regs->cfg, reg_config);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200190
191 /* save config register and control register */
192 mxcs->ctrl_reg = reg_ctrl;
193 mxcs->cfg_reg = reg_config;
194
195 /* clear interrupt reg */
Stefano Babicac87c172011-01-19 22:46:33 +0000196 reg_write(&regs->intr, 0);
197 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200198
199 return 0;
200}
201#endif
202
Stefano Babic2f721d12010-08-20 12:05:03 +0200203int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen,
204 const u8 *dout, u8 *din, unsigned long flags)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200205{
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200206 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
Axel Lin9675fed2013-06-14 21:13:32 +0800207 int nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200208 u32 data, cnt, i;
Stefano Babicac87c172011-01-19 22:46:33 +0000209 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200210
Stefano Babic2f721d12010-08-20 12:05:03 +0200211 debug("%s: bitlen %d dout 0x%x din 0x%x\n",
212 __func__, bitlen, (u32)dout, (u32)din);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200213
214 mxcs->ctrl_reg = (mxcs->ctrl_reg &
215 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100216 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
217
Stefano Babicac87c172011-01-19 22:46:33 +0000218 reg_write(&regs->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
Eric Nelson08c61a52012-01-31 07:52:03 +0000219#ifdef MXC_ECSPI
Stefano Babicac87c172011-01-19 22:46:33 +0000220 reg_write(&regs->cfg, mxcs->cfg_reg);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200221#endif
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200222
Stefano Babicd205ddc2010-04-04 22:43:38 +0200223 /* Clear interrupt register */
Stefano Babicac87c172011-01-19 22:46:33 +0000224 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100225
Stefano Babic2f721d12010-08-20 12:05:03 +0200226 /*
227 * The SPI controller works only with words,
228 * check if less than a word is sent.
229 * Access to the FIFO is only 32 bit
230 */
231 if (bitlen % 32) {
232 data = 0;
233 cnt = (bitlen % 32) / 8;
234 if (dout) {
235 for (i = 0; i < cnt; i++) {
236 data = (data << 8) | (*dout++ & 0xFF);
237 }
238 }
239 debug("Sending SPI 0x%x\n", data);
240
Stefano Babicac87c172011-01-19 22:46:33 +0000241 reg_write(&regs->txdata, data);
Stefano Babic2f721d12010-08-20 12:05:03 +0200242 nbytes -= cnt;
243 }
244
245 data = 0;
246
247 while (nbytes > 0) {
248 data = 0;
249 if (dout) {
250 /* Buffer is not 32-bit aligned */
251 if ((unsigned long)dout & 0x03) {
252 data = 0;
Anatolij Gustschindff01092011-01-20 07:53:06 +0000253 for (i = 0; i < 4; i++)
Stefano Babic2f721d12010-08-20 12:05:03 +0200254 data = (data << 8) | (*dout++ & 0xFF);
Stefano Babic2f721d12010-08-20 12:05:03 +0200255 } else {
256 data = *(u32 *)dout;
257 data = cpu_to_be32(data);
Timo Herbrecher6d5ce1b2013-10-16 00:05:09 +0530258 dout += 4;
Stefano Babic2f721d12010-08-20 12:05:03 +0200259 }
Stefano Babic2f721d12010-08-20 12:05:03 +0200260 }
261 debug("Sending SPI 0x%x\n", data);
Stefano Babicac87c172011-01-19 22:46:33 +0000262 reg_write(&regs->txdata, data);
Stefano Babic2f721d12010-08-20 12:05:03 +0200263 nbytes -= 4;
264 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200265
Stefano Babicd205ddc2010-04-04 22:43:38 +0200266 /* FIFO is written, now starts the transfer setting the XCH bit */
Stefano Babicac87c172011-01-19 22:46:33 +0000267 reg_write(&regs->ctrl, mxcs->ctrl_reg |
Stefano Babicd205ddc2010-04-04 22:43:38 +0200268 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200269
Stefano Babicd205ddc2010-04-04 22:43:38 +0200270 /* Wait until the TC (Transfer completed) bit is set */
Stefano Babicac87c172011-01-19 22:46:33 +0000271 while ((reg_read(&regs->stat) & MXC_CSPICTRL_TC) == 0)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200272 ;
273
Stefano Babicd205ddc2010-04-04 22:43:38 +0200274 /* Transfer completed, clear any pending request */
Stefano Babicac87c172011-01-19 22:46:33 +0000275 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100276
Axel Lin9675fed2013-06-14 21:13:32 +0800277 nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200278
Stefano Babic2f721d12010-08-20 12:05:03 +0200279 cnt = nbytes % 32;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200280
Stefano Babic2f721d12010-08-20 12:05:03 +0200281 if (bitlen % 32) {
Stefano Babicac87c172011-01-19 22:46:33 +0000282 data = reg_read(&regs->rxdata);
Stefano Babic2f721d12010-08-20 12:05:03 +0200283 cnt = (bitlen % 32) / 8;
Anatolij Gustschindff01092011-01-20 07:53:06 +0000284 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200285 debug("SPI Rx unaligned: 0x%x\n", data);
286 if (din) {
Anatolij Gustschindff01092011-01-20 07:53:06 +0000287 memcpy(din, &data, cnt);
288 din += cnt;
Stefano Babic2f721d12010-08-20 12:05:03 +0200289 }
290 nbytes -= cnt;
291 }
292
293 while (nbytes > 0) {
294 u32 tmp;
Stefano Babicac87c172011-01-19 22:46:33 +0000295 tmp = reg_read(&regs->rxdata);
Stefano Babic2f721d12010-08-20 12:05:03 +0200296 data = cpu_to_be32(tmp);
297 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
298 cnt = min(nbytes, sizeof(data));
299 if (din) {
300 memcpy(din, &data, cnt);
301 din += cnt;
302 }
303 nbytes -= cnt;
304 }
305
306 return 0;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200307
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200308}
309
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200310int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
311 void *din, unsigned long flags)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200312{
Axel Lin9675fed2013-06-14 21:13:32 +0800313 int n_bytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200314 int n_bits;
315 int ret;
316 u32 blk_size;
317 u8 *p_outbuf = (u8 *)dout;
318 u8 *p_inbuf = (u8 *)din;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200319
Stefano Babic2f721d12010-08-20 12:05:03 +0200320 if (!slave)
321 return -1;
322
323 if (flags & SPI_XFER_BEGIN)
324 spi_cs_activate(slave);
325
326 while (n_bytes > 0) {
Stefano Babic2f721d12010-08-20 12:05:03 +0200327 if (n_bytes < MAX_SPI_BYTES)
328 blk_size = n_bytes;
329 else
330 blk_size = MAX_SPI_BYTES;
331
332 n_bits = blk_size * 8;
333
334 ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0);
335
336 if (ret)
337 return ret;
338 if (dout)
339 p_outbuf += blk_size;
340 if (din)
341 p_inbuf += blk_size;
342 n_bytes -= blk_size;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200343 }
344
Stefano Babic2f721d12010-08-20 12:05:03 +0200345 if (flags & SPI_XFER_END) {
346 spi_cs_deactivate(slave);
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100347 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200348
349 return 0;
350}
351
352void spi_init(void)
353{
354}
355
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100356static int decode_cs(struct mxc_spi_slave *mxcs, unsigned int cs)
357{
358 int ret;
359
360 /*
361 * Some SPI devices require active chip-select over multiple
362 * transactions, we achieve this using a GPIO. Still, the SPI
363 * controller has to be configured to use one of its own chipselects.
364 * To use this feature you have to call spi_setup_slave() with
365 * cs = internal_cs | (gpio << 8), and you have to use some unused
366 * on this SPI controller cs between 0 and 3.
367 */
368 if (cs > 3) {
369 mxcs->gpio = cs >> 8;
370 cs &= 3;
Fabio Estevamde5bf022012-11-15 11:23:23 +0000371 ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100372 if (ret) {
373 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
374 return -EINVAL;
375 }
376 } else {
377 mxcs->gpio = -1;
378 }
379
380 return cs;
381}
382
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200383struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
384 unsigned int max_hz, unsigned int mode)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200385{
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200386 struct mxc_spi_slave *mxcs;
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100387 int ret;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200388
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100389 if (bus >= ARRAY_SIZE(spi_bases))
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200390 return NULL;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200391
Simon Glassd3504fe2013-03-18 19:23:40 +0000392 mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
Stefano Babic2f721d12010-08-20 12:05:03 +0200393 if (!mxcs) {
394 puts("mxc_spi: SPI Slave not allocated !\n");
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100395 return NULL;
Stefano Babic2f721d12010-08-20 12:05:03 +0200396 }
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100397
Fabio Estevamde5bf022012-11-15 11:23:23 +0000398 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
399
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100400 ret = decode_cs(mxcs, cs);
401 if (ret < 0) {
402 free(mxcs);
403 return NULL;
404 }
405
406 cs = ret;
407
Stefano Babicd205ddc2010-04-04 22:43:38 +0200408 mxcs->base = spi_bases[bus];
409
Stefano Babicc9d59c72011-01-19 22:46:30 +0000410 ret = spi_cfg_mxc(mxcs, cs, max_hz, mode);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200411 if (ret) {
412 printf("mxc_spi: cannot setup SPI controller\n");
413 free(mxcs);
414 return NULL;
415 }
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200416 return &mxcs->slave;
417}
418
419void spi_free_slave(struct spi_slave *slave)
420{
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100421 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
422
423 free(mxcs);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200424}
425
426int spi_claim_bus(struct spi_slave *slave)
427{
428 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
Stefano Babicac87c172011-01-19 22:46:33 +0000429 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200430
Stefano Babicac87c172011-01-19 22:46:33 +0000431 reg_write(&regs->rxdata, 1);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200432 udelay(1);
Stefano Babicac87c172011-01-19 22:46:33 +0000433 reg_write(&regs->ctrl, mxcs->ctrl_reg);
434 reg_write(&regs->period, MXC_CSPIPERIOD_32KHZ);
435 reg_write(&regs->intr, 0);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200436
437 return 0;
438}
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200439
440void spi_release_bus(struct spi_slave *slave)
441{
442 /* TODO: Shut the controller down */
443}