blob: 4d1317c36464cb2f9e732e601baab2c4209206a8 [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>
Haavard Skinnemoend74084a2008-05-16 11:10:31 +02008#include <malloc.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +02009#include <spi.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <dm/device_compat.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090011#include <linux/errno.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020012#include <asm/io.h>
Stefano Babic7faee912011-08-21 10:45:44 +020013#include <asm/gpio.h>
Stefano Babic78129d92011-03-14 15:43:56 +010014#include <asm/arch/imx-regs.h>
15#include <asm/arch/clock.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020016#include <asm/mach-imx/spi.h>
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020017
Peng Fanea0bce62017-08-09 13:09:33 +080018DECLARE_GLOBAL_DATA_PTR;
19
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020020#ifdef CONFIG_MX27
21/* i.MX27 has a completely wrong register layout and register definitions in the
22 * datasheet, the correct one is in the Freescale's Linux driver */
23
Helmut Raiger785efc92011-06-15 01:45:45 +000024#error "i.MX27 CSPI not supported due to drastic differences in register definitions" \
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020025"See linux mxc_spi driver from Freescale for details."
Eric Nelsonfe1e7612012-01-31 07:52:03 +000026#endif
Stefano Babicdcd73cd2011-01-19 22:46:30 +000027
Nikita Kiryanov00cd7382014-08-20 15:08:50 +030028__weak int board_spi_cs_gpio(unsigned bus, unsigned cs)
29{
30 return -1;
31}
32
Stefano Babicd77fe992010-07-06 17:05:06 +020033#define OUT MXC_GPIO_DIRECTION_OUT
34
Stefano Babic28580452011-01-19 22:46:33 +000035#define reg_read readl
36#define reg_write(a, v) writel(v, a)
37
Heiko Schocherb77c8882014-07-14 10:22:11 +020038#if !defined(CONFIG_SYS_SPI_MXC_WAIT)
39#define CONFIG_SYS_SPI_MXC_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
40#endif
41
Heiko Schocher053c2442019-05-26 12:15:47 +020042#define MAX_CS_COUNT 4
43
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020044struct mxc_spi_slave {
45 struct spi_slave slave;
46 unsigned long base;
47 u32 ctrl_reg;
Eric Nelsonfe1e7612012-01-31 07:52:03 +000048#if defined(MXC_ECSPI)
Stefano Babic6e6f4552010-04-04 22:43:38 +020049 u32 cfg_reg;
50#endif
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +010051 int gpio;
Stefano Babicd77fe992010-07-06 17:05:06 +020052 int ss_pol;
Markus Niebel8f769cf2014-10-23 16:09:39 +020053 unsigned int max_hz;
54 unsigned int mode;
Peng Fanea0bce62017-08-09 13:09:33 +080055 struct gpio_desc ss;
Heiko Schocher053c2442019-05-26 12:15:47 +020056 struct gpio_desc cs_gpios[MAX_CS_COUNT];
57 struct udevice *dev;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020058};
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020059
60static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
61{
62 return container_of(slave, struct mxc_spi_slave, slave);
63}
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +020064
Peng Fanea0bce62017-08-09 13:09:33 +080065static void mxc_spi_cs_activate(struct mxc_spi_slave *mxcs)
Stefano Babic6e6f4552010-04-04 22:43:38 +020066{
Heiko Schocher053c2442019-05-26 12:15:47 +020067#if defined(CONFIG_DM_SPI)
68 struct udevice *dev = mxcs->dev;
69 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
70
71 u32 cs = slave_plat->cs;
72
73 if (!dm_gpio_is_valid(&mxcs->cs_gpios[cs]))
74 return;
75
76 dm_gpio_set_value(&mxcs->cs_gpios[cs], 1);
77#else
78 if (mxcs->gpio > 0)
79 gpio_set_value(mxcs->gpio, mxcs->ss_pol);
80#endif
Stefano Babic6e6f4552010-04-04 22:43:38 +020081}
82
Peng Fanea0bce62017-08-09 13:09:33 +080083static void mxc_spi_cs_deactivate(struct mxc_spi_slave *mxcs)
Stefano Babic6e6f4552010-04-04 22:43:38 +020084{
Heiko Schocher053c2442019-05-26 12:15:47 +020085#if defined(CONFIG_DM_SPI)
86 struct udevice *dev = mxcs->dev;
87 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
88
89 u32 cs = slave_plat->cs;
90
91 if (!dm_gpio_is_valid(&mxcs->cs_gpios[cs]))
92 return;
93
94 dm_gpio_set_value(&mxcs->cs_gpios[cs], 0);
95#else
96 if (mxcs->gpio > 0)
97 gpio_set_value(mxcs->gpio, !(mxcs->ss_pol));
98#endif
Stefano Babic6e6f4552010-04-04 22:43:38 +020099}
100
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000101u32 get_cspi_div(u32 div)
102{
103 int i;
104
105 for (i = 0; i < 8; i++) {
106 if (div <= (4 << i))
107 return i;
108 }
109 return i;
110}
111
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000112#ifdef MXC_CSPI
Markus Niebel8f769cf2014-10-23 16:09:39 +0200113static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000114{
115 unsigned int ctrl_reg;
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000116 u32 clk_src;
117 u32 div;
Markus Niebel8f769cf2014-10-23 16:09:39 +0200118 unsigned int max_hz = mxcs->max_hz;
119 unsigned int mode = mxcs->mode;
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000120
121 clk_src = mxc_get_clock(MXC_CSPI_CLK);
122
Benoît Thébaudeau884622b2012-08-10 08:51:50 +0000123 div = DIV_ROUND_UP(clk_src, max_hz);
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000124 div = get_cspi_div(div);
125
126 debug("clk %d Hz, div %d, real clk %d Hz\n",
127 max_hz, div, clk_src / (4 << div));
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000128
129 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
130 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
Anatolij Gustschin0aa35fd2011-01-19 22:46:32 +0000131 MXC_CSPICTRL_DATARATE(div) |
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000132 MXC_CSPICTRL_EN |
133#ifdef CONFIG_MX35
134 MXC_CSPICTRL_SSCTL |
135#endif
136 MXC_CSPICTRL_MODE;
137
138 if (mode & SPI_CPHA)
139 ctrl_reg |= MXC_CSPICTRL_PHA;
140 if (mode & SPI_CPOL)
141 ctrl_reg |= MXC_CSPICTRL_POL;
142 if (mode & SPI_CS_HIGH)
143 ctrl_reg |= MXC_CSPICTRL_SSPOL;
144 mxcs->ctrl_reg = ctrl_reg;
145
146 return 0;
147}
148#endif
149
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000150#ifdef MXC_ECSPI
Markus Niebel8f769cf2014-10-23 16:09:39 +0200151static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
Stefano Babic6e6f4552010-04-04 22:43:38 +0200152{
153 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
Dirk Behmeb177b712013-05-11 07:25:54 +0200154 s32 reg_ctrl, reg_config;
Markus Niebel6683e622014-02-17 17:33:17 +0100155 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0;
156 u32 pre_div = 0, post_div = 0;
Stefano Babic28580452011-01-19 22:46:33 +0000157 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Markus Niebel8f769cf2014-10-23 16:09:39 +0200158 unsigned int max_hz = mxcs->max_hz;
159 unsigned int mode = mxcs->mode;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200160
Fabio Estevam833fb552013-04-09 13:06:25 +0000161 /*
162 * Reset SPI and set all CSs to master mode, if toggling
163 * between slave and master mode we might see a glitch
164 * on the clock line
165 */
166 reg_ctrl = MXC_CSPICTRL_MODE_MASK;
167 reg_write(&regs->ctrl, reg_ctrl);
168 reg_ctrl |= MXC_CSPICTRL_EN;
169 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200170
Stefano Babic6e6f4552010-04-04 22:43:38 +0200171 if (clk_src > max_hz) {
Dirk Behmeb177b712013-05-11 07:25:54 +0200172 pre_div = (clk_src - 1) / max_hz;
173 /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
174 post_div = fls(pre_div);
175 if (post_div > 4) {
176 post_div -= 4;
177 if (post_div >= 16) {
Stefano Babic6e6f4552010-04-04 22:43:38 +0200178 printf("Error: no divider for the freq: %d\n",
179 max_hz);
180 return -1;
181 }
Dirk Behmeb177b712013-05-11 07:25:54 +0200182 pre_div >>= post_div;
183 } else {
184 post_div = 0;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200185 }
186 }
187
188 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
189 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
190 MXC_CSPICTRL_SELCHAN(cs);
191 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
192 MXC_CSPICTRL_PREDIV(pre_div);
193 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
194 MXC_CSPICTRL_POSTDIV(post_div);
195
Stefano Babic6e6f4552010-04-04 22:43:38 +0200196 if (mode & SPI_CS_HIGH)
197 ss_pol = 1;
198
Markus Niebel6683e622014-02-17 17:33:17 +0100199 if (mode & SPI_CPOL) {
Stefano Babic6e6f4552010-04-04 22:43:38 +0200200 sclkpol = 1;
Markus Niebel6683e622014-02-17 17:33:17 +0100201 sclkctl = 1;
202 }
Stefano Babic6e6f4552010-04-04 22:43:38 +0200203
204 if (mode & SPI_CPHA)
205 sclkpha = 1;
206
Stefano Babic28580452011-01-19 22:46:33 +0000207 reg_config = reg_read(&regs->cfg);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200208
209 /*
210 * Configuration register setup
Stefano Babicdcd73cd2011-01-19 22:46:30 +0000211 * The MX51 supports different setup for each SS
Stefano Babic6e6f4552010-04-04 22:43:38 +0200212 */
213 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
214 (ss_pol << (cs + MXC_CSPICON_SSPOL));
215 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
216 (sclkpol << (cs + MXC_CSPICON_POL));
Markus Niebel6683e622014-02-17 17:33:17 +0100217 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_CTL))) |
218 (sclkctl << (cs + MXC_CSPICON_CTL));
Stefano Babic6e6f4552010-04-04 22:43:38 +0200219 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
220 (sclkpha << (cs + MXC_CSPICON_PHA));
221
222 debug("reg_ctrl = 0x%x\n", reg_ctrl);
Stefano Babic28580452011-01-19 22:46:33 +0000223 reg_write(&regs->ctrl, reg_ctrl);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200224 debug("reg_config = 0x%x\n", reg_config);
Stefano Babic28580452011-01-19 22:46:33 +0000225 reg_write(&regs->cfg, reg_config);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200226
227 /* save config register and control register */
228 mxcs->ctrl_reg = reg_ctrl;
229 mxcs->cfg_reg = reg_config;
230
231 /* clear interrupt reg */
Stefano Babic28580452011-01-19 22:46:33 +0000232 reg_write(&regs->intr, 0);
233 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200234
235 return 0;
236}
237#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200238
Peng Fanea0bce62017-08-09 13:09:33 +0800239int spi_xchg_single(struct mxc_spi_slave *mxcs, unsigned int bitlen,
Stefano Babic125f82a2010-08-20 12:05:03 +0200240 const u8 *dout, u8 *din, unsigned long flags)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200241{
Axel Linfb7def92013-06-14 21:13:32 +0800242 int nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic125f82a2010-08-20 12:05:03 +0200243 u32 data, cnt, i;
Stefano Babic28580452011-01-19 22:46:33 +0000244 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
Heiko Schocherb77c8882014-07-14 10:22:11 +0200245 u32 ts;
246 int status;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200247
Ye Li07955fb2019-01-04 09:26:00 +0000248 debug("%s: bitlen %d dout 0x%lx din 0x%lx\n",
249 __func__, bitlen, (ulong)dout, (ulong)din);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200250
Stefano Babic6e6f4552010-04-04 22:43:38 +0200251 mxcs->ctrl_reg = (mxcs->ctrl_reg &
252 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100253 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200254
Stefano Babic28580452011-01-19 22:46:33 +0000255 reg_write(&regs->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
Eric Nelsonfe1e7612012-01-31 07:52:03 +0000256#ifdef MXC_ECSPI
Stefano Babic28580452011-01-19 22:46:33 +0000257 reg_write(&regs->cfg, mxcs->cfg_reg);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200258#endif
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200259
Stefano Babic6e6f4552010-04-04 22:43:38 +0200260 /* Clear interrupt register */
Stefano Babic28580452011-01-19 22:46:33 +0000261 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100262
Stefano Babic125f82a2010-08-20 12:05:03 +0200263 /*
264 * The SPI controller works only with words,
265 * check if less than a word is sent.
266 * Access to the FIFO is only 32 bit
267 */
268 if (bitlen % 32) {
269 data = 0;
270 cnt = (bitlen % 32) / 8;
271 if (dout) {
272 for (i = 0; i < cnt; i++) {
273 data = (data << 8) | (*dout++ & 0xFF);
274 }
275 }
276 debug("Sending SPI 0x%x\n", data);
277
Stefano Babic28580452011-01-19 22:46:33 +0000278 reg_write(&regs->txdata, data);
Stefano Babic125f82a2010-08-20 12:05:03 +0200279 nbytes -= cnt;
280 }
281
282 data = 0;
283
284 while (nbytes > 0) {
285 data = 0;
286 if (dout) {
287 /* Buffer is not 32-bit aligned */
288 if ((unsigned long)dout & 0x03) {
289 data = 0;
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000290 for (i = 0; i < 4; i++)
Stefano Babic125f82a2010-08-20 12:05:03 +0200291 data = (data << 8) | (*dout++ & 0xFF);
Stefano Babic125f82a2010-08-20 12:05:03 +0200292 } else {
293 data = *(u32 *)dout;
294 data = cpu_to_be32(data);
Timo Herbrecher64203202013-10-16 00:05:09 +0530295 dout += 4;
Stefano Babic125f82a2010-08-20 12:05:03 +0200296 }
Stefano Babic125f82a2010-08-20 12:05:03 +0200297 }
298 debug("Sending SPI 0x%x\n", data);
Stefano Babic28580452011-01-19 22:46:33 +0000299 reg_write(&regs->txdata, data);
Stefano Babic125f82a2010-08-20 12:05:03 +0200300 nbytes -= 4;
301 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200302
Stefano Babic6e6f4552010-04-04 22:43:38 +0200303 /* FIFO is written, now starts the transfer setting the XCH bit */
Stefano Babic28580452011-01-19 22:46:33 +0000304 reg_write(&regs->ctrl, mxcs->ctrl_reg |
Stefano Babic6e6f4552010-04-04 22:43:38 +0200305 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200306
Heiko Schocherb77c8882014-07-14 10:22:11 +0200307 ts = get_timer(0);
308 status = reg_read(&regs->stat);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200309 /* Wait until the TC (Transfer completed) bit is set */
Heiko Schocherb77c8882014-07-14 10:22:11 +0200310 while ((status & MXC_CSPICTRL_TC) == 0) {
311 if (get_timer(ts) > CONFIG_SYS_SPI_MXC_WAIT) {
312 printf("spi_xchg_single: Timeout!\n");
313 return -1;
314 }
315 status = reg_read(&regs->stat);
316 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200317
Stefano Babic6e6f4552010-04-04 22:43:38 +0200318 /* Transfer completed, clear any pending request */
Stefano Babic28580452011-01-19 22:46:33 +0000319 reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200320
Axel Linfb7def92013-06-14 21:13:32 +0800321 nbytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic6e6f4552010-04-04 22:43:38 +0200322
Stefano Babic125f82a2010-08-20 12:05:03 +0200323 cnt = nbytes % 32;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100324
Stefano Babic125f82a2010-08-20 12:05:03 +0200325 if (bitlen % 32) {
Stefano Babic28580452011-01-19 22:46:33 +0000326 data = reg_read(&regs->rxdata);
Stefano Babic125f82a2010-08-20 12:05:03 +0200327 cnt = (bitlen % 32) / 8;
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000328 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
Stefano Babic125f82a2010-08-20 12:05:03 +0200329 debug("SPI Rx unaligned: 0x%x\n", data);
330 if (din) {
Anatolij Gustschin089ebe02011-01-20 07:53:06 +0000331 memcpy(din, &data, cnt);
332 din += cnt;
Stefano Babic125f82a2010-08-20 12:05:03 +0200333 }
334 nbytes -= cnt;
335 }
336
337 while (nbytes > 0) {
338 u32 tmp;
Stefano Babic28580452011-01-19 22:46:33 +0000339 tmp = reg_read(&regs->rxdata);
Stefano Babic125f82a2010-08-20 12:05:03 +0200340 data = cpu_to_be32(tmp);
341 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
Masahiro Yamadadb204642014-11-07 03:03:31 +0900342 cnt = min_t(u32, nbytes, sizeof(data));
Stefano Babic125f82a2010-08-20 12:05:03 +0200343 if (din) {
344 memcpy(din, &data, cnt);
345 din += cnt;
346 }
347 nbytes -= cnt;
348 }
349
350 return 0;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200351
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200352}
353
Peng Fanea0bce62017-08-09 13:09:33 +0800354static int mxc_spi_xfer_internal(struct mxc_spi_slave *mxcs,
355 unsigned int bitlen, const void *dout,
356 void *din, unsigned long flags)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200357{
Axel Linfb7def92013-06-14 21:13:32 +0800358 int n_bytes = DIV_ROUND_UP(bitlen, 8);
Stefano Babic125f82a2010-08-20 12:05:03 +0200359 int n_bits;
360 int ret;
361 u32 blk_size;
362 u8 *p_outbuf = (u8 *)dout;
363 u8 *p_inbuf = (u8 *)din;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200364
Peng Fanea0bce62017-08-09 13:09:33 +0800365 if (!mxcs)
366 return -EINVAL;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200367
Stefano Babic125f82a2010-08-20 12:05:03 +0200368 if (flags & SPI_XFER_BEGIN)
Peng Fanea0bce62017-08-09 13:09:33 +0800369 mxc_spi_cs_activate(mxcs);
Magnus Lilja1858a9a2010-02-09 22:05:39 +0100370
Stefano Babic125f82a2010-08-20 12:05:03 +0200371 while (n_bytes > 0) {
Stefano Babic125f82a2010-08-20 12:05:03 +0200372 if (n_bytes < MAX_SPI_BYTES)
373 blk_size = n_bytes;
374 else
375 blk_size = MAX_SPI_BYTES;
376
377 n_bits = blk_size * 8;
378
Peng Fanea0bce62017-08-09 13:09:33 +0800379 ret = spi_xchg_single(mxcs, n_bits, p_outbuf, p_inbuf, 0);
Stefano Babic125f82a2010-08-20 12:05:03 +0200380
381 if (ret)
382 return ret;
383 if (dout)
384 p_outbuf += blk_size;
385 if (din)
386 p_inbuf += blk_size;
387 n_bytes -= blk_size;
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100388 }
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200389
Stefano Babic125f82a2010-08-20 12:05:03 +0200390 if (flags & SPI_XFER_END) {
Peng Fanea0bce62017-08-09 13:09:33 +0800391 mxc_spi_cs_deactivate(mxcs);
Stefano Babic125f82a2010-08-20 12:05:03 +0200392 }
393
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200394 return 0;
395}
396
Peng Fanea0bce62017-08-09 13:09:33 +0800397static int mxc_spi_claim_bus_internal(struct mxc_spi_slave *mxcs, int cs)
398{
399 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
400 int ret;
401
402 reg_write(&regs->rxdata, 1);
403 udelay(1);
404 ret = spi_cfg_mxc(mxcs, cs);
405 if (ret) {
406 printf("mxc_spi: cannot setup SPI controller\n");
407 return ret;
408 }
409 reg_write(&regs->period, MXC_CSPIPERIOD_32KHZ);
410 reg_write(&regs->intr, 0);
411
412 return 0;
413}
414
415#ifndef CONFIG_DM_SPI
416int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
417 void *din, unsigned long flags)
418{
419 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
420
421 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
422}
423
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300424/*
425 * Some SPI devices require active chip-select over multiple
426 * transactions, we achieve this using a GPIO. Still, the SPI
427 * controller has to be configured to use one of its own chipselects.
428 * To use this feature you have to implement board_spi_cs_gpio() to assign
429 * a gpio value for each cs (-1 if cs doesn't need to use gpio).
430 * You must use some unused on this SPI controller cs between 0 and 3.
431 */
432static int setup_cs_gpio(struct mxc_spi_slave *mxcs,
433 unsigned int bus, unsigned int cs)
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100434{
435 int ret;
436
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300437 mxcs->gpio = board_spi_cs_gpio(bus, cs);
438 if (mxcs->gpio == -1)
439 return 0;
440
Peng Fanea0bce62017-08-09 13:09:33 +0800441 gpio_request(mxcs->gpio, "spi-cs");
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300442 ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
443 if (ret) {
444 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
445 return -EINVAL;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100446 }
447
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300448 return 0;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200449}
450
Peng Fanea0bce62017-08-09 13:09:33 +0800451static unsigned long spi_bases[] = {
452 MXC_SPI_BASE_ADDRESSES
453};
454
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200455struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
456 unsigned int max_hz, unsigned int mode)
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200457{
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200458 struct mxc_spi_slave *mxcs;
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100459 int ret;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200460
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100461 if (bus >= ARRAY_SIZE(spi_bases))
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200462 return NULL;
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200463
Markus Niebel8f769cf2014-10-23 16:09:39 +0200464 if (max_hz == 0) {
465 printf("Error: desired clock is 0\n");
466 return NULL;
467 }
468
Simon Glassd034a952013-03-18 19:23:40 +0000469 mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
Stefano Babic125f82a2010-08-20 12:05:03 +0200470 if (!mxcs) {
471 puts("mxc_spi: SPI Slave not allocated !\n");
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100472 return NULL;
Stefano Babic125f82a2010-08-20 12:05:03 +0200473 }
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100474
Fabio Estevam17cd2a82012-11-15 11:23:23 +0000475 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
476
Nikita Kiryanov00cd7382014-08-20 15:08:50 +0300477 ret = setup_cs_gpio(mxcs, bus, cs);
Guennadi Liakhovetski9a88d702009-02-13 09:26:40 +0100478 if (ret < 0) {
479 free(mxcs);
480 return NULL;
481 }
482
Stefano Babic6e6f4552010-04-04 22:43:38 +0200483 mxcs->base = spi_bases[bus];
Markus Niebel8f769cf2014-10-23 16:09:39 +0200484 mxcs->max_hz = max_hz;
485 mxcs->mode = mode;
Stefano Babic6e6f4552010-04-04 22:43:38 +0200486
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200487 return &mxcs->slave;
488}
489
490void spi_free_slave(struct spi_slave *slave)
491{
Guennadi Liakhovetskid3380132009-02-07 00:09:12 +0100492 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
493
494 free(mxcs);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200495}
496
497int spi_claim_bus(struct spi_slave *slave)
498{
499 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
500
Peng Fanea0bce62017-08-09 13:09:33 +0800501 return mxc_spi_claim_bus_internal(mxcs, slave->cs);
502}
503
504void spi_release_bus(struct spi_slave *slave)
505{
506 /* TODO: Shut the controller down */
507}
508#else
509
510static int mxc_spi_probe(struct udevice *bus)
511{
Peng Fanea0bce62017-08-09 13:09:33 +0800512 struct mxc_spi_slave *mxcs = dev_get_platdata(bus);
513 int node = dev_of_offset(bus);
514 const void *blob = gd->fdt_blob;
515 int ret;
Heiko Schocher053c2442019-05-26 12:15:47 +0200516 int i;
Peng Fanea0bce62017-08-09 13:09:33 +0800517
Heiko Schocher053c2442019-05-26 12:15:47 +0200518 ret = gpio_request_list_by_name(bus, "cs-gpios", mxcs->cs_gpios,
519 ARRAY_SIZE(mxcs->cs_gpios), 0);
520 if (ret < 0) {
521 pr_err("Can't get %s gpios! Error: %d", bus->name, ret);
522 return ret;
523 }
524
525 for (i = 0; i < ARRAY_SIZE(mxcs->cs_gpios); i++) {
526 if (!dm_gpio_is_valid(&mxcs->cs_gpios[i]))
527 continue;
528
529 ret = dm_gpio_set_dir_flags(&mxcs->cs_gpios[i],
530 GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
531 if (ret) {
532 dev_err(bus, "Setting cs %d error\n", i);
533 return ret;
534 }
Peng Fanea0bce62017-08-09 13:09:33 +0800535 }
536
Heiko Schocher6d49b4e2019-05-26 12:15:46 +0200537 mxcs->base = devfdt_get_addr(bus);
538 if (mxcs->base == FDT_ADDR_T_NONE)
Peng Fanea0bce62017-08-09 13:09:33 +0800539 return -ENODEV;
540
Peng Fanea0bce62017-08-09 13:09:33 +0800541 mxcs->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
542 20000000);
Guennadi Liakhovetski07327a52008-04-15 14:14:25 +0200543
544 return 0;
545}
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200546
Peng Fanea0bce62017-08-09 13:09:33 +0800547static int mxc_spi_xfer(struct udevice *dev, unsigned int bitlen,
548 const void *dout, void *din, unsigned long flags)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200549{
Peng Fanea0bce62017-08-09 13:09:33 +0800550 struct mxc_spi_slave *mxcs = dev_get_platdata(dev->parent);
551
552
553 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
554}
555
556static int mxc_spi_claim_bus(struct udevice *dev)
557{
558 struct mxc_spi_slave *mxcs = dev_get_platdata(dev->parent);
559 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
560
Heiko Schocher053c2442019-05-26 12:15:47 +0200561 mxcs->dev = dev;
562
Peng Fanea0bce62017-08-09 13:09:33 +0800563 return mxc_spi_claim_bus_internal(mxcs, slave_plat->cs);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200564}
Peng Fanea0bce62017-08-09 13:09:33 +0800565
566static int mxc_spi_release_bus(struct udevice *dev)
567{
568 return 0;
569}
570
571static int mxc_spi_set_speed(struct udevice *bus, uint speed)
572{
573 /* Nothing to do */
574 return 0;
575}
576
577static int mxc_spi_set_mode(struct udevice *bus, uint mode)
578{
579 struct mxc_spi_slave *mxcs = dev_get_platdata(bus);
580
581 mxcs->mode = mode;
582 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
583
584 return 0;
585}
586
587static const struct dm_spi_ops mxc_spi_ops = {
588 .claim_bus = mxc_spi_claim_bus,
589 .release_bus = mxc_spi_release_bus,
590 .xfer = mxc_spi_xfer,
591 .set_speed = mxc_spi_set_speed,
592 .set_mode = mxc_spi_set_mode,
593};
594
595static const struct udevice_id mxc_spi_ids[] = {
596 { .compatible = "fsl,imx51-ecspi" },
597 { }
598};
599
600U_BOOT_DRIVER(mxc_spi) = {
601 .name = "mxc_spi",
602 .id = UCLASS_SPI,
603 .of_match = mxc_spi_ids,
604 .ops = &mxc_spi_ops,
605 .platdata_auto_alloc_size = sizeof(struct mxc_spi_slave),
606 .probe = mxc_spi_probe,
607};
608#endif