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