blob: cd624f4d6f0be7eecfe30f8dac78d6d992213285 [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
Rasmus Villemoes15340312020-02-11 15:20:25 +00007#include <clk.h>
Jagan Teki52515d52019-04-29 01:58:53 +05308#include <dm.h>
9#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020011#include <malloc.h>
Ben Warren7efe9272008-01-16 22:37:35 -050012#include <spi.h>
13#include <asm/mpc8xxx_spi.h>
Jagan Teki52515d52019-04-29 01:58:53 +053014#include <asm-generic/gpio.h>
Rasmus Villemoesd4e78c32020-04-20 16:13:41 +020015#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060016#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060017#include <linux/delay.h>
Christophe Leroyc58bd4a2023-03-02 16:26:26 +010018#include <asm/arch/soc.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 */
Christophe Leroyc58bd4a2023-03-02 16:26:26 +010033 SPI_MODE_OP = BIT(31 - 17), /* CPU Mode, QE otherwise */
Mario Six10f300a2019-04-29 01:58:41 +053034
35 SPI_MODE_LEN_MASK = 0xf00000,
Rasmus Villemoes379e25d2020-02-11 15:20:25 +000036 SPI_MODE_LEN_SHIFT = 20,
Rasmus Villemoes15340312020-02-11 15:20:25 +000037 SPI_MODE_PM_SHIFT = 16,
Mario Six10f300a2019-04-29 01:58:41 +053038 SPI_MODE_PM_MASK = 0xf0000,
Ben Warren7efe9272008-01-16 22:37:35 -050039
Mario Six10f300a2019-04-29 01:58:41 +053040 SPI_COM_LST = BIT(31 - 9),
41};
Ben Warren7efe9272008-01-16 22:37:35 -050042
Jagan Teki52515d52019-04-29 01:58:53 +053043struct mpc8xxx_priv {
44 spi8xxx_t *spi;
45 struct gpio_desc gpios[16];
Rasmus Villemoesd31ec8b2020-02-11 15:20:24 +000046 int cs_count;
Rasmus Villemoes15340312020-02-11 15:20:25 +000047 ulong clk_rate;
Jagan Teki52515d52019-04-29 01:58:53 +053048};
49
Ben Warren7efe9272008-01-16 22:37:35 -050050#define SPI_TIMEOUT 1000
51
Simon Glassaad29ae2020-12-03 16:55:21 -070052static int mpc8xxx_spi_of_to_plat(struct udevice *dev)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020053{
Jagan Teki52515d52019-04-29 01:58:53 +053054 struct mpc8xxx_priv *priv = dev_get_priv(dev);
Rasmus Villemoes15340312020-02-11 15:20:25 +000055 struct clk clk;
Jagan Teki52515d52019-04-29 01:58:53 +053056 int ret;
57
Johan Jonker8d5d8e02023-03-13 01:32:04 +010058 priv->spi = dev_read_addr_ptr(dev);
Jagan Teki52515d52019-04-29 01:58:53 +053059
Jagan Teki52515d52019-04-29 01:58:53 +053060 ret = gpio_request_list_by_name(dev, "gpios", priv->gpios,
61 ARRAY_SIZE(priv->gpios), GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
62 if (ret < 0)
63 return -EINVAL;
64
Rasmus Villemoesd31ec8b2020-02-11 15:20:24 +000065 priv->cs_count = ret;
Jagan Teki52515d52019-04-29 01:58:53 +053066
Rasmus Villemoes15340312020-02-11 15:20:25 +000067 ret = clk_get_by_index(dev, 0, &clk);
68 if (ret) {
69 dev_err(dev, "%s: clock not defined\n", __func__);
70 return ret;
71 }
72
73 priv->clk_rate = clk_get_rate(&clk);
74 if (!priv->clk_rate) {
75 dev_err(dev, "%s: failed to get clock rate\n", __func__);
76 return -EINVAL;
77 }
78
Jagan Teki52515d52019-04-29 01:58:53 +053079 return 0;
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020080}
81
Jagan Teki52515d52019-04-29 01:58:53 +053082static int mpc8xxx_spi_probe(struct udevice *dev)
Ben Warren7efe9272008-01-16 22:37:35 -050083{
Jagan Teki52515d52019-04-29 01:58:53 +053084 struct mpc8xxx_priv *priv = dev_get_priv(dev);
Rasmus Villemoes379e25d2020-02-11 15:20:25 +000085 spi8xxx_t *spi = priv->spi;
Ben Warren7efe9272008-01-16 22:37:35 -050086
Kim Phillipsb8e25202008-01-17 12:48:00 -060087 /*
Ben Warren7efe9272008-01-16 22:37:35 -050088 * SPI pins on the MPC83xx are not muxed, so all we do is initialize
89 * some registers
Kim Phillipsb8e25202008-01-17 12:48:00 -060090 */
Rasmus Villemoes379e25d2020-02-11 15:20:25 +000091 out_be32(&priv->spi->mode, SPI_MODE_REV | SPI_MODE_MS);
92
Christophe Leroyc58bd4a2023-03-02 16:26:26 +010093 if (dev_get_driver_data(dev) == SOC_MPC832X)
94 setbits_be32(&priv->spi->mode, SPI_MODE_OP);
95
Rasmus Villemoes379e25d2020-02-11 15:20:25 +000096 /* set len to 8 bits */
97 setbits_be32(&spi->mode, (8 - 1) << SPI_MODE_LEN_SHIFT);
Jagan Teki52515d52019-04-29 01:58:53 +053098
Rasmus Villemoes379e25d2020-02-11 15:20:25 +000099 setbits_be32(&spi->mode, SPI_MODE_EN);
Jagan Teki52515d52019-04-29 01:58:53 +0530100
Mario Six4d3082b2019-04-29 01:58:37 +0530101 /* Clear all SPI events */
Jagan Teki52515d52019-04-29 01:58:53 +0530102 setbits_be32(&priv->spi->event, 0xffffffff);
Mario Six4d3082b2019-04-29 01:58:37 +0530103 /* Mask all SPI interrupts */
Jagan Teki52515d52019-04-29 01:58:53 +0530104 clrbits_be32(&priv->spi->mask, 0xffffffff);
Mario Six4d3082b2019-04-29 01:58:37 +0530105 /* LST bit doesn't do anything, so disregard */
Jagan Teki52515d52019-04-29 01:58:53 +0530106 out_be32(&priv->spi->com, 0);
107
108 return 0;
Ben Warren7efe9272008-01-16 22:37:35 -0500109}
110
Jagan Teki52515d52019-04-29 01:58:53 +0530111static void mpc8xxx_spi_cs_activate(struct udevice *dev)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200112{
Jagan Teki52515d52019-04-29 01:58:53 +0530113 struct mpc8xxx_priv *priv = dev_get_priv(dev->parent);
Simon Glassb75b15b2020-12-03 16:55:23 -0700114 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Jagan Teki52515d52019-04-29 01:58:53 +0530115
Simon Glass71fa5b42020-12-03 16:55:18 -0700116 dm_gpio_set_value(&priv->gpios[plat->cs], 1);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200117}
118
Jagan Teki52515d52019-04-29 01:58:53 +0530119static void mpc8xxx_spi_cs_deactivate(struct udevice *dev)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200120{
Jagan Teki52515d52019-04-29 01:58:53 +0530121 struct mpc8xxx_priv *priv = dev_get_priv(dev->parent);
Simon Glassb75b15b2020-12-03 16:55:23 -0700122 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Jagan Teki52515d52019-04-29 01:58:53 +0530123
Simon Glass71fa5b42020-12-03 16:55:18 -0700124 dm_gpio_set_value(&priv->gpios[plat->cs], 0);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200125}
126
Jagan Teki52515d52019-04-29 01:58:53 +0530127static int mpc8xxx_spi_xfer(struct udevice *dev, uint bitlen,
128 const void *dout, void *din, ulong flags)
Ben Warren7efe9272008-01-16 22:37:35 -0500129{
Jagan Teki52515d52019-04-29 01:58:53 +0530130 struct udevice *bus = dev->parent;
131 struct mpc8xxx_priv *priv = dev_get_priv(bus);
132 spi8xxx_t *spi = priv->spi;
Simon Glassb75b15b2020-12-03 16:55:23 -0700133 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000134 u32 tmpdin = 0, tmpdout = 0, n;
135 const u8 *cout = dout;
136 u8 *cin = din;
Christophe Leroyc58bd4a2023-03-02 16:26:26 +0100137 ulong type = dev_get_driver_data(bus);
Ben Warren7efe9272008-01-16 22:37:35 -0500138
Jagan Teki52515d52019-04-29 01:58:53 +0530139 debug("%s: slave %s:%u dout %08X din %08X bitlen %u\n", __func__,
Simon Glass71fa5b42020-12-03 16:55:18 -0700140 bus->name, plat->cs, (uint)dout, (uint)din, bitlen);
141 if (plat->cs >= priv->cs_count) {
Rasmus Villemoesd31ec8b2020-02-11 15:20:24 +0000142 dev_err(dev, "chip select index %d too large (cs_count=%d)\n",
Simon Glass71fa5b42020-12-03 16:55:18 -0700143 plat->cs, priv->cs_count);
Rasmus Villemoesd31ec8b2020-02-11 15:20:24 +0000144 return -EINVAL;
145 }
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000146 if (bitlen % 8) {
147 printf("*** spi_xfer: bitlen must be multiple of 8\n");
148 return -ENOTSUPP;
149 }
Ben Warren7efe9272008-01-16 22:37:35 -0500150
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200151 if (flags & SPI_XFER_BEGIN)
Jagan Teki52515d52019-04-29 01:58:53 +0530152 mpc8xxx_spi_cs_activate(dev);
Ben Warren7efe9272008-01-16 22:37:35 -0500153
Mario Six4d3082b2019-04-29 01:58:37 +0530154 /* Clear all SPI events */
Mario Sixdee99492019-04-29 01:58:42 +0530155 setbits_be32(&spi->event, 0xffffffff);
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000156 n = bitlen / 8;
Ben Warren7efe9272008-01-16 22:37:35 -0500157
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000158 /* Handle data in 8-bit chunks */
159 while (n--) {
Mario Six9083b132019-04-29 01:58:52 +0530160 ulong start;
Ben Warren7efe9272008-01-16 22:37:35 -0500161
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000162 if (cout)
163 tmpdout = *cout++;
Ben Warren7efe9272008-01-16 22:37:35 -0500164
Christophe Leroyc58bd4a2023-03-02 16:26:26 +0100165 if (type == SOC_MPC832X)
166 tmpdout <<= 24;
167
Mario Six4d3082b2019-04-29 01:58:37 +0530168 /* Write the data out */
Mario Sixdee99492019-04-29 01:58:42 +0530169 out_be32(&spi->tx, tmpdout);
Mario Six4d3082b2019-04-29 01:58:37 +0530170
Mario Sixf9d5ca22019-04-29 01:58:40 +0530171 debug("*** %s: ... %08x written\n", __func__, tmpdout);
Ben Warren7efe9272008-01-16 22:37:35 -0500172
Kim Phillipsb8e25202008-01-17 12:48:00 -0600173 /*
Ben Warren7efe9272008-01-16 22:37:35 -0500174 * Wait for SPI transmit to get out
175 * or time out (1 second = 1000 ms)
176 * The NE event must be read and cleared first
Kim Phillipsb8e25202008-01-17 12:48:00 -0600177 */
Mario Six9083b132019-04-29 01:58:52 +0530178 start = get_timer(0);
179 do {
Mario Six8d684ec2019-04-29 01:58:46 +0530180 u32 event = in_be32(&spi->event);
Mario Six4b671e12019-04-29 01:58:44 +0530181 bool have_ne = event & SPI_EV_NE;
182 bool have_nf = event & SPI_EV_NF;
183
Mario Six2afedfe2019-04-29 01:58:45 +0530184 if (!have_ne)
185 continue;
Ben Warren7efe9272008-01-16 22:37:35 -0500186
Mario Six2afedfe2019-04-29 01:58:45 +0530187 tmpdin = in_be32(&spi->rx);
188 setbits_be32(&spi->event, SPI_EV_NE);
189
Christophe Leroyc58bd4a2023-03-02 16:26:26 +0100190 if (type == SOC_MPC832X)
191 tmpdin >>= 16;
192
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000193 if (cin)
194 *cin++ = tmpdin;
Mario Six2afedfe2019-04-29 01:58:45 +0530195
Kim Phillipsb8e25202008-01-17 12:48:00 -0600196 /*
197 * Only bail when we've had both NE and NF events.
Ben Warren7efe9272008-01-16 22:37:35 -0500198 * This will cause timeouts on RO devices, so maybe
199 * in the future put an arbitrary delay after writing
Kim Phillipsb8e25202008-01-17 12:48:00 -0600200 * the device. Arbitrary delays suck, though...
201 */
Mario Six2afedfe2019-04-29 01:58:45 +0530202 if (have_nf)
Ben Warren7efe9272008-01-16 22:37:35 -0500203 break;
Mario Six9083b132019-04-29 01:58:52 +0530204
205 mdelay(1);
206 } while (get_timer(start) < SPI_TIMEOUT);
Mario Six2afedfe2019-04-29 01:58:45 +0530207
Jagan Teki52515d52019-04-29 01:58:53 +0530208 if (get_timer(start) >= SPI_TIMEOUT) {
Mario Sixf9d5ca22019-04-29 01:58:40 +0530209 debug("*** %s: Time out during SPI transfer\n",
210 __func__);
Jagan Teki52515d52019-04-29 01:58:53 +0530211 return -ETIMEDOUT;
212 }
Ben Warren7efe9272008-01-16 22:37:35 -0500213
Mario Sixf9d5ca22019-04-29 01:58:40 +0530214 debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin);
Ben Warren7efe9272008-01-16 22:37:35 -0500215 }
216
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200217 if (flags & SPI_XFER_END)
Jagan Teki52515d52019-04-29 01:58:53 +0530218 mpc8xxx_spi_cs_deactivate(dev);
219
220 return 0;
221}
222
223static int mpc8xxx_spi_set_speed(struct udevice *dev, uint speed)
224{
Rasmus Villemoes15340312020-02-11 15:20:25 +0000225 struct mpc8xxx_priv *priv = dev_get_priv(dev);
226 spi8xxx_t *spi = priv->spi;
227 u32 bits, mask, div16, pm;
228 u32 mode;
229 ulong clk;
230
231 clk = priv->clk_rate;
232 if (clk / 64 > speed) {
233 div16 = SPI_MODE_DIV16;
234 clk /= 16;
235 } else {
236 div16 = 0;
237 }
238 pm = (clk - 1)/(4*speed) + 1;
239 if (pm > 16) {
240 dev_err(dev, "requested speed %u too small\n", speed);
241 return -EINVAL;
242 }
243 pm--;
244
245 bits = div16 | (pm << SPI_MODE_PM_SHIFT);
246 mask = SPI_MODE_DIV16 | SPI_MODE_PM_MASK;
247 mode = in_be32(&spi->mode);
248 if ((mode & mask) != bits) {
249 /* Must clear mode[EN] while changing speed. */
250 mode &= ~(mask | SPI_MODE_EN);
251 out_be32(&spi->mode, mode);
252 mode |= bits;
253 out_be32(&spi->mode, mode);
254 mode |= SPI_MODE_EN;
255 out_be32(&spi->mode, mode);
256 }
257
258 debug("requested speed %u, set speed to %lu/(%s4*%u) == %lu\n",
259 speed, priv->clk_rate, div16 ? "16*" : "", pm + 1,
260 clk/(4*(pm + 1)));
261
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000262 return 0;
Jagan Teki52515d52019-04-29 01:58:53 +0530263}
Kim Phillipsb8e25202008-01-17 12:48:00 -0600264
Jagan Teki52515d52019-04-29 01:58:53 +0530265static int mpc8xxx_spi_set_mode(struct udevice *dev, uint mode)
266{
267 /* TODO(mario.six@gdsys.cc): Using SPI_CPHA (for clock phase) and
268 * SPI_CPOL (for clock polarity) should work
269 */
Ben Warren7efe9272008-01-16 22:37:35 -0500270 return 0;
271}
Jagan Teki52515d52019-04-29 01:58:53 +0530272
273static const struct dm_spi_ops mpc8xxx_spi_ops = {
274 .xfer = mpc8xxx_spi_xfer,
275 .set_speed = mpc8xxx_spi_set_speed,
276 .set_mode = mpc8xxx_spi_set_mode,
277 /*
278 * cs_info is not needed, since we require all chip selects to be
279 * in the device tree explicitly
280 */
281};
282
283static const struct udevice_id mpc8xxx_spi_ids[] = {
284 { .compatible = "fsl,spi" },
Christophe Leroyc58bd4a2023-03-02 16:26:26 +0100285 { .compatible = "fsl,mpc832x-spi", .data = SOC_MPC832X },
Jagan Teki52515d52019-04-29 01:58:53 +0530286 { }
287};
288
289U_BOOT_DRIVER(mpc8xxx_spi) = {
290 .name = "mpc8xxx_spi",
291 .id = UCLASS_SPI,
292 .of_match = mpc8xxx_spi_ids,
293 .ops = &mpc8xxx_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700294 .of_to_plat = mpc8xxx_spi_of_to_plat,
Jagan Teki52515d52019-04-29 01:58:53 +0530295 .probe = mpc8xxx_spi_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700296 .priv_auto = sizeof(struct mpc8xxx_priv),
Jagan Teki52515d52019-04-29 01:58:53 +0530297};