common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / spi / mpc8xxx_spi.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2006 Ben Warren, Qstreams Networks Inc.
4  * With help from the common/soft_spi and arch/powerpc/cpu/mpc8260 drivers
5  */
6
7 #include <common.h>
8 #include <clk.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <log.h>
12 #include <malloc.h>
13 #include <spi.h>
14 #include <asm/mpc8xxx_spi.h>
15 #include <asm-generic/gpio.h>
16 #include <dm/device_compat.h>
17 #include <linux/delay.h>
18
19 enum {
20         SPI_EV_NE = BIT(31 - 22),       /* Receiver Not Empty */
21         SPI_EV_NF = BIT(31 - 23),       /* Transmitter Not Full */
22 };
23
24 enum {
25         SPI_MODE_LOOP  = BIT(31 - 1),   /* Loopback mode */
26         SPI_MODE_CI    = BIT(31 - 2),   /* Clock invert */
27         SPI_MODE_CP    = BIT(31 - 3),   /* Clock phase */
28         SPI_MODE_DIV16 = BIT(31 - 4),   /* Divide clock source by 16 */
29         SPI_MODE_REV   = BIT(31 - 5),   /* Reverse mode - MSB first */
30         SPI_MODE_MS    = BIT(31 - 6),   /* Always master */
31         SPI_MODE_EN    = BIT(31 - 7),   /* Enable interface */
32
33         SPI_MODE_LEN_MASK = 0xf00000,
34         SPI_MODE_LEN_SHIFT = 20,
35         SPI_MODE_PM_SHIFT = 16,
36         SPI_MODE_PM_MASK = 0xf0000,
37
38         SPI_COM_LST = BIT(31 - 9),
39 };
40
41 struct mpc8xxx_priv {
42         spi8xxx_t *spi;
43         struct gpio_desc gpios[16];
44         int cs_count;
45         ulong clk_rate;
46 };
47
48 #define SPI_TIMEOUT     1000
49
50 static int mpc8xxx_spi_ofdata_to_platdata(struct udevice *dev)
51 {
52         struct mpc8xxx_priv *priv = dev_get_priv(dev);
53         struct clk clk;
54         int ret;
55
56         priv->spi = (spi8xxx_t *)dev_read_addr(dev);
57
58         ret = gpio_request_list_by_name(dev, "gpios", priv->gpios,
59                                         ARRAY_SIZE(priv->gpios), GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
60         if (ret < 0)
61                 return -EINVAL;
62
63         priv->cs_count = ret;
64
65         ret = clk_get_by_index(dev, 0, &clk);
66         if (ret) {
67                 dev_err(dev, "%s: clock not defined\n", __func__);
68                 return ret;
69         }
70
71         priv->clk_rate = clk_get_rate(&clk);
72         if (!priv->clk_rate) {
73                 dev_err(dev, "%s: failed to get clock rate\n", __func__);
74                 return -EINVAL;
75         }
76
77         return 0;
78 }
79
80 static int mpc8xxx_spi_probe(struct udevice *dev)
81 {
82         struct mpc8xxx_priv *priv = dev_get_priv(dev);
83         spi8xxx_t *spi = priv->spi;
84
85         /*
86          * SPI pins on the MPC83xx are not muxed, so all we do is initialize
87          * some registers
88          */
89         out_be32(&priv->spi->mode, SPI_MODE_REV | SPI_MODE_MS);
90
91         /* set len to 8 bits */
92         setbits_be32(&spi->mode, (8 - 1) << SPI_MODE_LEN_SHIFT);
93
94         setbits_be32(&spi->mode, SPI_MODE_EN);
95
96         /* Clear all SPI events */
97         setbits_be32(&priv->spi->event, 0xffffffff);
98         /* Mask  all SPI interrupts */
99         clrbits_be32(&priv->spi->mask, 0xffffffff);
100         /* LST bit doesn't do anything, so disregard */
101         out_be32(&priv->spi->com, 0);
102
103         return 0;
104 }
105
106 static void mpc8xxx_spi_cs_activate(struct udevice *dev)
107 {
108         struct mpc8xxx_priv *priv = dev_get_priv(dev->parent);
109         struct dm_spi_slave_platdata *platdata = dev_get_parent_platdata(dev);
110
111         dm_gpio_set_dir_flags(&priv->gpios[platdata->cs], GPIOD_IS_OUT);
112         dm_gpio_set_value(&priv->gpios[platdata->cs], 0);
113 }
114
115 static void mpc8xxx_spi_cs_deactivate(struct udevice *dev)
116 {
117         struct mpc8xxx_priv *priv = dev_get_priv(dev->parent);
118         struct dm_spi_slave_platdata *platdata = dev_get_parent_platdata(dev);
119
120         dm_gpio_set_dir_flags(&priv->gpios[platdata->cs], GPIOD_IS_OUT);
121         dm_gpio_set_value(&priv->gpios[platdata->cs], 1);
122 }
123
124 static int mpc8xxx_spi_xfer(struct udevice *dev, uint bitlen,
125                             const void *dout, void *din, ulong flags)
126 {
127         struct udevice *bus = dev->parent;
128         struct mpc8xxx_priv *priv = dev_get_priv(bus);
129         spi8xxx_t *spi = priv->spi;
130         struct dm_spi_slave_platdata *platdata = dev_get_parent_platdata(dev);
131         u32 tmpdin = 0, tmpdout = 0, n;
132         const u8 *cout = dout;
133         u8 *cin = din;
134
135         debug("%s: slave %s:%u dout %08X din %08X bitlen %u\n", __func__,
136               bus->name, platdata->cs, (uint)dout, (uint)din, bitlen);
137         if (platdata->cs >= priv->cs_count) {
138                 dev_err(dev, "chip select index %d too large (cs_count=%d)\n",
139                         platdata->cs, priv->cs_count);
140                 return -EINVAL;
141         }
142         if (bitlen % 8) {
143                 printf("*** spi_xfer: bitlen must be multiple of 8\n");
144                 return -ENOTSUPP;
145         }
146
147         if (flags & SPI_XFER_BEGIN)
148                 mpc8xxx_spi_cs_activate(dev);
149
150         /* Clear all SPI events */
151         setbits_be32(&spi->event, 0xffffffff);
152         n = bitlen / 8;
153
154         /* Handle data in 8-bit chunks */
155         while (n--) {
156                 ulong start;
157
158                 if (cout)
159                         tmpdout = *cout++;
160
161                 /* Write the data out */
162                 out_be32(&spi->tx, tmpdout);
163
164                 debug("*** %s: ... %08x written\n", __func__, tmpdout);
165
166                 /*
167                  * Wait for SPI transmit to get out
168                  * or time out (1 second = 1000 ms)
169                  * The NE event must be read and cleared first
170                  */
171                 start = get_timer(0);
172                 do {
173                         u32 event = in_be32(&spi->event);
174                         bool have_ne = event & SPI_EV_NE;
175                         bool have_nf = event & SPI_EV_NF;
176
177                         if (!have_ne)
178                                 continue;
179
180                         tmpdin = in_be32(&spi->rx);
181                         setbits_be32(&spi->event, SPI_EV_NE);
182
183                         if (cin)
184                                 *cin++ = tmpdin;
185
186                         /*
187                          * Only bail when we've had both NE and NF events.
188                          * This will cause timeouts on RO devices, so maybe
189                          * in the future put an arbitrary delay after writing
190                          * the device.  Arbitrary delays suck, though...
191                          */
192                         if (have_nf)
193                                 break;
194
195                         mdelay(1);
196                 } while (get_timer(start) < SPI_TIMEOUT);
197
198                 if (get_timer(start) >= SPI_TIMEOUT) {
199                         debug("*** %s: Time out during SPI transfer\n",
200                               __func__);
201                         return -ETIMEDOUT;
202                 }
203
204                 debug("*** %s: transfer ended. Value=%08x\n", __func__, tmpdin);
205         }
206
207         if (flags & SPI_XFER_END)
208                 mpc8xxx_spi_cs_deactivate(dev);
209
210         return 0;
211 }
212
213 static int mpc8xxx_spi_set_speed(struct udevice *dev, uint speed)
214 {
215         struct mpc8xxx_priv *priv = dev_get_priv(dev);
216         spi8xxx_t *spi = priv->spi;
217         u32 bits, mask, div16, pm;
218         u32 mode;
219         ulong clk;
220
221         clk = priv->clk_rate;
222         if (clk / 64 > speed) {
223                 div16 = SPI_MODE_DIV16;
224                 clk /= 16;
225         } else {
226                 div16 = 0;
227         }
228         pm = (clk - 1)/(4*speed) + 1;
229         if (pm > 16) {
230                 dev_err(dev, "requested speed %u too small\n", speed);
231                 return -EINVAL;
232         }
233         pm--;
234
235         bits = div16 | (pm << SPI_MODE_PM_SHIFT);
236         mask = SPI_MODE_DIV16 | SPI_MODE_PM_MASK;
237         mode = in_be32(&spi->mode);
238         if ((mode & mask) != bits) {
239                 /* Must clear mode[EN] while changing speed. */
240                 mode &= ~(mask | SPI_MODE_EN);
241                 out_be32(&spi->mode, mode);
242                 mode |= bits;
243                 out_be32(&spi->mode, mode);
244                 mode |= SPI_MODE_EN;
245                 out_be32(&spi->mode, mode);
246         }
247
248         debug("requested speed %u, set speed to %lu/(%s4*%u) == %lu\n",
249               speed, priv->clk_rate, div16 ? "16*" : "", pm + 1,
250               clk/(4*(pm + 1)));
251
252         return 0;
253 }
254
255 static int mpc8xxx_spi_set_mode(struct udevice *dev, uint mode)
256 {
257         /* TODO(mario.six@gdsys.cc): Using SPI_CPHA (for clock phase) and
258          * SPI_CPOL (for clock polarity) should work
259          */
260         return 0;
261 }
262
263 static const struct dm_spi_ops mpc8xxx_spi_ops = {
264         .xfer           = mpc8xxx_spi_xfer,
265         .set_speed      = mpc8xxx_spi_set_speed,
266         .set_mode       = mpc8xxx_spi_set_mode,
267         /*
268          * cs_info is not needed, since we require all chip selects to be
269          * in the device tree explicitly
270          */
271 };
272
273 static const struct udevice_id mpc8xxx_spi_ids[] = {
274         { .compatible = "fsl,spi" },
275         { }
276 };
277
278 U_BOOT_DRIVER(mpc8xxx_spi) = {
279         .name   = "mpc8xxx_spi",
280         .id     = UCLASS_SPI,
281         .of_match = mpc8xxx_spi_ids,
282         .ops    = &mpc8xxx_spi_ops,
283         .ofdata_to_platdata = mpc8xxx_spi_ofdata_to_platdata,
284         .probe  = mpc8xxx_spi_probe,
285         .priv_auto_alloc_size = sizeof(struct mpc8xxx_priv),
286 };