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