blob: e7d197f968c0446879cc5f7f7a4fac8dae3f5834 [file] [log] [blame]
Christophe Leroy907208c2017-07-06 10:23:22 +02001/*
2 * Copyright (c) 2001 Navin Boppuri / Prashant Patel
3 * <nboppuri@trinetcommunication.com>,
4 * <pmpatel@trinetcommunication.com>
5 * Copyright (c) 2001 Gerd Mennchen <Gerd.Mennchen@icn.siemens.de>
6 * Copyright (c) 2001 Wolfgang Denk, DENX Software Engineering, <wd@denx.de>.
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 */
10
11/*
12 * MPC8xx CPM SPI interface.
13 *
14 * Parts of this code are probably not portable and/or specific to
15 * the board which I used for the tests. Please send fixes/complaints
16 * to wd@denx.de
17 *
18 */
19
20#include <common.h>
21#include <mpc8xx.h>
22#include <commproc.h>
23#include <linux/ctype.h>
24#include <malloc.h>
25#include <post.h>
26#include <serial.h>
27
28#ifdef CONFIG_SPI
29
30#define SPI_EEPROM_WREN 0x06
31#define SPI_EEPROM_RDSR 0x05
32#define SPI_EEPROM_READ 0x03
33#define SPI_EEPROM_WRITE 0x02
34
35/* ---------------------------------------------------------------
36 * Offset for initial SPI buffers in DPRAM:
37 * We need a 520 byte scratch DPRAM area to use at an early stage.
38 * It is used between the two initialization calls (spi_init_f()
39 * and spi_init_r()).
40 * The value 0xb00 makes it far enough from the start of the data
41 * area (as well as from the stack pointer).
42 * --------------------------------------------------------------- */
43#ifndef CONFIG_SYS_SPI_INIT_OFFSET
44#define CONFIG_SYS_SPI_INIT_OFFSET 0xB00
45#endif
46
Christophe Leroyba3da732017-07-06 10:33:13 +020047#define CPM_SPI_BASE_RX CPM_SPI_BASE
48#define CPM_SPI_BASE_TX (CPM_SPI_BASE + sizeof(cbd_t))
49
Christophe Leroy907208c2017-07-06 10:23:22 +020050/* -------------------
51 * Function prototypes
52 * ------------------- */
53void spi_init (void);
54
55ssize_t spi_read (uchar *, int, uchar *, int);
56ssize_t spi_write (uchar *, int, uchar *, int);
57ssize_t spi_xfer (size_t);
58
59/* -------------------
60 * Variables
61 * ------------------- */
62
63#define MAX_BUFFER 0x104
64
65/* ----------------------------------------------------------------------
66 * Initially we place the RX and TX buffers at a fixed location in DPRAM!
67 * ---------------------------------------------------------------------- */
68static uchar *rxbuf =
69 (uchar *)&((cpm8xx_t *)&((immap_t *)CONFIG_SYS_IMMR)->im_cpm)->cp_dpmem
70 [CONFIG_SYS_SPI_INIT_OFFSET];
71static uchar *txbuf =
72 (uchar *)&((cpm8xx_t *)&((immap_t *)CONFIG_SYS_IMMR)->im_cpm)->cp_dpmem
73 [CONFIG_SYS_SPI_INIT_OFFSET+MAX_BUFFER];
74
75/* **************************************************************************
76 *
77 * Function: spi_init_f
78 *
79 * Description: Init SPI-Controller (ROM part)
80 *
81 * return: ---
82 *
83 * *********************************************************************** */
84void spi_init_f (void)
85{
Christophe Leroyba3da732017-07-06 10:33:13 +020086 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
87 cpm8xx_t __iomem *cp = &immr->im_cpm;
88 spi_t __iomem *spi = (spi_t __iomem *)&cp->cp_dparam[PROFF_SPI];
89 cbd_t __iomem *tbdf, *rbdf;
Christophe Leroy907208c2017-07-06 10:23:22 +020090
Christophe Leroy907208c2017-07-06 10:23:22 +020091 /* Disable relocation */
Christophe Leroyba3da732017-07-06 10:33:13 +020092 out_be16(&spi->spi_rpbase, 0);
Christophe Leroy907208c2017-07-06 10:23:22 +020093
94/* 1 */
95 /* ------------------------------------------------
96 * Initialize Port B SPI pins -> page 34-8 MPC860UM
97 * (we are only in Master Mode !)
98 * ------------------------------------------------ */
99
100 /* --------------------------------------------
101 * GPIO or per. Function
102 * PBPAR[28] = 1 [0x00000008] -> PERI: (SPIMISO)
103 * PBPAR[29] = 1 [0x00000004] -> PERI: (SPIMOSI)
104 * PBPAR[30] = 1 [0x00000002] -> PERI: (SPICLK)
105 * PBPAR[31] = 0 [0x00000001] -> GPIO: (CS for PCUE/CCM-EEPROM)
106 * -------------------------------------------- */
Christophe Leroyba3da732017-07-06 10:33:13 +0200107 clrsetbits_be32(&cp->cp_pbpar, 0x00000001, 0x0000000E); /* set bits */
Christophe Leroy907208c2017-07-06 10:23:22 +0200108
109 /* ----------------------------------------------
110 * In/Out or per. Function 0/1
111 * PBDIR[28] = 1 [0x00000008] -> PERI1: SPIMISO
112 * PBDIR[29] = 1 [0x00000004] -> PERI1: SPIMOSI
113 * PBDIR[30] = 1 [0x00000002] -> PERI1: SPICLK
114 * PBDIR[31] = 1 [0x00000001] -> GPIO OUT: CS for PCUE/CCM-EEPROM
115 * ---------------------------------------------- */
Christophe Leroyba3da732017-07-06 10:33:13 +0200116 setbits_be32(&cp->cp_pbdir, 0x0000000F);
Christophe Leroy907208c2017-07-06 10:23:22 +0200117
118 /* ----------------------------------------------
119 * open drain or active output
120 * PBODR[28] = 1 [0x00000008] -> open drain: SPIMISO
121 * PBODR[29] = 0 [0x00000004] -> active output SPIMOSI
122 * PBODR[30] = 0 [0x00000002] -> active output: SPICLK
123 * PBODR[31] = 0 [0x00000001] -> active output: GPIO OUT: CS for PCUE/CCM
124 * ---------------------------------------------- */
125
Christophe Leroyba3da732017-07-06 10:33:13 +0200126 clrsetbits_be16(&cp->cp_pbodr, 0x00000007, 0x00000008);
Christophe Leroy907208c2017-07-06 10:23:22 +0200127
128 /* Initialize the parameter ram.
129 * We need to make sure many things are initialized to zero
130 */
Christophe Leroyba3da732017-07-06 10:33:13 +0200131 out_be32(&spi->spi_rstate, 0);
132 out_be32(&spi->spi_rdp, 0);
133 out_be16(&spi->spi_rbptr, 0);
134 out_be16(&spi->spi_rbc, 0);
135 out_be32(&spi->spi_rxtmp, 0);
136 out_be32(&spi->spi_tstate, 0);
137 out_be32(&spi->spi_tdp, 0);
138 out_be16(&spi->spi_tbptr, 0);
139 out_be16(&spi->spi_tbc, 0);
140 out_be32(&spi->spi_txtmp, 0);
Christophe Leroy907208c2017-07-06 10:23:22 +0200141
142/* 3 */
143 /* Set up the SPI parameters in the parameter ram */
Christophe Leroyba3da732017-07-06 10:33:13 +0200144 out_be16(&spi->spi_rbase, CPM_SPI_BASE_RX);
145 out_be16(&spi->spi_tbase, CPM_SPI_BASE_TX);
Christophe Leroy907208c2017-07-06 10:23:22 +0200146
147 /***********IMPORTANT******************/
148
149 /*
150 * Setting transmit and receive buffer descriptor pointers
151 * initially to rbase and tbase. Only the microcode patches
152 * documentation talks about initializing this pointer. This
153 * is missing from the sample I2C driver. If you dont
154 * initialize these pointers, the kernel hangs.
155 */
Christophe Leroyba3da732017-07-06 10:33:13 +0200156 out_be16(&spi->spi_rbptr, CPM_SPI_BASE_RX);
157 out_be16(&spi->spi_tbptr, CPM_SPI_BASE_TX);
Christophe Leroy907208c2017-07-06 10:23:22 +0200158
159/* 4 */
160 /* Init SPI Tx + Rx Parameters */
Christophe Leroyba3da732017-07-06 10:33:13 +0200161 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
Christophe Leroy907208c2017-07-06 10:23:22 +0200162 ;
Christophe Leroyba3da732017-07-06 10:33:13 +0200163
164 out_be16(&cp->cp_cpcr, mk_cr_cmd(CPM_CR_CH_SPI, CPM_CR_INIT_TRX) |
165 CPM_CR_FLG);
166 while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
Christophe Leroy907208c2017-07-06 10:23:22 +0200167 ;
168
169/* 5 */
170 /* Set SDMA configuration register */
Christophe Leroyba3da732017-07-06 10:33:13 +0200171 out_be32(&immr->im_siu_conf.sc_sdcr, 0x0001);
Christophe Leroy907208c2017-07-06 10:23:22 +0200172
173/* 6 */
174 /* Set to big endian. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200175 out_8(&spi->spi_tfcr, SMC_EB);
176 out_8(&spi->spi_rfcr, SMC_EB);
Christophe Leroy907208c2017-07-06 10:23:22 +0200177
178/* 7 */
179 /* Set maximum receive size. */
Christophe Leroyba3da732017-07-06 10:33:13 +0200180 out_be16(&spi->spi_mrblr, MAX_BUFFER);
Christophe Leroy907208c2017-07-06 10:23:22 +0200181
182/* 8 + 9 */
183 /* tx and rx buffer descriptors */
Christophe Leroyba3da732017-07-06 10:33:13 +0200184 tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
185 rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
Christophe Leroy907208c2017-07-06 10:23:22 +0200186
Christophe Leroyba3da732017-07-06 10:33:13 +0200187 clrbits_be16(&tbdf->cbd_sc, BD_SC_READY);
188 clrbits_be16(&rbdf->cbd_sc, BD_SC_EMPTY);
Christophe Leroy907208c2017-07-06 10:23:22 +0200189
190 /* Set the bd's rx and tx buffer address pointers */
Christophe Leroyba3da732017-07-06 10:33:13 +0200191 out_be32(&rbdf->cbd_bufaddr, (ulong)rxbuf);
192 out_be32(&tbdf->cbd_bufaddr, (ulong)txbuf);
Christophe Leroy907208c2017-07-06 10:23:22 +0200193
194/* 10 + 11 */
Christophe Leroyba3da732017-07-06 10:33:13 +0200195 out_8(&cp->cp_spim, 0); /* Mask all SPI events */
196 out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
Christophe Leroy907208c2017-07-06 10:23:22 +0200197
198 return;
199}
200
201/* **************************************************************************
202 *
203 * Function: spi_init_r
204 *
205 * Description: Init SPI-Controller (RAM part) -
206 * The malloc engine is ready and we can move our buffers to
207 * normal RAM
208 *
209 * return: ---
210 *
211 * *********************************************************************** */
212void spi_init_r (void)
213{
Christophe Leroyba3da732017-07-06 10:33:13 +0200214 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
215 cpm8xx_t __iomem *cp = &immr->im_cpm;
216 spi_t __iomem *spi = (spi_t __iomem *)&cp->cp_dparam[PROFF_SPI];
217 cbd_t __iomem *tbdf, *rbdf;
Christophe Leroy907208c2017-07-06 10:23:22 +0200218
Christophe Leroy907208c2017-07-06 10:23:22 +0200219 /* Disable relocation */
Christophe Leroyba3da732017-07-06 10:33:13 +0200220 out_be16(&spi->spi_rpbase, 0);
Christophe Leroy907208c2017-07-06 10:23:22 +0200221
222 /* tx and rx buffer descriptors */
Christophe Leroyba3da732017-07-06 10:33:13 +0200223 tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
224 rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
Christophe Leroy907208c2017-07-06 10:23:22 +0200225
226 /* Allocate memory for RX and TX buffers */
227 rxbuf = (uchar *) malloc (MAX_BUFFER);
228 txbuf = (uchar *) malloc (MAX_BUFFER);
229
Christophe Leroyba3da732017-07-06 10:33:13 +0200230 out_be32(&rbdf->cbd_bufaddr, (ulong)rxbuf);
231 out_be32(&tbdf->cbd_bufaddr, (ulong)txbuf);
Christophe Leroy907208c2017-07-06 10:23:22 +0200232
233 return;
234}
235
236/****************************************************************************
237 * Function: spi_write
238 **************************************************************************** */
239ssize_t spi_write (uchar *addr, int alen, uchar *buffer, int len)
240{
241 int i;
242
243 memset(rxbuf, 0, MAX_BUFFER);
244 memset(txbuf, 0, MAX_BUFFER);
245 *txbuf = SPI_EEPROM_WREN; /* write enable */
246 spi_xfer(1);
247 memcpy(txbuf, addr, alen);
248 *txbuf = SPI_EEPROM_WRITE; /* WRITE memory array */
249 memcpy(alen + txbuf, buffer, len);
250 spi_xfer(alen + len);
251 /* ignore received data */
252 for (i = 0; i < 1000; i++) {
253 *txbuf = SPI_EEPROM_RDSR; /* read status */
254 txbuf[1] = 0;
255 spi_xfer(2);
256 if (!(rxbuf[1] & 1)) {
257 break;
258 }
259 udelay(1000);
260 }
261 if (i >= 1000) {
262 printf ("*** spi_write: Time out while writing!\n");
263 }
264
265 return len;
266}
267
268/****************************************************************************
269 * Function: spi_read
270 **************************************************************************** */
271ssize_t spi_read (uchar *addr, int alen, uchar *buffer, int len)
272{
273 memset(rxbuf, 0, MAX_BUFFER);
274 memset(txbuf, 0, MAX_BUFFER);
275 memcpy(txbuf, addr, alen);
276 *txbuf = SPI_EEPROM_READ; /* READ memory array */
277
278 /*
279 * There is a bug in 860T (?) that cuts the last byte of input
280 * if we're reading into DPRAM. The solution we choose here is
281 * to always read len+1 bytes (we have one extra byte at the
282 * end of the buffer).
283 */
284 spi_xfer(alen + len + 1);
285 memcpy(buffer, alen + rxbuf, len);
286
287 return len;
288}
289
290/****************************************************************************
291 * Function: spi_xfer
292 **************************************************************************** */
293ssize_t spi_xfer (size_t count)
294{
Christophe Leroyba3da732017-07-06 10:33:13 +0200295 immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
296 cpm8xx_t __iomem *cp = &immr->im_cpm;
297 spi_t __iomem *spi = (spi_t __iomem *)&cp->cp_dparam[PROFF_SPI];
298 cbd_t __iomem *tbdf, *rbdf;
Christophe Leroy907208c2017-07-06 10:23:22 +0200299 int tm;
300
Christophe Leroy907208c2017-07-06 10:23:22 +0200301 /* Disable relocation */
Christophe Leroyba3da732017-07-06 10:33:13 +0200302 out_be16(&spi->spi_rpbase, 0);
Christophe Leroy907208c2017-07-06 10:23:22 +0200303
Christophe Leroyba3da732017-07-06 10:33:13 +0200304 tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
305 rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
Christophe Leroy907208c2017-07-06 10:23:22 +0200306
307 /* Set CS for device */
Christophe Leroyba3da732017-07-06 10:33:13 +0200308 clrbits_be32(&cp->cp_pbdat, 0x0001);
Christophe Leroy907208c2017-07-06 10:23:22 +0200309
310 /* Setting tx bd status and data length */
Christophe Leroyba3da732017-07-06 10:33:13 +0200311 out_be16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_LAST | BD_SC_WRAP);
312 out_be16(&tbdf->cbd_datlen, count);
Christophe Leroy907208c2017-07-06 10:23:22 +0200313
314 /* Setting rx bd status and data length */
Christophe Leroyba3da732017-07-06 10:33:13 +0200315 out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
316 out_be16(&rbdf->cbd_datlen, 0); /* rx length has no significance */
Christophe Leroy907208c2017-07-06 10:23:22 +0200317
Christophe Leroyba3da732017-07-06 10:33:13 +0200318 clrsetbits_be16(&cp->cp_spmode, ~SPMODE_LOOP, SPMODE_REV | SPMODE_MSTR |
319 SPMODE_EN | SPMODE_LEN(8) | SPMODE_PM(0x8));
320 out_8(&cp->cp_spim, 0); /* Mask all SPI events */
321 out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
Christophe Leroy907208c2017-07-06 10:23:22 +0200322
323 /* start spi transfer */
Christophe Leroyba3da732017-07-06 10:33:13 +0200324 setbits_8(&cp->cp_spcom, SPI_STR); /* Start transmit */
Christophe Leroy907208c2017-07-06 10:23:22 +0200325
326 /* --------------------------------
327 * Wait for SPI transmit to get out
328 * or time out (1 second = 1000 ms)
329 * -------------------------------- */
330 for (tm=0; tm<1000; ++tm) {
Christophe Leroyba3da732017-07-06 10:33:13 +0200331 if (in_8(&cp->cp_spie) & SPI_TXB) /* Tx Buffer Empty */
Christophe Leroy907208c2017-07-06 10:23:22 +0200332 break;
Christophe Leroyba3da732017-07-06 10:33:13 +0200333 if ((in_be16(&tbdf->cbd_sc) & BD_SC_READY) == 0)
Christophe Leroy907208c2017-07-06 10:23:22 +0200334 break;
Christophe Leroy907208c2017-07-06 10:23:22 +0200335 udelay (1000);
336 }
337 if (tm >= 1000) {
338 printf ("*** spi_xfer: Time out while xferring to/from SPI!\n");
339 }
340
341 /* Clear CS for device */
Christophe Leroyba3da732017-07-06 10:33:13 +0200342 setbits_be32(&cp->cp_pbdat, 0x0001);
Christophe Leroy907208c2017-07-06 10:23:22 +0200343
344 return count;
345}
346#endif /* CONFIG_SPI */