summaryrefslogtreecommitdiffstats
path: root/board/bf533-stamp/spi.c
blob: 15141cf743273f1c11fc8856b590e1d9d20b84f6 (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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/****************************************************************************
 *  SPI flash driver for M25P64
 ****************************************************************************/
#include <common.h>
#include <linux/ctype.h>
#include <asm/io.h>
#include <asm/mach-common/bits/spi.h>

#if defined(CONFIG_SPI)

 /*Application definitions */

#define	NUM_SECTORS 	128	/* number of sectors */
#define SECTOR_SIZE		0x10000
#define NOP_NUM		1000

#define COMMON_SPI_SETTINGS (SPE|MSTR|CPHA|CPOL)	/*Settings to the SPI_CTL */
#define TIMOD01 (0x01)		/*stes the SPI to work with core instructions */

 /*Flash commands */
#define SPI_WREN	(0x06)	/*Set Write Enable Latch */
#define SPI_WRDI	(0x04)	/*Reset Write Enable Latch */
#define SPI_RDSR	(0x05)	/*Read Status Register */
#define SPI_WRSR	(0x01)	/*Write Status Register */
#define SPI_READ	(0x03)	/*Read data from memory */
#define SPI_PP  	(0x02)	/*Program Data into memory */
#define SPI_SE  	(0xD8)	/*Erase one sector in memory */
#define SPI_BE		(0xC7)	/*Erase all memory */
#define WIP		(0x1)	/*Check the write in progress bit of the SPI status register */
#define WEL		(0x2)	/*Check the write enable bit of the SPI status register */

#define TIMEOUT 350000000

typedef enum {
	NO_ERR,
	POLL_TIMEOUT,
	INVALID_SECTOR,
	INVALID_BLOCK,
} ERROR_CODE;

void spi_init_f(void);
void spi_init_r(void);
ssize_t spi_read(uchar *, int, uchar *, int);
ssize_t spi_write(uchar *, int, uchar *, int);

char ReadStatusRegister(void);
void Wait_For_SPIF(void);
void SetupSPI(const int spi_setting);
void SPI_OFF(void);
void SendSingleCommand(const int iCommand);

ERROR_CODE GetSectorNumber(unsigned long ulOffset, int *pnSector);
ERROR_CODE EraseBlock(int nBlock);
ERROR_CODE ReadData(unsigned long ulStart, long lCount, int *pnData);
ERROR_CODE WriteData(unsigned long ulStart, long lCount, int *pnData);
ERROR_CODE Wait_For_Status(char Statusbit);
ERROR_CODE Wait_For_WEL(void);

/* -------------------
 * Variables
 * ------------------- */

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

/* **************************************************************************
 *
 *  Function:    spi_init_r
 *
 *  Description: Init SPI-Controller (RAM part) -
 *		 The malloc engine is ready and we can move our buffers to
 *		 normal RAM
 *
 *  return:      ---
 *
 * *********************************************************************** */
void spi_init_r(void)
{
	return;
}

/****************************************************************************
 *  Function:    spi_write
 **************************************************************************** */
ssize_t spi_write(uchar * addr, int alen, uchar * buffer, int len)
{
	unsigned long offset;
	int start_block, end_block;
	int start_byte, end_byte;
	ERROR_CODE result = NO_ERR;
	uchar temp[SECTOR_SIZE];
	int i, num;

	offset = addr[0] << 16 | addr[1] << 8 | addr[2];
	/* Get the start block number */
	result = GetSectorNumber(offset, &start_block);
	if (result == INVALID_SECTOR) {
		printf("Invalid sector! ");
		return 0;
	}
	/* Get the end block number */
	result = GetSectorNumber(offset + len - 1, &end_block);
	if (result == INVALID_SECTOR) {
		printf("Invalid sector! ");
		return 0;
	}

	for (num = start_block; num <= end_block; num++) {
		ReadData(num * SECTOR_SIZE, SECTOR_SIZE, (int *)temp);
		start_byte = num * SECTOR_SIZE;
		end_byte = (num + 1) * SECTOR_SIZE - 1;
		if (start_byte < offset)
			start_byte = offset;
		if (end_byte > (offset + len))
			end_byte = (offset + len - 1);
		for (i = start_byte; i <= end_byte; i++)
			temp[i - num * SECTOR_SIZE] = buffer[i - offset];
		EraseBlock(num);
		result = WriteData(num * SECTOR_SIZE, SECTOR_SIZE, (int *)temp);
		if (result != NO_ERR)
			return 0;
		printf(".");
	}
	return len;
}

/****************************************************************************
 *  Function:    spi_read
 **************************************************************************** */
ssize_t spi_read(uchar * addr, int alen, uchar * buffer, int len)
{
	unsigned long offset;
	offset = addr[0] << 16 | addr[1] << 8 | addr[2];
	ReadData(offset, len, (int *)buffer);
	return len;
}

void SendSingleCommand(const int iCommand)
{
	unsigned short dummy;

	/*turns on the SPI in single write mode */
	SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));

	/*sends the actual command to the SPI TX register */
	*pSPI_TDBR = iCommand;
	SSYNC();

	/*The SPI status register will be polled to check the SPIF bit */
	Wait_For_SPIF();

	dummy = *pSPI_RDBR;

	/*The SPI will be turned off */
	SPI_OFF();

}

void SetupSPI(const int spi_setting)
{

	if (icache_status() || dcache_status())
		udelay(CONFIG_CCLK_HZ / 50000000);
	/*sets up the PF2 to be the slave select of the SPI */
	*pSPI_FLG = 0xFB04;
	*pSPI_BAUD = CONFIG_SPI_BAUD;
	*pSPI_CTL = spi_setting;
	SSYNC();
}

void SPI_OFF(void)
{

	*pSPI_CTL = 0x0400;	/* disable SPI */
	*pSPI_FLG = 0;
	*pSPI_BAUD = 0;
	SSYNC();
	udelay(CONFIG_CCLK_HZ / 50000000);

}

void Wait_For_SPIF(void)
{
	unsigned short dummyread;
	while ((*pSPI_STAT & TXS)) ;
	while (!(*pSPI_STAT & SPIF)) ;
	while (!(*pSPI_STAT & RXS)) ;
	dummyread = *pSPI_RDBR;	/* Read dummy to empty the receive register      */

}

ERROR_CODE Wait_For_WEL(void)
{
	int i;
	char status_register = 0;
	ERROR_CODE ErrorCode = NO_ERR;	/* tells us if there was an error erasing flash */

	for (i = 0; i < TIMEOUT; i++) {
		status_register = ReadStatusRegister();
		if ((status_register & WEL)) {
			ErrorCode = NO_ERR;	/* tells us if there was an error erasing flash */
			break;
		}
		ErrorCode = POLL_TIMEOUT;	/* Time out error */
	};

	return ErrorCode;
}

ERROR_CODE Wait_For_Status(char Statusbit)
{
	int i;
	char status_register = 0xFF;
	ERROR_CODE ErrorCode = NO_ERR;	/* tells us if there was an error erasing flash */

	for (i = 0; i < TIMEOUT; i++) {
		status_register = ReadStatusRegister();
		if (!(status_register & Statusbit)) {
			ErrorCode = NO_ERR;	/* tells us if there was an error erasing flash */
			break;
		}
		ErrorCode = POLL_TIMEOUT;	/* Time out error */
	};

	return ErrorCode;
}

char ReadStatusRegister(void)
{
	char status_register = 0;

	SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));	/* Turn on the SPI */

	*pSPI_TDBR = SPI_RDSR;	/* send instruction to read status register */
	SSYNC();
	Wait_For_SPIF();	/*wait until the instruction has been sent */
	*pSPI_TDBR = 0;		/*send dummy to receive the status register */
	SSYNC();
	Wait_For_SPIF();	/*wait until the data has been sent */
	status_register = *pSPI_RDBR;	/*read the status register */

	SPI_OFF();		/* Turn off the SPI */

	return status_register;
}

ERROR_CODE GetSectorNumber(unsigned long ulOffset, int *pnSector)
{
	int nSector = 0;
	ERROR_CODE ErrorCode = NO_ERR;

	if (ulOffset > (NUM_SECTORS * 0x10000 - 1)) {
		ErrorCode = INVALID_SECTOR;
		return ErrorCode;
	}

	nSector = (int)ulOffset / 0x10000;
	*pnSector = nSector;

	/* ok */
	return ErrorCode;
}

ERROR_CODE EraseBlock(int nBlock)
{
	unsigned long ulSectorOff = 0x0, ShiftValue;
	ERROR_CODE ErrorCode = NO_ERR;

	/* if the block is invalid just return */
	if ((nBlock < 0) || (nBlock > NUM_SECTORS)) {
		ErrorCode = INVALID_BLOCK;	/* tells us if there was an error erasing flash */
		return ErrorCode;
	}
	/* figure out the offset of the block in flash */
	if ((nBlock >= 0) && (nBlock < NUM_SECTORS)) {
		ulSectorOff = (nBlock * SECTOR_SIZE);

	} else {
		ErrorCode = INVALID_BLOCK;	/* tells us if there was an error erasing flash */
		return ErrorCode;
	}

	/* A write enable instruction must previously have been executed */
	SendSingleCommand(SPI_WREN);

	/*The status register will be polled to check the write enable latch "WREN" */
	ErrorCode = Wait_For_WEL();

	if (POLL_TIMEOUT == ErrorCode) {
		printf("SPI Erase block error\n");
		return ErrorCode;
	} else
		/*Turn on the SPI to send single commands */
		SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));

	/* Send the erase block command to the flash followed by the 24 address  */
	/* to point to the start of a sector. */
	*pSPI_TDBR = SPI_SE;
	SSYNC();
	Wait_For_SPIF();
	ShiftValue = (ulSectorOff >> 16);	/* Send the highest byte of the 24 bit address at first */
	*pSPI_TDBR = ShiftValue;
	SSYNC();
	Wait_For_SPIF();	/* Wait until the instruction has been sent */
	ShiftValue = (ulSectorOff >> 8);	/* Send the middle byte of the 24 bit address  at second */
	*pSPI_TDBR = ShiftValue;
	SSYNC();
	Wait_For_SPIF();	/* Wait until the instruction has been sent */
	*pSPI_TDBR = ulSectorOff;	/* Send the lowest byte of the 24 bit address finally */
	SSYNC();
	Wait_For_SPIF();	/* Wait until the instruction has been sent */

	/*Turns off the SPI */
	SPI_OFF();

	/* Poll the status register to check the Write in Progress bit */
	/* Sector erase takes time */
	ErrorCode = Wait_For_Status(WIP);

	/* block erase should be complete */
	return ErrorCode;
}

/*****************************************************************************
* ERROR_CODE ReadData()
*
* Read a value from flash for verify purpose
*
* Inputs:	unsigned long ulStart - holds the SPI start address
*			int pnData - pointer to store value read from flash
*			long lCount - number of elements to read
***************************************************************************** */
ERROR_CODE ReadData(unsigned long ulStart, long lCount, int *pnData)
{
	unsigned long ShiftValue;
	char *cnData;
	int i;

	cnData = (char *)pnData;	/* Pointer cast to be able to increment byte wise */

	/* Start SPI interface   */
	SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));

	*pSPI_TDBR = SPI_READ;	/* Send the read command to SPI device */
	SSYNC();
	Wait_For_SPIF();	/* Wait until the instruction has been sent */
	ShiftValue = (ulStart >> 16);	/* Send the highest byte of the 24 bit address at first */
	*pSPI_TDBR = ShiftValue;	/* Send the byte to the SPI device */
	SSYNC();
	Wait_For_SPIF();	/* Wait until the instruction has been sent */
	ShiftValue = (ulStart >> 8);	/* Send the middle byte of the 24 bit address  at second */
	*pSPI_TDBR = ShiftValue;	/* Send the byte to the SPI device */
	SSYNC();
	Wait_For_SPIF();	/* Wait until the instruction has been sent */
	*pSPI_TDBR = ulStart;	/* Send the lowest byte of the 24 bit address finally */
	SSYNC();
	Wait_For_SPIF();	/* Wait until the instruction has been sent */

	/* After the SPI device address has been placed on the MOSI pin the data can be */
	/* received on the MISO pin. */
	for (i = 0; i < lCount; i++) {
		*pSPI_TDBR = 0;	/*send dummy */
		SSYNC();
		while (!(*pSPI_STAT & RXS)) ;
		*cnData++ = *pSPI_RDBR;	/*read  */

		if ((i >= SECTOR_SIZE) && (i % SECTOR_SIZE == 0))
			printf(".");
	}

	SPI_OFF();		/* Turn off the SPI */

	return NO_ERR;
}

ERROR_CODE WriteFlash(unsigned long ulStartAddr, long lTransferCount,
		      int *iDataSource, long *lWriteCount)
{

	unsigned long ulWAddr;
	long lWTransferCount = 0;
	int i;
	char iData;
	char *temp = (char *)iDataSource;
	ERROR_CODE ErrorCode = NO_ERR;	/* tells us if there was an error erasing flash */

	/* First, a Write Enable Command must be sent to the SPI. */
	SendSingleCommand(SPI_WREN);

	/* Second, the SPI Status Register will be tested whether the  */
	/*         Write Enable Bit has been set.  */
	ErrorCode = Wait_For_WEL();
	if (POLL_TIMEOUT == ErrorCode) {
		printf("SPI Write Time Out\n");
		return ErrorCode;
	} else
		/* Third, the 24 bit address will be shifted out the SPI MOSI bytewise. */
		SetupSPI((COMMON_SPI_SETTINGS | TIMOD01));	/* Turns the SPI on */
	*pSPI_TDBR = SPI_PP;
	SSYNC();
	Wait_For_SPIF();	/*wait until the instruction has been sent */
	ulWAddr = (ulStartAddr >> 16);
	*pSPI_TDBR = ulWAddr;
	SSYNC();
	Wait_For_SPIF();	/*wait until the instruction has been sent */
	ulWAddr = (ulStartAddr >> 8);
	*pSPI_TDBR = ulWAddr;
	SSYNC();
	Wait_For_SPIF();	/*wait until the instruction has been sent */
	ulWAddr = ulStartAddr;
	*pSPI_TDBR = ulWAddr;
	SSYNC();
	Wait_For_SPIF();	/*wait until the instruction has been sent */
	/* Fourth, maximum number of 256 bytes will be taken from the Buffer */
	/* and sent to the SPI device. */
	for (i = 0; (i < lTransferCount) && (i < 256); i++, lWTransferCount++) {
		iData = *temp;
		*pSPI_TDBR = iData;
		SSYNC();
		Wait_For_SPIF();	/*wait until the instruction has been sent */
		temp++;
	}

	SPI_OFF();		/* Turns the SPI off */

	/* Sixth, the SPI Write in Progress Bit must be toggled to ensure the  */
	/* programming is done before start of next transfer. */
	ErrorCode = Wait_For_Status(WIP);

	if (POLL_TIMEOUT == ErrorCode) {
		printf("SPI Program Time out!\n");
		return ErrorCode;
	} else

		*lWriteCount = lWTransferCount;

	return ErrorCode;
}

ERROR_CODE WriteData(unsigned long ulStart, long lCount, int *pnData)
{

	unsigned long ulWStart = ulStart;
	long lWCount = lCount, lWriteCount;
	long *pnWriteCount = &lWriteCount;

	ERROR_CODE ErrorCode = NO_ERR;

	while (lWCount != 0) {
		ErrorCode = WriteFlash(ulWStart, lWCount, pnData, pnWriteCount);

		/* After each function call of WriteFlash the counter must be adjusted */
		lWCount -= *pnWriteCount;

		/* Also, both address pointers must be recalculated. */
		ulWStart += *pnWriteCount;
		pnData += *pnWriteCount / 4;
	}

	/* return the appropriate error code */
	return ErrorCode;
}

#endif				/* CONFIG_SPI */
OpenPOWER on IntegriCloud