1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2006 Ben Warren, Qstreams Networks Inc.
4 * With help from the common/soft_spi and arch/powerpc/cpu/mpc8260 drivers
14 #include <asm/mpc8xxx_spi.h>
15 #include <asm-generic/gpio.h>
16 #include <dm/device_compat.h>
17 #include <linux/bitops.h>
18 #include <linux/delay.h>
21 SPI_EV_NE = BIT(31 - 22), /* Receiver Not Empty */
22 SPI_EV_NF = BIT(31 - 23), /* Transmitter Not Full */
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 */
34 SPI_MODE_LEN_MASK = 0xf00000,
35 SPI_MODE_LEN_SHIFT = 20,
36 SPI_MODE_PM_SHIFT = 16,
37 SPI_MODE_PM_MASK = 0xf0000,
39 SPI_COM_LST = BIT(31 - 9),
44 struct gpio_desc gpios[16];
49 #define SPI_TIMEOUT 1000
51 static int mpc8xxx_spi_ofdata_to_platdata(struct udevice *dev)
53 struct mpc8xxx_priv *priv = dev_get_priv(dev);
57 priv->spi = (spi8xxx_t *)dev_read_addr(dev);
59 ret = gpio_request_list_by_name(dev, "gpios", priv->gpios,
60 ARRAY_SIZE(priv->gpios), GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
66 ret = clk_get_by_index(dev, 0, &clk);
68 dev_err(dev, "%s: clock not defined\n", __func__);
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__);
81 static int mpc8xxx_spi_probe(struct udevice *dev)
83 struct mpc8xxx_priv *priv = dev_get_priv(dev);
84 spi8xxx_t *spi = priv->spi;
87 * SPI pins on the MPC83xx are not muxed, so all we do is initialize
90 out_be32(&priv->spi->mode, SPI_MODE_REV | SPI_MODE_MS);
92 /* set len to 8 bits */
93 setbits_be32(&spi->mode, (8 - 1) << SPI_MODE_LEN_SHIFT);
95 setbits_be32(&spi->mode, SPI_MODE_EN);
97 /* Clear all SPI events */
98 setbits_be32(&priv->spi->event, 0xffffffff);
99 /* Mask all SPI interrupts */
100 clrbits_be32(&priv->spi->mask, 0xffffffff);
101 /* LST bit doesn't do anything, so disregard */
102 out_be32(&priv->spi->com, 0);
107 static void mpc8xxx_spi_cs_activate(struct udevice *dev)
109 struct mpc8xxx_priv *priv = dev_get_priv(dev->parent);
110 struct dm_spi_slave_platdata *platdata = dev_get_parent_platdata(dev);
112 dm_gpio_set_dir_flags(&priv->gpios[platdata->cs], GPIOD_IS_OUT);
113 dm_gpio_set_value(&priv->gpios[platdata->cs], 0);
116 static void mpc8xxx_spi_cs_deactivate(struct udevice *dev)
118 struct mpc8xxx_priv *priv = dev_get_priv(dev->parent);
119 struct dm_spi_slave_platdata *platdata = dev_get_parent_platdata(dev);
121 dm_gpio_set_dir_flags(&priv->gpios[platdata->cs], GPIOD_IS_OUT);
122 dm_gpio_set_value(&priv->gpios[platdata->cs], 1);
125 static int mpc8xxx_spi_xfer(struct udevice *dev, uint bitlen,
126 const void *dout, void *din, ulong flags)
128 struct udevice *bus = dev->parent;
129 struct mpc8xxx_priv *priv = dev_get_priv(bus);
130 spi8xxx_t *spi = priv->spi;
131 struct dm_spi_slave_platdata *platdata = dev_get_parent_platdata(dev);
132 u32 tmpdin = 0, tmpdout = 0, n;
133 const u8 *cout = dout;
136 debug("%s: slave %s:%u dout %08X din %08X bitlen %u\n", __func__,
137 bus->name, platdata->cs, (uint)dout, (uint)din, bitlen);
138 if (platdata->cs >= priv->cs_count) {
139 dev_err(dev, "chip select index %d too large (cs_count=%d)\n",
140 platdata->cs, priv->cs_count);
144 printf("*** spi_xfer: bitlen must be multiple of 8\n");
148 if (flags & SPI_XFER_BEGIN)
149 mpc8xxx_spi_cs_activate(dev);
151 /* Clear all SPI events */
152 setbits_be32(&spi->event, 0xffffffff);
155 /* Handle data in 8-bit chunks */
162 /* Write the data out */
163 out_be32(&spi->tx, tmpdout);
165 debug("*** %s: ... %08x written\n", __func__, tmpdout);
168 * Wait for SPI transmit to get out
169 * or time out (1 second = 1000 ms)
170 * The NE event must be read and cleared first
172 start = get_timer(0);
174 u32 event = in_be32(&spi->event);
175 bool have_ne = event & SPI_EV_NE;
176 bool have_nf = event & SPI_EV_NF;
181 tmpdin = in_be32(&spi->rx);
182 setbits_be32(&spi->event, SPI_EV_NE);
188 * Only bail when we've had both NE and NF events.
189 * This will cause timeouts on RO devices, so maybe
190 * in the future put an arbitrary delay after writing
191 * the device. Arbitrary delays suck, though...
197 } while (get_timer(start) < SPI_TIMEOUT);
199 if (get_timer(start) >= SPI_TIMEOUT) {
200 debug("*** %s: Time out during SPI transfer\n",
205 debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin);
208 if (flags & SPI_XFER_END)
209 mpc8xxx_spi_cs_deactivate(dev);
214 static int mpc8xxx_spi_set_speed(struct udevice *dev, uint speed)
216 struct mpc8xxx_priv *priv = dev_get_priv(dev);
217 spi8xxx_t *spi = priv->spi;
218 u32 bits, mask, div16, pm;
222 clk = priv->clk_rate;
223 if (clk / 64 > speed) {
224 div16 = SPI_MODE_DIV16;
229 pm = (clk - 1)/(4*speed) + 1;
231 dev_err(dev, "requested speed %u too small\n", speed);
236 bits = div16 | (pm << SPI_MODE_PM_SHIFT);
237 mask = SPI_MODE_DIV16 | SPI_MODE_PM_MASK;
238 mode = in_be32(&spi->mode);
239 if ((mode & mask) != bits) {
240 /* Must clear mode[EN] while changing speed. */
241 mode &= ~(mask | SPI_MODE_EN);
242 out_be32(&spi->mode, mode);
244 out_be32(&spi->mode, mode);
246 out_be32(&spi->mode, mode);
249 debug("requested speed %u, set speed to %lu/(%s4*%u) == %lu\n",
250 speed, priv->clk_rate, div16 ? "16*" : "", pm + 1,
256 static int mpc8xxx_spi_set_mode(struct udevice *dev, uint mode)
258 /* TODO(mario.six@gdsys.cc): Using SPI_CPHA (for clock phase) and
259 * SPI_CPOL (for clock polarity) should work
264 static const struct dm_spi_ops mpc8xxx_spi_ops = {
265 .xfer = mpc8xxx_spi_xfer,
266 .set_speed = mpc8xxx_spi_set_speed,
267 .set_mode = mpc8xxx_spi_set_mode,
269 * cs_info is not needed, since we require all chip selects to be
270 * in the device tree explicitly
274 static const struct udevice_id mpc8xxx_spi_ids[] = {
275 { .compatible = "fsl,spi" },
279 U_BOOT_DRIVER(mpc8xxx_spi) = {
280 .name = "mpc8xxx_spi",
282 .of_match = mpc8xxx_spi_ids,
283 .ops = &mpc8xxx_spi_ops,
284 .ofdata_to_platdata = mpc8xxx_spi_ofdata_to_platdata,
285 .probe = mpc8xxx_spi_probe,
286 .priv_auto_alloc_size = sizeof(struct mpc8xxx_priv),