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