blob: be102692d446450528886c998440ecefc8a32fbf [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>
Eric Nelson3acb0112014-09-30 15:40:03 -070015#include <asm/imx-common/spi.h>
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020016
17#ifdef CONFIG_MX27
18/* i.MX27 has a completely wrong register layout and register definitions in the
19 * datasheet, the correct one is in the Freescale's Linux driver */
20
Helmut Raiger61a58a12011-06-15 01:45:45 +000021#error "i.MX27 CSPI not supported due to drastic differences in register definitions" \
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020022"See linux mxc_spi driver from Freescale for details."
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020023#endif
24
Eric Nelson08c61a52012-01-31 07:52:03 +000025static unsigned long spi_bases[] = {
26 MXC_SPI_BASE_ADDRESSES
27};
28
Nikita Kiryanov155fa9a2014-08-20 15:08:50 +030029__weak int board_spi_cs_gpio(unsigned bus, unsigned cs)
30{
31 return -1;
32}
33
Stefano Babicc4ea1422010-07-06 17:05:06 +020034#define OUT MXC_GPIO_DIRECTION_OUT
35
Stefano Babicac87c172011-01-19 22:46:33 +000036#define reg_read readl
37#define reg_write(a, v) writel(v, a)
38
Heiko Schocherf659b572014-07-14 10:22:11 +020039#if !defined(CONFIG_SYS_SPI_MXC_WAIT)
40#define CONFIG_SYS_SPI_MXC_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
41#endif
42
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020043struct mxc_spi_slave {
44 struct spi_slave slave;
45 unsigned long base;
46 u32 ctrl_reg;
Eric Nelson08c61a52012-01-31 07:52:03 +000047#if defined(MXC_ECSPI)
Stefano Babicd205ddc2010-04-04 22:43:38 +020048 u32 cfg_reg;
49#endif
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +010050 int gpio;
Stefano Babicc4ea1422010-07-06 17:05:06 +020051 int ss_pol;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020052};
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020053
54static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
55{
56 return container_of(slave, struct mxc_spi_slave, slave);
57}
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020058
Stefano Babicd205ddc2010-04-04 22:43:38 +020059void spi_cs_activate(struct spi_slave *slave)
60{
61 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
62 if (mxcs->gpio > 0)
Stefano Babicd8e0ca82011-08-21 10:45:44 +020063 gpio_set_value(mxcs->gpio, mxcs->ss_pol);
Stefano Babicd205ddc2010-04-04 22:43:38 +020064}
65
66void spi_cs_deactivate(struct spi_slave *slave)
67{
68 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
69 if (mxcs->gpio > 0)
Stefano Babicd8e0ca82011-08-21 10:45:44 +020070 gpio_set_value(mxcs->gpio,
Stefano Babicc4ea1422010-07-06 17:05:06 +020071 !(mxcs->ss_pol));
Stefano Babicd205ddc2010-04-04 22:43:38 +020072}
73
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +000074u32 get_cspi_div(u32 div)
75{
76 int i;
77
78 for (i = 0; i < 8; i++) {
79 if (div <= (4 << i))
80 return i;
81 }
82 return i;
83}
84
Eric Nelson08c61a52012-01-31 07:52:03 +000085#ifdef MXC_CSPI
Stefano Babicc9d59c72011-01-19 22:46:30 +000086static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
87 unsigned int max_hz, unsigned int mode)
88{
89 unsigned int ctrl_reg;
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +000090 u32 clk_src;
91 u32 div;
92
93 clk_src = mxc_get_clock(MXC_CSPI_CLK);
94
Benoît Thébaudeaucd200402012-08-10 08:51:50 +000095 div = DIV_ROUND_UP(clk_src, max_hz);
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +000096 div = get_cspi_div(div);
97
98 debug("clk %d Hz, div %d, real clk %d Hz\n",
99 max_hz, div, clk_src / (4 << div));
Stefano Babicc9d59c72011-01-19 22:46:30 +0000100
101 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
102 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +0000103 MXC_CSPICTRL_DATARATE(div) |
Stefano Babicc9d59c72011-01-19 22:46:30 +0000104 MXC_CSPICTRL_EN |
105#ifdef CONFIG_MX35
106 MXC_CSPICTRL_SSCTL |
107#endif
108 MXC_CSPICTRL_MODE;
109
110 if (mode & SPI_CPHA)
111 ctrl_reg |= MXC_CSPICTRL_PHA;
112 if (mode & SPI_CPOL)
113 ctrl_reg |= MXC_CSPICTRL_POL;
114 if (mode & SPI_CS_HIGH)
115 ctrl_reg |= MXC_CSPICTRL_SSPOL;
116 mxcs->ctrl_reg = ctrl_reg;
117
118 return 0;
119}
120#endif
121
Eric Nelson08c61a52012-01-31 07:52:03 +0000122#ifdef MXC_ECSPI
Stefano Babicc9d59c72011-01-19 22:46:30 +0000123static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
Stefano Babicd205ddc2010-04-04 22:43:38 +0200124 unsigned int max_hz, unsigned int mode)
125{
126 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
Dirk Behme9a309032013-05-11 07:25:54 +0200127 s32 reg_ctrl, reg_config;
Markus Niebel5d584cc2014-02-17 17:33:17 +0100128 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0;
129 u32 pre_div = 0, post_div = 0;
Stefano Babicac87c172011-01-19 22:46:33 +0000130 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200131
132 if (max_hz == 0) {
133 printf("Error: desired clock is 0\n");
134 return -1;
135 }
136
Fabio Estevam0f1411b2013-04-09 13:06:25 +0000137 /*
138 * Reset SPI and set all CSs to master mode, if toggling
139 * between slave and master mode we might see a glitch
140 * on the clock line
141 */
142 reg_ctrl = MXC_CSPICTRL_MODE_MASK;
143 reg_write(&regs->ctrl, reg_ctrl);
144 reg_ctrl |= MXC_CSPICTRL_EN;
145 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200146
Stefano Babicd205ddc2010-04-04 22:43:38 +0200147 if (clk_src > max_hz) {
Dirk Behme9a309032013-05-11 07:25:54 +0200148 pre_div = (clk_src - 1) / max_hz;
149 /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
150 post_div = fls(pre_div);
151 if (post_div > 4) {
152 post_div -= 4;
153 if (post_div >= 16) {
Stefano Babicd205ddc2010-04-04 22:43:38 +0200154 printf("Error: no divider for the freq: %d\n",
155 max_hz);
156 return -1;
157 }
Dirk Behme9a309032013-05-11 07:25:54 +0200158 pre_div >>= post_div;
159 } else {
160 post_div = 0;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200161 }
162 }
163
164 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
165 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
166 MXC_CSPICTRL_SELCHAN(cs);
167 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
168 MXC_CSPICTRL_PREDIV(pre_div);
169 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
170 MXC_CSPICTRL_POSTDIV(post_div);
171
Stefano Babicd205ddc2010-04-04 22:43:38 +0200172 /* We need to disable SPI before changing registers */
173 reg_ctrl &= ~MXC_CSPICTRL_EN;
174
175 if (mode & SPI_CS_HIGH)
176 ss_pol = 1;
177
Markus Niebel5d584cc2014-02-17 17:33:17 +0100178 if (mode & SPI_CPOL) {
Stefano Babicd205ddc2010-04-04 22:43:38 +0200179 sclkpol = 1;
Markus Niebel5d584cc2014-02-17 17:33:17 +0100180 sclkctl = 1;
181 }
Stefano Babicd205ddc2010-04-04 22:43:38 +0200182
183 if (mode & SPI_CPHA)
184 sclkpha = 1;
185
Stefano Babicac87c172011-01-19 22:46:33 +0000186 reg_config = reg_read(&regs->cfg);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200187
188 /*
189 * Configuration register setup
Stefano Babicc9d59c72011-01-19 22:46:30 +0000190 * The MX51 supports different setup for each SS
Stefano Babicd205ddc2010-04-04 22:43:38 +0200191 */
192 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
193 (ss_pol << (cs + MXC_CSPICON_SSPOL));
194 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
195 (sclkpol << (cs + MXC_CSPICON_POL));
Markus Niebel5d584cc2014-02-17 17:33:17 +0100196 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_CTL))) |
197 (sclkctl << (cs + MXC_CSPICON_CTL));
Stefano Babicd205ddc2010-04-04 22:43:38 +0200198 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
199 (sclkpha << (cs + MXC_CSPICON_PHA));
200
201 debug("reg_ctrl = 0x%x\n", reg_ctrl);
Stefano Babicac87c172011-01-19 22:46:33 +0000202 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200203 debug("reg_config = 0x%x\n", reg_config);
Stefano Babicac87c172011-01-19 22:46:33 +0000204 reg_write(&regs->cfg, reg_config);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200205
206 /* save config register and control register */
207 mxcs->ctrl_reg = reg_ctrl;
208 mxcs->cfg_reg = reg_config;
209
210 /* clear interrupt reg */
Stefano Babicac87c172011-01-19 22:46:33 +0000211 reg_write(&regs->intr, 0);
212 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200213
214 return 0;
215}
216#endif
217
Stefano Babic2f721d12010-08-20 12:05:03 +0200218int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen,
219 const u8 *dout, u8 *din, unsigned long flags)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200220{
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200221 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
Axel Lin9675fed2013-06-14 21:13:32 +0800222 int nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200223 u32 data, cnt, i;
Stefano Babicac87c172011-01-19 22:46:33 +0000224 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Heiko Schocherf659b572014-07-14 10:22:11 +0200225 u32 ts;
226 int status;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200227
Stefano Babic2f721d12010-08-20 12:05:03 +0200228 debug("%s: bitlen %d dout 0x%x din 0x%x\n",
229 __func__, bitlen, (u32)dout, (u32)din);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200230
231 mxcs->ctrl_reg = (mxcs->ctrl_reg &
232 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100233 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
234
Stefano Babicac87c172011-01-19 22:46:33 +0000235 reg_write(&regs->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
Eric Nelson08c61a52012-01-31 07:52:03 +0000236#ifdef MXC_ECSPI
Stefano Babicac87c172011-01-19 22:46:33 +0000237 reg_write(&regs->cfg, mxcs->cfg_reg);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200238#endif
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200239
Stefano Babicd205ddc2010-04-04 22:43:38 +0200240 /* Clear interrupt register */
Stefano Babicac87c172011-01-19 22:46:33 +0000241 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100242
Stefano Babic2f721d12010-08-20 12:05:03 +0200243 /*
244 * The SPI controller works only with words,
245 * check if less than a word is sent.
246 * Access to the FIFO is only 32 bit
247 */
248 if (bitlen % 32) {
249 data = 0;
250 cnt = (bitlen % 32) / 8;
251 if (dout) {
252 for (i = 0; i < cnt; i++) {
253 data = (data << 8) | (*dout++ & 0xFF);
254 }
255 }
256 debug("Sending SPI 0x%x\n", data);
257
Stefano Babicac87c172011-01-19 22:46:33 +0000258 reg_write(&regs->txdata, data);
Stefano Babic2f721d12010-08-20 12:05:03 +0200259 nbytes -= cnt;
260 }
261
262 data = 0;
263
264 while (nbytes > 0) {
265 data = 0;
266 if (dout) {
267 /* Buffer is not 32-bit aligned */
268 if ((unsigned long)dout & 0x03) {
269 data = 0;
Anatolij Gustschindff01092011-01-20 07:53:06 +0000270 for (i = 0; i < 4; i++)
Stefano Babic2f721d12010-08-20 12:05:03 +0200271 data = (data << 8) | (*dout++ & 0xFF);
Stefano Babic2f721d12010-08-20 12:05:03 +0200272 } else {
273 data = *(u32 *)dout;
274 data = cpu_to_be32(data);
Timo Herbrecher6d5ce1b2013-10-16 00:05:09 +0530275 dout += 4;
Stefano Babic2f721d12010-08-20 12:05:03 +0200276 }
Stefano Babic2f721d12010-08-20 12:05:03 +0200277 }
278 debug("Sending SPI 0x%x\n", data);
Stefano Babicac87c172011-01-19 22:46:33 +0000279 reg_write(&regs->txdata, data);
Stefano Babic2f721d12010-08-20 12:05:03 +0200280 nbytes -= 4;
281 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200282
Stefano Babicd205ddc2010-04-04 22:43:38 +0200283 /* FIFO is written, now starts the transfer setting the XCH bit */
Stefano Babicac87c172011-01-19 22:46:33 +0000284 reg_write(&regs->ctrl, mxcs->ctrl_reg |
Stefano Babicd205ddc2010-04-04 22:43:38 +0200285 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200286
Heiko Schocherf659b572014-07-14 10:22:11 +0200287 ts = get_timer(0);
288 status = reg_read(&regs->stat);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200289 /* Wait until the TC (Transfer completed) bit is set */
Heiko Schocherf659b572014-07-14 10:22:11 +0200290 while ((status & MXC_CSPICTRL_TC) == 0) {
291 if (get_timer(ts) > CONFIG_SYS_SPI_MXC_WAIT) {
292 printf("spi_xchg_single: Timeout!\n");
293 return -1;
294 }
295 status = reg_read(&regs->stat);
296 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200297
Stefano Babicd205ddc2010-04-04 22:43:38 +0200298 /* Transfer completed, clear any pending request */
Stefano Babicac87c172011-01-19 22:46:33 +0000299 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100300
Axel Lin9675fed2013-06-14 21:13:32 +0800301 nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200302
Stefano Babic2f721d12010-08-20 12:05:03 +0200303 cnt = nbytes % 32;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200304
Stefano Babic2f721d12010-08-20 12:05:03 +0200305 if (bitlen % 32) {
Stefano Babicac87c172011-01-19 22:46:33 +0000306 data = reg_read(&regs->rxdata);
Stefano Babic2f721d12010-08-20 12:05:03 +0200307 cnt = (bitlen % 32) / 8;
Anatolij Gustschindff01092011-01-20 07:53:06 +0000308 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200309 debug("SPI Rx unaligned: 0x%x\n", data);
310 if (din) {
Anatolij Gustschindff01092011-01-20 07:53:06 +0000311 memcpy(din, &data, cnt);
312 din += cnt;
Stefano Babic2f721d12010-08-20 12:05:03 +0200313 }
314 nbytes -= cnt;
315 }
316
317 while (nbytes > 0) {
318 u32 tmp;
Stefano Babicac87c172011-01-19 22:46:33 +0000319 tmp = reg_read(&regs->rxdata);
Stefano Babic2f721d12010-08-20 12:05:03 +0200320 data = cpu_to_be32(tmp);
321 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
322 cnt = min(nbytes, sizeof(data));
323 if (din) {
324 memcpy(din, &data, cnt);
325 din += cnt;
326 }
327 nbytes -= cnt;
328 }
329
330 return 0;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200331
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200332}
333
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200334int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
335 void *din, unsigned long flags)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200336{
Axel Lin9675fed2013-06-14 21:13:32 +0800337 int n_bytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200338 int n_bits;
339 int ret;
340 u32 blk_size;
341 u8 *p_outbuf = (u8 *)dout;
342 u8 *p_inbuf = (u8 *)din;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200343
Stefano Babic2f721d12010-08-20 12:05:03 +0200344 if (!slave)
345 return -1;
346
347 if (flags & SPI_XFER_BEGIN)
348 spi_cs_activate(slave);
349
350 while (n_bytes > 0) {
Stefano Babic2f721d12010-08-20 12:05:03 +0200351 if (n_bytes < MAX_SPI_BYTES)
352 blk_size = n_bytes;
353 else
354 blk_size = MAX_SPI_BYTES;
355
356 n_bits = blk_size * 8;
357
358 ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0);
359
360 if (ret)
361 return ret;
362 if (dout)
363 p_outbuf += blk_size;
364 if (din)
365 p_inbuf += blk_size;
366 n_bytes -= blk_size;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200367 }
368
Stefano Babic2f721d12010-08-20 12:05:03 +0200369 if (flags & SPI_XFER_END) {
370 spi_cs_deactivate(slave);
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100371 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200372
373 return 0;
374}
375
376void spi_init(void)
377{
378}
379
Nikita Kiryanov155fa9a2014-08-20 15:08:50 +0300380/*
381 * Some SPI devices require active chip-select over multiple
382 * transactions, we achieve this using a GPIO. Still, the SPI
383 * controller has to be configured to use one of its own chipselects.
384 * To use this feature you have to implement board_spi_cs_gpio() to assign
385 * a gpio value for each cs (-1 if cs doesn't need to use gpio).
386 * You must use some unused on this SPI controller cs between 0 and 3.
387 */
388static int setup_cs_gpio(struct mxc_spi_slave *mxcs,
389 unsigned int bus, unsigned int cs)
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100390{
391 int ret;
392
Nikita Kiryanov155fa9a2014-08-20 15:08:50 +0300393 mxcs->gpio = board_spi_cs_gpio(bus, cs);
394 if (mxcs->gpio == -1)
395 return 0;
396
397 ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
398 if (ret) {
399 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
400 return -EINVAL;
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100401 }
402
Nikita Kiryanov155fa9a2014-08-20 15:08:50 +0300403 return 0;
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100404}
405
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200406struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
407 unsigned int max_hz, unsigned int mode)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200408{
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200409 struct mxc_spi_slave *mxcs;
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100410 int ret;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200411
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100412 if (bus >= ARRAY_SIZE(spi_bases))
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200413 return NULL;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200414
Simon Glassd3504fe2013-03-18 19:23:40 +0000415 mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
Stefano Babic2f721d12010-08-20 12:05:03 +0200416 if (!mxcs) {
417 puts("mxc_spi: SPI Slave not allocated !\n");
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100418 return NULL;
Stefano Babic2f721d12010-08-20 12:05:03 +0200419 }
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100420
Fabio Estevamde5bf022012-11-15 11:23:23 +0000421 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
422
Nikita Kiryanov155fa9a2014-08-20 15:08:50 +0300423 ret = setup_cs_gpio(mxcs, bus, cs);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100424 if (ret < 0) {
425 free(mxcs);
426 return NULL;
427 }
428
Stefano Babicd205ddc2010-04-04 22:43:38 +0200429 mxcs->base = spi_bases[bus];
430
Stefano Babicc9d59c72011-01-19 22:46:30 +0000431 ret = spi_cfg_mxc(mxcs, cs, max_hz, mode);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200432 if (ret) {
433 printf("mxc_spi: cannot setup SPI controller\n");
434 free(mxcs);
435 return NULL;
436 }
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200437 return &mxcs->slave;
438}
439
440void spi_free_slave(struct spi_slave *slave)
441{
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100442 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
443
444 free(mxcs);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200445}
446
447int spi_claim_bus(struct spi_slave *slave)
448{
449 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
Stefano Babicac87c172011-01-19 22:46:33 +0000450 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200451
Stefano Babicac87c172011-01-19 22:46:33 +0000452 reg_write(&regs->rxdata, 1);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200453 udelay(1);
Stefano Babicac87c172011-01-19 22:46:33 +0000454 reg_write(&regs->ctrl, mxcs->ctrl_reg);
455 reg_write(&regs->period, MXC_CSPIPERIOD_32KHZ);
456 reg_write(&regs->intr, 0);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200457
458 return 0;
459}
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200460
461void spi_release_bus(struct spi_slave *slave)
462{
463 /* TODO: Shut the controller down */
464}