1
2
3
4
5
6
7#include <config.h>
8#include <common.h>
9#include <dataflash.h>
10
11
12
13
14extern unsigned int AT91F_SpiWrite(AT91PS_DataflashDesc pDesc);
15extern void AT91F_SpiEnable(int cs);
16
17#define AT91C_TIMEOUT_WRDY 200000
18
19
20
21
22
23AT91S_DataFlashStatus AT91F_DataFlashSendCommand(AT91PS_DataFlash pDataFlash,
24 unsigned char OpCode,
25 unsigned int CmdSize,
26 unsigned int DataflashAddress)
27{
28 unsigned int adr;
29
30 if ((pDataFlash->pDataFlashDesc->state) != IDLE)
31 return DATAFLASH_BUSY;
32
33
34 adr = ((DataflashAddress / (pDataFlash->pDevice->pages_size)) <<
35 pDataFlash->pDevice->page_offset) +
36 (DataflashAddress % (pDataFlash->pDevice->pages_size));
37
38
39 pDataFlash->pDataFlashDesc->command[0] = OpCode;
40 if (pDataFlash->pDevice->pages_number >= 16384) {
41 pDataFlash->pDataFlashDesc->command[1] =
42 (unsigned char)((adr & 0x0F000000) >> 24);
43 pDataFlash->pDataFlashDesc->command[2] =
44 (unsigned char)((adr & 0x00FF0000) >> 16);
45 pDataFlash->pDataFlashDesc->command[3] =
46 (unsigned char)((adr & 0x0000FF00) >> 8);
47 pDataFlash->pDataFlashDesc->command[4] =
48 (unsigned char)(adr & 0x000000FF);
49 } else {
50 pDataFlash->pDataFlashDesc->command[1] =
51 (unsigned char)((adr & 0x00FF0000) >> 16);
52 pDataFlash->pDataFlashDesc->command[2] =
53 (unsigned char)((adr & 0x0000FF00) >> 8);
54 pDataFlash->pDataFlashDesc->command[3] =
55 (unsigned char)(adr & 0x000000FF);
56 pDataFlash->pDataFlashDesc->command[4] = 0;
57 }
58 pDataFlash->pDataFlashDesc->command[5] = 0;
59 pDataFlash->pDataFlashDesc->command[6] = 0;
60 pDataFlash->pDataFlashDesc->command[7] = 0;
61
62
63 pDataFlash->pDataFlashDesc->tx_cmd_pt =
64 pDataFlash->pDataFlashDesc->command;
65 pDataFlash->pDataFlashDesc->tx_cmd_size = CmdSize;
66 pDataFlash->pDataFlashDesc->rx_cmd_pt =
67 pDataFlash->pDataFlashDesc->command;
68 pDataFlash->pDataFlashDesc->rx_cmd_size = CmdSize;
69
70
71 return AT91F_SpiWrite(pDataFlash->pDataFlashDesc);
72}
73
74
75
76
77
78AT91S_DataFlashStatus AT91F_DataFlashGetStatus(AT91PS_DataflashDesc pDesc)
79{
80 AT91S_DataFlashStatus status;
81
82
83 if ((pDesc->state) != IDLE)
84 return DATAFLASH_BUSY;
85
86
87 pDesc->command[0] = DB_STATUS;
88 pDesc->command[1] = 0;
89
90 pDesc->DataFlash_state = GET_STATUS;
91 pDesc->tx_data_size = 0;
92
93 pDesc->tx_cmd_pt = pDesc->command;
94 pDesc->rx_cmd_pt = pDesc->command;
95 pDesc->rx_cmd_size = 2;
96 pDesc->tx_cmd_size = 2;
97 status = AT91F_SpiWrite(pDesc);
98
99 pDesc->DataFlash_state = *((unsigned char *)(pDesc->rx_cmd_pt) + 1);
100
101 return status;
102}
103
104
105
106
107
108AT91S_DataFlashStatus AT91F_DataFlashWaitReady(AT91PS_DataflashDesc
109 pDataFlashDesc,
110 unsigned int timeout)
111{
112 pDataFlashDesc->DataFlash_state = IDLE;
113
114 do {
115 AT91F_DataFlashGetStatus(pDataFlashDesc);
116 timeout--;
117 } while (((pDataFlashDesc->DataFlash_state & 0x80) != 0x80) &&
118 (timeout > 0));
119
120 if ((pDataFlashDesc->DataFlash_state & 0x80) != 0x80)
121 return DATAFLASH_ERROR;
122
123 return DATAFLASH_OK;
124}
125
126
127
128
129
130
131
132
133
134
135AT91S_DataFlashStatus AT91F_DataFlashContinuousRead(
136 AT91PS_DataFlash pDataFlash,
137 int src,
138 unsigned char *dataBuffer,
139 int sizeToRead)
140{
141 AT91S_DataFlashStatus status;
142
143 if ((src + sizeToRead) >
144 (pDataFlash->pDevice->pages_size *
145 (pDataFlash->pDevice->pages_number)))
146 return DATAFLASH_MEMORY_OVERFLOW;
147
148 pDataFlash->pDataFlashDesc->rx_data_pt = dataBuffer;
149 pDataFlash->pDataFlashDesc->rx_data_size = sizeToRead;
150 pDataFlash->pDataFlashDesc->tx_data_pt = dataBuffer;
151 pDataFlash->pDataFlashDesc->tx_data_size = sizeToRead;
152
153 status = AT91F_DataFlashSendCommand(
154 pDataFlash, DB_CONTINUOUS_ARRAY_READ, 8, src);
155
156 return (status);
157}
158
159
160
161
162
163
164
165
166
167
168AT91S_DataFlashStatus AT91F_DataFlashPagePgmBuf(AT91PS_DataFlash pDataFlash,
169 unsigned char *src,
170 unsigned int dest,
171 unsigned int SizeToWrite)
172{
173 int cmdsize;
174 pDataFlash->pDataFlashDesc->tx_data_pt = src;
175 pDataFlash->pDataFlashDesc->tx_data_size = SizeToWrite;
176 pDataFlash->pDataFlashDesc->rx_data_pt = src;
177 pDataFlash->pDataFlashDesc->rx_data_size = SizeToWrite;
178
179 cmdsize = 4;
180
181 if (pDataFlash->pDevice->pages_number >= 16384)
182 cmdsize = 5;
183 return (AT91F_DataFlashSendCommand(
184 pDataFlash, DB_PAGE_PGM_BUF1, cmdsize, dest));
185}
186
187
188
189
190
191
192
193
194
195AT91S_DataFlashStatus AT91F_MainMemoryToBufferTransfert(
196 AT91PS_DataFlash
197 pDataFlash,
198 unsigned char
199 BufferCommand,
200 unsigned int page)
201{
202 int cmdsize;
203
204 if ((BufferCommand != DB_PAGE_2_BUF1_TRF) &&
205 (BufferCommand != DB_PAGE_2_BUF2_TRF)) {
206 return DATAFLASH_BAD_COMMAND;
207 }
208
209
210 pDataFlash->pDataFlashDesc->tx_data_size = 0;
211 cmdsize = 4;
212 if (pDataFlash->pDevice->pages_number >= 16384)
213 cmdsize = 5;
214 return (AT91F_DataFlashSendCommand(
215 pDataFlash, BufferCommand, cmdsize,
216 page * pDataFlash->pDevice->pages_size));
217}
218
219
220
221
222
223
224
225
226
227
228
229AT91S_DataFlashStatus AT91F_DataFlashWriteBuffer(
230 AT91PS_DataFlash pDataFlash,
231 unsigned char BufferCommand,
232 unsigned char *dataBuffer,
233 unsigned int bufferAddress,
234 int SizeToWrite)
235{
236 int cmdsize;
237
238 if ((BufferCommand != DB_BUF1_WRITE) &&
239 (BufferCommand != DB_BUF2_WRITE)) {
240 return DATAFLASH_BAD_COMMAND;
241 }
242
243
244 if (bufferAddress > pDataFlash->pDevice->pages_size)
245 return DATAFLASH_BAD_ADDRESS;
246
247 if ((pDataFlash->pDataFlashDesc->state) != IDLE)
248 return DATAFLASH_BUSY;
249
250
251 pDataFlash->pDataFlashDesc->command[0] = BufferCommand;
252 pDataFlash->pDataFlashDesc->command[1] = 0;
253 if (pDataFlash->pDevice->pages_number >= 16384) {
254 pDataFlash->pDataFlashDesc->command[2] = 0;
255 pDataFlash->pDataFlashDesc->command[3] =
256 (unsigned char)(((unsigned int)(bufferAddress &
257 pDataFlash->pDevice->
258 byte_mask)) >> 8);
259 pDataFlash->pDataFlashDesc->command[4] =
260 (unsigned char)((unsigned int)bufferAddress & 0x00FF);
261 cmdsize = 5;
262 } else {
263 pDataFlash->pDataFlashDesc->command[2] =
264 (unsigned char)(((unsigned int)(bufferAddress &
265 pDataFlash->pDevice->
266 byte_mask)) >> 8);
267 pDataFlash->pDataFlashDesc->command[3] =
268 (unsigned char)((unsigned int)bufferAddress & 0x00FF);
269 pDataFlash->pDataFlashDesc->command[4] = 0;
270 cmdsize = 4;
271 }
272
273 pDataFlash->pDataFlashDesc->tx_cmd_pt =
274 pDataFlash->pDataFlashDesc->command;
275 pDataFlash->pDataFlashDesc->tx_cmd_size = cmdsize;
276 pDataFlash->pDataFlashDesc->rx_cmd_pt =
277 pDataFlash->pDataFlashDesc->command;
278 pDataFlash->pDataFlashDesc->rx_cmd_size = cmdsize;
279
280 pDataFlash->pDataFlashDesc->rx_data_pt = dataBuffer;
281 pDataFlash->pDataFlashDesc->tx_data_pt = dataBuffer;
282 pDataFlash->pDataFlashDesc->rx_data_size = SizeToWrite;
283 pDataFlash->pDataFlashDesc->tx_data_size = SizeToWrite;
284
285 return AT91F_SpiWrite(pDataFlash->pDataFlashDesc);
286}
287
288
289
290
291
292
293
294
295
296AT91S_DataFlashStatus AT91F_PageErase(
297 AT91PS_DataFlash pDataFlash,
298 unsigned int page)
299{
300 int cmdsize;
301
302
303 pDataFlash->pDataFlashDesc->tx_data_size = 0;
304
305 cmdsize = 4;
306 if (pDataFlash->pDevice->pages_number >= 16384)
307 cmdsize = 5;
308 return (AT91F_DataFlashSendCommand(pDataFlash,
309 DB_PAGE_ERASE, cmdsize,
310 page * pDataFlash->pDevice->pages_size));
311}
312
313
314
315
316
317
318
319
320
321AT91S_DataFlashStatus AT91F_BlockErase(
322 AT91PS_DataFlash pDataFlash,
323 unsigned int block)
324{
325 int cmdsize;
326
327
328 pDataFlash->pDataFlashDesc->tx_data_size = 0;
329 cmdsize = 4;
330 if (pDataFlash->pDevice->pages_number >= 16384)
331 cmdsize = 5;
332 return (AT91F_DataFlashSendCommand(pDataFlash, DB_BLOCK_ERASE, cmdsize,
333 block * 8 *
334 pDataFlash->pDevice->pages_size));
335}
336
337
338
339
340
341
342
343
344
345AT91S_DataFlashStatus AT91F_WriteBufferToMain(AT91PS_DataFlash pDataFlash,
346 unsigned char BufferCommand,
347 unsigned int dest)
348{
349 int cmdsize;
350
351 if ((BufferCommand != DB_BUF1_PAGE_PGM) &&
352 (BufferCommand != DB_BUF1_PAGE_ERASE_PGM) &&
353 (BufferCommand != DB_BUF2_PAGE_PGM) &&
354 (BufferCommand != DB_BUF2_PAGE_ERASE_PGM))
355 return DATAFLASH_BAD_COMMAND;
356
357
358 pDataFlash->pDataFlashDesc->tx_data_size = 0;
359
360 cmdsize = 4;
361 if (pDataFlash->pDevice->pages_number >= 16384)
362 cmdsize = 5;
363
364 return (AT91F_DataFlashSendCommand(pDataFlash, BufferCommand,
365 cmdsize, dest));
366}
367
368
369
370
371
372
373
374
375AT91S_DataFlashStatus AT91F_PartialPageWrite(AT91PS_DataFlash pDataFlash,
376 unsigned char *src,
377 unsigned int dest,
378 unsigned int size)
379{
380 unsigned int page;
381 unsigned int AdrInPage;
382
383 page = dest / (pDataFlash->pDevice->pages_size);
384 AdrInPage = dest % (pDataFlash->pDevice->pages_size);
385
386
387 AT91F_MainMemoryToBufferTransfert(pDataFlash, DB_PAGE_2_BUF1_TRF, page);
388 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
389 AT91C_TIMEOUT_WRDY);
390
391 AT91F_DataFlashWriteBuffer(pDataFlash, DB_BUF1_WRITE, src,
392 AdrInPage, size);
393
394 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
395 AT91C_TIMEOUT_WRDY);
396
397
398 if (pDataFlash->pDevice->pages_number >= 16384) {
399 AT91F_PageErase(pDataFlash, page);
400
401 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
402 AT91C_TIMEOUT_WRDY);
403 }
404
405
406 return (AT91F_WriteBufferToMain(pDataFlash, DB_BUF1_PAGE_ERASE_PGM,
407 (page *
408 pDataFlash->pDevice->pages_size)));
409}
410
411
412
413
414
415
416
417
418AT91S_DataFlashStatus AT91F_DataFlashWrite(AT91PS_DataFlash pDataFlash,
419 unsigned char *src,
420 int dest, int size)
421{
422 unsigned int length;
423 unsigned int page;
424 unsigned int status;
425
426 AT91F_SpiEnable(pDataFlash->pDevice->cs);
427
428 if ((dest + size) > (pDataFlash->pDevice->pages_size *
429 (pDataFlash->pDevice->pages_number)))
430 return DATAFLASH_MEMORY_OVERFLOW;
431
432
433 if ((dest % ((unsigned int)(pDataFlash->pDevice->pages_size))) != 0) {
434 length =
435 pDataFlash->pDevice->pages_size -
436 (dest % ((unsigned int)(pDataFlash->pDevice->pages_size)));
437
438 if (size < length)
439 length = size;
440
441 if (!AT91F_PartialPageWrite(pDataFlash, src, dest, length))
442 return DATAFLASH_ERROR;
443
444 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
445 AT91C_TIMEOUT_WRDY);
446
447
448 size -= length;
449 dest += length;
450 src += length;
451 }
452
453 while ((size - pDataFlash->pDevice->pages_size) >= 0) {
454
455 page = (unsigned int)dest / (pDataFlash->pDevice->pages_size);
456
457 status = AT91F_DataFlashWriteBuffer(pDataFlash,
458 DB_BUF1_WRITE, src, 0,
459 pDataFlash->pDevice->
460 pages_size);
461 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
462 AT91C_TIMEOUT_WRDY);
463
464 status = AT91F_PageErase(pDataFlash, page);
465 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
466 AT91C_TIMEOUT_WRDY);
467 if (!status)
468 return DATAFLASH_ERROR;
469
470 status = AT91F_WriteBufferToMain(pDataFlash,
471 DB_BUF1_PAGE_PGM, dest);
472 if (!status)
473 return DATAFLASH_ERROR;
474
475 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
476 AT91C_TIMEOUT_WRDY);
477
478
479 size -= pDataFlash->pDevice->pages_size;
480 dest += pDataFlash->pDevice->pages_size;
481 src += pDataFlash->pDevice->pages_size;
482 }
483
484
485 if (size > 0) {
486
487 if (!AT91F_PartialPageWrite(pDataFlash, src, dest, size))
488 return DATAFLASH_ERROR;
489
490 AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
491 AT91C_TIMEOUT_WRDY);
492 }
493 return DATAFLASH_OK;
494}
495
496
497
498
499
500
501
502int AT91F_DataFlashRead(AT91PS_DataFlash pDataFlash,
503 unsigned long addr, unsigned long size, char *buffer)
504{
505 unsigned long SizeToRead;
506
507 AT91F_SpiEnable(pDataFlash->pDevice->cs);
508
509 if (AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
510 AT91C_TIMEOUT_WRDY) != DATAFLASH_OK)
511 return -1;
512
513 while (size) {
514 SizeToRead = (size < 0x8000) ? size : 0x8000;
515
516 if (AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc,
517 AT91C_TIMEOUT_WRDY) !=
518 DATAFLASH_OK)
519 return -1;
520
521 if (AT91F_DataFlashContinuousRead(pDataFlash, addr,
522 (uchar *) buffer,
523 SizeToRead) != DATAFLASH_OK)
524 return -1;
525
526 size -= SizeToRead;
527 addr += SizeToRead;
528 buffer += SizeToRead;
529 }
530
531 return DATAFLASH_OK;
532}
533
534
535
536
537
538
539
540int AT91F_DataflashProbe(int cs, AT91PS_DataflashDesc pDesc)
541{
542 AT91F_SpiEnable(cs);
543 AT91F_DataFlashGetStatus(pDesc);
544 return ((pDesc->command[1] == 0xFF) ? 0 : pDesc->command[1] & 0x3C);
545}
546