blob: c61ab74edcbd789b73650c00194c27efdd8e75d6 [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 Glass4dcacfc2020-05-10 11:40:13 -060012#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090014#include <linux/errno.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020015#include <asm/io.h>
Stefano Babic7faee912011-08-21 10:45:44 +020016#include <asm/gpio.h>
Stefano Babic78129d92011-03-14 15:43:56 +010017#include <asm/arch/imx-regs.h>
18#include <asm/arch/clock.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020019#include <asm/mach-imx/spi.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020020
Peng Fanea0bce62017-08-09 13:09:33 +080021DECLARE_GLOBAL_DATA_PTR;
22
Marek Vasuteb68aa12021-01-19 00:58:32 +010023/* MX35 and older is CSPI */
24#if defined(CONFIG_MX25) || defined(CONFIG_MX31) || defined(CONFIG_MX35)
25#define MXC_CSPI
26struct cspi_regs {
27 u32 rxdata;
28 u32 txdata;
29 u32 ctrl;
30 u32 intr;
31 u32 dma;
32 u32 stat;
33 u32 period;
34 u32 test;
35};
36
37#define MXC_CSPICTRL_EN BIT(0)
38#define MXC_CSPICTRL_MODE BIT(1)
39#define MXC_CSPICTRL_XCH BIT(2)
40#define MXC_CSPICTRL_SMC BIT(3)
41#define MXC_CSPICTRL_POL BIT(4)
42#define MXC_CSPICTRL_PHA BIT(5)
43#define MXC_CSPICTRL_SSCTL BIT(6)
44#define MXC_CSPICTRL_SSPOL BIT(7)
45#define MXC_CSPICTRL_DATARATE(x) (((x) & 0x7) << 16)
46#define MXC_CSPICTRL_RXOVF BIT(6)
47#define MXC_CSPIPERIOD_32KHZ BIT(15)
48#define MAX_SPI_BYTES 4
49#if defined(CONFIG_MX25) || defined(CONFIG_MX35)
50#define MXC_CSPICTRL_CHIPSELECT(x) (((x) & 0x3) << 12)
51#define MXC_CSPICTRL_BITCOUNT(x) (((x) & 0xfff) << 20)
52#define MXC_CSPICTRL_TC BIT(7)
53#define MXC_CSPICTRL_MAXBITS 0xfff
54#else /* MX31 */
55#define MXC_CSPICTRL_CHIPSELECT(x) (((x) & 0x3) << 24)
56#define MXC_CSPICTRL_BITCOUNT(x) (((x) & 0x1f) << 8)
57#define MXC_CSPICTRL_TC BIT(8)
58#define MXC_CSPICTRL_MAXBITS 0x1f
59#endif
60
61#else /* MX51 and newer is ECSPI */
62#define MXC_ECSPI
63struct cspi_regs {
64 u32 rxdata;
65 u32 txdata;
66 u32 ctrl;
67 u32 cfg;
68 u32 intr;
69 u32 dma;
70 u32 stat;
71 u32 period;
72};
73
74#define MXC_CSPICTRL_EN BIT(0)
75#define MXC_CSPICTRL_MODE BIT(1)
76#define MXC_CSPICTRL_XCH BIT(2)
77#define MXC_CSPICTRL_MODE_MASK (0xf << 4)
78#define MXC_CSPICTRL_CHIPSELECT(x) (((x) & 0x3) << 12)
79#define MXC_CSPICTRL_BITCOUNT(x) (((x) & 0xfff) << 20)
80#define MXC_CSPICTRL_PREDIV(x) (((x) & 0xF) << 12)
81#define MXC_CSPICTRL_POSTDIV(x) (((x) & 0xF) << 8)
82#define MXC_CSPICTRL_SELCHAN(x) (((x) & 0x3) << 18)
83#define MXC_CSPICTRL_MAXBITS 0xfff
84#define MXC_CSPICTRL_TC BIT(7)
85#define MXC_CSPICTRL_RXOVF BIT(6)
86#define MXC_CSPIPERIOD_32KHZ BIT(15)
87#define MAX_SPI_BYTES 32
88
89/* Bit position inside CTRL register to be associated with SS */
90#define MXC_CSPICTRL_CHAN 18
91
92/* Bit position inside CON register to be associated with SS */
93#define MXC_CSPICON_PHA 0 /* SCLK phase control */
94#define MXC_CSPICON_POL 4 /* SCLK polarity */
95#define MXC_CSPICON_SSPOL 12 /* SS polarity */
96#define MXC_CSPICON_CTL 20 /* inactive state of SCLK */
97#endif
98
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020099#ifdef CONFIG_MX27
100/* i.MX27 has a completely wrong register layout and register definitions in the
101 * datasheet, the correct one is in the Freescale's Linux driver */
102
Helmut Raiger785efc92011-06-15 01:45:45 +0000103#error "i.MX27 CSPI not supported due to drastic differences in register definitions" \
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200104"See linux mxc_spi driver from Freescale for details."
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000105#endif
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000106
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300107__weak int board_spi_cs_gpio(unsigned bus, unsigned cs)
108{
109 return -1;
110}
111
Stefano Babicd77fe992010-07-06 17:05:06 +0200112#define OUT MXC_GPIO_DIRECTION_OUT
113
Stefano Babic28580452011-01-19 22:46:33 +0000114#define reg_read readl
115#define reg_write(a, v) writel(v, a)
116
Heiko Schocherb77c8882014-07-14 10:22:11 +0200117#if !defined(CONFIG_SYS_SPI_MXC_WAIT)
118#define CONFIG_SYS_SPI_MXC_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
119#endif
120
Heiko Schocher053c2442019-05-26 12:15:47 +0200121#define MAX_CS_COUNT 4
122
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200123struct mxc_spi_slave {
124 struct spi_slave slave;
125 unsigned long base;
126 u32 ctrl_reg;
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000127#if defined(MXC_ECSPI)
Stefano Babic6e6f4552010-04-04 22:43:38 +0200128 u32 cfg_reg;
129#endif
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100130 int gpio;
Stefano Babicd77fe992010-07-06 17:05:06 +0200131 int ss_pol;
Markus Niebel8f769cf2014-10-23 16:09:39 +0200132 unsigned int max_hz;
133 unsigned int mode;
Peng Fanea0bce62017-08-09 13:09:33 +0800134 struct gpio_desc ss;
Heiko Schocher053c2442019-05-26 12:15:47 +0200135 struct gpio_desc cs_gpios[MAX_CS_COUNT];
136 struct udevice *dev;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200137};
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200138
139static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
140{
141 return container_of(slave, struct mxc_spi_slave, slave);
142}
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200143
Peng Fanea0bce62017-08-09 13:09:33 +0800144static void mxc_spi_cs_activate(struct mxc_spi_slave *mxcs)
Stefano Babic6e6f4552010-04-04 22:43:38 +0200145{
Lukasz Majewski76f442982020-06-04 23:11:53 +0800146#if CONFIG_IS_ENABLED(DM_SPI)
Heiko Schocher053c2442019-05-26 12:15:47 +0200147 struct udevice *dev = mxcs->dev;
Simon Glassb75b15b2020-12-03 16:55:23 -0700148 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Heiko Schocher053c2442019-05-26 12:15:47 +0200149
150 u32 cs = slave_plat->cs;
151
152 if (!dm_gpio_is_valid(&mxcs->cs_gpios[cs]))
153 return;
154
155 dm_gpio_set_value(&mxcs->cs_gpios[cs], 1);
156#else
157 if (mxcs->gpio > 0)
158 gpio_set_value(mxcs->gpio, mxcs->ss_pol);
159#endif
Stefano Babic6e6f4552010-04-04 22:43:38 +0200160}
161
Peng Fanea0bce62017-08-09 13:09:33 +0800162static void mxc_spi_cs_deactivate(struct mxc_spi_slave *mxcs)
Stefano Babic6e6f4552010-04-04 22:43:38 +0200163{
Lukasz Majewski76f442982020-06-04 23:11:53 +0800164#if CONFIG_IS_ENABLED(DM_SPI)
Heiko Schocher053c2442019-05-26 12:15:47 +0200165 struct udevice *dev = mxcs->dev;
Simon Glassb75b15b2020-12-03 16:55:23 -0700166 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Heiko Schocher053c2442019-05-26 12:15:47 +0200167
168 u32 cs = slave_plat->cs;
169
170 if (!dm_gpio_is_valid(&mxcs->cs_gpios[cs]))
171 return;
172
173 dm_gpio_set_value(&mxcs->cs_gpios[cs], 0);
174#else
175 if (mxcs->gpio > 0)
176 gpio_set_value(mxcs->gpio, !(mxcs->ss_pol));
177#endif
Stefano Babic6e6f4552010-04-04 22:43:38 +0200178}
179
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000180u32 get_cspi_div(u32 div)
181{
182 int i;
183
184 for (i = 0; i < 8; i++) {
185 if (div <= (4 << i))
186 return i;
187 }
188 return i;
189}
190
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000191#ifdef MXC_CSPI
Markus Niebel8f769cf2014-10-23 16:09:39 +0200192static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000193{
194 unsigned int ctrl_reg;
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000195 u32 clk_src;
196 u32 div;
Markus Niebel8f769cf2014-10-23 16:09:39 +0200197 unsigned int max_hz = mxcs->max_hz;
198 unsigned int mode = mxcs->mode;
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000199
200 clk_src = mxc_get_clock(MXC_CSPI_CLK);
201
Benoît Thébaudeau884622b2012-08-10 08:51:50 +0000202 div = DIV_ROUND_UP(clk_src, max_hz);
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000203 div = get_cspi_div(div);
204
205 debug("clk %d Hz, div %d, real clk %d Hz\n",
206 max_hz, div, clk_src / (4 << div));
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000207
208 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
209 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000210 MXC_CSPICTRL_DATARATE(div) |
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000211 MXC_CSPICTRL_EN |
212#ifdef CONFIG_MX35
213 MXC_CSPICTRL_SSCTL |
214#endif
215 MXC_CSPICTRL_MODE;
216
217 if (mode & SPI_CPHA)
218 ctrl_reg |= MXC_CSPICTRL_PHA;
219 if (mode & SPI_CPOL)
220 ctrl_reg |= MXC_CSPICTRL_POL;
221 if (mode & SPI_CS_HIGH)
222 ctrl_reg |= MXC_CSPICTRL_SSPOL;
223 mxcs->ctrl_reg = ctrl_reg;
224
225 return 0;
226}
227#endif
228
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000229#ifdef MXC_ECSPI
Markus Niebel8f769cf2014-10-23 16:09:39 +0200230static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
Stefano Babic6e6f4552010-04-04 22:43:38 +0200231{
232 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
Dirk Behmeb177b712013-05-11 07:25:54 +0200233 s32 reg_ctrl, reg_config;
Markus Niebel6683e622014-02-17 17:33:17 +0100234 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0;
235 u32 pre_div = 0, post_div = 0;
Stefano Babic28580452011-01-19 22:46:33 +0000236 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Markus Niebel8f769cf2014-10-23 16:09:39 +0200237 unsigned int max_hz = mxcs->max_hz;
238 unsigned int mode = mxcs->mode;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200239
Fabio Estevam833fb552013-04-09 13:06:25 +0000240 /*
241 * Reset SPI and set all CSs to master mode, if toggling
242 * between slave and master mode we might see a glitch
243 * on the clock line
244 */
245 reg_ctrl = MXC_CSPICTRL_MODE_MASK;
246 reg_write(&regs->ctrl, reg_ctrl);
247 reg_ctrl |= MXC_CSPICTRL_EN;
248 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200249
Stefano Babic6e6f4552010-04-04 22:43:38 +0200250 if (clk_src > max_hz) {
Dirk Behmeb177b712013-05-11 07:25:54 +0200251 pre_div = (clk_src - 1) / max_hz;
252 /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
253 post_div = fls(pre_div);
254 if (post_div > 4) {
255 post_div -= 4;
256 if (post_div >= 16) {
Stefano Babic6e6f4552010-04-04 22:43:38 +0200257 printf("Error: no divider for the freq: %d\n",
258 max_hz);
259 return -1;
260 }
Dirk Behmeb177b712013-05-11 07:25:54 +0200261 pre_div >>= post_div;
262 } else {
263 post_div = 0;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200264 }
265 }
266
267 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
268 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
269 MXC_CSPICTRL_SELCHAN(cs);
270 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
271 MXC_CSPICTRL_PREDIV(pre_div);
272 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
273 MXC_CSPICTRL_POSTDIV(post_div);
274
Stefano Babic6e6f4552010-04-04 22:43:38 +0200275 if (mode & SPI_CS_HIGH)
276 ss_pol = 1;
277
Markus Niebel6683e622014-02-17 17:33:17 +0100278 if (mode & SPI_CPOL) {
Stefano Babic6e6f4552010-04-04 22:43:38 +0200279 sclkpol = 1;
Markus Niebel6683e622014-02-17 17:33:17 +0100280 sclkctl = 1;
281 }
Stefano Babic6e6f4552010-04-04 22:43:38 +0200282
283 if (mode & SPI_CPHA)
284 sclkpha = 1;
285
Stefano Babic28580452011-01-19 22:46:33 +0000286 reg_config = reg_read(&regs->cfg);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200287
288 /*
289 * Configuration register setup
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000290 * The MX51 supports different setup for each SS
Stefano Babic6e6f4552010-04-04 22:43:38 +0200291 */
292 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
293 (ss_pol << (cs + MXC_CSPICON_SSPOL));
294 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
295 (sclkpol << (cs + MXC_CSPICON_POL));
Markus Niebel6683e622014-02-17 17:33:17 +0100296 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_CTL))) |
297 (sclkctl << (cs + MXC_CSPICON_CTL));
Stefano Babic6e6f4552010-04-04 22:43:38 +0200298 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
299 (sclkpha << (cs + MXC_CSPICON_PHA));
300
301 debug("reg_ctrl = 0x%x\n", reg_ctrl);
Stefano Babic28580452011-01-19 22:46:33 +0000302 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200303 debug("reg_config = 0x%x\n", reg_config);
Stefano Babic28580452011-01-19 22:46:33 +0000304 reg_write(&regs->cfg, reg_config);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200305
306 /* save config register and control register */
307 mxcs->ctrl_reg = reg_ctrl;
308 mxcs->cfg_reg = reg_config;
309
310 /* clear interrupt reg */
Stefano Babic28580452011-01-19 22:46:33 +0000311 reg_write(&regs->intr, 0);
312 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200313
314 return 0;
315}
316#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200317
Peng Fanea0bce62017-08-09 13:09:33 +0800318int spi_xchg_single(struct mxc_spi_slave *mxcs, unsigned int bitlen,
Stefano Babic125f82a2010-08-20 12:05:03 +0200319 const u8 *dout, u8 *din, unsigned long flags)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200320{
Axel Linfb7def92013-06-14 21:13:32 +0800321 int nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic125f82a2010-08-20 12:05:03 +0200322 u32 data, cnt, i;
Stefano Babic28580452011-01-19 22:46:33 +0000323 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Heiko Schocherb77c8882014-07-14 10:22:11 +0200324 u32 ts;
325 int status;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200326
Ye Li07955fb2019-01-04 09:26:00 +0000327 debug("%s: bitlen %d dout 0x%lx din 0x%lx\n",
328 __func__, bitlen, (ulong)dout, (ulong)din);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200329
Stefano Babic6e6f4552010-04-04 22:43:38 +0200330 mxcs->ctrl_reg = (mxcs->ctrl_reg &
331 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100332 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200333
Stefano Babic28580452011-01-19 22:46:33 +0000334 reg_write(&regs->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000335#ifdef MXC_ECSPI
Stefano Babic28580452011-01-19 22:46:33 +0000336 reg_write(&regs->cfg, mxcs->cfg_reg);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200337#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200338
Stefano Babic6e6f4552010-04-04 22:43:38 +0200339 /* Clear interrupt register */
Stefano Babic28580452011-01-19 22:46:33 +0000340 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100341
Stefano Babic125f82a2010-08-20 12:05:03 +0200342 /*
343 * The SPI controller works only with words,
344 * check if less than a word is sent.
345 * Access to the FIFO is only 32 bit
346 */
347 if (bitlen % 32) {
348 data = 0;
349 cnt = (bitlen % 32) / 8;
350 if (dout) {
351 for (i = 0; i < cnt; i++) {
352 data = (data << 8) | (*dout++ & 0xFF);
353 }
354 }
355 debug("Sending SPI 0x%x\n", data);
356
Stefano Babic28580452011-01-19 22:46:33 +0000357 reg_write(&regs->txdata, data);
Stefano Babic125f82a2010-08-20 12:05:03 +0200358 nbytes -= cnt;
359 }
360
361 data = 0;
362
363 while (nbytes > 0) {
364 data = 0;
365 if (dout) {
366 /* Buffer is not 32-bit aligned */
367 if ((unsigned long)dout & 0x03) {
368 data = 0;
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000369 for (i = 0; i < 4; i++)
Stefano Babic125f82a2010-08-20 12:05:03 +0200370 data = (data << 8) | (*dout++ & 0xFF);
Stefano Babic125f82a2010-08-20 12:05:03 +0200371 } else {
372 data = *(u32 *)dout;
373 data = cpu_to_be32(data);
Timo Herbrecher64203202013-10-16 00:05:09 +0530374 dout += 4;
Stefano Babic125f82a2010-08-20 12:05:03 +0200375 }
Stefano Babic125f82a2010-08-20 12:05:03 +0200376 }
377 debug("Sending SPI 0x%x\n", data);
Stefano Babic28580452011-01-19 22:46:33 +0000378 reg_write(&regs->txdata, data);
Stefano Babic125f82a2010-08-20 12:05:03 +0200379 nbytes -= 4;
380 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200381
Stefano Babic6e6f4552010-04-04 22:43:38 +0200382 /* FIFO is written, now starts the transfer setting the XCH bit */
Stefano Babic28580452011-01-19 22:46:33 +0000383 reg_write(&regs->ctrl, mxcs->ctrl_reg |
Stefano Babic6e6f4552010-04-04 22:43:38 +0200384 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200385
Heiko Schocherb77c8882014-07-14 10:22:11 +0200386 ts = get_timer(0);
387 status = reg_read(&regs->stat);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200388 /* Wait until the TC (Transfer completed) bit is set */
Heiko Schocherb77c8882014-07-14 10:22:11 +0200389 while ((status & MXC_CSPICTRL_TC) == 0) {
390 if (get_timer(ts) > CONFIG_SYS_SPI_MXC_WAIT) {
391 printf("spi_xchg_single: Timeout!\n");
392 return -1;
393 }
394 status = reg_read(&regs->stat);
395 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200396
Stefano Babic6e6f4552010-04-04 22:43:38 +0200397 /* Transfer completed, clear any pending request */
Stefano Babic28580452011-01-19 22:46:33 +0000398 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200399
Axel Linfb7def92013-06-14 21:13:32 +0800400 nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200401
Stefano Babic125f82a2010-08-20 12:05:03 +0200402 cnt = nbytes % 32;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100403
Stefano Babic125f82a2010-08-20 12:05:03 +0200404 if (bitlen % 32) {
Stefano Babic28580452011-01-19 22:46:33 +0000405 data = reg_read(&regs->rxdata);
Stefano Babic125f82a2010-08-20 12:05:03 +0200406 cnt = (bitlen % 32) / 8;
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000407 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
Stefano Babic125f82a2010-08-20 12:05:03 +0200408 debug("SPI Rx unaligned: 0x%x\n", data);
409 if (din) {
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000410 memcpy(din, &data, cnt);
411 din += cnt;
Stefano Babic125f82a2010-08-20 12:05:03 +0200412 }
413 nbytes -= cnt;
414 }
415
416 while (nbytes > 0) {
417 u32 tmp;
Stefano Babic28580452011-01-19 22:46:33 +0000418 tmp = reg_read(&regs->rxdata);
Stefano Babic125f82a2010-08-20 12:05:03 +0200419 data = cpu_to_be32(tmp);
420 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
Masahiro Yamadadb204642014-11-07 03:03:31 +0900421 cnt = min_t(u32, nbytes, sizeof(data));
Stefano Babic125f82a2010-08-20 12:05:03 +0200422 if (din) {
423 memcpy(din, &data, cnt);
424 din += cnt;
425 }
426 nbytes -= cnt;
427 }
428
429 return 0;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200430
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200431}
432
Peng Fanea0bce62017-08-09 13:09:33 +0800433static int mxc_spi_xfer_internal(struct mxc_spi_slave *mxcs,
434 unsigned int bitlen, const void *dout,
435 void *din, unsigned long flags)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200436{
Axel Linfb7def92013-06-14 21:13:32 +0800437 int n_bytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic125f82a2010-08-20 12:05:03 +0200438 int n_bits;
439 int ret;
440 u32 blk_size;
441 u8 *p_outbuf = (u8 *)dout;
442 u8 *p_inbuf = (u8 *)din;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200443
Peng Fanea0bce62017-08-09 13:09:33 +0800444 if (!mxcs)
445 return -EINVAL;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200446
Stefano Babic125f82a2010-08-20 12:05:03 +0200447 if (flags & SPI_XFER_BEGIN)
Peng Fanea0bce62017-08-09 13:09:33 +0800448 mxc_spi_cs_activate(mxcs);
Magnus Lilja1858a9a2010-02-09 22:05:39 +0100449
Stefano Babic125f82a2010-08-20 12:05:03 +0200450 while (n_bytes > 0) {
Stefano Babic125f82a2010-08-20 12:05:03 +0200451 if (n_bytes < MAX_SPI_BYTES)
452 blk_size = n_bytes;
453 else
454 blk_size = MAX_SPI_BYTES;
455
456 n_bits = blk_size * 8;
457
Peng Fanea0bce62017-08-09 13:09:33 +0800458 ret = spi_xchg_single(mxcs, n_bits, p_outbuf, p_inbuf, 0);
Stefano Babic125f82a2010-08-20 12:05:03 +0200459
460 if (ret)
461 return ret;
462 if (dout)
463 p_outbuf += blk_size;
464 if (din)
465 p_inbuf += blk_size;
466 n_bytes -= blk_size;
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100467 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200468
Stefano Babic125f82a2010-08-20 12:05:03 +0200469 if (flags & SPI_XFER_END) {
Peng Fanea0bce62017-08-09 13:09:33 +0800470 mxc_spi_cs_deactivate(mxcs);
Stefano Babic125f82a2010-08-20 12:05:03 +0200471 }
472
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200473 return 0;
474}
475
Peng Fanea0bce62017-08-09 13:09:33 +0800476static int mxc_spi_claim_bus_internal(struct mxc_spi_slave *mxcs, int cs)
477{
478 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
479 int ret;
480
481 reg_write(&regs->rxdata, 1);
482 udelay(1);
483 ret = spi_cfg_mxc(mxcs, cs);
484 if (ret) {
485 printf("mxc_spi: cannot setup SPI controller\n");
486 return ret;
487 }
488 reg_write(&regs->period, MXC_CSPIPERIOD_32KHZ);
489 reg_write(&regs->intr, 0);
490
491 return 0;
492}
493
Lukasz Majewski76f442982020-06-04 23:11:53 +0800494#if !CONFIG_IS_ENABLED(DM_SPI)
Peng Fanea0bce62017-08-09 13:09:33 +0800495int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
496 void *din, unsigned long flags)
497{
498 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
499
500 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
501}
502
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300503/*
504 * Some SPI devices require active chip-select over multiple
505 * transactions, we achieve this using a GPIO. Still, the SPI
506 * controller has to be configured to use one of its own chipselects.
507 * To use this feature you have to implement board_spi_cs_gpio() to assign
508 * a gpio value for each cs (-1 if cs doesn't need to use gpio).
509 * You must use some unused on this SPI controller cs between 0 and 3.
510 */
511static int setup_cs_gpio(struct mxc_spi_slave *mxcs,
512 unsigned int bus, unsigned int cs)
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100513{
514 int ret;
515
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300516 mxcs->gpio = board_spi_cs_gpio(bus, cs);
517 if (mxcs->gpio == -1)
518 return 0;
519
Peng Fanea0bce62017-08-09 13:09:33 +0800520 gpio_request(mxcs->gpio, "spi-cs");
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300521 ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
522 if (ret) {
523 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
524 return -EINVAL;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100525 }
526
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300527 return 0;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200528}
529
Peng Fanea0bce62017-08-09 13:09:33 +0800530static unsigned long spi_bases[] = {
531 MXC_SPI_BASE_ADDRESSES
532};
533
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200534struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
535 unsigned int max_hz, unsigned int mode)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200536{
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200537 struct mxc_spi_slave *mxcs;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100538 int ret;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200539
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100540 if (bus >= ARRAY_SIZE(spi_bases))
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200541 return NULL;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200542
Markus Niebel8f769cf2014-10-23 16:09:39 +0200543 if (max_hz == 0) {
544 printf("Error: desired clock is 0\n");
545 return NULL;
546 }
547
Simon Glassd034a952013-03-18 19:23:40 +0000548 mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
Stefano Babic125f82a2010-08-20 12:05:03 +0200549 if (!mxcs) {
550 puts("mxc_spi: SPI Slave not allocated !\n");
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100551 return NULL;
Stefano Babic125f82a2010-08-20 12:05:03 +0200552 }
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100553
Fabio Estevam17cd2a82012-11-15 11:23:23 +0000554 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
555
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300556 ret = setup_cs_gpio(mxcs, bus, cs);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100557 if (ret < 0) {
558 free(mxcs);
559 return NULL;
560 }
561
Stefano Babic6e6f4552010-04-04 22:43:38 +0200562 mxcs->base = spi_bases[bus];
Markus Niebel8f769cf2014-10-23 16:09:39 +0200563 mxcs->max_hz = max_hz;
564 mxcs->mode = mode;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200565
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200566 return &mxcs->slave;
567}
568
569void spi_free_slave(struct spi_slave *slave)
570{
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100571 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
572
573 free(mxcs);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200574}
575
576int spi_claim_bus(struct spi_slave *slave)
577{
578 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
579
Peng Fanea0bce62017-08-09 13:09:33 +0800580 return mxc_spi_claim_bus_internal(mxcs, slave->cs);
581}
582
583void spi_release_bus(struct spi_slave *slave)
584{
585 /* TODO: Shut the controller down */
586}
587#else
588
589static int mxc_spi_probe(struct udevice *bus)
590{
Simon Glassfa20e932020-12-03 16:55:20 -0700591 struct mxc_spi_slave *mxcs = dev_get_plat(bus);
Peng Fanea0bce62017-08-09 13:09:33 +0800592 int node = dev_of_offset(bus);
593 const void *blob = gd->fdt_blob;
594 int ret;
Heiko Schocher053c2442019-05-26 12:15:47 +0200595 int i;
Peng Fanea0bce62017-08-09 13:09:33 +0800596
Heiko Schocher053c2442019-05-26 12:15:47 +0200597 ret = gpio_request_list_by_name(bus, "cs-gpios", mxcs->cs_gpios,
598 ARRAY_SIZE(mxcs->cs_gpios), 0);
599 if (ret < 0) {
600 pr_err("Can't get %s gpios! Error: %d", bus->name, ret);
601 return ret;
602 }
603
604 for (i = 0; i < ARRAY_SIZE(mxcs->cs_gpios); i++) {
605 if (!dm_gpio_is_valid(&mxcs->cs_gpios[i]))
606 continue;
607
608 ret = dm_gpio_set_dir_flags(&mxcs->cs_gpios[i],
609 GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
610 if (ret) {
611 dev_err(bus, "Setting cs %d error\n", i);
612 return ret;
613 }
Peng Fanea0bce62017-08-09 13:09:33 +0800614 }
615
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900616 mxcs->base = dev_read_addr(bus);
Heiko Schocher6d49b4e2019-05-26 12:15:46 +0200617 if (mxcs->base == FDT_ADDR_T_NONE)
Peng Fanea0bce62017-08-09 13:09:33 +0800618 return -ENODEV;
619
Peng Fanea0bce62017-08-09 13:09:33 +0800620 mxcs->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
621 20000000);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200622
623 return 0;
624}
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200625
Peng Fanea0bce62017-08-09 13:09:33 +0800626static int mxc_spi_xfer(struct udevice *dev, unsigned int bitlen,
627 const void *dout, void *din, unsigned long flags)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200628{
Simon Glassfa20e932020-12-03 16:55:20 -0700629 struct mxc_spi_slave *mxcs = dev_get_plat(dev->parent);
Peng Fanea0bce62017-08-09 13:09:33 +0800630
631
632 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
633}
634
635static int mxc_spi_claim_bus(struct udevice *dev)
636{
Simon Glassfa20e932020-12-03 16:55:20 -0700637 struct mxc_spi_slave *mxcs = dev_get_plat(dev->parent);
Simon Glassb75b15b2020-12-03 16:55:23 -0700638 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Peng Fanea0bce62017-08-09 13:09:33 +0800639
Heiko Schocher053c2442019-05-26 12:15:47 +0200640 mxcs->dev = dev;
641
Peng Fanea0bce62017-08-09 13:09:33 +0800642 return mxc_spi_claim_bus_internal(mxcs, slave_plat->cs);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200643}
Peng Fanea0bce62017-08-09 13:09:33 +0800644
645static int mxc_spi_release_bus(struct udevice *dev)
646{
647 return 0;
648}
649
650static int mxc_spi_set_speed(struct udevice *bus, uint speed)
651{
652 /* Nothing to do */
653 return 0;
654}
655
656static int mxc_spi_set_mode(struct udevice *bus, uint mode)
657{
Simon Glassfa20e932020-12-03 16:55:20 -0700658 struct mxc_spi_slave *mxcs = dev_get_plat(bus);
Peng Fanea0bce62017-08-09 13:09:33 +0800659
660 mxcs->mode = mode;
661 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
662
663 return 0;
664}
665
666static const struct dm_spi_ops mxc_spi_ops = {
667 .claim_bus = mxc_spi_claim_bus,
668 .release_bus = mxc_spi_release_bus,
669 .xfer = mxc_spi_xfer,
670 .set_speed = mxc_spi_set_speed,
671 .set_mode = mxc_spi_set_mode,
672};
673
674static const struct udevice_id mxc_spi_ids[] = {
675 { .compatible = "fsl,imx51-ecspi" },
676 { }
677};
678
679U_BOOT_DRIVER(mxc_spi) = {
680 .name = "mxc_spi",
681 .id = UCLASS_SPI,
682 .of_match = mxc_spi_ids,
683 .ops = &mxc_spi_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -0700684 .plat_auto = sizeof(struct mxc_spi_slave),
Peng Fanea0bce62017-08-09 13:09:33 +0800685 .probe = mxc_spi_probe,
686};
687#endif