blob: dadf228844de497f5b3c407ddc7b14ea03ab819a [file] [log] [blame]
Guennadi Liakhovetski07327a52008-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 Skinnemoend74084a2008-05-16 11:10:31 +020022#include <malloc.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020023#include <spi.h>
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +010024#include <asm/errno.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020025#include <asm/io.h>
Stefano Babicd77fe992010-07-06 17:05:06 +020026#include <mxc_gpio.h>
Guennadi Liakhovetski07327a52008-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 Babic6e6f4552010-04-04 22:43:38 +020035#elif defined(CONFIG_MX31)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020036
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +010037#include <asm/arch/mx31.h>
38
Guennadi Liakhovetski07327a52008-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 Denka1be4762008-05-20 16:00:29 +020056#define MXC_CSPICTRL_SSPOL (1 << 7)
Guennadi Liakhovetski07327a52008-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 Babic6e6f4552010-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 Liakhovetski07327a52008-04-15 14:14:25 +020063
64#define MXC_CSPIPERIOD_32KHZ (1 << 15)
Stefano Babic125f82a2010-08-20 12:05:03 +020065#define MAX_SPI_BYTES 4
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020066
67static unsigned long spi_bases[] = {
68 0x43fa4000,
69 0x50010000,
70 0x53f84000,
71};
72
Stefano Babicdcd73cd2011-01-19 22:46:30 +000073#define mxc_get_clock(x) mx31_get_ipg_clk()
74
Stefano Babic6e6f4552010-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 Babic125f82a2010-08-20 12:05:03 +0200101#define MAX_SPI_BYTES 32
Stefano Babic6e6f4552010-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 Babicdcd73cd2011-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 Babic6e6f4552010-04-04 22:43:38 +0200155#else
156#error "Unsupported architecture"
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200157#endif
158
Stefano Babicd77fe992010-07-06 17:05:06 +0200159#define OUT MXC_GPIO_DIRECTION_OUT
160
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200161struct mxc_spi_slave {
162 struct spi_slave slave;
163 unsigned long base;
164 u32 ctrl_reg;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200165#if defined(CONFIG_MX51)
166 u32 cfg_reg;
167#endif
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100168 int gpio;
Stefano Babicd77fe992010-07-06 17:05:06 +0200169 int ss_pol;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200170};
Haavard Skinnemoend74084a2008-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 Liakhovetski07327a52008-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}
Stefano Babic6e6f4552010-04-04 22:43:38 +0200186
187void 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 Babicd77fe992010-07-06 17:05:06 +0200191 mxc_gpio_set(mxcs->gpio, mxcs->ss_pol);
Stefano Babic6e6f4552010-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 Babicd77fe992010-07-06 17:05:06 +0200199 !(mxcs->ss_pol));
Stefano Babic6e6f4552010-04-04 22:43:38 +0200200}
201
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000202#if defined(CONFIG_MX31) || defined(CONFIG_MX35)
203static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
204 unsigned int max_hz, unsigned int mode)
205{
206 unsigned int ctrl_reg;
207
208 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
209 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
210 MXC_CSPICTRL_DATARATE(7) | /* FIXME: calculate data rate */
211 MXC_CSPICTRL_EN |
212#ifdef CONFIG_MX35
213 MXC_CSPICTRL_SSCTL |
214#endif
215 MXC_CSPICTRL_MODE;
216
217 if (mode & SPI_CPHA)
218 ctrl_reg |= MXC_CSPICTRL_PHA;
219 if (mode & SPI_CPOL)
220 ctrl_reg |= MXC_CSPICTRL_POL;
221 if (mode & SPI_CS_HIGH)
222 ctrl_reg |= MXC_CSPICTRL_SSPOL;
223 mxcs->ctrl_reg = ctrl_reg;
224
225 return 0;
226}
227#endif
228
229#if defined(CONFIG_MX51)
230static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
Stefano Babic6e6f4552010-04-04 22:43:38 +0200231 unsigned int max_hz, unsigned int mode)
232{
233 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
234 s32 pre_div = 0, post_div = 0, i, reg_ctrl, reg_config;
235 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0;
236
237 if (max_hz == 0) {
238 printf("Error: desired clock is 0\n");
239 return -1;
240 }
241
242 reg_ctrl = reg_read(mxcs->base + MXC_CSPICTRL);
243
244 /* Reset spi */
245 reg_write(mxcs->base + MXC_CSPICTRL, 0);
246 reg_write(mxcs->base + MXC_CSPICTRL, (reg_ctrl | 0x1));
247
248 /*
249 * The following computation is taken directly from Freescale's code.
250 */
251 if (clk_src > max_hz) {
252 pre_div = clk_src / max_hz;
253 if (pre_div > 16) {
254 post_div = pre_div / 16;
255 pre_div = 15;
256 }
257 if (post_div != 0) {
258 for (i = 0; i < 16; i++) {
259 if ((1 << i) >= post_div)
260 break;
261 }
262 if (i == 16) {
263 printf("Error: no divider for the freq: %d\n",
264 max_hz);
265 return -1;
266 }
267 post_div = i;
268 }
269 }
270
271 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
272 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
273 MXC_CSPICTRL_SELCHAN(cs);
274 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
275 MXC_CSPICTRL_PREDIV(pre_div);
276 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
277 MXC_CSPICTRL_POSTDIV(post_div);
278
279 /* always set to master mode */
280 reg_ctrl |= 1 << (cs + 4);
281
282 /* We need to disable SPI before changing registers */
283 reg_ctrl &= ~MXC_CSPICTRL_EN;
284
285 if (mode & SPI_CS_HIGH)
286 ss_pol = 1;
287
Stefano Babic4c596992010-08-23 20:41:19 +0200288 if (mode & SPI_CPOL)
Stefano Babic6e6f4552010-04-04 22:43:38 +0200289 sclkpol = 1;
290
291 if (mode & SPI_CPHA)
292 sclkpha = 1;
293
294 reg_config = reg_read(mxcs->base + MXC_CSPICON);
295
296 /*
297 * Configuration register setup
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000298 * The MX51 supports different setup for each SS
Stefano Babic6e6f4552010-04-04 22:43:38 +0200299 */
300 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
301 (ss_pol << (cs + MXC_CSPICON_SSPOL));
302 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
303 (sclkpol << (cs + MXC_CSPICON_POL));
304 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
305 (sclkpha << (cs + MXC_CSPICON_PHA));
306
307 debug("reg_ctrl = 0x%x\n", reg_ctrl);
308 reg_write(mxcs->base + MXC_CSPICTRL, reg_ctrl);
309 debug("reg_config = 0x%x\n", reg_config);
310 reg_write(mxcs->base + MXC_CSPICON, reg_config);
311
312 /* save config register and control register */
313 mxcs->ctrl_reg = reg_ctrl;
314 mxcs->cfg_reg = reg_config;
315
316 /* clear interrupt reg */
317 reg_write(mxcs->base + MXC_CSPIINT, 0);
318 reg_write(mxcs->base + MXC_CSPISTAT,
319 MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
320
321 return 0;
322}
323#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200324
Stefano Babic125f82a2010-08-20 12:05:03 +0200325int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen,
326 const u8 *dout, u8 *din, unsigned long flags)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200327{
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200328 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
Stefano Babic125f82a2010-08-20 12:05:03 +0200329 int nbytes = (bitlen + 7) / 8;
330 u32 data, cnt, i;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200331
Stefano Babic125f82a2010-08-20 12:05:03 +0200332 debug("%s: bitlen %d dout 0x%x din 0x%x\n",
333 __func__, bitlen, (u32)dout, (u32)din);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200334
Stefano Babic6e6f4552010-04-04 22:43:38 +0200335 mxcs->ctrl_reg = (mxcs->ctrl_reg &
336 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100337 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200338
Stefano Babic6e6f4552010-04-04 22:43:38 +0200339 reg_write(mxcs->base + MXC_CSPICTRL, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
340#ifdef CONFIG_MX51
341 reg_write(mxcs->base + MXC_CSPICON, mxcs->cfg_reg);
342#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200343
Stefano Babic6e6f4552010-04-04 22:43:38 +0200344 /* Clear interrupt register */
345 reg_write(mxcs->base + MXC_CSPISTAT,
346 MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100347
Stefano Babic125f82a2010-08-20 12:05:03 +0200348 /*
349 * The SPI controller works only with words,
350 * check if less than a word is sent.
351 * Access to the FIFO is only 32 bit
352 */
353 if (bitlen % 32) {
354 data = 0;
355 cnt = (bitlen % 32) / 8;
356 if (dout) {
357 for (i = 0; i < cnt; i++) {
358 data = (data << 8) | (*dout++ & 0xFF);
359 }
360 }
361 debug("Sending SPI 0x%x\n", data);
362
363 reg_write(mxcs->base + MXC_CSPITXDATA, data);
364 nbytes -= cnt;
365 }
366
367 data = 0;
368
369 while (nbytes > 0) {
370 data = 0;
371 if (dout) {
372 /* Buffer is not 32-bit aligned */
373 if ((unsigned long)dout & 0x03) {
374 data = 0;
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000375 for (i = 0; i < 4; i++)
Stefano Babic125f82a2010-08-20 12:05:03 +0200376 data = (data << 8) | (*dout++ & 0xFF);
Stefano Babic125f82a2010-08-20 12:05:03 +0200377 } else {
378 data = *(u32 *)dout;
379 data = cpu_to_be32(data);
380 }
381 dout += 4;
382 }
383 debug("Sending SPI 0x%x\n", data);
384 reg_write(mxcs->base + MXC_CSPITXDATA, data);
385 nbytes -= 4;
386 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200387
Stefano Babic6e6f4552010-04-04 22:43:38 +0200388 /* FIFO is written, now starts the transfer setting the XCH bit */
389 reg_write(mxcs->base + MXC_CSPICTRL, mxcs->ctrl_reg |
390 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200391
Stefano Babic6e6f4552010-04-04 22:43:38 +0200392 /* Wait until the TC (Transfer completed) bit is set */
393 while ((reg_read(mxcs->base + MXC_CSPISTAT) & MXC_CSPICTRL_TC) == 0)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200394 ;
395
Stefano Babic6e6f4552010-04-04 22:43:38 +0200396 /* Transfer completed, clear any pending request */
397 reg_write(mxcs->base + MXC_CSPISTAT,
398 MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
399
Stefano Babic125f82a2010-08-20 12:05:03 +0200400 nbytes = (bitlen + 7) / 8;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200401
Stefano Babic125f82a2010-08-20 12:05:03 +0200402 cnt = nbytes % 32;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100403
Stefano Babic125f82a2010-08-20 12:05:03 +0200404 if (bitlen % 32) {
405 data = reg_read(mxcs->base + MXC_CSPIRXDATA);
406 cnt = (bitlen % 32) / 8;
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000407 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
Stefano Babic125f82a2010-08-20 12:05:03 +0200408 debug("SPI Rx unaligned: 0x%x\n", data);
409 if (din) {
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000410 memcpy(din, &data, cnt);
411 din += cnt;
Stefano Babic125f82a2010-08-20 12:05:03 +0200412 }
413 nbytes -= cnt;
414 }
415
416 while (nbytes > 0) {
417 u32 tmp;
418 tmp = reg_read(mxcs->base + MXC_CSPIRXDATA);
419 data = cpu_to_be32(tmp);
420 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
421 cnt = min(nbytes, sizeof(data));
422 if (din) {
423 memcpy(din, &data, cnt);
424 din += cnt;
425 }
426 nbytes -= cnt;
427 }
428
429 return 0;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200430
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200431}
432
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200433int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
434 void *din, unsigned long flags)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200435{
Stefano Babic125f82a2010-08-20 12:05:03 +0200436 int n_bytes = (bitlen + 7) / 8;
437 int n_bits;
438 int ret;
439 u32 blk_size;
440 u8 *p_outbuf = (u8 *)dout;
441 u8 *p_inbuf = (u8 *)din;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200442
Stefano Babic125f82a2010-08-20 12:05:03 +0200443 if (!slave)
444 return -1;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200445
Stefano Babic125f82a2010-08-20 12:05:03 +0200446 if (flags & SPI_XFER_BEGIN)
447 spi_cs_activate(slave);
Magnus Lilja1858a9a2010-02-09 22:05:39 +0100448
Stefano Babic125f82a2010-08-20 12:05:03 +0200449 while (n_bytes > 0) {
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100450
Stefano Babic125f82a2010-08-20 12:05:03 +0200451 if (n_bytes < MAX_SPI_BYTES)
452 blk_size = n_bytes;
453 else
454 blk_size = MAX_SPI_BYTES;
455
456 n_bits = blk_size * 8;
457
458 ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0);
459
460 if (ret)
461 return ret;
462 if (dout)
463 p_outbuf += blk_size;
464 if (din)
465 p_inbuf += blk_size;
466 n_bytes -= blk_size;
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100467 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200468
Stefano Babic125f82a2010-08-20 12:05:03 +0200469 if (flags & SPI_XFER_END) {
470 spi_cs_deactivate(slave);
471 }
472
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200473 return 0;
474}
475
476void spi_init(void)
477{
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100478}
479
480static int decode_cs(struct mxc_spi_slave *mxcs, unsigned int cs)
481{
482 int ret;
483
484 /*
485 * Some SPI devices require active chip-select over multiple
486 * transactions, we achieve this using a GPIO. Still, the SPI
487 * controller has to be configured to use one of its own chipselects.
488 * To use this feature you have to call spi_setup_slave() with
489 * cs = internal_cs | (gpio << 8), and you have to use some unused
490 * on this SPI controller cs between 0 and 3.
491 */
492 if (cs > 3) {
493 mxcs->gpio = cs >> 8;
494 cs &= 3;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200495 ret = mxc_gpio_direction(mxcs->gpio, OUT);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100496 if (ret) {
497 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
498 return -EINVAL;
499 }
500 } else {
501 mxcs->gpio = -1;
502 }
503
504 return cs;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200505}
506
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200507struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
508 unsigned int max_hz, unsigned int mode)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200509{
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200510 struct mxc_spi_slave *mxcs;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100511 int ret;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200512
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100513 if (bus >= ARRAY_SIZE(spi_bases))
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200514 return NULL;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200515
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100516 mxcs = malloc(sizeof(struct mxc_spi_slave));
Stefano Babic125f82a2010-08-20 12:05:03 +0200517 if (!mxcs) {
518 puts("mxc_spi: SPI Slave not allocated !\n");
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100519 return NULL;
Stefano Babic125f82a2010-08-20 12:05:03 +0200520 }
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100521
522 ret = decode_cs(mxcs, cs);
523 if (ret < 0) {
524 free(mxcs);
525 return NULL;
526 }
527
528 cs = ret;
529
Stefano Babic6e6f4552010-04-04 22:43:38 +0200530 mxcs->slave.bus = bus;
531 mxcs->slave.cs = cs;
532 mxcs->base = spi_bases[bus];
Stefano Babicd77fe992010-07-06 17:05:06 +0200533 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200534
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000535 ret = spi_cfg_mxc(mxcs, cs, max_hz, mode);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200536 if (ret) {
537 printf("mxc_spi: cannot setup SPI controller\n");
538 free(mxcs);
539 return NULL;
540 }
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200541 return &mxcs->slave;
542}
543
544void spi_free_slave(struct spi_slave *slave)
545{
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100546 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
547
548 free(mxcs);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200549}
550
551int spi_claim_bus(struct spi_slave *slave)
552{
553 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
554
555 reg_write(mxcs->base + MXC_CSPIRESET, 1);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200556 udelay(1);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200557 reg_write(mxcs->base + MXC_CSPICTRL, mxcs->ctrl_reg);
558 reg_write(mxcs->base + MXC_CSPIPERIOD,
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200559 MXC_CSPIPERIOD_32KHZ);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200560 reg_write(mxcs->base + MXC_CSPIINT, 0);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200561
562 return 0;
563}
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200564
565void spi_release_bus(struct spi_slave *slave)
566{
567 /* TODO: Shut the controller down */
568}