summaryrefslogtreecommitdiffstats
path: root/arch/powerpc/cpu/mpc5xx/spi.c
blob: 3ca15ea83860472ebd6d97afc573695f53d69a4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 * Copyright (c) 2001 Navin Boppuri / Prashant Patel
 *	<nboppuri@trinetcommunication.com>,
 *	<pmpatel@trinetcommunication.com>
 * Copyright (c) 2001 Gerd Mennchen <Gerd.Mennchen@icn.siemens.de>
 * Copyright (c) 2001 Wolfgang Denk, DENX Software Engineering, <wd@denx.de>.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

/*
 * MPC5xx CPM SPI interface.
 *
 * Parts of this code are probably not portable and/or specific to
 * the board which I used for the tests. Please send fixes/complaints
 * to wd@denx.de
 *
 * Ported to MPC5xx
 * Copyright (c) 2003 Denis Peter, MPL AG Switzerland, d.petr@mpl.ch.
 */

#include <common.h>
#include <mpc5xx.h>
#include <asm/5xx_immap.h>
#include <linux/ctype.h>
#include <malloc.h>
#include <post.h>
#include <net.h>

#if defined(CONFIG_SPI)

#undef	DEBUG

#define SPI_EEPROM_WREN		0x06
#define SPI_EEPROM_RDSR		0x05
#define SPI_EEPROM_READ		0x03
#define SPI_EEPROM_WRITE	0x02


#ifdef	DEBUG

#define	DPRINT(a)	printf a;
/* -----------------------------------------------
 * Helper functions to peek into tx and rx buffers
 * ----------------------------------------------- */
static const char * const hex_digit = "0123456789ABCDEF";

static char quickhex (int i)
{
	return hex_digit[i];
}

static void memdump (void *pv, int num)
{
	int i;
	unsigned char *pc = (unsigned char *) pv;

	for (i = 0; i < num; i++)
		printf ("%c%c ", quickhex (pc[i] >> 4), quickhex (pc[i] & 0x0f));
	printf ("\t");
	for (i = 0; i < num; i++)
		printf ("%c", isprint (pc[i]) ? pc[i] : '.');
	printf ("\n");
}
#else	/* !DEBUG */

#define	DPRINT(a)

#endif	/* DEBUG */

/* -------------------
 * Function prototypes
 * ------------------- */
void spi_init (void);

ssize_t spi_read (uchar *, int, uchar *, int);
ssize_t spi_write (uchar *, int, uchar *, int);
ssize_t spi_xfer (size_t);


/* **************************************************************************
 *
 *  Function:    spi_init_f
 *
 *  Description: Init SPI-Controller (ROM part)
 *
 *  return:      ---
 *
 * *********************************************************************** */

void spi_init_f (void)
{
	int i;

	volatile immap_t *immr;
	volatile qsmcm5xx_t *qsmcm;

	immr = (immap_t *)  CONFIG_SYS_IMMR;
	qsmcm = (qsmcm5xx_t *)&immr->im_qsmcm;

	qsmcm->qsmcm_qsmcr = 0; /* all accesses enabled */
	qsmcm->qsmcm_qspi_il = 0; /* lowest IRQ */

	/* --------------------------------------------
	 * GPIO or per. Function
	 * PQSPAR[00] = 0 reserved
	 * PQSPAR[01] = 1 [0x4000] -> PERI: (SPICS3)
	 * PQSPAR[02] = 0 [0x0000] -> GPIO
	 * PQSPAR[03] = 0 [0x0000] -> GPIO
	 * PQSPAR[04] = 1 [0x0800] -> PERI: (SPICS0)
	 * PQSPAR[05] = 0 reseved
	 * PQSPAR[06] = 1 [0x0200] -> PERI: (SPIMOSI)
	 * PQSPAR[07] = 1 [0x0100] -> PERI: (SPIMISO)
	 * -------------------------------------------- */
	qsmcm->qsmcm_pqspar =  0x3 | (CONFIG_SYS_SPI_CS_USED << 3);

	 /* --------------------------------------------
	 * DDRQS[00] = 0 reserved
	 * DDRQS[01] = 1 [0x0040] -> SPICS3 Output
	 * DDRQS[02] = 0 [0x0000] -> GPIO Output
	 * DDRQS[03] = 0 [0x0000] -> GPIO Output
	 * DDRQS[04] = 1 [0x0008] -> SPICS0 Output
	 * DDRQS[05] = 1 [0x0004] -> SPICLK Output
	 * DDRQS[06] = 1 [0x0002] -> SPIMOSI Output
	 * DDRQS[07] = 0 [0x0001] -> SPIMISO Input
	 * -------------------------------------------- */
	qsmcm->qsmcm_ddrqs = 0x7E;
	 /* --------------------------------------------
	 * Base state for used SPI CS pins, if base = 0 active must be 1
	 * PORTQS[00] = 0 reserved
	 * PORTQS[01] = 0 reserved
	 * PORTQS[02] = 0 reserved
	 * PORTQS[03] = 0 reserved
	 * PORTQS[04] = 0 [0x0000] RxD2
	 * PORTQS[05] = 1 [0x0400] TxD2
	 * PORTQS[06] = 0 [0x0000] RxD1
	 * PORTQS[07] = 1 [0x0100] TxD1
	 * PORTQS[08] = 0 reserved
	 * PORTQS[09] = 0 [0x0000] -> SPICS3 Base Output
	 * PORTQS[10] = 0 [0x0000] -> SPICS2 Base Output
	 * PORTQS[11] = 0 [0x0000] -> SPICS1 Base Output
	 * PORTQS[12] = 0 [0x0000] -> SPICS0 Base Output
	 * PORTQS[13] = 0 [0x0004] -> SPICLK Output
	 * PORTQS[14] = 0 [0x0002] -> SPIMOSI Output
	 * PORTQS[15] = 0 [0x0001] -> SPIMISO Input
	 * -------------------------------------------- */
	qsmcm->qsmcm_portqs |= (CONFIG_SYS_SPI_CS_BASE << 3);
	/* --------------------------------------------
	 * Controll Register 0
	 * SPCR0[00] = 1 (0x8000) Master
	 * SPCR0[01] = 0 (0x0000) Wired-Or
	 * SPCR0[2..5] = (0x2000) Bits per transfer (default 8)
	 * SPCR0[06] = 0 (0x0000) Normal polarity
	 * SPCR0[07] = 0 (0x0000) Normal Clock Phase
	 * SPCR0[08..15] = 14 1.4MHz
	 */
	qsmcm->qsmcm_spcr0=0xA00E;
	/* --------------------------------------------
	 * Controll Register 1
	 * SPCR1[00] = 0 (0x0000) QSPI enabled
	 * SPCR1[1..7] =  (0x7F00) Delay before Transfer
	 * SPCR1[8..15] = (0x0000) Delay After transfer (204.8usec@40MHz)
	 */
	qsmcm->qsmcm_spcr1=0x7F00;
	/* --------------------------------------------
	 * Controll Register 2
	 * SPCR2[00] = 0 (0x0000) SPI IRQs Disabeld
	 * SPCR2[01] = 0 (0x0000) No Wrap around
	 * SPCR2[02] = 0 (0x0000) Wrap to 0
	 * SPCR2[3..7] = (0x0000) End Queue pointer = 0
	 * SPCR2[8..10] = 0 (0x0000) reserved
	 * SPCR2[11..15] = 0 (0x0000) NewQueue Address = 0
	 */
	qsmcm->qsmcm_spcr2=0x0000;
	/* --------------------------------------------
	 * Controll Register 3
	 * SPCR3[00..04] = 0 (0x0000) reserved
	 * SPCR3[05] = 0 (0x0000) Feedback disabled
	 * SPCR3[06] = 0 (0x0000) IRQ on HALTA & MODF disabled
	 * SPCR3[07] = 0 (0x0000) Not halted
	 */
	qsmcm->qsmcm_spcr3=0x00;
	/* --------------------------------------------
	 * SPSR (Controll Register 3) Read only/ reset Flags 08,09,10
	 * SPCR3[08] = 1 (0x80) QSPI finished
	 * SPCR3[09] = 1 (0x40) Mode Fault Flag
	 * SPCR3[10] = 1 (0x20) HALTA
	 * SPCR3[11..15] = 0 (0x0000) Last executed command
	 */
	qsmcm->qsmcm_spsr=0xE0;
	/*-------------------------------------------
	 * Setup RAM
	 */
	for(i=0;i<32;i++) {
		 qsmcm->qsmcm_recram[i]=0x0000;
		 qsmcm->qsmcm_tranram[i]=0x0000;
		 qsmcm->qsmcm_comdram[i]=0x00;
	}
	return;
}

/* **************************************************************************
 *
 *  Function:    spi_init_r
 *  Dummy, all initializations have been done in spi_init_r
 * *********************************************************************** */
void spi_init_r (void)
{
	return;

}

/****************************************************************************
 *  Function:    spi_write
 **************************************************************************** */
ssize_t short_spi_write (uchar *addr, int alen, uchar *buffer, int len)
{
	int i,dlen;
	volatile immap_t *immr;
	volatile qsmcm5xx_t *qsmcm;

	immr = (immap_t *)  CONFIG_SYS_IMMR;
	qsmcm = (qsmcm5xx_t *)&immr->im_qsmcm;
	for(i=0;i<32;i++) {
		 qsmcm->qsmcm_recram[i]=0x0000;
		 qsmcm->qsmcm_tranram[i]=0x0000;
		 qsmcm->qsmcm_comdram[i]=0x00;
	}
	qsmcm->qsmcm_tranram[0] =  SPI_EEPROM_WREN; /* write enable */
	spi_xfer(1);
	i=0;
	qsmcm->qsmcm_tranram[i++] =  SPI_EEPROM_WRITE; /* WRITE memory array */
	qsmcm->qsmcm_tranram[i++] =  addr[0];
	qsmcm->qsmcm_tranram[i++] =  addr[1];

	for(dlen=0;dlen<len;dlen++) {
		qsmcm->qsmcm_tranram[i+dlen] = buffer[dlen]; /* WRITE memory array */
	}
	/* transmit it */
	spi_xfer(i+dlen);
	/* ignore received data	*/
	for (i = 0; i < 1000; i++) {
		qsmcm->qsmcm_tranram[0] =  SPI_EEPROM_RDSR; /* read status */
		qsmcm->qsmcm_tranram[1] = 0;
		spi_xfer(2);
		if (!(qsmcm->qsmcm_recram[1] & 1)) {
			break;
		}
		udelay(1000);
	}
	if (i >= 1000) {
		printf ("*** spi_write: Time out while writing!\n");
	}
	return len;
}

#define TRANSFER_LEN 16

ssize_t spi_write (uchar *addr, int alen, uchar *buffer, int len)
{
	int index,i,newlen;
	uchar newaddr[2];
	int curraddr;

	curraddr=(addr[alen-2]<<8)+addr[alen-1];
	i=len;
	index=0;
	do {
		newaddr[1]=(curraddr & 0xff);
		newaddr[0]=((curraddr>>8) & 0xff);
		if(i>TRANSFER_LEN) {
			newlen=TRANSFER_LEN;
			i-=TRANSFER_LEN;
		}
		else {
			newlen=i;
			i=0;
		}
		short_spi_write (newaddr, 2, &buffer[index], newlen);
		index+=newlen;
		curraddr+=newlen;
	}while(i);
	return (len);
}

/****************************************************************************
 *  Function:    spi_read
 **************************************************************************** */
ssize_t short_spi_read (uchar *addr, int alen, uchar *buffer, int len)
{
	int i;
	volatile immap_t *immr;
	volatile qsmcm5xx_t *qsmcm;

	immr = (immap_t *)  CONFIG_SYS_IMMR;
	qsmcm = (qsmcm5xx_t *)&immr->im_qsmcm;

	for(i=0;i<32;i++) {
		 qsmcm->qsmcm_recram[i]=0x0000;
		 qsmcm->qsmcm_tranram[i]=0x0000;
		 qsmcm->qsmcm_comdram[i]=0x00;
	}
	i=0;
	qsmcm->qsmcm_tranram[i++] = (SPI_EEPROM_READ); /* READ memory array */
	qsmcm->qsmcm_tranram[i++] = addr[0] & 0xff;
	qsmcm->qsmcm_tranram[i++] = addr[1] & 0xff;
	spi_xfer(3 + len);
	for(i=0;i<len;i++) {
		*buffer++=(char)qsmcm->qsmcm_recram[i+3];
	}
	return len;
}

ssize_t spi_read (uchar *addr, int alen, uchar *buffer, int len)
{
	int index,i,newlen;
	uchar newaddr[2];
	int curraddr;

	curraddr=(addr[alen-2]<<8)+addr[alen-1];
	i=len;
	index=0;
	do {
		newaddr[1]=(curraddr & 0xff);
		newaddr[0]=((curraddr>>8) & 0xff);
		if(i>TRANSFER_LEN) {
			newlen=TRANSFER_LEN;
			i-=TRANSFER_LEN;
		}
		else {
			newlen=i;
			i=0;
		}
		short_spi_read (newaddr, 2, &buffer[index], newlen);
		index+=newlen;
		curraddr+=newlen;
	}while(i);
	return (len);
}

/****************************************************************************
 *  Function:    spi_xfer
 **************************************************************************** */
ssize_t spi_xfer (size_t count)
{
	volatile immap_t *immr;
	volatile qsmcm5xx_t *qsmcm;
	int i;
	int tm;
	ushort status;
	immr = (immap_t *)  CONFIG_SYS_IMMR;
	qsmcm = (qsmcm5xx_t *)&immr->im_qsmcm;
	DPRINT (("*** spi_xfer entered count %d***\n",count));

	/* Set CS for device */
	for(i=0;i<(count-1);i++)
		qsmcm->qsmcm_comdram[i] = 0x80 | CONFIG_SYS_SPI_CS_ACT;  /* CS3 is connected to the SPI EEPROM */

	qsmcm->qsmcm_comdram[i] = CONFIG_SYS_SPI_CS_ACT; /* CS3 is connected to the SPI EEPROM */
	qsmcm->qsmcm_spcr2=((count-1)&0x1F)<<8;

	DPRINT (("*** spi_xfer: Bytes to be xferred: %d ***\n", count));

	qsmcm->qsmcm_spsr=0xE0; /* clear all flags */

	/* start spi transfer */
	DPRINT (("*** spi_xfer: Performing transfer ...\n"));
	qsmcm->qsmcm_spcr1 |= 0x8000;		/* Start transmit */

	/* --------------------------------
	 * Wait for SPI transmit to get out
	 * or time out (1 second = 1000 ms)
	 * -------------------------------- */
	for (tm=0; tm<1000; ++tm) {
		status=qsmcm->qsmcm_spcr1;
		if((status & 0x8000)==0)
			break;
		udelay (1000);
	}
	if (tm >= 1000) {
		printf ("*** spi_xfer: Time out while xferring to/from SPI!\n");
	}
#ifdef	DEBUG
	printf ("\nspi_xfer: txbuf after xfer\n");
	memdump ((void *) qsmcm->qsmcm_tranram, 32);	/* dump of txbuf before transmit */
	printf ("spi_xfer: rxbuf after xfer\n");
	memdump ((void *) qsmcm->qsmcm_recram, 32);	/* dump of rxbuf after transmit */
	printf ("\nspi_xfer: commbuf after xfer\n");
	memdump ((void *) qsmcm->qsmcm_comdram, 32);	/* dump of txbuf before transmit */
	printf ("\n");
#endif

	return count;
}

#endif	/* CONFIG_SPI  */