blob: 7d15390c56b327227ccc3d91bab27a98c0578961 [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>
Christophe Leroyc58bd4a2023-03-02 16:26:26 +010019#include <asm/arch/soc.h>
Ben Warren7efe9272008-01-16 22:37:35 -050020
Mario Six10f300a2019-04-29 01:58:41 +053021enum {
22 SPI_EV_NE = BIT(31 - 22), /* Receiver Not Empty */
23 SPI_EV_NF = BIT(31 - 23), /* Transmitter Not Full */
24};
25
26enum {
27 SPI_MODE_LOOP = BIT(31 - 1), /* Loopback mode */
28 SPI_MODE_CI = BIT(31 - 2), /* Clock invert */
29 SPI_MODE_CP = BIT(31 - 3), /* Clock phase */
30 SPI_MODE_DIV16 = BIT(31 - 4), /* Divide clock source by 16 */
31 SPI_MODE_REV = BIT(31 - 5), /* Reverse mode - MSB first */
32 SPI_MODE_MS = BIT(31 - 6), /* Always master */
33 SPI_MODE_EN = BIT(31 - 7), /* Enable interface */
Christophe Leroyc58bd4a2023-03-02 16:26:26 +010034 SPI_MODE_OP = BIT(31 - 17), /* CPU Mode, QE otherwise */
Mario Six10f300a2019-04-29 01:58:41 +053035
36 SPI_MODE_LEN_MASK = 0xf00000,
Rasmus Villemoes379e25d2020-02-11 15:20:25 +000037 SPI_MODE_LEN_SHIFT = 20,
Rasmus Villemoes15340312020-02-11 15:20:25 +000038 SPI_MODE_PM_SHIFT = 16,
Mario Six10f300a2019-04-29 01:58:41 +053039 SPI_MODE_PM_MASK = 0xf0000,
Ben Warren7efe9272008-01-16 22:37:35 -050040
Mario Six10f300a2019-04-29 01:58:41 +053041 SPI_COM_LST = BIT(31 - 9),
42};
Ben Warren7efe9272008-01-16 22:37:35 -050043
Jagan Teki52515d52019-04-29 01:58:53 +053044struct mpc8xxx_priv {
45 spi8xxx_t *spi;
46 struct gpio_desc gpios[16];
Rasmus Villemoesd31ec8b2020-02-11 15:20:24 +000047 int cs_count;
Rasmus Villemoes15340312020-02-11 15:20:25 +000048 ulong clk_rate;
Jagan Teki52515d52019-04-29 01:58:53 +053049};
50
Ben Warren7efe9272008-01-16 22:37:35 -050051#define SPI_TIMEOUT 1000
52
Simon Glassaad29ae2020-12-03 16:55:21 -070053static int mpc8xxx_spi_of_to_plat(struct udevice *dev)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020054{
Jagan Teki52515d52019-04-29 01:58:53 +053055 struct mpc8xxx_priv *priv = dev_get_priv(dev);
Rasmus Villemoes15340312020-02-11 15:20:25 +000056 struct clk clk;
Jagan Teki52515d52019-04-29 01:58:53 +053057 int ret;
58
Johan Jonker8d5d8e02023-03-13 01:32:04 +010059 priv->spi = dev_read_addr_ptr(dev);
Jagan Teki52515d52019-04-29 01:58:53 +053060
Jagan Teki52515d52019-04-29 01:58:53 +053061 ret = gpio_request_list_by_name(dev, "gpios", priv->gpios,
62 ARRAY_SIZE(priv->gpios), GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
63 if (ret < 0)
64 return -EINVAL;
65
Rasmus Villemoesd31ec8b2020-02-11 15:20:24 +000066 priv->cs_count = ret;
Jagan Teki52515d52019-04-29 01:58:53 +053067
Rasmus Villemoes15340312020-02-11 15:20:25 +000068 ret = clk_get_by_index(dev, 0, &clk);
69 if (ret) {
70 dev_err(dev, "%s: clock not defined\n", __func__);
71 return ret;
72 }
73
74 priv->clk_rate = clk_get_rate(&clk);
75 if (!priv->clk_rate) {
76 dev_err(dev, "%s: failed to get clock rate\n", __func__);
77 return -EINVAL;
78 }
79
Jagan Teki52515d52019-04-29 01:58:53 +053080 return 0;
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020081}
82
Jagan Teki52515d52019-04-29 01:58:53 +053083static int mpc8xxx_spi_probe(struct udevice *dev)
Ben Warren7efe9272008-01-16 22:37:35 -050084{
Jagan Teki52515d52019-04-29 01:58:53 +053085 struct mpc8xxx_priv *priv = dev_get_priv(dev);
Rasmus Villemoes379e25d2020-02-11 15:20:25 +000086 spi8xxx_t *spi = priv->spi;
Ben Warren7efe9272008-01-16 22:37:35 -050087
Kim Phillipsb8e25202008-01-17 12:48:00 -060088 /*
Ben Warren7efe9272008-01-16 22:37:35 -050089 * SPI pins on the MPC83xx are not muxed, so all we do is initialize
90 * some registers
Kim Phillipsb8e25202008-01-17 12:48:00 -060091 */
Rasmus Villemoes379e25d2020-02-11 15:20:25 +000092 out_be32(&priv->spi->mode, SPI_MODE_REV | SPI_MODE_MS);
93
Christophe Leroyc58bd4a2023-03-02 16:26:26 +010094 if (dev_get_driver_data(dev) == SOC_MPC832X)
95 setbits_be32(&priv->spi->mode, SPI_MODE_OP);
96
Rasmus Villemoes379e25d2020-02-11 15:20:25 +000097 /* set len to 8 bits */
98 setbits_be32(&spi->mode, (8 - 1) << SPI_MODE_LEN_SHIFT);
Jagan Teki52515d52019-04-29 01:58:53 +053099
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000100 setbits_be32(&spi->mode, SPI_MODE_EN);
Jagan Teki52515d52019-04-29 01:58:53 +0530101
Mario Six4d3082b2019-04-29 01:58:37 +0530102 /* Clear all SPI events */
Jagan Teki52515d52019-04-29 01:58:53 +0530103 setbits_be32(&priv->spi->event, 0xffffffff);
Mario Six4d3082b2019-04-29 01:58:37 +0530104 /* Mask all SPI interrupts */
Jagan Teki52515d52019-04-29 01:58:53 +0530105 clrbits_be32(&priv->spi->mask, 0xffffffff);
Mario Six4d3082b2019-04-29 01:58:37 +0530106 /* LST bit doesn't do anything, so disregard */
Jagan Teki52515d52019-04-29 01:58:53 +0530107 out_be32(&priv->spi->com, 0);
108
109 return 0;
Ben Warren7efe9272008-01-16 22:37:35 -0500110}
111
Jagan Teki52515d52019-04-29 01:58:53 +0530112static void mpc8xxx_spi_cs_activate(struct udevice *dev)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200113{
Jagan Teki52515d52019-04-29 01:58:53 +0530114 struct mpc8xxx_priv *priv = dev_get_priv(dev->parent);
Simon Glassb75b15b2020-12-03 16:55:23 -0700115 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Jagan Teki52515d52019-04-29 01:58:53 +0530116
Simon Glass71fa5b42020-12-03 16:55:18 -0700117 dm_gpio_set_value(&priv->gpios[plat->cs], 1);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200118}
119
Jagan Teki52515d52019-04-29 01:58:53 +0530120static void mpc8xxx_spi_cs_deactivate(struct udevice *dev)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200121{
Jagan Teki52515d52019-04-29 01:58:53 +0530122 struct mpc8xxx_priv *priv = dev_get_priv(dev->parent);
Simon Glassb75b15b2020-12-03 16:55:23 -0700123 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Jagan Teki52515d52019-04-29 01:58:53 +0530124
Simon Glass71fa5b42020-12-03 16:55:18 -0700125 dm_gpio_set_value(&priv->gpios[plat->cs], 0);
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200126}
127
Jagan Teki52515d52019-04-29 01:58:53 +0530128static int mpc8xxx_spi_xfer(struct udevice *dev, uint bitlen,
129 const void *dout, void *din, ulong flags)
Ben Warren7efe9272008-01-16 22:37:35 -0500130{
Jagan Teki52515d52019-04-29 01:58:53 +0530131 struct udevice *bus = dev->parent;
132 struct mpc8xxx_priv *priv = dev_get_priv(bus);
133 spi8xxx_t *spi = priv->spi;
Simon Glassb75b15b2020-12-03 16:55:23 -0700134 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000135 u32 tmpdin = 0, tmpdout = 0, n;
136 const u8 *cout = dout;
137 u8 *cin = din;
Christophe Leroyc58bd4a2023-03-02 16:26:26 +0100138 ulong type = dev_get_driver_data(bus);
Ben Warren7efe9272008-01-16 22:37:35 -0500139
Jagan Teki52515d52019-04-29 01:58:53 +0530140 debug("%s: slave %s:%u dout %08X din %08X bitlen %u\n", __func__,
Simon Glass71fa5b42020-12-03 16:55:18 -0700141 bus->name, plat->cs, (uint)dout, (uint)din, bitlen);
142 if (plat->cs >= priv->cs_count) {
Rasmus Villemoesd31ec8b2020-02-11 15:20:24 +0000143 dev_err(dev, "chip select index %d too large (cs_count=%d)\n",
Simon Glass71fa5b42020-12-03 16:55:18 -0700144 plat->cs, priv->cs_count);
Rasmus Villemoesd31ec8b2020-02-11 15:20:24 +0000145 return -EINVAL;
146 }
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000147 if (bitlen % 8) {
148 printf("*** spi_xfer: bitlen must be multiple of 8\n");
149 return -ENOTSUPP;
150 }
Ben Warren7efe9272008-01-16 22:37:35 -0500151
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200152 if (flags & SPI_XFER_BEGIN)
Jagan Teki52515d52019-04-29 01:58:53 +0530153 mpc8xxx_spi_cs_activate(dev);
Ben Warren7efe9272008-01-16 22:37:35 -0500154
Mario Six4d3082b2019-04-29 01:58:37 +0530155 /* Clear all SPI events */
Mario Sixdee99492019-04-29 01:58:42 +0530156 setbits_be32(&spi->event, 0xffffffff);
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000157 n = bitlen / 8;
Ben Warren7efe9272008-01-16 22:37:35 -0500158
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000159 /* Handle data in 8-bit chunks */
160 while (n--) {
Mario Six9083b132019-04-29 01:58:52 +0530161 ulong start;
Ben Warren7efe9272008-01-16 22:37:35 -0500162
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000163 if (cout)
164 tmpdout = *cout++;
Ben Warren7efe9272008-01-16 22:37:35 -0500165
Christophe Leroyc58bd4a2023-03-02 16:26:26 +0100166 if (type == SOC_MPC832X)
167 tmpdout <<= 24;
168
Mario Six4d3082b2019-04-29 01:58:37 +0530169 /* Write the data out */
Mario Sixdee99492019-04-29 01:58:42 +0530170 out_be32(&spi->tx, tmpdout);
Mario Six4d3082b2019-04-29 01:58:37 +0530171
Mario Sixf9d5ca22019-04-29 01:58:40 +0530172 debug("*** %s: ... %08x written\n", __func__, tmpdout);
Ben Warren7efe9272008-01-16 22:37:35 -0500173
Kim Phillipsb8e25202008-01-17 12:48:00 -0600174 /*
Ben Warren7efe9272008-01-16 22:37:35 -0500175 * Wait for SPI transmit to get out
176 * or time out (1 second = 1000 ms)
177 * The NE event must be read and cleared first
Kim Phillipsb8e25202008-01-17 12:48:00 -0600178 */
Mario Six9083b132019-04-29 01:58:52 +0530179 start = get_timer(0);
180 do {
Mario Six8d684ec2019-04-29 01:58:46 +0530181 u32 event = in_be32(&spi->event);
Mario Six4b671e12019-04-29 01:58:44 +0530182 bool have_ne = event & SPI_EV_NE;
183 bool have_nf = event & SPI_EV_NF;
184
Mario Six2afedfe2019-04-29 01:58:45 +0530185 if (!have_ne)
186 continue;
Ben Warren7efe9272008-01-16 22:37:35 -0500187
Mario Six2afedfe2019-04-29 01:58:45 +0530188 tmpdin = in_be32(&spi->rx);
189 setbits_be32(&spi->event, SPI_EV_NE);
190
Christophe Leroyc58bd4a2023-03-02 16:26:26 +0100191 if (type == SOC_MPC832X)
192 tmpdin >>= 16;
193
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000194 if (cin)
195 *cin++ = tmpdin;
Mario Six2afedfe2019-04-29 01:58:45 +0530196
Kim Phillipsb8e25202008-01-17 12:48:00 -0600197 /*
198 * Only bail when we've had both NE and NF events.
Ben Warren7efe9272008-01-16 22:37:35 -0500199 * This will cause timeouts on RO devices, so maybe
200 * in the future put an arbitrary delay after writing
Kim Phillipsb8e25202008-01-17 12:48:00 -0600201 * the device. Arbitrary delays suck, though...
202 */
Mario Six2afedfe2019-04-29 01:58:45 +0530203 if (have_nf)
Ben Warren7efe9272008-01-16 22:37:35 -0500204 break;
Mario Six9083b132019-04-29 01:58:52 +0530205
206 mdelay(1);
207 } while (get_timer(start) < SPI_TIMEOUT);
Mario Six2afedfe2019-04-29 01:58:45 +0530208
Jagan Teki52515d52019-04-29 01:58:53 +0530209 if (get_timer(start) >= SPI_TIMEOUT) {
Mario Sixf9d5ca22019-04-29 01:58:40 +0530210 debug("*** %s: Time out during SPI transfer\n",
211 __func__);
Jagan Teki52515d52019-04-29 01:58:53 +0530212 return -ETIMEDOUT;
213 }
Ben Warren7efe9272008-01-16 22:37:35 -0500214
Mario Sixf9d5ca22019-04-29 01:58:40 +0530215 debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin);
Ben Warren7efe9272008-01-16 22:37:35 -0500216 }
217
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200218 if (flags & SPI_XFER_END)
Jagan Teki52515d52019-04-29 01:58:53 +0530219 mpc8xxx_spi_cs_deactivate(dev);
220
221 return 0;
222}
223
224static int mpc8xxx_spi_set_speed(struct udevice *dev, uint speed)
225{
Rasmus Villemoes15340312020-02-11 15:20:25 +0000226 struct mpc8xxx_priv *priv = dev_get_priv(dev);
227 spi8xxx_t *spi = priv->spi;
228 u32 bits, mask, div16, pm;
229 u32 mode;
230 ulong clk;
231
232 clk = priv->clk_rate;
233 if (clk / 64 > speed) {
234 div16 = SPI_MODE_DIV16;
235 clk /= 16;
236 } else {
237 div16 = 0;
238 }
239 pm = (clk - 1)/(4*speed) + 1;
240 if (pm > 16) {
241 dev_err(dev, "requested speed %u too small\n", speed);
242 return -EINVAL;
243 }
244 pm--;
245
246 bits = div16 | (pm << SPI_MODE_PM_SHIFT);
247 mask = SPI_MODE_DIV16 | SPI_MODE_PM_MASK;
248 mode = in_be32(&spi->mode);
249 if ((mode & mask) != bits) {
250 /* Must clear mode[EN] while changing speed. */
251 mode &= ~(mask | SPI_MODE_EN);
252 out_be32(&spi->mode, mode);
253 mode |= bits;
254 out_be32(&spi->mode, mode);
255 mode |= SPI_MODE_EN;
256 out_be32(&spi->mode, mode);
257 }
258
259 debug("requested speed %u, set speed to %lu/(%s4*%u) == %lu\n",
260 speed, priv->clk_rate, div16 ? "16*" : "", pm + 1,
261 clk/(4*(pm + 1)));
262
Rasmus Villemoes379e25d2020-02-11 15:20:25 +0000263 return 0;
Jagan Teki52515d52019-04-29 01:58:53 +0530264}
Kim Phillipsb8e25202008-01-17 12:48:00 -0600265
Jagan Teki52515d52019-04-29 01:58:53 +0530266static int mpc8xxx_spi_set_mode(struct udevice *dev, uint mode)
267{
268 /* TODO(mario.six@gdsys.cc): Using SPI_CPHA (for clock phase) and
269 * SPI_CPOL (for clock polarity) should work
270 */
Ben Warren7efe9272008-01-16 22:37:35 -0500271 return 0;
272}
Jagan Teki52515d52019-04-29 01:58:53 +0530273
274static const struct dm_spi_ops mpc8xxx_spi_ops = {
275 .xfer = mpc8xxx_spi_xfer,
276 .set_speed = mpc8xxx_spi_set_speed,
277 .set_mode = mpc8xxx_spi_set_mode,
278 /*
279 * cs_info is not needed, since we require all chip selects to be
280 * in the device tree explicitly
281 */
282};
283
284static const struct udevice_id mpc8xxx_spi_ids[] = {
285 { .compatible = "fsl,spi" },
Christophe Leroyc58bd4a2023-03-02 16:26:26 +0100286 { .compatible = "fsl,mpc832x-spi", .data = SOC_MPC832X },
Jagan Teki52515d52019-04-29 01:58:53 +0530287 { }
288};
289
290U_BOOT_DRIVER(mpc8xxx_spi) = {
291 .name = "mpc8xxx_spi",
292 .id = UCLASS_SPI,
293 .of_match = mpc8xxx_spi_ids,
294 .ops = &mpc8xxx_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700295 .of_to_plat = mpc8xxx_spi_of_to_plat,
Jagan Teki52515d52019-04-29 01:58:53 +0530296 .probe = mpc8xxx_spi_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700297 .priv_auto = sizeof(struct mpc8xxx_priv),
Jagan Teki52515d52019-04-29 01:58:53 +0530298};