Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2006 Ben Warren, Qstreams Networks Inc. |
Stefan Roese | 88fbf93 | 2010-04-15 16:07:28 +0200 | [diff] [blame] | 4 | * With help from the common/soft_spi and arch/powerpc/cpu/mpc8260 drivers |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
Rasmus Villemoes | 1534031 | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 7 | #include <clk.h> |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 11 | #include <malloc.h> |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 12 | #include <spi.h> |
| 13 | #include <asm/mpc8xxx_spi.h> |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 14 | #include <asm-generic/gpio.h> |
Rasmus Villemoes | d4e78c3 | 2020-04-20 16:13:41 +0200 | [diff] [blame] | 15 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 16 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 17 | #include <linux/delay.h> |
Christophe Leroy | c58bd4a | 2023-03-02 16:26:26 +0100 | [diff] [blame] | 18 | #include <asm/arch/soc.h> |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 19 | |
Mario Six | 10f300a | 2019-04-29 01:58:41 +0530 | [diff] [blame] | 20 | enum { |
| 21 | SPI_EV_NE = BIT(31 - 22), /* Receiver Not Empty */ |
| 22 | SPI_EV_NF = BIT(31 - 23), /* Transmitter Not Full */ |
| 23 | }; |
| 24 | |
| 25 | enum { |
| 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 */ |
Christophe Leroy | c58bd4a | 2023-03-02 16:26:26 +0100 | [diff] [blame] | 33 | SPI_MODE_OP = BIT(31 - 17), /* CPU Mode, QE otherwise */ |
Mario Six | 10f300a | 2019-04-29 01:58:41 +0530 | [diff] [blame] | 34 | |
| 35 | SPI_MODE_LEN_MASK = 0xf00000, |
Rasmus Villemoes | 379e25d | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 36 | SPI_MODE_LEN_SHIFT = 20, |
Rasmus Villemoes | 1534031 | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 37 | SPI_MODE_PM_SHIFT = 16, |
Mario Six | 10f300a | 2019-04-29 01:58:41 +0530 | [diff] [blame] | 38 | SPI_MODE_PM_MASK = 0xf0000, |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 39 | |
Mario Six | 10f300a | 2019-04-29 01:58:41 +0530 | [diff] [blame] | 40 | SPI_COM_LST = BIT(31 - 9), |
| 41 | }; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 42 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 43 | struct mpc8xxx_priv { |
| 44 | spi8xxx_t *spi; |
| 45 | struct gpio_desc gpios[16]; |
Rasmus Villemoes | d31ec8b | 2020-02-11 15:20:24 +0000 | [diff] [blame] | 46 | int cs_count; |
Rasmus Villemoes | 1534031 | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 47 | ulong clk_rate; |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 48 | }; |
| 49 | |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 50 | #define SPI_TIMEOUT 1000 |
| 51 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 52 | static int mpc8xxx_spi_of_to_plat(struct udevice *dev) |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 53 | { |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 54 | struct mpc8xxx_priv *priv = dev_get_priv(dev); |
Rasmus Villemoes | 1534031 | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 55 | struct clk clk; |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 56 | int ret; |
| 57 | |
Johan Jonker | 8d5d8e0 | 2023-03-13 01:32:04 +0100 | [diff] [blame] | 58 | priv->spi = dev_read_addr_ptr(dev); |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 59 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 60 | ret = gpio_request_list_by_name(dev, "gpios", priv->gpios, |
| 61 | ARRAY_SIZE(priv->gpios), GPIOD_IS_OUT | GPIOD_ACTIVE_LOW); |
| 62 | if (ret < 0) |
| 63 | return -EINVAL; |
| 64 | |
Rasmus Villemoes | d31ec8b | 2020-02-11 15:20:24 +0000 | [diff] [blame] | 65 | priv->cs_count = ret; |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 66 | |
Rasmus Villemoes | 1534031 | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 67 | ret = clk_get_by_index(dev, 0, &clk); |
| 68 | if (ret) { |
| 69 | dev_err(dev, "%s: clock not defined\n", __func__); |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 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__); |
| 76 | return -EINVAL; |
| 77 | } |
| 78 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 79 | return 0; |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 82 | static int mpc8xxx_spi_probe(struct udevice *dev) |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 83 | { |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 84 | struct mpc8xxx_priv *priv = dev_get_priv(dev); |
Rasmus Villemoes | 379e25d | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 85 | spi8xxx_t *spi = priv->spi; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 86 | |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 87 | /* |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 88 | * SPI pins on the MPC83xx are not muxed, so all we do is initialize |
| 89 | * some registers |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 90 | */ |
Rasmus Villemoes | 379e25d | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 91 | out_be32(&priv->spi->mode, SPI_MODE_REV | SPI_MODE_MS); |
| 92 | |
Christophe Leroy | c58bd4a | 2023-03-02 16:26:26 +0100 | [diff] [blame] | 93 | if (dev_get_driver_data(dev) == SOC_MPC832X) |
| 94 | setbits_be32(&priv->spi->mode, SPI_MODE_OP); |
| 95 | |
Rasmus Villemoes | 379e25d | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 96 | /* set len to 8 bits */ |
| 97 | setbits_be32(&spi->mode, (8 - 1) << SPI_MODE_LEN_SHIFT); |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 98 | |
Rasmus Villemoes | 379e25d | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 99 | setbits_be32(&spi->mode, SPI_MODE_EN); |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 100 | |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 101 | /* Clear all SPI events */ |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 102 | setbits_be32(&priv->spi->event, 0xffffffff); |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 103 | /* Mask all SPI interrupts */ |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 104 | clrbits_be32(&priv->spi->mask, 0xffffffff); |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 105 | /* LST bit doesn't do anything, so disregard */ |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 106 | out_be32(&priv->spi->com, 0); |
| 107 | |
| 108 | return 0; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 111 | static void mpc8xxx_spi_cs_activate(struct udevice *dev) |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 112 | { |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 113 | struct mpc8xxx_priv *priv = dev_get_priv(dev->parent); |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 114 | struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev); |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 115 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 116 | dm_gpio_set_value(&priv->gpios[plat->cs], 1); |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 119 | static void mpc8xxx_spi_cs_deactivate(struct udevice *dev) |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 120 | { |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 121 | struct mpc8xxx_priv *priv = dev_get_priv(dev->parent); |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 122 | struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev); |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 123 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 124 | dm_gpio_set_value(&priv->gpios[plat->cs], 0); |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 127 | static int mpc8xxx_spi_xfer(struct udevice *dev, uint bitlen, |
| 128 | const void *dout, void *din, ulong flags) |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 129 | { |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 130 | struct udevice *bus = dev->parent; |
| 131 | struct mpc8xxx_priv *priv = dev_get_priv(bus); |
| 132 | spi8xxx_t *spi = priv->spi; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 133 | struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev); |
Rasmus Villemoes | 379e25d | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 134 | u32 tmpdin = 0, tmpdout = 0, n; |
| 135 | const u8 *cout = dout; |
| 136 | u8 *cin = din; |
Christophe Leroy | c58bd4a | 2023-03-02 16:26:26 +0100 | [diff] [blame] | 137 | ulong type = dev_get_driver_data(bus); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 138 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 139 | debug("%s: slave %s:%u dout %08X din %08X bitlen %u\n", __func__, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 140 | bus->name, plat->cs, (uint)dout, (uint)din, bitlen); |
| 141 | if (plat->cs >= priv->cs_count) { |
Rasmus Villemoes | d31ec8b | 2020-02-11 15:20:24 +0000 | [diff] [blame] | 142 | dev_err(dev, "chip select index %d too large (cs_count=%d)\n", |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 143 | plat->cs, priv->cs_count); |
Rasmus Villemoes | d31ec8b | 2020-02-11 15:20:24 +0000 | [diff] [blame] | 144 | return -EINVAL; |
| 145 | } |
Rasmus Villemoes | 379e25d | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 146 | if (bitlen % 8) { |
| 147 | printf("*** spi_xfer: bitlen must be multiple of 8\n"); |
| 148 | return -ENOTSUPP; |
| 149 | } |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 150 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 151 | if (flags & SPI_XFER_BEGIN) |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 152 | mpc8xxx_spi_cs_activate(dev); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 153 | |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 154 | /* Clear all SPI events */ |
Mario Six | dee9949 | 2019-04-29 01:58:42 +0530 | [diff] [blame] | 155 | setbits_be32(&spi->event, 0xffffffff); |
Rasmus Villemoes | 379e25d | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 156 | n = bitlen / 8; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 157 | |
Rasmus Villemoes | 379e25d | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 158 | /* Handle data in 8-bit chunks */ |
| 159 | while (n--) { |
Mario Six | 9083b13 | 2019-04-29 01:58:52 +0530 | [diff] [blame] | 160 | ulong start; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 161 | |
Rasmus Villemoes | 379e25d | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 162 | if (cout) |
| 163 | tmpdout = *cout++; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 164 | |
Christophe Leroy | c58bd4a | 2023-03-02 16:26:26 +0100 | [diff] [blame] | 165 | if (type == SOC_MPC832X) |
| 166 | tmpdout <<= 24; |
| 167 | |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 168 | /* Write the data out */ |
Mario Six | dee9949 | 2019-04-29 01:58:42 +0530 | [diff] [blame] | 169 | out_be32(&spi->tx, tmpdout); |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 170 | |
Mario Six | f9d5ca2 | 2019-04-29 01:58:40 +0530 | [diff] [blame] | 171 | debug("*** %s: ... %08x written\n", __func__, tmpdout); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 172 | |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 173 | /* |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 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 |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 177 | */ |
Mario Six | 9083b13 | 2019-04-29 01:58:52 +0530 | [diff] [blame] | 178 | start = get_timer(0); |
| 179 | do { |
Mario Six | 8d684ec | 2019-04-29 01:58:46 +0530 | [diff] [blame] | 180 | u32 event = in_be32(&spi->event); |
Mario Six | 4b671e1 | 2019-04-29 01:58:44 +0530 | [diff] [blame] | 181 | bool have_ne = event & SPI_EV_NE; |
| 182 | bool have_nf = event & SPI_EV_NF; |
| 183 | |
Mario Six | 2afedfe | 2019-04-29 01:58:45 +0530 | [diff] [blame] | 184 | if (!have_ne) |
| 185 | continue; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 186 | |
Mario Six | 2afedfe | 2019-04-29 01:58:45 +0530 | [diff] [blame] | 187 | tmpdin = in_be32(&spi->rx); |
| 188 | setbits_be32(&spi->event, SPI_EV_NE); |
| 189 | |
Christophe Leroy | c58bd4a | 2023-03-02 16:26:26 +0100 | [diff] [blame] | 190 | if (type == SOC_MPC832X) |
| 191 | tmpdin >>= 16; |
| 192 | |
Rasmus Villemoes | 379e25d | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 193 | if (cin) |
| 194 | *cin++ = tmpdin; |
Mario Six | 2afedfe | 2019-04-29 01:58:45 +0530 | [diff] [blame] | 195 | |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 196 | /* |
| 197 | * Only bail when we've had both NE and NF events. |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 198 | * This will cause timeouts on RO devices, so maybe |
| 199 | * in the future put an arbitrary delay after writing |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 200 | * the device. Arbitrary delays suck, though... |
| 201 | */ |
Mario Six | 2afedfe | 2019-04-29 01:58:45 +0530 | [diff] [blame] | 202 | if (have_nf) |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 203 | break; |
Mario Six | 9083b13 | 2019-04-29 01:58:52 +0530 | [diff] [blame] | 204 | |
| 205 | mdelay(1); |
| 206 | } while (get_timer(start) < SPI_TIMEOUT); |
Mario Six | 2afedfe | 2019-04-29 01:58:45 +0530 | [diff] [blame] | 207 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 208 | if (get_timer(start) >= SPI_TIMEOUT) { |
Mario Six | f9d5ca2 | 2019-04-29 01:58:40 +0530 | [diff] [blame] | 209 | debug("*** %s: Time out during SPI transfer\n", |
| 210 | __func__); |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 211 | return -ETIMEDOUT; |
| 212 | } |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 213 | |
Mario Six | f9d5ca2 | 2019-04-29 01:58:40 +0530 | [diff] [blame] | 214 | debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 215 | } |
| 216 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 217 | if (flags & SPI_XFER_END) |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 218 | mpc8xxx_spi_cs_deactivate(dev); |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static int mpc8xxx_spi_set_speed(struct udevice *dev, uint speed) |
| 224 | { |
Rasmus Villemoes | 1534031 | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 225 | struct mpc8xxx_priv *priv = dev_get_priv(dev); |
| 226 | spi8xxx_t *spi = priv->spi; |
| 227 | u32 bits, mask, div16, pm; |
| 228 | u32 mode; |
| 229 | ulong clk; |
| 230 | |
| 231 | clk = priv->clk_rate; |
| 232 | if (clk / 64 > speed) { |
| 233 | div16 = SPI_MODE_DIV16; |
| 234 | clk /= 16; |
| 235 | } else { |
| 236 | div16 = 0; |
| 237 | } |
| 238 | pm = (clk - 1)/(4*speed) + 1; |
| 239 | if (pm > 16) { |
| 240 | dev_err(dev, "requested speed %u too small\n", speed); |
| 241 | return -EINVAL; |
| 242 | } |
| 243 | pm--; |
| 244 | |
| 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); |
| 252 | mode |= bits; |
| 253 | out_be32(&spi->mode, mode); |
| 254 | mode |= SPI_MODE_EN; |
| 255 | out_be32(&spi->mode, mode); |
| 256 | } |
| 257 | |
| 258 | debug("requested speed %u, set speed to %lu/(%s4*%u) == %lu\n", |
| 259 | speed, priv->clk_rate, div16 ? "16*" : "", pm + 1, |
| 260 | clk/(4*(pm + 1))); |
| 261 | |
Rasmus Villemoes | 379e25d | 2020-02-11 15:20:25 +0000 | [diff] [blame] | 262 | return 0; |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 263 | } |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 264 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 265 | static int mpc8xxx_spi_set_mode(struct udevice *dev, uint mode) |
| 266 | { |
| 267 | /* TODO(mario.six@gdsys.cc): Using SPI_CPHA (for clock phase) and |
| 268 | * SPI_CPOL (for clock polarity) should work |
| 269 | */ |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 270 | return 0; |
| 271 | } |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 272 | |
| 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, |
| 277 | /* |
| 278 | * cs_info is not needed, since we require all chip selects to be |
| 279 | * in the device tree explicitly |
| 280 | */ |
| 281 | }; |
| 282 | |
| 283 | static const struct udevice_id mpc8xxx_spi_ids[] = { |
| 284 | { .compatible = "fsl,spi" }, |
Christophe Leroy | c58bd4a | 2023-03-02 16:26:26 +0100 | [diff] [blame] | 285 | { .compatible = "fsl,mpc832x-spi", .data = SOC_MPC832X }, |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 286 | { } |
| 287 | }; |
| 288 | |
| 289 | U_BOOT_DRIVER(mpc8xxx_spi) = { |
| 290 | .name = "mpc8xxx_spi", |
| 291 | .id = UCLASS_SPI, |
| 292 | .of_match = mpc8xxx_spi_ids, |
| 293 | .ops = &mpc8xxx_spi_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 294 | .of_to_plat = mpc8xxx_spi_of_to_plat, |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 295 | .probe = mpc8xxx_spi_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 296 | .priv_auto = sizeof(struct mpc8xxx_priv), |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 297 | }; |