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
13 #include <asm/mpc8xxx_spi.h>
14 #include <asm-generic/gpio.h>
15 #include <dm/device_compat.h>
16 #include <linux/bitops.h>
17 #include <linux/delay.h>
18 #include <asm/arch/soc.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 */
33 SPI_MODE_OP = BIT(31 - 17), /* CPU Mode, QE otherwise */
35 SPI_MODE_LEN_MASK = 0xf00000,
36 SPI_MODE_LEN_SHIFT = 20,
37 SPI_MODE_PM_SHIFT = 16,
38 SPI_MODE_PM_MASK = 0xf0000,
40 SPI_COM_LST = BIT(31 - 9),
45 struct gpio_desc gpios[16];
50 #define SPI_TIMEOUT 1000
52 static int mpc8xxx_spi_of_to_plat(struct udevice *dev)
54 struct mpc8xxx_priv *priv = dev_get_priv(dev);
58 priv->spi = dev_read_addr_ptr(dev);
60 ret = gpio_request_list_by_name(dev, "gpios", priv->gpios,
61 ARRAY_SIZE(priv->gpios), GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
67 ret = clk_get_by_index(dev, 0, &clk);
69 dev_err(dev, "%s: clock not defined\n", __func__);
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__);
82 static int mpc8xxx_spi_probe(struct udevice *dev)
84 struct mpc8xxx_priv *priv = dev_get_priv(dev);
85 spi8xxx_t *spi = priv->spi;
88 * SPI pins on the MPC83xx are not muxed, so all we do is initialize
91 out_be32(&priv->spi->mode, SPI_MODE_REV | SPI_MODE_MS);
93 if (dev_get_driver_data(dev) == SOC_MPC832X)
94 setbits_be32(&priv->spi->mode, SPI_MODE_OP);
96 /* set len to 8 bits */
97 setbits_be32(&spi->mode, (8 - 1) << SPI_MODE_LEN_SHIFT);
99 setbits_be32(&spi->mode, SPI_MODE_EN);
101 /* Clear all SPI events */
102 setbits_be32(&priv->spi->event, 0xffffffff);
103 /* Mask all SPI interrupts */
104 clrbits_be32(&priv->spi->mask, 0xffffffff);
105 /* LST bit doesn't do anything, so disregard */
106 out_be32(&priv->spi->com, 0);
111 static void mpc8xxx_spi_cs_activate(struct udevice *dev)
113 struct mpc8xxx_priv *priv = dev_get_priv(dev->parent);
114 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
116 dm_gpio_set_value(&priv->gpios[plat->cs], 1);
119 static void mpc8xxx_spi_cs_deactivate(struct udevice *dev)
121 struct mpc8xxx_priv *priv = dev_get_priv(dev->parent);
122 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
124 dm_gpio_set_value(&priv->gpios[plat->cs], 0);
127 static int mpc8xxx_spi_xfer(struct udevice *dev, uint bitlen,
128 const void *dout, void *din, ulong flags)
130 struct udevice *bus = dev->parent;
131 struct mpc8xxx_priv *priv = dev_get_priv(bus);
132 spi8xxx_t *spi = priv->spi;
133 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
134 u32 tmpdin = 0, tmpdout = 0, n;
135 const u8 *cout = dout;
137 ulong type = dev_get_driver_data(bus);
139 debug("%s: slave %s:%u dout %08X din %08X bitlen %u\n", __func__,
140 bus->name, plat->cs, (uint)dout, (uint)din, bitlen);
141 if (plat->cs >= priv->cs_count) {
142 dev_err(dev, "chip select index %d too large (cs_count=%d)\n",
143 plat->cs, priv->cs_count);
147 printf("*** spi_xfer: bitlen must be multiple of 8\n");
151 if (flags & SPI_XFER_BEGIN)
152 mpc8xxx_spi_cs_activate(dev);
154 /* Clear all SPI events */
155 setbits_be32(&spi->event, 0xffffffff);
158 /* Handle data in 8-bit chunks */
165 if (type == SOC_MPC832X)
168 /* Write the data out */
169 out_be32(&spi->tx, tmpdout);
171 debug("*** %s: ... %08x written\n", __func__, tmpdout);
174 * 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
178 start = get_timer(0);
180 u32 event = in_be32(&spi->event);
181 bool have_ne = event & SPI_EV_NE;
182 bool have_nf = event & SPI_EV_NF;
187 tmpdin = in_be32(&spi->rx);
188 setbits_be32(&spi->event, SPI_EV_NE);
190 if (type == SOC_MPC832X)
197 * Only bail when we've had both NE and NF events.
198 * This will cause timeouts on RO devices, so maybe
199 * in the future put an arbitrary delay after writing
200 * the device. Arbitrary delays suck, though...
206 } while (get_timer(start) < SPI_TIMEOUT);
208 if (get_timer(start) >= SPI_TIMEOUT) {
209 debug("*** %s: Time out during SPI transfer\n",
214 debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin);
217 if (flags & SPI_XFER_END)
218 mpc8xxx_spi_cs_deactivate(dev);
223 static int mpc8xxx_spi_set_speed(struct udevice *dev, uint speed)
225 struct mpc8xxx_priv *priv = dev_get_priv(dev);
226 spi8xxx_t *spi = priv->spi;
227 u32 bits, mask, div16, pm;
231 clk = priv->clk_rate;
232 if (clk / 64 > speed) {
233 div16 = SPI_MODE_DIV16;
238 pm = (clk - 1)/(4*speed) + 1;
240 dev_err(dev, "requested speed %u too small\n", speed);
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);
253 out_be32(&spi->mode, mode);
255 out_be32(&spi->mode, mode);
258 debug("requested speed %u, set speed to %lu/(%s4*%u) == %lu\n",
259 speed, priv->clk_rate, div16 ? "16*" : "", pm + 1,
265 static int mpc8xxx_spi_set_mode(struct udevice *dev, uint mode)
267 /* TODO(mario.six@gdsys.cc): Using SPI_CPHA (for clock phase) and
268 * SPI_CPOL (for clock polarity) should work
273 static 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,
278 * cs_info is not needed, since we require all chip selects to be
279 * in the device tree explicitly
283 static const struct udevice_id mpc8xxx_spi_ids[] = {
284 { .compatible = "fsl,spi" },
285 { .compatible = "fsl,mpc832x-spi", .data = SOC_MPC832X },
289 U_BOOT_DRIVER(mpc8xxx_spi) = {
290 .name = "mpc8xxx_spi",
292 .of_match = mpc8xxx_spi_ids,
293 .ops = &mpc8xxx_spi_ops,
294 .of_to_plat = mpc8xxx_spi_of_to_plat,
295 .probe = mpc8xxx_spi_probe,
296 .priv_auto = sizeof(struct mpc8xxx_priv),