blob: e35c6f9b4a9fe29b5fb83dd54541005aa80d0dc5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +02002/*
3 * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +02004 */
5
6#include <common.h>
Peng Fanea0bce62017-08-09 13:09:33 +08007#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Haavard Skinnemoend74084a2008-05-16 11:10:31 +02009#include <malloc.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020010#include <spi.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <dm/device_compat.h>
Simon Glassdbd79542020-05-10 11:40:11 -060012#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090013#include <linux/errno.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020014#include <asm/io.h>
Stefano Babic7faee912011-08-21 10:45:44 +020015#include <asm/gpio.h>
Stefano Babic78129d92011-03-14 15:43:56 +010016#include <asm/arch/imx-regs.h>
17#include <asm/arch/clock.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020018#include <asm/mach-imx/spi.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020019
Peng Fanea0bce62017-08-09 13:09:33 +080020DECLARE_GLOBAL_DATA_PTR;
21
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020022#ifdef CONFIG_MX27
23/* i.MX27 has a completely wrong register layout and register definitions in the
24 * datasheet, the correct one is in the Freescale's Linux driver */
25
Helmut Raiger785efc92011-06-15 01:45:45 +000026#error "i.MX27 CSPI not supported due to drastic differences in register definitions" \
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020027"See linux mxc_spi driver from Freescale for details."
Eric Nelsonfe1e7612012-01-31 07:52:03 +000028#endif
Stefano Babicdcd73cd2011-01-19 22:46:30 +000029
Nikita Kiryanov00cd7382014-08-20 15:08:50 +030030__weak int board_spi_cs_gpio(unsigned bus, unsigned cs)
31{
32 return -1;
33}
34
Stefano Babicd77fe992010-07-06 17:05:06 +020035#define OUT MXC_GPIO_DIRECTION_OUT
36
Stefano Babic28580452011-01-19 22:46:33 +000037#define reg_read readl
38#define reg_write(a, v) writel(v, a)
39
Heiko Schocherb77c8882014-07-14 10:22:11 +020040#if !defined(CONFIG_SYS_SPI_MXC_WAIT)
41#define CONFIG_SYS_SPI_MXC_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
42#endif
43
Heiko Schocher053c2442019-05-26 12:15:47 +020044#define MAX_CS_COUNT 4
45
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020046struct mxc_spi_slave {
47 struct spi_slave slave;
48 unsigned long base;
49 u32 ctrl_reg;
Eric Nelsonfe1e7612012-01-31 07:52:03 +000050#if defined(MXC_ECSPI)
Stefano Babic6e6f4552010-04-04 22:43:38 +020051 u32 cfg_reg;
52#endif
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +010053 int gpio;
Stefano Babicd77fe992010-07-06 17:05:06 +020054 int ss_pol;
Markus Niebel8f769cf2014-10-23 16:09:39 +020055 unsigned int max_hz;
56 unsigned int mode;
Peng Fanea0bce62017-08-09 13:09:33 +080057 struct gpio_desc ss;
Heiko Schocher053c2442019-05-26 12:15:47 +020058 struct gpio_desc cs_gpios[MAX_CS_COUNT];
59 struct udevice *dev;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020060};
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020061
62static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
63{
64 return container_of(slave, struct mxc_spi_slave, slave);
65}
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020066
Peng Fanea0bce62017-08-09 13:09:33 +080067static void mxc_spi_cs_activate(struct mxc_spi_slave *mxcs)
Stefano Babic6e6f4552010-04-04 22:43:38 +020068{
Heiko Schocher053c2442019-05-26 12:15:47 +020069#if defined(CONFIG_DM_SPI)
70 struct udevice *dev = mxcs->dev;
71 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
72
73 u32 cs = slave_plat->cs;
74
75 if (!dm_gpio_is_valid(&mxcs->cs_gpios[cs]))
76 return;
77
78 dm_gpio_set_value(&mxcs->cs_gpios[cs], 1);
79#else
80 if (mxcs->gpio > 0)
81 gpio_set_value(mxcs->gpio, mxcs->ss_pol);
82#endif
Stefano Babic6e6f4552010-04-04 22:43:38 +020083}
84
Peng Fanea0bce62017-08-09 13:09:33 +080085static void mxc_spi_cs_deactivate(struct mxc_spi_slave *mxcs)
Stefano Babic6e6f4552010-04-04 22:43:38 +020086{
Heiko Schocher053c2442019-05-26 12:15:47 +020087#if defined(CONFIG_DM_SPI)
88 struct udevice *dev = mxcs->dev;
89 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
90
91 u32 cs = slave_plat->cs;
92
93 if (!dm_gpio_is_valid(&mxcs->cs_gpios[cs]))
94 return;
95
96 dm_gpio_set_value(&mxcs->cs_gpios[cs], 0);
97#else
98 if (mxcs->gpio > 0)
99 gpio_set_value(mxcs->gpio, !(mxcs->ss_pol));
100#endif
Stefano Babic6e6f4552010-04-04 22:43:38 +0200101}
102
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000103u32 get_cspi_div(u32 div)
104{
105 int i;
106
107 for (i = 0; i < 8; i++) {
108 if (div <= (4 << i))
109 return i;
110 }
111 return i;
112}
113
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000114#ifdef MXC_CSPI
Markus Niebel8f769cf2014-10-23 16:09:39 +0200115static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000116{
117 unsigned int ctrl_reg;
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000118 u32 clk_src;
119 u32 div;
Markus Niebel8f769cf2014-10-23 16:09:39 +0200120 unsigned int max_hz = mxcs->max_hz;
121 unsigned int mode = mxcs->mode;
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000122
123 clk_src = mxc_get_clock(MXC_CSPI_CLK);
124
Benoît Thébaudeau884622b2012-08-10 08:51:50 +0000125 div = DIV_ROUND_UP(clk_src, max_hz);
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000126 div = get_cspi_div(div);
127
128 debug("clk %d Hz, div %d, real clk %d Hz\n",
129 max_hz, div, clk_src / (4 << div));
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000130
131 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
132 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000133 MXC_CSPICTRL_DATARATE(div) |
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000134 MXC_CSPICTRL_EN |
135#ifdef CONFIG_MX35
136 MXC_CSPICTRL_SSCTL |
137#endif
138 MXC_CSPICTRL_MODE;
139
140 if (mode & SPI_CPHA)
141 ctrl_reg |= MXC_CSPICTRL_PHA;
142 if (mode & SPI_CPOL)
143 ctrl_reg |= MXC_CSPICTRL_POL;
144 if (mode & SPI_CS_HIGH)
145 ctrl_reg |= MXC_CSPICTRL_SSPOL;
146 mxcs->ctrl_reg = ctrl_reg;
147
148 return 0;
149}
150#endif
151
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000152#ifdef MXC_ECSPI
Markus Niebel8f769cf2014-10-23 16:09:39 +0200153static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
Stefano Babic6e6f4552010-04-04 22:43:38 +0200154{
155 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
Dirk Behmeb177b712013-05-11 07:25:54 +0200156 s32 reg_ctrl, reg_config;
Markus Niebel6683e622014-02-17 17:33:17 +0100157 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0;
158 u32 pre_div = 0, post_div = 0;
Stefano Babic28580452011-01-19 22:46:33 +0000159 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Markus Niebel8f769cf2014-10-23 16:09:39 +0200160 unsigned int max_hz = mxcs->max_hz;
161 unsigned int mode = mxcs->mode;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200162
Fabio Estevam833fb552013-04-09 13:06:25 +0000163 /*
164 * Reset SPI and set all CSs to master mode, if toggling
165 * between slave and master mode we might see a glitch
166 * on the clock line
167 */
168 reg_ctrl = MXC_CSPICTRL_MODE_MASK;
169 reg_write(&regs->ctrl, reg_ctrl);
170 reg_ctrl |= MXC_CSPICTRL_EN;
171 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200172
Stefano Babic6e6f4552010-04-04 22:43:38 +0200173 if (clk_src > max_hz) {
Dirk Behmeb177b712013-05-11 07:25:54 +0200174 pre_div = (clk_src - 1) / max_hz;
175 /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
176 post_div = fls(pre_div);
177 if (post_div > 4) {
178 post_div -= 4;
179 if (post_div >= 16) {
Stefano Babic6e6f4552010-04-04 22:43:38 +0200180 printf("Error: no divider for the freq: %d\n",
181 max_hz);
182 return -1;
183 }
Dirk Behmeb177b712013-05-11 07:25:54 +0200184 pre_div >>= post_div;
185 } else {
186 post_div = 0;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200187 }
188 }
189
190 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
191 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
192 MXC_CSPICTRL_SELCHAN(cs);
193 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
194 MXC_CSPICTRL_PREDIV(pre_div);
195 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
196 MXC_CSPICTRL_POSTDIV(post_div);
197
Stefano Babic6e6f4552010-04-04 22:43:38 +0200198 if (mode & SPI_CS_HIGH)
199 ss_pol = 1;
200
Markus Niebel6683e622014-02-17 17:33:17 +0100201 if (mode & SPI_CPOL) {
Stefano Babic6e6f4552010-04-04 22:43:38 +0200202 sclkpol = 1;
Markus Niebel6683e622014-02-17 17:33:17 +0100203 sclkctl = 1;
204 }
Stefano Babic6e6f4552010-04-04 22:43:38 +0200205
206 if (mode & SPI_CPHA)
207 sclkpha = 1;
208
Stefano Babic28580452011-01-19 22:46:33 +0000209 reg_config = reg_read(&regs->cfg);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200210
211 /*
212 * Configuration register setup
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000213 * The MX51 supports different setup for each SS
Stefano Babic6e6f4552010-04-04 22:43:38 +0200214 */
215 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
216 (ss_pol << (cs + MXC_CSPICON_SSPOL));
217 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
218 (sclkpol << (cs + MXC_CSPICON_POL));
Markus Niebel6683e622014-02-17 17:33:17 +0100219 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_CTL))) |
220 (sclkctl << (cs + MXC_CSPICON_CTL));
Stefano Babic6e6f4552010-04-04 22:43:38 +0200221 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
222 (sclkpha << (cs + MXC_CSPICON_PHA));
223
224 debug("reg_ctrl = 0x%x\n", reg_ctrl);
Stefano Babic28580452011-01-19 22:46:33 +0000225 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200226 debug("reg_config = 0x%x\n", reg_config);
Stefano Babic28580452011-01-19 22:46:33 +0000227 reg_write(&regs->cfg, reg_config);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200228
229 /* save config register and control register */
230 mxcs->ctrl_reg = reg_ctrl;
231 mxcs->cfg_reg = reg_config;
232
233 /* clear interrupt reg */
Stefano Babic28580452011-01-19 22:46:33 +0000234 reg_write(&regs->intr, 0);
235 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200236
237 return 0;
238}
239#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200240
Peng Fanea0bce62017-08-09 13:09:33 +0800241int spi_xchg_single(struct mxc_spi_slave *mxcs, unsigned int bitlen,
Stefano Babic125f82a2010-08-20 12:05:03 +0200242 const u8 *dout, u8 *din, unsigned long flags)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200243{
Axel Linfb7def92013-06-14 21:13:32 +0800244 int nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic125f82a2010-08-20 12:05:03 +0200245 u32 data, cnt, i;
Stefano Babic28580452011-01-19 22:46:33 +0000246 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Heiko Schocherb77c8882014-07-14 10:22:11 +0200247 u32 ts;
248 int status;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200249
Ye Li07955fb2019-01-04 09:26:00 +0000250 debug("%s: bitlen %d dout 0x%lx din 0x%lx\n",
251 __func__, bitlen, (ulong)dout, (ulong)din);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200252
Stefano Babic6e6f4552010-04-04 22:43:38 +0200253 mxcs->ctrl_reg = (mxcs->ctrl_reg &
254 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100255 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200256
Stefano Babic28580452011-01-19 22:46:33 +0000257 reg_write(&regs->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000258#ifdef MXC_ECSPI
Stefano Babic28580452011-01-19 22:46:33 +0000259 reg_write(&regs->cfg, mxcs->cfg_reg);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200260#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200261
Stefano Babic6e6f4552010-04-04 22:43:38 +0200262 /* Clear interrupt register */
Stefano Babic28580452011-01-19 22:46:33 +0000263 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100264
Stefano Babic125f82a2010-08-20 12:05:03 +0200265 /*
266 * The SPI controller works only with words,
267 * check if less than a word is sent.
268 * Access to the FIFO is only 32 bit
269 */
270 if (bitlen % 32) {
271 data = 0;
272 cnt = (bitlen % 32) / 8;
273 if (dout) {
274 for (i = 0; i < cnt; i++) {
275 data = (data << 8) | (*dout++ & 0xFF);
276 }
277 }
278 debug("Sending SPI 0x%x\n", data);
279
Stefano Babic28580452011-01-19 22:46:33 +0000280 reg_write(&regs->txdata, data);
Stefano Babic125f82a2010-08-20 12:05:03 +0200281 nbytes -= cnt;
282 }
283
284 data = 0;
285
286 while (nbytes > 0) {
287 data = 0;
288 if (dout) {
289 /* Buffer is not 32-bit aligned */
290 if ((unsigned long)dout & 0x03) {
291 data = 0;
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000292 for (i = 0; i < 4; i++)
Stefano Babic125f82a2010-08-20 12:05:03 +0200293 data = (data << 8) | (*dout++ & 0xFF);
Stefano Babic125f82a2010-08-20 12:05:03 +0200294 } else {
295 data = *(u32 *)dout;
296 data = cpu_to_be32(data);
Timo Herbrecher64203202013-10-16 00:05:09 +0530297 dout += 4;
Stefano Babic125f82a2010-08-20 12:05:03 +0200298 }
Stefano Babic125f82a2010-08-20 12:05:03 +0200299 }
300 debug("Sending SPI 0x%x\n", data);
Stefano Babic28580452011-01-19 22:46:33 +0000301 reg_write(&regs->txdata, data);
Stefano Babic125f82a2010-08-20 12:05:03 +0200302 nbytes -= 4;
303 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200304
Stefano Babic6e6f4552010-04-04 22:43:38 +0200305 /* FIFO is written, now starts the transfer setting the XCH bit */
Stefano Babic28580452011-01-19 22:46:33 +0000306 reg_write(&regs->ctrl, mxcs->ctrl_reg |
Stefano Babic6e6f4552010-04-04 22:43:38 +0200307 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200308
Heiko Schocherb77c8882014-07-14 10:22:11 +0200309 ts = get_timer(0);
310 status = reg_read(&regs->stat);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200311 /* Wait until the TC (Transfer completed) bit is set */
Heiko Schocherb77c8882014-07-14 10:22:11 +0200312 while ((status & MXC_CSPICTRL_TC) == 0) {
313 if (get_timer(ts) > CONFIG_SYS_SPI_MXC_WAIT) {
314 printf("spi_xchg_single: Timeout!\n");
315 return -1;
316 }
317 status = reg_read(&regs->stat);
318 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200319
Stefano Babic6e6f4552010-04-04 22:43:38 +0200320 /* Transfer completed, clear any pending request */
Stefano Babic28580452011-01-19 22:46:33 +0000321 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200322
Axel Linfb7def92013-06-14 21:13:32 +0800323 nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200324
Stefano Babic125f82a2010-08-20 12:05:03 +0200325 cnt = nbytes % 32;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100326
Stefano Babic125f82a2010-08-20 12:05:03 +0200327 if (bitlen % 32) {
Stefano Babic28580452011-01-19 22:46:33 +0000328 data = reg_read(&regs->rxdata);
Stefano Babic125f82a2010-08-20 12:05:03 +0200329 cnt = (bitlen % 32) / 8;
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000330 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
Stefano Babic125f82a2010-08-20 12:05:03 +0200331 debug("SPI Rx unaligned: 0x%x\n", data);
332 if (din) {
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000333 memcpy(din, &data, cnt);
334 din += cnt;
Stefano Babic125f82a2010-08-20 12:05:03 +0200335 }
336 nbytes -= cnt;
337 }
338
339 while (nbytes > 0) {
340 u32 tmp;
Stefano Babic28580452011-01-19 22:46:33 +0000341 tmp = reg_read(&regs->rxdata);
Stefano Babic125f82a2010-08-20 12:05:03 +0200342 data = cpu_to_be32(tmp);
343 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
Masahiro Yamadadb204642014-11-07 03:03:31 +0900344 cnt = min_t(u32, nbytes, sizeof(data));
Stefano Babic125f82a2010-08-20 12:05:03 +0200345 if (din) {
346 memcpy(din, &data, cnt);
347 din += cnt;
348 }
349 nbytes -= cnt;
350 }
351
352 return 0;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200353
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200354}
355
Peng Fanea0bce62017-08-09 13:09:33 +0800356static int mxc_spi_xfer_internal(struct mxc_spi_slave *mxcs,
357 unsigned int bitlen, const void *dout,
358 void *din, unsigned long flags)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200359{
Axel Linfb7def92013-06-14 21:13:32 +0800360 int n_bytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic125f82a2010-08-20 12:05:03 +0200361 int n_bits;
362 int ret;
363 u32 blk_size;
364 u8 *p_outbuf = (u8 *)dout;
365 u8 *p_inbuf = (u8 *)din;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200366
Peng Fanea0bce62017-08-09 13:09:33 +0800367 if (!mxcs)
368 return -EINVAL;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200369
Stefano Babic125f82a2010-08-20 12:05:03 +0200370 if (flags & SPI_XFER_BEGIN)
Peng Fanea0bce62017-08-09 13:09:33 +0800371 mxc_spi_cs_activate(mxcs);
Magnus Lilja1858a9a2010-02-09 22:05:39 +0100372
Stefano Babic125f82a2010-08-20 12:05:03 +0200373 while (n_bytes > 0) {
Stefano Babic125f82a2010-08-20 12:05:03 +0200374 if (n_bytes < MAX_SPI_BYTES)
375 blk_size = n_bytes;
376 else
377 blk_size = MAX_SPI_BYTES;
378
379 n_bits = blk_size * 8;
380
Peng Fanea0bce62017-08-09 13:09:33 +0800381 ret = spi_xchg_single(mxcs, n_bits, p_outbuf, p_inbuf, 0);
Stefano Babic125f82a2010-08-20 12:05:03 +0200382
383 if (ret)
384 return ret;
385 if (dout)
386 p_outbuf += blk_size;
387 if (din)
388 p_inbuf += blk_size;
389 n_bytes -= blk_size;
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100390 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200391
Stefano Babic125f82a2010-08-20 12:05:03 +0200392 if (flags & SPI_XFER_END) {
Peng Fanea0bce62017-08-09 13:09:33 +0800393 mxc_spi_cs_deactivate(mxcs);
Stefano Babic125f82a2010-08-20 12:05:03 +0200394 }
395
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200396 return 0;
397}
398
Peng Fanea0bce62017-08-09 13:09:33 +0800399static int mxc_spi_claim_bus_internal(struct mxc_spi_slave *mxcs, int cs)
400{
401 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
402 int ret;
403
404 reg_write(&regs->rxdata, 1);
405 udelay(1);
406 ret = spi_cfg_mxc(mxcs, cs);
407 if (ret) {
408 printf("mxc_spi: cannot setup SPI controller\n");
409 return ret;
410 }
411 reg_write(&regs->period, MXC_CSPIPERIOD_32KHZ);
412 reg_write(&regs->intr, 0);
413
414 return 0;
415}
416
417#ifndef CONFIG_DM_SPI
418int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
419 void *din, unsigned long flags)
420{
421 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
422
423 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
424}
425
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300426/*
427 * Some SPI devices require active chip-select over multiple
428 * transactions, we achieve this using a GPIO. Still, the SPI
429 * controller has to be configured to use one of its own chipselects.
430 * To use this feature you have to implement board_spi_cs_gpio() to assign
431 * a gpio value for each cs (-1 if cs doesn't need to use gpio).
432 * You must use some unused on this SPI controller cs between 0 and 3.
433 */
434static int setup_cs_gpio(struct mxc_spi_slave *mxcs,
435 unsigned int bus, unsigned int cs)
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100436{
437 int ret;
438
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300439 mxcs->gpio = board_spi_cs_gpio(bus, cs);
440 if (mxcs->gpio == -1)
441 return 0;
442
Peng Fanea0bce62017-08-09 13:09:33 +0800443 gpio_request(mxcs->gpio, "spi-cs");
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300444 ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
445 if (ret) {
446 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
447 return -EINVAL;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100448 }
449
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300450 return 0;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200451}
452
Peng Fanea0bce62017-08-09 13:09:33 +0800453static unsigned long spi_bases[] = {
454 MXC_SPI_BASE_ADDRESSES
455};
456
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200457struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
458 unsigned int max_hz, unsigned int mode)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200459{
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200460 struct mxc_spi_slave *mxcs;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100461 int ret;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200462
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100463 if (bus >= ARRAY_SIZE(spi_bases))
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200464 return NULL;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200465
Markus Niebel8f769cf2014-10-23 16:09:39 +0200466 if (max_hz == 0) {
467 printf("Error: desired clock is 0\n");
468 return NULL;
469 }
470
Simon Glassd034a952013-03-18 19:23:40 +0000471 mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
Stefano Babic125f82a2010-08-20 12:05:03 +0200472 if (!mxcs) {
473 puts("mxc_spi: SPI Slave not allocated !\n");
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100474 return NULL;
Stefano Babic125f82a2010-08-20 12:05:03 +0200475 }
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100476
Fabio Estevam17cd2a82012-11-15 11:23:23 +0000477 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
478
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300479 ret = setup_cs_gpio(mxcs, bus, cs);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100480 if (ret < 0) {
481 free(mxcs);
482 return NULL;
483 }
484
Stefano Babic6e6f4552010-04-04 22:43:38 +0200485 mxcs->base = spi_bases[bus];
Markus Niebel8f769cf2014-10-23 16:09:39 +0200486 mxcs->max_hz = max_hz;
487 mxcs->mode = mode;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200488
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200489 return &mxcs->slave;
490}
491
492void spi_free_slave(struct spi_slave *slave)
493{
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100494 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
495
496 free(mxcs);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200497}
498
499int spi_claim_bus(struct spi_slave *slave)
500{
501 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
502
Peng Fanea0bce62017-08-09 13:09:33 +0800503 return mxc_spi_claim_bus_internal(mxcs, slave->cs);
504}
505
506void spi_release_bus(struct spi_slave *slave)
507{
508 /* TODO: Shut the controller down */
509}
510#else
511
512static int mxc_spi_probe(struct udevice *bus)
513{
Peng Fanea0bce62017-08-09 13:09:33 +0800514 struct mxc_spi_slave *mxcs = dev_get_platdata(bus);
515 int node = dev_of_offset(bus);
516 const void *blob = gd->fdt_blob;
517 int ret;
Heiko Schocher053c2442019-05-26 12:15:47 +0200518 int i;
Peng Fanea0bce62017-08-09 13:09:33 +0800519
Heiko Schocher053c2442019-05-26 12:15:47 +0200520 ret = gpio_request_list_by_name(bus, "cs-gpios", mxcs->cs_gpios,
521 ARRAY_SIZE(mxcs->cs_gpios), 0);
522 if (ret < 0) {
523 pr_err("Can't get %s gpios! Error: %d", bus->name, ret);
524 return ret;
525 }
526
527 for (i = 0; i < ARRAY_SIZE(mxcs->cs_gpios); i++) {
528 if (!dm_gpio_is_valid(&mxcs->cs_gpios[i]))
529 continue;
530
531 ret = dm_gpio_set_dir_flags(&mxcs->cs_gpios[i],
532 GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
533 if (ret) {
534 dev_err(bus, "Setting cs %d error\n", i);
535 return ret;
536 }
Peng Fanea0bce62017-08-09 13:09:33 +0800537 }
538
Heiko Schocher6d49b4e2019-05-26 12:15:46 +0200539 mxcs->base = devfdt_get_addr(bus);
540 if (mxcs->base == FDT_ADDR_T_NONE)
Peng Fanea0bce62017-08-09 13:09:33 +0800541 return -ENODEV;
542
Peng Fanea0bce62017-08-09 13:09:33 +0800543 mxcs->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
544 20000000);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200545
546 return 0;
547}
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200548
Peng Fanea0bce62017-08-09 13:09:33 +0800549static int mxc_spi_xfer(struct udevice *dev, unsigned int bitlen,
550 const void *dout, void *din, unsigned long flags)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200551{
Peng Fanea0bce62017-08-09 13:09:33 +0800552 struct mxc_spi_slave *mxcs = dev_get_platdata(dev->parent);
553
554
555 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
556}
557
558static int mxc_spi_claim_bus(struct udevice *dev)
559{
560 struct mxc_spi_slave *mxcs = dev_get_platdata(dev->parent);
561 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
562
Heiko Schocher053c2442019-05-26 12:15:47 +0200563 mxcs->dev = dev;
564
Peng Fanea0bce62017-08-09 13:09:33 +0800565 return mxc_spi_claim_bus_internal(mxcs, slave_plat->cs);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200566}
Peng Fanea0bce62017-08-09 13:09:33 +0800567
568static int mxc_spi_release_bus(struct udevice *dev)
569{
570 return 0;
571}
572
573static int mxc_spi_set_speed(struct udevice *bus, uint speed)
574{
575 /* Nothing to do */
576 return 0;
577}
578
579static int mxc_spi_set_mode(struct udevice *bus, uint mode)
580{
581 struct mxc_spi_slave *mxcs = dev_get_platdata(bus);
582
583 mxcs->mode = mode;
584 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
585
586 return 0;
587}
588
589static const struct dm_spi_ops mxc_spi_ops = {
590 .claim_bus = mxc_spi_claim_bus,
591 .release_bus = mxc_spi_release_bus,
592 .xfer = mxc_spi_xfer,
593 .set_speed = mxc_spi_set_speed,
594 .set_mode = mxc_spi_set_mode,
595};
596
597static const struct udevice_id mxc_spi_ids[] = {
598 { .compatible = "fsl,imx51-ecspi" },
599 { }
600};
601
602U_BOOT_DRIVER(mxc_spi) = {
603 .name = "mxc_spi",
604 .id = UCLASS_SPI,
605 .of_match = mxc_spi_ids,
606 .ops = &mxc_spi_ops,
607 .platdata_auto_alloc_size = sizeof(struct mxc_spi_slave),
608 .probe = mxc_spi_probe,
609};
610#endif