blob: 684676271997b50e198506581e3e9ebd388ed268 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +02002/*
3 * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de>
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +02004 */
5
6#include <common.h>
Peng Fan994266b2017-08-09 13:09:33 +08007#include <dm.h>
Haavard Skinnemoend255bb02008-05-16 11:10:31 +02008#include <malloc.h>
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +02009#include <spi.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090010#include <linux/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>
Stefano Babic552a8482017-06-29 10:16:06 +020015#include <asm/mach-imx/spi.h>
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020016
Peng Fan994266b2017-08-09 13:09:33 +080017DECLARE_GLOBAL_DATA_PTR;
18
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020019#ifdef CONFIG_MX27
20/* i.MX27 has a completely wrong register layout and register definitions in the
21 * datasheet, the correct one is in the Freescale's Linux driver */
22
Helmut Raiger61a58a12011-06-15 01:45:45 +000023#error "i.MX27 CSPI not supported due to drastic differences in register definitions" \
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020024"See linux mxc_spi driver from Freescale for details."
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020025#endif
26
Nikita Kiryanov155fa9a2014-08-20 15:08:50 +030027__weak int board_spi_cs_gpio(unsigned bus, unsigned cs)
28{
29 return -1;
30}
31
Stefano Babicc4ea1422010-07-06 17:05:06 +020032#define OUT MXC_GPIO_DIRECTION_OUT
33
Stefano Babicac87c172011-01-19 22:46:33 +000034#define reg_read readl
35#define reg_write(a, v) writel(v, a)
36
Heiko Schocherf659b572014-07-14 10:22:11 +020037#if !defined(CONFIG_SYS_SPI_MXC_WAIT)
38#define CONFIG_SYS_SPI_MXC_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
39#endif
40
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020041struct mxc_spi_slave {
42 struct spi_slave slave;
43 unsigned long base;
44 u32 ctrl_reg;
Eric Nelson08c61a52012-01-31 07:52:03 +000045#if defined(MXC_ECSPI)
Stefano Babicd205ddc2010-04-04 22:43:38 +020046 u32 cfg_reg;
47#endif
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +010048 int gpio;
Stefano Babicc4ea1422010-07-06 17:05:06 +020049 int ss_pol;
Markus Niebel027a9a02014-10-23 16:09:39 +020050 unsigned int max_hz;
51 unsigned int mode;
Peng Fan994266b2017-08-09 13:09:33 +080052 struct gpio_desc ss;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020053};
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020054
55static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
56{
57 return container_of(slave, struct mxc_spi_slave, slave);
58}
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020059
Peng Fan994266b2017-08-09 13:09:33 +080060static void mxc_spi_cs_activate(struct mxc_spi_slave *mxcs)
Stefano Babicd205ddc2010-04-04 22:43:38 +020061{
Peng Fan994266b2017-08-09 13:09:33 +080062 if (CONFIG_IS_ENABLED(DM_SPI)) {
Michael Trimarchi34ad7492018-06-20 22:51:18 +020063 dm_gpio_set_value(&mxcs->ss, 1);
Peng Fan994266b2017-08-09 13:09:33 +080064 } else {
65 if (mxcs->gpio > 0)
66 gpio_set_value(mxcs->gpio, mxcs->ss_pol);
67 }
Stefano Babicd205ddc2010-04-04 22:43:38 +020068}
69
Peng Fan994266b2017-08-09 13:09:33 +080070static void mxc_spi_cs_deactivate(struct mxc_spi_slave *mxcs)
Stefano Babicd205ddc2010-04-04 22:43:38 +020071{
Peng Fan994266b2017-08-09 13:09:33 +080072 if (CONFIG_IS_ENABLED(DM_SPI)) {
Michael Trimarchi34ad7492018-06-20 22:51:18 +020073 dm_gpio_set_value(&mxcs->ss, 0);
Peng Fan994266b2017-08-09 13:09:33 +080074 } else {
75 if (mxcs->gpio > 0)
76 gpio_set_value(mxcs->gpio, !(mxcs->ss_pol));
77 }
Stefano Babicd205ddc2010-04-04 22:43:38 +020078}
79
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +000080u32 get_cspi_div(u32 div)
81{
82 int i;
83
84 for (i = 0; i < 8; i++) {
85 if (div <= (4 << i))
86 return i;
87 }
88 return i;
89}
90
Eric Nelson08c61a52012-01-31 07:52:03 +000091#ifdef MXC_CSPI
Markus Niebel027a9a02014-10-23 16:09:39 +020092static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
Stefano Babicc9d59c72011-01-19 22:46:30 +000093{
94 unsigned int ctrl_reg;
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +000095 u32 clk_src;
96 u32 div;
Markus Niebel027a9a02014-10-23 16:09:39 +020097 unsigned int max_hz = mxcs->max_hz;
98 unsigned int mode = mxcs->mode;
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +000099
100 clk_src = mxc_get_clock(MXC_CSPI_CLK);
101
Benoît Thébaudeaucd200402012-08-10 08:51:50 +0000102 div = DIV_ROUND_UP(clk_src, max_hz);
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +0000103 div = get_cspi_div(div);
104
105 debug("clk %d Hz, div %d, real clk %d Hz\n",
106 max_hz, div, clk_src / (4 << div));
Stefano Babicc9d59c72011-01-19 22:46:30 +0000107
108 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
109 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +0000110 MXC_CSPICTRL_DATARATE(div) |
Stefano Babicc9d59c72011-01-19 22:46:30 +0000111 MXC_CSPICTRL_EN |
112#ifdef CONFIG_MX35
113 MXC_CSPICTRL_SSCTL |
114#endif
115 MXC_CSPICTRL_MODE;
116
117 if (mode & SPI_CPHA)
118 ctrl_reg |= MXC_CSPICTRL_PHA;
119 if (mode & SPI_CPOL)
120 ctrl_reg |= MXC_CSPICTRL_POL;
121 if (mode & SPI_CS_HIGH)
122 ctrl_reg |= MXC_CSPICTRL_SSPOL;
123 mxcs->ctrl_reg = ctrl_reg;
124
125 return 0;
126}
127#endif
128
Eric Nelson08c61a52012-01-31 07:52:03 +0000129#ifdef MXC_ECSPI
Markus Niebel027a9a02014-10-23 16:09:39 +0200130static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
Stefano Babicd205ddc2010-04-04 22:43:38 +0200131{
132 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
Dirk Behme9a309032013-05-11 07:25:54 +0200133 s32 reg_ctrl, reg_config;
Markus Niebel5d584cc2014-02-17 17:33:17 +0100134 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0;
135 u32 pre_div = 0, post_div = 0;
Stefano Babicac87c172011-01-19 22:46:33 +0000136 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Markus Niebel027a9a02014-10-23 16:09:39 +0200137 unsigned int max_hz = mxcs->max_hz;
138 unsigned int mode = mxcs->mode;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200139
Fabio Estevam0f1411b2013-04-09 13:06:25 +0000140 /*
141 * Reset SPI and set all CSs to master mode, if toggling
142 * between slave and master mode we might see a glitch
143 * on the clock line
144 */
145 reg_ctrl = MXC_CSPICTRL_MODE_MASK;
146 reg_write(&regs->ctrl, reg_ctrl);
147 reg_ctrl |= MXC_CSPICTRL_EN;
148 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200149
Stefano Babicd205ddc2010-04-04 22:43:38 +0200150 if (clk_src > max_hz) {
Dirk Behme9a309032013-05-11 07:25:54 +0200151 pre_div = (clk_src - 1) / max_hz;
152 /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
153 post_div = fls(pre_div);
154 if (post_div > 4) {
155 post_div -= 4;
156 if (post_div >= 16) {
Stefano Babicd205ddc2010-04-04 22:43:38 +0200157 printf("Error: no divider for the freq: %d\n",
158 max_hz);
159 return -1;
160 }
Dirk Behme9a309032013-05-11 07:25:54 +0200161 pre_div >>= post_div;
162 } else {
163 post_div = 0;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200164 }
165 }
166
167 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
168 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
169 MXC_CSPICTRL_SELCHAN(cs);
170 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
171 MXC_CSPICTRL_PREDIV(pre_div);
172 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
173 MXC_CSPICTRL_POSTDIV(post_div);
174
Stefano Babicd205ddc2010-04-04 22:43:38 +0200175 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
Peng Fan994266b2017-08-09 13:09:33 +0800218int spi_xchg_single(struct mxc_spi_slave *mxcs, unsigned int bitlen,
Stefano Babic2f721d12010-08-20 12:05:03 +0200219 const u8 *dout, u8 *din, unsigned long flags)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200220{
Axel Lin9675fed2013-06-14 21:13:32 +0800221 int nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200222 u32 data, cnt, i;
Stefano Babicac87c172011-01-19 22:46:33 +0000223 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Heiko Schocherf659b572014-07-14 10:22:11 +0200224 u32 ts;
225 int status;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200226
Ye Li65a106e2019-01-04 09:26:00 +0000227 debug("%s: bitlen %d dout 0x%lx din 0x%lx\n",
228 __func__, bitlen, (ulong)dout, (ulong)din);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200229
230 mxcs->ctrl_reg = (mxcs->ctrl_reg &
231 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100232 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
233
Stefano Babicac87c172011-01-19 22:46:33 +0000234 reg_write(&regs->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
Eric Nelson08c61a52012-01-31 07:52:03 +0000235#ifdef MXC_ECSPI
Stefano Babicac87c172011-01-19 22:46:33 +0000236 reg_write(&regs->cfg, mxcs->cfg_reg);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200237#endif
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200238
Stefano Babicd205ddc2010-04-04 22:43:38 +0200239 /* Clear interrupt register */
Stefano Babicac87c172011-01-19 22:46:33 +0000240 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100241
Stefano Babic2f721d12010-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 Babicac87c172011-01-19 22:46:33 +0000257 reg_write(&regs->txdata, data);
Stefano Babic2f721d12010-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 Gustschindff01092011-01-20 07:53:06 +0000269 for (i = 0; i < 4; i++)
Stefano Babic2f721d12010-08-20 12:05:03 +0200270 data = (data << 8) | (*dout++ & 0xFF);
Stefano Babic2f721d12010-08-20 12:05:03 +0200271 } else {
272 data = *(u32 *)dout;
273 data = cpu_to_be32(data);
Timo Herbrecher6d5ce1b2013-10-16 00:05:09 +0530274 dout += 4;
Stefano Babic2f721d12010-08-20 12:05:03 +0200275 }
Stefano Babic2f721d12010-08-20 12:05:03 +0200276 }
277 debug("Sending SPI 0x%x\n", data);
Stefano Babicac87c172011-01-19 22:46:33 +0000278 reg_write(&regs->txdata, data);
Stefano Babic2f721d12010-08-20 12:05:03 +0200279 nbytes -= 4;
280 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200281
Stefano Babicd205ddc2010-04-04 22:43:38 +0200282 /* FIFO is written, now starts the transfer setting the XCH bit */
Stefano Babicac87c172011-01-19 22:46:33 +0000283 reg_write(&regs->ctrl, mxcs->ctrl_reg |
Stefano Babicd205ddc2010-04-04 22:43:38 +0200284 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200285
Heiko Schocherf659b572014-07-14 10:22:11 +0200286 ts = get_timer(0);
287 status = reg_read(&regs->stat);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200288 /* Wait until the TC (Transfer completed) bit is set */
Heiko Schocherf659b572014-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 Liakhovetski38254f42008-04-15 14:14:25 +0200296
Stefano Babicd205ddc2010-04-04 22:43:38 +0200297 /* Transfer completed, clear any pending request */
Stefano Babicac87c172011-01-19 22:46:33 +0000298 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100299
Axel Lin9675fed2013-06-14 21:13:32 +0800300 nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200301
Stefano Babic2f721d12010-08-20 12:05:03 +0200302 cnt = nbytes % 32;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200303
Stefano Babic2f721d12010-08-20 12:05:03 +0200304 if (bitlen % 32) {
Stefano Babicac87c172011-01-19 22:46:33 +0000305 data = reg_read(&regs->rxdata);
Stefano Babic2f721d12010-08-20 12:05:03 +0200306 cnt = (bitlen % 32) / 8;
Anatolij Gustschindff01092011-01-20 07:53:06 +0000307 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200308 debug("SPI Rx unaligned: 0x%x\n", data);
309 if (din) {
Anatolij Gustschindff01092011-01-20 07:53:06 +0000310 memcpy(din, &data, cnt);
311 din += cnt;
Stefano Babic2f721d12010-08-20 12:05:03 +0200312 }
313 nbytes -= cnt;
314 }
315
316 while (nbytes > 0) {
317 u32 tmp;
Stefano Babicac87c172011-01-19 22:46:33 +0000318 tmp = reg_read(&regs->rxdata);
Stefano Babic2f721d12010-08-20 12:05:03 +0200319 data = cpu_to_be32(tmp);
320 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
Masahiro Yamadab4141192014-11-07 03:03:31 +0900321 cnt = min_t(u32, nbytes, sizeof(data));
Stefano Babic2f721d12010-08-20 12:05:03 +0200322 if (din) {
323 memcpy(din, &data, cnt);
324 din += cnt;
325 }
326 nbytes -= cnt;
327 }
328
329 return 0;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200330
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200331}
332
Peng Fan994266b2017-08-09 13:09:33 +0800333static int mxc_spi_xfer_internal(struct mxc_spi_slave *mxcs,
334 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
Peng Fan994266b2017-08-09 13:09:33 +0800344 if (!mxcs)
345 return -EINVAL;
Stefano Babic2f721d12010-08-20 12:05:03 +0200346
347 if (flags & SPI_XFER_BEGIN)
Peng Fan994266b2017-08-09 13:09:33 +0800348 mxc_spi_cs_activate(mxcs);
Stefano Babic2f721d12010-08-20 12:05:03 +0200349
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
Peng Fan994266b2017-08-09 13:09:33 +0800358 ret = spi_xchg_single(mxcs, n_bits, p_outbuf, p_inbuf, 0);
Stefano Babic2f721d12010-08-20 12:05:03 +0200359
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) {
Peng Fan994266b2017-08-09 13:09:33 +0800370 mxc_spi_cs_deactivate(mxcs);
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100371 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200372
373 return 0;
374}
375
Peng Fan994266b2017-08-09 13:09:33 +0800376static int mxc_spi_claim_bus_internal(struct mxc_spi_slave *mxcs, int cs)
377{
378 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
379 int ret;
380
381 reg_write(&regs->rxdata, 1);
382 udelay(1);
383 ret = spi_cfg_mxc(mxcs, cs);
384 if (ret) {
385 printf("mxc_spi: cannot setup SPI controller\n");
386 return ret;
387 }
388 reg_write(&regs->period, MXC_CSPIPERIOD_32KHZ);
389 reg_write(&regs->intr, 0);
390
391 return 0;
392}
393
394#ifndef CONFIG_DM_SPI
395int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
396 void *din, unsigned long flags)
397{
398 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
399
400 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
401}
402
Nikita Kiryanov155fa9a2014-08-20 15:08:50 +0300403/*
404 * Some SPI devices require active chip-select over multiple
405 * transactions, we achieve this using a GPIO. Still, the SPI
406 * controller has to be configured to use one of its own chipselects.
407 * To use this feature you have to implement board_spi_cs_gpio() to assign
408 * a gpio value for each cs (-1 if cs doesn't need to use gpio).
409 * You must use some unused on this SPI controller cs between 0 and 3.
410 */
411static int setup_cs_gpio(struct mxc_spi_slave *mxcs,
412 unsigned int bus, unsigned int cs)
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100413{
414 int ret;
415
Nikita Kiryanov155fa9a2014-08-20 15:08:50 +0300416 mxcs->gpio = board_spi_cs_gpio(bus, cs);
417 if (mxcs->gpio == -1)
418 return 0;
419
Peng Fan994266b2017-08-09 13:09:33 +0800420 gpio_request(mxcs->gpio, "spi-cs");
Nikita Kiryanov155fa9a2014-08-20 15:08:50 +0300421 ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
422 if (ret) {
423 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
424 return -EINVAL;
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100425 }
426
Nikita Kiryanov155fa9a2014-08-20 15:08:50 +0300427 return 0;
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100428}
429
Peng Fan994266b2017-08-09 13:09:33 +0800430static unsigned long spi_bases[] = {
431 MXC_SPI_BASE_ADDRESSES
432};
433
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200434struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
435 unsigned int max_hz, unsigned int mode)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200436{
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200437 struct mxc_spi_slave *mxcs;
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100438 int ret;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200439
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100440 if (bus >= ARRAY_SIZE(spi_bases))
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200441 return NULL;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200442
Markus Niebel027a9a02014-10-23 16:09:39 +0200443 if (max_hz == 0) {
444 printf("Error: desired clock is 0\n");
445 return NULL;
446 }
447
Simon Glassd3504fe2013-03-18 19:23:40 +0000448 mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
Stefano Babic2f721d12010-08-20 12:05:03 +0200449 if (!mxcs) {
450 puts("mxc_spi: SPI Slave not allocated !\n");
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100451 return NULL;
Stefano Babic2f721d12010-08-20 12:05:03 +0200452 }
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100453
Fabio Estevamde5bf022012-11-15 11:23:23 +0000454 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
455
Nikita Kiryanov155fa9a2014-08-20 15:08:50 +0300456 ret = setup_cs_gpio(mxcs, bus, cs);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100457 if (ret < 0) {
458 free(mxcs);
459 return NULL;
460 }
461
Stefano Babicd205ddc2010-04-04 22:43:38 +0200462 mxcs->base = spi_bases[bus];
Markus Niebel027a9a02014-10-23 16:09:39 +0200463 mxcs->max_hz = max_hz;
464 mxcs->mode = mode;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200465
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200466 return &mxcs->slave;
467}
468
469void spi_free_slave(struct spi_slave *slave)
470{
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100471 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
472
473 free(mxcs);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200474}
475
476int spi_claim_bus(struct spi_slave *slave)
477{
478 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
479
Peng Fan994266b2017-08-09 13:09:33 +0800480 return mxc_spi_claim_bus_internal(mxcs, slave->cs);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200481}
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200482
483void spi_release_bus(struct spi_slave *slave)
484{
485 /* TODO: Shut the controller down */
486}
Peng Fan994266b2017-08-09 13:09:33 +0800487#else
488
489static int mxc_spi_probe(struct udevice *bus)
490{
491 struct mxc_spi_slave *plat = bus->platdata;
492 struct mxc_spi_slave *mxcs = dev_get_platdata(bus);
493 int node = dev_of_offset(bus);
494 const void *blob = gd->fdt_blob;
495 int ret;
496
497 if (gpio_request_by_name(bus, "cs-gpios", 0, &plat->ss,
498 GPIOD_IS_OUT)) {
499 dev_err(bus, "No cs-gpios property\n");
500 return -EINVAL;
501 }
502
Michael Trimarchi618e8e22018-06-20 22:51:17 +0200503 plat->base = devfdt_get_addr(bus);
Peng Fan994266b2017-08-09 13:09:33 +0800504 if (plat->base == FDT_ADDR_T_NONE)
505 return -ENODEV;
506
Michael Trimarchi34ad7492018-06-20 22:51:18 +0200507 ret = dm_gpio_set_value(&plat->ss, 0);
Peng Fan994266b2017-08-09 13:09:33 +0800508 if (ret) {
509 dev_err(bus, "Setting cs error\n");
510 return ret;
511 }
512
513 mxcs->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
514 20000000);
515
516 return 0;
517}
518
519static int mxc_spi_xfer(struct udevice *dev, unsigned int bitlen,
520 const void *dout, void *din, unsigned long flags)
521{
522 struct mxc_spi_slave *mxcs = dev_get_platdata(dev->parent);
523
524
525 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
526}
527
528static int mxc_spi_claim_bus(struct udevice *dev)
529{
530 struct mxc_spi_slave *mxcs = dev_get_platdata(dev->parent);
531 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
532
533 return mxc_spi_claim_bus_internal(mxcs, slave_plat->cs);
534}
535
536static int mxc_spi_release_bus(struct udevice *dev)
537{
538 return 0;
539}
540
541static int mxc_spi_set_speed(struct udevice *bus, uint speed)
542{
543 /* Nothing to do */
544 return 0;
545}
546
547static int mxc_spi_set_mode(struct udevice *bus, uint mode)
548{
549 struct mxc_spi_slave *mxcs = dev_get_platdata(bus);
550
551 mxcs->mode = mode;
552 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
553
554 return 0;
555}
556
557static const struct dm_spi_ops mxc_spi_ops = {
558 .claim_bus = mxc_spi_claim_bus,
559 .release_bus = mxc_spi_release_bus,
560 .xfer = mxc_spi_xfer,
561 .set_speed = mxc_spi_set_speed,
562 .set_mode = mxc_spi_set_mode,
563};
564
565static const struct udevice_id mxc_spi_ids[] = {
566 { .compatible = "fsl,imx51-ecspi" },
567 { }
568};
569
570U_BOOT_DRIVER(mxc_spi) = {
571 .name = "mxc_spi",
572 .id = UCLASS_SPI,
573 .of_match = mxc_spi_ids,
574 .ops = &mxc_spi_ops,
575 .platdata_auto_alloc_size = sizeof(struct mxc_spi_slave),
576 .probe = mxc_spi_probe,
577};
578#endif