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
36
37
38
39
40
43
49
50
56
57
58
59
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
135
136
137
138
139
140
141
142
143
147
148
149
150
151
152
156
157
158
159
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
191
192
193
197
198
199
200
201
205
206
210
211
212
217
218
219
220
221
222
223
224
225
229
230
231
232
236
237
238
239
243
244
245
246
247
248
249
250
251
252
253
254
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
276
277
278
279
280
281
282
283
284
285
286
290
291
292
293
294
295
296
297
298
299
300
301
305
306
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
327
328
329
330
334
335
336
337
338
339
343
344
345
346
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
374
375
376
377
378
379
380
381
382
383
384
385
390
391
421
422
423
424
425
426
427
428
429
430
431
436
437
455
456
457
458
459
460
461
462
463
464
465
470
471
489
490
491
492
493
494
495
496
497
498
499
504
505
523
524
525
526
527
528
529
530
531
532
533
538
539
556
557
558
559
560
564
565
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
594
595
596
597
598
602
603
604
605
609
610
611
615
616
624
625
626
627
631
632
636
637
638
639
643
644
645
649
650
651
655
656
657
658
659
660
661
662
667
668
669
674
675
676
677
681
682
683
684
688
689
690
694
695
696
697
701
702
707
708
709
710
711
715
716
717
721
722
727
728
729
730
731
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
895
896
897
898
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
920
921
922
923
927
928
929
930
934
935
936
937
941
942
943
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
968
969
970
974
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1037
1038
1039
1040
1044
1045
1046
1047
1048
1049
1052
1053
1054
1055
1059
1060
1061
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1079
1080
1081
1082
1083
1084
1085
1086
1087
1091
1092
1093
1094
1095
1096
1100
1101
1102
1103
1104
1105
1109
1110
1111
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1148
1149
1150
1151
1152
1153
1157
1158
1160
1161
1162
1163
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1184
1185
1190
1198
1199
1200
1201
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1222
1223
1227
1232
1233
1238
1239
1243
1244
1245
1246
1247
1248
1257
1258
1259
1260
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1297
1301
1302
1304
1305
1309
1310
1311
1312
1316
1317
1318
1322
1323
1331
1332
1334
1335
1339
1340
1344
1345
1346
1347
1351
1352
1353
1357
1358
1359
1360
1361
1365
1366
1367
1368
1369
1370
1374
1375
1380
1381
1382
1386
1387
1397
1398
1406
1407
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1449
1450
1451
1452
1456
1457
1458
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1476
1477
1478
1479
1480
1484
1485
1486
1487
1488
1489
1493
1494
1495
1503
1504
1505
1506
1507
1508
1512
1513
1514
1515
1516
1517
1518
1519
1520
1524
1525
1526
1527
1528
1529
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1573
1574
1585
1586
1587
1588
1589
1590
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1627
1628
1633
1634
1635
1636
1637
1638
1642
1643
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1667
1668
1669
1670
1671
1672
1673
1674
1676
1677
1681
1682
1687
1688
1689
1690
1691
1692
1696
1697
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1725
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1742
1743
1744
1745
1746
1747
1748
1749
1754
1755
1756
1757
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "imp.h"
#include "lpc32xx.h"
#include <target/target.h>
static int lpc32xx_reset(struct nand_device *nand);
static int lpc32xx_controller_ready(struct nand_device *nand, int timeout);
static int lpc32xx_tc_ready(struct nand_device *nand, int timeout);
/* ... */
#define ECC_OFFS 0x120
#define SPARE_OFFS 0x140
#define DATA_OFFS 0x200
static const int sp_ooblayout[] = {
10, 11, 12, 13, 14, 15
...};
static const int lp_ooblayout[] = {
40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57,
58, 59, 60, 61, 62, 63
...};
struct dmac_ll {
volatile uint32_t dma_src;
volatile uint32_t dma_dest;
volatile uint32_t next_lli;
volatile uint32_t next_ctrl;
...};
static struct dmac_ll dmalist[(2048/256) * 2 + 1];
/* ... */
NAND_DEVICE_COMMAND_HANDLER(lpc32xx_nand_device_command)
{
if (CMD_ARGC < 3)
return ERROR_COMMAND_SYNTAX_ERROR;
uint32_t osc_freq;
COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], osc_freq);
struct lpc32xx_nand_controller *lpc32xx_info;
lpc32xx_info = malloc(sizeof(struct lpc32xx_nand_controller));
nand->controller_priv = lpc32xx_info;
lpc32xx_info->osc_freq = osc_freq;
if ((lpc32xx_info->osc_freq < 1000) || (lpc32xx_info->osc_freq > 20000))
LOG_WARNING("LPC32xx oscillator frequency should be between "
"1000 and 20000 kHz, was %i",
lpc32xx_info->osc_freq);
lpc32xx_info->selected_controller = LPC32XX_NO_CONTROLLER;
lpc32xx_info->sw_write_protection = 0;
lpc32xx_info->sw_wp_lower_bound = 0x0;
lpc32xx_info->sw_wp_upper_bound = 0x0;
return ERROR_OK;
}{ ... }
static int lpc32xx_pll(int fclkin, uint32_t pll_ctrl)
{
int bypass = (pll_ctrl & 0x8000) >> 15;
int direct = (pll_ctrl & 0x4000) >> 14;
int feedback = (pll_ctrl & 0x2000) >> 13;
int p = (1 << ((pll_ctrl & 0x1800) >> 11) * 2);
int n = ((pll_ctrl & 0x0600) >> 9) + 1;
int m = ((pll_ctrl & 0x01fe) >> 1) + 1;
int lock = (pll_ctrl & 0x1);
if (!lock)
LOG_WARNING("PLL is not locked");
if (!bypass && direct)
return (m * fclkin) / n;
if (bypass && !direct)
return fclkin / (2 * p);
if (bypass & direct)
return fclkin;
if (feedback)
return m * (fclkin / n);
else
return (m / (2 * p)) * (fclkin / n);
}{ ... }
static float lpc32xx_cycle_time(struct nand_device *nand)
{
struct lpc32xx_nand_controller *lpc32xx_info = nand->controller_priv;
struct target *target = nand->target;
uint32_t sysclk_ctrl, pwr_ctrl, hclkdiv_ctrl, hclkpll_ctrl;
int sysclk;
int hclk;
int hclk_pll;
float cycle;
int retval;
retval = target_read_u32(target, 0x40004050, &sysclk_ctrl);
if (retval != ERROR_OK) {
LOG_ERROR("could not read SYSCLK_CTRL");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if ((sysclk_ctrl & 1) == 0)
sysclk = lpc32xx_info->osc_freq;
else
sysclk = 13000;
retval = target_read_u32(target, 0x40004044, &pwr_ctrl);
if (retval != ERROR_OK) {
LOG_ERROR("could not read HCLK_CTRL");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if ((pwr_ctrl & (1 << 2)) == 0)
hclk = sysclk;
else {
retval = target_read_u32(target, 0x40004058, &hclkpll_ctrl);
if (retval != ERROR_OK) {
LOG_ERROR("could not read HCLKPLL_CTRL");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
hclk_pll = lpc32xx_pll(sysclk, hclkpll_ctrl);
retval = target_read_u32(target, 0x40004040, &hclkdiv_ctrl);
if (retval != ERROR_OK) {
LOG_ERROR("could not read CLKDIV_CTRL");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (pwr_ctrl & (1 << 10))
hclk = hclk_pll / (((hclkdiv_ctrl & 0x7c) >> 2) + 1);
else
hclk = hclk_pll / (1 << (hclkdiv_ctrl & 0x3));
}else { ... }
LOG_DEBUG("LPC32xx HCLK currently clocked at %i kHz", hclk);
cycle = (1.0 / hclk) * 1000000.0;
return cycle;
}{ ... }
static int lpc32xx_init(struct nand_device *nand)
{
struct lpc32xx_nand_controller *lpc32xx_info = nand->controller_priv;
struct target *target = nand->target;
int bus_width = nand->bus_width ? nand->bus_width : 8;
int address_cycles = nand->address_cycles ? nand->address_cycles : 3;
int page_size = nand->page_size ? nand->page_size : 512;
int retval;
if (target->state != TARGET_HALTED) {
LOG_ERROR("target must be halted to use LPC32xx "
"NAND flash controller");
return ERROR_NAND_OPERATION_FAILED;
}if (target->state != TARGET_HALTED) { ... }
if (bus_width != 8) {
LOG_ERROR("LPC32xx doesn't support %i", bus_width);
return ERROR_NAND_OPERATION_NOT_SUPPORTED;
}if (bus_width != 8) { ... }
nand->bus_width = bus_width;
if ((address_cycles < 3) || (address_cycles > 5)) {
LOG_ERROR("LPC32xx driver doesn't support %i address cycles", address_cycles);
return ERROR_NAND_OPERATION_NOT_SUPPORTED;
}if ((address_cycles < 3) || (address_cycles > 5)) { ... }
if ((page_size != 512) && (page_size != 2048)) {
LOG_ERROR("LPC32xx doesn't support page size %i", page_size);
return ERROR_NAND_OPERATION_NOT_SUPPORTED;
}if ((page_size != 512) && (page_size != 2048)) { ... }
if (lpc32xx_info->selected_controller == LPC32XX_NO_CONTROLLER) {
LOG_DEBUG("no LPC32xx NAND flash controller selected, "
"using default 'slc'");
lpc32xx_info->selected_controller = LPC32XX_SLC_CONTROLLER;
}if (lpc32xx_info->selected_controller == LPC32XX_NO_CONTROLLER) { ... }
if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) {
uint32_t mlc_icr_value = 0x0;
float cycle;
int twp, twh, trp, treh, trhz, trbwb, tcea;
retval = target_write_u32(target, 0x400040c8, 0x22);
if (retval != ERROR_OK) {
LOG_ERROR("could not set FLASHCLK_CTRL");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x200b804c, 0x0);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_CEH");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x200b8044, 0xa25e);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_LOCK");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (lpc32xx_info->sw_write_protection)
mlc_icr_value |= 0x8;
if (page_size == 2048)
mlc_icr_value |= 0x4;
if (address_cycles == 4)
mlc_icr_value |= 0x2;
if (bus_width == 16)
mlc_icr_value |= 0x1;
retval = target_write_u32(target, 0x200b8030, mlc_icr_value);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ICR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
cycle = lpc32xx_cycle_time(nand);
twp = ((40 / cycle) + 1);
twh = ((20 / cycle) + 1);
trp = ((30 / cycle) + 1);
treh = ((15 / cycle) + 1);
trhz = ((30 / cycle) + 1);
trbwb = ((100 / cycle) + 1);
tcea = ((45 / cycle) + 1);
retval = target_write_u32(target, 0x200b8044, 0xa25e);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_LOCK");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x200b8034,
(twp & 0xf)
| ((twh & 0xf) << 4)
| ((trp & 0xf) << 8)
| ((treh & 0xf) << 12)
| ((trhz & 0x7) << 16)
| ((trbwb & 0x1f) << 19)
| ((tcea & 0x3) << 24));
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_TIME_REG");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = lpc32xx_reset(nand);
if (retval != ERROR_OK)
return ERROR_NAND_OPERATION_FAILED;
}if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) {
float cycle;
int r_setup, r_hold, r_width, r_rdy;
int w_setup, w_hold, w_width, w_rdy;
retval = target_write_u32(target, 0x400040c8, 0x05);
if (retval != ERROR_OK) {
LOG_ERROR("could not set FLASHCLK_CTRL");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
/* ... */
retval = lpc32xx_reset(nand);
if (retval != ERROR_OK)
return ERROR_NAND_OPERATION_FAILED;
/* ... */
retval = target_write_u32(target, 0x20020014,
0x3e | ((bus_width == 16) ? 1 : 0));
if (retval != ERROR_OK) {
LOG_ERROR("could not set SLC_CFG");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x20020020, 0x03);
if (retval != ERROR_OK) {
LOG_ERROR("could not set SLC_IEN");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x400040e8, 0x01);
if (retval != ERROR_OK) {
LOG_ERROR("could not set DMACLK_CTRL");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x31000030, 0x01);
if (retval != ERROR_OK) {
LOG_ERROR("could not set DMACConfig");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
cycle = lpc32xx_cycle_time(nand);
r_setup = w_setup = 0;
r_hold = w_hold = 10 / cycle;
r_width = 30 / cycle;
w_width = 40 / cycle;
r_rdy = w_rdy = 100 / cycle;
retval = target_write_u32(target, 0x2002002c,
(r_setup & 0xf)
| ((r_hold & 0xf) << 4)
| ((r_width & 0xf) << 8)
| ((r_rdy & 0xf) << 12)
| ((w_setup & 0xf) << 16)
| ((w_hold & 0xf) << 20)
| ((w_width & 0xf) << 24)
| ((w_rdy & 0xf) << 28));
if (retval != ERROR_OK) {
LOG_ERROR("could not set SLC_TAC");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
}else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) { ... }
return ERROR_OK;
}{ ... }
static int lpc32xx_reset(struct nand_device *nand)
{
struct lpc32xx_nand_controller *lpc32xx_info = nand->controller_priv;
struct target *target = nand->target;
int retval;
if (target->state != TARGET_HALTED) {
LOG_ERROR("target must be halted to use "
"LPC32xx NAND flash controller");
return ERROR_NAND_OPERATION_FAILED;
}if (target->state != TARGET_HALTED) { ... }
if (lpc32xx_info->selected_controller == LPC32XX_NO_CONTROLLER) {
LOG_ERROR("BUG: no LPC32xx NAND flash controller selected");
return ERROR_NAND_OPERATION_FAILED;
}if (lpc32xx_info->selected_controller == LPC32XX_NO_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) {
retval = target_write_u32(target, 0x200b8000, 0xff);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_CMD");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (!lpc32xx_controller_ready(nand, 100)) {
LOG_ERROR("LPC32xx MLC NAND controller timed out "
"after reset");
return ERROR_NAND_OPERATION_TIMEOUT;
}if (!lpc32xx_controller_ready(nand, 100)) { ... }
}else if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) {
retval = target_write_u32(target, 0x20020010, 0x6);
if (retval != ERROR_OK) {
LOG_ERROR("could not set SLC_CTRL");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (!lpc32xx_controller_ready(nand, 100)) {
LOG_ERROR("LPC32xx SLC NAND controller timed out "
"after reset");
return ERROR_NAND_OPERATION_TIMEOUT;
}if (!lpc32xx_controller_ready(nand, 100)) { ... }
}else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) { ... }
return ERROR_OK;
}{ ... }
static int lpc32xx_command(struct nand_device *nand, uint8_t command)
{
struct lpc32xx_nand_controller *lpc32xx_info = nand->controller_priv;
struct target *target = nand->target;
int retval;
if (target->state != TARGET_HALTED) {
LOG_ERROR("target must be halted to use "
"LPC32xx NAND flash controller");
return ERROR_NAND_OPERATION_FAILED;
}if (target->state != TARGET_HALTED) { ... }
if (lpc32xx_info->selected_controller == LPC32XX_NO_CONTROLLER) {
LOG_ERROR("BUG: no LPC32xx NAND flash controller selected");
return ERROR_NAND_OPERATION_FAILED;
}if (lpc32xx_info->selected_controller == LPC32XX_NO_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) {
retval = target_write_u32(target, 0x200b8000, command);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_CMD");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
}else if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) {
retval = target_write_u32(target, 0x20020008, command);
if (retval != ERROR_OK) {
LOG_ERROR("could not set SLC_CMD");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
}else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) { ... }
return ERROR_OK;
}{ ... }
static int lpc32xx_address(struct nand_device *nand, uint8_t address)
{
struct lpc32xx_nand_controller *lpc32xx_info = nand->controller_priv;
struct target *target = nand->target;
int retval;
if (target->state != TARGET_HALTED) {
LOG_ERROR("target must be halted to use "
"LPC32xx NAND flash controller");
return ERROR_NAND_OPERATION_FAILED;
}if (target->state != TARGET_HALTED) { ... }
if (lpc32xx_info->selected_controller == LPC32XX_NO_CONTROLLER) {
LOG_ERROR("BUG: no LPC32xx NAND flash controller selected");
return ERROR_NAND_OPERATION_FAILED;
}if (lpc32xx_info->selected_controller == LPC32XX_NO_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) {
retval = target_write_u32(target, 0x200b8004, address);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
}else if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) {
retval = target_write_u32(target, 0x20020004, address);
if (retval != ERROR_OK) {
LOG_ERROR("could not set SLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
}else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) { ... }
return ERROR_OK;
}{ ... }
static int lpc32xx_write_data(struct nand_device *nand, uint16_t data)
{
struct lpc32xx_nand_controller *lpc32xx_info = nand->controller_priv;
struct target *target = nand->target;
int retval;
if (target->state != TARGET_HALTED) {
LOG_ERROR("target must be halted to use "
"LPC32xx NAND flash controller");
return ERROR_NAND_OPERATION_FAILED;
}if (target->state != TARGET_HALTED) { ... }
if (lpc32xx_info->selected_controller == LPC32XX_NO_CONTROLLER) {
LOG_ERROR("BUG: no LPC32xx NAND flash controller selected");
return ERROR_NAND_OPERATION_FAILED;
}if (lpc32xx_info->selected_controller == LPC32XX_NO_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) {
retval = target_write_u32(target, 0x200b0000, data);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_DATA");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
}else if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) {
retval = target_write_u32(target, 0x20020000, data);
if (retval != ERROR_OK) {
LOG_ERROR("could not set SLC_DATA");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
}else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) { ... }
return ERROR_OK;
}{ ... }
static int lpc32xx_read_data(struct nand_device *nand, void *data)
{
struct lpc32xx_nand_controller *lpc32xx_info = nand->controller_priv;
struct target *target = nand->target;
int retval;
if (target->state != TARGET_HALTED) {
LOG_ERROR("target must be halted to use LPC32xx "
"NAND flash controller");
return ERROR_NAND_OPERATION_FAILED;
}if (target->state != TARGET_HALTED) { ... }
if (lpc32xx_info->selected_controller == LPC32XX_NO_CONTROLLER) {
LOG_ERROR("BUG: no LPC32xx NAND flash controller selected");
return ERROR_NAND_OPERATION_FAILED;
}if (lpc32xx_info->selected_controller == LPC32XX_NO_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) {
if (nand->bus_width == 8) {
uint8_t *data8 = data;
retval = target_read_u8(target, 0x200b0000, data8);
}if (nand->bus_width == 8) { ... } else {
LOG_ERROR("BUG: bus_width neither 8 nor 16 bit");
return ERROR_NAND_OPERATION_FAILED;
}else { ... }
if (retval != ERROR_OK) {
LOG_ERROR("could not read MLC_DATA");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
}else if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) {
uint32_t data32;
retval = target_read_u32(target, 0x20020000, &data32);
if (retval != ERROR_OK) {
LOG_ERROR("could not read SLC_DATA");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (nand->bus_width == 8) {
uint8_t *data8 = data;
*data8 = data32 & 0xff;
}if (nand->bus_width == 8) { ... } else {
LOG_ERROR("BUG: bus_width neither 8 nor 16 bit");
return ERROR_NAND_OPERATION_FAILED;
}else { ... }
}else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) { ... }
return ERROR_OK;
}{ ... }
static int lpc32xx_write_page_mlc(struct nand_device *nand, uint32_t page,
uint8_t *data, uint32_t data_size,
uint8_t *oob, uint32_t oob_size)
{
struct target *target = nand->target;
int retval;
uint8_t status;
static uint8_t page_buffer[512];
static uint8_t oob_buffer[6];
int quarter, num_quarters;
retval = target_write_u32(target, 0x200b8000, NAND_CMD_SEQIN);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_CMD");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (nand->page_size == 512) {
retval = target_write_u32(target, 0x200b8004, 0x0);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x200b8004, page & 0xff);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x200b8004,
(page >> 8) & 0xff);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (nand->address_cycles == 4) {
retval = target_write_u32(target, 0x200b8004,
(page >> 16) & 0xff);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
}if (nand->address_cycles == 4) { ... }
}if (nand->page_size == 512) { ... } else {
retval = target_write_u32(target, 0x200b8004, 0x0);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x200b8004, 0x0);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x200b8004, page & 0xff);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x200b8004,
(page >> 8) & 0xff);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
}else { ... }
/* ... */
num_quarters = (nand->page_size == 2048) ? 4 : 1;
for (quarter = 0; quarter < num_quarters; quarter++) {
int thisrun_data_size = (data_size > 512) ? 512 : data_size;
int thisrun_oob_size = (oob_size > 6) ? 6 : oob_size;
memset(page_buffer, 0xff, 512);
if (data) {
memcpy(page_buffer, data, thisrun_data_size);
data_size -= thisrun_data_size;
data += thisrun_data_size;
}if (data) { ... }
memset(oob_buffer, 0xff, 6);
if (oob) {
memcpy(oob_buffer, oob, thisrun_oob_size);
oob_size -= thisrun_oob_size;
oob += thisrun_oob_size;
}if (oob) { ... }
retval = target_write_u32(target, 0x200b8008, 0x0);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ECC_ENC_REG");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_memory(target, 0x200a8000,
4, 128, page_buffer);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_BUF (data)");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_memory(target, 0x200a8000,
1, 6, oob_buffer);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_BUF (oob)");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x200b8010, 0x0);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ECC_AUTO_ENC_REG");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (!lpc32xx_controller_ready(nand, 1000)) {
LOG_ERROR("timeout while waiting for "
"completion of auto encode cycle");
return ERROR_NAND_OPERATION_FAILED;
}if (!lpc32xx_controller_ready(nand, 1000)) { ... }
}for (quarter = 0; quarter < num_quarters; quarter++) { ... }
retval = target_write_u32(target, 0x200b8000, NAND_CMD_PAGEPROG);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_CMD");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = nand_read_status(nand, &status);
if (retval != ERROR_OK) {
LOG_ERROR("couldn't read status");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (status & NAND_STATUS_FAIL) {
LOG_ERROR("write operation didn't pass, status: 0x%2.2x",
status);
return ERROR_NAND_OPERATION_FAILED;
}if (status & NAND_STATUS_FAIL) { ... }
return ERROR_OK;
}{ ... }
/* ... */
static int lpc32xx_make_dma_list(uint32_t target_mem_base, uint32_t page_size,
int do_read)
{
uint32_t i, dmasrc, ctrl, ecc_ctrl, oob_ctrl, dmadst;
/* ... */
ctrl = (0x40 | 3 << 12 | 3 << 15 | 2 << 18 | 2 << 21 | 0 << 24
| 0 << 25 | 0 << 26 | 0 << 27 | 0 << 31);
/* ... */
ecc_ctrl = 0x01 | 1 << 12 | 1 << 15 | 2 << 18 | 2 << 21 | 0 << 24
| 0 << 25 | 0 << 26 | 1 << 27 | 0 << 31;
/* ... */
oob_ctrl = (page_size == 2048 ? 0x10 : 0x04)
| 3 << 12 | 3 << 15 | 2 << 18 | 2 << 21 | 0 << 24
| 0 << 25 | 0 << 26 | 0 << 27 | 1 << 31;
if (do_read) {
ctrl |= 1 << 27;
oob_ctrl |= 1 << 27;
dmasrc = 0x20020038;
dmadst = target_mem_base + DATA_OFFS;
}if (do_read) { ... } else {
ctrl |= 1 << 26;
oob_ctrl |= 1 << 26;
dmasrc = target_mem_base + DATA_OFFS;
dmadst = 0x20020038;
}else { ... }
/* ... */
for (i = 0; i < page_size/0x100; i++) {
dmalist[i*2].dma_src = (do_read ? dmasrc : (dmasrc + i * 256));
dmalist[i*2].dma_dest = (do_read ? (dmadst + i * 256) : dmadst);
dmalist[i*2].next_lli =
target_mem_base + (i*2 + 1) * sizeof(struct dmac_ll);
dmalist[i*2].next_ctrl = ctrl;
dmalist[(i*2) + 1].dma_src = 0x20020034;
dmalist[(i*2) + 1].dma_dest =
target_mem_base + ECC_OFFS + i * 4;
dmalist[(i*2) + 1].next_lli =
target_mem_base + (i*2 + 2) * sizeof(struct dmac_ll);
dmalist[(i*2) + 1].next_ctrl = ecc_ctrl;
}for (i = 0; i < page_size/0x100; i++) { ... }
if (do_read)
dmadst = target_mem_base + SPARE_OFFS;
else {
dmasrc = target_mem_base + SPARE_OFFS;
dmalist[(i*2) - 1].next_lli = 0;
dmalist[(i*2) - 1].next_ctrl |= (1 << 31);
}else { ... }
dmalist[i*2].dma_src = dmasrc;
dmalist[i*2].dma_dest = dmadst;
dmalist[i*2].next_lli = 0;
dmalist[i*2].next_ctrl = oob_ctrl;
return i * 2 + 1;
}{ ... }
static int lpc32xx_start_slc_dma(struct nand_device *nand, uint32_t count,
int do_wait)
{
struct target *target = nand->target;
int retval;
retval = target_write_u32(target, 0x31000008, 1);
if (retval != ERROR_OK) {
LOG_ERROR("Could not set DMACIntTCClear");
return retval;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x31000010, 1);
if (retval != ERROR_OK) {
LOG_ERROR("Could not set DMACIntErrClear");
return retval;
}if (retval != ERROR_OK) { ... }
/* ... */
retval = target_write_u32(target, 0x31000110,
1 | 1<<1 | 1<<6 | 2<<11 | 0<<14
| 0<<15 | 0<<16 | 0<<18);
if (retval != ERROR_OK) {
LOG_ERROR("Could not set DMACC0Config");
return retval;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x20020010, 0x3);
if (retval != ERROR_OK) {
LOG_ERROR("Could not set SLC_CTRL");
return retval;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x20020028, 2);
if (retval != ERROR_OK) {
LOG_ERROR("Could not set SLC_ICR");
return retval;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x20020030, count);
if (retval != ERROR_OK) {
LOG_ERROR("lpc32xx_start_slc_dma: Could not set SLC_TC");
return retval;
}if (retval != ERROR_OK) { ... }
if (do_wait && !lpc32xx_tc_ready(nand, 100)) {
LOG_ERROR("timeout while waiting for completion of DMA");
return ERROR_NAND_OPERATION_FAILED;
}if (do_wait && !lpc32xx_tc_ready(nand, 100)) { ... }
return retval;
}{ ... }
static int lpc32xx_dma_ready(struct nand_device *nand, int timeout)
{
struct target *target = nand->target;
LOG_DEBUG("lpc32xx_dma_ready count start=%d", timeout);
do {
uint32_t tc_stat;
uint32_t err_stat;
int retval;
retval = target_read_u32(target, 0x31000014, &tc_stat);
if (retval != ERROR_OK) {
LOG_ERROR("Could not read DMACRawIntTCStat");
return 0;
}if (retval != ERROR_OK) { ... }
retval = target_read_u32(target, 0x31000018, &err_stat);
if (retval != ERROR_OK) {
LOG_ERROR("Could not read DMACRawIntErrStat");
return 0;
}if (retval != ERROR_OK) { ... }
if ((tc_stat | err_stat) & 1) {
LOG_DEBUG("lpc32xx_dma_ready count=%d",
timeout);
if (err_stat & 1) {
LOG_ERROR("lpc32xx_dma_ready "
"DMA error, aborted");
return 0;
}if (err_stat & 1) { ... } else
return 1;
}if ((tc_stat | err_stat) & 1) { ... }
alive_sleep(1);
...} while (timeout-- > 0);
return 0;
}{ ... }
static uint32_t slc_ecc_copy_to_buffer(uint8_t *spare,
const uint32_t *ecc, int count)
{
int i;
for (i = 0; i < (count * 3); i += 3) {
uint32_t ce = ecc[i/3];
ce = ~(ce << 2) & 0xFFFFFF;
spare[i+2] = (uint8_t)(ce & 0xFF); ce >>= 8;
spare[i+1] = (uint8_t)(ce & 0xFF); ce >>= 8;
spare[i] = (uint8_t)(ce & 0xFF);
}for (i = 0; i < (count * 3); i += 3) { ... }
return 0;
}{ ... }
static void lpc32xx_dump_oob(uint8_t *oob, uint32_t oob_size)
{
int addr = 0;
while (oob_size > 0) {
LOG_DEBUG("%02x: %02x %02x %02x %02x %02x %02x %02x %02x", addr,
oob[0], oob[1], oob[2], oob[3],
oob[4], oob[5], oob[6], oob[7]);
oob += 8;
addr += 8;
oob_size -= 8;
}while (oob_size > 0) { ... }
}{ ... }
static int lpc32xx_write_page_slc(struct nand_device *nand,
struct working_area *pworking_area,
uint32_t page, uint8_t *data,
uint32_t data_size, uint8_t *oob,
uint32_t oob_size)
{
struct target *target = nand->target;
int retval;
uint32_t target_mem_base;
LOG_DEBUG("SLC write page %" PRIx32 " data=%d, oob=%d, "
"data_size=%" PRIu32 ", oob_size=%" PRIu32,
page, !!data, !!oob, data_size, oob_size);
target_mem_base = pworking_area->address;
/* ... */
if (data && !oob) {
uint32_t i, all_ff = 1;
for (i = 0; i < data_size; i++)
if (data[i] != 0xFF) {
all_ff = 0;
break;
}if (data[i] != 0xFF) { ... }
if (all_ff)
return ERROR_OK;
}if (data && !oob) { ... }
int nll = lpc32xx_make_dma_list(target_mem_base, nand->page_size, 0);
/* ... */
retval = target_write_memory(target, target_mem_base, 4,
nll * sizeof(struct dmac_ll) / 4,
(uint8_t *)dmalist);
if (retval != ERROR_OK) {
LOG_ERROR("Could not write DMA descriptors to IRAM");
return retval;
}if (retval != ERROR_OK) { ... }
retval = nand_page_command(nand, page, NAND_CMD_SEQIN, !data);
if (retval != ERROR_OK) {
LOG_ERROR("NAND_CMD_SEQIN failed");
return retval;
}if (retval != ERROR_OK) { ... }
/* ... */
retval = target_write_u32(target, 0x20020014, 0x3c);
if (retval != ERROR_OK) {
LOG_ERROR("Could not set SLC_CFG");
return retval;
}if (retval != ERROR_OK) { ... }
if (data) {
static uint8_t fdata[2048];
memset(fdata, 0xFF, nand->page_size);
memcpy(fdata, data, data_size);
retval = target_write_memory(target,
target_mem_base + DATA_OFFS,
4, nand->page_size/4, fdata);
if (retval != ERROR_OK) {
LOG_ERROR("Could not write data to IRAM");
return retval;
}if (retval != ERROR_OK) { ... }
retval = target_write_memory(target, 0x31000100, 4,
sizeof(struct dmac_ll) / 4,
(uint8_t *)dmalist);
if (retval != ERROR_OK) {
LOG_ERROR("Could not write DMA descriptor to DMAC");
return retval;
}if (retval != ERROR_OK) { ... }
int tot_size = nand->page_size;
tot_size += tot_size == 2048 ? 64 : 16;
retval = lpc32xx_start_slc_dma(nand, tot_size, 0);
if (retval != ERROR_OK) {
LOG_ERROR("DMA failed");
return retval;
}if (retval != ERROR_OK) { ... }
if (!lpc32xx_dma_ready(nand, 100)) {
LOG_ERROR("Data DMA failed during write");
return ERROR_FLASH_OPERATION_FAILED;
}if (!lpc32xx_dma_ready(nand, 100)) { ... }
}if (data) { ... }
static uint8_t foob[64];
int foob_size = nand->page_size == 2048 ? 64 : 16;
memset(foob, 0xFF, foob_size);
if (oob)
memcpy(foob, oob, oob_size);
else {
int ecc_count = nand->page_size == 2048 ? 8 : 2;
static uint32_t hw_ecc[8];
retval = target_read_memory(target, target_mem_base + ECC_OFFS,
4, ecc_count, (uint8_t *)hw_ecc);
if (retval != ERROR_OK) {
LOG_ERROR("Reading hw generated ECC from IRAM failed");
return retval;
}if (retval != ERROR_OK) { ... }
static uint8_t ecc[24];
slc_ecc_copy_to_buffer(ecc, hw_ecc, ecc_count);
const int *layout = nand->page_size == 2048 ? lp_ooblayout : sp_ooblayout;
int i;
for (i = 0; i < ecc_count * 3; i++)
foob[layout[i]] = ecc[i];
lpc32xx_dump_oob(foob, foob_size);
}else { ... }
retval = target_write_memory(target, target_mem_base + SPARE_OFFS, 4,
foob_size / 4, foob);
if (retval != ERROR_OK) {
LOG_ERROR("Writing OOB to IRAM failed");
return retval;
}if (retval != ERROR_OK) { ... }
retval = target_write_memory(target, 0x31000100, 4,
sizeof(struct dmac_ll) / 4,
(uint8_t *)(&dmalist[nll-1]));
if (retval != ERROR_OK) {
LOG_ERROR("Could not write OOB DMA descriptor to DMAC");
return retval;
}if (retval != ERROR_OK) { ... }
if (data) {
/* ... */
retval = target_write_u32(target, 0x31000008, 1);
if (retval != ERROR_OK) {
LOG_ERROR("Could not set DMACIntTCClear");
return retval;
}if (retval != ERROR_OK) { ... }
/* ... */
retval = target_write_u32(target, 0x31000110,
1 | 1<<1 | 1<<6 | 2<<11 | 0<<14
| 0<<15 | 0<<16 | 0<<18);
if (retval != ERROR_OK) {
LOG_ERROR("Could not set DMACC0Config");
return retval;
}if (retval != ERROR_OK) { ... }
if (!lpc32xx_tc_ready(nand, 100)) {
LOG_ERROR("timeout while waiting for "
"completion of DMA");
return ERROR_NAND_OPERATION_FAILED;
}if (!lpc32xx_tc_ready(nand, 100)) { ... }
}if (data) { ... } else {
retval = lpc32xx_start_slc_dma(nand, foob_size, 1);
if (retval != ERROR_OK) {
LOG_ERROR("DMA OOB failed");
return retval;
}if (retval != ERROR_OK) { ... }
}else { ... }
retval = nand_write_finish(nand);
if (retval != ERROR_OK) {
LOG_ERROR("nand_write_finish failed");
return retval;
}if (retval != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
static int lpc32xx_write_page(struct nand_device *nand, uint32_t page,
uint8_t *data, uint32_t data_size,
uint8_t *oob, uint32_t oob_size)
{
struct lpc32xx_nand_controller *lpc32xx_info = nand->controller_priv;
struct target *target = nand->target;
int retval = ERROR_OK;
if (target->state != TARGET_HALTED) {
LOG_ERROR("target must be halted to use LPC32xx "
"NAND flash controller");
return ERROR_NAND_OPERATION_FAILED;
}if (target->state != TARGET_HALTED) { ... }
if (lpc32xx_info->selected_controller == LPC32XX_NO_CONTROLLER) {
LOG_ERROR("BUG: no LPC32xx NAND flash controller selected");
return ERROR_NAND_OPERATION_FAILED;
}if (lpc32xx_info->selected_controller == LPC32XX_NO_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) {
if (!data && oob) {
LOG_ERROR("LPC32xx MLC controller can't write "
"OOB data only");
return ERROR_NAND_OPERATION_NOT_SUPPORTED;
}if (!data && oob) { ... }
if (oob && (oob_size > 24)) {
LOG_ERROR("LPC32xx MLC controller can't write more "
"than 6 bytes for each quarter's OOB data");
return ERROR_NAND_OPERATION_NOT_SUPPORTED;
}if (oob && (oob_size > 24)) { ... }
if (data_size > (uint32_t)nand->page_size) {
LOG_ERROR("data size exceeds page size");
return ERROR_NAND_OPERATION_NOT_SUPPORTED;
}if (data_size > (uint32_t)nand->page_size) { ... }
retval = lpc32xx_write_page_mlc(nand, page, data, data_size,
oob, oob_size);
}else if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) {
struct working_area *pworking_area;
if (!data && oob) {
/* ... */
return nand_write_page_raw(nand, page, data,
data_size, oob, oob_size);
}if (!data && oob) { ... }
retval = target_alloc_working_area(target,
nand->page_size + DATA_OFFS,
&pworking_area);
if (retval != ERROR_OK) {
LOG_ERROR("Can't allocate working area in "
"LPC internal RAM");
return ERROR_FLASH_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = lpc32xx_write_page_slc(nand, pworking_area, page,
data, data_size, oob, oob_size);
target_free_working_area(target, pworking_area);
}else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) { ... }
return retval;
}{ ... }
static int lpc32xx_read_page_mlc(struct nand_device *nand, uint32_t page,
uint8_t *data, uint32_t data_size,
uint8_t *oob, uint32_t oob_size)
{
struct target *target = nand->target;
static uint8_t page_buffer[2048];
static uint8_t oob_buffer[64];
uint32_t page_bytes_done = 0;
uint32_t oob_bytes_done = 0;
uint32_t mlc_isr;
int retval;
if (!data && oob) {
/* ... */
retval = target_write_u32(target, 0x200b8000, NAND_CMD_READOOB);
}if (!data && oob) { ... } else {
retval = target_write_u32(target, 0x200b8000, NAND_CMD_READ0);
}else { ... }
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_CMD");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (nand->page_size == 512) {
/* ... */
retval = target_write_u32(target, 0x200b8004, 0x0);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x200b8004, page & 0xff);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x200b8004,
(page >> 8) & 0xff);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (nand->address_cycles == 4) {
retval = target_write_u32(target, 0x200b8004,
(page >> 16) & 0xff);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
}if (nand->address_cycles == 4) { ... }
}if (nand->page_size == 512) { ... } else {
/* ... */
retval = target_write_u32(target, 0x200b8004, 0x0);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x200b8004, 0x0);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x200b8004, page & 0xff);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x200b8004,
(page >> 8) & 0xff);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ADDR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = target_write_u32(target, 0x200b8000,
NAND_CMD_READSTART);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_CMD");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
}else { ... }
while (page_bytes_done < (uint32_t)nand->page_size) {
retval = target_write_u32(target, 0x200b8014, 0xaa55aa55);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_ECC_AUTO_DEC_REG");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (!lpc32xx_controller_ready(nand, 1000)) {
LOG_ERROR("timeout while waiting for "
"completion of auto decode cycle");
return ERROR_NAND_OPERATION_FAILED;
}if (!lpc32xx_controller_ready(nand, 1000)) { ... }
retval = target_read_u32(target, 0x200b8048, &mlc_isr);
if (retval != ERROR_OK) {
LOG_ERROR("could not read MLC_ISR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (mlc_isr & 0x8) {
if (mlc_isr & 0x40) {
LOG_ERROR("uncorrectable error detected: "
"0x%2.2x", (unsigned)mlc_isr);
return ERROR_NAND_OPERATION_FAILED;
}if (mlc_isr & 0x40) { ... }
LOG_WARNING("%i symbol error detected and corrected",
((int)(((mlc_isr & 0x30) >> 4) + 1)));
}if (mlc_isr & 0x8) { ... }
if (data) {
retval = target_read_memory(target, 0x200a8000, 4, 128,
page_buffer + page_bytes_done);
if (retval != ERROR_OK) {
LOG_ERROR("could not read MLC_BUF (data)");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
}if (data) { ... }
if (oob) {
retval = target_read_memory(target, 0x200a8000, 4, 4,
oob_buffer + oob_bytes_done);
if (retval != ERROR_OK) {
LOG_ERROR("could not read MLC_BUF (oob)");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
}if (oob) { ... }
page_bytes_done += 512;
oob_bytes_done += 16;
}while (page_bytes_done < (uint32_t)nand->page_size) { ... }
if (data)
memcpy(data, page_buffer, data_size);
if (oob)
memcpy(oob, oob_buffer, oob_size);
return ERROR_OK;
}{ ... }
static int lpc32xx_read_page_slc(struct nand_device *nand,
struct working_area *pworking_area,
uint32_t page, uint8_t *data,
uint32_t data_size, uint8_t *oob,
uint32_t oob_size)
{
struct target *target = nand->target;
int retval;
uint32_t target_mem_base;
LOG_DEBUG("SLC read page %" PRIx32 " data=%" PRIu32 ", oob=%" PRIu32,
page, data_size, oob_size);
target_mem_base = pworking_area->address;
int nll = lpc32xx_make_dma_list(target_mem_base, nand->page_size, 1);
/* ... */
retval = target_write_memory(target, target_mem_base, 4,
nll * sizeof(struct dmac_ll) / 4,
(uint8_t *)dmalist);
if (retval != ERROR_OK) {
LOG_ERROR("Could not write DMA descriptors to IRAM");
return retval;
}if (retval != ERROR_OK) { ... }
retval = nand_page_command(nand, page, NAND_CMD_READ0, 0);
if (retval != ERROR_OK) {
LOG_ERROR("lpc32xx_read_page_slc: NAND_CMD_READ0 failed");
return retval;
}if (retval != ERROR_OK) { ... }
/* ... */
retval = target_write_u32(target, 0x20020014, 0x3e);
if (retval != ERROR_OK) {
LOG_ERROR("lpc32xx_read_page_slc: Could not set SLC_CFG");
return retval;
}if (retval != ERROR_OK) { ... }
retval = target_write_memory(target, 0x31000100, 4,
sizeof(struct dmac_ll) / 4, (uint8_t *)dmalist);
if (retval != ERROR_OK) {
LOG_ERROR("Could not write DMA descriptor to DMAC");
return retval;
}if (retval != ERROR_OK) { ... }
int tot_size = nand->page_size;
tot_size += nand->page_size == 2048 ? 64 : 16;
retval = lpc32xx_start_slc_dma(nand, tot_size, 1);
if (retval != ERROR_OK) {
LOG_ERROR("lpc32xx_read_page_slc: DMA read failed");
return retval;
}if (retval != ERROR_OK) { ... }
if (data) {
retval = target_read_memory(target, target_mem_base + DATA_OFFS,
4, data_size/4, data);
if (retval != ERROR_OK) {
LOG_ERROR("Could not read data from IRAM");
return retval;
}if (retval != ERROR_OK) { ... }
}if (data) { ... }
if (oob) {
retval = target_read_memory(target,
target_mem_base + SPARE_OFFS, 4,
oob_size/4, oob);
if (retval != ERROR_OK) {
LOG_ERROR("Could not read OOB from IRAM");
return retval;
}if (retval != ERROR_OK) { ... }
return ERROR_OK;
}if (oob) { ... }
static uint8_t foob[64];
retval = target_read_memory(target, target_mem_base + SPARE_OFFS,
4, nand->page_size == 2048 ? 16 : 4, foob);
lpc32xx_dump_oob(foob, nand->page_size == 2048 ? 64 : 16);
if (retval != ERROR_OK) {
LOG_ERROR("Could not read OOB from IRAM");
return retval;
}if (retval != ERROR_OK) { ... }
int ecc_count = nand->page_size == 2048 ? 8 : 2;
static uint32_t hw_ecc[8];
retval = target_read_memory(target, target_mem_base + ECC_OFFS, 4,
ecc_count, (uint8_t *)hw_ecc);
if (retval != ERROR_OK) {
LOG_ERROR("Could not read hw generated ECC from IRAM");
return retval;
}if (retval != ERROR_OK) { ... }
static uint8_t ecc[24];
slc_ecc_copy_to_buffer(ecc, hw_ecc, ecc_count);
static uint8_t fecc[24];
const int *layout = nand->page_size == 2048 ? lp_ooblayout : sp_ooblayout;
int i;
for (i = 0; i < ecc_count * 3; i++)
fecc[i] = foob[layout[i]];
for (i = 0; i < ecc_count; i++) {
retval = nand_correct_data(nand, data + 256*i, &fecc[i * 3],
&ecc[i * 3]);
if (retval > 0)
LOG_WARNING("error detected and corrected: %" PRIu32 "/%d",
page, i);
if (retval < 0)
break;
}for (i = 0; i < ecc_count; i++) { ... }
if (i == ecc_count)
retval = ERROR_OK;
else {
LOG_ERROR("uncorrectable error detected: %" PRIu32 "/%d", page, i);
retval = ERROR_NAND_OPERATION_FAILED;
}else { ... }
return retval;
}{ ... }
static int lpc32xx_read_page(struct nand_device *nand, uint32_t page,
uint8_t *data, uint32_t data_size,
uint8_t *oob, uint32_t oob_size)
{
struct lpc32xx_nand_controller *lpc32xx_info = nand->controller_priv;
struct target *target = nand->target;
int retval = ERROR_OK;
if (target->state != TARGET_HALTED) {
LOG_ERROR("target must be halted to use LPC32xx "
"NAND flash controller");
return ERROR_NAND_OPERATION_FAILED;
}if (target->state != TARGET_HALTED) { ... }
if (lpc32xx_info->selected_controller == LPC32XX_NO_CONTROLLER) {
LOG_ERROR("BUG: no LPC32xx NAND flash controller selected");
return ERROR_NAND_OPERATION_FAILED;
}if (lpc32xx_info->selected_controller == LPC32XX_NO_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) {
if (data_size > (uint32_t)nand->page_size) {
LOG_ERROR("data size exceeds page size");
return ERROR_NAND_OPERATION_NOT_SUPPORTED;
}if (data_size > (uint32_t)nand->page_size) { ... }
retval = lpc32xx_read_page_mlc(nand, page, data, data_size,
oob, oob_size);
}else if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) {
struct working_area *pworking_area;
retval = target_alloc_working_area(target,
nand->page_size + 0x200,
&pworking_area);
if (retval != ERROR_OK) {
LOG_ERROR("Can't allocate working area in "
"LPC internal RAM");
return ERROR_FLASH_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
retval = lpc32xx_read_page_slc(nand, pworking_area, page,
data, data_size, oob, oob_size);
target_free_working_area(target, pworking_area);
}else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) { ... }
return retval;
}{ ... }
static int lpc32xx_controller_ready(struct nand_device *nand, int timeout)
{
struct lpc32xx_nand_controller *lpc32xx_info = nand->controller_priv;
struct target *target = nand->target;
int retval;
if (target->state != TARGET_HALTED) {
LOG_ERROR("target must be halted to use LPC32xx "
"NAND flash controller");
return ERROR_NAND_OPERATION_FAILED;
}if (target->state != TARGET_HALTED) { ... }
LOG_DEBUG("lpc32xx_controller_ready count start=%d", timeout);
do {
if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) {
uint8_t status;
retval = target_read_u8(target, 0x200b8048, &status);
if (retval != ERROR_OK) {
LOG_ERROR("could not set MLC_STAT");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (status & 2) {
LOG_DEBUG("lpc32xx_controller_ready count=%d",
timeout);
return 1;
}if (status & 2) { ... }
}if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) {
uint32_t status;
retval = target_read_u32(target, 0x20020018, &status);
if (retval != ERROR_OK) {
LOG_ERROR("could not set SLC_STAT");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (status & 1) {
LOG_DEBUG("lpc32xx_controller_ready count=%d",
timeout);
return 1;
}if (status & 1) { ... }
}else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) { ... }
alive_sleep(1);
...} while (timeout-- > 0);
return 0;
}{ ... }
static int lpc32xx_nand_ready(struct nand_device *nand, int timeout)
{
struct lpc32xx_nand_controller *lpc32xx_info = nand->controller_priv;
struct target *target = nand->target;
int retval;
if (target->state != TARGET_HALTED) {
LOG_ERROR("target must be halted to use LPC32xx "
"NAND flash controller");
return ERROR_NAND_OPERATION_FAILED;
}if (target->state != TARGET_HALTED) { ... }
LOG_DEBUG("lpc32xx_nand_ready count start=%d", timeout);
do {
if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) {
uint8_t status = 0x0;
/* ... */
retval = target_read_u8(target, 0x200b8048, &status);
if (retval != ERROR_OK) {
LOG_ERROR("could not read MLC_ISR");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (status & 1) {
LOG_DEBUG("lpc32xx_nand_ready count end=%d",
timeout);
return 1;
}if (status & 1) { ... }
}if (lpc32xx_info->selected_controller == LPC32XX_MLC_CONTROLLER) { ... } else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) {
uint32_t status = 0x0;
retval = target_read_u32(target, 0x20020018, &status);
if (retval != ERROR_OK) {
LOG_ERROR("could not read SLC_STAT");
return ERROR_NAND_OPERATION_FAILED;
}if (retval != ERROR_OK) { ... }
if (status & 1) {
LOG_DEBUG("lpc32xx_nand_ready count end=%d",
timeout);
return 1;
}if (status & 1) { ... }
}else if (lpc32xx_info->selected_controller == LPC32XX_SLC_CONTROLLER) { ... }
alive_sleep(1);
...} while (timeout-- > 0);
return 0;
}{ ... }
static int lpc32xx_tc_ready(struct nand_device *nand, int timeout)
{
struct target *target = nand->target;
LOG_DEBUG("lpc32xx_tc_ready count start=%d", timeout);
do {
uint32_t status = 0x0;
int retval;
retval = target_read_u32(target, 0x2002001c, &status);
if (retval != ERROR_OK) {
LOG_ERROR("Could not read SLC_INT_STAT");
return 0;
}if (retval != ERROR_OK) { ... }
if (status & 2) {
LOG_DEBUG("lpc32xx_tc_ready count=%d", timeout);
return 1;
}if (status & 2) { ... }
alive_sleep(1);
...} while (timeout-- > 0);
return 0;
}{ ... }
COMMAND_HANDLER(handle_lpc32xx_select_command)
{
struct lpc32xx_nand_controller *lpc32xx_info = NULL;
char *selected[] = {
"no", "mlc", "slc"
...};
if ((CMD_ARGC < 1) || (CMD_ARGC > 3))
return ERROR_COMMAND_SYNTAX_ERROR;
unsigned num;
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
struct nand_device *nand = get_nand_device_by_num(num);
if (!nand) {
command_print(CMD, "nand device '#%s' is out of bounds",
CMD_ARGV[0]);
return ERROR_OK;
}if (!nand) { ... }
lpc32xx_info = nand->controller_priv;
if (CMD_ARGC >= 2) {
if (strcmp(CMD_ARGV[1], "mlc") == 0) {
lpc32xx_info->selected_controller =
LPC32XX_MLC_CONTROLLER;
}if (strcmp(CMD_ARGV[1], "mlc") == 0) { ... } else if (strcmp(CMD_ARGV[1], "slc") == 0) {
lpc32xx_info->selected_controller =
LPC32XX_SLC_CONTROLLER;
}else if (strcmp(CMD_ARGV[1], "slc") == 0) { ... } else
return ERROR_COMMAND_SYNTAX_ERROR;
}if (CMD_ARGC >= 2) { ... }
command_print(CMD, "%s controller selected",
selected[lpc32xx_info->selected_controller]);
return ERROR_OK;
}{ ... }
static const struct command_registration lpc32xx_exec_command_handlers[] = {
{
.name = "select",
.handler = handle_lpc32xx_select_command,
.mode = COMMAND_EXEC,
.help = "select MLC or SLC controller (default is MLC)",
.usage = "bank_id ['mlc'|'slc' ]",
...},
COMMAND_REGISTRATION_DONE
...};
static const struct command_registration lpc32xx_command_handler[] = {
{
.name = "lpc32xx",
.mode = COMMAND_ANY,
.help = "LPC32xx NAND flash controller commands",
.usage = "",
.chain = lpc32xx_exec_command_handlers,
...},
COMMAND_REGISTRATION_DONE
...};
struct nand_flash_controller lpc32xx_nand_controller = {
.name = "lpc32xx",
.commands = lpc32xx_command_handler,
.nand_device_command = lpc32xx_nand_device_command,
.init = lpc32xx_init,
.reset = lpc32xx_reset,
.command = lpc32xx_command,
.address = lpc32xx_address,
.write_data = lpc32xx_write_data,
.read_data = lpc32xx_read_data,
.write_page = lpc32xx_write_page,
.read_page = lpc32xx_read_page,
.nand_ready = lpc32xx_nand_ready,
...};