blob: 1c7bf10f91e37ccd83f314c0b9b73b230c6fef7d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ben Warren7efe9272008-01-16 22:37:35 -05002/*
3 * Copyright (c) 2006 Ben Warren, Qstreams Networks Inc.
Stefan Roese88fbf932010-04-15 16:07:28 +02004 * With help from the common/soft_spi and arch/powerpc/cpu/mpc8260 drivers
Ben Warren7efe9272008-01-16 22:37:35 -05005 */
6
7#include <common.h>
Jagan Teki52515d52019-04-29 01:58:53 +05308#include <dm.h>
9#include <errno.h>
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020010#include <malloc.h>
Ben Warren7efe9272008-01-16 22:37:35 -050011#include <spi.h>
12#include <asm/mpc8xxx_spi.h>
Jagan Teki52515d52019-04-29 01:58:53 +053013#include <asm-generic/gpio.h>
Ben Warren7efe9272008-01-16 22:37:35 -050014
Mario Six10f300a2019-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};
19
20enum {
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 Warren7efe9272008-01-16 22:37:35 -050031
Mario Six10f300a2019-04-29 01:58:41 +053032 SPI_COM_LST = BIT(31 - 9),
33};
Ben Warren7efe9272008-01-16 22:37:35 -050034
Jagan Teki52515d52019-04-29 01:58:53 +053035struct mpc8xxx_priv {
36 spi8xxx_t *spi;
37 struct gpio_desc gpios[16];
38 int max_cs;
39};
40
Mario Sixeb2533a2019-04-29 01:58:47 +053041static inline u32 to_prescale_mod(u32 val)
42{
43 return (min(val, (u32)15) << 16);
44}
45
46static void set_char_len(spi8xxx_t *spi, u32 val)
47{
48 clrsetbits_be32(&spi->mode, SPI_MODE_LEN_MASK, (val << 20));
49}
50
Ben Warren7efe9272008-01-16 22:37:35 -050051#define SPI_TIMEOUT 1000
52
Jagan Teki52515d52019-04-29 01:58:53 +053053static int __spi_set_speed(spi8xxx_t *spi, uint speed)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020054{
Jagan Teki52515d52019-04-29 01:58:53 +053055 /* TODO(mario.six@gdsys.cc): This only ever sets one fixed speed */
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020056
Jagan Teki52515d52019-04-29 01:58:53 +053057 /* Use SYSCLK / 8 (16.67MHz typ.) */
58 clrsetbits_be32(&spi->mode, SPI_MODE_PM_MASK, to_prescale_mod(1));
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020059
Jagan Teki52515d52019-04-29 01:58:53 +053060 return 0;
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020061}
62
Jagan Teki52515d52019-04-29 01:58:53 +053063static int mpc8xxx_spi_ofdata_to_platdata(struct udevice *dev)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020064{
Jagan Teki52515d52019-04-29 01:58:53 +053065 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 Skinnemoend74084a2008-05-16 11:10:31 +020080}
81
Jagan Teki52515d52019-04-29 01:58:53 +053082static int mpc8xxx_spi_probe(struct udevice *dev)
Ben Warren7efe9272008-01-16 22:37:35 -050083{
Jagan Teki52515d52019-04-29 01:58:53 +053084 struct mpc8xxx_priv *priv = dev_get_priv(dev);
Ben Warren7efe9272008-01-16 22:37:35 -050085
Kim Phillipsb8e25202008-01-17 12:48:00 -060086 /*
Ben Warren7efe9272008-01-16 22:37:35 -050087 * SPI pins on the MPC83xx are not muxed, so all we do is initialize
88 * some registers
Kim Phillipsb8e25202008-01-17 12:48:00 -060089 */
Jagan Teki52515d52019-04-29 01:58:53 +053090 out_be32(&priv->spi->mode, SPI_MODE_REV | SPI_MODE_MS | SPI_MODE_EN);
91
92 __spi_set_speed(priv->spi, 16666667);
93
Mario Six4d3082b2019-04-29 01:58:37 +053094 /* Clear all SPI events */
Jagan Teki52515d52019-04-29 01:58:53 +053095 setbits_be32(&priv->spi->event, 0xffffffff);
Mario Six4d3082b2019-04-29 01:58:37 +053096 /* Mask all SPI interrupts */
Jagan Teki52515d52019-04-29 01:58:53 +053097 clrbits_be32(&priv->spi->mask, 0xffffffff);
Mario Six4d3082b2019-04-29 01:58:37 +053098 /* LST bit doesn't do anything, so disregard */
Jagan Teki52515d52019-04-29 01:58:53 +053099 out_be32(&priv->spi->com, 0);
100
101 return 0;
Ben Warren7efe9272008-01-16 22:37:35 -0500102}
103
Jagan Teki52515d52019-04-29 01:58:53 +0530104static void mpc8xxx_spi_cs_activate(struct udevice *dev)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200105{
Jagan Teki52515d52019-04-29 01:58:53 +0530106 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 Skinnemoend74084a2008-05-16 11:10:31 +0200111}
112
Jagan Teki52515d52019-04-29 01:58:53 +0530113static void mpc8xxx_spi_cs_deactivate(struct udevice *dev)
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200114{
Jagan Teki52515d52019-04-29 01:58:53 +0530115 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 Skinnemoend74084a2008-05-16 11:10:31 +0200120}
121
Jagan Teki52515d52019-04-29 01:58:53 +0530122static int mpc8xxx_spi_xfer(struct udevice *dev, uint bitlen,
123 const void *dout, void *din, ulong flags)
Ben Warren7efe9272008-01-16 22:37:35 -0500124{
Jagan Teki52515d52019-04-29 01:58:53 +0530125 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 Six56edb8b2019-04-29 01:58:38 +0530130 int num_blks = DIV_ROUND_UP(bitlen, 32);
Ben Warren7efe9272008-01-16 22:37:35 -0500131
Jagan Teki52515d52019-04-29 01:58:53 +0530132 debug("%s: slave %s:%u dout %08X din %08X bitlen %u\n", __func__,
133 bus->name, platdata->cs, *(uint *)dout, *(uint *)din, bitlen);
Ben Warren7efe9272008-01-16 22:37:35 -0500134
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200135 if (flags & SPI_XFER_BEGIN)
Jagan Teki52515d52019-04-29 01:58:53 +0530136 mpc8xxx_spi_cs_activate(dev);
Ben Warren7efe9272008-01-16 22:37:35 -0500137
Mario Six4d3082b2019-04-29 01:58:37 +0530138 /* Clear all SPI events */
Mario Sixdee99492019-04-29 01:58:42 +0530139 setbits_be32(&spi->event, 0xffffffff);
Ben Warren7efe9272008-01-16 22:37:35 -0500140
Mario Six4d3082b2019-04-29 01:58:37 +0530141 /* Handle data in 32-bit chunks */
Mario Six56edb8b2019-04-29 01:58:38 +0530142 while (num_blks--) {
Mario Six8d684ec2019-04-29 01:58:46 +0530143 u32 tmpdout = 0;
Mario Sixa67510f2019-04-29 01:58:48 +0530144 uchar xfer_bitlen = (bitlen >= 32 ? 32 : bitlen);
Mario Six9083b132019-04-29 01:58:52 +0530145 ulong start;
Ben Warren7efe9272008-01-16 22:37:35 -0500146
Mario Sixdee99492019-04-29 01:58:42 +0530147 clrbits_be32(&spi->mode, SPI_MODE_EN);
Ira W. Snyder5261b002012-09-12 14:17:31 -0700148
Mario Six64b07ff2019-04-29 01:58:49 +0530149 /* Set up length for this transfer */
150
151 if (bitlen <= 4) /* 4 bits or less */
Mario Sixeb2533a2019-04-29 01:58:47 +0530152 set_char_len(spi, 3);
Mario Six64b07ff2019-04-29 01:58:49 +0530153 else if (bitlen <= 16) /* at most 16 bits */
Mario Sixeb2533a2019-04-29 01:58:47 +0530154 set_char_len(spi, bitlen - 1);
Mario Six64b07ff2019-04-29 01:58:49 +0530155 else /* more than 16 bits -> full 32 bit transfer */
Mario Sixeb2533a2019-04-29 01:58:47 +0530156 set_char_len(spi, 0);
157
Mario Six4bc4cba2019-04-29 01:58:50 +0530158 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 Sixc3685222019-04-29 01:58:51 +0530163 if (bitlen > 32) {
Ben Warren7efe9272008-01-16 22:37:35 -0500164 /* Set up the next iteration if sending > 32 bits */
165 bitlen -= 32;
166 dout += 4;
167 }
168
Mario Six4d3082b2019-04-29 01:58:37 +0530169 /* Write the data out */
Mario Sixdee99492019-04-29 01:58:42 +0530170 out_be32(&spi->tx, tmpdout);
Mario Six4d3082b2019-04-29 01:58:37 +0530171
Mario Sixf9d5ca22019-04-29 01:58:40 +0530172 debug("*** %s: ... %08x written\n", __func__, tmpdout);
Ben Warren7efe9272008-01-16 22:37:35 -0500173
Kim Phillipsb8e25202008-01-17 12:48:00 -0600174 /*
Ben Warren7efe9272008-01-16 22:37:35 -0500175 * 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 Phillipsb8e25202008-01-17 12:48:00 -0600178 */
Mario Six9083b132019-04-29 01:58:52 +0530179 start = get_timer(0);
180 do {
Mario Six8d684ec2019-04-29 01:58:46 +0530181 u32 event = in_be32(&spi->event);
Mario Six4b671e12019-04-29 01:58:44 +0530182 bool have_ne = event & SPI_EV_NE;
183 bool have_nf = event & SPI_EV_NF;
184
Mario Six2afedfe2019-04-29 01:58:45 +0530185 if (!have_ne)
186 continue;
Ben Warren7efe9272008-01-16 22:37:35 -0500187
Mario Six2afedfe2019-04-29 01:58:45 +0530188 tmpdin = in_be32(&spi->rx);
189 setbits_be32(&spi->event, SPI_EV_NE);
190
Mario Sixa67510f2019-04-29 01:58:48 +0530191 *(u32 *)din = (tmpdin << (32 - xfer_bitlen));
192 if (xfer_bitlen == 32) {
Mario Six2afedfe2019-04-29 01:58:45 +0530193 /* Advance output buffer by 32 bits */
194 din += 4;
Ben Warren7efe9272008-01-16 22:37:35 -0500195 }
Mario Six2afedfe2019-04-29 01:58:45 +0530196
Kim Phillipsb8e25202008-01-17 12:48:00 -0600197 /*
198 * Only bail when we've had both NE and NF events.
Ben Warren7efe9272008-01-16 22:37:35 -0500199 * This will cause timeouts on RO devices, so maybe
200 * in the future put an arbitrary delay after writing
Kim Phillipsb8e25202008-01-17 12:48:00 -0600201 * the device. Arbitrary delays suck, though...
202 */
Mario Six2afedfe2019-04-29 01:58:45 +0530203 if (have_nf)
Ben Warren7efe9272008-01-16 22:37:35 -0500204 break;
Mario Six9083b132019-04-29 01:58:52 +0530205
206 mdelay(1);
207 } while (get_timer(start) < SPI_TIMEOUT);
Mario Six2afedfe2019-04-29 01:58:45 +0530208
Jagan Teki52515d52019-04-29 01:58:53 +0530209 if (get_timer(start) >= SPI_TIMEOUT) {
Mario Sixf9d5ca22019-04-29 01:58:40 +0530210 debug("*** %s: Time out during SPI transfer\n",
211 __func__);
Jagan Teki52515d52019-04-29 01:58:53 +0530212 return -ETIMEDOUT;
213 }
Ben Warren7efe9272008-01-16 22:37:35 -0500214
Mario Sixf9d5ca22019-04-29 01:58:40 +0530215 debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin);
Ben Warren7efe9272008-01-16 22:37:35 -0500216 }
217
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200218 if (flags & SPI_XFER_END)
Jagan Teki52515d52019-04-29 01:58:53 +0530219 mpc8xxx_spi_cs_deactivate(dev);
220
221 return 0;
222}
223
224static 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 Phillipsb8e25202008-01-17 12:48:00 -0600230
Jagan Teki52515d52019-04-29 01:58:53 +0530231static 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 Warren7efe9272008-01-16 22:37:35 -0500236 return 0;
237}
Jagan Teki52515d52019-04-29 01:58:53 +0530238
239static 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
249static const struct udevice_id mpc8xxx_spi_ids[] = {
250 { .compatible = "fsl,spi" },
251 { }
252};
253
254U_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};