summaryrefslogtreecommitdiffstats
path: root/sys/boot/arm/at91/libat91/spi_flash.c
blob: ab5f01e234b78655ccfc2ce83dfb022da9d6a321 (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
/******************************************************************************
 *
 * Filename: spi_flash.c
 *
 * Instantiation of SPI flash control routines supporting AT45DB161B
 *
 * Revision information:
 *
 * 17JAN2005	kb_admin	initial creation
 *				adapted from external sources
 *				tested for basic operation only!!!
 *
 * BEGIN_KBDD_BLOCK
 * No warranty, expressed or implied, is included with this software.  It is
 * provided "AS IS" and no warranty of any kind including statutory or aspects
 * relating to merchantability or fitness for any purpose is provided.  All
 * intellectual property rights of others is maintained with the respective
 * owners.  This software is not copyrighted and is intended for reference
 * only.
 * END_BLOCK
 *
 * $FreeBSD$
 *****************************************************************************/

#include "at91rm9200.h"
#include "spi_flash.h"
#include "lib.h"

/*********************** PRIVATE FUNCTIONS/DATA ******************************/


static spiCommand_t	spi_command;
static char		tx_commandBuffer[8], rx_commandBuffer[8];

/*
 * .KB_C_FN_DEFINITION_START
 * void SendCommand(spiCommand_t *pCommand)
 *  Private function sends 8-bit value to the device and returns the 8-bit
 * value in response.
 * .KB_C_FN_DEFINITION_END
 */
static void
SendCommand(spiCommand_t *pCommand)
{
	AT91PS_SPI	pSPI = AT91C_BASE_SPI;

	pSPI->SPI_PTCR = AT91C_PDC_TXTDIS | AT91C_PDC_RXTDIS;

	pSPI->SPI_RPR = (unsigned)pCommand->rx_cmd;
	pSPI->SPI_RCR = pCommand->rx_cmd_size;
	pSPI->SPI_TPR = (unsigned)pCommand->tx_cmd;
	pSPI->SPI_TCR = pCommand->tx_cmd_size;

	pSPI->SPI_TNPR = (unsigned)pCommand->tx_data;
	pSPI->SPI_TNCR = pCommand->tx_data_size;
	pSPI->SPI_RNPR = (unsigned)pCommand->rx_data;
	pSPI->SPI_RNCR = pCommand->rx_data_size;

	pSPI->SPI_PTCR = AT91C_PDC_TXTEN | AT91C_PDC_RXTEN;

	// wait for completion
	while (!(pSPI->SPI_SR & AT91C_SPI_SPENDRX))
		Delay(700);
}


/*
 * .KB_C_FN_DEFINITION_START
 * char GetFlashStatus(void)
 *  Private function to return device status.
 * .KB_C_FN_DEFINITION_END
 */
static char
GetFlashStatus(void)
{
	p_memset((char *)&spi_command, 0, sizeof(spi_command));
	p_memset(tx_commandBuffer, 0, 8);
	tx_commandBuffer[0] = STATUS_REGISTER_READ;
	p_memset(rx_commandBuffer, 0, 8);
	spi_command.tx_cmd = tx_commandBuffer;
	spi_command.rx_cmd = rx_commandBuffer;
	spi_command.rx_cmd_size = 2;
	spi_command.tx_cmd_size = 2;
	SendCommand(&spi_command);
	return (rx_commandBuffer[1]);
}

/*
 * .KB_C_FN_DEFINITION_START
 * void WaitForDeviceReady(void)
 *  Private function to poll until the device is ready for next operation.
 * .KB_C_FN_DEFINITION_END
 */
static void
WaitForDeviceReady(void)
{
	while (!(GetFlashStatus() & 0x80)) ;
}

/*************************** GLOBAL FUNCTIONS ********************************/


/*
 * .KB_C_FN_DEFINITION_START
 * void SPI_ReadFlash(unsigned flash_addr, unsigned dest_addr, unsigned size)
 *  Global function to read the SPI flash device using the continuous read
 * array command.
 * .KB_C_FN_DEFINITION_END
 */
void
SPI_ReadFlash(unsigned flash_addr, char *dest_addr, unsigned size)
{
	unsigned	pageAddress, byteAddress;

	// determine page address
	pageAddress = flash_addr / FLASH_PAGE_SIZE;

	// determine byte address
	byteAddress = flash_addr % FLASH_PAGE_SIZE;

	p_memset(tx_commandBuffer, 0, 8);
#ifdef BOOT_BWCT
	tx_commandBuffer[0] = 0xd2;
	tx_commandBuffer[1] = ((pageAddress >> 6) & 0xFF);
	tx_commandBuffer[2] = ((pageAddress << 2) & 0xFC) |
				((byteAddress >> 8) & 0x3);
	tx_commandBuffer[3] = byteAddress & 0xFF;
	spi_command.tx_cmd = tx_commandBuffer;
	spi_command.tx_cmd_size = 8;
	spi_command.tx_data_size = size;
	spi_command.tx_data = dest_addr;

	p_memset(rx_commandBuffer, 0, 8);
	spi_command.rx_cmd = rx_commandBuffer;
	spi_command.rx_cmd_size = 8;
	spi_command.rx_data_size = size;
	spi_command.rx_data = dest_addr;
#else
	tx_commandBuffer[0] = CONTINUOUS_ARRAY_READ_HF;
	tx_commandBuffer[1] = ((pageAddress >> 5) & 0xFF);
	tx_commandBuffer[2] = ((pageAddress << 3) & 0xF8) |
				((byteAddress >> 8) & 0x7);
	tx_commandBuffer[3] = byteAddress & 0xFF;
	spi_command.tx_cmd = tx_commandBuffer;
	spi_command.tx_cmd_size = 5;
	spi_command.tx_data_size = size;
	spi_command.tx_data = dest_addr;

	p_memset(rx_commandBuffer, 0, 8);
	spi_command.rx_cmd = rx_commandBuffer;
	spi_command.rx_cmd_size = 5;
	spi_command.rx_data_size = size;
	spi_command.rx_data = dest_addr;
#endif

	SendCommand(&spi_command);
}


/*
 * .KB_C_FN_DEFINITION_START
 * void SPI_WriteFlash(unsigned flash_addr, unsigned src_addr, unsigned size)
 *  Global function to program the SPI flash device.  Notice the warning
 * provided in lower-level functions regarding corruption of data in non-
 * page aligned write operations.
 * .KB_C_FN_DEFINITION_END
 */
void
SPI_WriteFlash(unsigned flash_addr, char *src_addr, unsigned size)
{
	unsigned	pageAddress, byteAddress;

	// determine page address
	pageAddress = flash_addr / FLASH_PAGE_SIZE;

	// determine byte address
	byteAddress = flash_addr % FLASH_PAGE_SIZE;

	p_memset(tx_commandBuffer, 0, 8);
#ifdef BOOT_BWCT
	tx_commandBuffer[0] = 0x82;
	tx_commandBuffer[1] = ((pageAddress >> 6) & 0xFF);
	tx_commandBuffer[2] = ((pageAddress << 2) & 0xFC) |
				((byteAddress >> 8) & 0x3);
	tx_commandBuffer[3] = (byteAddress & 0xFF);
#else
	tx_commandBuffer[0] = PROGRAM_THROUGH_BUFFER;
	tx_commandBuffer[1] = ((pageAddress >> 5) & 0xFF);
	tx_commandBuffer[2] = ((pageAddress << 3) & 0xF8) |
				((byteAddress >> 8) & 0x7);
	tx_commandBuffer[3] = (byteAddress & 0xFF);
#endif

	p_memset(rx_commandBuffer, 0, 8);

	spi_command.tx_cmd = tx_commandBuffer;
	spi_command.rx_cmd = rx_commandBuffer;
	spi_command.rx_cmd_size = 4;
	spi_command.tx_cmd_size = 4;

	spi_command.tx_data_size = size;
	spi_command.tx_data = src_addr;
	spi_command.rx_data_size = size;
	spi_command.rx_data = src_addr;

	SendCommand(&spi_command);

	WaitForDeviceReady();
}

/*
 * .KB_C_FN_DEFINITION_START
 * void SPI_InitFlash(void)
 *  Global function to initialize the SPI flash device/accessor functions.
 * .KB_C_FN_DEFINITION_END
 */
void
SPI_InitFlash(void)
{
	AT91PS_PIO	pPio;
	AT91PS_SPI	pSPI = AT91C_BASE_SPI;
	unsigned	value;

	// enable CS0, CLK, MOSI, MISO
	pPio = (AT91PS_PIO)AT91C_BASE_PIOA;
	pPio->PIO_ASR = AT91C_PA3_NPCS0 | AT91C_PA1_MOSI | AT91C_PA0_MISO |
	    AT91C_PA2_SPCK;
	pPio->PIO_PDR = AT91C_PA3_NPCS0 | AT91C_PA1_MOSI | AT91C_PA0_MISO |
	    AT91C_PA2_SPCK;

	// enable clocks to SPI
	AT91C_BASE_PMC->PMC_PCER = 1u << AT91C_ID_SPI;

	// reset the SPI
	pSPI->SPI_CR = AT91C_SPI_SWRST;

	pSPI->SPI_MR = (0xf << 24) | AT91C_SPI_MSTR | AT91C_SPI_MODFDIS |
	    (0xE << 16);

	pSPI->SPI_CSR[0] = AT91C_SPI_CPOL | (4 << 16) | (2 << 8);
	pSPI->SPI_CR = AT91C_SPI_SPIEN;

	pSPI->SPI_PTCR = AT91C_PDC_TXTDIS;
	pSPI->SPI_PTCR = AT91C_PDC_RXTDIS;
	pSPI->SPI_RNPR = 0;
	pSPI->SPI_RNCR = 0;
	pSPI->SPI_TNPR = 0;
	pSPI->SPI_TNCR = 0;
	pSPI->SPI_RPR = 0;
	pSPI->SPI_RCR = 0;
	pSPI->SPI_TPR = 0;
	pSPI->SPI_TCR = 0;
	pSPI->SPI_PTCR = AT91C_PDC_RXTEN;
	pSPI->SPI_PTCR = AT91C_PDC_TXTEN;

	value = pSPI->SPI_RDR;
	value = pSPI->SPI_SR;

	// Increment real time counter every SLCK
	AT91C_BASE_ST->ST_RTMR = 1;

#ifdef BOOT_BWCT
	if (((value = GetFlashStatus()) & 0xFC) != 0xB4)
		printf(" Bad SPI status: 0x%x\n", value);
#else
	if (((value = GetFlashStatus()) & 0xFC) != 0xBC)
		printf(" Bad SPI status: 0x%x\n", value);
#endif
}
OpenPOWER on IntegriCloud