blob: f3f029d634abd1f2045c20ba3e159bc1aecf7e4a [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;
Markus Niebel5d584cc2014-02-17 17:33:17 +0100118 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0;
119 u32 pre_div = 0, post_div = 0;
Stefano Babicac87c172011-01-19 22:46:33 +0000120 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200121
122 if (max_hz == 0) {
123 printf("Error: desired clock is 0\n");
124 return -1;
125 }
126
Fabio Estevam0f1411b2013-04-09 13:06:25 +0000127 /*
128 * Reset SPI and set all CSs to master mode, if toggling
129 * between slave and master mode we might see a glitch
130 * on the clock line
131 */
132 reg_ctrl = MXC_CSPICTRL_MODE_MASK;
133 reg_write(&regs->ctrl, reg_ctrl);
134 reg_ctrl |= MXC_CSPICTRL_EN;
135 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200136
Stefano Babicd205ddc2010-04-04 22:43:38 +0200137 if (clk_src > max_hz) {
Dirk Behme9a309032013-05-11 07:25:54 +0200138 pre_div = (clk_src - 1) / max_hz;
139 /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
140 post_div = fls(pre_div);
141 if (post_div > 4) {
142 post_div -= 4;
143 if (post_div >= 16) {
Stefano Babicd205ddc2010-04-04 22:43:38 +0200144 printf("Error: no divider for the freq: %d\n",
145 max_hz);
146 return -1;
147 }
Dirk Behme9a309032013-05-11 07:25:54 +0200148 pre_div >>= post_div;
149 } else {
150 post_div = 0;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200151 }
152 }
153
154 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
155 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
156 MXC_CSPICTRL_SELCHAN(cs);
157 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
158 MXC_CSPICTRL_PREDIV(pre_div);
159 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
160 MXC_CSPICTRL_POSTDIV(post_div);
161
Stefano Babicd205ddc2010-04-04 22:43:38 +0200162 /* We need to disable SPI before changing registers */
163 reg_ctrl &= ~MXC_CSPICTRL_EN;
164
165 if (mode & SPI_CS_HIGH)
166 ss_pol = 1;
167
Markus Niebel5d584cc2014-02-17 17:33:17 +0100168 if (mode & SPI_CPOL) {
Stefano Babicd205ddc2010-04-04 22:43:38 +0200169 sclkpol = 1;
Markus Niebel5d584cc2014-02-17 17:33:17 +0100170 sclkctl = 1;
171 }
Stefano Babicd205ddc2010-04-04 22:43:38 +0200172
173 if (mode & SPI_CPHA)
174 sclkpha = 1;
175
Stefano Babicac87c172011-01-19 22:46:33 +0000176 reg_config = reg_read(&regs->cfg);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200177
178 /*
179 * Configuration register setup
Stefano Babicc9d59c72011-01-19 22:46:30 +0000180 * The MX51 supports different setup for each SS
Stefano Babicd205ddc2010-04-04 22:43:38 +0200181 */
182 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
183 (ss_pol << (cs + MXC_CSPICON_SSPOL));
184 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
185 (sclkpol << (cs + MXC_CSPICON_POL));
Markus Niebel5d584cc2014-02-17 17:33:17 +0100186 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_CTL))) |
187 (sclkctl << (cs + MXC_CSPICON_CTL));
Stefano Babicd205ddc2010-04-04 22:43:38 +0200188 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
189 (sclkpha << (cs + MXC_CSPICON_PHA));
190
191 debug("reg_ctrl = 0x%x\n", reg_ctrl);
Stefano Babicac87c172011-01-19 22:46:33 +0000192 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200193 debug("reg_config = 0x%x\n", reg_config);
Stefano Babicac87c172011-01-19 22:46:33 +0000194 reg_write(&regs->cfg, reg_config);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200195
196 /* save config register and control register */
197 mxcs->ctrl_reg = reg_ctrl;
198 mxcs->cfg_reg = reg_config;
199
200 /* clear interrupt reg */
Stefano Babicac87c172011-01-19 22:46:33 +0000201 reg_write(&regs->intr, 0);
202 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200203
204 return 0;
205}
206#endif
207
Stefano Babic2f721d12010-08-20 12:05:03 +0200208int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen,
209 const u8 *dout, u8 *din, unsigned long flags)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200210{
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200211 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
Axel Lin9675fed2013-06-14 21:13:32 +0800212 int nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200213 u32 data, cnt, i;
Stefano Babicac87c172011-01-19 22:46:33 +0000214 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200215
Stefano Babic2f721d12010-08-20 12:05:03 +0200216 debug("%s: bitlen %d dout 0x%x din 0x%x\n",
217 __func__, bitlen, (u32)dout, (u32)din);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200218
219 mxcs->ctrl_reg = (mxcs->ctrl_reg &
220 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100221 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
222
Stefano Babicac87c172011-01-19 22:46:33 +0000223 reg_write(&regs->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
Eric Nelson08c61a52012-01-31 07:52:03 +0000224#ifdef MXC_ECSPI
Stefano Babicac87c172011-01-19 22:46:33 +0000225 reg_write(&regs->cfg, mxcs->cfg_reg);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200226#endif
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200227
Stefano Babicd205ddc2010-04-04 22:43:38 +0200228 /* Clear interrupt register */
Stefano Babicac87c172011-01-19 22:46:33 +0000229 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100230
Stefano Babic2f721d12010-08-20 12:05:03 +0200231 /*
232 * The SPI controller works only with words,
233 * check if less than a word is sent.
234 * Access to the FIFO is only 32 bit
235 */
236 if (bitlen % 32) {
237 data = 0;
238 cnt = (bitlen % 32) / 8;
239 if (dout) {
240 for (i = 0; i < cnt; i++) {
241 data = (data << 8) | (*dout++ & 0xFF);
242 }
243 }
244 debug("Sending SPI 0x%x\n", data);
245
Stefano Babicac87c172011-01-19 22:46:33 +0000246 reg_write(&regs->txdata, data);
Stefano Babic2f721d12010-08-20 12:05:03 +0200247 nbytes -= cnt;
248 }
249
250 data = 0;
251
252 while (nbytes > 0) {
253 data = 0;
254 if (dout) {
255 /* Buffer is not 32-bit aligned */
256 if ((unsigned long)dout & 0x03) {
257 data = 0;
Anatolij Gustschindff01092011-01-20 07:53:06 +0000258 for (i = 0; i < 4; i++)
Stefano Babic2f721d12010-08-20 12:05:03 +0200259 data = (data << 8) | (*dout++ & 0xFF);
Stefano Babic2f721d12010-08-20 12:05:03 +0200260 } else {
261 data = *(u32 *)dout;
262 data = cpu_to_be32(data);
Timo Herbrecher6d5ce1b2013-10-16 00:05:09 +0530263 dout += 4;
Stefano Babic2f721d12010-08-20 12:05:03 +0200264 }
Stefano Babic2f721d12010-08-20 12:05:03 +0200265 }
266 debug("Sending SPI 0x%x\n", data);
Stefano Babicac87c172011-01-19 22:46:33 +0000267 reg_write(&regs->txdata, data);
Stefano Babic2f721d12010-08-20 12:05:03 +0200268 nbytes -= 4;
269 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200270
Stefano Babicd205ddc2010-04-04 22:43:38 +0200271 /* FIFO is written, now starts the transfer setting the XCH bit */
Stefano Babicac87c172011-01-19 22:46:33 +0000272 reg_write(&regs->ctrl, mxcs->ctrl_reg |
Stefano Babicd205ddc2010-04-04 22:43:38 +0200273 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200274
Stefano Babicd205ddc2010-04-04 22:43:38 +0200275 /* Wait until the TC (Transfer completed) bit is set */
Stefano Babicac87c172011-01-19 22:46:33 +0000276 while ((reg_read(&regs->stat) & MXC_CSPICTRL_TC) == 0)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200277 ;
278
Stefano Babicd205ddc2010-04-04 22:43:38 +0200279 /* Transfer completed, clear any pending request */
Stefano Babicac87c172011-01-19 22:46:33 +0000280 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100281
Axel Lin9675fed2013-06-14 21:13:32 +0800282 nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200283
Stefano Babic2f721d12010-08-20 12:05:03 +0200284 cnt = nbytes % 32;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200285
Stefano Babic2f721d12010-08-20 12:05:03 +0200286 if (bitlen % 32) {
Stefano Babicac87c172011-01-19 22:46:33 +0000287 data = reg_read(&regs->rxdata);
Stefano Babic2f721d12010-08-20 12:05:03 +0200288 cnt = (bitlen % 32) / 8;
Anatolij Gustschindff01092011-01-20 07:53:06 +0000289 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200290 debug("SPI Rx unaligned: 0x%x\n", data);
291 if (din) {
Anatolij Gustschindff01092011-01-20 07:53:06 +0000292 memcpy(din, &data, cnt);
293 din += cnt;
Stefano Babic2f721d12010-08-20 12:05:03 +0200294 }
295 nbytes -= cnt;
296 }
297
298 while (nbytes > 0) {
299 u32 tmp;
Stefano Babicac87c172011-01-19 22:46:33 +0000300 tmp = reg_read(&regs->rxdata);
Stefano Babic2f721d12010-08-20 12:05:03 +0200301 data = cpu_to_be32(tmp);
302 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
303 cnt = min(nbytes, sizeof(data));
304 if (din) {
305 memcpy(din, &data, cnt);
306 din += cnt;
307 }
308 nbytes -= cnt;
309 }
310
311 return 0;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200312
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200313}
314
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200315int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
316 void *din, unsigned long flags)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200317{
Axel Lin9675fed2013-06-14 21:13:32 +0800318 int n_bytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200319 int n_bits;
320 int ret;
321 u32 blk_size;
322 u8 *p_outbuf = (u8 *)dout;
323 u8 *p_inbuf = (u8 *)din;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200324
Stefano Babic2f721d12010-08-20 12:05:03 +0200325 if (!slave)
326 return -1;
327
328 if (flags & SPI_XFER_BEGIN)
329 spi_cs_activate(slave);
330
331 while (n_bytes > 0) {
Stefano Babic2f721d12010-08-20 12:05:03 +0200332 if (n_bytes < MAX_SPI_BYTES)
333 blk_size = n_bytes;
334 else
335 blk_size = MAX_SPI_BYTES;
336
337 n_bits = blk_size * 8;
338
339 ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0);
340
341 if (ret)
342 return ret;
343 if (dout)
344 p_outbuf += blk_size;
345 if (din)
346 p_inbuf += blk_size;
347 n_bytes -= blk_size;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200348 }
349
Stefano Babic2f721d12010-08-20 12:05:03 +0200350 if (flags & SPI_XFER_END) {
351 spi_cs_deactivate(slave);
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100352 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200353
354 return 0;
355}
356
357void spi_init(void)
358{
359}
360
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100361static int decode_cs(struct mxc_spi_slave *mxcs, unsigned int cs)
362{
363 int ret;
364
365 /*
366 * Some SPI devices require active chip-select over multiple
367 * transactions, we achieve this using a GPIO. Still, the SPI
368 * controller has to be configured to use one of its own chipselects.
369 * To use this feature you have to call spi_setup_slave() with
370 * cs = internal_cs | (gpio << 8), and you have to use some unused
371 * on this SPI controller cs between 0 and 3.
372 */
373 if (cs > 3) {
374 mxcs->gpio = cs >> 8;
375 cs &= 3;
Fabio Estevamde5bf022012-11-15 11:23:23 +0000376 ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100377 if (ret) {
378 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
379 return -EINVAL;
380 }
381 } else {
382 mxcs->gpio = -1;
383 }
384
385 return cs;
386}
387
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200388struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
389 unsigned int max_hz, unsigned int mode)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200390{
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200391 struct mxc_spi_slave *mxcs;
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100392 int ret;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200393
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100394 if (bus >= ARRAY_SIZE(spi_bases))
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200395 return NULL;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200396
Simon Glassd3504fe2013-03-18 19:23:40 +0000397 mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
Stefano Babic2f721d12010-08-20 12:05:03 +0200398 if (!mxcs) {
399 puts("mxc_spi: SPI Slave not allocated !\n");
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100400 return NULL;
Stefano Babic2f721d12010-08-20 12:05:03 +0200401 }
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100402
Fabio Estevamde5bf022012-11-15 11:23:23 +0000403 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
404
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100405 ret = decode_cs(mxcs, cs);
406 if (ret < 0) {
407 free(mxcs);
408 return NULL;
409 }
410
411 cs = ret;
412
Stefano Babicd205ddc2010-04-04 22:43:38 +0200413 mxcs->base = spi_bases[bus];
414
Stefano Babicc9d59c72011-01-19 22:46:30 +0000415 ret = spi_cfg_mxc(mxcs, cs, max_hz, mode);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200416 if (ret) {
417 printf("mxc_spi: cannot setup SPI controller\n");
418 free(mxcs);
419 return NULL;
420 }
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200421 return &mxcs->slave;
422}
423
424void spi_free_slave(struct spi_slave *slave)
425{
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100426 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
427
428 free(mxcs);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200429}
430
431int spi_claim_bus(struct spi_slave *slave)
432{
433 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
Stefano Babicac87c172011-01-19 22:46:33 +0000434 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200435
Stefano Babicac87c172011-01-19 22:46:33 +0000436 reg_write(&regs->rxdata, 1);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200437 udelay(1);
Stefano Babicac87c172011-01-19 22:46:33 +0000438 reg_write(&regs->ctrl, mxcs->ctrl_reg);
439 reg_write(&regs->period, MXC_CSPIPERIOD_32KHZ);
440 reg_write(&regs->intr, 0);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200441
442 return 0;
443}
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200444
445void spi_release_bus(struct spi_slave *slave)
446{
447 /* TODO: Shut the controller down */
448}