blob: 026f680d80d90e60591d04bb3b092058df371883 [file] [log] [blame]
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +02001/*
2 * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de>
3 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +02005 */
6
7#include <common.h>
Haavard Skinnemoend74084a2008-05-16 11:10:31 +02008#include <malloc.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +02009#include <spi.h>
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +010010#include <asm/errno.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020011#include <asm/io.h>
Stefano Babic7faee912011-08-21 10:45:44 +020012#include <asm/gpio.h>
Stefano Babic78129d92011-03-14 15:43:56 +010013#include <asm/arch/imx-regs.h>
14#include <asm/arch/clock.h>
Guennadi Liakhovetski07327a52008-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 Raiger785efc92011-06-15 01:45:45 +000020#error "i.MX27 CSPI not supported due to drastic differences in register definitions" \
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020021"See linux mxc_spi driver from Freescale for details."
Eric Nelsonfe1e7612012-01-31 07:52:03 +000022#endif
Stefano Babicdcd73cd2011-01-19 22:46:30 +000023
24static unsigned long spi_bases[] = {
Eric Nelsonfe1e7612012-01-31 07:52:03 +000025 MXC_SPI_BASE_ADDRESSES
Stefano Babicdcd73cd2011-01-19 22:46:30 +000026};
27
Nikita Kiryanov00cd7382014-08-20 15:08:50 +030028__weak int board_spi_cs_gpio(unsigned bus, unsigned cs)
29{
30 return -1;
31}
32
Stefano Babicd77fe992010-07-06 17:05:06 +020033#define OUT MXC_GPIO_DIRECTION_OUT
34
Stefano Babic28580452011-01-19 22:46:33 +000035#define reg_read readl
36#define reg_write(a, v) writel(v, a)
37
Heiko Schocherb77c8882014-07-14 10:22:11 +020038#if !defined(CONFIG_SYS_SPI_MXC_WAIT)
39#define CONFIG_SYS_SPI_MXC_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
40#endif
41
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020042struct mxc_spi_slave {
43 struct spi_slave slave;
44 unsigned long base;
45 u32 ctrl_reg;
Eric Nelsonfe1e7612012-01-31 07:52:03 +000046#if defined(MXC_ECSPI)
Stefano Babic6e6f4552010-04-04 22:43:38 +020047 u32 cfg_reg;
48#endif
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +010049 int gpio;
Stefano Babicd77fe992010-07-06 17:05:06 +020050 int ss_pol;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020051};
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020052
53static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
54{
55 return container_of(slave, struct mxc_spi_slave, slave);
56}
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020057
Stefano Babic6e6f4552010-04-04 22:43:38 +020058void spi_cs_activate(struct spi_slave *slave)
59{
60 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
61 if (mxcs->gpio > 0)
Stefano Babic7faee912011-08-21 10:45:44 +020062 gpio_set_value(mxcs->gpio, mxcs->ss_pol);
Stefano Babic6e6f4552010-04-04 22:43:38 +020063}
64
65void spi_cs_deactivate(struct spi_slave *slave)
66{
67 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
68 if (mxcs->gpio > 0)
Stefano Babic7faee912011-08-21 10:45:44 +020069 gpio_set_value(mxcs->gpio,
Stefano Babicd77fe992010-07-06 17:05:06 +020070 !(mxcs->ss_pol));
Stefano Babic6e6f4552010-04-04 22:43:38 +020071}
72
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +000073u32 get_cspi_div(u32 div)
74{
75 int i;
76
77 for (i = 0; i < 8; i++) {
78 if (div <= (4 << i))
79 return i;
80 }
81 return i;
82}
83
Eric Nelsonfe1e7612012-01-31 07:52:03 +000084#ifdef MXC_CSPI
Stefano Babicdcd73cd2011-01-19 22:46:30 +000085static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
86 unsigned int max_hz, unsigned int mode)
87{
88 unsigned int ctrl_reg;
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +000089 u32 clk_src;
90 u32 div;
91
92 clk_src = mxc_get_clock(MXC_CSPI_CLK);
93
Benoît Thébaudeau884622b2012-08-10 08:51:50 +000094 div = DIV_ROUND_UP(clk_src, max_hz);
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +000095 div = get_cspi_div(div);
96
97 debug("clk %d Hz, div %d, real clk %d Hz\n",
98 max_hz, div, clk_src / (4 << div));
Stefano Babicdcd73cd2011-01-19 22:46:30 +000099
100 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
101 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000102 MXC_CSPICTRL_DATARATE(div) |
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000103 MXC_CSPICTRL_EN |
104#ifdef CONFIG_MX35
105 MXC_CSPICTRL_SSCTL |
106#endif
107 MXC_CSPICTRL_MODE;
108
109 if (mode & SPI_CPHA)
110 ctrl_reg |= MXC_CSPICTRL_PHA;
111 if (mode & SPI_CPOL)
112 ctrl_reg |= MXC_CSPICTRL_POL;
113 if (mode & SPI_CS_HIGH)
114 ctrl_reg |= MXC_CSPICTRL_SSPOL;
115 mxcs->ctrl_reg = ctrl_reg;
116
117 return 0;
118}
119#endif
120
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000121#ifdef MXC_ECSPI
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000122static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
Stefano Babic6e6f4552010-04-04 22:43:38 +0200123 unsigned int max_hz, unsigned int mode)
124{
125 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
Dirk Behmeb177b712013-05-11 07:25:54 +0200126 s32 reg_ctrl, reg_config;
Markus Niebel6683e622014-02-17 17:33:17 +0100127 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0;
128 u32 pre_div = 0, post_div = 0;
Stefano Babic28580452011-01-19 22:46:33 +0000129 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200130
131 if (max_hz == 0) {
132 printf("Error: desired clock is 0\n");
133 return -1;
134 }
135
Fabio Estevam833fb552013-04-09 13:06:25 +0000136 /*
137 * Reset SPI and set all CSs to master mode, if toggling
138 * between slave and master mode we might see a glitch
139 * on the clock line
140 */
141 reg_ctrl = MXC_CSPICTRL_MODE_MASK;
142 reg_write(&regs->ctrl, reg_ctrl);
143 reg_ctrl |= MXC_CSPICTRL_EN;
144 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200145
Stefano Babic6e6f4552010-04-04 22:43:38 +0200146 if (clk_src > max_hz) {
Dirk Behmeb177b712013-05-11 07:25:54 +0200147 pre_div = (clk_src - 1) / max_hz;
148 /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
149 post_div = fls(pre_div);
150 if (post_div > 4) {
151 post_div -= 4;
152 if (post_div >= 16) {
Stefano Babic6e6f4552010-04-04 22:43:38 +0200153 printf("Error: no divider for the freq: %d\n",
154 max_hz);
155 return -1;
156 }
Dirk Behmeb177b712013-05-11 07:25:54 +0200157 pre_div >>= post_div;
158 } else {
159 post_div = 0;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200160 }
161 }
162
163 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
164 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
165 MXC_CSPICTRL_SELCHAN(cs);
166 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
167 MXC_CSPICTRL_PREDIV(pre_div);
168 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
169 MXC_CSPICTRL_POSTDIV(post_div);
170
Stefano Babic6e6f4552010-04-04 22:43:38 +0200171 /* We need to disable SPI before changing registers */
172 reg_ctrl &= ~MXC_CSPICTRL_EN;
173
174 if (mode & SPI_CS_HIGH)
175 ss_pol = 1;
176
Markus Niebel6683e622014-02-17 17:33:17 +0100177 if (mode & SPI_CPOL) {
Stefano Babic6e6f4552010-04-04 22:43:38 +0200178 sclkpol = 1;
Markus Niebel6683e622014-02-17 17:33:17 +0100179 sclkctl = 1;
180 }
Stefano Babic6e6f4552010-04-04 22:43:38 +0200181
182 if (mode & SPI_CPHA)
183 sclkpha = 1;
184
Stefano Babic28580452011-01-19 22:46:33 +0000185 reg_config = reg_read(&regs->cfg);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200186
187 /*
188 * Configuration register setup
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000189 * The MX51 supports different setup for each SS
Stefano Babic6e6f4552010-04-04 22:43:38 +0200190 */
191 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
192 (ss_pol << (cs + MXC_CSPICON_SSPOL));
193 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
194 (sclkpol << (cs + MXC_CSPICON_POL));
Markus Niebel6683e622014-02-17 17:33:17 +0100195 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_CTL))) |
196 (sclkctl << (cs + MXC_CSPICON_CTL));
Stefano Babic6e6f4552010-04-04 22:43:38 +0200197 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
198 (sclkpha << (cs + MXC_CSPICON_PHA));
199
200 debug("reg_ctrl = 0x%x\n", reg_ctrl);
Stefano Babic28580452011-01-19 22:46:33 +0000201 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200202 debug("reg_config = 0x%x\n", reg_config);
Stefano Babic28580452011-01-19 22:46:33 +0000203 reg_write(&regs->cfg, reg_config);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200204
205 /* save config register and control register */
206 mxcs->ctrl_reg = reg_ctrl;
207 mxcs->cfg_reg = reg_config;
208
209 /* clear interrupt reg */
Stefano Babic28580452011-01-19 22:46:33 +0000210 reg_write(&regs->intr, 0);
211 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200212
213 return 0;
214}
215#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200216
Stefano Babic125f82a2010-08-20 12:05:03 +0200217int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen,
218 const u8 *dout, u8 *din, unsigned long flags)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200219{
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200220 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
Axel Linfb7def92013-06-14 21:13:32 +0800221 int nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic125f82a2010-08-20 12:05:03 +0200222 u32 data, cnt, i;
Stefano Babic28580452011-01-19 22:46:33 +0000223 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Heiko Schocherb77c8882014-07-14 10:22:11 +0200224 u32 ts;
225 int status;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200226
Stefano Babic125f82a2010-08-20 12:05:03 +0200227 debug("%s: bitlen %d dout 0x%x din 0x%x\n",
228 __func__, bitlen, (u32)dout, (u32)din);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200229
Stefano Babic6e6f4552010-04-04 22:43:38 +0200230 mxcs->ctrl_reg = (mxcs->ctrl_reg &
231 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100232 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200233
Stefano Babic28580452011-01-19 22:46:33 +0000234 reg_write(&regs->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000235#ifdef MXC_ECSPI
Stefano Babic28580452011-01-19 22:46:33 +0000236 reg_write(&regs->cfg, mxcs->cfg_reg);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200237#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200238
Stefano Babic6e6f4552010-04-04 22:43:38 +0200239 /* Clear interrupt register */
Stefano Babic28580452011-01-19 22:46:33 +0000240 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100241
Stefano Babic125f82a2010-08-20 12:05:03 +0200242 /*
243 * The SPI controller works only with words,
244 * check if less than a word is sent.
245 * Access to the FIFO is only 32 bit
246 */
247 if (bitlen % 32) {
248 data = 0;
249 cnt = (bitlen % 32) / 8;
250 if (dout) {
251 for (i = 0; i < cnt; i++) {
252 data = (data << 8) | (*dout++ & 0xFF);
253 }
254 }
255 debug("Sending SPI 0x%x\n", data);
256
Stefano Babic28580452011-01-19 22:46:33 +0000257 reg_write(&regs->txdata, data);
Stefano Babic125f82a2010-08-20 12:05:03 +0200258 nbytes -= cnt;
259 }
260
261 data = 0;
262
263 while (nbytes > 0) {
264 data = 0;
265 if (dout) {
266 /* Buffer is not 32-bit aligned */
267 if ((unsigned long)dout & 0x03) {
268 data = 0;
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000269 for (i = 0; i < 4; i++)
Stefano Babic125f82a2010-08-20 12:05:03 +0200270 data = (data << 8) | (*dout++ & 0xFF);
Stefano Babic125f82a2010-08-20 12:05:03 +0200271 } else {
272 data = *(u32 *)dout;
273 data = cpu_to_be32(data);
Timo Herbrecher64203202013-10-16 00:05:09 +0530274 dout += 4;
Stefano Babic125f82a2010-08-20 12:05:03 +0200275 }
Stefano Babic125f82a2010-08-20 12:05:03 +0200276 }
277 debug("Sending SPI 0x%x\n", data);
Stefano Babic28580452011-01-19 22:46:33 +0000278 reg_write(&regs->txdata, data);
Stefano Babic125f82a2010-08-20 12:05:03 +0200279 nbytes -= 4;
280 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200281
Stefano Babic6e6f4552010-04-04 22:43:38 +0200282 /* FIFO is written, now starts the transfer setting the XCH bit */
Stefano Babic28580452011-01-19 22:46:33 +0000283 reg_write(&regs->ctrl, mxcs->ctrl_reg |
Stefano Babic6e6f4552010-04-04 22:43:38 +0200284 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200285
Heiko Schocherb77c8882014-07-14 10:22:11 +0200286 ts = get_timer(0);
287 status = reg_read(&regs->stat);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200288 /* Wait until the TC (Transfer completed) bit is set */
Heiko Schocherb77c8882014-07-14 10:22:11 +0200289 while ((status & MXC_CSPICTRL_TC) == 0) {
290 if (get_timer(ts) > CONFIG_SYS_SPI_MXC_WAIT) {
291 printf("spi_xchg_single: Timeout!\n");
292 return -1;
293 }
294 status = reg_read(&regs->stat);
295 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200296
Stefano Babic6e6f4552010-04-04 22:43:38 +0200297 /* Transfer completed, clear any pending request */
Stefano Babic28580452011-01-19 22:46:33 +0000298 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200299
Axel Linfb7def92013-06-14 21:13:32 +0800300 nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200301
Stefano Babic125f82a2010-08-20 12:05:03 +0200302 cnt = nbytes % 32;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100303
Stefano Babic125f82a2010-08-20 12:05:03 +0200304 if (bitlen % 32) {
Stefano Babic28580452011-01-19 22:46:33 +0000305 data = reg_read(&regs->rxdata);
Stefano Babic125f82a2010-08-20 12:05:03 +0200306 cnt = (bitlen % 32) / 8;
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000307 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
Stefano Babic125f82a2010-08-20 12:05:03 +0200308 debug("SPI Rx unaligned: 0x%x\n", data);
309 if (din) {
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000310 memcpy(din, &data, cnt);
311 din += cnt;
Stefano Babic125f82a2010-08-20 12:05:03 +0200312 }
313 nbytes -= cnt;
314 }
315
316 while (nbytes > 0) {
317 u32 tmp;
Stefano Babic28580452011-01-19 22:46:33 +0000318 tmp = reg_read(&regs->rxdata);
Stefano Babic125f82a2010-08-20 12:05:03 +0200319 data = cpu_to_be32(tmp);
320 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
321 cnt = min(nbytes, sizeof(data));
322 if (din) {
323 memcpy(din, &data, cnt);
324 din += cnt;
325 }
326 nbytes -= cnt;
327 }
328
329 return 0;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200330
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200331}
332
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200333int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
334 void *din, unsigned long flags)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200335{
Axel Linfb7def92013-06-14 21:13:32 +0800336 int n_bytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic125f82a2010-08-20 12:05:03 +0200337 int n_bits;
338 int ret;
339 u32 blk_size;
340 u8 *p_outbuf = (u8 *)dout;
341 u8 *p_inbuf = (u8 *)din;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200342
Stefano Babic125f82a2010-08-20 12:05:03 +0200343 if (!slave)
344 return -1;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200345
Stefano Babic125f82a2010-08-20 12:05:03 +0200346 if (flags & SPI_XFER_BEGIN)
347 spi_cs_activate(slave);
Magnus Lilja1858a9a2010-02-09 22:05:39 +0100348
Stefano Babic125f82a2010-08-20 12:05:03 +0200349 while (n_bytes > 0) {
Stefano Babic125f82a2010-08-20 12:05:03 +0200350 if (n_bytes < MAX_SPI_BYTES)
351 blk_size = n_bytes;
352 else
353 blk_size = MAX_SPI_BYTES;
354
355 n_bits = blk_size * 8;
356
357 ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0);
358
359 if (ret)
360 return ret;
361 if (dout)
362 p_outbuf += blk_size;
363 if (din)
364 p_inbuf += blk_size;
365 n_bytes -= blk_size;
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100366 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200367
Stefano Babic125f82a2010-08-20 12:05:03 +0200368 if (flags & SPI_XFER_END) {
369 spi_cs_deactivate(slave);
370 }
371
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200372 return 0;
373}
374
375void spi_init(void)
376{
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100377}
378
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300379/*
380 * Some SPI devices require active chip-select over multiple
381 * transactions, we achieve this using a GPIO. Still, the SPI
382 * controller has to be configured to use one of its own chipselects.
383 * To use this feature you have to implement board_spi_cs_gpio() to assign
384 * a gpio value for each cs (-1 if cs doesn't need to use gpio).
385 * You must use some unused on this SPI controller cs between 0 and 3.
386 */
387static int setup_cs_gpio(struct mxc_spi_slave *mxcs,
388 unsigned int bus, unsigned int cs)
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100389{
390 int ret;
391
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300392 mxcs->gpio = board_spi_cs_gpio(bus, cs);
393 if (mxcs->gpio == -1)
394 return 0;
395
396 ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
397 if (ret) {
398 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
399 return -EINVAL;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100400 }
401
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300402 return 0;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200403}
404
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200405struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
406 unsigned int max_hz, unsigned int mode)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200407{
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200408 struct mxc_spi_slave *mxcs;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100409 int ret;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200410
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100411 if (bus >= ARRAY_SIZE(spi_bases))
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200412 return NULL;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200413
Simon Glassd034a952013-03-18 19:23:40 +0000414 mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
Stefano Babic125f82a2010-08-20 12:05:03 +0200415 if (!mxcs) {
416 puts("mxc_spi: SPI Slave not allocated !\n");
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100417 return NULL;
Stefano Babic125f82a2010-08-20 12:05:03 +0200418 }
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100419
Fabio Estevam17cd2a82012-11-15 11:23:23 +0000420 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
421
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300422 ret = setup_cs_gpio(mxcs, bus, cs);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100423 if (ret < 0) {
424 free(mxcs);
425 return NULL;
426 }
427
Stefano Babic6e6f4552010-04-04 22:43:38 +0200428 mxcs->base = spi_bases[bus];
429
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000430 ret = spi_cfg_mxc(mxcs, cs, max_hz, mode);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200431 if (ret) {
432 printf("mxc_spi: cannot setup SPI controller\n");
433 free(mxcs);
434 return NULL;
435 }
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200436 return &mxcs->slave;
437}
438
439void spi_free_slave(struct spi_slave *slave)
440{
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100441 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
442
443 free(mxcs);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200444}
445
446int spi_claim_bus(struct spi_slave *slave)
447{
448 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
Stefano Babic28580452011-01-19 22:46:33 +0000449 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200450
Stefano Babic28580452011-01-19 22:46:33 +0000451 reg_write(&regs->rxdata, 1);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200452 udelay(1);
Stefano Babic28580452011-01-19 22:46:33 +0000453 reg_write(&regs->ctrl, mxcs->ctrl_reg);
454 reg_write(&regs->period, MXC_CSPIPERIOD_32KHZ);
455 reg_write(&regs->intr, 0);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200456
457 return 0;
458}
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200459
460void spi_release_bus(struct spi_slave *slave)
461{
462 /* TODO: Shut the controller down */
463}