blob: a193ac711b44e3c6257739765f9dba48f47cda45 [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 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 Leroy907208c2017-07-06 10:23:22 +020033
Christophe Leroy773ad4e2022-10-14 09:14:44 +020034struct mpc8xx_priv {
35 spi_t __iomem *spi;
36 struct gpio_desc gpios[16];
37 int max_cs;
38};
39
Christophe Leroyea208202024-04-09 08:38:08 +020040static char dummy_buffer[MAX_BUFFER];
41
Christophe Leroy773ad4e2022-10-14 09:14:44 +020042static int mpc8xx_spi_set_mode(struct udevice *dev, uint mod)
43{
44 return 0;
45}
46
47static int mpc8xx_spi_set_speed(struct udevice *dev, uint speed)
48{
49 return 0;
50}
51
Christophe Leroyfb0204e2018-11-21 08:51:57 +000052static int mpc8xx_spi_probe(struct udevice *dev)
Christophe Leroy907208c2017-07-06 10:23:22 +020053{
Christophe Leroyba3da732017-07-06 10:33:13 +020054 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
55 cpm8xx_t __iomem *cp = &immr->im_cpm;
Christophe Leroyfdd243d2023-05-03 10:31:19 +020056 spi_t __iomem *spi = (spi_t __iomem *)&cp->cp_dpmem[PROFF_SPI];
Christophe Leroy14e64c12023-05-03 09:05:33 +020057 u16 spi_rpbase;
Christophe Leroyba3da732017-07-06 10:33:13 +020058 cbd_t __iomem *tbdf, *rbdf;
Christophe Leroy907208c2017-07-06 10:23:22 +020059
Christophe Leroy14e64c12023-05-03 09:05:33 +020060 spi_rpbase = in_be16(&spi->spi_rpbase);
61 if (spi_rpbase)
62 spi = (spi_t __iomem *)&cp->cp_dpmem[spi_rpbase];
Christophe Leroy907208c2017-07-06 10:23:22 +020063
64/* 1 */
Christophe Leroy907208c2017-07-06 10:23:22 +020065 /* Initialize the parameter ram.
66 * We need to make sure many things are initialized to zero
67 */
Christophe Leroyba3da732017-07-06 10:33:13 +020068 out_be32(&spi->spi_rstate, 0);
69 out_be32(&spi->spi_rdp, 0);
70 out_be16(&spi->spi_rbptr, 0);
71 out_be16(&spi->spi_rbc, 0);
72 out_be32(&spi->spi_rxtmp, 0);
73 out_be32(&spi->spi_tstate, 0);
74 out_be32(&spi->spi_tdp, 0);
75 out_be16(&spi->spi_tbptr, 0);
76 out_be16(&spi->spi_tbc, 0);
77 out_be32(&spi->spi_txtmp, 0);
Christophe Leroy907208c2017-07-06 10:23:22 +020078
79/* 3 */
80 /* Set up the SPI parameters in the parameter ram */
Christophe Leroyba3da732017-07-06 10:33:13 +020081 out_be16(&spi->spi_rbase, CPM_SPI_BASE_RX);
82 out_be16(&spi->spi_tbase, CPM_SPI_BASE_TX);
Christophe Leroy907208c2017-07-06 10:23:22 +020083
84 /***********IMPORTANT******************/
85
86 /*
87 * Setting transmit and receive buffer descriptor pointers
88 * initially to rbase and tbase. Only the microcode patches
89 * documentation talks about initializing this pointer. This
90 * is missing from the sample I2C driver. If you dont
91 * initialize these pointers, the kernel hangs.
92 */
Christophe Leroyba3da732017-07-06 10:33:13 +020093 out_be16(&spi->spi_rbptr, CPM_SPI_BASE_RX);
94 out_be16(&spi->spi_tbptr, CPM_SPI_BASE_TX);
Christophe Leroy907208c2017-07-06 10:23:22 +020095
96/* 4 */
97 /* Init SPI Tx + Rx Parameters */
Christophe Leroyba3da732017-07-06 10:33:13 +020098 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
Christophe Leroy907208c2017-07-06 10:23:22 +020099 ;
Christophe Leroyba3da732017-07-06 10:33:13 +0200100
101 out_be16(&cp->cp_cpcr, mk_cr_cmd(CPM_CR_CH_SPI, CPM_CR_INIT_TRX) |
102 CPM_CR_FLG);
103 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
Christophe Leroy907208c2017-07-06 10:23:22 +0200104 ;
105
Christophe Leroy907208c2017-07-06 10:23:22 +0200106/* 6 */
107 /* Set to big endian. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200108 out_8(&spi->spi_tfcr, SMC_EB);
109 out_8(&spi->spi_rfcr, SMC_EB);
Christophe Leroy907208c2017-07-06 10:23:22 +0200110
111/* 7 */
112 /* Set maximum receive size. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200113 out_be16(&spi->spi_mrblr, MAX_BUFFER);
Christophe Leroy907208c2017-07-06 10:23:22 +0200114
115/* 8 + 9 */
116 /* tx and rx buffer descriptors */
Christophe Leroyba3da732017-07-06 10:33:13 +0200117 tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
118 rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
Christophe Leroy907208c2017-07-06 10:23:22 +0200119
Christophe Leroyba3da732017-07-06 10:33:13 +0200120 clrbits_be16(&tbdf->cbd_sc, BD_SC_READY);
121 clrbits_be16(&rbdf->cbd_sc, BD_SC_EMPTY);
Christophe Leroy907208c2017-07-06 10:23:22 +0200122
Christophe Leroy907208c2017-07-06 10:23:22 +0200123/* 10 + 11 */
Christophe Leroyba3da732017-07-06 10:33:13 +0200124 out_8(&cp->cp_spim, 0); /* Mask all SPI events */
125 out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
Christophe Leroy907208c2017-07-06 10:23:22 +0200126
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000127 return 0;
Christophe Leroy907208c2017-07-06 10:23:22 +0200128}
129
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200130static void mpc8xx_spi_cs_activate(struct udevice *dev)
131{
132 struct mpc8xx_priv *priv = dev_get_priv(dev->parent);
133 struct dm_spi_slave_plat *platdata = dev_get_parent_plat(dev);
134
135 dm_gpio_set_value(&priv->gpios[platdata->cs], 1);
136}
137
138static void mpc8xx_spi_cs_deactivate(struct udevice *dev)
139{
140 struct mpc8xx_priv *priv = dev_get_priv(dev->parent);
141 struct dm_spi_slave_plat *platdata = dev_get_parent_plat(dev);
142
143 dm_gpio_set_value(&priv->gpios[platdata->cs], 0);
144}
145
Christophe Leroy244f8462024-04-12 13:53:57 +0200146static int mpc8xx_spi_xfer_one(struct udevice *dev, size_t count,
147 const void *dout, void *din)
Christophe Leroy907208c2017-07-06 10:23:22 +0200148{
Christophe Leroyba3da732017-07-06 10:33:13 +0200149 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
150 cpm8xx_t __iomem *cp = &immr->im_cpm;
Christophe Leroyba3da732017-07-06 10:33:13 +0200151 cbd_t __iomem *tbdf, *rbdf;
Christophe Leroy907208c2017-07-06 10:23:22 +0200152 int tm;
Christophe Leroy907208c2017-07-06 10:23:22 +0200153
Christophe Leroyba3da732017-07-06 10:33:13 +0200154 tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
155 rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
Christophe Leroy907208c2017-07-06 10:23:22 +0200156
Christophe Leroy907208c2017-07-06 10:23:22 +0200157 /* Setting tx bd status and data length */
Christophe Leroyea208202024-04-09 08:38:08 +0200158 out_be32(&tbdf->cbd_bufaddr, dout ? (ulong)dout : (ulong)dummy_buffer);
Christophe Leroyba3da732017-07-06 10:33:13 +0200159 out_be16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_LAST | BD_SC_WRAP);
160 out_be16(&tbdf->cbd_datlen, count);
Christophe Leroy907208c2017-07-06 10:23:22 +0200161
162 /* Setting rx bd status and data length */
Christophe Leroyea208202024-04-09 08:38:08 +0200163 out_be32(&rbdf->cbd_bufaddr, din ? (ulong)din : (ulong)dummy_buffer);
Christophe Leroyba3da732017-07-06 10:33:13 +0200164 out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
165 out_be16(&rbdf->cbd_datlen, 0); /* rx length has no significance */
Christophe Leroy907208c2017-07-06 10:23:22 +0200166
Christophe Leroyba3da732017-07-06 10:33:13 +0200167 clrsetbits_be16(&cp->cp_spmode, ~SPMODE_LOOP, SPMODE_REV | SPMODE_MSTR |
168 SPMODE_EN | SPMODE_LEN(8) | SPMODE_PM(0x8));
169 out_8(&cp->cp_spim, 0); /* Mask all SPI events */
170 out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
Christophe Leroy907208c2017-07-06 10:23:22 +0200171
172 /* start spi transfer */
Christophe Leroyba3da732017-07-06 10:33:13 +0200173 setbits_8(&cp->cp_spcom, SPI_STR); /* Start transmit */
Christophe Leroy907208c2017-07-06 10:23:22 +0200174
175 /* --------------------------------
176 * Wait for SPI transmit to get out
177 * or time out (1 second = 1000 ms)
178 * -------------------------------- */
Christophe Leroy70fd0712017-07-06 10:33:17 +0200179 for (tm = 0; tm < 1000; ++tm) {
Christophe Leroyba3da732017-07-06 10:33:13 +0200180 if (in_8(&cp->cp_spie) & SPI_TXB) /* Tx Buffer Empty */
Christophe Leroy907208c2017-07-06 10:23:22 +0200181 break;
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200182
Christophe Leroyba3da732017-07-06 10:33:13 +0200183 if ((in_be16(&tbdf->cbd_sc) & BD_SC_READY) == 0)
Christophe Leroy907208c2017-07-06 10:23:22 +0200184 break;
Christophe Leroy70fd0712017-07-06 10:33:17 +0200185 udelay(1000);
Christophe Leroy907208c2017-07-06 10:23:22 +0200186 }
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200187
Christophe Leroy70fd0712017-07-06 10:33:17 +0200188 if (tm >= 1000)
Christophe Leroy244f8462024-04-12 13:53:57 +0200189 return -ETIMEDOUT;
Christophe Leroy907208c2017-07-06 10:23:22 +0200190
Christophe Leroy244f8462024-04-12 13:53:57 +0200191 return 0;
192}
193
194static int mpc8xx_spi_xfer(struct udevice *dev, unsigned int bitlen,
195 const void *dout, void *din, unsigned long flags)
196{
197 size_t count = (bitlen + 7) / 8;
198 size_t offset = 0;
199 int ret = 0;
200
201 if (!din && !dout)
202 return -EINVAL;
203
204 /* Set CS for device */
205 if (flags & SPI_XFER_BEGIN)
206 mpc8xx_spi_cs_activate(dev);
207
208 while (count > 0 && !ret) {
209 size_t chunk = min(count, (size_t)MAX_BUFFER);
210 const void *out = dout ? dout + offset : NULL;
211 void *in = din ? din + offset : NULL;
212
213 ret = mpc8xx_spi_xfer_one(dev, chunk, out, in);
214
215 offset += chunk;
216 count -= chunk;
217 }
Christophe Leroy907208c2017-07-06 10:23:22 +0200218 /* Clear CS for device */
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200219 if (flags & SPI_XFER_END)
220 mpc8xx_spi_cs_deactivate(dev);
Christophe Leroy907208c2017-07-06 10:23:22 +0200221
Christophe Leroy244f8462024-04-12 13:53:57 +0200222 if (ret)
223 printf("*** spi_xfer: Time out while xferring to/from SPI!\n");
224
225 return ret;
Christophe Leroy907208c2017-07-06 10:23:22 +0200226}
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000227
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200228static int mpc8xx_spi_ofdata_to_platdata(struct udevice *dev)
229{
230 struct mpc8xx_priv *priv = dev_get_priv(dev);
231 int ret;
232
233 ret = gpio_request_list_by_name(dev, "gpios", priv->gpios,
234 ARRAY_SIZE(priv->gpios), GPIOD_IS_OUT);
235 if (ret < 0)
236 return ret;
237
238 priv->max_cs = ret;
239
240 return 0;
241}
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000242static const struct dm_spi_ops mpc8xx_spi_ops = {
243 .xfer = mpc8xx_spi_xfer,
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200244 .set_speed = mpc8xx_spi_set_speed,
245 .set_mode = mpc8xx_spi_set_mode,
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000246};
247
248static const struct udevice_id mpc8xx_spi_ids[] = {
249 { .compatible = "fsl,mpc8xx-spi" },
250 { }
251};
252
253U_BOOT_DRIVER(mpc8xx_spi) = {
254 .name = "mpc8xx_spi",
255 .id = UCLASS_SPI,
256 .of_match = mpc8xx_spi_ids,
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200257 .of_to_plat = mpc8xx_spi_ofdata_to_platdata,
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000258 .ops = &mpc8xx_spi_ops,
259 .probe = mpc8xx_spi_probe,
Christophe Leroy773ad4e2022-10-14 09:14:44 +0200260 .priv_auto = sizeof(struct mpc8xx_priv),
Christophe Leroyfb0204e2018-11-21 08:51:57 +0000261};