blob: 7e72fb9e23dc7d60e8d76709b375c5363e58d37f [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
Christophe Leroyfb0204e2018-11-21 08:51:57 +000019#include <dm.h>
Christophe Leroydff36802024-04-12 18:16:59 +020020#include <malloc.h>
Christophe Leroy907208c2017-07-06 10:23:22 +020021#include <mpc8xx.h>
Christophe Leroyfb0204e2018-11-21 08:51:57 +000022#include <spi.h>
Simon Glassc05ed002020-05-10 11:40:11 -060023#include <linux/delay.h>
Christophe Leroyfb0204e2018-11-21 08:51:57 +000024
Christophe Leroy18f8d4c2018-03-16 17:20:43 +010025#include <asm/cpm_8xx.h>
Christophe Leroyfb0204e2018-11-21 08:51:57 +000026#include <asm/io.h>
Christophe Leroy773ad4e2022-10-14 09:14:44 +020027#include <asm/gpio.h>
Christophe Leroy907208c2017-07-06 10:23:22 +020028
Christophe Leroyba3da732017-07-06 10:33:13 +020029#define CPM_SPI_BASE_RX CPM_SPI_BASE
30#define CPM_SPI_BASE_TX (CPM_SPI_BASE + sizeof(cbd_t))
31
Christophe Leroyea208202024-04-09 08:38:08 +020032#define MAX_BUFFER 0x8000 /* Max possible is 0xffff. We want power of 2 */
Christophe Leroydff36802024-04-12 18:16:59 +020033#define MIN_HWORD_XFER 64 /* Minimum size for 16 bits transfer */
Christophe Leroy907208c2017-07-06 10:23:22 +020034
Christophe Leroy773ad4e2022-10-14 09:14:44 +020035struct mpc8xx_priv {
36 spi_t __iomem *spi;
37 struct gpio_desc gpios[16];
38 int max_cs;
39};
40
Christophe Leroyea208202024-04-09 08:38:08 +020041static char dummy_buffer[MAX_BUFFER];
42
Christophe Leroy773ad4e2022-10-14 09:14:44 +020043static int mpc8xx_spi_set_mode(struct udevice *dev, uint mod)
44{
45 return 0;
46}
47
48static int mpc8xx_spi_set_speed(struct udevice *dev, uint speed)
49{
Christophe Leroy4fb931e2024-04-12 19:36:19 +020050 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
51 cpm8xx_t __iomem *cp = &immr->im_cpm;
52 u8 pm = (gd->arch.brg_clk - 1) / (speed * 16);
53
54 if (pm > 16) {
55 setbits_be16(&cp->cp_spmode, SPMODE_DIV16);
56 pm /= 16;
57 if (pm > 16)
58 pm = 16;
59 } else {
60 clrbits_be16(&cp->cp_spmode, SPMODE_DIV16);
61 }
62
63 clrsetbits_be16(&cp->cp_spmode, SPMODE_PM(0xf), SPMODE_PM(pm));
64
Christophe Leroy773ad4e2022-10-14 09:14:44 +020065 return 0;
66}
67
Christophe Leroyfb0204e2018-11-21 08:51:57 +000068static int mpc8xx_spi_probe(struct udevice *dev)
Christophe Leroy907208c2017-07-06 10:23:22 +020069{
Christophe Leroyba3da732017-07-06 10:33:13 +020070 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
71 cpm8xx_t __iomem *cp = &immr->im_cpm;
Christophe Leroyfdd243d2023-05-03 10:31:19 +020072 spi_t __iomem *spi = (spi_t __iomem *)&cp->cp_dpmem[PROFF_SPI];
Christophe Leroy14e64c12023-05-03 09:05:33 +020073 u16 spi_rpbase;
Christophe Leroyba3da732017-07-06 10:33:13 +020074 cbd_t __iomem *tbdf, *rbdf;
Christophe Leroy907208c2017-07-06 10:23:22 +020075
Christophe Leroy14e64c12023-05-03 09:05:33 +020076 spi_rpbase = in_be16(&spi->spi_rpbase);
77 if (spi_rpbase)
78 spi = (spi_t __iomem *)&cp->cp_dpmem[spi_rpbase];
Christophe Leroy907208c2017-07-06 10:23:22 +020079
80/* 1 */
Christophe Leroy907208c2017-07-06 10:23:22 +020081 /* Initialize the parameter ram.
82 * We need to make sure many things are initialized to zero
83 */
Christophe Leroyba3da732017-07-06 10:33:13 +020084 out_be32(&spi->spi_rstate, 0);
85 out_be32(&spi->spi_rdp, 0);
86 out_be16(&spi->spi_rbptr, 0);
87 out_be16(&spi->spi_rbc, 0);
88 out_be32(&spi->spi_rxtmp, 0);
89 out_be32(&spi->spi_tstate, 0);
90 out_be32(&spi->spi_tdp, 0);
91 out_be16(&spi->spi_tbptr, 0);
92 out_be16(&spi->spi_tbc, 0);
93 out_be32(&spi->spi_txtmp, 0);
Christophe Leroy907208c2017-07-06 10:23:22 +020094
95/* 3 */
96 /* Set up the SPI parameters in the parameter ram */
Christophe Leroyba3da732017-07-06 10:33:13 +020097 out_be16(&spi->spi_rbase, CPM_SPI_BASE_RX);
98 out_be16(&spi->spi_tbase, CPM_SPI_BASE_TX);
Christophe Leroy907208c2017-07-06 10:23:22 +020099
100 /***********IMPORTANT******************/
101
102 /*
103 * Setting transmit and receive buffer descriptor pointers
104 * initially to rbase and tbase. Only the microcode patches
105 * documentation talks about initializing this pointer. This
106 * is missing from the sample I2C driver. If you dont
107 * initialize these pointers, the kernel hangs.
108 */
Christophe Leroyba3da732017-07-06 10:33:13 +0200109 out_be16(&spi->spi_rbptr, CPM_SPI_BASE_RX);
110 out_be16(&spi->spi_tbptr, CPM_SPI_BASE_TX);
Christophe Leroy907208c2017-07-06 10:23:22 +0200111
112/* 4 */
113 /* Init SPI Tx + Rx Parameters */
Christophe Leroyba3da732017-07-06 10:33:13 +0200114 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
Christophe Leroy907208c2017-07-06 10:23:22 +0200115 ;
Christophe Leroyba3da732017-07-06 10:33:13 +0200116
117 out_be16(&cp->cp_cpcr, mk_cr_cmd(CPM_CR_CH_SPI, CPM_CR_INIT_TRX) |
118 CPM_CR_FLG);
119 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
Christophe Leroy907208c2017-07-06 10:23:22 +0200120 ;
121
Christophe Leroy907208c2017-07-06 10:23:22 +0200122/* 6 */
123 /* Set to big endian. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200124 out_8(&spi->spi_tfcr, SMC_EB);
125 out_8(&spi->spi_rfcr, SMC_EB);
Christophe Leroy907208c2017-07-06 10:23:22 +0200126
127/* 7 */
128 /* Set maximum receive size. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200129 out_be16(&spi->spi_mrblr, MAX_BUFFER);
Christophe Leroy907208c2017-07-06 10:23:22 +0200130
131/* 8 + 9 */
132 /* tx and rx buffer descriptors */
Christophe Leroyba3da732017-07-06 10:33:13 +0200133 tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
134 rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
Christophe Leroy907208c2017-07-06 10:23:22 +0200135
Christophe Leroyba3da732017-07-06 10:33:13 +0200136 clrbits_be16(&tbdf->cbd_sc, BD_SC_READY);
137 clrbits_be16(&rbdf->cbd_sc, BD_SC_EMPTY);
Christophe Leroy907208c2017-07-06 10:23:22 +0200138
Christophe Leroy907208c2017-07-06 10:23:22 +0200139/* 10 + 11 */
Christophe Leroyba3da732017-07-06 10:33:13 +0200140 out_8(&cp->cp_spim, 0); /* Mask all SPI events */
141 out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
Christophe Leroy907208c2017-07-06 10:23:22 +0200142
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000143 return 0;
Christophe Leroy907208c2017-07-06 10:23:22 +0200144}
145
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200146static void mpc8xx_spi_cs_activate(struct udevice *dev)
147{
148 struct mpc8xx_priv *priv = dev_get_priv(dev->parent);
149 struct dm_spi_slave_plat *platdata = dev_get_parent_plat(dev);
150
151 dm_gpio_set_value(&priv->gpios[platdata->cs], 1);
152}
153
154static void mpc8xx_spi_cs_deactivate(struct udevice *dev)
155{
156 struct mpc8xx_priv *priv = dev_get_priv(dev->parent);
157 struct dm_spi_slave_plat *platdata = dev_get_parent_plat(dev);
158
159 dm_gpio_set_value(&priv->gpios[platdata->cs], 0);
160}
161
Christophe Leroy244f8462024-04-12 13:53:57 +0200162static int mpc8xx_spi_xfer_one(struct udevice *dev, size_t count,
163 const void *dout, void *din)
Christophe Leroy907208c2017-07-06 10:23:22 +0200164{
Christophe Leroyba3da732017-07-06 10:33:13 +0200165 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
166 cpm8xx_t __iomem *cp = &immr->im_cpm;
Christophe Leroyba3da732017-07-06 10:33:13 +0200167 cbd_t __iomem *tbdf, *rbdf;
Christophe Leroydff36802024-04-12 18:16:59 +0200168 void *bufout, *bufin;
169 u16 spmode_len;
Christophe Leroy907208c2017-07-06 10:23:22 +0200170 int tm;
Christophe Leroy907208c2017-07-06 10:23:22 +0200171
Christophe Leroyba3da732017-07-06 10:33:13 +0200172 tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
173 rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
Christophe Leroy907208c2017-07-06 10:23:22 +0200174
Christophe Leroydff36802024-04-12 18:16:59 +0200175 if (!(count & 1) && count >= MIN_HWORD_XFER) {
176 spmode_len = SPMODE_LEN(16);
177 if (dout) {
178 int i;
179
180 bufout = malloc(count);
181 for (i = 0; i < count; i += 2)
182 *(u16 *)(bufout + i) = swab16(*(u16 *)(dout + i));
183 } else {
184 bufout = NULL;
185 }
186 if (din)
187 bufin = malloc(count);
188 else
189 bufin = NULL;
190 } else {
191 spmode_len = SPMODE_LEN(8);
192 bufout = (void *)dout;
193 bufin = din;
194 }
195
Christophe Leroy907208c2017-07-06 10:23:22 +0200196 /* Setting tx bd status and data length */
Christophe Leroydff36802024-04-12 18:16:59 +0200197 out_be32(&tbdf->cbd_bufaddr, bufout ? (ulong)bufout : (ulong)dummy_buffer);
Christophe Leroyba3da732017-07-06 10:33:13 +0200198 out_be16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_LAST | BD_SC_WRAP);
199 out_be16(&tbdf->cbd_datlen, count);
Christophe Leroy907208c2017-07-06 10:23:22 +0200200
201 /* Setting rx bd status and data length */
Christophe Leroydff36802024-04-12 18:16:59 +0200202 out_be32(&rbdf->cbd_bufaddr, bufin ? (ulong)bufin : (ulong)dummy_buffer);
Christophe Leroyba3da732017-07-06 10:33:13 +0200203 out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
204 out_be16(&rbdf->cbd_datlen, 0); /* rx length has no significance */
Christophe Leroy907208c2017-07-06 10:23:22 +0200205
Christophe Leroy4fb931e2024-04-12 19:36:19 +0200206 clrsetbits_be16(&cp->cp_spmode, ~(SPMODE_LOOP | SPMODE_PM(0xf) | SPMODE_DIV16),
207 SPMODE_REV | SPMODE_MSTR | SPMODE_EN | spmode_len);
Christophe Leroyba3da732017-07-06 10:33:13 +0200208 out_8(&cp->cp_spim, 0); /* Mask all SPI events */
209 out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
Christophe Leroy907208c2017-07-06 10:23:22 +0200210
211 /* start spi transfer */
Christophe Leroyba3da732017-07-06 10:33:13 +0200212 setbits_8(&cp->cp_spcom, SPI_STR); /* Start transmit */
Christophe Leroy907208c2017-07-06 10:23:22 +0200213
214 /* --------------------------------
215 * Wait for SPI transmit to get out
216 * or time out (1 second = 1000 ms)
217 * -------------------------------- */
Christophe Leroy70fd0712017-07-06 10:33:17 +0200218 for (tm = 0; tm < 1000; ++tm) {
Christophe Leroyba3da732017-07-06 10:33:13 +0200219 if (in_8(&cp->cp_spie) & SPI_TXB) /* Tx Buffer Empty */
Christophe Leroy907208c2017-07-06 10:23:22 +0200220 break;
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200221
Christophe Leroyba3da732017-07-06 10:33:13 +0200222 if ((in_be16(&tbdf->cbd_sc) & BD_SC_READY) == 0)
Christophe Leroy907208c2017-07-06 10:23:22 +0200223 break;
Christophe Leroy70fd0712017-07-06 10:33:17 +0200224 udelay(1000);
Christophe Leroy907208c2017-07-06 10:23:22 +0200225 }
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200226
Christophe Leroy70fd0712017-07-06 10:33:17 +0200227 if (tm >= 1000)
Christophe Leroy244f8462024-04-12 13:53:57 +0200228 return -ETIMEDOUT;
Christophe Leroy907208c2017-07-06 10:23:22 +0200229
Christophe Leroydff36802024-04-12 18:16:59 +0200230 if (!(count & 1) && count > MIN_HWORD_XFER) {
231 if (dout)
232 free(bufout);
233 if (din) {
234 int i;
235
236 bufout = malloc(count);
237 for (i = 0; i < count; i += 2)
238 *(u16 *)(din + i) = swab16(*(u16 *)(bufin + i));
239 free(bufin);
240 }
241 }
242
Christophe Leroy244f8462024-04-12 13:53:57 +0200243 return 0;
244}
245
246static int mpc8xx_spi_xfer(struct udevice *dev, unsigned int bitlen,
247 const void *dout, void *din, unsigned long flags)
248{
249 size_t count = (bitlen + 7) / 8;
250 size_t offset = 0;
251 int ret = 0;
252
253 if (!din && !dout)
254 return -EINVAL;
255
256 /* Set CS for device */
257 if (flags & SPI_XFER_BEGIN)
258 mpc8xx_spi_cs_activate(dev);
259
260 while (count > 0 && !ret) {
261 size_t chunk = min(count, (size_t)MAX_BUFFER);
262 const void *out = dout ? dout + offset : NULL;
263 void *in = din ? din + offset : NULL;
264
265 ret = mpc8xx_spi_xfer_one(dev, chunk, out, in);
266
267 offset += chunk;
268 count -= chunk;
269 }
Christophe Leroy907208c2017-07-06 10:23:22 +0200270 /* Clear CS for device */
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200271 if (flags & SPI_XFER_END)
272 mpc8xx_spi_cs_deactivate(dev);
Christophe Leroy907208c2017-07-06 10:23:22 +0200273
Christophe Leroy244f8462024-04-12 13:53:57 +0200274 if (ret)
275 printf("*** spi_xfer: Time out while xferring to/from SPI!\n");
276
277 return ret;
Christophe Leroy907208c2017-07-06 10:23:22 +0200278}
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000279
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200280static int mpc8xx_spi_ofdata_to_platdata(struct udevice *dev)
281{
282 struct mpc8xx_priv *priv = dev_get_priv(dev);
283 int ret;
284
285 ret = gpio_request_list_by_name(dev, "gpios", priv->gpios,
286 ARRAY_SIZE(priv->gpios), GPIOD_IS_OUT);
287 if (ret < 0)
288 return ret;
289
290 priv->max_cs = ret;
291
292 return 0;
293}
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000294static const struct dm_spi_ops mpc8xx_spi_ops = {
295 .xfer = mpc8xx_spi_xfer,
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200296 .set_speed = mpc8xx_spi_set_speed,
297 .set_mode = mpc8xx_spi_set_mode,
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000298};
299
300static const struct udevice_id mpc8xx_spi_ids[] = {
301 { .compatible = "fsl,mpc8xx-spi" },
302 { }
303};
304
305U_BOOT_DRIVER(mpc8xx_spi) = {
306 .name = "mpc8xx_spi",
307 .id = UCLASS_SPI,
308 .of_match = mpc8xx_spi_ids,
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200309 .of_to_plat = mpc8xx_spi_ofdata_to_platdata,
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000310 .ops = &mpc8xx_spi_ops,
311 .probe = mpc8xx_spi_probe,
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200312 .priv_auto = sizeof(struct mpc8xx_priv),
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000313};