blob: 5c8d760935141214fb73aed988c2ca86fcf940bf [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Christophe Leroy069fa832017-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 Leroy069fa832017-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 Leroy996f2352018-11-21 08:51:57 +000020#include <dm.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020021#include <mpc8xx.h>
Christophe Leroy996f2352018-11-21 08:51:57 +000022#include <spi.h>
Simon Glassdbd79542020-05-10 11:40:11 -060023#include <linux/delay.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020024
Christophe Leroy996f2352018-11-21 08:51:57 +000025#include <asm/cpm_8xx.h>
26#include <asm/io.h>
Christophe Leroyf4ced3c2022-10-14 09:14:44 +020027#include <asm/gpio.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020028
Christophe Leroy394f9b32017-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 Leroy069fa832017-07-06 10:23:22 +020032#define MAX_BUFFER 0x104
33
Christophe Leroyf4ced3c2022-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
40static int mpc8xx_spi_set_mode(struct udevice *dev, uint mod)
41{
42 return 0;
43}
44
45static int mpc8xx_spi_set_speed(struct udevice *dev, uint speed)
46{
47 return 0;
48}
49
Christophe Leroy996f2352018-11-21 08:51:57 +000050static int mpc8xx_spi_probe(struct udevice *dev)
Christophe Leroy069fa832017-07-06 10:23:22 +020051{
Christophe Leroy394f9b32017-07-06 10:33:13 +020052 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
53 cpm8xx_t __iomem *cp = &immr->im_cpm;
Christophe Leroye6050cc2023-05-03 10:31:19 +020054 spi_t __iomem *spi = (spi_t __iomem *)&cp->cp_dpmem[PROFF_SPI];
Christophe Leroybb32bfd2023-05-03 09:05:33 +020055 u16 spi_rpbase;
Christophe Leroy394f9b32017-07-06 10:33:13 +020056 cbd_t __iomem *tbdf, *rbdf;
Christophe Leroy069fa832017-07-06 10:23:22 +020057
Christophe Leroybb32bfd2023-05-03 09:05:33 +020058 spi_rpbase = in_be16(&spi->spi_rpbase);
59 if (spi_rpbase)
60 spi = (spi_t __iomem *)&cp->cp_dpmem[spi_rpbase];
Christophe Leroy069fa832017-07-06 10:23:22 +020061
62/* 1 */
Christophe Leroy069fa832017-07-06 10:23:22 +020063 /* Initialize the parameter ram.
64 * We need to make sure many things are initialized to zero
65 */
Christophe Leroy394f9b32017-07-06 10:33:13 +020066 out_be32(&spi->spi_rstate, 0);
67 out_be32(&spi->spi_rdp, 0);
68 out_be16(&spi->spi_rbptr, 0);
69 out_be16(&spi->spi_rbc, 0);
70 out_be32(&spi->spi_rxtmp, 0);
71 out_be32(&spi->spi_tstate, 0);
72 out_be32(&spi->spi_tdp, 0);
73 out_be16(&spi->spi_tbptr, 0);
74 out_be16(&spi->spi_tbc, 0);
75 out_be32(&spi->spi_txtmp, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +020076
77/* 3 */
78 /* Set up the SPI parameters in the parameter ram */
Christophe Leroy394f9b32017-07-06 10:33:13 +020079 out_be16(&spi->spi_rbase, CPM_SPI_BASE_RX);
80 out_be16(&spi->spi_tbase, CPM_SPI_BASE_TX);
Christophe Leroy069fa832017-07-06 10:23:22 +020081
82 /***********IMPORTANT******************/
83
84 /*
85 * Setting transmit and receive buffer descriptor pointers
86 * initially to rbase and tbase. Only the microcode patches
87 * documentation talks about initializing this pointer. This
88 * is missing from the sample I2C driver. If you dont
89 * initialize these pointers, the kernel hangs.
90 */
Christophe Leroy394f9b32017-07-06 10:33:13 +020091 out_be16(&spi->spi_rbptr, CPM_SPI_BASE_RX);
92 out_be16(&spi->spi_tbptr, CPM_SPI_BASE_TX);
Christophe Leroy069fa832017-07-06 10:23:22 +020093
94/* 4 */
95 /* Init SPI Tx + Rx Parameters */
Christophe Leroy394f9b32017-07-06 10:33:13 +020096 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
Christophe Leroy069fa832017-07-06 10:23:22 +020097 ;
Christophe Leroy394f9b32017-07-06 10:33:13 +020098
99 out_be16(&cp->cp_cpcr, mk_cr_cmd(CPM_CR_CH_SPI, CPM_CR_INIT_TRX) |
100 CPM_CR_FLG);
101 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
Christophe Leroy069fa832017-07-06 10:23:22 +0200102 ;
103
104/* 5 */
105 /* Set SDMA configuration register */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200106 out_be32(&immr->im_siu_conf.sc_sdcr, 0x0001);
Christophe Leroy069fa832017-07-06 10:23:22 +0200107
108/* 6 */
109 /* Set to big endian. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200110 out_8(&spi->spi_tfcr, SMC_EB);
111 out_8(&spi->spi_rfcr, SMC_EB);
Christophe Leroy069fa832017-07-06 10:23:22 +0200112
113/* 7 */
114 /* Set maximum receive size. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200115 out_be16(&spi->spi_mrblr, MAX_BUFFER);
Christophe Leroy069fa832017-07-06 10:23:22 +0200116
117/* 8 + 9 */
118 /* tx and rx buffer descriptors */
Christophe Leroy394f9b32017-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 Leroy069fa832017-07-06 10:23:22 +0200121
Christophe Leroy394f9b32017-07-06 10:33:13 +0200122 clrbits_be16(&tbdf->cbd_sc, BD_SC_READY);
123 clrbits_be16(&rbdf->cbd_sc, BD_SC_EMPTY);
Christophe Leroy069fa832017-07-06 10:23:22 +0200124
Christophe Leroy069fa832017-07-06 10:23:22 +0200125/* 10 + 11 */
Christophe Leroy394f9b32017-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 Leroy069fa832017-07-06 10:23:22 +0200128
Christophe Leroy996f2352018-11-21 08:51:57 +0000129 return 0;
Christophe Leroy069fa832017-07-06 10:23:22 +0200130}
131
Christophe Leroyf4ced3c2022-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 Leroy996f2352018-11-21 08:51:57 +0000148static int mpc8xx_spi_xfer(struct udevice *dev, unsigned int bitlen,
149 const void *dout, void *din, unsigned long flags)
Christophe Leroy069fa832017-07-06 10:23:22 +0200150{
Christophe Leroy394f9b32017-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 Leroy394f9b32017-07-06 10:33:13 +0200153 cbd_t __iomem *tbdf, *rbdf;
Christophe Leroy069fa832017-07-06 10:23:22 +0200154 int tm;
Christophe Leroy996f2352018-11-21 08:51:57 +0000155 size_t count = (bitlen + 7) / 8;
Christophe Leroy069fa832017-07-06 10:23:22 +0200156
Christophe Leroy996f2352018-11-21 08:51:57 +0000157 if (count > MAX_BUFFER)
158 return -EINVAL;
Christophe Leroy069fa832017-07-06 10:23:22 +0200159
Christophe Leroy394f9b32017-07-06 10:33:13 +0200160 tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
161 rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
Christophe Leroy069fa832017-07-06 10:23:22 +0200162
163 /* Set CS for device */
Christophe Leroyf4ced3c2022-10-14 09:14:44 +0200164 if (flags & SPI_XFER_BEGIN)
165 mpc8xx_spi_cs_activate(dev);
Christophe Leroy069fa832017-07-06 10:23:22 +0200166
167 /* Setting tx bd status and data length */
Christophe Leroy996f2352018-11-21 08:51:57 +0000168 out_be32(&tbdf->cbd_bufaddr, (ulong)dout);
Christophe Leroy394f9b32017-07-06 10:33:13 +0200169 out_be16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_LAST | BD_SC_WRAP);
170 out_be16(&tbdf->cbd_datlen, count);
Christophe Leroy069fa832017-07-06 10:23:22 +0200171
172 /* Setting rx bd status and data length */
Christophe Leroy996f2352018-11-21 08:51:57 +0000173 out_be32(&rbdf->cbd_bufaddr, (ulong)din);
Christophe Leroy394f9b32017-07-06 10:33:13 +0200174 out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
175 out_be16(&rbdf->cbd_datlen, 0); /* rx length has no significance */
Christophe Leroy069fa832017-07-06 10:23:22 +0200176
Christophe Leroy394f9b32017-07-06 10:33:13 +0200177 clrsetbits_be16(&cp->cp_spmode, ~SPMODE_LOOP, SPMODE_REV | SPMODE_MSTR |
178 SPMODE_EN | SPMODE_LEN(8) | SPMODE_PM(0x8));
179 out_8(&cp->cp_spim, 0); /* Mask all SPI events */
180 out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
Christophe Leroy069fa832017-07-06 10:23:22 +0200181
182 /* start spi transfer */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200183 setbits_8(&cp->cp_spcom, SPI_STR); /* Start transmit */
Christophe Leroy069fa832017-07-06 10:23:22 +0200184
185 /* --------------------------------
186 * Wait for SPI transmit to get out
187 * or time out (1 second = 1000 ms)
188 * -------------------------------- */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200189 for (tm = 0; tm < 1000; ++tm) {
Christophe Leroy394f9b32017-07-06 10:33:13 +0200190 if (in_8(&cp->cp_spie) & SPI_TXB) /* Tx Buffer Empty */
Christophe Leroy069fa832017-07-06 10:23:22 +0200191 break;
Christophe Leroyf4ced3c2022-10-14 09:14:44 +0200192
Christophe Leroy394f9b32017-07-06 10:33:13 +0200193 if ((in_be16(&tbdf->cbd_sc) & BD_SC_READY) == 0)
Christophe Leroy069fa832017-07-06 10:23:22 +0200194 break;
Christophe Leroy48f896d2017-07-06 10:33:17 +0200195 udelay(1000);
Christophe Leroy069fa832017-07-06 10:23:22 +0200196 }
Christophe Leroyf4ced3c2022-10-14 09:14:44 +0200197
Christophe Leroy48f896d2017-07-06 10:33:17 +0200198 if (tm >= 1000)
199 printf("*** spi_xfer: Time out while xferring to/from SPI!\n");
Christophe Leroy069fa832017-07-06 10:23:22 +0200200
201 /* Clear CS for device */
Christophe Leroyf4ced3c2022-10-14 09:14:44 +0200202 if (flags & SPI_XFER_END)
203 mpc8xx_spi_cs_deactivate(dev);
Christophe Leroy069fa832017-07-06 10:23:22 +0200204
Christophe Leroyf4ced3c2022-10-14 09:14:44 +0200205 return 0;
Christophe Leroy069fa832017-07-06 10:23:22 +0200206}
Christophe Leroy996f2352018-11-21 08:51:57 +0000207
Christophe Leroyf4ced3c2022-10-14 09:14:44 +0200208static int mpc8xx_spi_ofdata_to_platdata(struct udevice *dev)
209{
210 struct mpc8xx_priv *priv = dev_get_priv(dev);
211 int ret;
212
213 ret = gpio_request_list_by_name(dev, "gpios", priv->gpios,
214 ARRAY_SIZE(priv->gpios), GPIOD_IS_OUT);
215 if (ret < 0)
216 return ret;
217
218 priv->max_cs = ret;
219
220 return 0;
221}
Christophe Leroy996f2352018-11-21 08:51:57 +0000222static const struct dm_spi_ops mpc8xx_spi_ops = {
223 .xfer = mpc8xx_spi_xfer,
Christophe Leroyf4ced3c2022-10-14 09:14:44 +0200224 .set_speed = mpc8xx_spi_set_speed,
225 .set_mode = mpc8xx_spi_set_mode,
Christophe Leroy996f2352018-11-21 08:51:57 +0000226};
227
228static const struct udevice_id mpc8xx_spi_ids[] = {
229 { .compatible = "fsl,mpc8xx-spi" },
230 { }
231};
232
233U_BOOT_DRIVER(mpc8xx_spi) = {
234 .name = "mpc8xx_spi",
235 .id = UCLASS_SPI,
236 .of_match = mpc8xx_spi_ids,
Christophe Leroyf4ced3c2022-10-14 09:14:44 +0200237 .of_to_plat = mpc8xx_spi_ofdata_to_platdata,
Christophe Leroy996f2352018-11-21 08:51:57 +0000238 .ops = &mpc8xx_spi_ops,
239 .probe = mpc8xx_spi_probe,
Christophe Leroyf4ced3c2022-10-14 09:14:44 +0200240 .priv_auto = sizeof(struct mpc8xx_priv),
Christophe Leroy996f2352018-11-21 08:51:57 +0000241};