blob: b020ce2b9d77fa905ee0212ca8556bb75b92fa4c [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>
Christophe Leroy069fa832017-07-06 10:23:22 +020023
Christophe Leroy996f2352018-11-21 08:51:57 +000024#include <asm/cpm_8xx.h>
25#include <asm/io.h>
Christophe Leroy069fa832017-07-06 10:23:22 +020026
Christophe Leroy394f9b32017-07-06 10:33:13 +020027#define CPM_SPI_BASE_RX CPM_SPI_BASE
28#define CPM_SPI_BASE_TX (CPM_SPI_BASE + sizeof(cbd_t))
29
Christophe Leroy069fa832017-07-06 10:23:22 +020030#define MAX_BUFFER 0x104
31
Christophe Leroy996f2352018-11-21 08:51:57 +000032static int mpc8xx_spi_probe(struct udevice *dev)
Christophe Leroy069fa832017-07-06 10:23:22 +020033{
Christophe Leroy394f9b32017-07-06 10:33:13 +020034 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
35 cpm8xx_t __iomem *cp = &immr->im_cpm;
36 spi_t __iomem *spi = (spi_t __iomem *)&cp->cp_dparam[PROFF_SPI];
37 cbd_t __iomem *tbdf, *rbdf;
Christophe Leroy069fa832017-07-06 10:23:22 +020038
Christophe Leroy069fa832017-07-06 10:23:22 +020039 /* Disable relocation */
Christophe Leroy394f9b32017-07-06 10:33:13 +020040 out_be16(&spi->spi_rpbase, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +020041
42/* 1 */
43 /* ------------------------------------------------
44 * Initialize Port B SPI pins -> page 34-8 MPC860UM
45 * (we are only in Master Mode !)
46 * ------------------------------------------------ */
47
48 /* --------------------------------------------
49 * GPIO or per. Function
50 * PBPAR[28] = 1 [0x00000008] -> PERI: (SPIMISO)
51 * PBPAR[29] = 1 [0x00000004] -> PERI: (SPIMOSI)
52 * PBPAR[30] = 1 [0x00000002] -> PERI: (SPICLK)
53 * PBPAR[31] = 0 [0x00000001] -> GPIO: (CS for PCUE/CCM-EEPROM)
54 * -------------------------------------------- */
Christophe Leroy394f9b32017-07-06 10:33:13 +020055 clrsetbits_be32(&cp->cp_pbpar, 0x00000001, 0x0000000E); /* set bits */
Christophe Leroy069fa832017-07-06 10:23:22 +020056
57 /* ----------------------------------------------
58 * In/Out or per. Function 0/1
59 * PBDIR[28] = 1 [0x00000008] -> PERI1: SPIMISO
60 * PBDIR[29] = 1 [0x00000004] -> PERI1: SPIMOSI
61 * PBDIR[30] = 1 [0x00000002] -> PERI1: SPICLK
62 * PBDIR[31] = 1 [0x00000001] -> GPIO OUT: CS for PCUE/CCM-EEPROM
63 * ---------------------------------------------- */
Christophe Leroy394f9b32017-07-06 10:33:13 +020064 setbits_be32(&cp->cp_pbdir, 0x0000000F);
Christophe Leroy069fa832017-07-06 10:23:22 +020065
66 /* ----------------------------------------------
67 * open drain or active output
68 * PBODR[28] = 1 [0x00000008] -> open drain: SPIMISO
69 * PBODR[29] = 0 [0x00000004] -> active output SPIMOSI
70 * PBODR[30] = 0 [0x00000002] -> active output: SPICLK
Christophe Leroy48f896d2017-07-06 10:33:17 +020071 * PBODR[31] = 0 [0x00000001] -> active output GPIO OUT: CS for PCUE/CCM
Christophe Leroy069fa832017-07-06 10:23:22 +020072 * ---------------------------------------------- */
73
Christophe Leroy394f9b32017-07-06 10:33:13 +020074 clrsetbits_be16(&cp->cp_pbodr, 0x00000007, 0x00000008);
Christophe Leroy069fa832017-07-06 10:23:22 +020075
76 /* Initialize the parameter ram.
77 * We need to make sure many things are initialized to zero
78 */
Christophe Leroy394f9b32017-07-06 10:33:13 +020079 out_be32(&spi->spi_rstate, 0);
80 out_be32(&spi->spi_rdp, 0);
81 out_be16(&spi->spi_rbptr, 0);
82 out_be16(&spi->spi_rbc, 0);
83 out_be32(&spi->spi_rxtmp, 0);
84 out_be32(&spi->spi_tstate, 0);
85 out_be32(&spi->spi_tdp, 0);
86 out_be16(&spi->spi_tbptr, 0);
87 out_be16(&spi->spi_tbc, 0);
88 out_be32(&spi->spi_txtmp, 0);
Christophe Leroy069fa832017-07-06 10:23:22 +020089
90/* 3 */
91 /* Set up the SPI parameters in the parameter ram */
Christophe Leroy394f9b32017-07-06 10:33:13 +020092 out_be16(&spi->spi_rbase, CPM_SPI_BASE_RX);
93 out_be16(&spi->spi_tbase, CPM_SPI_BASE_TX);
Christophe Leroy069fa832017-07-06 10:23:22 +020094
95 /***********IMPORTANT******************/
96
97 /*
98 * Setting transmit and receive buffer descriptor pointers
99 * initially to rbase and tbase. Only the microcode patches
100 * documentation talks about initializing this pointer. This
101 * is missing from the sample I2C driver. If you dont
102 * initialize these pointers, the kernel hangs.
103 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200104 out_be16(&spi->spi_rbptr, CPM_SPI_BASE_RX);
105 out_be16(&spi->spi_tbptr, CPM_SPI_BASE_TX);
Christophe Leroy069fa832017-07-06 10:23:22 +0200106
107/* 4 */
108 /* Init SPI Tx + Rx Parameters */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200109 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
Christophe Leroy069fa832017-07-06 10:23:22 +0200110 ;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200111
112 out_be16(&cp->cp_cpcr, mk_cr_cmd(CPM_CR_CH_SPI, CPM_CR_INIT_TRX) |
113 CPM_CR_FLG);
114 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
Christophe Leroy069fa832017-07-06 10:23:22 +0200115 ;
116
117/* 5 */
118 /* Set SDMA configuration register */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200119 out_be32(&immr->im_siu_conf.sc_sdcr, 0x0001);
Christophe Leroy069fa832017-07-06 10:23:22 +0200120
121/* 6 */
122 /* Set to big endian. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200123 out_8(&spi->spi_tfcr, SMC_EB);
124 out_8(&spi->spi_rfcr, SMC_EB);
Christophe Leroy069fa832017-07-06 10:23:22 +0200125
126/* 7 */
127 /* Set maximum receive size. */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200128 out_be16(&spi->spi_mrblr, MAX_BUFFER);
Christophe Leroy069fa832017-07-06 10:23:22 +0200129
130/* 8 + 9 */
131 /* tx and rx buffer descriptors */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200132 tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
133 rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
Christophe Leroy069fa832017-07-06 10:23:22 +0200134
Christophe Leroy394f9b32017-07-06 10:33:13 +0200135 clrbits_be16(&tbdf->cbd_sc, BD_SC_READY);
136 clrbits_be16(&rbdf->cbd_sc, BD_SC_EMPTY);
Christophe Leroy069fa832017-07-06 10:23:22 +0200137
Christophe Leroy069fa832017-07-06 10:23:22 +0200138/* 10 + 11 */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200139 out_8(&cp->cp_spim, 0); /* Mask all SPI events */
140 out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
Christophe Leroy069fa832017-07-06 10:23:22 +0200141
Christophe Leroy996f2352018-11-21 08:51:57 +0000142 return 0;
Christophe Leroy069fa832017-07-06 10:23:22 +0200143}
144
Christophe Leroy996f2352018-11-21 08:51:57 +0000145static int mpc8xx_spi_xfer(struct udevice *dev, unsigned int bitlen,
146 const void *dout, void *din, unsigned long flags)
Christophe Leroy069fa832017-07-06 10:23:22 +0200147{
Christophe Leroy394f9b32017-07-06 10:33:13 +0200148 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
149 cpm8xx_t __iomem *cp = &immr->im_cpm;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200150 cbd_t __iomem *tbdf, *rbdf;
Christophe Leroy069fa832017-07-06 10:23:22 +0200151 int tm;
Christophe Leroy996f2352018-11-21 08:51:57 +0000152 size_t count = (bitlen + 7) / 8;
Christophe Leroy069fa832017-07-06 10:23:22 +0200153
Christophe Leroy996f2352018-11-21 08:51:57 +0000154 if (count > MAX_BUFFER)
155 return -EINVAL;
Christophe Leroy069fa832017-07-06 10:23:22 +0200156
Christophe Leroy394f9b32017-07-06 10:33:13 +0200157 tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
158 rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
Christophe Leroy069fa832017-07-06 10:23:22 +0200159
160 /* Set CS for device */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200161 clrbits_be32(&cp->cp_pbdat, 0x0001);
Christophe Leroy069fa832017-07-06 10:23:22 +0200162
163 /* Setting tx bd status and data length */
Christophe Leroy996f2352018-11-21 08:51:57 +0000164 out_be32(&tbdf->cbd_bufaddr, (ulong)dout);
Christophe Leroy394f9b32017-07-06 10:33:13 +0200165 out_be16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_LAST | BD_SC_WRAP);
166 out_be16(&tbdf->cbd_datlen, count);
Christophe Leroy069fa832017-07-06 10:23:22 +0200167
168 /* Setting rx bd status and data length */
Christophe Leroy996f2352018-11-21 08:51:57 +0000169 out_be32(&rbdf->cbd_bufaddr, (ulong)din);
Christophe Leroy394f9b32017-07-06 10:33:13 +0200170 out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
171 out_be16(&rbdf->cbd_datlen, 0); /* rx length has no significance */
Christophe Leroy069fa832017-07-06 10:23:22 +0200172
Christophe Leroy394f9b32017-07-06 10:33:13 +0200173 clrsetbits_be16(&cp->cp_spmode, ~SPMODE_LOOP, SPMODE_REV | SPMODE_MSTR |
174 SPMODE_EN | SPMODE_LEN(8) | SPMODE_PM(0x8));
175 out_8(&cp->cp_spim, 0); /* Mask all SPI events */
176 out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
Christophe Leroy069fa832017-07-06 10:23:22 +0200177
178 /* start spi transfer */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200179 setbits_8(&cp->cp_spcom, SPI_STR); /* Start transmit */
Christophe Leroy069fa832017-07-06 10:23:22 +0200180
181 /* --------------------------------
182 * Wait for SPI transmit to get out
183 * or time out (1 second = 1000 ms)
184 * -------------------------------- */
Christophe Leroy48f896d2017-07-06 10:33:17 +0200185 for (tm = 0; tm < 1000; ++tm) {
Christophe Leroy394f9b32017-07-06 10:33:13 +0200186 if (in_8(&cp->cp_spie) & SPI_TXB) /* Tx Buffer Empty */
Christophe Leroy069fa832017-07-06 10:23:22 +0200187 break;
Christophe Leroy394f9b32017-07-06 10:33:13 +0200188 if ((in_be16(&tbdf->cbd_sc) & BD_SC_READY) == 0)
Christophe Leroy069fa832017-07-06 10:23:22 +0200189 break;
Christophe Leroy48f896d2017-07-06 10:33:17 +0200190 udelay(1000);
Christophe Leroy069fa832017-07-06 10:23:22 +0200191 }
Christophe Leroy48f896d2017-07-06 10:33:17 +0200192 if (tm >= 1000)
193 printf("*** spi_xfer: Time out while xferring to/from SPI!\n");
Christophe Leroy069fa832017-07-06 10:23:22 +0200194
195 /* Clear CS for device */
Christophe Leroy394f9b32017-07-06 10:33:13 +0200196 setbits_be32(&cp->cp_pbdat, 0x0001);
Christophe Leroy069fa832017-07-06 10:23:22 +0200197
198 return count;
199}
Christophe Leroy996f2352018-11-21 08:51:57 +0000200
201static const struct dm_spi_ops mpc8xx_spi_ops = {
202 .xfer = mpc8xx_spi_xfer,
203};
204
205static const struct udevice_id mpc8xx_spi_ids[] = {
206 { .compatible = "fsl,mpc8xx-spi" },
207 { }
208};
209
210U_BOOT_DRIVER(mpc8xx_spi) = {
211 .name = "mpc8xx_spi",
212 .id = UCLASS_SPI,
213 .of_match = mpc8xx_spi_ids,
214 .ops = &mpc8xx_spi_ops,
215 .probe = mpc8xx_spi_probe,
216};