183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2f88c431bSChristophe Leroy /*
3f88c431bSChristophe Leroy * Copyright (c) 2001 Navin Boppuri / Prashant Patel
4f88c431bSChristophe Leroy * <nboppuri@trinetcommunication.com>,
5f88c431bSChristophe Leroy * <pmpatel@trinetcommunication.com>
6f88c431bSChristophe Leroy * Copyright (c) 2001 Gerd Mennchen <Gerd.Mennchen@icn.siemens.de>
7f88c431bSChristophe Leroy * Copyright (c) 2001 Wolfgang Denk, DENX Software Engineering, <wd@denx.de>.
8f88c431bSChristophe Leroy */
9f88c431bSChristophe Leroy
10f88c431bSChristophe Leroy /*
11f88c431bSChristophe Leroy * MPC8xx CPM SPI interface.
12f88c431bSChristophe Leroy *
13f88c431bSChristophe Leroy * Parts of this code are probably not portable and/or specific to
14f88c431bSChristophe Leroy * the board which I used for the tests. Please send fixes/complaints
15f88c431bSChristophe Leroy * to wd@denx.de
16f88c431bSChristophe Leroy *
17f88c431bSChristophe Leroy */
18f88c431bSChristophe Leroy
19f88c431bSChristophe Leroy #include <common.h>
20*fb0204e4SChristophe Leroy #include <dm.h>
21f88c431bSChristophe Leroy #include <mpc8xx.h>
22*fb0204e4SChristophe Leroy #include <spi.h>
23*fb0204e4SChristophe Leroy
2418f8d4c6SChristophe Leroy #include <asm/cpm_8xx.h>
25*fb0204e4SChristophe Leroy #include <asm/io.h>
26f88c431bSChristophe Leroy
27f88c431bSChristophe Leroy #define CPM_SPI_BASE_RX CPM_SPI_BASE
28f88c431bSChristophe Leroy #define CPM_SPI_BASE_TX (CPM_SPI_BASE + sizeof(cbd_t))
29f88c431bSChristophe Leroy
30f88c431bSChristophe Leroy #define MAX_BUFFER 0x104
31f88c431bSChristophe Leroy
mpc8xx_spi_probe(struct udevice * dev)32*fb0204e4SChristophe Leroy static int mpc8xx_spi_probe(struct udevice *dev)
33f88c431bSChristophe Leroy {
34f88c431bSChristophe Leroy immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
35f88c431bSChristophe Leroy cpm8xx_t __iomem *cp = &immr->im_cpm;
36f88c431bSChristophe Leroy spi_t __iomem *spi = (spi_t __iomem *)&cp->cp_dparam[PROFF_SPI];
37f88c431bSChristophe Leroy cbd_t __iomem *tbdf, *rbdf;
38f88c431bSChristophe Leroy
39f88c431bSChristophe Leroy /* Disable relocation */
40f88c431bSChristophe Leroy out_be16(&spi->spi_rpbase, 0);
41f88c431bSChristophe Leroy
42f88c431bSChristophe Leroy /* 1 */
43f88c431bSChristophe Leroy /* ------------------------------------------------
44f88c431bSChristophe Leroy * Initialize Port B SPI pins -> page 34-8 MPC860UM
45f88c431bSChristophe Leroy * (we are only in Master Mode !)
46f88c431bSChristophe Leroy * ------------------------------------------------ */
47f88c431bSChristophe Leroy
48f88c431bSChristophe Leroy /* --------------------------------------------
49f88c431bSChristophe Leroy * GPIO or per. Function
50f88c431bSChristophe Leroy * PBPAR[28] = 1 [0x00000008] -> PERI: (SPIMISO)
51f88c431bSChristophe Leroy * PBPAR[29] = 1 [0x00000004] -> PERI: (SPIMOSI)
52f88c431bSChristophe Leroy * PBPAR[30] = 1 [0x00000002] -> PERI: (SPICLK)
53f88c431bSChristophe Leroy * PBPAR[31] = 0 [0x00000001] -> GPIO: (CS for PCUE/CCM-EEPROM)
54f88c431bSChristophe Leroy * -------------------------------------------- */
55f88c431bSChristophe Leroy clrsetbits_be32(&cp->cp_pbpar, 0x00000001, 0x0000000E); /* set bits */
56f88c431bSChristophe Leroy
57f88c431bSChristophe Leroy /* ----------------------------------------------
58f88c431bSChristophe Leroy * In/Out or per. Function 0/1
59f88c431bSChristophe Leroy * PBDIR[28] = 1 [0x00000008] -> PERI1: SPIMISO
60f88c431bSChristophe Leroy * PBDIR[29] = 1 [0x00000004] -> PERI1: SPIMOSI
61f88c431bSChristophe Leroy * PBDIR[30] = 1 [0x00000002] -> PERI1: SPICLK
62f88c431bSChristophe Leroy * PBDIR[31] = 1 [0x00000001] -> GPIO OUT: CS for PCUE/CCM-EEPROM
63f88c431bSChristophe Leroy * ---------------------------------------------- */
64f88c431bSChristophe Leroy setbits_be32(&cp->cp_pbdir, 0x0000000F);
65f88c431bSChristophe Leroy
66f88c431bSChristophe Leroy /* ----------------------------------------------
67f88c431bSChristophe Leroy * open drain or active output
68f88c431bSChristophe Leroy * PBODR[28] = 1 [0x00000008] -> open drain: SPIMISO
69f88c431bSChristophe Leroy * PBODR[29] = 0 [0x00000004] -> active output SPIMOSI
70f88c431bSChristophe Leroy * PBODR[30] = 0 [0x00000002] -> active output: SPICLK
71f88c431bSChristophe Leroy * PBODR[31] = 0 [0x00000001] -> active output GPIO OUT: CS for PCUE/CCM
72f88c431bSChristophe Leroy * ---------------------------------------------- */
73f88c431bSChristophe Leroy
74f88c431bSChristophe Leroy clrsetbits_be16(&cp->cp_pbodr, 0x00000007, 0x00000008);
75f88c431bSChristophe Leroy
76f88c431bSChristophe Leroy /* Initialize the parameter ram.
77f88c431bSChristophe Leroy * We need to make sure many things are initialized to zero
78f88c431bSChristophe Leroy */
79f88c431bSChristophe Leroy out_be32(&spi->spi_rstate, 0);
80f88c431bSChristophe Leroy out_be32(&spi->spi_rdp, 0);
81f88c431bSChristophe Leroy out_be16(&spi->spi_rbptr, 0);
82f88c431bSChristophe Leroy out_be16(&spi->spi_rbc, 0);
83f88c431bSChristophe Leroy out_be32(&spi->spi_rxtmp, 0);
84f88c431bSChristophe Leroy out_be32(&spi->spi_tstate, 0);
85f88c431bSChristophe Leroy out_be32(&spi->spi_tdp, 0);
86f88c431bSChristophe Leroy out_be16(&spi->spi_tbptr, 0);
87f88c431bSChristophe Leroy out_be16(&spi->spi_tbc, 0);
88f88c431bSChristophe Leroy out_be32(&spi->spi_txtmp, 0);
89f88c431bSChristophe Leroy
90f88c431bSChristophe Leroy /* 3 */
91f88c431bSChristophe Leroy /* Set up the SPI parameters in the parameter ram */
92f88c431bSChristophe Leroy out_be16(&spi->spi_rbase, CPM_SPI_BASE_RX);
93f88c431bSChristophe Leroy out_be16(&spi->spi_tbase, CPM_SPI_BASE_TX);
94f88c431bSChristophe Leroy
95f88c431bSChristophe Leroy /***********IMPORTANT******************/
96f88c431bSChristophe Leroy
97f88c431bSChristophe Leroy /*
98f88c431bSChristophe Leroy * Setting transmit and receive buffer descriptor pointers
99f88c431bSChristophe Leroy * initially to rbase and tbase. Only the microcode patches
100f88c431bSChristophe Leroy * documentation talks about initializing this pointer. This
101f88c431bSChristophe Leroy * is missing from the sample I2C driver. If you dont
102f88c431bSChristophe Leroy * initialize these pointers, the kernel hangs.
103f88c431bSChristophe Leroy */
104f88c431bSChristophe Leroy out_be16(&spi->spi_rbptr, CPM_SPI_BASE_RX);
105f88c431bSChristophe Leroy out_be16(&spi->spi_tbptr, CPM_SPI_BASE_TX);
106f88c431bSChristophe Leroy
107f88c431bSChristophe Leroy /* 4 */
108f88c431bSChristophe Leroy /* Init SPI Tx + Rx Parameters */
109f88c431bSChristophe Leroy while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
110f88c431bSChristophe Leroy ;
111f88c431bSChristophe Leroy
112f88c431bSChristophe Leroy out_be16(&cp->cp_cpcr, mk_cr_cmd(CPM_CR_CH_SPI, CPM_CR_INIT_TRX) |
113f88c431bSChristophe Leroy CPM_CR_FLG);
114f88c431bSChristophe Leroy while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
115f88c431bSChristophe Leroy ;
116f88c431bSChristophe Leroy
117f88c431bSChristophe Leroy /* 5 */
118f88c431bSChristophe Leroy /* Set SDMA configuration register */
119f88c431bSChristophe Leroy out_be32(&immr->im_siu_conf.sc_sdcr, 0x0001);
120f88c431bSChristophe Leroy
121f88c431bSChristophe Leroy /* 6 */
122f88c431bSChristophe Leroy /* Set to big endian. */
123f88c431bSChristophe Leroy out_8(&spi->spi_tfcr, SMC_EB);
124f88c431bSChristophe Leroy out_8(&spi->spi_rfcr, SMC_EB);
125f88c431bSChristophe Leroy
126f88c431bSChristophe Leroy /* 7 */
127f88c431bSChristophe Leroy /* Set maximum receive size. */
128f88c431bSChristophe Leroy out_be16(&spi->spi_mrblr, MAX_BUFFER);
129f88c431bSChristophe Leroy
130f88c431bSChristophe Leroy /* 8 + 9 */
131f88c431bSChristophe Leroy /* tx and rx buffer descriptors */
132f88c431bSChristophe Leroy tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
133f88c431bSChristophe Leroy rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
134f88c431bSChristophe Leroy
135f88c431bSChristophe Leroy clrbits_be16(&tbdf->cbd_sc, BD_SC_READY);
136f88c431bSChristophe Leroy clrbits_be16(&rbdf->cbd_sc, BD_SC_EMPTY);
137f88c431bSChristophe Leroy
138f88c431bSChristophe Leroy /* 10 + 11 */
139f88c431bSChristophe Leroy out_8(&cp->cp_spim, 0); /* Mask all SPI events */
140f88c431bSChristophe Leroy out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
141f88c431bSChristophe Leroy
142*fb0204e4SChristophe Leroy return 0;
143f88c431bSChristophe Leroy }
144f88c431bSChristophe Leroy
mpc8xx_spi_xfer(struct udevice * dev,unsigned int bitlen,const void * dout,void * din,unsigned long flags)145*fb0204e4SChristophe Leroy static int mpc8xx_spi_xfer(struct udevice *dev, unsigned int bitlen,
146*fb0204e4SChristophe Leroy const void *dout, void *din, unsigned long flags)
147f88c431bSChristophe Leroy {
148f88c431bSChristophe Leroy immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
149f88c431bSChristophe Leroy cpm8xx_t __iomem *cp = &immr->im_cpm;
150f88c431bSChristophe Leroy cbd_t __iomem *tbdf, *rbdf;
151f88c431bSChristophe Leroy int tm;
152*fb0204e4SChristophe Leroy size_t count = (bitlen + 7) / 8;
153f88c431bSChristophe Leroy
154*fb0204e4SChristophe Leroy if (count > MAX_BUFFER)
155*fb0204e4SChristophe Leroy return -EINVAL;
156f88c431bSChristophe Leroy
157f88c431bSChristophe Leroy tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
158f88c431bSChristophe Leroy rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
159f88c431bSChristophe Leroy
160f88c431bSChristophe Leroy /* Set CS for device */
161f88c431bSChristophe Leroy clrbits_be32(&cp->cp_pbdat, 0x0001);
162f88c431bSChristophe Leroy
163f88c431bSChristophe Leroy /* Setting tx bd status and data length */
164*fb0204e4SChristophe Leroy out_be32(&tbdf->cbd_bufaddr, (ulong)dout);
165f88c431bSChristophe Leroy out_be16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_LAST | BD_SC_WRAP);
166f88c431bSChristophe Leroy out_be16(&tbdf->cbd_datlen, count);
167f88c431bSChristophe Leroy
168f88c431bSChristophe Leroy /* Setting rx bd status and data length */
169*fb0204e4SChristophe Leroy out_be32(&rbdf->cbd_bufaddr, (ulong)din);
170f88c431bSChristophe Leroy out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
171f88c431bSChristophe Leroy out_be16(&rbdf->cbd_datlen, 0); /* rx length has no significance */
172f88c431bSChristophe Leroy
173f88c431bSChristophe Leroy clrsetbits_be16(&cp->cp_spmode, ~SPMODE_LOOP, SPMODE_REV | SPMODE_MSTR |
174f88c431bSChristophe Leroy SPMODE_EN | SPMODE_LEN(8) | SPMODE_PM(0x8));
175f88c431bSChristophe Leroy out_8(&cp->cp_spim, 0); /* Mask all SPI events */
176f88c431bSChristophe Leroy out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
177f88c431bSChristophe Leroy
178f88c431bSChristophe Leroy /* start spi transfer */
179f88c431bSChristophe Leroy setbits_8(&cp->cp_spcom, SPI_STR); /* Start transmit */
180f88c431bSChristophe Leroy
181f88c431bSChristophe Leroy /* --------------------------------
182f88c431bSChristophe Leroy * Wait for SPI transmit to get out
183f88c431bSChristophe Leroy * or time out (1 second = 1000 ms)
184f88c431bSChristophe Leroy * -------------------------------- */
185f88c431bSChristophe Leroy for (tm = 0; tm < 1000; ++tm) {
186f88c431bSChristophe Leroy if (in_8(&cp->cp_spie) & SPI_TXB) /* Tx Buffer Empty */
187f88c431bSChristophe Leroy break;
188f88c431bSChristophe Leroy if ((in_be16(&tbdf->cbd_sc) & BD_SC_READY) == 0)
189f88c431bSChristophe Leroy break;
190f88c431bSChristophe Leroy udelay(1000);
191f88c431bSChristophe Leroy }
192f88c431bSChristophe Leroy if (tm >= 1000)
193f88c431bSChristophe Leroy printf("*** spi_xfer: Time out while xferring to/from SPI!\n");
194f88c431bSChristophe Leroy
195f88c431bSChristophe Leroy /* Clear CS for device */
196f88c431bSChristophe Leroy setbits_be32(&cp->cp_pbdat, 0x0001);
197f88c431bSChristophe Leroy
198f88c431bSChristophe Leroy return count;
199f88c431bSChristophe Leroy }
200*fb0204e4SChristophe Leroy
201*fb0204e4SChristophe Leroy static const struct dm_spi_ops mpc8xx_spi_ops = {
202*fb0204e4SChristophe Leroy .xfer = mpc8xx_spi_xfer,
203*fb0204e4SChristophe Leroy };
204*fb0204e4SChristophe Leroy
205*fb0204e4SChristophe Leroy static const struct udevice_id mpc8xx_spi_ids[] = {
206*fb0204e4SChristophe Leroy { .compatible = "fsl,mpc8xx-spi" },
207*fb0204e4SChristophe Leroy { }
208*fb0204e4SChristophe Leroy };
209*fb0204e4SChristophe Leroy
210*fb0204e4SChristophe Leroy U_BOOT_DRIVER(mpc8xx_spi) = {
211*fb0204e4SChristophe Leroy .name = "mpc8xx_spi",
212*fb0204e4SChristophe Leroy .id = UCLASS_SPI,
213*fb0204e4SChristophe Leroy .of_match = mpc8xx_spi_ids,
214*fb0204e4SChristophe Leroy .ops = &mpc8xx_spi_ops,
215*fb0204e4SChristophe Leroy .probe = mpc8xx_spi_probe,
216*fb0204e4SChristophe Leroy };
217