blob: 698e7266e8167c98f20ee635ba4ebdad68cb5c23 [file] [log] [blame]
Guennadi Liakhovetski38254f42008-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 Skinnemoend255bb02008-05-16 11:10:31 +020022#include <malloc.h>
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020023#include <spi.h>
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +010024#include <asm/errno.h>
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020025#include <asm/io.h>
Stefano Babicc4ea1422010-07-06 17:05:06 +020026#include <mxc_gpio.h>
Stefano Babic86271112011-03-14 15:43:56 +010027#include <asm/arch/imx-regs.h>
28#include <asm/arch/clock.h>
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020029
30#ifdef CONFIG_MX27
31/* i.MX27 has a completely wrong register layout and register definitions in the
32 * datasheet, the correct one is in the Freescale's Linux driver */
33
Helmut Raiger61a58a12011-06-15 01:45:45 +000034#error "i.MX27 CSPI not supported due to drastic differences in register definitions" \
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020035"See linux mxc_spi driver from Freescale for details."
36
Stefano Babicd205ddc2010-04-04 22:43:38 +020037#elif defined(CONFIG_MX31)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020038
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020039#define MXC_CSPICTRL_EN (1 << 0)
40#define MXC_CSPICTRL_MODE (1 << 1)
41#define MXC_CSPICTRL_XCH (1 << 2)
42#define MXC_CSPICTRL_SMC (1 << 3)
43#define MXC_CSPICTRL_POL (1 << 4)
44#define MXC_CSPICTRL_PHA (1 << 5)
45#define MXC_CSPICTRL_SSCTL (1 << 6)
Wolfgang Denk53677ef2008-05-20 16:00:29 +020046#define MXC_CSPICTRL_SSPOL (1 << 7)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020047#define MXC_CSPICTRL_CHIPSELECT(x) (((x) & 0x3) << 24)
48#define MXC_CSPICTRL_BITCOUNT(x) (((x) & 0x1f) << 8)
49#define MXC_CSPICTRL_DATARATE(x) (((x) & 0x7) << 16)
Stefano Babicd205ddc2010-04-04 22:43:38 +020050#define MXC_CSPICTRL_TC (1 << 8)
51#define MXC_CSPICTRL_RXOVF (1 << 6)
52#define MXC_CSPICTRL_MAXBITS 0x1f
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020053
54#define MXC_CSPIPERIOD_32KHZ (1 << 15)
Stefano Babic2f721d12010-08-20 12:05:03 +020055#define MAX_SPI_BYTES 4
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020056
57static unsigned long spi_bases[] = {
58 0x43fa4000,
59 0x50010000,
60 0x53f84000,
61};
62
Stefano Babicc9d59c72011-01-19 22:46:30 +000063#define mxc_get_clock(x) mx31_get_ipg_clk()
64
Stefano Babicd205ddc2010-04-04 22:43:38 +020065#elif defined(CONFIG_MX51)
Stefano Babicd205ddc2010-04-04 22:43:38 +020066
Stefano Babicd205ddc2010-04-04 22:43:38 +020067#define MXC_CSPICTRL_EN (1 << 0)
68#define MXC_CSPICTRL_MODE (1 << 1)
69#define MXC_CSPICTRL_XCH (1 << 2)
70#define MXC_CSPICTRL_CHIPSELECT(x) (((x) & 0x3) << 12)
71#define MXC_CSPICTRL_BITCOUNT(x) (((x) & 0xfff) << 20)
72#define MXC_CSPICTRL_PREDIV(x) (((x) & 0xF) << 12)
73#define MXC_CSPICTRL_POSTDIV(x) (((x) & 0xF) << 8)
74#define MXC_CSPICTRL_SELCHAN(x) (((x) & 0x3) << 18)
75#define MXC_CSPICTRL_MAXBITS 0xfff
76#define MXC_CSPICTRL_TC (1 << 7)
77#define MXC_CSPICTRL_RXOVF (1 << 6)
78
79#define MXC_CSPIPERIOD_32KHZ (1 << 15)
Stefano Babic2f721d12010-08-20 12:05:03 +020080#define MAX_SPI_BYTES 32
Stefano Babicd205ddc2010-04-04 22:43:38 +020081
82/* Bit position inside CTRL register to be associated with SS */
83#define MXC_CSPICTRL_CHAN 18
84
85/* Bit position inside CON register to be associated with SS */
86#define MXC_CSPICON_POL 4
87#define MXC_CSPICON_PHA 0
88#define MXC_CSPICON_SSPOL 12
89
90static unsigned long spi_bases[] = {
91 CSPI1_BASE_ADDR,
92 CSPI2_BASE_ADDR,
93 CSPI3_BASE_ADDR,
94};
Stefano Babicc9d59c72011-01-19 22:46:30 +000095
96#elif defined(CONFIG_MX35)
97
Stefano Babicc9d59c72011-01-19 22:46:30 +000098#define MXC_CSPICTRL_EN (1 << 0)
99#define MXC_CSPICTRL_MODE (1 << 1)
100#define MXC_CSPICTRL_XCH (1 << 2)
101#define MXC_CSPICTRL_SMC (1 << 3)
102#define MXC_CSPICTRL_POL (1 << 4)
103#define MXC_CSPICTRL_PHA (1 << 5)
104#define MXC_CSPICTRL_SSCTL (1 << 6)
105#define MXC_CSPICTRL_SSPOL (1 << 7)
106#define MXC_CSPICTRL_CHIPSELECT(x) (((x) & 0x3) << 12)
107#define MXC_CSPICTRL_BITCOUNT(x) (((x) & 0xfff) << 20)
108#define MXC_CSPICTRL_DATARATE(x) (((x) & 0x7) << 16)
109#define MXC_CSPICTRL_TC (1 << 7)
110#define MXC_CSPICTRL_RXOVF (1 << 6)
111#define MXC_CSPICTRL_MAXBITS 0xfff
112
113#define MXC_CSPIPERIOD_32KHZ (1 << 15)
114#define MAX_SPI_BYTES 4
115
116static unsigned long spi_bases[] = {
117 0x43fa4000,
118 0x50010000,
119};
120
Stefano Babicd205ddc2010-04-04 22:43:38 +0200121#else
122#error "Unsupported architecture"
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200123#endif
124
Stefano Babicc4ea1422010-07-06 17:05:06 +0200125#define OUT MXC_GPIO_DIRECTION_OUT
126
Stefano Babicac87c172011-01-19 22:46:33 +0000127#define reg_read readl
128#define reg_write(a, v) writel(v, a)
129
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200130struct mxc_spi_slave {
131 struct spi_slave slave;
132 unsigned long base;
133 u32 ctrl_reg;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200134#if defined(CONFIG_MX51)
135 u32 cfg_reg;
136#endif
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100137 int gpio;
Stefano Babicc4ea1422010-07-06 17:05:06 +0200138 int ss_pol;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200139};
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200140
141static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
142{
143 return container_of(slave, struct mxc_spi_slave, slave);
144}
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200145
Stefano Babicd205ddc2010-04-04 22:43:38 +0200146void spi_cs_activate(struct spi_slave *slave)
147{
148 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
149 if (mxcs->gpio > 0)
Stefano Babicc4ea1422010-07-06 17:05:06 +0200150 mxc_gpio_set(mxcs->gpio, mxcs->ss_pol);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200151}
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,
Stefano Babicc4ea1422010-07-06 17:05:06 +0200158 !(mxcs->ss_pol));
Stefano Babicd205ddc2010-04-04 22:43:38 +0200159}
160
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +0000161u32 get_cspi_div(u32 div)
162{
163 int i;
164
165 for (i = 0; i < 8; i++) {
166 if (div <= (4 << i))
167 return i;
168 }
169 return i;
170}
171
Stefano Babicc9d59c72011-01-19 22:46:30 +0000172#if defined(CONFIG_MX31) || defined(CONFIG_MX35)
173static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
174 unsigned int max_hz, unsigned int mode)
175{
176 unsigned int ctrl_reg;
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +0000177 u32 clk_src;
178 u32 div;
179
180 clk_src = mxc_get_clock(MXC_CSPI_CLK);
181
182 div = clk_src / max_hz;
183 div = get_cspi_div(div);
184
185 debug("clk %d Hz, div %d, real clk %d Hz\n",
186 max_hz, div, clk_src / (4 << div));
Stefano Babicc9d59c72011-01-19 22:46:30 +0000187
188 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
189 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +0000190 MXC_CSPICTRL_DATARATE(div) |
Stefano Babicc9d59c72011-01-19 22:46:30 +0000191 MXC_CSPICTRL_EN |
192#ifdef CONFIG_MX35
193 MXC_CSPICTRL_SSCTL |
194#endif
195 MXC_CSPICTRL_MODE;
196
197 if (mode & SPI_CPHA)
198 ctrl_reg |= MXC_CSPICTRL_PHA;
199 if (mode & SPI_CPOL)
200 ctrl_reg |= MXC_CSPICTRL_POL;
201 if (mode & SPI_CS_HIGH)
202 ctrl_reg |= MXC_CSPICTRL_SSPOL;
203 mxcs->ctrl_reg = ctrl_reg;
204
205 return 0;
206}
207#endif
208
209#if defined(CONFIG_MX51)
210static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
Stefano Babicd205ddc2010-04-04 22:43:38 +0200211 unsigned int max_hz, unsigned int mode)
212{
213 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
214 s32 pre_div = 0, post_div = 0, i, reg_ctrl, reg_config;
215 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0;
Stefano Babicac87c172011-01-19 22:46:33 +0000216 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200217
218 if (max_hz == 0) {
219 printf("Error: desired clock is 0\n");
220 return -1;
221 }
222
Stefano Babicac87c172011-01-19 22:46:33 +0000223 reg_ctrl = reg_read(&regs->ctrl);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200224
225 /* Reset spi */
Stefano Babicac87c172011-01-19 22:46:33 +0000226 reg_write(&regs->ctrl, 0);
227 reg_write(&regs->ctrl, (reg_ctrl | 0x1));
Stefano Babicd205ddc2010-04-04 22:43:38 +0200228
229 /*
230 * The following computation is taken directly from Freescale's code.
231 */
232 if (clk_src > max_hz) {
233 pre_div = clk_src / max_hz;
234 if (pre_div > 16) {
235 post_div = pre_div / 16;
236 pre_div = 15;
237 }
238 if (post_div != 0) {
239 for (i = 0; i < 16; i++) {
240 if ((1 << i) >= post_div)
241 break;
242 }
243 if (i == 16) {
244 printf("Error: no divider for the freq: %d\n",
245 max_hz);
246 return -1;
247 }
248 post_div = i;
249 }
250 }
251
252 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
253 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
254 MXC_CSPICTRL_SELCHAN(cs);
255 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
256 MXC_CSPICTRL_PREDIV(pre_div);
257 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
258 MXC_CSPICTRL_POSTDIV(post_div);
259
260 /* always set to master mode */
261 reg_ctrl |= 1 << (cs + 4);
262
263 /* We need to disable SPI before changing registers */
264 reg_ctrl &= ~MXC_CSPICTRL_EN;
265
266 if (mode & SPI_CS_HIGH)
267 ss_pol = 1;
268
Stefano Babic9f481e92010-08-23 20:41:19 +0200269 if (mode & SPI_CPOL)
Stefano Babicd205ddc2010-04-04 22:43:38 +0200270 sclkpol = 1;
271
272 if (mode & SPI_CPHA)
273 sclkpha = 1;
274
Stefano Babicac87c172011-01-19 22:46:33 +0000275 reg_config = reg_read(&regs->cfg);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200276
277 /*
278 * Configuration register setup
Stefano Babicc9d59c72011-01-19 22:46:30 +0000279 * The MX51 supports different setup for each SS
Stefano Babicd205ddc2010-04-04 22:43:38 +0200280 */
281 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
282 (ss_pol << (cs + MXC_CSPICON_SSPOL));
283 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
284 (sclkpol << (cs + MXC_CSPICON_POL));
285 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
286 (sclkpha << (cs + MXC_CSPICON_PHA));
287
288 debug("reg_ctrl = 0x%x\n", reg_ctrl);
Stefano Babicac87c172011-01-19 22:46:33 +0000289 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200290 debug("reg_config = 0x%x\n", reg_config);
Stefano Babicac87c172011-01-19 22:46:33 +0000291 reg_write(&regs->cfg, reg_config);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200292
293 /* save config register and control register */
294 mxcs->ctrl_reg = reg_ctrl;
295 mxcs->cfg_reg = reg_config;
296
297 /* clear interrupt reg */
Stefano Babicac87c172011-01-19 22:46:33 +0000298 reg_write(&regs->intr, 0);
299 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200300
301 return 0;
302}
303#endif
304
Stefano Babic2f721d12010-08-20 12:05:03 +0200305int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen,
306 const u8 *dout, u8 *din, unsigned long flags)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200307{
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200308 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
Stefano Babic2f721d12010-08-20 12:05:03 +0200309 int nbytes = (bitlen + 7) / 8;
310 u32 data, cnt, i;
Stefano Babicac87c172011-01-19 22:46:33 +0000311 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200312
Stefano Babic2f721d12010-08-20 12:05:03 +0200313 debug("%s: bitlen %d dout 0x%x din 0x%x\n",
314 __func__, bitlen, (u32)dout, (u32)din);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200315
316 mxcs->ctrl_reg = (mxcs->ctrl_reg &
317 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100318 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
319
Stefano Babicac87c172011-01-19 22:46:33 +0000320 reg_write(&regs->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200321#ifdef CONFIG_MX51
Stefano Babicac87c172011-01-19 22:46:33 +0000322 reg_write(&regs->cfg, mxcs->cfg_reg);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200323#endif
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200324
Stefano Babicd205ddc2010-04-04 22:43:38 +0200325 /* Clear interrupt register */
Stefano Babicac87c172011-01-19 22:46:33 +0000326 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100327
Stefano Babic2f721d12010-08-20 12:05:03 +0200328 /*
329 * The SPI controller works only with words,
330 * check if less than a word is sent.
331 * Access to the FIFO is only 32 bit
332 */
333 if (bitlen % 32) {
334 data = 0;
335 cnt = (bitlen % 32) / 8;
336 if (dout) {
337 for (i = 0; i < cnt; i++) {
338 data = (data << 8) | (*dout++ & 0xFF);
339 }
340 }
341 debug("Sending SPI 0x%x\n", data);
342
Stefano Babicac87c172011-01-19 22:46:33 +0000343 reg_write(&regs->txdata, data);
Stefano Babic2f721d12010-08-20 12:05:03 +0200344 nbytes -= cnt;
345 }
346
347 data = 0;
348
349 while (nbytes > 0) {
350 data = 0;
351 if (dout) {
352 /* Buffer is not 32-bit aligned */
353 if ((unsigned long)dout & 0x03) {
354 data = 0;
Anatolij Gustschindff01092011-01-20 07:53:06 +0000355 for (i = 0; i < 4; i++)
Stefano Babic2f721d12010-08-20 12:05:03 +0200356 data = (data << 8) | (*dout++ & 0xFF);
Stefano Babic2f721d12010-08-20 12:05:03 +0200357 } else {
358 data = *(u32 *)dout;
359 data = cpu_to_be32(data);
360 }
361 dout += 4;
362 }
363 debug("Sending SPI 0x%x\n", data);
Stefano Babicac87c172011-01-19 22:46:33 +0000364 reg_write(&regs->txdata, data);
Stefano Babic2f721d12010-08-20 12:05:03 +0200365 nbytes -= 4;
366 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200367
Stefano Babicd205ddc2010-04-04 22:43:38 +0200368 /* FIFO is written, now starts the transfer setting the XCH bit */
Stefano Babicac87c172011-01-19 22:46:33 +0000369 reg_write(&regs->ctrl, mxcs->ctrl_reg |
Stefano Babicd205ddc2010-04-04 22:43:38 +0200370 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200371
Stefano Babicd205ddc2010-04-04 22:43:38 +0200372 /* Wait until the TC (Transfer completed) bit is set */
Stefano Babicac87c172011-01-19 22:46:33 +0000373 while ((reg_read(&regs->stat) & MXC_CSPICTRL_TC) == 0)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200374 ;
375
Stefano Babicd205ddc2010-04-04 22:43:38 +0200376 /* Transfer completed, clear any pending request */
Stefano Babicac87c172011-01-19 22:46:33 +0000377 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100378
Stefano Babic2f721d12010-08-20 12:05:03 +0200379 nbytes = (bitlen + 7) / 8;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200380
Stefano Babic2f721d12010-08-20 12:05:03 +0200381 cnt = nbytes % 32;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200382
Stefano Babic2f721d12010-08-20 12:05:03 +0200383 if (bitlen % 32) {
Stefano Babicac87c172011-01-19 22:46:33 +0000384 data = reg_read(&regs->rxdata);
Stefano Babic2f721d12010-08-20 12:05:03 +0200385 cnt = (bitlen % 32) / 8;
Anatolij Gustschindff01092011-01-20 07:53:06 +0000386 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200387 debug("SPI Rx unaligned: 0x%x\n", data);
388 if (din) {
Anatolij Gustschindff01092011-01-20 07:53:06 +0000389 memcpy(din, &data, cnt);
390 din += cnt;
Stefano Babic2f721d12010-08-20 12:05:03 +0200391 }
392 nbytes -= cnt;
393 }
394
395 while (nbytes > 0) {
396 u32 tmp;
Stefano Babicac87c172011-01-19 22:46:33 +0000397 tmp = reg_read(&regs->rxdata);
Stefano Babic2f721d12010-08-20 12:05:03 +0200398 data = cpu_to_be32(tmp);
399 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
400 cnt = min(nbytes, sizeof(data));
401 if (din) {
402 memcpy(din, &data, cnt);
403 din += cnt;
404 }
405 nbytes -= cnt;
406 }
407
408 return 0;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200409
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200410}
411
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200412int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
413 void *din, unsigned long flags)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200414{
Stefano Babic2f721d12010-08-20 12:05:03 +0200415 int n_bytes = (bitlen + 7) / 8;
416 int n_bits;
417 int ret;
418 u32 blk_size;
419 u8 *p_outbuf = (u8 *)dout;
420 u8 *p_inbuf = (u8 *)din;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200421
Stefano Babic2f721d12010-08-20 12:05:03 +0200422 if (!slave)
423 return -1;
424
425 if (flags & SPI_XFER_BEGIN)
426 spi_cs_activate(slave);
427
428 while (n_bytes > 0) {
Stefano Babic2f721d12010-08-20 12:05:03 +0200429 if (n_bytes < MAX_SPI_BYTES)
430 blk_size = n_bytes;
431 else
432 blk_size = MAX_SPI_BYTES;
433
434 n_bits = blk_size * 8;
435
436 ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0);
437
438 if (ret)
439 return ret;
440 if (dout)
441 p_outbuf += blk_size;
442 if (din)
443 p_inbuf += blk_size;
444 n_bytes -= blk_size;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200445 }
446
Stefano Babic2f721d12010-08-20 12:05:03 +0200447 if (flags & SPI_XFER_END) {
448 spi_cs_deactivate(slave);
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100449 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200450
451 return 0;
452}
453
454void spi_init(void)
455{
456}
457
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100458static int decode_cs(struct mxc_spi_slave *mxcs, unsigned int cs)
459{
460 int ret;
461
462 /*
463 * Some SPI devices require active chip-select over multiple
464 * transactions, we achieve this using a GPIO. Still, the SPI
465 * controller has to be configured to use one of its own chipselects.
466 * To use this feature you have to call spi_setup_slave() with
467 * cs = internal_cs | (gpio << 8), and you have to use some unused
468 * on this SPI controller cs between 0 and 3.
469 */
470 if (cs > 3) {
471 mxcs->gpio = cs >> 8;
472 cs &= 3;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200473 ret = mxc_gpio_direction(mxcs->gpio, OUT);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100474 if (ret) {
475 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
476 return -EINVAL;
477 }
478 } else {
479 mxcs->gpio = -1;
480 }
481
482 return cs;
483}
484
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200485struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
486 unsigned int max_hz, unsigned int mode)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200487{
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200488 struct mxc_spi_slave *mxcs;
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100489 int ret;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200490
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100491 if (bus >= ARRAY_SIZE(spi_bases))
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200492 return NULL;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200493
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100494 mxcs = malloc(sizeof(struct mxc_spi_slave));
Stefano Babic2f721d12010-08-20 12:05:03 +0200495 if (!mxcs) {
496 puts("mxc_spi: SPI Slave not allocated !\n");
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100497 return NULL;
Stefano Babic2f721d12010-08-20 12:05:03 +0200498 }
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100499
500 ret = decode_cs(mxcs, cs);
501 if (ret < 0) {
502 free(mxcs);
503 return NULL;
504 }
505
506 cs = ret;
507
Stefano Babicd205ddc2010-04-04 22:43:38 +0200508 mxcs->slave.bus = bus;
509 mxcs->slave.cs = cs;
510 mxcs->base = spi_bases[bus];
Stefano Babicc4ea1422010-07-06 17:05:06 +0200511 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200512
Stefano Babicc9d59c72011-01-19 22:46:30 +0000513 ret = spi_cfg_mxc(mxcs, cs, max_hz, mode);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200514 if (ret) {
515 printf("mxc_spi: cannot setup SPI controller\n");
516 free(mxcs);
517 return NULL;
518 }
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200519 return &mxcs->slave;
520}
521
522void spi_free_slave(struct spi_slave *slave)
523{
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100524 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
525
526 free(mxcs);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200527}
528
529int spi_claim_bus(struct spi_slave *slave)
530{
531 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
Stefano Babicac87c172011-01-19 22:46:33 +0000532 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200533
Stefano Babicac87c172011-01-19 22:46:33 +0000534 reg_write(&regs->rxdata, 1);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200535 udelay(1);
Stefano Babicac87c172011-01-19 22:46:33 +0000536 reg_write(&regs->ctrl, mxcs->ctrl_reg);
537 reg_write(&regs->period, MXC_CSPIPERIOD_32KHZ);
538 reg_write(&regs->intr, 0);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200539
540 return 0;
541}
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200542
543void spi_release_bus(struct spi_slave *slave)
544{
545 /* TODO: Shut the controller down */
546}