blob: 2a4ddade323a5669e313f11e7e0811b1275a6009 [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>
Guennadi Liakhovetski38254f42008-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 Babicd205ddc2010-04-04 22:43:38 +020035#elif defined(CONFIG_MX31)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020036
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +010037#include <asm/arch/mx31.h>
38
Guennadi Liakhovetski38254f42008-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 Denk53677ef2008-05-20 16:00:29 +020056#define MXC_CSPICTRL_SSPOL (1 << 7)
Guennadi Liakhovetski38254f42008-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 Babicd205ddc2010-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 Liakhovetski38254f42008-04-15 14:14:25 +020063
64#define MXC_CSPIPERIOD_32KHZ (1 << 15)
Stefano Babic2f721d12010-08-20 12:05:03 +020065#define MAX_SPI_BYTES 4
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020066
67static unsigned long spi_bases[] = {
68 0x43fa4000,
69 0x50010000,
70 0x53f84000,
71};
72
Stefano Babicc9d59c72011-01-19 22:46:30 +000073#define mxc_get_clock(x) mx31_get_ipg_clk()
74
Stefano Babicd205ddc2010-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 Babic2f721d12010-08-20 12:05:03 +0200101#define MAX_SPI_BYTES 32
Stefano Babicd205ddc2010-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 Babicc9d59c72011-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 Babicd205ddc2010-04-04 22:43:38 +0200155#else
156#error "Unsupported architecture"
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200157#endif
158
Stefano Babicc4ea1422010-07-06 17:05:06 +0200159#define OUT MXC_GPIO_DIRECTION_OUT
160
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200161struct mxc_spi_slave {
162 struct spi_slave slave;
163 unsigned long base;
164 u32 ctrl_reg;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200165#if defined(CONFIG_MX51)
166 u32 cfg_reg;
167#endif
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100168 int gpio;
Stefano Babicc4ea1422010-07-06 17:05:06 +0200169 int ss_pol;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200170};
Haavard Skinnemoend255bb02008-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 Liakhovetski38254f42008-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}
186
Stefano Babicd205ddc2010-04-04 22:43:38 +0200187void 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 Babicc4ea1422010-07-06 17:05:06 +0200191 mxc_gpio_set(mxcs->gpio, mxcs->ss_pol);
Stefano Babicd205ddc2010-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 Babicc4ea1422010-07-06 17:05:06 +0200199 !(mxcs->ss_pol));
Stefano Babicd205ddc2010-04-04 22:43:38 +0200200}
201
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +0000202u32 get_cspi_div(u32 div)
203{
204 int i;
205
206 for (i = 0; i < 8; i++) {
207 if (div <= (4 << i))
208 return i;
209 }
210 return i;
211}
212
Stefano Babicc9d59c72011-01-19 22:46:30 +0000213#if defined(CONFIG_MX31) || defined(CONFIG_MX35)
214static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
215 unsigned int max_hz, unsigned int mode)
216{
217 unsigned int ctrl_reg;
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +0000218 u32 clk_src;
219 u32 div;
220
221 clk_src = mxc_get_clock(MXC_CSPI_CLK);
222
223 div = clk_src / max_hz;
224 div = get_cspi_div(div);
225
226 debug("clk %d Hz, div %d, real clk %d Hz\n",
227 max_hz, div, clk_src / (4 << div));
Stefano Babicc9d59c72011-01-19 22:46:30 +0000228
229 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
230 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +0000231 MXC_CSPICTRL_DATARATE(div) |
Stefano Babicc9d59c72011-01-19 22:46:30 +0000232 MXC_CSPICTRL_EN |
233#ifdef CONFIG_MX35
234 MXC_CSPICTRL_SSCTL |
235#endif
236 MXC_CSPICTRL_MODE;
237
238 if (mode & SPI_CPHA)
239 ctrl_reg |= MXC_CSPICTRL_PHA;
240 if (mode & SPI_CPOL)
241 ctrl_reg |= MXC_CSPICTRL_POL;
242 if (mode & SPI_CS_HIGH)
243 ctrl_reg |= MXC_CSPICTRL_SSPOL;
244 mxcs->ctrl_reg = ctrl_reg;
245
246 return 0;
247}
248#endif
249
250#if defined(CONFIG_MX51)
251static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
Stefano Babicd205ddc2010-04-04 22:43:38 +0200252 unsigned int max_hz, unsigned int mode)
253{
254 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
255 s32 pre_div = 0, post_div = 0, i, reg_ctrl, reg_config;
256 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0;
257
258 if (max_hz == 0) {
259 printf("Error: desired clock is 0\n");
260 return -1;
261 }
262
263 reg_ctrl = reg_read(mxcs->base + MXC_CSPICTRL);
264
265 /* Reset spi */
266 reg_write(mxcs->base + MXC_CSPICTRL, 0);
267 reg_write(mxcs->base + MXC_CSPICTRL, (reg_ctrl | 0x1));
268
269 /*
270 * The following computation is taken directly from Freescale's code.
271 */
272 if (clk_src > max_hz) {
273 pre_div = clk_src / max_hz;
274 if (pre_div > 16) {
275 post_div = pre_div / 16;
276 pre_div = 15;
277 }
278 if (post_div != 0) {
279 for (i = 0; i < 16; i++) {
280 if ((1 << i) >= post_div)
281 break;
282 }
283 if (i == 16) {
284 printf("Error: no divider for the freq: %d\n",
285 max_hz);
286 return -1;
287 }
288 post_div = i;
289 }
290 }
291
292 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
293 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
294 MXC_CSPICTRL_SELCHAN(cs);
295 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
296 MXC_CSPICTRL_PREDIV(pre_div);
297 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
298 MXC_CSPICTRL_POSTDIV(post_div);
299
300 /* always set to master mode */
301 reg_ctrl |= 1 << (cs + 4);
302
303 /* We need to disable SPI before changing registers */
304 reg_ctrl &= ~MXC_CSPICTRL_EN;
305
306 if (mode & SPI_CS_HIGH)
307 ss_pol = 1;
308
Stefano Babic9f481e92010-08-23 20:41:19 +0200309 if (mode & SPI_CPOL)
Stefano Babicd205ddc2010-04-04 22:43:38 +0200310 sclkpol = 1;
311
312 if (mode & SPI_CPHA)
313 sclkpha = 1;
314
315 reg_config = reg_read(mxcs->base + MXC_CSPICON);
316
317 /*
318 * Configuration register setup
Stefano Babicc9d59c72011-01-19 22:46:30 +0000319 * The MX51 supports different setup for each SS
Stefano Babicd205ddc2010-04-04 22:43:38 +0200320 */
321 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
322 (ss_pol << (cs + MXC_CSPICON_SSPOL));
323 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
324 (sclkpol << (cs + MXC_CSPICON_POL));
325 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
326 (sclkpha << (cs + MXC_CSPICON_PHA));
327
328 debug("reg_ctrl = 0x%x\n", reg_ctrl);
329 reg_write(mxcs->base + MXC_CSPICTRL, reg_ctrl);
330 debug("reg_config = 0x%x\n", reg_config);
331 reg_write(mxcs->base + MXC_CSPICON, reg_config);
332
333 /* save config register and control register */
334 mxcs->ctrl_reg = reg_ctrl;
335 mxcs->cfg_reg = reg_config;
336
337 /* clear interrupt reg */
338 reg_write(mxcs->base + MXC_CSPIINT, 0);
339 reg_write(mxcs->base + MXC_CSPISTAT,
340 MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
341
342 return 0;
343}
344#endif
345
Stefano Babic2f721d12010-08-20 12:05:03 +0200346int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen,
347 const u8 *dout, u8 *din, unsigned long flags)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200348{
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200349 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
Stefano Babic2f721d12010-08-20 12:05:03 +0200350 int nbytes = (bitlen + 7) / 8;
351 u32 data, cnt, i;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200352
Stefano Babic2f721d12010-08-20 12:05:03 +0200353 debug("%s: bitlen %d dout 0x%x din 0x%x\n",
354 __func__, bitlen, (u32)dout, (u32)din);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200355
356 mxcs->ctrl_reg = (mxcs->ctrl_reg &
357 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100358 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
359
Stefano Babicd205ddc2010-04-04 22:43:38 +0200360 reg_write(mxcs->base + MXC_CSPICTRL, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
361#ifdef CONFIG_MX51
362 reg_write(mxcs->base + MXC_CSPICON, mxcs->cfg_reg);
363#endif
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200364
Stefano Babicd205ddc2010-04-04 22:43:38 +0200365 /* Clear interrupt register */
366 reg_write(mxcs->base + MXC_CSPISTAT,
367 MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100368
Stefano Babic2f721d12010-08-20 12:05:03 +0200369 /*
370 * The SPI controller works only with words,
371 * check if less than a word is sent.
372 * Access to the FIFO is only 32 bit
373 */
374 if (bitlen % 32) {
375 data = 0;
376 cnt = (bitlen % 32) / 8;
377 if (dout) {
378 for (i = 0; i < cnt; i++) {
379 data = (data << 8) | (*dout++ & 0xFF);
380 }
381 }
382 debug("Sending SPI 0x%x\n", data);
383
384 reg_write(mxcs->base + MXC_CSPITXDATA, data);
385 nbytes -= cnt;
386 }
387
388 data = 0;
389
390 while (nbytes > 0) {
391 data = 0;
392 if (dout) {
393 /* Buffer is not 32-bit aligned */
394 if ((unsigned long)dout & 0x03) {
395 data = 0;
Anatolij Gustschindff01092011-01-20 07:53:06 +0000396 for (i = 0; i < 4; i++)
Stefano Babic2f721d12010-08-20 12:05:03 +0200397 data = (data << 8) | (*dout++ & 0xFF);
Stefano Babic2f721d12010-08-20 12:05:03 +0200398 } else {
399 data = *(u32 *)dout;
400 data = cpu_to_be32(data);
401 }
402 dout += 4;
403 }
404 debug("Sending SPI 0x%x\n", data);
405 reg_write(mxcs->base + MXC_CSPITXDATA, data);
406 nbytes -= 4;
407 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200408
Stefano Babicd205ddc2010-04-04 22:43:38 +0200409 /* FIFO is written, now starts the transfer setting the XCH bit */
410 reg_write(mxcs->base + MXC_CSPICTRL, mxcs->ctrl_reg |
411 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200412
Stefano Babicd205ddc2010-04-04 22:43:38 +0200413 /* Wait until the TC (Transfer completed) bit is set */
414 while ((reg_read(mxcs->base + MXC_CSPISTAT) & MXC_CSPICTRL_TC) == 0)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200415 ;
416
Stefano Babicd205ddc2010-04-04 22:43:38 +0200417 /* Transfer completed, clear any pending request */
418 reg_write(mxcs->base + MXC_CSPISTAT,
419 MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100420
Stefano Babic2f721d12010-08-20 12:05:03 +0200421 nbytes = (bitlen + 7) / 8;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200422
Stefano Babic2f721d12010-08-20 12:05:03 +0200423 cnt = nbytes % 32;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200424
Stefano Babic2f721d12010-08-20 12:05:03 +0200425 if (bitlen % 32) {
426 data = reg_read(mxcs->base + MXC_CSPIRXDATA);
427 cnt = (bitlen % 32) / 8;
Anatolij Gustschindff01092011-01-20 07:53:06 +0000428 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200429 debug("SPI Rx unaligned: 0x%x\n", data);
430 if (din) {
Anatolij Gustschindff01092011-01-20 07:53:06 +0000431 memcpy(din, &data, cnt);
432 din += cnt;
Stefano Babic2f721d12010-08-20 12:05:03 +0200433 }
434 nbytes -= cnt;
435 }
436
437 while (nbytes > 0) {
438 u32 tmp;
439 tmp = reg_read(mxcs->base + MXC_CSPIRXDATA);
440 data = cpu_to_be32(tmp);
441 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
442 cnt = min(nbytes, sizeof(data));
443 if (din) {
444 memcpy(din, &data, cnt);
445 din += cnt;
446 }
447 nbytes -= cnt;
448 }
449
450 return 0;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200451
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200452}
453
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200454int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
455 void *din, unsigned long flags)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200456{
Stefano Babic2f721d12010-08-20 12:05:03 +0200457 int n_bytes = (bitlen + 7) / 8;
458 int n_bits;
459 int ret;
460 u32 blk_size;
461 u8 *p_outbuf = (u8 *)dout;
462 u8 *p_inbuf = (u8 *)din;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200463
Stefano Babic2f721d12010-08-20 12:05:03 +0200464 if (!slave)
465 return -1;
466
467 if (flags & SPI_XFER_BEGIN)
468 spi_cs_activate(slave);
469
470 while (n_bytes > 0) {
471
472 if (n_bytes < MAX_SPI_BYTES)
473 blk_size = n_bytes;
474 else
475 blk_size = MAX_SPI_BYTES;
476
477 n_bits = blk_size * 8;
478
479 ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0);
480
481 if (ret)
482 return ret;
483 if (dout)
484 p_outbuf += blk_size;
485 if (din)
486 p_inbuf += blk_size;
487 n_bytes -= blk_size;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200488 }
489
Stefano Babic2f721d12010-08-20 12:05:03 +0200490 if (flags & SPI_XFER_END) {
491 spi_cs_deactivate(slave);
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100492 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200493
494 return 0;
495}
496
497void spi_init(void)
498{
499}
500
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100501static int decode_cs(struct mxc_spi_slave *mxcs, unsigned int cs)
502{
503 int ret;
504
505 /*
506 * Some SPI devices require active chip-select over multiple
507 * transactions, we achieve this using a GPIO. Still, the SPI
508 * controller has to be configured to use one of its own chipselects.
509 * To use this feature you have to call spi_setup_slave() with
510 * cs = internal_cs | (gpio << 8), and you have to use some unused
511 * on this SPI controller cs between 0 and 3.
512 */
513 if (cs > 3) {
514 mxcs->gpio = cs >> 8;
515 cs &= 3;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200516 ret = mxc_gpio_direction(mxcs->gpio, OUT);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100517 if (ret) {
518 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
519 return -EINVAL;
520 }
521 } else {
522 mxcs->gpio = -1;
523 }
524
525 return cs;
526}
527
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200528struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
529 unsigned int max_hz, unsigned int mode)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200530{
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200531 struct mxc_spi_slave *mxcs;
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100532 int ret;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200533
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100534 if (bus >= ARRAY_SIZE(spi_bases))
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200535 return NULL;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200536
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100537 mxcs = malloc(sizeof(struct mxc_spi_slave));
Stefano Babic2f721d12010-08-20 12:05:03 +0200538 if (!mxcs) {
539 puts("mxc_spi: SPI Slave not allocated !\n");
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100540 return NULL;
Stefano Babic2f721d12010-08-20 12:05:03 +0200541 }
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100542
543 ret = decode_cs(mxcs, cs);
544 if (ret < 0) {
545 free(mxcs);
546 return NULL;
547 }
548
549 cs = ret;
550
Stefano Babicd205ddc2010-04-04 22:43:38 +0200551 mxcs->slave.bus = bus;
552 mxcs->slave.cs = cs;
553 mxcs->base = spi_bases[bus];
Stefano Babicc4ea1422010-07-06 17:05:06 +0200554 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200555
Stefano Babicc9d59c72011-01-19 22:46:30 +0000556 ret = spi_cfg_mxc(mxcs, cs, max_hz, mode);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200557 if (ret) {
558 printf("mxc_spi: cannot setup SPI controller\n");
559 free(mxcs);
560 return NULL;
561 }
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200562 return &mxcs->slave;
563}
564
565void spi_free_slave(struct spi_slave *slave)
566{
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100567 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
568
569 free(mxcs);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200570}
571
572int spi_claim_bus(struct spi_slave *slave)
573{
574 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
575
576 reg_write(mxcs->base + MXC_CSPIRESET, 1);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200577 udelay(1);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200578 reg_write(mxcs->base + MXC_CSPICTRL, mxcs->ctrl_reg);
579 reg_write(mxcs->base + MXC_CSPIPERIOD,
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200580 MXC_CSPIPERIOD_32KHZ);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200581 reg_write(mxcs->base + MXC_CSPIINT, 0);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200582
583 return 0;
584}
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200585
586void spi_release_bus(struct spi_slave *slave)
587{
588 /* TODO: Shut the controller down */
589}