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