blob: 6869d60d97bbc20e0a66eff811a41a813e9076ec [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ben Warren7efe9272008-01-16 22:37:35 -05002/*
3 * Copyright (c) 2006 Ben Warren, Qstreams Networks Inc.
Stefan Roese88fbf932010-04-15 16:07:28 +02004 * With help from the common/soft_spi and arch/powerpc/cpu/mpc8260 drivers
Ben Warren7efe9272008-01-16 22:37:35 -05005 */
6
7#include <common.h>
Rasmus Villemoes15340312020-02-11 15:20:25 +00008#include <clk.h>
Jagan Teki52515d52019-04-29 01:58:53 +05309#include <dm.h>
10#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020012#include <malloc.h>
Ben Warren7efe9272008-01-16 22:37:35 -050013#include <spi.h>
14#include <asm/mpc8xxx_spi.h>
Jagan Teki52515d52019-04-29 01:58:53 +053015#include <asm-generic/gpio.h>
Rasmus Villemoesd4e78c32020-04-20 16:13:41 +020016#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Ben Warren7efe9272008-01-16 22:37:35 -050019
Mario Six10f300a2019-04-29 01:58:41 +053020enum {
21 SPI_EV_NE = BIT(31 - 22), /* Receiver Not Empty */
22 SPI_EV_NF = BIT(31 - 23), /* Transmitter Not Full */
23};
24
25enum {
26 SPI_MODE_LOOP = BIT(31 - 1), /* Loopback mode */
27 SPI_MODE_CI = BIT(31 - 2), /* Clock invert */
28 SPI_MODE_CP = BIT(31 - 3), /* Clock phase */
29 SPI_MODE_DIV16 = BIT(31 - 4), /* Divide clock source by 16 */
30 SPI_MODE_REV = BIT(31 - 5), /* Reverse mode - MSB first */
31 SPI_MODE_MS = BIT(31 - 6), /* Always master */
32 SPI_MODE_EN = BIT(31 - 7), /* Enable interface */
33
34 SPI_MODE_LEN_MASK = 0xf00000,
Rasmus Villemoes379e25d2020-02-11 15:20:25 +000035 SPI_MODE_LEN_SHIFT = 20,
Rasmus Villemoes15340312020-02-11 15:20:25 +000036 SPI_MODE_PM_SHIFT = 16,
Mario Six10f300a2019-04-29 01:58:41 +053037 SPI_MODE_PM_MASK = 0xf0000,
Ben Warren7efe9272008-01-16 22:37:35 -050038
Mario Six10f300a2019-04-29 01:58:41 +053039 SPI_COM_LST = BIT(31 - 9),
40};
Ben Warren7efe9272008-01-16 22:37:35 -050041
Jagan Teki52515d52019-04-29 01:58:53 +053042struct mpc8xxx_priv {
43 spi8xxx_t *spi;
44 struct gpio_desc gpios[16];
Rasmus Villemoesd31ec8b2020-02-11 15:20:24 +000045 int cs_count;
Rasmus Villemoes15340312020-02-11 15:20:25 +000046 ulong clk_rate;
Jagan Teki52515d52019-04-29 01:58:53 +053047};
48
Ben Warren7efe9272008-01-16 22:37:35 -050049#define SPI_TIMEOUT 1000
50
Simon Glassaad29ae2020-12-03 16:55:21 -070051static int mpc8xxx_spi_of_to_plat(struct udevice *dev)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020052{
Jagan Teki52515d52019-04-29 01:58:53 +053053 struct mpc8xxx_priv *priv = dev_get_priv(dev);
Rasmus Villemoes15340312020-02-11 15:20:25 +000054 struct clk clk;
Jagan Teki52515d52019-04-29 01:58:53 +053055 int ret;
56
57 priv->spi = (spi8xxx_t *)dev_read_addr(dev);
58
Jagan Teki52515d52019-04-29 01:58:53 +053059 ret = gpio_request_list_by_name(dev, "gpios", priv->gpios,
60 ARRAY_SIZE(priv->gpios), GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
61 if (ret < 0)
62 return -EINVAL;
63
Rasmus Villemoesd31ec8b2020-02-11 15:20:24 +000064 priv->cs_count = ret;
Jagan Teki52515d52019-04-29 01:58:53 +053065
Rasmus Villemoes15340312020-02-11 15:20:25 +000066 ret = clk_get_by_index(dev, 0, &clk);
67 if (ret) {
68 dev_err(dev, "%s: clock not defined\n", __func__);
69 return ret;
70 }
71
72 priv->clk_rate = clk_get_rate(&clk);
73 if (!priv->clk_rate) {
74 dev_err(dev, "%s: failed to get clock rate\n", __func__);
75 return -EINVAL;
76 }
77
Jagan Teki52515d52019-04-29 01:58:53 +053078 return 0;
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020079}
80
Jagan Teki52515d52019-04-29 01:58:53 +053081static int mpc8xxx_spi_probe(struct udevice *dev)
Ben Warren7efe9272008-01-16 22:37:35 -050082{
Jagan Teki52515d52019-04-29 01:58:53 +053083 struct mpc8xxx_priv *priv = dev_get_priv(dev);
Rasmus Villemoes379e25d2020-02-11 15:20:25 +000084 spi8xxx_t *spi = priv->spi;
Ben Warren7efe9272008-01-16 22:37:35 -050085
Kim Phillipsb8e25202008-01-17 12:48:00 -060086 /*
Ben Warren7efe9272008-01-16 22:37:35 -050087 * SPI pins on the MPC83xx are not muxed, so all we do is initialize
88 * some registers
Kim Phillipsb8e25202008-01-17 12:48:00 -060089 */
Rasmus Villemoes379e25d2020-02-11 15:20:25 +000090 out_be32(&priv->spi->mode, SPI_MODE_REV | SPI_MODE_MS);
91
92 /* set len to 8 bits */
93 setbits_be32(&spi->mode, (8 - 1) << SPI_MODE_LEN_SHIFT);
Jagan Teki52515d52019-04-29 01:58:53 +053094
Rasmus Villemoes379e25d2020-02-11 15:20:25 +000095 setbits_be32(&spi->mode, SPI_MODE_EN);
Jagan Teki52515d52019-04-29 01:58:53 +053096
Mario Six4d3082b2019-04-29 01:58:37 +053097 /* Clear all SPI events */
Jagan Teki52515d52019-04-29 01:58:53 +053098 setbits_be32(&priv->spi->event, 0xffffffff);
Mario Six4d3082b2019-04-29 01:58:37 +053099 /* Mask all SPI interrupts */
Jagan Teki52515d52019-04-29 01:58:53 +0530100 clrbits_be32(&priv->spi->mask, 0xffffffff);
Mario Six4d3082b2019-04-29 01:58:37 +0530101 /* LST bit doesn't do anything, so disregard */
Jagan Teki52515d52019-04-29 01:58:53 +0530102 out_be32(&priv->spi->com, 0);
103
104 return 0;
Ben Warren7efe9272008-01-16 22:37:35 -0500105}
106
Jagan Teki52515d52019-04-29 01:58:53 +0530107static void mpc8xxx_spi_cs_activate(struct udevice *dev)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200108{
Jagan Teki52515d52019-04-29 01:58:53 +0530109 struct mpc8xxx_priv *priv = dev_get_priv(dev->parent);
Simon Glassb75b15b2020-12-03 16:55:23 -0700110 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Jagan Teki52515d52019-04-29 01:58:53 +0530111
Simon Glass71fa5b42020-12-03 16:55:18 -0700112 dm_gpio_set_value(&priv->gpios[plat->cs], 1);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200113}
114
Jagan Teki52515d52019-04-29 01:58:53 +0530115static void mpc8xxx_spi_cs_deactivate(struct udevice *dev)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200116{
Jagan Teki52515d52019-04-29 01:58:53 +0530117 struct mpc8xxx_priv *priv = dev_get_priv(dev->parent);
Simon Glassb75b15b2020-12-03 16:55:23 -0700118 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Jagan Teki52515d52019-04-29 01:58:53 +0530119
Simon Glass71fa5b42020-12-03 16:55:18 -0700120 dm_gpio_set_value(&priv->gpios[plat->cs], 0);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200121}
122
Jagan Teki52515d52019-04-29 01:58:53 +0530123static int mpc8xxx_spi_xfer(struct udevice *dev, uint bitlen,
124 const void *dout, void *din, ulong flags)
Ben Warren7efe9272008-01-16 22:37:35 -0500125{
Jagan Teki52515d52019-04-29 01:58:53 +0530126 struct udevice *bus = dev->parent;
127 struct mpc8xxx_priv *priv = dev_get_priv(bus);
128 spi8xxx_t *spi = priv->spi;
Simon Glassb75b15b2020-12-03 16:55:23 -0700129 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000130 u32 tmpdin = 0, tmpdout = 0, n;
131 const u8 *cout = dout;
132 u8 *cin = din;
Ben Warren7efe9272008-01-16 22:37:35 -0500133
Jagan Teki52515d52019-04-29 01:58:53 +0530134 debug("%s: slave %s:%u dout %08X din %08X bitlen %u\n", __func__,
Simon Glass71fa5b42020-12-03 16:55:18 -0700135 bus->name, plat->cs, (uint)dout, (uint)din, bitlen);
136 if (plat->cs >= priv->cs_count) {
Rasmus Villemoesd31ec8b2020-02-11 15:20:24 +0000137 dev_err(dev, "chip select index %d too large (cs_count=%d)\n",
Simon Glass71fa5b42020-12-03 16:55:18 -0700138 plat->cs, priv->cs_count);
Rasmus Villemoesd31ec8b2020-02-11 15:20:24 +0000139 return -EINVAL;
140 }
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000141 if (bitlen % 8) {
142 printf("*** spi_xfer: bitlen must be multiple of 8\n");
143 return -ENOTSUPP;
144 }
Ben Warren7efe9272008-01-16 22:37:35 -0500145
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200146 if (flags & SPI_XFER_BEGIN)
Jagan Teki52515d52019-04-29 01:58:53 +0530147 mpc8xxx_spi_cs_activate(dev);
Ben Warren7efe9272008-01-16 22:37:35 -0500148
Mario Six4d3082b2019-04-29 01:58:37 +0530149 /* Clear all SPI events */
Mario Sixdee99492019-04-29 01:58:42 +0530150 setbits_be32(&spi->event, 0xffffffff);
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000151 n = bitlen / 8;
Ben Warren7efe9272008-01-16 22:37:35 -0500152
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000153 /* Handle data in 8-bit chunks */
154 while (n--) {
Mario Six9083b132019-04-29 01:58:52 +0530155 ulong start;
Ben Warren7efe9272008-01-16 22:37:35 -0500156
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000157 if (cout)
158 tmpdout = *cout++;
Ben Warren7efe9272008-01-16 22:37:35 -0500159
Mario Six4d3082b2019-04-29 01:58:37 +0530160 /* Write the data out */
Mario Sixdee99492019-04-29 01:58:42 +0530161 out_be32(&spi->tx, tmpdout);
Mario Six4d3082b2019-04-29 01:58:37 +0530162
Mario Sixf9d5ca22019-04-29 01:58:40 +0530163 debug("*** %s: ... %08x written\n", __func__, tmpdout);
Ben Warren7efe9272008-01-16 22:37:35 -0500164
Kim Phillipsb8e25202008-01-17 12:48:00 -0600165 /*
Ben Warren7efe9272008-01-16 22:37:35 -0500166 * Wait for SPI transmit to get out
167 * or time out (1 second = 1000 ms)
168 * The NE event must be read and cleared first
Kim Phillipsb8e25202008-01-17 12:48:00 -0600169 */
Mario Six9083b132019-04-29 01:58:52 +0530170 start = get_timer(0);
171 do {
Mario Six8d684ec2019-04-29 01:58:46 +0530172 u32 event = in_be32(&spi->event);
Mario Six4b671e12019-04-29 01:58:44 +0530173 bool have_ne = event & SPI_EV_NE;
174 bool have_nf = event & SPI_EV_NF;
175
Mario Six2afedfe2019-04-29 01:58:45 +0530176 if (!have_ne)
177 continue;
Ben Warren7efe9272008-01-16 22:37:35 -0500178
Mario Six2afedfe2019-04-29 01:58:45 +0530179 tmpdin = in_be32(&spi->rx);
180 setbits_be32(&spi->event, SPI_EV_NE);
181
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000182 if (cin)
183 *cin++ = tmpdin;
Mario Six2afedfe2019-04-29 01:58:45 +0530184
Kim Phillipsb8e25202008-01-17 12:48:00 -0600185 /*
186 * Only bail when we've had both NE and NF events.
Ben Warren7efe9272008-01-16 22:37:35 -0500187 * This will cause timeouts on RO devices, so maybe
188 * in the future put an arbitrary delay after writing
Kim Phillipsb8e25202008-01-17 12:48:00 -0600189 * the device. Arbitrary delays suck, though...
190 */
Mario Six2afedfe2019-04-29 01:58:45 +0530191 if (have_nf)
Ben Warren7efe9272008-01-16 22:37:35 -0500192 break;
Mario Six9083b132019-04-29 01:58:52 +0530193
194 mdelay(1);
195 } while (get_timer(start) < SPI_TIMEOUT);
Mario Six2afedfe2019-04-29 01:58:45 +0530196
Jagan Teki52515d52019-04-29 01:58:53 +0530197 if (get_timer(start) >= SPI_TIMEOUT) {
Mario Sixf9d5ca22019-04-29 01:58:40 +0530198 debug("*** %s: Time out during SPI transfer\n",
199 __func__);
Jagan Teki52515d52019-04-29 01:58:53 +0530200 return -ETIMEDOUT;
201 }
Ben Warren7efe9272008-01-16 22:37:35 -0500202
Mario Sixf9d5ca22019-04-29 01:58:40 +0530203 debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin);
Ben Warren7efe9272008-01-16 22:37:35 -0500204 }
205
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200206 if (flags & SPI_XFER_END)
Jagan Teki52515d52019-04-29 01:58:53 +0530207 mpc8xxx_spi_cs_deactivate(dev);
208
209 return 0;
210}
211
212static int mpc8xxx_spi_set_speed(struct udevice *dev, uint speed)
213{
Rasmus Villemoes15340312020-02-11 15:20:25 +0000214 struct mpc8xxx_priv *priv = dev_get_priv(dev);
215 spi8xxx_t *spi = priv->spi;
216 u32 bits, mask, div16, pm;
217 u32 mode;
218 ulong clk;
219
220 clk = priv->clk_rate;
221 if (clk / 64 > speed) {
222 div16 = SPI_MODE_DIV16;
223 clk /= 16;
224 } else {
225 div16 = 0;
226 }
227 pm = (clk - 1)/(4*speed) + 1;
228 if (pm > 16) {
229 dev_err(dev, "requested speed %u too small\n", speed);
230 return -EINVAL;
231 }
232 pm--;
233
234 bits = div16 | (pm << SPI_MODE_PM_SHIFT);
235 mask = SPI_MODE_DIV16 | SPI_MODE_PM_MASK;
236 mode = in_be32(&spi->mode);
237 if ((mode & mask) != bits) {
238 /* Must clear mode[EN] while changing speed. */
239 mode &= ~(mask | SPI_MODE_EN);
240 out_be32(&spi->mode, mode);
241 mode |= bits;
242 out_be32(&spi->mode, mode);
243 mode |= SPI_MODE_EN;
244 out_be32(&spi->mode, mode);
245 }
246
247 debug("requested speed %u, set speed to %lu/(%s4*%u) == %lu\n",
248 speed, priv->clk_rate, div16 ? "16*" : "", pm + 1,
249 clk/(4*(pm + 1)));
250
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000251 return 0;
Jagan Teki52515d52019-04-29 01:58:53 +0530252}
Kim Phillipsb8e25202008-01-17 12:48:00 -0600253
Jagan Teki52515d52019-04-29 01:58:53 +0530254static int mpc8xxx_spi_set_mode(struct udevice *dev, uint mode)
255{
256 /* TODO(mario.six@gdsys.cc): Using SPI_CPHA (for clock phase) and
257 * SPI_CPOL (for clock polarity) should work
258 */
Ben Warren7efe9272008-01-16 22:37:35 -0500259 return 0;
260}
Jagan Teki52515d52019-04-29 01:58:53 +0530261
262static const struct dm_spi_ops mpc8xxx_spi_ops = {
263 .xfer = mpc8xxx_spi_xfer,
264 .set_speed = mpc8xxx_spi_set_speed,
265 .set_mode = mpc8xxx_spi_set_mode,
266 /*
267 * cs_info is not needed, since we require all chip selects to be
268 * in the device tree explicitly
269 */
270};
271
272static const struct udevice_id mpc8xxx_spi_ids[] = {
273 { .compatible = "fsl,spi" },
274 { }
275};
276
277U_BOOT_DRIVER(mpc8xxx_spi) = {
278 .name = "mpc8xxx_spi",
279 .id = UCLASS_SPI,
280 .of_match = mpc8xxx_spi_ids,
281 .ops = &mpc8xxx_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700282 .of_to_plat = mpc8xxx_spi_of_to_plat,
Jagan Teki52515d52019-04-29 01:58:53 +0530283 .probe = mpc8xxx_spi_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700284 .priv_auto = sizeof(struct mpc8xxx_priv),
Jagan Teki52515d52019-04-29 01:58:53 +0530285};