blob: 5adfdf8b5fc68eaaecc8bea6861805bd8b77208f [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>
Marek Vasutbc0d3c82021-01-19 00:58:33 +01007#include <clk.h>
Peng Fanea0bce62017-08-09 13:09:33 +08008#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020010#include <malloc.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020011#include <spi.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060014#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090016#include <linux/errno.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020017#include <asm/io.h>
Stefano Babic7faee912011-08-21 10:45:44 +020018#include <asm/gpio.h>
Stefano Babic78129d92011-03-14 15:43:56 +010019#include <asm/arch/imx-regs.h>
20#include <asm/arch/clock.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020021#include <asm/mach-imx/spi.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020022
Peng Fanea0bce62017-08-09 13:09:33 +080023DECLARE_GLOBAL_DATA_PTR;
24
Marek Vasuteb68aa12021-01-19 00:58:32 +010025/* MX35 and older is CSPI */
Tom Rinieac76b82021-09-09 07:54:50 -040026#if defined(CONFIG_MX31)
Marek Vasuteb68aa12021-01-19 00:58:32 +010027#define MXC_CSPI
28struct cspi_regs {
29 u32 rxdata;
30 u32 txdata;
31 u32 ctrl;
32 u32 intr;
33 u32 dma;
34 u32 stat;
35 u32 period;
36 u32 test;
37};
38
39#define MXC_CSPICTRL_EN BIT(0)
40#define MXC_CSPICTRL_MODE BIT(1)
41#define MXC_CSPICTRL_XCH BIT(2)
42#define MXC_CSPICTRL_SMC BIT(3)
43#define MXC_CSPICTRL_POL BIT(4)
44#define MXC_CSPICTRL_PHA BIT(5)
45#define MXC_CSPICTRL_SSCTL BIT(6)
46#define MXC_CSPICTRL_SSPOL BIT(7)
47#define MXC_CSPICTRL_DATARATE(x) (((x) & 0x7) << 16)
48#define MXC_CSPICTRL_RXOVF BIT(6)
49#define MXC_CSPIPERIOD_32KHZ BIT(15)
50#define MAX_SPI_BYTES 4
Marek Vasuteb68aa12021-01-19 00:58:32 +010051#define MXC_CSPICTRL_CHIPSELECT(x) (((x) & 0x3) << 24)
52#define MXC_CSPICTRL_BITCOUNT(x) (((x) & 0x1f) << 8)
53#define MXC_CSPICTRL_TC BIT(8)
54#define MXC_CSPICTRL_MAXBITS 0x1f
Marek Vasuteb68aa12021-01-19 00:58:32 +010055
56#else /* MX51 and newer is ECSPI */
57#define MXC_ECSPI
58struct cspi_regs {
59 u32 rxdata;
60 u32 txdata;
61 u32 ctrl;
62 u32 cfg;
63 u32 intr;
64 u32 dma;
65 u32 stat;
66 u32 period;
67};
68
69#define MXC_CSPICTRL_EN BIT(0)
70#define MXC_CSPICTRL_MODE BIT(1)
71#define MXC_CSPICTRL_XCH BIT(2)
72#define MXC_CSPICTRL_MODE_MASK (0xf << 4)
73#define MXC_CSPICTRL_CHIPSELECT(x) (((x) & 0x3) << 12)
74#define MXC_CSPICTRL_BITCOUNT(x) (((x) & 0xfff) << 20)
75#define MXC_CSPICTRL_PREDIV(x) (((x) & 0xF) << 12)
76#define MXC_CSPICTRL_POSTDIV(x) (((x) & 0xF) << 8)
77#define MXC_CSPICTRL_SELCHAN(x) (((x) & 0x3) << 18)
78#define MXC_CSPICTRL_MAXBITS 0xfff
79#define MXC_CSPICTRL_TC BIT(7)
80#define MXC_CSPICTRL_RXOVF BIT(6)
81#define MXC_CSPIPERIOD_32KHZ BIT(15)
82#define MAX_SPI_BYTES 32
83
84/* Bit position inside CTRL register to be associated with SS */
85#define MXC_CSPICTRL_CHAN 18
86
87/* Bit position inside CON register to be associated with SS */
88#define MXC_CSPICON_PHA 0 /* SCLK phase control */
89#define MXC_CSPICON_POL 4 /* SCLK polarity */
90#define MXC_CSPICON_SSPOL 12 /* SS polarity */
91#define MXC_CSPICON_CTL 20 /* inactive state of SCLK */
92#endif
93
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020094#ifdef CONFIG_MX27
95/* i.MX27 has a completely wrong register layout and register definitions in the
96 * datasheet, the correct one is in the Freescale's Linux driver */
97
Helmut Raiger785efc92011-06-15 01:45:45 +000098#error "i.MX27 CSPI not supported due to drastic differences in register definitions" \
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020099"See linux mxc_spi driver from Freescale for details."
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000100#endif
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000101
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300102__weak int board_spi_cs_gpio(unsigned bus, unsigned cs)
103{
104 return -1;
105}
106
Stefano Babicd77fe992010-07-06 17:05:06 +0200107#define OUT MXC_GPIO_DIRECTION_OUT
108
Stefano Babic28580452011-01-19 22:46:33 +0000109#define reg_read readl
110#define reg_write(a, v) writel(v, a)
111
Heiko Schocherb77c8882014-07-14 10:22:11 +0200112#if !defined(CONFIG_SYS_SPI_MXC_WAIT)
113#define CONFIG_SYS_SPI_MXC_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
114#endif
115
Heiko Schocher053c2442019-05-26 12:15:47 +0200116#define MAX_CS_COUNT 4
117
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200118struct mxc_spi_slave {
119 struct spi_slave slave;
120 unsigned long base;
121 u32 ctrl_reg;
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000122#if defined(MXC_ECSPI)
Stefano Babic6e6f4552010-04-04 22:43:38 +0200123 u32 cfg_reg;
124#endif
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100125 int gpio;
Stefano Babicd77fe992010-07-06 17:05:06 +0200126 int ss_pol;
Markus Niebel8f769cf2014-10-23 16:09:39 +0200127 unsigned int max_hz;
128 unsigned int mode;
Peng Fanea0bce62017-08-09 13:09:33 +0800129 struct gpio_desc ss;
Heiko Schocher053c2442019-05-26 12:15:47 +0200130 struct gpio_desc cs_gpios[MAX_CS_COUNT];
131 struct udevice *dev;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200132};
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200133
134static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
135{
136 return container_of(slave, struct mxc_spi_slave, slave);
137}
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200138
Peng Fanea0bce62017-08-09 13:09:33 +0800139static void mxc_spi_cs_activate(struct mxc_spi_slave *mxcs)
Stefano Babic6e6f4552010-04-04 22:43:38 +0200140{
Lukasz Majewski76f442982020-06-04 23:11:53 +0800141#if CONFIG_IS_ENABLED(DM_SPI)
Heiko Schocher053c2442019-05-26 12:15:47 +0200142 struct udevice *dev = mxcs->dev;
Simon Glassb75b15b2020-12-03 16:55:23 -0700143 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Heiko Schocher053c2442019-05-26 12:15:47 +0200144
145 u32 cs = slave_plat->cs;
146
147 if (!dm_gpio_is_valid(&mxcs->cs_gpios[cs]))
148 return;
149
150 dm_gpio_set_value(&mxcs->cs_gpios[cs], 1);
151#else
152 if (mxcs->gpio > 0)
153 gpio_set_value(mxcs->gpio, mxcs->ss_pol);
154#endif
Stefano Babic6e6f4552010-04-04 22:43:38 +0200155}
156
Peng Fanea0bce62017-08-09 13:09:33 +0800157static void mxc_spi_cs_deactivate(struct mxc_spi_slave *mxcs)
Stefano Babic6e6f4552010-04-04 22:43:38 +0200158{
Lukasz Majewski76f442982020-06-04 23:11:53 +0800159#if CONFIG_IS_ENABLED(DM_SPI)
Heiko Schocher053c2442019-05-26 12:15:47 +0200160 struct udevice *dev = mxcs->dev;
Simon Glassb75b15b2020-12-03 16:55:23 -0700161 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Heiko Schocher053c2442019-05-26 12:15:47 +0200162
163 u32 cs = slave_plat->cs;
164
165 if (!dm_gpio_is_valid(&mxcs->cs_gpios[cs]))
166 return;
167
168 dm_gpio_set_value(&mxcs->cs_gpios[cs], 0);
169#else
170 if (mxcs->gpio > 0)
171 gpio_set_value(mxcs->gpio, !(mxcs->ss_pol));
172#endif
Stefano Babic6e6f4552010-04-04 22:43:38 +0200173}
174
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000175u32 get_cspi_div(u32 div)
176{
177 int i;
178
179 for (i = 0; i < 8; i++) {
180 if (div <= (4 << i))
181 return i;
182 }
183 return i;
184}
185
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000186#ifdef MXC_CSPI
Markus Niebel8f769cf2014-10-23 16:09:39 +0200187static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000188{
189 unsigned int ctrl_reg;
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000190 u32 clk_src;
191 u32 div;
Markus Niebel8f769cf2014-10-23 16:09:39 +0200192 unsigned int max_hz = mxcs->max_hz;
193 unsigned int mode = mxcs->mode;
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000194
195 clk_src = mxc_get_clock(MXC_CSPI_CLK);
196
Benoît Thébaudeau884622b2012-08-10 08:51:50 +0000197 div = DIV_ROUND_UP(clk_src, max_hz);
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000198 div = get_cspi_div(div);
199
200 debug("clk %d Hz, div %d, real clk %d Hz\n",
201 max_hz, div, clk_src / (4 << div));
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000202
203 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
204 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000205 MXC_CSPICTRL_DATARATE(div) |
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000206 MXC_CSPICTRL_EN |
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000207 MXC_CSPICTRL_MODE;
208
209 if (mode & SPI_CPHA)
210 ctrl_reg |= MXC_CSPICTRL_PHA;
211 if (mode & SPI_CPOL)
212 ctrl_reg |= MXC_CSPICTRL_POL;
213 if (mode & SPI_CS_HIGH)
214 ctrl_reg |= MXC_CSPICTRL_SSPOL;
215 mxcs->ctrl_reg = ctrl_reg;
216
217 return 0;
218}
219#endif
220
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000221#ifdef MXC_ECSPI
Markus Niebel8f769cf2014-10-23 16:09:39 +0200222static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
Stefano Babic6e6f4552010-04-04 22:43:38 +0200223{
224 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
Dirk Behmeb177b712013-05-11 07:25:54 +0200225 s32 reg_ctrl, reg_config;
Markus Niebel6683e622014-02-17 17:33:17 +0100226 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0;
227 u32 pre_div = 0, post_div = 0;
Stefano Babic28580452011-01-19 22:46:33 +0000228 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Markus Niebel8f769cf2014-10-23 16:09:39 +0200229 unsigned int max_hz = mxcs->max_hz;
230 unsigned int mode = mxcs->mode;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200231
Fabio Estevam833fb552013-04-09 13:06:25 +0000232 /*
233 * Reset SPI and set all CSs to master mode, if toggling
234 * between slave and master mode we might see a glitch
235 * on the clock line
236 */
237 reg_ctrl = MXC_CSPICTRL_MODE_MASK;
238 reg_write(&regs->ctrl, reg_ctrl);
239 reg_ctrl |= MXC_CSPICTRL_EN;
240 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200241
Stefano Babic6e6f4552010-04-04 22:43:38 +0200242 if (clk_src > max_hz) {
Dirk Behmeb177b712013-05-11 07:25:54 +0200243 pre_div = (clk_src - 1) / max_hz;
244 /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
245 post_div = fls(pre_div);
246 if (post_div > 4) {
247 post_div -= 4;
248 if (post_div >= 16) {
Stefano Babic6e6f4552010-04-04 22:43:38 +0200249 printf("Error: no divider for the freq: %d\n",
250 max_hz);
251 return -1;
252 }
Dirk Behmeb177b712013-05-11 07:25:54 +0200253 pre_div >>= post_div;
254 } else {
255 post_div = 0;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200256 }
257 }
258
259 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
260 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
261 MXC_CSPICTRL_SELCHAN(cs);
262 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
263 MXC_CSPICTRL_PREDIV(pre_div);
264 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
265 MXC_CSPICTRL_POSTDIV(post_div);
266
Stefano Babic6e6f4552010-04-04 22:43:38 +0200267 if (mode & SPI_CS_HIGH)
268 ss_pol = 1;
269
Markus Niebel6683e622014-02-17 17:33:17 +0100270 if (mode & SPI_CPOL) {
Stefano Babic6e6f4552010-04-04 22:43:38 +0200271 sclkpol = 1;
Markus Niebel6683e622014-02-17 17:33:17 +0100272 sclkctl = 1;
273 }
Stefano Babic6e6f4552010-04-04 22:43:38 +0200274
275 if (mode & SPI_CPHA)
276 sclkpha = 1;
277
Stefano Babic28580452011-01-19 22:46:33 +0000278 reg_config = reg_read(&regs->cfg);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200279
280 /*
281 * Configuration register setup
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000282 * The MX51 supports different setup for each SS
Stefano Babic6e6f4552010-04-04 22:43:38 +0200283 */
284 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
285 (ss_pol << (cs + MXC_CSPICON_SSPOL));
286 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
287 (sclkpol << (cs + MXC_CSPICON_POL));
Markus Niebel6683e622014-02-17 17:33:17 +0100288 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_CTL))) |
289 (sclkctl << (cs + MXC_CSPICON_CTL));
Stefano Babic6e6f4552010-04-04 22:43:38 +0200290 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
291 (sclkpha << (cs + MXC_CSPICON_PHA));
292
293 debug("reg_ctrl = 0x%x\n", reg_ctrl);
Stefano Babic28580452011-01-19 22:46:33 +0000294 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200295 debug("reg_config = 0x%x\n", reg_config);
Stefano Babic28580452011-01-19 22:46:33 +0000296 reg_write(&regs->cfg, reg_config);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200297
298 /* save config register and control register */
299 mxcs->ctrl_reg = reg_ctrl;
300 mxcs->cfg_reg = reg_config;
301
302 /* clear interrupt reg */
Stefano Babic28580452011-01-19 22:46:33 +0000303 reg_write(&regs->intr, 0);
304 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200305
306 return 0;
307}
308#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200309
Peng Fanea0bce62017-08-09 13:09:33 +0800310int spi_xchg_single(struct mxc_spi_slave *mxcs, unsigned int bitlen,
Stefano Babic125f82a2010-08-20 12:05:03 +0200311 const u8 *dout, u8 *din, unsigned long flags)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200312{
Axel Linfb7def92013-06-14 21:13:32 +0800313 int nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic125f82a2010-08-20 12:05:03 +0200314 u32 data, cnt, i;
Stefano Babic28580452011-01-19 22:46:33 +0000315 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Heiko Schocherb77c8882014-07-14 10:22:11 +0200316 u32 ts;
317 int status;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200318
Ye Li07955fb2019-01-04 09:26:00 +0000319 debug("%s: bitlen %d dout 0x%lx din 0x%lx\n",
320 __func__, bitlen, (ulong)dout, (ulong)din);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200321
Stefano Babic6e6f4552010-04-04 22:43:38 +0200322 mxcs->ctrl_reg = (mxcs->ctrl_reg &
323 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100324 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200325
Stefano Babic28580452011-01-19 22:46:33 +0000326 reg_write(&regs->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000327#ifdef MXC_ECSPI
Stefano Babic28580452011-01-19 22:46:33 +0000328 reg_write(&regs->cfg, mxcs->cfg_reg);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200329#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200330
Stefano Babic6e6f4552010-04-04 22:43:38 +0200331 /* Clear interrupt register */
Stefano Babic28580452011-01-19 22:46:33 +0000332 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100333
Stefano Babic125f82a2010-08-20 12:05:03 +0200334 /*
335 * The SPI controller works only with words,
336 * check if less than a word is sent.
337 * Access to the FIFO is only 32 bit
338 */
339 if (bitlen % 32) {
340 data = 0;
341 cnt = (bitlen % 32) / 8;
342 if (dout) {
343 for (i = 0; i < cnt; i++) {
344 data = (data << 8) | (*dout++ & 0xFF);
345 }
346 }
347 debug("Sending SPI 0x%x\n", data);
348
Stefano Babic28580452011-01-19 22:46:33 +0000349 reg_write(&regs->txdata, data);
Stefano Babic125f82a2010-08-20 12:05:03 +0200350 nbytes -= cnt;
351 }
352
353 data = 0;
354
355 while (nbytes > 0) {
356 data = 0;
357 if (dout) {
358 /* Buffer is not 32-bit aligned */
359 if ((unsigned long)dout & 0x03) {
360 data = 0;
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000361 for (i = 0; i < 4; i++)
Stefano Babic125f82a2010-08-20 12:05:03 +0200362 data = (data << 8) | (*dout++ & 0xFF);
Stefano Babic125f82a2010-08-20 12:05:03 +0200363 } else {
364 data = *(u32 *)dout;
365 data = cpu_to_be32(data);
Timo Herbrecher64203202013-10-16 00:05:09 +0530366 dout += 4;
Stefano Babic125f82a2010-08-20 12:05:03 +0200367 }
Stefano Babic125f82a2010-08-20 12:05:03 +0200368 }
369 debug("Sending SPI 0x%x\n", data);
Stefano Babic28580452011-01-19 22:46:33 +0000370 reg_write(&regs->txdata, data);
Stefano Babic125f82a2010-08-20 12:05:03 +0200371 nbytes -= 4;
372 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200373
Stefano Babic6e6f4552010-04-04 22:43:38 +0200374 /* FIFO is written, now starts the transfer setting the XCH bit */
Stefano Babic28580452011-01-19 22:46:33 +0000375 reg_write(&regs->ctrl, mxcs->ctrl_reg |
Stefano Babic6e6f4552010-04-04 22:43:38 +0200376 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200377
Heiko Schocherb77c8882014-07-14 10:22:11 +0200378 ts = get_timer(0);
379 status = reg_read(&regs->stat);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200380 /* Wait until the TC (Transfer completed) bit is set */
Heiko Schocherb77c8882014-07-14 10:22:11 +0200381 while ((status & MXC_CSPICTRL_TC) == 0) {
382 if (get_timer(ts) > CONFIG_SYS_SPI_MXC_WAIT) {
383 printf("spi_xchg_single: Timeout!\n");
384 return -1;
385 }
386 status = reg_read(&regs->stat);
387 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200388
Stefano Babic6e6f4552010-04-04 22:43:38 +0200389 /* Transfer completed, clear any pending request */
Stefano Babic28580452011-01-19 22:46:33 +0000390 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200391
Axel Linfb7def92013-06-14 21:13:32 +0800392 nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200393
Stefano Babic125f82a2010-08-20 12:05:03 +0200394 if (bitlen % 32) {
Stefano Babic28580452011-01-19 22:46:33 +0000395 data = reg_read(&regs->rxdata);
Stefano Babic125f82a2010-08-20 12:05:03 +0200396 cnt = (bitlen % 32) / 8;
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000397 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
Stefano Babic125f82a2010-08-20 12:05:03 +0200398 debug("SPI Rx unaligned: 0x%x\n", data);
399 if (din) {
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000400 memcpy(din, &data, cnt);
401 din += cnt;
Stefano Babic125f82a2010-08-20 12:05:03 +0200402 }
403 nbytes -= cnt;
404 }
405
406 while (nbytes > 0) {
407 u32 tmp;
Stefano Babic28580452011-01-19 22:46:33 +0000408 tmp = reg_read(&regs->rxdata);
Stefano Babic125f82a2010-08-20 12:05:03 +0200409 data = cpu_to_be32(tmp);
410 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
Masahiro Yamadadb204642014-11-07 03:03:31 +0900411 cnt = min_t(u32, nbytes, sizeof(data));
Stefano Babic125f82a2010-08-20 12:05:03 +0200412 if (din) {
413 memcpy(din, &data, cnt);
414 din += cnt;
415 }
416 nbytes -= cnt;
417 }
418
419 return 0;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200420
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200421}
422
Peng Fanea0bce62017-08-09 13:09:33 +0800423static int mxc_spi_xfer_internal(struct mxc_spi_slave *mxcs,
424 unsigned int bitlen, const void *dout,
425 void *din, unsigned long flags)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200426{
Axel Linfb7def92013-06-14 21:13:32 +0800427 int n_bytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic125f82a2010-08-20 12:05:03 +0200428 int n_bits;
429 int ret;
430 u32 blk_size;
431 u8 *p_outbuf = (u8 *)dout;
432 u8 *p_inbuf = (u8 *)din;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200433
Peng Fanea0bce62017-08-09 13:09:33 +0800434 if (!mxcs)
435 return -EINVAL;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200436
Stefano Babic125f82a2010-08-20 12:05:03 +0200437 if (flags & SPI_XFER_BEGIN)
Peng Fanea0bce62017-08-09 13:09:33 +0800438 mxc_spi_cs_activate(mxcs);
Magnus Lilja1858a9a2010-02-09 22:05:39 +0100439
Stefano Babic125f82a2010-08-20 12:05:03 +0200440 while (n_bytes > 0) {
Stefano Babic125f82a2010-08-20 12:05:03 +0200441 if (n_bytes < MAX_SPI_BYTES)
442 blk_size = n_bytes;
443 else
444 blk_size = MAX_SPI_BYTES;
445
446 n_bits = blk_size * 8;
447
Peng Fanea0bce62017-08-09 13:09:33 +0800448 ret = spi_xchg_single(mxcs, n_bits, p_outbuf, p_inbuf, 0);
Stefano Babic125f82a2010-08-20 12:05:03 +0200449
450 if (ret)
451 return ret;
452 if (dout)
453 p_outbuf += blk_size;
454 if (din)
455 p_inbuf += blk_size;
456 n_bytes -= blk_size;
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100457 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200458
Stefano Babic125f82a2010-08-20 12:05:03 +0200459 if (flags & SPI_XFER_END) {
Peng Fanea0bce62017-08-09 13:09:33 +0800460 mxc_spi_cs_deactivate(mxcs);
Stefano Babic125f82a2010-08-20 12:05:03 +0200461 }
462
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200463 return 0;
464}
465
Peng Fanea0bce62017-08-09 13:09:33 +0800466static int mxc_spi_claim_bus_internal(struct mxc_spi_slave *mxcs, int cs)
467{
468 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
469 int ret;
470
471 reg_write(&regs->rxdata, 1);
472 udelay(1);
473 ret = spi_cfg_mxc(mxcs, cs);
474 if (ret) {
475 printf("mxc_spi: cannot setup SPI controller\n");
476 return ret;
477 }
478 reg_write(&regs->period, MXC_CSPIPERIOD_32KHZ);
479 reg_write(&regs->intr, 0);
480
481 return 0;
482}
483
Lukasz Majewski76f442982020-06-04 23:11:53 +0800484#if !CONFIG_IS_ENABLED(DM_SPI)
Peng Fanea0bce62017-08-09 13:09:33 +0800485int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
486 void *din, unsigned long flags)
487{
488 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
489
490 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
491}
492
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300493/*
494 * Some SPI devices require active chip-select over multiple
495 * transactions, we achieve this using a GPIO. Still, the SPI
496 * controller has to be configured to use one of its own chipselects.
497 * To use this feature you have to implement board_spi_cs_gpio() to assign
498 * a gpio value for each cs (-1 if cs doesn't need to use gpio).
499 * You must use some unused on this SPI controller cs between 0 and 3.
500 */
501static int setup_cs_gpio(struct mxc_spi_slave *mxcs,
502 unsigned int bus, unsigned int cs)
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100503{
504 int ret;
505
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300506 mxcs->gpio = board_spi_cs_gpio(bus, cs);
507 if (mxcs->gpio == -1)
508 return 0;
509
Peng Fanea0bce62017-08-09 13:09:33 +0800510 gpio_request(mxcs->gpio, "spi-cs");
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300511 ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
512 if (ret) {
513 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
514 return -EINVAL;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100515 }
516
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300517 return 0;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200518}
519
Peng Fanea0bce62017-08-09 13:09:33 +0800520static unsigned long spi_bases[] = {
521 MXC_SPI_BASE_ADDRESSES
522};
523
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200524struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
525 unsigned int max_hz, unsigned int mode)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200526{
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200527 struct mxc_spi_slave *mxcs;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100528 int ret;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200529
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100530 if (bus >= ARRAY_SIZE(spi_bases))
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200531 return NULL;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200532
Markus Niebel8f769cf2014-10-23 16:09:39 +0200533 if (max_hz == 0) {
534 printf("Error: desired clock is 0\n");
535 return NULL;
536 }
537
Simon Glassd034a952013-03-18 19:23:40 +0000538 mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
Stefano Babic125f82a2010-08-20 12:05:03 +0200539 if (!mxcs) {
540 puts("mxc_spi: SPI Slave not allocated !\n");
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100541 return NULL;
Stefano Babic125f82a2010-08-20 12:05:03 +0200542 }
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100543
Fabio Estevam17cd2a82012-11-15 11:23:23 +0000544 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
545
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300546 ret = setup_cs_gpio(mxcs, bus, cs);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100547 if (ret < 0) {
548 free(mxcs);
549 return NULL;
550 }
551
Stefano Babic6e6f4552010-04-04 22:43:38 +0200552 mxcs->base = spi_bases[bus];
Markus Niebel8f769cf2014-10-23 16:09:39 +0200553 mxcs->max_hz = max_hz;
554 mxcs->mode = mode;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200555
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200556 return &mxcs->slave;
557}
558
559void spi_free_slave(struct spi_slave *slave)
560{
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100561 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
562
563 free(mxcs);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200564}
565
566int spi_claim_bus(struct spi_slave *slave)
567{
568 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
569
Peng Fanea0bce62017-08-09 13:09:33 +0800570 return mxc_spi_claim_bus_internal(mxcs, slave->cs);
571}
572
573void spi_release_bus(struct spi_slave *slave)
574{
575 /* TODO: Shut the controller down */
576}
577#else
578
579static int mxc_spi_probe(struct udevice *bus)
580{
Simon Glassfa20e932020-12-03 16:55:20 -0700581 struct mxc_spi_slave *mxcs = dev_get_plat(bus);
Peng Fanea0bce62017-08-09 13:09:33 +0800582 int ret;
Heiko Schocher053c2442019-05-26 12:15:47 +0200583 int i;
Peng Fanea0bce62017-08-09 13:09:33 +0800584
Heiko Schocher053c2442019-05-26 12:15:47 +0200585 ret = gpio_request_list_by_name(bus, "cs-gpios", mxcs->cs_gpios,
586 ARRAY_SIZE(mxcs->cs_gpios), 0);
587 if (ret < 0) {
588 pr_err("Can't get %s gpios! Error: %d", bus->name, ret);
589 return ret;
590 }
591
592 for (i = 0; i < ARRAY_SIZE(mxcs->cs_gpios); i++) {
593 if (!dm_gpio_is_valid(&mxcs->cs_gpios[i]))
594 continue;
595
596 ret = dm_gpio_set_dir_flags(&mxcs->cs_gpios[i],
597 GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
598 if (ret) {
599 dev_err(bus, "Setting cs %d error\n", i);
600 return ret;
601 }
Peng Fanea0bce62017-08-09 13:09:33 +0800602 }
603
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900604 mxcs->base = dev_read_addr(bus);
Heiko Schocher6d49b4e2019-05-26 12:15:46 +0200605 if (mxcs->base == FDT_ADDR_T_NONE)
Peng Fanea0bce62017-08-09 13:09:33 +0800606 return -ENODEV;
607
Marek Vasutbc0d3c82021-01-19 00:58:33 +0100608#if CONFIG_IS_ENABLED(CLK)
609 struct clk clk;
610 ret = clk_get_by_index(bus, 0, &clk);
611 if (ret)
612 return ret;
613
614 clk_enable(&clk);
615
616 mxcs->max_hz = clk_get_rate(&clk);
617#else
Stefano Babic2fb24172021-07-10 16:31:29 +0200618 int node = dev_of_offset(bus);
619 const void *blob = gd->fdt_blob;
Peng Fanea0bce62017-08-09 13:09:33 +0800620 mxcs->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
621 20000000);
Marek Vasutbc0d3c82021-01-19 00:58:33 +0100622#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200623
624 return 0;
625}
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200626
Peng Fanea0bce62017-08-09 13:09:33 +0800627static int mxc_spi_xfer(struct udevice *dev, unsigned int bitlen,
628 const void *dout, void *din, unsigned long flags)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200629{
Simon Glassfa20e932020-12-03 16:55:20 -0700630 struct mxc_spi_slave *mxcs = dev_get_plat(dev->parent);
Peng Fanea0bce62017-08-09 13:09:33 +0800631
632
633 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
634}
635
636static int mxc_spi_claim_bus(struct udevice *dev)
637{
Simon Glassfa20e932020-12-03 16:55:20 -0700638 struct mxc_spi_slave *mxcs = dev_get_plat(dev->parent);
Simon Glassb75b15b2020-12-03 16:55:23 -0700639 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Peng Fanea0bce62017-08-09 13:09:33 +0800640
Heiko Schocher053c2442019-05-26 12:15:47 +0200641 mxcs->dev = dev;
642
Peng Fanea0bce62017-08-09 13:09:33 +0800643 return mxc_spi_claim_bus_internal(mxcs, slave_plat->cs);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200644}
Peng Fanea0bce62017-08-09 13:09:33 +0800645
646static int mxc_spi_release_bus(struct udevice *dev)
647{
648 return 0;
649}
650
651static int mxc_spi_set_speed(struct udevice *bus, uint speed)
652{
Marek Vasut060ae382021-02-03 17:53:57 +0100653 struct mxc_spi_slave *mxcs = dev_get_plat(bus);
654
655 mxcs->max_hz = speed;
656
Peng Fanea0bce62017-08-09 13:09:33 +0800657 return 0;
658}
659
660static int mxc_spi_set_mode(struct udevice *bus, uint mode)
661{
Simon Glassfa20e932020-12-03 16:55:20 -0700662 struct mxc_spi_slave *mxcs = dev_get_plat(bus);
Peng Fanea0bce62017-08-09 13:09:33 +0800663
664 mxcs->mode = mode;
665 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
666
667 return 0;
668}
669
670static const struct dm_spi_ops mxc_spi_ops = {
671 .claim_bus = mxc_spi_claim_bus,
672 .release_bus = mxc_spi_release_bus,
673 .xfer = mxc_spi_xfer,
674 .set_speed = mxc_spi_set_speed,
675 .set_mode = mxc_spi_set_mode,
676};
677
678static const struct udevice_id mxc_spi_ids[] = {
679 { .compatible = "fsl,imx51-ecspi" },
680 { }
681};
682
683U_BOOT_DRIVER(mxc_spi) = {
684 .name = "mxc_spi",
685 .id = UCLASS_SPI,
686 .of_match = mxc_spi_ids,
687 .ops = &mxc_spi_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -0700688 .plat_auto = sizeof(struct mxc_spi_slave),
Peng Fanea0bce62017-08-09 13:09:33 +0800689 .probe = mxc_spi_probe,
690};
691#endif