blob: d94aaf9fdbd2048da4f3e6c7e262fa544e160ada [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
Heiko Schocher7a3faf32019-05-26 12:15:47 +020041#define MAX_CS_COUNT 4
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;
Markus Niebel027a9a02014-10-23 16:09:39 +020052 unsigned int max_hz;
53 unsigned int mode;
Peng Fan994266b2017-08-09 13:09:33 +080054 struct gpio_desc ss;
Heiko Schocher7a3faf32019-05-26 12:15:47 +020055 struct gpio_desc cs_gpios[MAX_CS_COUNT];
56 struct udevice *dev;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020057};
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020058
59static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
60{
61 return container_of(slave, struct mxc_spi_slave, slave);
62}
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +020063
Peng Fan994266b2017-08-09 13:09:33 +080064static void mxc_spi_cs_activate(struct mxc_spi_slave *mxcs)
Stefano Babicd205ddc2010-04-04 22:43:38 +020065{
Heiko Schocher7a3faf32019-05-26 12:15:47 +020066#if defined(CONFIG_DM_SPI)
67 struct udevice *dev = mxcs->dev;
68 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
69
70 u32 cs = slave_plat->cs;
71
72 if (!dm_gpio_is_valid(&mxcs->cs_gpios[cs]))
73 return;
74
75 dm_gpio_set_value(&mxcs->cs_gpios[cs], 1);
76#else
77 if (mxcs->gpio > 0)
78 gpio_set_value(mxcs->gpio, mxcs->ss_pol);
79#endif
Stefano Babicd205ddc2010-04-04 22:43:38 +020080}
81
Peng Fan994266b2017-08-09 13:09:33 +080082static void mxc_spi_cs_deactivate(struct mxc_spi_slave *mxcs)
Stefano Babicd205ddc2010-04-04 22:43:38 +020083{
Heiko Schocher7a3faf32019-05-26 12:15:47 +020084#if defined(CONFIG_DM_SPI)
85 struct udevice *dev = mxcs->dev;
86 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
87
88 u32 cs = slave_plat->cs;
89
90 if (!dm_gpio_is_valid(&mxcs->cs_gpios[cs]))
91 return;
92
93 dm_gpio_set_value(&mxcs->cs_gpios[cs], 0);
94#else
95 if (mxcs->gpio > 0)
96 gpio_set_value(mxcs->gpio, !(mxcs->ss_pol));
97#endif
Stefano Babicd205ddc2010-04-04 22:43:38 +020098}
99
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +0000100u32 get_cspi_div(u32 div)
101{
102 int i;
103
104 for (i = 0; i < 8; i++) {
105 if (div <= (4 << i))
106 return i;
107 }
108 return i;
109}
110
Eric Nelson08c61a52012-01-31 07:52:03 +0000111#ifdef MXC_CSPI
Markus Niebel027a9a02014-10-23 16:09:39 +0200112static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
Stefano Babicc9d59c72011-01-19 22:46:30 +0000113{
114 unsigned int ctrl_reg;
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +0000115 u32 clk_src;
116 u32 div;
Markus Niebel027a9a02014-10-23 16:09:39 +0200117 unsigned int max_hz = mxcs->max_hz;
118 unsigned int mode = mxcs->mode;
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +0000119
120 clk_src = mxc_get_clock(MXC_CSPI_CLK);
121
Benoît Thébaudeaucd200402012-08-10 08:51:50 +0000122 div = DIV_ROUND_UP(clk_src, max_hz);
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +0000123 div = get_cspi_div(div);
124
125 debug("clk %d Hz, div %d, real clk %d Hz\n",
126 max_hz, div, clk_src / (4 << div));
Stefano Babicc9d59c72011-01-19 22:46:30 +0000127
128 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
129 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
Anatolij Gustschinafaa9f62011-01-19 22:46:32 +0000130 MXC_CSPICTRL_DATARATE(div) |
Stefano Babicc9d59c72011-01-19 22:46:30 +0000131 MXC_CSPICTRL_EN |
132#ifdef CONFIG_MX35
133 MXC_CSPICTRL_SSCTL |
134#endif
135 MXC_CSPICTRL_MODE;
136
137 if (mode & SPI_CPHA)
138 ctrl_reg |= MXC_CSPICTRL_PHA;
139 if (mode & SPI_CPOL)
140 ctrl_reg |= MXC_CSPICTRL_POL;
141 if (mode & SPI_CS_HIGH)
142 ctrl_reg |= MXC_CSPICTRL_SSPOL;
143 mxcs->ctrl_reg = ctrl_reg;
144
145 return 0;
146}
147#endif
148
Eric Nelson08c61a52012-01-31 07:52:03 +0000149#ifdef MXC_ECSPI
Markus Niebel027a9a02014-10-23 16:09:39 +0200150static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
Stefano Babicd205ddc2010-04-04 22:43:38 +0200151{
152 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
Dirk Behme9a309032013-05-11 07:25:54 +0200153 s32 reg_ctrl, reg_config;
Markus Niebel5d584cc2014-02-17 17:33:17 +0100154 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0;
155 u32 pre_div = 0, post_div = 0;
Stefano Babicac87c172011-01-19 22:46:33 +0000156 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Markus Niebel027a9a02014-10-23 16:09:39 +0200157 unsigned int max_hz = mxcs->max_hz;
158 unsigned int mode = mxcs->mode;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200159
Fabio Estevam0f1411b2013-04-09 13:06:25 +0000160 /*
161 * Reset SPI and set all CSs to master mode, if toggling
162 * between slave and master mode we might see a glitch
163 * on the clock line
164 */
165 reg_ctrl = MXC_CSPICTRL_MODE_MASK;
166 reg_write(&regs->ctrl, reg_ctrl);
167 reg_ctrl |= MXC_CSPICTRL_EN;
168 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200169
Stefano Babicd205ddc2010-04-04 22:43:38 +0200170 if (clk_src > max_hz) {
Dirk Behme9a309032013-05-11 07:25:54 +0200171 pre_div = (clk_src - 1) / max_hz;
172 /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
173 post_div = fls(pre_div);
174 if (post_div > 4) {
175 post_div -= 4;
176 if (post_div >= 16) {
Stefano Babicd205ddc2010-04-04 22:43:38 +0200177 printf("Error: no divider for the freq: %d\n",
178 max_hz);
179 return -1;
180 }
Dirk Behme9a309032013-05-11 07:25:54 +0200181 pre_div >>= post_div;
182 } else {
183 post_div = 0;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200184 }
185 }
186
187 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
188 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
189 MXC_CSPICTRL_SELCHAN(cs);
190 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
191 MXC_CSPICTRL_PREDIV(pre_div);
192 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
193 MXC_CSPICTRL_POSTDIV(post_div);
194
Stefano Babicd205ddc2010-04-04 22:43:38 +0200195 if (mode & SPI_CS_HIGH)
196 ss_pol = 1;
197
Markus Niebel5d584cc2014-02-17 17:33:17 +0100198 if (mode & SPI_CPOL) {
Stefano Babicd205ddc2010-04-04 22:43:38 +0200199 sclkpol = 1;
Markus Niebel5d584cc2014-02-17 17:33:17 +0100200 sclkctl = 1;
201 }
Stefano Babicd205ddc2010-04-04 22:43:38 +0200202
203 if (mode & SPI_CPHA)
204 sclkpha = 1;
205
Stefano Babicac87c172011-01-19 22:46:33 +0000206 reg_config = reg_read(&regs->cfg);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200207
208 /*
209 * Configuration register setup
Stefano Babicc9d59c72011-01-19 22:46:30 +0000210 * The MX51 supports different setup for each SS
Stefano Babicd205ddc2010-04-04 22:43:38 +0200211 */
212 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
213 (ss_pol << (cs + MXC_CSPICON_SSPOL));
214 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
215 (sclkpol << (cs + MXC_CSPICON_POL));
Markus Niebel5d584cc2014-02-17 17:33:17 +0100216 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_CTL))) |
217 (sclkctl << (cs + MXC_CSPICON_CTL));
Stefano Babicd205ddc2010-04-04 22:43:38 +0200218 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
219 (sclkpha << (cs + MXC_CSPICON_PHA));
220
221 debug("reg_ctrl = 0x%x\n", reg_ctrl);
Stefano Babicac87c172011-01-19 22:46:33 +0000222 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200223 debug("reg_config = 0x%x\n", reg_config);
Stefano Babicac87c172011-01-19 22:46:33 +0000224 reg_write(&regs->cfg, reg_config);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200225
226 /* save config register and control register */
227 mxcs->ctrl_reg = reg_ctrl;
228 mxcs->cfg_reg = reg_config;
229
230 /* clear interrupt reg */
Stefano Babicac87c172011-01-19 22:46:33 +0000231 reg_write(&regs->intr, 0);
232 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200233
234 return 0;
235}
236#endif
237
Peng Fan994266b2017-08-09 13:09:33 +0800238int spi_xchg_single(struct mxc_spi_slave *mxcs, unsigned int bitlen,
Stefano Babic2f721d12010-08-20 12:05:03 +0200239 const u8 *dout, u8 *din, unsigned long flags)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200240{
Axel Lin9675fed2013-06-14 21:13:32 +0800241 int nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200242 u32 data, cnt, i;
Stefano Babicac87c172011-01-19 22:46:33 +0000243 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Heiko Schocherf659b572014-07-14 10:22:11 +0200244 u32 ts;
245 int status;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200246
Ye Li65a106e2019-01-04 09:26:00 +0000247 debug("%s: bitlen %d dout 0x%lx din 0x%lx\n",
248 __func__, bitlen, (ulong)dout, (ulong)din);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200249
250 mxcs->ctrl_reg = (mxcs->ctrl_reg &
251 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100252 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
253
Stefano Babicac87c172011-01-19 22:46:33 +0000254 reg_write(&regs->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
Eric Nelson08c61a52012-01-31 07:52:03 +0000255#ifdef MXC_ECSPI
Stefano Babicac87c172011-01-19 22:46:33 +0000256 reg_write(&regs->cfg, mxcs->cfg_reg);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200257#endif
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200258
Stefano Babicd205ddc2010-04-04 22:43:38 +0200259 /* Clear interrupt register */
Stefano Babicac87c172011-01-19 22:46:33 +0000260 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100261
Stefano Babic2f721d12010-08-20 12:05:03 +0200262 /*
263 * The SPI controller works only with words,
264 * check if less than a word is sent.
265 * Access to the FIFO is only 32 bit
266 */
267 if (bitlen % 32) {
268 data = 0;
269 cnt = (bitlen % 32) / 8;
270 if (dout) {
271 for (i = 0; i < cnt; i++) {
272 data = (data << 8) | (*dout++ & 0xFF);
273 }
274 }
275 debug("Sending SPI 0x%x\n", data);
276
Stefano Babicac87c172011-01-19 22:46:33 +0000277 reg_write(&regs->txdata, data);
Stefano Babic2f721d12010-08-20 12:05:03 +0200278 nbytes -= cnt;
279 }
280
281 data = 0;
282
283 while (nbytes > 0) {
284 data = 0;
285 if (dout) {
286 /* Buffer is not 32-bit aligned */
287 if ((unsigned long)dout & 0x03) {
288 data = 0;
Anatolij Gustschindff01092011-01-20 07:53:06 +0000289 for (i = 0; i < 4; i++)
Stefano Babic2f721d12010-08-20 12:05:03 +0200290 data = (data << 8) | (*dout++ & 0xFF);
Stefano Babic2f721d12010-08-20 12:05:03 +0200291 } else {
292 data = *(u32 *)dout;
293 data = cpu_to_be32(data);
Timo Herbrecher6d5ce1b2013-10-16 00:05:09 +0530294 dout += 4;
Stefano Babic2f721d12010-08-20 12:05:03 +0200295 }
Stefano Babic2f721d12010-08-20 12:05:03 +0200296 }
297 debug("Sending SPI 0x%x\n", data);
Stefano Babicac87c172011-01-19 22:46:33 +0000298 reg_write(&regs->txdata, data);
Stefano Babic2f721d12010-08-20 12:05:03 +0200299 nbytes -= 4;
300 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200301
Stefano Babicd205ddc2010-04-04 22:43:38 +0200302 /* FIFO is written, now starts the transfer setting the XCH bit */
Stefano Babicac87c172011-01-19 22:46:33 +0000303 reg_write(&regs->ctrl, mxcs->ctrl_reg |
Stefano Babicd205ddc2010-04-04 22:43:38 +0200304 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200305
Heiko Schocherf659b572014-07-14 10:22:11 +0200306 ts = get_timer(0);
307 status = reg_read(&regs->stat);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200308 /* Wait until the TC (Transfer completed) bit is set */
Heiko Schocherf659b572014-07-14 10:22:11 +0200309 while ((status & MXC_CSPICTRL_TC) == 0) {
310 if (get_timer(ts) > CONFIG_SYS_SPI_MXC_WAIT) {
311 printf("spi_xchg_single: Timeout!\n");
312 return -1;
313 }
314 status = reg_read(&regs->stat);
315 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200316
Stefano Babicd205ddc2010-04-04 22:43:38 +0200317 /* Transfer completed, clear any pending request */
Stefano Babicac87c172011-01-19 22:46:33 +0000318 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100319
Axel Lin9675fed2013-06-14 21:13:32 +0800320 nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babicd205ddc2010-04-04 22:43:38 +0200321
Stefano Babic2f721d12010-08-20 12:05:03 +0200322 cnt = nbytes % 32;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200323
Stefano Babic2f721d12010-08-20 12:05:03 +0200324 if (bitlen % 32) {
Stefano Babicac87c172011-01-19 22:46:33 +0000325 data = reg_read(&regs->rxdata);
Stefano Babic2f721d12010-08-20 12:05:03 +0200326 cnt = (bitlen % 32) / 8;
Anatolij Gustschindff01092011-01-20 07:53:06 +0000327 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200328 debug("SPI Rx unaligned: 0x%x\n", data);
329 if (din) {
Anatolij Gustschindff01092011-01-20 07:53:06 +0000330 memcpy(din, &data, cnt);
331 din += cnt;
Stefano Babic2f721d12010-08-20 12:05:03 +0200332 }
333 nbytes -= cnt;
334 }
335
336 while (nbytes > 0) {
337 u32 tmp;
Stefano Babicac87c172011-01-19 22:46:33 +0000338 tmp = reg_read(&regs->rxdata);
Stefano Babic2f721d12010-08-20 12:05:03 +0200339 data = cpu_to_be32(tmp);
340 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
Masahiro Yamadab4141192014-11-07 03:03:31 +0900341 cnt = min_t(u32, nbytes, sizeof(data));
Stefano Babic2f721d12010-08-20 12:05:03 +0200342 if (din) {
343 memcpy(din, &data, cnt);
344 din += cnt;
345 }
346 nbytes -= cnt;
347 }
348
349 return 0;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200350
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200351}
352
Peng Fan994266b2017-08-09 13:09:33 +0800353static int mxc_spi_xfer_internal(struct mxc_spi_slave *mxcs,
354 unsigned int bitlen, const void *dout,
355 void *din, unsigned long flags)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200356{
Axel Lin9675fed2013-06-14 21:13:32 +0800357 int n_bytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic2f721d12010-08-20 12:05:03 +0200358 int n_bits;
359 int ret;
360 u32 blk_size;
361 u8 *p_outbuf = (u8 *)dout;
362 u8 *p_inbuf = (u8 *)din;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200363
Peng Fan994266b2017-08-09 13:09:33 +0800364 if (!mxcs)
365 return -EINVAL;
Stefano Babic2f721d12010-08-20 12:05:03 +0200366
367 if (flags & SPI_XFER_BEGIN)
Peng Fan994266b2017-08-09 13:09:33 +0800368 mxc_spi_cs_activate(mxcs);
Stefano Babic2f721d12010-08-20 12:05:03 +0200369
370 while (n_bytes > 0) {
Stefano Babic2f721d12010-08-20 12:05:03 +0200371 if (n_bytes < MAX_SPI_BYTES)
372 blk_size = n_bytes;
373 else
374 blk_size = MAX_SPI_BYTES;
375
376 n_bits = blk_size * 8;
377
Peng Fan994266b2017-08-09 13:09:33 +0800378 ret = spi_xchg_single(mxcs, n_bits, p_outbuf, p_inbuf, 0);
Stefano Babic2f721d12010-08-20 12:05:03 +0200379
380 if (ret)
381 return ret;
382 if (dout)
383 p_outbuf += blk_size;
384 if (din)
385 p_inbuf += blk_size;
386 n_bytes -= blk_size;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200387 }
388
Stefano Babic2f721d12010-08-20 12:05:03 +0200389 if (flags & SPI_XFER_END) {
Peng Fan994266b2017-08-09 13:09:33 +0800390 mxc_spi_cs_deactivate(mxcs);
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100391 }
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200392
393 return 0;
394}
395
Peng Fan994266b2017-08-09 13:09:33 +0800396static int mxc_spi_claim_bus_internal(struct mxc_spi_slave *mxcs, int cs)
397{
398 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
399 int ret;
400
401 reg_write(&regs->rxdata, 1);
402 udelay(1);
403 ret = spi_cfg_mxc(mxcs, cs);
404 if (ret) {
405 printf("mxc_spi: cannot setup SPI controller\n");
406 return ret;
407 }
408 reg_write(&regs->period, MXC_CSPIPERIOD_32KHZ);
409 reg_write(&regs->intr, 0);
410
411 return 0;
412}
413
414#ifndef CONFIG_DM_SPI
415int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
416 void *din, unsigned long flags)
417{
418 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
419
420 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
421}
422
Nikita Kiryanov155fa9a2014-08-20 15:08:50 +0300423/*
424 * Some SPI devices require active chip-select over multiple
425 * transactions, we achieve this using a GPIO. Still, the SPI
426 * controller has to be configured to use one of its own chipselects.
427 * To use this feature you have to implement board_spi_cs_gpio() to assign
428 * a gpio value for each cs (-1 if cs doesn't need to use gpio).
429 * You must use some unused on this SPI controller cs between 0 and 3.
430 */
431static int setup_cs_gpio(struct mxc_spi_slave *mxcs,
432 unsigned int bus, unsigned int cs)
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100433{
434 int ret;
435
Nikita Kiryanov155fa9a2014-08-20 15:08:50 +0300436 mxcs->gpio = board_spi_cs_gpio(bus, cs);
437 if (mxcs->gpio == -1)
438 return 0;
439
Peng Fan994266b2017-08-09 13:09:33 +0800440 gpio_request(mxcs->gpio, "spi-cs");
Nikita Kiryanov155fa9a2014-08-20 15:08:50 +0300441 ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
442 if (ret) {
443 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
444 return -EINVAL;
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100445 }
446
Nikita Kiryanov155fa9a2014-08-20 15:08:50 +0300447 return 0;
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100448}
449
Peng Fan994266b2017-08-09 13:09:33 +0800450static unsigned long spi_bases[] = {
451 MXC_SPI_BASE_ADDRESSES
452};
453
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200454struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
455 unsigned int max_hz, unsigned int mode)
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200456{
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200457 struct mxc_spi_slave *mxcs;
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100458 int ret;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200459
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100460 if (bus >= ARRAY_SIZE(spi_bases))
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200461 return NULL;
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200462
Markus Niebel027a9a02014-10-23 16:09:39 +0200463 if (max_hz == 0) {
464 printf("Error: desired clock is 0\n");
465 return NULL;
466 }
467
Simon Glassd3504fe2013-03-18 19:23:40 +0000468 mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
Stefano Babic2f721d12010-08-20 12:05:03 +0200469 if (!mxcs) {
470 puts("mxc_spi: SPI Slave not allocated !\n");
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100471 return NULL;
Stefano Babic2f721d12010-08-20 12:05:03 +0200472 }
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100473
Fabio Estevamde5bf022012-11-15 11:23:23 +0000474 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
475
Nikita Kiryanov155fa9a2014-08-20 15:08:50 +0300476 ret = setup_cs_gpio(mxcs, bus, cs);
Guennadi Liakhovetskifc7a93c2009-02-13 09:26:40 +0100477 if (ret < 0) {
478 free(mxcs);
479 return NULL;
480 }
481
Stefano Babicd205ddc2010-04-04 22:43:38 +0200482 mxcs->base = spi_bases[bus];
Markus Niebel027a9a02014-10-23 16:09:39 +0200483 mxcs->max_hz = max_hz;
484 mxcs->mode = mode;
Stefano Babicd205ddc2010-04-04 22:43:38 +0200485
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200486 return &mxcs->slave;
487}
488
489void spi_free_slave(struct spi_slave *slave)
490{
Guennadi Liakhovetskif9b6a152009-02-07 00:09:12 +0100491 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
492
493 free(mxcs);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200494}
495
496int spi_claim_bus(struct spi_slave *slave)
497{
498 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
499
Peng Fan994266b2017-08-09 13:09:33 +0800500 return mxc_spi_claim_bus_internal(mxcs, slave->cs);
Guennadi Liakhovetski38254f42008-04-15 14:14:25 +0200501}
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200502
503void spi_release_bus(struct spi_slave *slave)
504{
505 /* TODO: Shut the controller down */
506}
Peng Fan994266b2017-08-09 13:09:33 +0800507#else
508
509static int mxc_spi_probe(struct udevice *bus)
510{
Peng Fan994266b2017-08-09 13:09:33 +0800511 struct mxc_spi_slave *mxcs = dev_get_platdata(bus);
512 int node = dev_of_offset(bus);
513 const void *blob = gd->fdt_blob;
514 int ret;
Heiko Schocher7a3faf32019-05-26 12:15:47 +0200515 int i;
Peng Fan994266b2017-08-09 13:09:33 +0800516
Heiko Schocher7a3faf32019-05-26 12:15:47 +0200517 ret = gpio_request_list_by_name(bus, "cs-gpios", mxcs->cs_gpios,
518 ARRAY_SIZE(mxcs->cs_gpios), 0);
519 if (ret < 0) {
520 pr_err("Can't get %s gpios! Error: %d", bus->name, ret);
521 return ret;
522 }
523
524 for (i = 0; i < ARRAY_SIZE(mxcs->cs_gpios); i++) {
525 if (!dm_gpio_is_valid(&mxcs->cs_gpios[i]))
526 continue;
527
528 ret = dm_gpio_set_dir_flags(&mxcs->cs_gpios[i],
529 GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
530 if (ret) {
531 dev_err(bus, "Setting cs %d error\n", i);
532 return ret;
533 }
Peng Fan994266b2017-08-09 13:09:33 +0800534 }
535
Heiko Schocher2b849e12019-05-26 12:15:46 +0200536 mxcs->base = devfdt_get_addr(bus);
537 if (mxcs->base == FDT_ADDR_T_NONE)
Peng Fan994266b2017-08-09 13:09:33 +0800538 return -ENODEV;
539
Peng Fan994266b2017-08-09 13:09:33 +0800540 mxcs->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
541 20000000);
542
543 return 0;
544}
545
546static int mxc_spi_xfer(struct udevice *dev, unsigned int bitlen,
547 const void *dout, void *din, unsigned long flags)
548{
549 struct mxc_spi_slave *mxcs = dev_get_platdata(dev->parent);
550
551
552 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
553}
554
555static int mxc_spi_claim_bus(struct udevice *dev)
556{
557 struct mxc_spi_slave *mxcs = dev_get_platdata(dev->parent);
558 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
559
Heiko Schocher7a3faf32019-05-26 12:15:47 +0200560 mxcs->dev = dev;
561
Peng Fan994266b2017-08-09 13:09:33 +0800562 return mxc_spi_claim_bus_internal(mxcs, slave_plat->cs);
563}
564
565static int mxc_spi_release_bus(struct udevice *dev)
566{
567 return 0;
568}
569
570static int mxc_spi_set_speed(struct udevice *bus, uint speed)
571{
572 /* Nothing to do */
573 return 0;
574}
575
576static int mxc_spi_set_mode(struct udevice *bus, uint mode)
577{
578 struct mxc_spi_slave *mxcs = dev_get_platdata(bus);
579
580 mxcs->mode = mode;
581 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
582
583 return 0;
584}
585
586static const struct dm_spi_ops mxc_spi_ops = {
587 .claim_bus = mxc_spi_claim_bus,
588 .release_bus = mxc_spi_release_bus,
589 .xfer = mxc_spi_xfer,
590 .set_speed = mxc_spi_set_speed,
591 .set_mode = mxc_spi_set_mode,
592};
593
594static const struct udevice_id mxc_spi_ids[] = {
595 { .compatible = "fsl,imx51-ecspi" },
596 { }
597};
598
599U_BOOT_DRIVER(mxc_spi) = {
600 .name = "mxc_spi",
601 .id = UCLASS_SPI,
602 .of_match = mxc_spi_ids,
603 .ops = &mxc_spi_ops,
604 .platdata_auto_alloc_size = sizeof(struct mxc_spi_slave),
605 .probe = mxc_spi_probe,
606};
607#endif