blob: 1c7bf10f91e37ccd83f314c0b9b73b230c6fef7d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ben Warren04a9e112008-01-16 22:37:35 -05002/*
3 * Copyright (c) 2006 Ben Warren, Qstreams Networks Inc.
Stefan Roesea47a12b2010-04-15 16:07:28 +02004 * With help from the common/soft_spi and arch/powerpc/cpu/mpc8260 drivers
Ben Warren04a9e112008-01-16 22:37:35 -05005 */
6
7#include <common.h>
Jagan Tekic1a3f1e2019-04-29 01:58:53 +05308#include <dm.h>
9#include <errno.h>
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020010#include <malloc.h>
Ben Warren04a9e112008-01-16 22:37:35 -050011#include <spi.h>
12#include <asm/mpc8xxx_spi.h>
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053013#include <asm-generic/gpio.h>
Ben Warren04a9e112008-01-16 22:37:35 -050014
Mario Six6ea93952019-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};
Ben Warren04a9e112008-01-16 22:37:35 -050019
Mario Six6ea93952019-04-29 01:58:41 +053020enum {
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,
31
32 SPI_COM_LST = BIT(31 - 9),
33};
Ben Warren04a9e112008-01-16 22:37:35 -050034
Jagan Tekic1a3f1e2019-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 Six8dea61d2019-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 Warren04a9e112008-01-16 22:37:35 -050051#define SPI_TIMEOUT 1000
52
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053053static int __spi_set_speed(spi8xxx_t *spi, uint speed)
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020054{
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053055 /* TODO(mario.six@gdsys.cc): This only ever sets one fixed speed */
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020056
Jagan Tekic1a3f1e2019-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 Skinnemoend255bb02008-05-16 11:10:31 +020059
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053060 return 0;
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020061}
62
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053063static int mpc8xxx_spi_ofdata_to_platdata(struct udevice *dev)
Haavard Skinnemoend255bb02008-05-16 11:10:31 +020064{
Jagan Tekic1a3f1e2019-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 Skinnemoend255bb02008-05-16 11:10:31 +020080}
81
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053082static int mpc8xxx_spi_probe(struct udevice *dev)
Ben Warren04a9e112008-01-16 22:37:35 -050083{
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053084 struct mpc8xxx_priv *priv = dev_get_priv(dev);
Ben Warren04a9e112008-01-16 22:37:35 -050085
Kim Phillips2956acd2008-01-17 12:48:00 -060086 /*
Ben Warren04a9e112008-01-16 22:37:35 -050087 * SPI pins on the MPC83xx are not muxed, so all we do is initialize
88 * some registers
Kim Phillips2956acd2008-01-17 12:48:00 -060089 */
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053090 out_be32(&priv->spi->mode, SPI_MODE_REV | SPI_MODE_MS | SPI_MODE_EN);
Ben Warren04a9e112008-01-16 22:37:35 -050091
Jagan Tekic1a3f1e2019-04-29 01:58:53 +053092 __spi_set_speed(priv->spi, 16666667);
93
94 /* Clear all SPI events */
95 setbits_be32(&priv->spi->event, 0xffffffff);
96 /* Mask all SPI interrupts */
97 clrbits_be32(&priv->spi->mask, 0xffffffff);
98 /* LST bit doesn't do anything, so disregard */
99 out_be32(&priv->spi->com, 0);
100
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200101 return 0;
102}
103
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530104static void mpc8xxx_spi_cs_activate(struct udevice *dev)
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200105{
Jagan Tekic1a3f1e2019-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 Skinnemoend255bb02008-05-16 11:10:31 +0200111}
112
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530113static void mpc8xxx_spi_cs_deactivate(struct udevice *dev)
Ben Warren04a9e112008-01-16 22:37:35 -0500114{
Jagan Tekic1a3f1e2019-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);
120}
121
122static int mpc8xxx_spi_xfer(struct udevice *dev, uint bitlen,
123 const void *dout, void *din, ulong flags)
124{
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 Six01ac1e12019-04-29 01:58:38 +0530130 int num_blks = DIV_ROUND_UP(bitlen, 32);
Ben Warren04a9e112008-01-16 22:37:35 -0500131
Jagan Tekic1a3f1e2019-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 Warren04a9e112008-01-16 22:37:35 -0500134
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200135 if (flags & SPI_XFER_BEGIN)
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530136 mpc8xxx_spi_cs_activate(dev);
Ben Warren04a9e112008-01-16 22:37:35 -0500137
Mario Sixd93fe312019-04-29 01:58:37 +0530138 /* Clear all SPI events */
Mario Six1a907e42019-04-29 01:58:42 +0530139 setbits_be32(&spi->event, 0xffffffff);
Ben Warren04a9e112008-01-16 22:37:35 -0500140
Mario Sixd93fe312019-04-29 01:58:37 +0530141 /* Handle data in 32-bit chunks */
Mario Six01ac1e12019-04-29 01:58:38 +0530142 while (num_blks--) {
Mario Six65f88e02019-04-29 01:58:46 +0530143 u32 tmpdout = 0;
Mario Six5ccfb8a2019-04-29 01:58:48 +0530144 uchar xfer_bitlen = (bitlen >= 32 ? 32 : bitlen);
Mario Six67adbae2019-04-29 01:58:52 +0530145 ulong start;
Ben Warren04a9e112008-01-16 22:37:35 -0500146
Mario Six1a907e42019-04-29 01:58:42 +0530147 clrbits_be32(&spi->mode, SPI_MODE_EN);
Ira W. Snyderf138ca12012-09-12 14:17:31 -0700148
Mario Six85fa2652019-04-29 01:58:49 +0530149 /* Set up length for this transfer */
150
151 if (bitlen <= 4) /* 4 bits or less */
Mario Six8dea61d2019-04-29 01:58:47 +0530152 set_char_len(spi, 3);
Mario Six85fa2652019-04-29 01:58:49 +0530153 else if (bitlen <= 16) /* at most 16 bits */
Mario Six8dea61d2019-04-29 01:58:47 +0530154 set_char_len(spi, bitlen - 1);
Mario Six85fa2652019-04-29 01:58:49 +0530155 else /* more than 16 bits -> full 32 bit transfer */
Mario Six8dea61d2019-04-29 01:58:47 +0530156 set_char_len(spi, 0);
157
Mario Sixa1c178e2019-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 Sixf6fcad52019-04-29 01:58:51 +0530163 if (bitlen > 32) {
Ben Warren04a9e112008-01-16 22:37:35 -0500164 /* Set up the next iteration if sending > 32 bits */
165 bitlen -= 32;
166 dout += 4;
167 }
168
Mario Sixd93fe312019-04-29 01:58:37 +0530169 /* Write the data out */
Mario Six1a907e42019-04-29 01:58:42 +0530170 out_be32(&spi->tx, tmpdout);
Mario Sixd93fe312019-04-29 01:58:37 +0530171
Mario Sixfabe6c42019-04-29 01:58:40 +0530172 debug("*** %s: ... %08x written\n", __func__, tmpdout);
Ben Warren04a9e112008-01-16 22:37:35 -0500173
Kim Phillips2956acd2008-01-17 12:48:00 -0600174 /*
Ben Warren04a9e112008-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 Phillips2956acd2008-01-17 12:48:00 -0600178 */
Mario Six67adbae2019-04-29 01:58:52 +0530179 start = get_timer(0);
180 do {
Mario Six65f88e02019-04-29 01:58:46 +0530181 u32 event = in_be32(&spi->event);
Mario Six6409c612019-04-29 01:58:44 +0530182 bool have_ne = event & SPI_EV_NE;
183 bool have_nf = event & SPI_EV_NF;
184
Mario Sixe4da4c22019-04-29 01:58:45 +0530185 if (!have_ne)
186 continue;
Ben Warren04a9e112008-01-16 22:37:35 -0500187
Mario Sixe4da4c22019-04-29 01:58:45 +0530188 tmpdin = in_be32(&spi->rx);
189 setbits_be32(&spi->event, SPI_EV_NE);
190
Mario Six5ccfb8a2019-04-29 01:58:48 +0530191 *(u32 *)din = (tmpdin << (32 - xfer_bitlen));
192 if (xfer_bitlen == 32) {
Mario Sixe4da4c22019-04-29 01:58:45 +0530193 /* Advance output buffer by 32 bits */
194 din += 4;
Ben Warren04a9e112008-01-16 22:37:35 -0500195 }
Mario Sixe4da4c22019-04-29 01:58:45 +0530196
Kim Phillips2956acd2008-01-17 12:48:00 -0600197 /*
198 * Only bail when we've had both NE and NF events.
Ben Warren04a9e112008-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 Phillips2956acd2008-01-17 12:48:00 -0600201 * the device. Arbitrary delays suck, though...
202 */
Mario Sixe4da4c22019-04-29 01:58:45 +0530203 if (have_nf)
Ben Warren04a9e112008-01-16 22:37:35 -0500204 break;
Mario Sixe4da4c22019-04-29 01:58:45 +0530205
Mario Six67adbae2019-04-29 01:58:52 +0530206 mdelay(1);
207 } while (get_timer(start) < SPI_TIMEOUT);
208
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530209 if (get_timer(start) >= SPI_TIMEOUT) {
Mario Sixfabe6c42019-04-29 01:58:40 +0530210 debug("*** %s: Time out during SPI transfer\n",
211 __func__);
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530212 return -ETIMEDOUT;
213 }
Ben Warren04a9e112008-01-16 22:37:35 -0500214
Mario Sixfabe6c42019-04-29 01:58:40 +0530215 debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin);
Ben Warren04a9e112008-01-16 22:37:35 -0500216 }
217
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200218 if (flags & SPI_XFER_END)
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530219 mpc8xxx_spi_cs_deactivate(dev);
Kim Phillips2956acd2008-01-17 12:48:00 -0600220
Ben Warren04a9e112008-01-16 22:37:35 -0500221 return 0;
222}
Jagan Tekic1a3f1e2019-04-29 01:58:53 +0530223
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}
230
231static 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 */
236 return 0;
237}
238
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};