blob: 78892173dc1f40b90cb18cddc7d0674aec493528 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ben Warren04a9e112008-01-16 22:37:35 -05002/*
3 * Copyright (c) 2006 Ben Warren, Qstreams Networks Inc.
Stefan Roesea47a12b2010-04-15 16:07:28 +02004 * With help from the common/soft_spi and arch/powerpc/cpu/mpc8260 drivers
Ben Warren04a9e112008-01-16 22:37:35 -05005 */
6
7#include <common.h>
Rasmus Villemoes4856cc72020-02-11 15:20:25 +00008#include <clk.h>
Jagan Tekic1a3f1e2019-04-29 01:58:53 +05309#include <dm.h>
10#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020012#include <malloc.h>
Ben Warren04a9e112008-01-16 22:37:35 -050013#include <spi.h>
14#include <asm/mpc8xxx_spi.h>
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053015#include <asm-generic/gpio.h>
Rasmus Villemoescffedec2020-04-20 16:13:41 +020016#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060017#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060018#include <linux/delay.h>
Christophe Leroy83945ef2023-03-02 16:26:26 +010019#include <asm/arch/soc.h>
Ben Warren04a9e112008-01-16 22:37:35 -050020
Mario Six6ea93952019-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};
Ben Warren04a9e112008-01-16 22:37:35 -050025
Mario Six6ea93952019-04-29 01:58:41 +053026enum {
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 Leroy83945ef2023-03-02 16:26:26 +010034 SPI_MODE_OP = BIT(31 - 17), /* CPU Mode, QE otherwise */
Mario Six6ea93952019-04-29 01:58:41 +053035
36 SPI_MODE_LEN_MASK = 0xf00000,
Rasmus Villemoes391c4002020-02-11 15:20:25 +000037 SPI_MODE_LEN_SHIFT = 20,
Rasmus Villemoes4856cc72020-02-11 15:20:25 +000038 SPI_MODE_PM_SHIFT = 16,
Mario Six6ea93952019-04-29 01:58:41 +053039 SPI_MODE_PM_MASK = 0xf0000,
40
41 SPI_COM_LST = BIT(31 - 9),
42};
Ben Warren04a9e112008-01-16 22:37:35 -050043
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053044struct mpc8xxx_priv {
45 spi8xxx_t *spi;
46 struct gpio_desc gpios[16];
Rasmus Villemoes1a7b4622020-02-11 15:20:24 +000047 int cs_count;
Rasmus Villemoes4856cc72020-02-11 15:20:25 +000048 ulong clk_rate;
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053049};
50
Ben Warren04a9e112008-01-16 22:37:35 -050051#define SPI_TIMEOUT 1000
52
Simon Glassd1998a92020-12-03 16:55:21 -070053static int mpc8xxx_spi_of_to_plat(struct udevice *dev)
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020054{
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053055 struct mpc8xxx_priv *priv = dev_get_priv(dev);
Rasmus Villemoes4856cc72020-02-11 15:20:25 +000056 struct clk clk;
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053057 int ret;
58
59 priv->spi = (spi8xxx_t *)dev_read_addr(dev);
60
Jagan Tekic1a3f1e2019-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 Villemoes1a7b4622020-02-11 15:20:24 +000066 priv->cs_count = ret;
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053067
Rasmus Villemoes4856cc72020-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 Tekic1a3f1e2019-04-29 01:58:53 +053080 return 0;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020081}
82
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053083static int mpc8xxx_spi_probe(struct udevice *dev)
Ben Warren04a9e112008-01-16 22:37:35 -050084{
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053085 struct mpc8xxx_priv *priv = dev_get_priv(dev);
Rasmus Villemoes391c4002020-02-11 15:20:25 +000086 spi8xxx_t *spi = priv->spi;
Ben Warren04a9e112008-01-16 22:37:35 -050087
Kim Phillips2956acd2008-01-17 12:48:00 -060088 /*
Ben Warren04a9e112008-01-16 22:37:35 -050089 * SPI pins on the MPC83xx are not muxed, so all we do is initialize
90 * some registers
Kim Phillips2956acd2008-01-17 12:48:00 -060091 */
Rasmus Villemoes391c4002020-02-11 15:20:25 +000092 out_be32(&priv->spi->mode, SPI_MODE_REV | SPI_MODE_MS);
Ben Warren04a9e112008-01-16 22:37:35 -050093
Christophe Leroy83945ef2023-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 Villemoes391c4002020-02-11 15:20:25 +000097 /* set len to 8 bits */
98 setbits_be32(&spi->mode, (8 - 1) << SPI_MODE_LEN_SHIFT);
99
Rasmus Villemoes391c4002020-02-11 15:20:25 +0000100 setbits_be32(&spi->mode, SPI_MODE_EN);
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530101
102 /* Clear all SPI events */
103 setbits_be32(&priv->spi->event, 0xffffffff);
104 /* Mask all SPI interrupts */
105 clrbits_be32(&priv->spi->mask, 0xffffffff);
106 /* LST bit doesn't do anything, so disregard */
107 out_be32(&priv->spi->com, 0);
108
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200109 return 0;
110}
111
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530112static void mpc8xxx_spi_cs_activate(struct udevice *dev)
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200113{
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530114 struct mpc8xxx_priv *priv = dev_get_priv(dev->parent);
Simon Glass8a8d24b2020-12-03 16:55:23 -0700115 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530116
Simon Glasscaa4daa2020-12-03 16:55:18 -0700117 dm_gpio_set_value(&priv->gpios[plat->cs], 1);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200118}
119
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530120static void mpc8xxx_spi_cs_deactivate(struct udevice *dev)
Ben Warren04a9e112008-01-16 22:37:35 -0500121{
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530122 struct mpc8xxx_priv *priv = dev_get_priv(dev->parent);
Simon Glass8a8d24b2020-12-03 16:55:23 -0700123 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530124
Simon Glasscaa4daa2020-12-03 16:55:18 -0700125 dm_gpio_set_value(&priv->gpios[plat->cs], 0);
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530126}
127
128static int mpc8xxx_spi_xfer(struct udevice *dev, uint bitlen,
129 const void *dout, void *din, ulong flags)
130{
131 struct udevice *bus = dev->parent;
132 struct mpc8xxx_priv *priv = dev_get_priv(bus);
133 spi8xxx_t *spi = priv->spi;
Simon Glass8a8d24b2020-12-03 16:55:23 -0700134 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Rasmus Villemoes391c4002020-02-11 15:20:25 +0000135 u32 tmpdin = 0, tmpdout = 0, n;
136 const u8 *cout = dout;
137 u8 *cin = din;
Christophe Leroy83945ef2023-03-02 16:26:26 +0100138 ulong type = dev_get_driver_data(bus);
Ben Warren04a9e112008-01-16 22:37:35 -0500139
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530140 debug("%s: slave %s:%u dout %08X din %08X bitlen %u\n", __func__,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700141 bus->name, plat->cs, (uint)dout, (uint)din, bitlen);
142 if (plat->cs >= priv->cs_count) {
Rasmus Villemoes1a7b4622020-02-11 15:20:24 +0000143 dev_err(dev, "chip select index %d too large (cs_count=%d)\n",
Simon Glasscaa4daa2020-12-03 16:55:18 -0700144 plat->cs, priv->cs_count);
Rasmus Villemoes1a7b4622020-02-11 15:20:24 +0000145 return -EINVAL;
146 }
Rasmus Villemoes391c4002020-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 Warren04a9e112008-01-16 22:37:35 -0500151
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200152 if (flags & SPI_XFER_BEGIN)
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530153 mpc8xxx_spi_cs_activate(dev);
Ben Warren04a9e112008-01-16 22:37:35 -0500154
Mario Sixd93fe312019-04-29 01:58:37 +0530155 /* Clear all SPI events */
Mario Six1a907e42019-04-29 01:58:42 +0530156 setbits_be32(&spi->event, 0xffffffff);
Rasmus Villemoes391c4002020-02-11 15:20:25 +0000157 n = bitlen / 8;
Ben Warren04a9e112008-01-16 22:37:35 -0500158
Rasmus Villemoes391c4002020-02-11 15:20:25 +0000159 /* Handle data in 8-bit chunks */
160 while (n--) {
Mario Six67adbae2019-04-29 01:58:52 +0530161 ulong start;
Ben Warren04a9e112008-01-16 22:37:35 -0500162
Rasmus Villemoes391c4002020-02-11 15:20:25 +0000163 if (cout)
164 tmpdout = *cout++;
Ben Warren04a9e112008-01-16 22:37:35 -0500165
Christophe Leroy83945ef2023-03-02 16:26:26 +0100166 if (type == SOC_MPC832X)
167 tmpdout <<= 24;
168
Mario Sixd93fe312019-04-29 01:58:37 +0530169 /* Write the data out */
Mario Six1a907e42019-04-29 01:58:42 +0530170 out_be32(&spi->tx, tmpdout);
Mario Sixd93fe312019-04-29 01:58:37 +0530171
Mario Sixfabe6c42019-04-29 01:58:40 +0530172 debug("*** %s: ... %08x written\n", __func__, tmpdout);
Ben Warren04a9e112008-01-16 22:37:35 -0500173
Kim Phillips2956acd2008-01-17 12:48:00 -0600174 /*
Ben Warren04a9e112008-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 Phillips2956acd2008-01-17 12:48:00 -0600178 */
Mario Six67adbae2019-04-29 01:58:52 +0530179 start = get_timer(0);
180 do {
Mario Six65f88e02019-04-29 01:58:46 +0530181 u32 event = in_be32(&spi->event);
Mario Six6409c612019-04-29 01:58:44 +0530182 bool have_ne = event & SPI_EV_NE;
183 bool have_nf = event & SPI_EV_NF;
184
Mario Sixe4da4c22019-04-29 01:58:45 +0530185 if (!have_ne)
186 continue;
Ben Warren04a9e112008-01-16 22:37:35 -0500187
Mario Sixe4da4c22019-04-29 01:58:45 +0530188 tmpdin = in_be32(&spi->rx);
189 setbits_be32(&spi->event, SPI_EV_NE);
190
Christophe Leroy83945ef2023-03-02 16:26:26 +0100191 if (type == SOC_MPC832X)
192 tmpdin >>= 16;
193
Rasmus Villemoes391c4002020-02-11 15:20:25 +0000194 if (cin)
195 *cin++ = tmpdin;
Mario Sixe4da4c22019-04-29 01:58:45 +0530196
Kim Phillips2956acd2008-01-17 12:48:00 -0600197 /*
198 * Only bail when we've had both NE and NF events.
Ben Warren04a9e112008-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 Phillips2956acd2008-01-17 12:48:00 -0600201 * the device. Arbitrary delays suck, though...
202 */
Mario Sixe4da4c22019-04-29 01:58:45 +0530203 if (have_nf)
Ben Warren04a9e112008-01-16 22:37:35 -0500204 break;
Mario Sixe4da4c22019-04-29 01:58:45 +0530205
Mario Six67adbae2019-04-29 01:58:52 +0530206 mdelay(1);
207 } while (get_timer(start) < SPI_TIMEOUT);
208
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530209 if (get_timer(start) >= SPI_TIMEOUT) {
Mario Sixfabe6c42019-04-29 01:58:40 +0530210 debug("*** %s: Time out during SPI transfer\n",
211 __func__);
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530212 return -ETIMEDOUT;
213 }
Ben Warren04a9e112008-01-16 22:37:35 -0500214
Mario Sixfabe6c42019-04-29 01:58:40 +0530215 debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin);
Ben Warren04a9e112008-01-16 22:37:35 -0500216 }
217
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200218 if (flags & SPI_XFER_END)
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530219 mpc8xxx_spi_cs_deactivate(dev);
Kim Phillips2956acd2008-01-17 12:48:00 -0600220
Ben Warren04a9e112008-01-16 22:37:35 -0500221 return 0;
222}
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530223
224static int mpc8xxx_spi_set_speed(struct udevice *dev, uint speed)
225{
Rasmus Villemoes4856cc72020-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 Villemoes391c4002020-02-11 15:20:25 +0000263 return 0;
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530264}
265
266static 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 */
271 return 0;
272}
273
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 Leroy83945ef2023-03-02 16:26:26 +0100286 { .compatible = "fsl,mpc832x-spi", .data = SOC_MPC832X },
Jagan Tekic1a3f1e2019-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 Glassd1998a92020-12-03 16:55:21 -0700295 .of_to_plat = mpc8xxx_spi_of_to_plat,
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530296 .probe = mpc8xxx_spi_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700297 .priv_auto = sizeof(struct mpc8xxx_priv),
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530298};