blob: 2d5f3850da8e95b41f7bcbfb227eb9958c8d15c6 [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
Heiko Schocherf659b572014-07-14 10:22:11 +020033#if !defined(CONFIG_SYS_SPI_MXC_WAIT)
34#define CONFIG_SYS_SPI_MXC_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
35#endif
36
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020037struct mxc_spi_slave {
38 struct spi_slave slave;
39 unsigned long base;
40 u32 ctrl_reg;
Eric Nelson08c61a52012-01-31 07:52:03 +000041#if defined(MXC_ECSPI)
Stefano Babicd205ddc2010-04-04 22:43:38 +020042 u32 cfg_reg;
43#endif
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +010044 int gpio;
Stefano Babicc4ea1422010-07-06 17:05:06 +020045 int ss_pol;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020046};
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020047
48static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
49{
50 return container_of(slave, struct mxc_spi_slave, slave);
51}
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020052
Stefano Babicd205ddc2010-04-04 22:43:38 +020053void spi_cs_activate(struct spi_slave *slave)
54{
55 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
56 if (mxcs->gpio > 0)
Stefano Babicd8e0ca82011-08-21 10:45:44 +020057 gpio_set_value(mxcs->gpio, mxcs->ss_pol);
Stefano Babicd205ddc2010-04-04 22:43:38 +020058}
59
60void spi_cs_deactivate(struct spi_slave *slave)
61{
62 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
63 if (mxcs->gpio > 0)
Stefano Babicd8e0ca82011-08-21 10:45:44 +020064 gpio_set_value(mxcs->gpio,
Stefano Babicc4ea1422010-07-06 17:05:06 +020065 !(mxcs->ss_pol));
Stefano Babicd205ddc2010-04-04 22:43:38 +020066}
67
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +000068u32 get_cspi_div(u32 div)
69{
70 int i;
71
72 for (i = 0; i < 8; i++) {
73 if (div <= (4 << i))
74 return i;
75 }
76 return i;
77}
78
Eric Nelson08c61a52012-01-31 07:52:03 +000079#ifdef MXC_CSPI
Stefano Babicc9d59c72011-01-19 22:46:30 +000080static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
81 unsigned int max_hz, unsigned int mode)
82{
83 unsigned int ctrl_reg;
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +000084 u32 clk_src;
85 u32 div;
86
87 clk_src = mxc_get_clock(MXC_CSPI_CLK);
88
Benoît Thébaudeaucd200402012-08-10 08:51:50 +000089 div = DIV_ROUND_UP(clk_src, max_hz);
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +000090 div = get_cspi_div(div);
91
92 debug("clk %d Hz, div %d, real clk %d Hz\n",
93 max_hz, div, clk_src / (4 << div));
Stefano Babicc9d59c72011-01-19 22:46:30 +000094
95 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
96 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +000097 MXC_CSPICTRL_DATARATE(div) |
Stefano Babicc9d59c72011-01-19 22:46:30 +000098 MXC_CSPICTRL_EN |
99#ifdef CONFIG_MX35
100 MXC_CSPICTRL_SSCTL |
101#endif
102 MXC_CSPICTRL_MODE;
103
104 if (mode & SPI_CPHA)
105 ctrl_reg |= MXC_CSPICTRL_PHA;
106 if (mode & SPI_CPOL)
107 ctrl_reg |= MXC_CSPICTRL_POL;
108 if (mode & SPI_CS_HIGH)
109 ctrl_reg |= MXC_CSPICTRL_SSPOL;
110 mxcs->ctrl_reg = ctrl_reg;
111
112 return 0;
113}
114#endif
115
Eric Nelson08c61a52012-01-31 07:52:03 +0000116#ifdef MXC_ECSPI
Stefano Babicc9d59c72011-01-19 22:46:30 +0000117static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
Stefano Babicd205ddc2010-04-04 22:43:38 +0200118 unsigned int max_hz, unsigned int mode)
119{
120 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
Dirk Behme9a309032013-05-11 07:25:54 +0200121 s32 reg_ctrl, reg_config;
Markus Niebel5d584cc2014-02-17 17:33:17 +0100122 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0;
123 u32 pre_div = 0, post_div = 0;
Stefano Babicac87c172011-01-19 22:46:33 +0000124 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200125
126 if (max_hz == 0) {
127 printf("Error: desired clock is 0\n");
128 return -1;
129 }
130
Fabio Estevam0f1411b2013-04-09 13:06:25 +0000131 /*
132 * Reset SPI and set all CSs to master mode, if toggling
133 * between slave and master mode we might see a glitch
134 * on the clock line
135 */
136 reg_ctrl = MXC_CSPICTRL_MODE_MASK;
137 reg_write(&regs->ctrl, reg_ctrl);
138 reg_ctrl |= MXC_CSPICTRL_EN;
139 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200140
Stefano Babicd205ddc2010-04-04 22:43:38 +0200141 if (clk_src > max_hz) {
Dirk Behme9a309032013-05-11 07:25:54 +0200142 pre_div = (clk_src - 1) / max_hz;
143 /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
144 post_div = fls(pre_div);
145 if (post_div > 4) {
146 post_div -= 4;
147 if (post_div >= 16) {
Stefano Babicd205ddc2010-04-04 22:43:38 +0200148 printf("Error: no divider for the freq: %d\n",
149 max_hz);
150 return -1;
151 }
Dirk Behme9a309032013-05-11 07:25:54 +0200152 pre_div >>= post_div;
153 } else {
154 post_div = 0;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200155 }
156 }
157
158 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
159 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
160 MXC_CSPICTRL_SELCHAN(cs);
161 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
162 MXC_CSPICTRL_PREDIV(pre_div);
163 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
164 MXC_CSPICTRL_POSTDIV(post_div);
165
Stefano Babicd205ddc2010-04-04 22:43:38 +0200166 /* We need to disable SPI before changing registers */
167 reg_ctrl &= ~MXC_CSPICTRL_EN;
168
169 if (mode & SPI_CS_HIGH)
170 ss_pol = 1;
171
Markus Niebel5d584cc2014-02-17 17:33:17 +0100172 if (mode & SPI_CPOL) {
Stefano Babicd205ddc2010-04-04 22:43:38 +0200173 sclkpol = 1;
Markus Niebel5d584cc2014-02-17 17:33:17 +0100174 sclkctl = 1;
175 }
Stefano Babicd205ddc2010-04-04 22:43:38 +0200176
177 if (mode & SPI_CPHA)
178 sclkpha = 1;
179
Stefano Babicac87c172011-01-19 22:46:33 +0000180 reg_config = reg_read(&regs->cfg);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200181
182 /*
183 * Configuration register setup
Stefano Babicc9d59c72011-01-19 22:46:30 +0000184 * The MX51 supports different setup for each SS
Stefano Babicd205ddc2010-04-04 22:43:38 +0200185 */
186 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
187 (ss_pol << (cs + MXC_CSPICON_SSPOL));
188 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
189 (sclkpol << (cs + MXC_CSPICON_POL));
Markus Niebel5d584cc2014-02-17 17:33:17 +0100190 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_CTL))) |
191 (sclkctl << (cs + MXC_CSPICON_CTL));
Stefano Babicd205ddc2010-04-04 22:43:38 +0200192 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
193 (sclkpha << (cs + MXC_CSPICON_PHA));
194
195 debug("reg_ctrl = 0x%x\n", reg_ctrl);
Stefano Babicac87c172011-01-19 22:46:33 +0000196 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200197 debug("reg_config = 0x%x\n", reg_config);
Stefano Babicac87c172011-01-19 22:46:33 +0000198 reg_write(&regs->cfg, reg_config);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200199
200 /* save config register and control register */
201 mxcs->ctrl_reg = reg_ctrl;
202 mxcs->cfg_reg = reg_config;
203
204 /* clear interrupt reg */
Stefano Babicac87c172011-01-19 22:46:33 +0000205 reg_write(&regs->intr, 0);
206 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200207
208 return 0;
209}
210#endif
211
Stefano Babic2f721d12010-08-20 12:05:03 +0200212int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen,
213 const u8 *dout, u8 *din, unsigned long flags)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200214{
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200215 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
Axel Lin9675fed2013-06-14 21:13:32 +0800216 int nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200217 u32 data, cnt, i;
Stefano Babicac87c172011-01-19 22:46:33 +0000218 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Heiko Schocherf659b572014-07-14 10:22:11 +0200219 u32 ts;
220 int status;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200221
Stefano Babic2f721d12010-08-20 12:05:03 +0200222 debug("%s: bitlen %d dout 0x%x din 0x%x\n",
223 __func__, bitlen, (u32)dout, (u32)din);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200224
225 mxcs->ctrl_reg = (mxcs->ctrl_reg &
226 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100227 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
228
Stefano Babicac87c172011-01-19 22:46:33 +0000229 reg_write(&regs->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
Eric Nelson08c61a52012-01-31 07:52:03 +0000230#ifdef MXC_ECSPI
Stefano Babicac87c172011-01-19 22:46:33 +0000231 reg_write(&regs->cfg, mxcs->cfg_reg);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200232#endif
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200233
Stefano Babicd205ddc2010-04-04 22:43:38 +0200234 /* Clear interrupt register */
Stefano Babicac87c172011-01-19 22:46:33 +0000235 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100236
Stefano Babic2f721d12010-08-20 12:05:03 +0200237 /*
238 * The SPI controller works only with words,
239 * check if less than a word is sent.
240 * Access to the FIFO is only 32 bit
241 */
242 if (bitlen % 32) {
243 data = 0;
244 cnt = (bitlen % 32) / 8;
245 if (dout) {
246 for (i = 0; i < cnt; i++) {
247 data = (data << 8) | (*dout++ & 0xFF);
248 }
249 }
250 debug("Sending SPI 0x%x\n", data);
251
Stefano Babicac87c172011-01-19 22:46:33 +0000252 reg_write(&regs->txdata, data);
Stefano Babic2f721d12010-08-20 12:05:03 +0200253 nbytes -= cnt;
254 }
255
256 data = 0;
257
258 while (nbytes > 0) {
259 data = 0;
260 if (dout) {
261 /* Buffer is not 32-bit aligned */
262 if ((unsigned long)dout & 0x03) {
263 data = 0;
Anatolij Gustschindff01092011-01-20 07:53:06 +0000264 for (i = 0; i < 4; i++)
Stefano Babic2f721d12010-08-20 12:05:03 +0200265 data = (data << 8) | (*dout++ & 0xFF);
Stefano Babic2f721d12010-08-20 12:05:03 +0200266 } else {
267 data = *(u32 *)dout;
268 data = cpu_to_be32(data);
Timo Herbrecher6d5ce1b2013-10-16 00:05:09 +0530269 dout += 4;
Stefano Babic2f721d12010-08-20 12:05:03 +0200270 }
Stefano Babic2f721d12010-08-20 12:05:03 +0200271 }
272 debug("Sending SPI 0x%x\n", data);
Stefano Babicac87c172011-01-19 22:46:33 +0000273 reg_write(&regs->txdata, data);
Stefano Babic2f721d12010-08-20 12:05:03 +0200274 nbytes -= 4;
275 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200276
Stefano Babicd205ddc2010-04-04 22:43:38 +0200277 /* FIFO is written, now starts the transfer setting the XCH bit */
Stefano Babicac87c172011-01-19 22:46:33 +0000278 reg_write(&regs->ctrl, mxcs->ctrl_reg |
Stefano Babicd205ddc2010-04-04 22:43:38 +0200279 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200280
Heiko Schocherf659b572014-07-14 10:22:11 +0200281 ts = get_timer(0);
282 status = reg_read(&regs->stat);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200283 /* Wait until the TC (Transfer completed) bit is set */
Heiko Schocherf659b572014-07-14 10:22:11 +0200284 while ((status & MXC_CSPICTRL_TC) == 0) {
285 if (get_timer(ts) > CONFIG_SYS_SPI_MXC_WAIT) {
286 printf("spi_xchg_single: Timeout!\n");
287 return -1;
288 }
289 status = reg_read(&regs->stat);
290 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200291
Stefano Babicd205ddc2010-04-04 22:43:38 +0200292 /* Transfer completed, clear any pending request */
Stefano Babicac87c172011-01-19 22:46:33 +0000293 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100294
Axel Lin9675fed2013-06-14 21:13:32 +0800295 nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200296
Stefano Babic2f721d12010-08-20 12:05:03 +0200297 cnt = nbytes % 32;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200298
Stefano Babic2f721d12010-08-20 12:05:03 +0200299 if (bitlen % 32) {
Stefano Babicac87c172011-01-19 22:46:33 +0000300 data = reg_read(&regs->rxdata);
Stefano Babic2f721d12010-08-20 12:05:03 +0200301 cnt = (bitlen % 32) / 8;
Anatolij Gustschindff01092011-01-20 07:53:06 +0000302 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200303 debug("SPI Rx unaligned: 0x%x\n", data);
304 if (din) {
Anatolij Gustschindff01092011-01-20 07:53:06 +0000305 memcpy(din, &data, cnt);
306 din += cnt;
Stefano Babic2f721d12010-08-20 12:05:03 +0200307 }
308 nbytes -= cnt;
309 }
310
311 while (nbytes > 0) {
312 u32 tmp;
Stefano Babicac87c172011-01-19 22:46:33 +0000313 tmp = reg_read(&regs->rxdata);
Stefano Babic2f721d12010-08-20 12:05:03 +0200314 data = cpu_to_be32(tmp);
315 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
316 cnt = min(nbytes, sizeof(data));
317 if (din) {
318 memcpy(din, &data, cnt);
319 din += cnt;
320 }
321 nbytes -= cnt;
322 }
323
324 return 0;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200325
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200326}
327
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200328int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
329 void *din, unsigned long flags)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200330{
Axel Lin9675fed2013-06-14 21:13:32 +0800331 int n_bytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200332 int n_bits;
333 int ret;
334 u32 blk_size;
335 u8 *p_outbuf = (u8 *)dout;
336 u8 *p_inbuf = (u8 *)din;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200337
Stefano Babic2f721d12010-08-20 12:05:03 +0200338 if (!slave)
339 return -1;
340
341 if (flags & SPI_XFER_BEGIN)
342 spi_cs_activate(slave);
343
344 while (n_bytes > 0) {
Stefano Babic2f721d12010-08-20 12:05:03 +0200345 if (n_bytes < MAX_SPI_BYTES)
346 blk_size = n_bytes;
347 else
348 blk_size = MAX_SPI_BYTES;
349
350 n_bits = blk_size * 8;
351
352 ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0);
353
354 if (ret)
355 return ret;
356 if (dout)
357 p_outbuf += blk_size;
358 if (din)
359 p_inbuf += blk_size;
360 n_bytes -= blk_size;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200361 }
362
Stefano Babic2f721d12010-08-20 12:05:03 +0200363 if (flags & SPI_XFER_END) {
364 spi_cs_deactivate(slave);
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100365 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200366
367 return 0;
368}
369
370void spi_init(void)
371{
372}
373
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100374static int decode_cs(struct mxc_spi_slave *mxcs, unsigned int cs)
375{
376 int ret;
377
378 /*
379 * Some SPI devices require active chip-select over multiple
380 * transactions, we achieve this using a GPIO. Still, the SPI
381 * controller has to be configured to use one of its own chipselects.
382 * To use this feature you have to call spi_setup_slave() with
383 * cs = internal_cs | (gpio << 8), and you have to use some unused
384 * on this SPI controller cs between 0 and 3.
385 */
386 if (cs > 3) {
387 mxcs->gpio = cs >> 8;
388 cs &= 3;
Fabio Estevamde5bf022012-11-15 11:23:23 +0000389 ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100390 if (ret) {
391 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
392 return -EINVAL;
393 }
394 } else {
395 mxcs->gpio = -1;
396 }
397
398 return cs;
399}
400
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200401struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
402 unsigned int max_hz, unsigned int mode)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200403{
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200404 struct mxc_spi_slave *mxcs;
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100405 int ret;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200406
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100407 if (bus >= ARRAY_SIZE(spi_bases))
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200408 return NULL;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200409
Simon Glassd3504fe2013-03-18 19:23:40 +0000410 mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
Stefano Babic2f721d12010-08-20 12:05:03 +0200411 if (!mxcs) {
412 puts("mxc_spi: SPI Slave not allocated !\n");
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100413 return NULL;
Stefano Babic2f721d12010-08-20 12:05:03 +0200414 }
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100415
Fabio Estevamde5bf022012-11-15 11:23:23 +0000416 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
417
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100418 ret = decode_cs(mxcs, cs);
419 if (ret < 0) {
420 free(mxcs);
421 return NULL;
422 }
423
424 cs = ret;
425
Stefano Babicd205ddc2010-04-04 22:43:38 +0200426 mxcs->base = spi_bases[bus];
427
Stefano Babicc9d59c72011-01-19 22:46:30 +0000428 ret = spi_cfg_mxc(mxcs, cs, max_hz, mode);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200429 if (ret) {
430 printf("mxc_spi: cannot setup SPI controller\n");
431 free(mxcs);
432 return NULL;
433 }
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200434 return &mxcs->slave;
435}
436
437void spi_free_slave(struct spi_slave *slave)
438{
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100439 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
440
441 free(mxcs);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200442}
443
444int spi_claim_bus(struct spi_slave *slave)
445{
446 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
Stefano Babicac87c172011-01-19 22:46:33 +0000447 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200448
Stefano Babicac87c172011-01-19 22:46:33 +0000449 reg_write(&regs->rxdata, 1);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200450 udelay(1);
Stefano Babicac87c172011-01-19 22:46:33 +0000451 reg_write(&regs->ctrl, mxcs->ctrl_reg);
452 reg_write(&regs->period, MXC_CSPIPERIOD_32KHZ);
453 reg_write(&regs->intr, 0);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200454
455 return 0;
456}
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200457
458void spi_release_bus(struct spi_slave *slave)
459{
460 /* TODO: Shut the controller down */
461}