blob: b1abfbf4fcf8fb0d1ffd88393c4ddf9c36634e15 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Christophe Leroy907208c2017-07-06 10:23:22 +02002/*
3 * Copyright (c) 2001 Navin Boppuri / Prashant Patel
4 * <nboppuri@trinetcommunication.com>,
5 * <pmpatel@trinetcommunication.com>
6 * Copyright (c) 2001 Gerd Mennchen <Gerd.Mennchen@icn.siemens.de>
7 * Copyright (c) 2001 Wolfgang Denk, DENX Software Engineering, <wd@denx.de>.
Christophe Leroy907208c2017-07-06 10:23:22 +02008 */
9
10/*
11 * MPC8xx CPM SPI interface.
12 *
13 * Parts of this code are probably not portable and/or specific to
14 * the board which I used for the tests. Please send fixes/complaints
15 * to wd@denx.de
16 *
17 */
18
19#include <common.h>
Christophe Leroyfb0204e2018-11-21 08:51:57 +000020#include <dm.h>
Christophe Leroydff36802024-04-12 18:16:59 +020021#include <malloc.h>
Christophe Leroy907208c2017-07-06 10:23:22 +020022#include <mpc8xx.h>
Christophe Leroyfb0204e2018-11-21 08:51:57 +000023#include <spi.h>
Simon Glassc05ed002020-05-10 11:40:11 -060024#include <linux/delay.h>
Christophe Leroyfb0204e2018-11-21 08:51:57 +000025
Christophe Leroy18f8d4c2018-03-16 17:20:43 +010026#include <asm/cpm_8xx.h>
Christophe Leroyfb0204e2018-11-21 08:51:57 +000027#include <asm/io.h>
Christophe Leroy773ad4e2022-10-14 09:14:44 +020028#include <asm/gpio.h>
Christophe Leroy907208c2017-07-06 10:23:22 +020029
Christophe Leroyba3da732017-07-06 10:33:13 +020030#define CPM_SPI_BASE_RX CPM_SPI_BASE
31#define CPM_SPI_BASE_TX (CPM_SPI_BASE + sizeof(cbd_t))
32
Christophe Leroyea208202024-04-09 08:38:08 +020033#define MAX_BUFFER 0x8000 /* Max possible is 0xffff. We want power of 2 */
Christophe Leroydff36802024-04-12 18:16:59 +020034#define MIN_HWORD_XFER 64 /* Minimum size for 16 bits transfer */
Christophe Leroy907208c2017-07-06 10:23:22 +020035
Christophe Leroy773ad4e2022-10-14 09:14:44 +020036struct mpc8xx_priv {
37 spi_t __iomem *spi;
38 struct gpio_desc gpios[16];
39 int max_cs;
40};
41
Christophe Leroyea208202024-04-09 08:38:08 +020042static char dummy_buffer[MAX_BUFFER];
43
Christophe Leroy773ad4e2022-10-14 09:14:44 +020044static int mpc8xx_spi_set_mode(struct udevice *dev, uint mod)
45{
46 return 0;
47}
48
49static int mpc8xx_spi_set_speed(struct udevice *dev, uint speed)
50{
51 return 0;
52}
53
Christophe Leroyfb0204e2018-11-21 08:51:57 +000054static int mpc8xx_spi_probe(struct udevice *dev)
Christophe Leroy907208c2017-07-06 10:23:22 +020055{
Christophe Leroyba3da732017-07-06 10:33:13 +020056 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
57 cpm8xx_t __iomem *cp = &immr->im_cpm;
Christophe Leroyfdd243d2023-05-03 10:31:19 +020058 spi_t __iomem *spi = (spi_t __iomem *)&cp->cp_dpmem[PROFF_SPI];
Christophe Leroy14e64c12023-05-03 09:05:33 +020059 u16 spi_rpbase;
Christophe Leroyba3da732017-07-06 10:33:13 +020060 cbd_t __iomem *tbdf, *rbdf;
Christophe Leroy907208c2017-07-06 10:23:22 +020061
Christophe Leroy14e64c12023-05-03 09:05:33 +020062 spi_rpbase = in_be16(&spi->spi_rpbase);
63 if (spi_rpbase)
64 spi = (spi_t __iomem *)&cp->cp_dpmem[spi_rpbase];
Christophe Leroy907208c2017-07-06 10:23:22 +020065
66/* 1 */
Christophe Leroy907208c2017-07-06 10:23:22 +020067 /* Initialize the parameter ram.
68 * We need to make sure many things are initialized to zero
69 */
Christophe Leroyba3da732017-07-06 10:33:13 +020070 out_be32(&spi->spi_rstate, 0);
71 out_be32(&spi->spi_rdp, 0);
72 out_be16(&spi->spi_rbptr, 0);
73 out_be16(&spi->spi_rbc, 0);
74 out_be32(&spi->spi_rxtmp, 0);
75 out_be32(&spi->spi_tstate, 0);
76 out_be32(&spi->spi_tdp, 0);
77 out_be16(&spi->spi_tbptr, 0);
78 out_be16(&spi->spi_tbc, 0);
79 out_be32(&spi->spi_txtmp, 0);
Christophe Leroy907208c2017-07-06 10:23:22 +020080
81/* 3 */
82 /* Set up the SPI parameters in the parameter ram */
Christophe Leroyba3da732017-07-06 10:33:13 +020083 out_be16(&spi->spi_rbase, CPM_SPI_BASE_RX);
84 out_be16(&spi->spi_tbase, CPM_SPI_BASE_TX);
Christophe Leroy907208c2017-07-06 10:23:22 +020085
86 /***********IMPORTANT******************/
87
88 /*
89 * Setting transmit and receive buffer descriptor pointers
90 * initially to rbase and tbase. Only the microcode patches
91 * documentation talks about initializing this pointer. This
92 * is missing from the sample I2C driver. If you dont
93 * initialize these pointers, the kernel hangs.
94 */
Christophe Leroyba3da732017-07-06 10:33:13 +020095 out_be16(&spi->spi_rbptr, CPM_SPI_BASE_RX);
96 out_be16(&spi->spi_tbptr, CPM_SPI_BASE_TX);
Christophe Leroy907208c2017-07-06 10:23:22 +020097
98/* 4 */
99 /* Init SPI Tx + Rx Parameters */
Christophe Leroyba3da732017-07-06 10:33:13 +0200100 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
Christophe Leroy907208c2017-07-06 10:23:22 +0200101 ;
Christophe Leroyba3da732017-07-06 10:33:13 +0200102
103 out_be16(&cp->cp_cpcr, mk_cr_cmd(CPM_CR_CH_SPI, CPM_CR_INIT_TRX) |
104 CPM_CR_FLG);
105 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
Christophe Leroy907208c2017-07-06 10:23:22 +0200106 ;
107
Christophe Leroy907208c2017-07-06 10:23:22 +0200108/* 6 */
109 /* Set to big endian. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200110 out_8(&spi->spi_tfcr, SMC_EB);
111 out_8(&spi->spi_rfcr, SMC_EB);
Christophe Leroy907208c2017-07-06 10:23:22 +0200112
113/* 7 */
114 /* Set maximum receive size. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200115 out_be16(&spi->spi_mrblr, MAX_BUFFER);
Christophe Leroy907208c2017-07-06 10:23:22 +0200116
117/* 8 + 9 */
118 /* tx and rx buffer descriptors */
Christophe Leroyba3da732017-07-06 10:33:13 +0200119 tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
120 rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
Christophe Leroy907208c2017-07-06 10:23:22 +0200121
Christophe Leroyba3da732017-07-06 10:33:13 +0200122 clrbits_be16(&tbdf->cbd_sc, BD_SC_READY);
123 clrbits_be16(&rbdf->cbd_sc, BD_SC_EMPTY);
Christophe Leroy907208c2017-07-06 10:23:22 +0200124
Christophe Leroy907208c2017-07-06 10:23:22 +0200125/* 10 + 11 */
Christophe Leroyba3da732017-07-06 10:33:13 +0200126 out_8(&cp->cp_spim, 0); /* Mask all SPI events */
127 out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
Christophe Leroy907208c2017-07-06 10:23:22 +0200128
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000129 return 0;
Christophe Leroy907208c2017-07-06 10:23:22 +0200130}
131
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200132static void mpc8xx_spi_cs_activate(struct udevice *dev)
133{
134 struct mpc8xx_priv *priv = dev_get_priv(dev->parent);
135 struct dm_spi_slave_plat *platdata = dev_get_parent_plat(dev);
136
137 dm_gpio_set_value(&priv->gpios[platdata->cs], 1);
138}
139
140static void mpc8xx_spi_cs_deactivate(struct udevice *dev)
141{
142 struct mpc8xx_priv *priv = dev_get_priv(dev->parent);
143 struct dm_spi_slave_plat *platdata = dev_get_parent_plat(dev);
144
145 dm_gpio_set_value(&priv->gpios[platdata->cs], 0);
146}
147
Christophe Leroy244f8462024-04-12 13:53:57 +0200148static int mpc8xx_spi_xfer_one(struct udevice *dev, size_t count,
149 const void *dout, void *din)
Christophe Leroy907208c2017-07-06 10:23:22 +0200150{
Christophe Leroyba3da732017-07-06 10:33:13 +0200151 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
152 cpm8xx_t __iomem *cp = &immr->im_cpm;
Christophe Leroyba3da732017-07-06 10:33:13 +0200153 cbd_t __iomem *tbdf, *rbdf;
Christophe Leroydff36802024-04-12 18:16:59 +0200154 void *bufout, *bufin;
155 u16 spmode_len;
Christophe Leroy907208c2017-07-06 10:23:22 +0200156 int tm;
Christophe Leroy907208c2017-07-06 10:23:22 +0200157
Christophe Leroyba3da732017-07-06 10:33:13 +0200158 tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
159 rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
Christophe Leroy907208c2017-07-06 10:23:22 +0200160
Christophe Leroydff36802024-04-12 18:16:59 +0200161 if (!(count & 1) && count >= MIN_HWORD_XFER) {
162 spmode_len = SPMODE_LEN(16);
163 if (dout) {
164 int i;
165
166 bufout = malloc(count);
167 for (i = 0; i < count; i += 2)
168 *(u16 *)(bufout + i) = swab16(*(u16 *)(dout + i));
169 } else {
170 bufout = NULL;
171 }
172 if (din)
173 bufin = malloc(count);
174 else
175 bufin = NULL;
176 } else {
177 spmode_len = SPMODE_LEN(8);
178 bufout = (void *)dout;
179 bufin = din;
180 }
181
Christophe Leroy907208c2017-07-06 10:23:22 +0200182 /* Setting tx bd status and data length */
Christophe Leroydff36802024-04-12 18:16:59 +0200183 out_be32(&tbdf->cbd_bufaddr, bufout ? (ulong)bufout : (ulong)dummy_buffer);
Christophe Leroyba3da732017-07-06 10:33:13 +0200184 out_be16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_LAST | BD_SC_WRAP);
185 out_be16(&tbdf->cbd_datlen, count);
Christophe Leroy907208c2017-07-06 10:23:22 +0200186
187 /* Setting rx bd status and data length */
Christophe Leroydff36802024-04-12 18:16:59 +0200188 out_be32(&rbdf->cbd_bufaddr, bufin ? (ulong)bufin : (ulong)dummy_buffer);
Christophe Leroyba3da732017-07-06 10:33:13 +0200189 out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
190 out_be16(&rbdf->cbd_datlen, 0); /* rx length has no significance */
Christophe Leroy907208c2017-07-06 10:23:22 +0200191
Christophe Leroyba3da732017-07-06 10:33:13 +0200192 clrsetbits_be16(&cp->cp_spmode, ~SPMODE_LOOP, SPMODE_REV | SPMODE_MSTR |
Christophe Leroydff36802024-04-12 18:16:59 +0200193 SPMODE_EN | spmode_len | SPMODE_PM(0x8));
Christophe Leroyba3da732017-07-06 10:33:13 +0200194 out_8(&cp->cp_spim, 0); /* Mask all SPI events */
195 out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
Christophe Leroy907208c2017-07-06 10:23:22 +0200196
197 /* start spi transfer */
Christophe Leroyba3da732017-07-06 10:33:13 +0200198 setbits_8(&cp->cp_spcom, SPI_STR); /* Start transmit */
Christophe Leroy907208c2017-07-06 10:23:22 +0200199
200 /* --------------------------------
201 * Wait for SPI transmit to get out
202 * or time out (1 second = 1000 ms)
203 * -------------------------------- */
Christophe Leroy70fd0712017-07-06 10:33:17 +0200204 for (tm = 0; tm < 1000; ++tm) {
Christophe Leroyba3da732017-07-06 10:33:13 +0200205 if (in_8(&cp->cp_spie) & SPI_TXB) /* Tx Buffer Empty */
Christophe Leroy907208c2017-07-06 10:23:22 +0200206 break;
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200207
Christophe Leroyba3da732017-07-06 10:33:13 +0200208 if ((in_be16(&tbdf->cbd_sc) & BD_SC_READY) == 0)
Christophe Leroy907208c2017-07-06 10:23:22 +0200209 break;
Christophe Leroy70fd0712017-07-06 10:33:17 +0200210 udelay(1000);
Christophe Leroy907208c2017-07-06 10:23:22 +0200211 }
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200212
Christophe Leroy70fd0712017-07-06 10:33:17 +0200213 if (tm >= 1000)
Christophe Leroy244f8462024-04-12 13:53:57 +0200214 return -ETIMEDOUT;
Christophe Leroy907208c2017-07-06 10:23:22 +0200215
Christophe Leroydff36802024-04-12 18:16:59 +0200216 if (!(count & 1) && count > MIN_HWORD_XFER) {
217 if (dout)
218 free(bufout);
219 if (din) {
220 int i;
221
222 bufout = malloc(count);
223 for (i = 0; i < count; i += 2)
224 *(u16 *)(din + i) = swab16(*(u16 *)(bufin + i));
225 free(bufin);
226 }
227 }
228
Christophe Leroy244f8462024-04-12 13:53:57 +0200229 return 0;
230}
231
232static int mpc8xx_spi_xfer(struct udevice *dev, unsigned int bitlen,
233 const void *dout, void *din, unsigned long flags)
234{
235 size_t count = (bitlen + 7) / 8;
236 size_t offset = 0;
237 int ret = 0;
238
239 if (!din && !dout)
240 return -EINVAL;
241
242 /* Set CS for device */
243 if (flags & SPI_XFER_BEGIN)
244 mpc8xx_spi_cs_activate(dev);
245
246 while (count > 0 && !ret) {
247 size_t chunk = min(count, (size_t)MAX_BUFFER);
248 const void *out = dout ? dout + offset : NULL;
249 void *in = din ? din + offset : NULL;
250
251 ret = mpc8xx_spi_xfer_one(dev, chunk, out, in);
252
253 offset += chunk;
254 count -= chunk;
255 }
Christophe Leroy907208c2017-07-06 10:23:22 +0200256 /* Clear CS for device */
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200257 if (flags & SPI_XFER_END)
258 mpc8xx_spi_cs_deactivate(dev);
Christophe Leroy907208c2017-07-06 10:23:22 +0200259
Christophe Leroy244f8462024-04-12 13:53:57 +0200260 if (ret)
261 printf("*** spi_xfer: Time out while xferring to/from SPI!\n");
262
263 return ret;
Christophe Leroy907208c2017-07-06 10:23:22 +0200264}
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000265
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200266static int mpc8xx_spi_ofdata_to_platdata(struct udevice *dev)
267{
268 struct mpc8xx_priv *priv = dev_get_priv(dev);
269 int ret;
270
271 ret = gpio_request_list_by_name(dev, "gpios", priv->gpios,
272 ARRAY_SIZE(priv->gpios), GPIOD_IS_OUT);
273 if (ret < 0)
274 return ret;
275
276 priv->max_cs = ret;
277
278 return 0;
279}
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000280static const struct dm_spi_ops mpc8xx_spi_ops = {
281 .xfer = mpc8xx_spi_xfer,
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200282 .set_speed = mpc8xx_spi_set_speed,
283 .set_mode = mpc8xx_spi_set_mode,
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000284};
285
286static const struct udevice_id mpc8xx_spi_ids[] = {
287 { .compatible = "fsl,mpc8xx-spi" },
288 { }
289};
290
291U_BOOT_DRIVER(mpc8xx_spi) = {
292 .name = "mpc8xx_spi",
293 .id = UCLASS_SPI,
294 .of_match = mpc8xx_spi_ids,
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200295 .of_to_plat = mpc8xx_spi_ofdata_to_platdata,
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000296 .ops = &mpc8xx_spi_ops,
297 .probe = mpc8xx_spi_probe,
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200298 .priv_auto = sizeof(struct mpc8xx_priv),
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000299};