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 | |
| 7 | #include <common.h> |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 10 | #include <malloc.h> |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 11 | #include <spi.h> |
| 12 | #include <asm/mpc8xxx_spi.h> |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 13 | #include <asm-generic/gpio.h> |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 14 | |
Mario Six | 10f300a | 2019-04-29 01:58:41 +0530 | [diff] [blame] | 15 | enum { |
| 16 | SPI_EV_NE = BIT(31 - 22), /* Receiver Not Empty */ |
| 17 | SPI_EV_NF = BIT(31 - 23), /* Transmitter Not Full */ |
| 18 | }; |
| 19 | |
| 20 | enum { |
| 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, |
| 30 | SPI_MODE_PM_MASK = 0xf0000, |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 31 | |
Mario Six | 10f300a | 2019-04-29 01:58:41 +0530 | [diff] [blame] | 32 | SPI_COM_LST = BIT(31 - 9), |
| 33 | }; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 34 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 35 | struct mpc8xxx_priv { |
| 36 | spi8xxx_t *spi; |
| 37 | struct gpio_desc gpios[16]; |
| 38 | int max_cs; |
| 39 | }; |
| 40 | |
Mario Six | eb2533a | 2019-04-29 01:58:47 +0530 | [diff] [blame] | 41 | static inline u32 to_prescale_mod(u32 val) |
| 42 | { |
| 43 | return (min(val, (u32)15) << 16); |
| 44 | } |
| 45 | |
| 46 | static void set_char_len(spi8xxx_t *spi, u32 val) |
| 47 | { |
| 48 | clrsetbits_be32(&spi->mode, SPI_MODE_LEN_MASK, (val << 20)); |
| 49 | } |
| 50 | |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 51 | #define SPI_TIMEOUT 1000 |
| 52 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 53 | static int __spi_set_speed(spi8xxx_t *spi, uint speed) |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 54 | { |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 55 | /* TODO(mario.six@gdsys.cc): This only ever sets one fixed speed */ |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 56 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 57 | /* Use SYSCLK / 8 (16.67MHz typ.) */ |
| 58 | clrsetbits_be32(&spi->mode, SPI_MODE_PM_MASK, to_prescale_mod(1)); |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 59 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 60 | return 0; |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 63 | static int mpc8xxx_spi_ofdata_to_platdata(struct udevice *dev) |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 64 | { |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 65 | struct mpc8xxx_priv *priv = dev_get_priv(dev); |
| 66 | int ret; |
| 67 | |
| 68 | priv->spi = (spi8xxx_t *)dev_read_addr(dev); |
| 69 | |
| 70 | /* TODO(mario.six@gdsys.cc): Read clock and save the value */ |
| 71 | |
| 72 | ret = gpio_request_list_by_name(dev, "gpios", priv->gpios, |
| 73 | ARRAY_SIZE(priv->gpios), GPIOD_IS_OUT | GPIOD_ACTIVE_LOW); |
| 74 | if (ret < 0) |
| 75 | return -EINVAL; |
| 76 | |
| 77 | priv->max_cs = ret; |
| 78 | |
| 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); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 85 | |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 86 | /* |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 87 | * SPI pins on the MPC83xx are not muxed, so all we do is initialize |
| 88 | * some registers |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 89 | */ |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 90 | out_be32(&priv->spi->mode, SPI_MODE_REV | SPI_MODE_MS | SPI_MODE_EN); |
| 91 | |
| 92 | __spi_set_speed(priv->spi, 16666667); |
| 93 | |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 94 | /* Clear all SPI events */ |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 95 | setbits_be32(&priv->spi->event, 0xffffffff); |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 96 | /* Mask all SPI interrupts */ |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 97 | clrbits_be32(&priv->spi->mask, 0xffffffff); |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 98 | /* LST bit doesn't do anything, so disregard */ |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 99 | out_be32(&priv->spi->com, 0); |
| 100 | |
| 101 | return 0; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 102 | } |
| 103 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 104 | static void mpc8xxx_spi_cs_activate(struct udevice *dev) |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 105 | { |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 106 | struct mpc8xxx_priv *priv = dev_get_priv(dev->parent); |
| 107 | struct dm_spi_slave_platdata *platdata = dev_get_parent_platdata(dev); |
| 108 | |
| 109 | dm_gpio_set_dir_flags(&priv->gpios[platdata->cs], GPIOD_IS_OUT); |
| 110 | dm_gpio_set_value(&priv->gpios[platdata->cs], 0); |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 113 | static void mpc8xxx_spi_cs_deactivate(struct udevice *dev) |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 114 | { |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 115 | struct mpc8xxx_priv *priv = dev_get_priv(dev->parent); |
| 116 | struct dm_spi_slave_platdata *platdata = dev_get_parent_platdata(dev); |
| 117 | |
| 118 | dm_gpio_set_dir_flags(&priv->gpios[platdata->cs], GPIOD_IS_OUT); |
| 119 | dm_gpio_set_value(&priv->gpios[platdata->cs], 1); |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 122 | static int mpc8xxx_spi_xfer(struct udevice *dev, uint bitlen, |
| 123 | const void *dout, void *din, ulong flags) |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 124 | { |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 125 | struct udevice *bus = dev->parent; |
| 126 | struct mpc8xxx_priv *priv = dev_get_priv(bus); |
| 127 | spi8xxx_t *spi = priv->spi; |
| 128 | struct dm_spi_slave_platdata *platdata = dev_get_parent_platdata(dev); |
| 129 | u32 tmpdin = 0; |
Mario Six | 56edb8b | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 130 | int num_blks = DIV_ROUND_UP(bitlen, 32); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 131 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 132 | debug("%s: slave %s:%u dout %08X din %08X bitlen %u\n", __func__, |
| 133 | bus->name, platdata->cs, *(uint *)dout, *(uint *)din, bitlen); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 134 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 135 | if (flags & SPI_XFER_BEGIN) |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 136 | mpc8xxx_spi_cs_activate(dev); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 137 | |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 138 | /* Clear all SPI events */ |
Mario Six | dee9949 | 2019-04-29 01:58:42 +0530 | [diff] [blame] | 139 | setbits_be32(&spi->event, 0xffffffff); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 140 | |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 141 | /* Handle data in 32-bit chunks */ |
Mario Six | 56edb8b | 2019-04-29 01:58:38 +0530 | [diff] [blame] | 142 | while (num_blks--) { |
Mario Six | 8d684ec | 2019-04-29 01:58:46 +0530 | [diff] [blame] | 143 | u32 tmpdout = 0; |
Mario Six | a67510f | 2019-04-29 01:58:48 +0530 | [diff] [blame] | 144 | uchar xfer_bitlen = (bitlen >= 32 ? 32 : bitlen); |
Mario Six | 9083b13 | 2019-04-29 01:58:52 +0530 | [diff] [blame] | 145 | ulong start; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 146 | |
Mario Six | dee9949 | 2019-04-29 01:58:42 +0530 | [diff] [blame] | 147 | clrbits_be32(&spi->mode, SPI_MODE_EN); |
Ira W. Snyder | 5261b00 | 2012-09-12 14:17:31 -0700 | [diff] [blame] | 148 | |
Mario Six | 64b07ff | 2019-04-29 01:58:49 +0530 | [diff] [blame] | 149 | /* Set up length for this transfer */ |
| 150 | |
| 151 | if (bitlen <= 4) /* 4 bits or less */ |
Mario Six | eb2533a | 2019-04-29 01:58:47 +0530 | [diff] [blame] | 152 | set_char_len(spi, 3); |
Mario Six | 64b07ff | 2019-04-29 01:58:49 +0530 | [diff] [blame] | 153 | else if (bitlen <= 16) /* at most 16 bits */ |
Mario Six | eb2533a | 2019-04-29 01:58:47 +0530 | [diff] [blame] | 154 | set_char_len(spi, bitlen - 1); |
Mario Six | 64b07ff | 2019-04-29 01:58:49 +0530 | [diff] [blame] | 155 | else /* more than 16 bits -> full 32 bit transfer */ |
Mario Six | eb2533a | 2019-04-29 01:58:47 +0530 | [diff] [blame] | 156 | set_char_len(spi, 0); |
| 157 | |
Mario Six | 4bc4cba | 2019-04-29 01:58:50 +0530 | [diff] [blame] | 158 | setbits_be32(&spi->mode, SPI_MODE_EN); |
| 159 | |
| 160 | /* Shift data so it's msb-justified */ |
| 161 | tmpdout = *(u32 *)dout >> (32 - xfer_bitlen); |
| 162 | |
Mario Six | c368522 | 2019-04-29 01:58:51 +0530 | [diff] [blame] | 163 | if (bitlen > 32) { |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 164 | /* Set up the next iteration if sending > 32 bits */ |
| 165 | bitlen -= 32; |
| 166 | dout += 4; |
| 167 | } |
| 168 | |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 169 | /* Write the data out */ |
Mario Six | dee9949 | 2019-04-29 01:58:42 +0530 | [diff] [blame] | 170 | out_be32(&spi->tx, tmpdout); |
Mario Six | 4d3082b | 2019-04-29 01:58:37 +0530 | [diff] [blame] | 171 | |
Mario Six | f9d5ca2 | 2019-04-29 01:58:40 +0530 | [diff] [blame] | 172 | debug("*** %s: ... %08x written\n", __func__, tmpdout); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 173 | |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 174 | /* |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 175 | * 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 Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 178 | */ |
Mario Six | 9083b13 | 2019-04-29 01:58:52 +0530 | [diff] [blame] | 179 | start = get_timer(0); |
| 180 | do { |
Mario Six | 8d684ec | 2019-04-29 01:58:46 +0530 | [diff] [blame] | 181 | u32 event = in_be32(&spi->event); |
Mario Six | 4b671e1 | 2019-04-29 01:58:44 +0530 | [diff] [blame] | 182 | bool have_ne = event & SPI_EV_NE; |
| 183 | bool have_nf = event & SPI_EV_NF; |
| 184 | |
Mario Six | 2afedfe | 2019-04-29 01:58:45 +0530 | [diff] [blame] | 185 | if (!have_ne) |
| 186 | continue; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 187 | |
Mario Six | 2afedfe | 2019-04-29 01:58:45 +0530 | [diff] [blame] | 188 | tmpdin = in_be32(&spi->rx); |
| 189 | setbits_be32(&spi->event, SPI_EV_NE); |
| 190 | |
Mario Six | a67510f | 2019-04-29 01:58:48 +0530 | [diff] [blame] | 191 | *(u32 *)din = (tmpdin << (32 - xfer_bitlen)); |
| 192 | if (xfer_bitlen == 32) { |
Mario Six | 2afedfe | 2019-04-29 01:58:45 +0530 | [diff] [blame] | 193 | /* Advance output buffer by 32 bits */ |
| 194 | din += 4; |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 195 | } |
Mario Six | 2afedfe | 2019-04-29 01:58:45 +0530 | [diff] [blame] | 196 | |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 197 | /* |
| 198 | * Only bail when we've had both NE and NF events. |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 199 | * This will cause timeouts on RO devices, so maybe |
| 200 | * in the future put an arbitrary delay after writing |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 201 | * the device. Arbitrary delays suck, though... |
| 202 | */ |
Mario Six | 2afedfe | 2019-04-29 01:58:45 +0530 | [diff] [blame] | 203 | if (have_nf) |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 204 | break; |
Mario Six | 9083b13 | 2019-04-29 01:58:52 +0530 | [diff] [blame] | 205 | |
| 206 | mdelay(1); |
| 207 | } while (get_timer(start) < SPI_TIMEOUT); |
Mario Six | 2afedfe | 2019-04-29 01:58:45 +0530 | [diff] [blame] | 208 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 209 | if (get_timer(start) >= SPI_TIMEOUT) { |
Mario Six | f9d5ca2 | 2019-04-29 01:58:40 +0530 | [diff] [blame] | 210 | debug("*** %s: Time out during SPI transfer\n", |
| 211 | __func__); |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 212 | return -ETIMEDOUT; |
| 213 | } |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 214 | |
Mario Six | f9d5ca2 | 2019-04-29 01:58:40 +0530 | [diff] [blame] | 215 | debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin); |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 216 | } |
| 217 | |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 218 | if (flags & SPI_XFER_END) |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 219 | mpc8xxx_spi_cs_deactivate(dev); |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static int mpc8xxx_spi_set_speed(struct udevice *dev, uint speed) |
| 225 | { |
| 226 | struct mpc8xxx_priv *priv = dev_get_priv(dev); |
| 227 | |
| 228 | return __spi_set_speed(priv->spi, speed); |
| 229 | } |
Kim Phillips | b8e2520 | 2008-01-17 12:48:00 -0600 | [diff] [blame] | 230 | |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 231 | static int mpc8xxx_spi_set_mode(struct udevice *dev, uint mode) |
| 232 | { |
| 233 | /* TODO(mario.six@gdsys.cc): Using SPI_CPHA (for clock phase) and |
| 234 | * SPI_CPOL (for clock polarity) should work |
| 235 | */ |
Ben Warren | 7efe927 | 2008-01-16 22:37:35 -0500 | [diff] [blame] | 236 | return 0; |
| 237 | } |
Jagan Teki | 52515d5 | 2019-04-29 01:58:53 +0530 | [diff] [blame] | 238 | |
| 239 | static const struct dm_spi_ops mpc8xxx_spi_ops = { |
| 240 | .xfer = mpc8xxx_spi_xfer, |
| 241 | .set_speed = mpc8xxx_spi_set_speed, |
| 242 | .set_mode = mpc8xxx_spi_set_mode, |
| 243 | /* |
| 244 | * cs_info is not needed, since we require all chip selects to be |
| 245 | * in the device tree explicitly |
| 246 | */ |
| 247 | }; |
| 248 | |
| 249 | static const struct udevice_id mpc8xxx_spi_ids[] = { |
| 250 | { .compatible = "fsl,spi" }, |
| 251 | { } |
| 252 | }; |
| 253 | |
| 254 | U_BOOT_DRIVER(mpc8xxx_spi) = { |
| 255 | .name = "mpc8xxx_spi", |
| 256 | .id = UCLASS_SPI, |
| 257 | .of_match = mpc8xxx_spi_ids, |
| 258 | .ops = &mpc8xxx_spi_ops, |
| 259 | .ofdata_to_platdata = mpc8xxx_spi_ofdata_to_platdata, |
| 260 | .probe = mpc8xxx_spi_probe, |
| 261 | .priv_auto_alloc_size = sizeof(struct mpc8xxx_priv), |
| 262 | }; |