blob: 8ef24514112585b800cf551432c18aeea9d847a1 [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>
Jagan Tekic1a3f1e2019-04-29 01:58:53 +05308#include <dm.h>
9#include <errno.h>
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020010#include <malloc.h>
Ben Warren04a9e112008-01-16 22:37:35 -050011#include <spi.h>
12#include <asm/mpc8xxx_spi.h>
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053013#include <asm-generic/gpio.h>
Ben Warren04a9e112008-01-16 22:37:35 -050014
Mario Six6ea93952019-04-29 01:58:41 +053015enum {
16 SPI_EV_NE = BIT(31 - 22), /* Receiver Not Empty */
17 SPI_EV_NF = BIT(31 - 23), /* Transmitter Not Full */
18};
Ben Warren04a9e112008-01-16 22:37:35 -050019
Mario Six6ea93952019-04-29 01:58:41 +053020enum {
21 SPI_MODE_LOOP = BIT(31 - 1), /* Loopback mode */
22 SPI_MODE_CI = BIT(31 - 2), /* Clock invert */
23 SPI_MODE_CP = BIT(31 - 3), /* Clock phase */
24 SPI_MODE_DIV16 = BIT(31 - 4), /* Divide clock source by 16 */
25 SPI_MODE_REV = BIT(31 - 5), /* Reverse mode - MSB first */
26 SPI_MODE_MS = BIT(31 - 6), /* Always master */
27 SPI_MODE_EN = BIT(31 - 7), /* Enable interface */
28
29 SPI_MODE_LEN_MASK = 0xf00000,
Rasmus Villemoes391c4002020-02-11 15:20:25 +000030 SPI_MODE_LEN_SHIFT = 20,
Mario Six6ea93952019-04-29 01:58:41 +053031 SPI_MODE_PM_MASK = 0xf0000,
32
33 SPI_COM_LST = BIT(31 - 9),
34};
Ben Warren04a9e112008-01-16 22:37:35 -050035
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053036struct mpc8xxx_priv {
37 spi8xxx_t *spi;
38 struct gpio_desc gpios[16];
Rasmus Villemoes1a7b4622020-02-11 15:20:24 +000039 int cs_count;
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053040};
41
Mario Six8dea61d2019-04-29 01:58:47 +053042static inline u32 to_prescale_mod(u32 val)
43{
44 return (min(val, (u32)15) << 16);
45}
46
Ben Warren04a9e112008-01-16 22:37:35 -050047#define SPI_TIMEOUT 1000
48
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053049static int mpc8xxx_spi_ofdata_to_platdata(struct udevice *dev)
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020050{
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053051 struct mpc8xxx_priv *priv = dev_get_priv(dev);
52 int ret;
53
54 priv->spi = (spi8xxx_t *)dev_read_addr(dev);
55
56 /* TODO(mario.six@gdsys.cc): Read clock and save the value */
57
58 ret = gpio_request_list_by_name(dev, "gpios", priv->gpios,
59 ARRAY_SIZE(priv->gpios), GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
60 if (ret < 0)
61 return -EINVAL;
62
Rasmus Villemoes1a7b4622020-02-11 15:20:24 +000063 priv->cs_count = ret;
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053064
65 return 0;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020066}
67
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053068static int mpc8xxx_spi_probe(struct udevice *dev)
Ben Warren04a9e112008-01-16 22:37:35 -050069{
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053070 struct mpc8xxx_priv *priv = dev_get_priv(dev);
Rasmus Villemoes391c4002020-02-11 15:20:25 +000071 spi8xxx_t *spi = priv->spi;
Ben Warren04a9e112008-01-16 22:37:35 -050072
Kim Phillips2956acd2008-01-17 12:48:00 -060073 /*
Ben Warren04a9e112008-01-16 22:37:35 -050074 * SPI pins on the MPC83xx are not muxed, so all we do is initialize
75 * some registers
Kim Phillips2956acd2008-01-17 12:48:00 -060076 */
Rasmus Villemoes391c4002020-02-11 15:20:25 +000077 out_be32(&priv->spi->mode, SPI_MODE_REV | SPI_MODE_MS);
Ben Warren04a9e112008-01-16 22:37:35 -050078
Rasmus Villemoes391c4002020-02-11 15:20:25 +000079 /* set len to 8 bits */
80 setbits_be32(&spi->mode, (8 - 1) << SPI_MODE_LEN_SHIFT);
81
82 /* TODO(mario.six@gdsys.cc): This only ever sets one fixed speed */
83 /* Use SYSCLK / 8 (16.67MHz typ.) */
84 clrsetbits_be32(&spi->mode, SPI_MODE_PM_MASK, to_prescale_mod(1));
85
86 setbits_be32(&spi->mode, SPI_MODE_EN);
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053087
88 /* Clear all SPI events */
89 setbits_be32(&priv->spi->event, 0xffffffff);
90 /* Mask all SPI interrupts */
91 clrbits_be32(&priv->spi->mask, 0xffffffff);
92 /* LST bit doesn't do anything, so disregard */
93 out_be32(&priv->spi->com, 0);
94
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020095 return 0;
96}
97
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053098static void mpc8xxx_spi_cs_activate(struct udevice *dev)
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020099{
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530100 struct mpc8xxx_priv *priv = dev_get_priv(dev->parent);
101 struct dm_spi_slave_platdata *platdata = dev_get_parent_platdata(dev);
102
103 dm_gpio_set_dir_flags(&priv->gpios[platdata->cs], GPIOD_IS_OUT);
104 dm_gpio_set_value(&priv->gpios[platdata->cs], 0);
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200105}
106
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530107static void mpc8xxx_spi_cs_deactivate(struct udevice *dev)
Ben Warren04a9e112008-01-16 22:37:35 -0500108{
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530109 struct mpc8xxx_priv *priv = dev_get_priv(dev->parent);
110 struct dm_spi_slave_platdata *platdata = dev_get_parent_platdata(dev);
111
112 dm_gpio_set_dir_flags(&priv->gpios[platdata->cs], GPIOD_IS_OUT);
113 dm_gpio_set_value(&priv->gpios[platdata->cs], 1);
114}
115
116static int mpc8xxx_spi_xfer(struct udevice *dev, uint bitlen,
117 const void *dout, void *din, ulong flags)
118{
119 struct udevice *bus = dev->parent;
120 struct mpc8xxx_priv *priv = dev_get_priv(bus);
121 spi8xxx_t *spi = priv->spi;
122 struct dm_spi_slave_platdata *platdata = dev_get_parent_platdata(dev);
Rasmus Villemoes391c4002020-02-11 15:20:25 +0000123 u32 tmpdin = 0, tmpdout = 0, n;
124 const u8 *cout = dout;
125 u8 *cin = din;
Ben Warren04a9e112008-01-16 22:37:35 -0500126
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530127 debug("%s: slave %s:%u dout %08X din %08X bitlen %u\n", __func__,
Rasmus Villemoes391c4002020-02-11 15:20:25 +0000128 bus->name, platdata->cs, (uint)dout, (uint)din, bitlen);
Rasmus Villemoes1a7b4622020-02-11 15:20:24 +0000129 if (platdata->cs >= priv->cs_count) {
130 dev_err(dev, "chip select index %d too large (cs_count=%d)\n",
131 platdata->cs, priv->cs_count);
132 return -EINVAL;
133 }
Rasmus Villemoes391c4002020-02-11 15:20:25 +0000134 if (bitlen % 8) {
135 printf("*** spi_xfer: bitlen must be multiple of 8\n");
136 return -ENOTSUPP;
137 }
Ben Warren04a9e112008-01-16 22:37:35 -0500138
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200139 if (flags & SPI_XFER_BEGIN)
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530140 mpc8xxx_spi_cs_activate(dev);
Ben Warren04a9e112008-01-16 22:37:35 -0500141
Mario Sixd93fe312019-04-29 01:58:37 +0530142 /* Clear all SPI events */
Mario Six1a907e42019-04-29 01:58:42 +0530143 setbits_be32(&spi->event, 0xffffffff);
Rasmus Villemoes391c4002020-02-11 15:20:25 +0000144 n = bitlen / 8;
Ben Warren04a9e112008-01-16 22:37:35 -0500145
Rasmus Villemoes391c4002020-02-11 15:20:25 +0000146 /* Handle data in 8-bit chunks */
147 while (n--) {
Mario Six67adbae2019-04-29 01:58:52 +0530148 ulong start;
Ben Warren04a9e112008-01-16 22:37:35 -0500149
Rasmus Villemoes391c4002020-02-11 15:20:25 +0000150 if (cout)
151 tmpdout = *cout++;
Ben Warren04a9e112008-01-16 22:37:35 -0500152
Mario Sixd93fe312019-04-29 01:58:37 +0530153 /* Write the data out */
Mario Six1a907e42019-04-29 01:58:42 +0530154 out_be32(&spi->tx, tmpdout);
Mario Sixd93fe312019-04-29 01:58:37 +0530155
Mario Sixfabe6c42019-04-29 01:58:40 +0530156 debug("*** %s: ... %08x written\n", __func__, tmpdout);
Ben Warren04a9e112008-01-16 22:37:35 -0500157
Kim Phillips2956acd2008-01-17 12:48:00 -0600158 /*
Ben Warren04a9e112008-01-16 22:37:35 -0500159 * Wait for SPI transmit to get out
160 * or time out (1 second = 1000 ms)
161 * The NE event must be read and cleared first
Kim Phillips2956acd2008-01-17 12:48:00 -0600162 */
Mario Six67adbae2019-04-29 01:58:52 +0530163 start = get_timer(0);
164 do {
Mario Six65f88e02019-04-29 01:58:46 +0530165 u32 event = in_be32(&spi->event);
Mario Six6409c612019-04-29 01:58:44 +0530166 bool have_ne = event & SPI_EV_NE;
167 bool have_nf = event & SPI_EV_NF;
168
Mario Sixe4da4c22019-04-29 01:58:45 +0530169 if (!have_ne)
170 continue;
Ben Warren04a9e112008-01-16 22:37:35 -0500171
Mario Sixe4da4c22019-04-29 01:58:45 +0530172 tmpdin = in_be32(&spi->rx);
173 setbits_be32(&spi->event, SPI_EV_NE);
174
Rasmus Villemoes391c4002020-02-11 15:20:25 +0000175 if (cin)
176 *cin++ = tmpdin;
Mario Sixe4da4c22019-04-29 01:58:45 +0530177
Kim Phillips2956acd2008-01-17 12:48:00 -0600178 /*
179 * Only bail when we've had both NE and NF events.
Ben Warren04a9e112008-01-16 22:37:35 -0500180 * This will cause timeouts on RO devices, so maybe
181 * in the future put an arbitrary delay after writing
Kim Phillips2956acd2008-01-17 12:48:00 -0600182 * the device. Arbitrary delays suck, though...
183 */
Mario Sixe4da4c22019-04-29 01:58:45 +0530184 if (have_nf)
Ben Warren04a9e112008-01-16 22:37:35 -0500185 break;
Mario Sixe4da4c22019-04-29 01:58:45 +0530186
Mario Six67adbae2019-04-29 01:58:52 +0530187 mdelay(1);
188 } while (get_timer(start) < SPI_TIMEOUT);
189
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530190 if (get_timer(start) >= SPI_TIMEOUT) {
Mario Sixfabe6c42019-04-29 01:58:40 +0530191 debug("*** %s: Time out during SPI transfer\n",
192 __func__);
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530193 return -ETIMEDOUT;
194 }
Ben Warren04a9e112008-01-16 22:37:35 -0500195
Mario Sixfabe6c42019-04-29 01:58:40 +0530196 debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin);
Ben Warren04a9e112008-01-16 22:37:35 -0500197 }
198
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200199 if (flags & SPI_XFER_END)
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530200 mpc8xxx_spi_cs_deactivate(dev);
Kim Phillips2956acd2008-01-17 12:48:00 -0600201
Ben Warren04a9e112008-01-16 22:37:35 -0500202 return 0;
203}
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530204
205static int mpc8xxx_spi_set_speed(struct udevice *dev, uint speed)
206{
Rasmus Villemoes391c4002020-02-11 15:20:25 +0000207 return 0;
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530208}
209
210static int mpc8xxx_spi_set_mode(struct udevice *dev, uint mode)
211{
212 /* TODO(mario.six@gdsys.cc): Using SPI_CPHA (for clock phase) and
213 * SPI_CPOL (for clock polarity) should work
214 */
215 return 0;
216}
217
218static const struct dm_spi_ops mpc8xxx_spi_ops = {
219 .xfer = mpc8xxx_spi_xfer,
220 .set_speed = mpc8xxx_spi_set_speed,
221 .set_mode = mpc8xxx_spi_set_mode,
222 /*
223 * cs_info is not needed, since we require all chip selects to be
224 * in the device tree explicitly
225 */
226};
227
228static const struct udevice_id mpc8xxx_spi_ids[] = {
229 { .compatible = "fsl,spi" },
230 { }
231};
232
233U_BOOT_DRIVER(mpc8xxx_spi) = {
234 .name = "mpc8xxx_spi",
235 .id = UCLASS_SPI,
236 .of_match = mpc8xxx_spi_ids,
237 .ops = &mpc8xxx_spi_ops,
238 .ofdata_to_platdata = mpc8xxx_spi_ofdata_to_platdata,
239 .probe = mpc8xxx_spi_probe,
240 .priv_auto_alloc_size = sizeof(struct mpc8xxx_priv),
241};