summaryrefslogtreecommitdiffstats
path: root/uClinux-2.4.20-uc1/drivers/block/blkmem.c
blob: 27c506f9e6d7a510acc34dd85b22604eb0817dd8 (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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
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
815
816
817
818
819
820
821
822
823
824
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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
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
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
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
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
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
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
/* blkmem.c: Block access to memory spaces
 *
 * Copyright (C) 2001  SnapGear Inc. <davidm@snapgear.com>
 * Copyright (C) 2000  Lineo, Inc.  (www.lineo.com)   
 * Copyright (C) 1997, 1998  D. Jeff Dionne <jeff@lineo.ca>,
 *                           Kenneth Albanowski <kjahds@kjahds.com>,
 * Copyright (C) 1999-2003   Greg Ungerer <gerg@snapgear.com>
 *
 * Based z2ram - Amiga pseudo-driver to access 16bit-RAM in ZorroII space
 * Copyright (C) 1994 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  This software is provided "as is" without express or
 * implied warranty.
 *
 * NOV/2000 -- hacked for Linux kernel 2.2 and NETtel/x86 (gerg@snapgear.com)
 * VZ Support/Fixes             Evan Stawnyczy <e@lineo.ca>
 */

#include <linux/module.h>
#include <linux/config.h>
#include <linux/major.h>
#include <linux/slab.h>
#include <linux/reboot.h>
#include <linux/ledman.h>
#include <linux/init.h>
#include <linux/devfs_fs_kernel.h>
#include <linux/tqueue.h>
#include <asm/semaphore.h>
#include <asm/uaccess.h>
#include <asm/io.h>

#undef VERBOSE
#undef DEBUG

#define TRUE                  (1)
#define FALSE                 (0)

#define	BLKMEM_MAJOR	31

#define MAJOR_NR BLKMEM_MAJOR

#ifdef CONFIG_ARCH_CNXT
#define	CONFIG_FLASH_SNAPGEAR	1
#define DEVEL  1 /* everything in SDRAM for now */
#endif

// #define DEVICE_NAME "blkmem"
#define DEVICE_REQUEST do_blkmem_request
#define DEVICE_NR(device) (MINOR(device))
#define DEVICE_ON(device)
#define DEVICE_OFF(device)
#define DEVICE_NO_RANDOM
#define TIMEOUT_VALUE (6 * HZ)

#include <linux/blkmem.h>
#include <linux/blk.h>

#ifdef CONFIG_LEDMAN
#include <linux/ledman.h>
#endif

#include <asm/bitops.h>
#include <asm/delay.h>
#include <asm/semaphore.h>

/*
 *	Timeout loop counter for FLASH operations.
 *
 * On the flash part on the M5272C3, it can take up to 60 seconds for a write
 * in a non-ideal environment.  The counter above wasn't enough even in an
 * ideal one.
 */
#define	FTIMEOUT	990000000


/*
 * Please, configure the ROMFS for your system here
 */


/* Samsung S3C4510-SNDS100 arch */
#ifdef CONFIG_BOARD_SNDS100
extern char romfs_data[];
extern char romfs_data_end[];
#endif

/* v850e; this config stuff is ugly, ugly, ugly! */
#ifdef CONFIG_V850E
#include <asm/blkmem.h>
#endif

#ifdef CONFIG_MICROBLAZE
#include <asm/blkmem.h>
#endif

#if defined(CONFIG_ARCH_TA7S)
#include <asm/arch/blkmem.h>
#endif

/* (es) */
/* note: this is configured somewhere in arch/m68knommu/kernel/setup.c */
/* does it need to be here? */
#if defined( CONFIG_M68328 ) || defined ( CONFIG_M68EZ328 ) || defined( CONFIG_M68VZ328)
#include <asm/shglports.h>
#define CAT_ROMARRAY
#endif
/* (/es) */

#if defined( CONFIG_PILOT ) && defined( CONFIG_M68EZ328 )
extern char _flashstart[];
#define FIXED_ROMARRAY _flashstart
#endif

/* (es) */
#if defined(CONFIG_UCSIMM) || defined(CONFIG_UCDIMM) || defined (CONFIG_DRAGEN2)
#define CAT_ROMARRAY
#endif
/* (/es) */

#ifdef CONFIG_M68EZ328ADS
#ifdef CONFIG_M68EZ328ADS_RAM
extern char _flashstart[];
#define FIXED_ROMARRAY _flashstart
#else
#define CAT_ROMARRAY
#endif 
#endif 

#ifdef CONFIG_ARCH_TRIO
#define FIXED_ROMARRAY (char*)(3512*1024)
#endif

#ifdef CONFIG_EXCALIBUR
extern char _romfs_start[];
#define FIXED_ROMARRAY _romfs_start
#endif

#ifdef CONFIG_ALMA_ANS
#ifdef CONFIG_ALMA_ANS_RAM
extern char _flashstart[];
#define FIXED_ROMARRAY _flashstart
#else
#define CAT_ROMARRAY
#endif 
#endif 

#ifdef CONFIG_COLDFIRE
#ifdef CONFIG_TELOS
#define CAT_ROMARRAY
#else
unsigned char *romarray;
extern char _ebss;

#ifndef CONFIG_ROMFS_FROM_ROM
  #define FIXUP_ARENAS 	arena[0].address = (unsigned long) &_ebss;
#else
  #define FIXUP_ARENAS	{ \
		register char *sp = (char *) arena[4].address; \
		register char *ep = sp + arena[4].length; \
		if (strncmp((char *) &_ebss, "-rom1fs-", 8) == 0) { \
			sp = (char *) &_ebss; \
		} else { \
			while (sp < ep && strncmp(sp, "-rom1fs-", 8)) \
				sp++; \
			if (sp >= ep) \
				sp = &_ebss; \
		} \
		arena[0].address = (unsigned long) sp; \
	}
#endif /* CONFIG_ROMFS_FROM_ROM */

/*
 *  Stub out the LED functions for now.
 */
#define SET_ALARM_LED(x)
#define GET_COMM_STATUS_LED(x)
#define SET_COMM_STATUS_LED(x)
#define SET_COMM_ERROR_LED(x)
#endif /* CONFIG_TELOS */
#endif /* CONFIG_COLDFIRE */


#if defined(CONFIG_ARCH_DSC21) || defined(CONFIG_ARCH_ATMEL)
#define FIXED_ROMARRAY (char *)(FLASH_MEM_BASE)
#endif

#if defined( CONFIG_M68360 )
#define CAT_ROMARRAY
#endif

/*
 *	Lineo hardware has similar FLASH layouts on all devices.
 */
#if defined(CONFIG_NETtel) ||	\
    defined(CONFIG_eLIA) ||	\
    defined(CONFIG_DISKtel) ||	\
    defined(CONFIG_SECUREEDGEMP3) || \
    defined(CONFIG_SE1100) || \
    defined(CONFIG_GILBARCONAP)
#define	CONFIG_FLASH_SNAPGEAR	1
#endif

#if defined(CONFIG_CPU_H8300H) || defined(CONFIG_CPU_H8S)
#if defined(CONFIG_INTELFLASH)
  #define CONFIG_FLASH_SNAPGEAR	1
  extern char _ebss;
  #define FIXUP_ARENAS 	arena[0].address = ((unsigned long) &_ebss) + 512;
#else
extern char _blkimg[];
#define FIXED_ROMARRAY _blkimg
#endif
#endif

#if defined(CONFIG_BOARD_SMDK40100)
extern char __romfs_start[];
#define FIXED_ROMARRAY __romfs_start
#endif

/******* END OF BOARD-SPECIFIC CONFIGURATION ************/

/* Simple romfs, at internal, cat on the end of kernel, or seperate fixed adderess romfs. */

#ifdef INTERNAL_ROMARRAY
#include "romdisk.c"
#endif

#ifdef CAT_ROMARRAY
unsigned char *romarray;
extern char __data_rom_start[];
extern char _edata[];
extern char __data_start[];
#ifndef FIXUP_ARENAS
#define FIXUP_ARENAS \
	arena[0].address = (unsigned long)__data_rom_start + (unsigned long)_edata - (unsigned long)__data_start;
#endif
#endif

#if defined(CONFIG_WATCHDOG)
extern void watchdog_disable(void);
extern void watchdog_enable(void);
#endif

#ifdef FIXED_ROMARRAY
unsigned char *romarray = (char *)(FIXED_ROMARRAY);
#endif

/* If defined, ROOT_ARENA causes the root device to be the specified arena, useful with romfs */

/* Now defining ROOT_DEV in arch/setup.c */
/*#define ROOT_ARENA 0*/

struct arena_t;

typedef void (*xfer_func_t)(struct arena_t *, unsigned long address, unsigned long length, char * buffer);
typedef void (*erase_func_t)(struct arena_t *, unsigned long address);
typedef void (*program_func_t)(struct arena_t *, struct blkmem_program_t * prog);

#ifndef CONFIG_COLDFIRE
void program_main(struct arena_t *, struct blkmem_program_t *);
void read_spare(struct arena_t *, unsigned long, unsigned long, char *);
void write_spare(struct arena_t *, unsigned long, unsigned long, char *);
void erase_spare(struct arena_t *, unsigned long);
#endif

#if defined(CONFIG_ARNEWSH) || defined(CONFIG_M5206eC3)
void flash_amd8_pair_write(struct arena_t *, unsigned long, unsigned long, char *);
void flash_amd8_pair_erase(struct arena_t *, unsigned long);
#endif

#if defined(CONFIG_M5272C3) || defined(CONFIG_COBRA5272) 
/*
 *	M5272C3 evaluation board with 16bit AMD FLASH.
 *	(The COBRA5272 has the same flash)
 */
static void flash_amd16_writeall(struct arena_t *, struct blkmem_program_t *);
static void flash_amd16_write(struct arena_t *, unsigned long, unsigned long, char *);
static void flash_amd16_erase(struct arena_t *, unsigned long);
#define	flash_erase	flash_amd16_erase
#define	flash_write	flash_amd16_write
#define	flash_writeall	flash_amd16_writeall
#endif


#if defined(CONFIG_FLASH_SNAPGEAR)
#if defined(CONFIG_INTELFLASH)
/*
 *	Lineo hardware with INTEL FLASH.
 */
static void flash_intel_writeall(struct arena_t *, struct blkmem_program_t *);
static void flash_intel_write(struct arena_t *, unsigned long, unsigned long, char *);
static void flash_intel_erase(struct arena_t *, unsigned long);
#define	flash_erase	flash_intel_erase
#define	flash_write	flash_intel_write
#define	flash_writeall	flash_intel_writeall
#else
#ifdef CONFIG_FLASH8BIT
/*
 *	Lineo hardware with 8bit AMD FLASH.
 */
static void flash_amd8_writeall(struct arena_t *, struct blkmem_program_t *);
static void flash_amd8_write(struct arena_t *, unsigned long, unsigned long, char *);
static void flash_amd8_erase(struct arena_t *, unsigned long);
#define	flash_erase	flash_amd8_erase
#define	flash_write	flash_amd8_write
#define	flash_writeall	flash_amd8_writeall
#else
/*
 *	Lineo hardware with 16bit AMD FLASH (this is the default).
 */
static void flash_amd16_writeall(struct arena_t *, struct blkmem_program_t *);
static void flash_amd16_write(struct arena_t *, unsigned long, unsigned long, char *);
static void flash_amd16_erase(struct arena_t *, unsigned long);
#define	flash_erase	flash_amd16_erase
#define	flash_write	flash_amd16_write
#define	flash_writeall	flash_amd16_writeall
#endif /* !COFNIG_FLASH8BIT */
#endif /* !CONFIG_INTELFLASH */
#endif /* CONFIG_FLASH_SNAPGEAR */

#if defined(CONFIG_CLEOPATRA)
/*
 *	Feith hardware with 16bit AMD FLASH (this is the default).
 */
static void flash_amd16_writeall(struct arena_t *, struct blkmem_program_t *);
static void flash_amd16_write(struct arena_t *, unsigned long, unsigned long, char *);
static void flash_amd16_erase(struct arena_t *, unsigned long);
#define	flash_erase	flash_amd16_erase
#define	flash_write	flash_amd16_write
#define	flash_writeall	flash_amd16_writeall
#endif


/* This array of structures defines the actual set of memory arenas, including
   access functions (if the memory isn't part of the main address space) */

struct arena_t {
	int rw;

	unsigned long address; /* Address of memory arena */
	unsigned long length;  /* Length of memory arena. If -1, try to get size from romfs header */

	program_func_t program_func; /* Function to program in one go */

	xfer_func_t read_func; /* Function to transfer data to main memory, or zero if none needed */
	xfer_func_t write_func; /* Function to transfer data from main memory, zero if none needed */

	erase_func_t erase_func; /* Function to erase a block of memory to zeros, or 0 if N/A */
	unsigned long blksize; /* Size of block that can be erased at one time, or 0 if N/A */
	unsigned long unitsize;
	unsigned char erasevalue; /* Contents of sectors when erased */
	
	/*unsigned int auto_erase_bits;
	unsigned int did_erase_bits;*/
	
} arena[] = {

#ifdef INTERNAL_ROMARRAY
	{0, (unsigned long)romarray, sizeof(romarray)},
#endif

#ifdef CAT_ROMARRAY
	{0, 0, -1},
#endif

#ifdef FIXED_ROMARRAY
	{0, (unsigned long) FIXED_ROMARRAY, -1},
#endif

#ifdef CONFIG_BOARD_SNDS100
	{0, romfs_data, -1},
#endif

#ifdef CONFIG_ARCH_CNXT
    /*  AM29LV004T flash
     *  rom0 -- root file-system 
     */
#ifdef DEVEL
	/*
	 * rom0 currently  in RAM
	 */
	{1, 0x800000, 0x100000,0,0, flash_write, flash_erase, 0x10000, 0x10000, 0xff},
#else	
	{1, 0x400000, 0x10000,0,0, flash_write, flash_erase, 0x10000, 0x10000, 0xff},
	{1, 0x410000, 0xf0000,0,0, flash_write, flash_erase, 0x10000, 0x10000, 0xff},
	{1, 0x500000,0x100000,0,0, flash_write, flash_erase, 0x10000,0x10000,0xff},
	{1, 0x600000,0x200000,0,0, flash_write, flash_erase,0x10000,0x10000,0xff},
	{1, 0x801000,0xff000,0,0, flash_write,flash_erase,0xff000,0xff000,0xff},
#endif

#endif /* CONFIG_ARCH_CNXT*/

#if (defined(CONFIG_CPU_H8300H) || defined(CONFIG_CPU_H8S)) && \
		defined(CONFIG_INTELFLASH)
    {0, 0, -1}, /* In RAM romfs */
    {1,0x00000000,0x020000,0,0,flash_write,flash_erase,0x20000,0x20000,0xff},
    {1,0x00020000,0x020000,0,0,flash_write,flash_erase,0x20000,0x20000,0xff},
    {1,0x00040000,0x3c0000,0,0,flash_write,flash_erase,0x20000,0x20000,0xff},
    {1,0x00000000,0x400000,0,0,flash_write,flash_erase,0x20000,0x20000,0xff},
#endif

#ifdef CONFIG_COLDFIRE
    /*
     *	The ROM file-system is RAM resident on the ColdFire eval boards.
     *	This arena is defined for access to it.
     */
    {0, 0, -1},

#ifdef CONFIG_ARN5206
    /*
     *	The spare FLASH segment on the Arnewsh 5206 board.
     */
    {1,0xffe20000,0x20000,0,0,flash_amd8_pair_write,flash_amd8_pair_erase,0x8000,0x20000,0xff},
#endif

#if defined(CONFIG_ARN5307) || defined(CONFIG_M5206eC3)
    /*  pair of AM29LV004T flash for 1Mbyte total
     *  rom0 -- root file-system (actually in RAM)
     *  rom1 -- FLASH SA0   128K boot
     *  rom2 -- FLASH SA1-6 768k kernel & romfs
     *  rom3 -- FLASH SA7   64k spare
     *  rom4 -- FLASH SA8   16k spare
     *  rom5 -- FLASH SA9   16k spare
     *  rom6 -- FLASH SA10  32k spare
     */
    {1,0xffe00000,0x20000,0,0,flash_amd8_pair_write,flash_amd8_pair_erase,0x20000,0x20000,0xff},
    {1,0xffe20000,0xc0000,0,0,flash_amd8_pair_write,flash_amd8_pair_erase,0x20000,0xc0000,0xff},
    {1,0xffee0000,0x10000,0,0,flash_amd8_pair_write,flash_amd8_pair_erase,0x10000,0x10000,0xff},
    {1,0xffef0000,0x4000,0,0,flash_amd8_pair_write,flash_amd8_pair_erase,0x4000,0x4000,0xff},
    {1,0xffef4000,0x4000,0,0,flash_amd8_pair_write,flash_amd8_pair_erase,0x4000,0x4000,0xff},
    {1,0xffef8000,0x8000,0,0,flash_amd8_pair_write,flash_amd8_pair_erase,0x8000,0x8000,0xff},
#endif /* CONFIG_ARNEWSH || CONFING_M5206eC3 */

#if defined(CONFIG_M5272C3) || defined(CONFIG_COBRA5272)
/*
 *	Motorola M5272C3 evaluation board with 2MB FLASH.
 *	The following devices are supported:
 *		rom0 -- root file-system (actually in RAM)
 *		rom1 -- FLASH boot block (256k)
 *		rom2 -- FLASH low boot chunks (32k total)
 *		rom3 -- FLASH high large boot area hunk (224k)
 *		rom4 -- FLASH kernel+file-system binary (1792k)
 *		rom5 -- FLASH config file-system (top 1024k)
 *		rom6 -- FLASH the whole thing (2MB)!
 *
 * (The COBRA5272 board has the same flash layout)
 */
    {1,0xffe00000,0x040000,flash_writeall, 0, 0, 0,    0x40000,0x040000,0xff},
    {1,0xffe00000,0x008000,flash_writeall, 0, 0, 0,    0x08000,0x008000,0xff},
    {1,0xffe08000,0x038000,0,0,flash_write,flash_erase,0x38000,0x038000,0xff},
    {1,0xffe40000,0x1c0000,0,0,flash_write,flash_erase,0x40000,0x1c0000,0xff},
    {1,0xfff00000,0x100000,0,0,flash_write,flash_erase,0x40000,0x100000,0xff},
    {1,0xffe00000,0x200000,flash_writeall, 0, 0, 0,    0x40000,0x200000,0xff},
#endif
#if defined(CONFIG_SE1100)
/* SE1100 hardware has a slightly different layout to the standard
 * snapgear image.  We've got a disproportionately large config area which
 * we've splatted to the end of the flash, rather than the start.  This lets
 * us adjust sizes more easily later but hurts if we change flash size.
 *	The following devices are supported:
 *		rom0 -- root file-system (actually in RAM)
 *		rom1 -- FLASH boot block (16k)
 *		rom2 -- FLASH boot arguments (8k)
 *		rom3 -- FLASH MAC addresses (8k)
 *		rom4 -- FLASH kernel+file-system binary (1792k)
 *		rom5 -- FLASH config file-system (192k)
 *		rom6 -- FLASH the whole damn thing (2Mb)!
 *		rom7 -- FLASH spare block (32k)
 */
    {1,0xf0000000,0x004000,flash_writeall, 0, 0, 0,    0x04000,0x004000,0xff},
    {1,0xf0004000,0x002000,0,0,flash_write,flash_erase,0x02000,0x002000,0xff},
    {1,0xf0006000,0x002000,0,0,flash_write,flash_erase,0x02000,0x002000,0xff},
    {1,0xf0010000,0x1c0000,flash_writeall, 0, 0, 0,    0x10000,0x1e0000,0xff},
    {1,0xf01d0000,0x030000,0,0,flash_write,flash_erase,0x10000,0x010000,0xff},
    {1,0xf0000000,0x200000,flash_writeall, 0, 0, 0,    0x10000,0x200000,0xff},
    {1,0xf0008000,0x08000,flash_writeall,0,flash_write,flash_erase,0x08000,0x08000,0xff},
#elif defined(CONFIG_FLASH_SNAPGEAR)
#if defined(CONFIG_FLASH8MB)
/*
 *	SnapGear hardware with 8MB FLASH.
 *	The following devices are supported:
 *		rom0 -- root file-system (actually in RAM)
 *		rom1 -- FLASH boot block (128k)
 *		rom2 -- FLASH boot arguments (128k)
 *		rom3 -- FLASH MAC addresses (128k)
 *		rom4 -- FLASH kernel+file-system binary (7mb)
 *		rom5 -- FLASH config file-system (512k)
 *		rom6 -- FLASH the whole damn thing (8Mb)!
 *		rom7 -- FLASH spare block (128k)
 */
    {1,0xf0000000,0x020000,flash_writeall, 0, 0, 0,    0x20000,0x020000,0xff},
    {1,0xf0020000,0x020000,0,0,flash_write,flash_erase,0x20000,0x020000,0xff},
    {1,0xf0040000,0x020000,0,0,flash_write,flash_erase,0x20000,0x020000,0xff},
    {1,0xf0100000,0x700000,flash_writeall, 0, 0, 0,    0x20000,0x700000,0xff},
    {1,0xf0080000,0x080000,0,0,flash_write,flash_erase,0x20000,0x080000,0xff},
    {1,0xf0000000,0x800000,flash_writeall, 0, 0, 0,    0x20000,0x800000,0xff},
    {1,0xf0060000,0x020000,flash_writeall,0,flash_write,flash_erase,0x20000,0x02000,0xff},
#if defined(CONFIG_EXTRA_FLASH)
/*
 *		rom8 -- FLASH extra.
 */
    {1,0xf0800000,0x800000,flash_writeall, 0, 0, 0,  0x10000,0x800000,0xff},
#endif /* CONFIG_EXTRA_FLASH */
#elif defined(CONFIG_FLASH4MB)
/*
 *	SnapGear hardware with 4MB FLASH.
 *	The following devices are supported:
 *		rom0 -- root file-system (actually in RAM)
 *		rom1 -- FLASH boot block (16k)
 *		rom2 -- FLASH boot arguments (8k)
 *		rom3 -- FLASH MAC addresses (8k)
 *		rom4 -- FLASH kernel+file-system binary (3920k)
 *		rom5 -- FLASH config file-system (64k)
 *		rom6 -- FLASH the whole damn thing (4Mb)!
 *		rom7 -- FLASH spare block (32k)
 */
    {1,0xf0000000,0x004000,flash_writeall, 0, 0, 0,    0x04000,0x004000,0xff},
    {1,0xf0004000,0x002000,0,0,flash_write,flash_erase,0x02000,0x002000,0xff},
    {1,0xf0006000,0x002000,0,0,flash_write,flash_erase,0x02000,0x002000,0xff},
    {1,0xf0020000,0x3e0000,flash_writeall, 0, 0, 0,    0x10000,0x3e0000,0xff},
    {1,0xf0010000,0x010000,0,0,flash_write,flash_erase,0x10000,0x010000,0xff},
    {1,0xf0000000,0x400000,flash_writeall, 0, 0, 0,    0x10000,0x400000,0xff},
    {1,0xf0008000,0x08000,flash_writeall,0,flash_write,flash_erase,0x08000,0x08000,0xff},
#if defined(CONFIG_EXTRA_FLASH)
/*
 *		rom8 -- FLASH extra.
 */
    {1,0xf0400000,0x400000,flash_writeall, 0, 0, 0,  0x10000,0x400000,0xff},
#endif /* CONFIG_EXTRA_FLASH */
#elif defined(CONFIG_FLASH2MB)
/*
 *	SnapGear hardware with primary 2MB FLASH.
 *	The following devices are supported:
 *		rom0 -- root file-system (actually in RAM)
 *		rom1 -- FLASH boot block (16k)
 *		rom2 -- FLASH boot arguments (8k)
 *		rom3 -- FLASH MAC addresses (8k)
 *		rom4 -- FLASH kernel+file-system binary (1920k)
 *		rom5 -- FLASH config file-system (64k)
 *		rom6 -- FLASH the whole damn thing (2Mb)!
 *		rom7 -- FLASH spare block (32k)
 */
    {1,0xf0000000,0x004000,flash_writeall, 0, 0, 0,    0x04000,0x004000,0xff},
    {1,0xf0004000,0x002000,0,0,flash_write,flash_erase,0x02000,0x002000,0xff},
    {1,0xf0006000,0x002000,0,0,flash_write,flash_erase,0x02000,0x002000,0xff},
    {1,0xf0020000,0x1e0000,flash_writeall, 0, 0, 0,    0x10000,0x1e0000,0xff},
    {1,0xf0010000,0x010000,0,0,flash_write,flash_erase,0x10000,0x010000,0xff},
    {1,0xf0000000,0x200000,flash_writeall, 0, 0, 0,    0x10000,0x200000,0xff},
    {1,0xf0008000,0x08000,flash_writeall,0,flash_write,flash_erase,0x08000,0x08000,0xff},
#if defined(CONFIG_EXTRA_FLASH)
/*
 *		rom8 -- FLASH extra.
 */
    {1,0xf0200000,0x200000,flash_writeall, 0, 0, 0,  0x10000,0x200000,0xff},
#endif /* CONFIG_EXTRA_FLASH */
#else
/*
 *	SnapGear hardware FLASH erase/program entry points (1MB FLASH).
 *	The following devices are supported:
 *		rom0 -- root file-system (actually in RAM)
 *		rom1 -- FLASH boot block (16k)
 *		rom2 -- FLASH boot arguments (8k)
 *		rom3 -- FLASH MAC addresses (8k)
 *		rom4 -- FLASH kernel+file-system binary (896k)
 *		rom5 -- FLASH config file-system (64k)
 *		rom6 -- FLASH the whole damn thing (1Mb)!
 *		rom7 -- FLASH spare block (32k)
 */
    {1,0xf0000000,0x04000,flash_writeall, 0, 0, 0,    0x04000,0x04000,0xff},
    {1,0xf0004000,0x02000,0,0,flash_write,flash_erase,0x02000,0x02000,0xff},
    {1,0xf0006000,0x02000,0,0,flash_write,flash_erase,0x02000,0x02000,0xff},
    {1,0xf0010000,0xe0000,flash_writeall, 0, 0, 0,    0x10000,0xe0000,0xff},
    {1,0xf00f0000,0x10000,0,0,flash_write,flash_erase,0x10000,0x10000,0xff},
    {1,0xf0000000,0x100000,flash_writeall, 0, 0, 0,  0x10000,0x100000,0xff},
    {1,0xf0008000,0x08000,flash_writeall,0,flash_write,flash_erase,0x08000,0x08000,0xff},
#if defined(CONFIG_EXTRA_FLASH)
/*
 *		rom8 -- FLASH extra. where the NETtel3540 stores the dsl image
 */
    {1,0xf0100000,0x100000,flash_writeall, 0, 0, 0,  0x10000,0x100000,0xff},
#endif /* CONFIG_EXTRA_FLASH */
#endif /* CONFIG_FLASH2MB */
#endif /* CONFIG_FLASH_SNAPGEAR */

#if defined(CONFIG_CLEOPATRA)
/*
 *	CLEOPATRA with 2MB/4MB FLASH erase/program entry points.
 *	The following devices are supported:
 *		rom0 -- root file-system (actually in RAM)
 *		rom1 -- FLASH boot block (16k)
 *		rom2 -- FLASH boot arguments (8k)
 *		rom3 -- FLASH MAC addresses (8k)
 *		rom4 -- FLASH kernel+file-system binary (1792k)
 *		rom5 -- FLASH config file-system (64k)
 *		rom6 -- FLASH the whole damn thing (2Mb)!
 *		rom7 -- FLASH spare block (32k)
 *		rom8 -- FLASH application config (128k)
 *		rom9 -- FLASH user block (1536k) (4MB only)
 *		rom10-- FLASH user block (512k) (4MB only)
 *		rom11-- FLASH user block (2048k) (6MB only)
 */
    {1,0xf0000000,0x004000,flash_writeall, 0, 0, 0,    0x04000,0x004000,0xff},
    {1,0xf0004000,0x002000,0,0,flash_write,flash_erase,0x02000,0x002000,0xff},
    {1,0xf0006000,0x002000,0,0,flash_write,flash_erase,0x02000,0x002000,0xff},
    {1,0xf0020000,0x1c0000,flash_writeall, 0, 0, 0,    0x10000,0x1c0000,0xff},
    {1,0xf0010000,0x010000,0,0,flash_write,flash_erase,0x10000,0x010000,0xff},
    {1,0xf0000000,0x200000,flash_writeall, 0, 0, 0,    0x10000,0x200000,0xff},
    {1,0xf0008000,0x08000,flash_writeall,0,flash_write,flash_erase,0x08000,0x08000,0xff},
    {1,0xf01e0000,0x020000,0,0,flash_write,flash_erase,0x10000,0x020000,0xff},
#if defined(CONFIG_FLASH4MB)
    {1,0xf0200000,0x180000,0,0,flash_write,flash_erase,0x10000,0x180000,0xff},
    {1,0xf0380000,0x080000,0,0,flash_write,flash_erase,0x10000,0x080000,0xff},
#endif

#if defined(CONFIG_FLASH6MB)
    {1,0xf0200000,0x180000,0,0,flash_write,flash_erase,0x10000,0x180000,0xff},
    {1,0xf0380000,0x080000,0,0,flash_write,flash_erase,0x10000,0x080000,0xff},
    {1,0xf0400000,0x200000,0,0,flash_write,flash_erase,0x10000,0x200000,0xff},
#endif
#endif

#endif /* CONFIG_COLDFIRE */


#ifdef CONFIG_SHGLCORE

#ifdef CONFIG_SHGLCORE_2MEG
	{0, 0x0A0000, 0x200000-0x0A0000},	/* ROM FS */
	{1, SHGLCORE_FLASH_BANK_0_ADDR, 0x80000, 0, 0, write_spare, erase_spare, 0x10000, 0x80000, 0xff},
	{1, 0x000000, 0x200000, program_main, 0,0,0, 0x20000, 0x100000}, /* All main FLASH */
#else
	{0, 0x0A0000, 0x100000-0x0A0000},	/* ROM FS */
	{1, SHGLCORE_FLASH_BANK_0_ADDR, 0x80000, 0, 0, write_spare, erase_spare, 0x10000, 0x80000, 0xff},
	{1, 0x000000, 0x100000, program_main, 0,0,0, 0x20000, 0x100000}, /* All main FLASH */
#endif

#define FIXUP_ARENAS \
	extern unsigned long rom_length; \
	arena[0].length = (unsigned long)rom_length - 0xA0000; \
	arena[2].length = (unsigned long)rom_length;

#endif
};

#define arenas (sizeof(arena) / sizeof(struct arena_t))


static int blkmem_blocksizes[arenas];
static int blkmem_sizes[arenas];
static devfs_handle_t devfs_handle;


#if defined(CONFIG_ARNEWSH) || defined(CONFIG_M5206eC3)

static DECLARE_MUTEX(spare_lock);

/*
 *	FLASH erase and programming routines for the odd/even FLASH
 *	pair on the ColdFire eval boards.
 */

void flash_amd8_pair_write(struct arena_t * a, unsigned long pos, unsigned long length, char * buffer)
{
  volatile unsigned short *address;
  unsigned short *wbuf, word;
  unsigned short result, prevresult;
  unsigned long flags, start;
  unsigned long fbase = a->address;
  int i;
  
#if 0
  printk("%s(%d): flash_amd8_pair_write(a=%x,pos=%x,length=%d,buf=%x)\n",
	__FILE__, __LINE__, (int) a, (int) pos, (int) length, (int) buffer);
#endif

  down(&spare_lock);

  start = jiffies;
  address = (unsigned volatile short *) (fbase + pos);
  wbuf = (unsigned short *) buffer;

  for (length >>= 1; (length > 0); length--, address++) {
  
    word = *wbuf++;

    if (*address != word) {
      save_flags(flags); cli();

      *((unsigned volatile short *) (fbase | (0x5555 << 1))) = 0xaaaa;
      *((unsigned volatile short *) (fbase | (0x2aaa << 1))) = 0x5555;
      *((unsigned volatile short *) (fbase | (0x5555 << 1))) = 0xa0a0;
      *address = word;
      udelay(1);

      /* Wait for write to complete, timeout after a few tries */
      for (prevresult = 0, i = 0; (i < 0x10000); i++) {
        result = *address;
        if ((result & 0x8080) == (word & 0x8080))
          break;
	if ((result & 0x4040) == (prevresult & 0x4040))
	  break;
	prevresult = result;
      }
 
      if (*address != word) {
          printk("%s(%d): FLASH write failed, address %p, write value %x"
		" (now %x, previous %x), count=%d\n", __FILE__, __LINE__,
		address, word, *address, prevresult, i);
	  printk("%s(%d): count=%d word=%x prevresult=%x result=%x\n",
                __FILE__, __LINE__, i, word, prevresult, result);
          *((unsigned volatile short *) fbase) = 0xf0f0; /* Reset */
      }
      restore_flags(flags);
    }
  }
  
  up(&spare_lock);
}

void flash_amd8_pair_erase(struct arena_t * a, unsigned long pos)
{
  unsigned volatile short *address;
  unsigned short result;
  unsigned long fbase = a->address;
  unsigned long flags;
  
#if 0
    printk("%s(%d): flash_amd8_pair_erase(): addr:%x, len:%x, pos:%x\n",
	__FILE__, __LINE__, a->address, a->length, pos);
#endif

  if (pos >= a->length)
    return;

  address = (unsigned volatile short *) (fbase + pos);

  /* Mutex all access to FLASH memory */
  down(&spare_lock);
  save_flags(flags); cli();

  /* Initiate erase of FLASH sector */
#ifdef CONFIG_ARN5206
  *((unsigned volatile short *) (fbase | (0x5555 << 1))) = 0xaaaa;
  *((unsigned volatile short *) (fbase | (0x2aaa << 1))) = 0x5555;
  *((unsigned volatile short *) (fbase | (0x5555 << 1))) = 0x8080;
  *((unsigned volatile short *) (fbase | (0x5555 << 1))) = 0xaaaa;
  *((unsigned volatile short *) (fbase | (0x2aaa << 1))) = 0x5555;
  *address = 0x3030;
#else
  *((unsigned volatile short *) (fbase | (0x0555 << 1))) = 0xaaaa;/*first*/
  *((unsigned volatile short *) (fbase | (0x02aa << 1))) = 0x5555;/*second*/
  *((unsigned volatile short *) (fbase | (0x0555 << 1))) = 0x8080;/*third*/
  *((unsigned volatile short *) (fbase | (0x0555 << 1))) = 0xaaaa;/*fourth*/
  *((unsigned volatile short *) (fbase | (0x02aa << 1))) = 0x5555;/*fifth*/
  *address = 0x3030;    /* sixth (0x30 to sector address) */
#endif

    for (;;) {
	result = *address;
/*	printk("result: %x\n", result);    */
	if ((result & 0x8080) == 0x8080)
	    break;    /* sucessful erase */
	if (((result & 0xff00) != 0xff00) && (result & 0x2000)) {
	     printk("%s(%d): erase failed: high byte, address %p\n",
		    __FILE__, __LINE__, address);
	     *((unsigned volatile short *) fbase) = 0xf0f0; /* Reset */
	    break;
	}    
	if (((result & 0x00ff) != 0x00ff) && (result & 0x0020)) {
	     printk("%s(%d): erase failed: low byte, address %p\n",
		    __FILE__, __LINE__, address);
	     *((unsigned volatile short *) fbase) = 0xf0f0; /* Reset */
	    break;
	}
    }

  restore_flags(flags);
  up(&spare_lock);
}
#endif    /* CONFIG_ARNEWSH || CONFIG_M5206eC3 */

/****************************************************************************/
/*                       SNAPGEAR FLASH SUPPORT                             */
/****************************************************************************/

#if defined(CONFIG_FLASH_SNAPGEAR) || defined(CONFIG_CLEOPATRA) || defined(CONFIG_M5272C3) || defined(CONFIG_COBRA5272)

static DECLARE_MUTEX(spare_lock);

/*
 *	Offsets to support the small boot segments of bottom booter flash.
 */
unsigned long flash_bootsegs[] = {
#if defined(CONFIG_FLASH4MB)
	/* This supports the AMD 29lv320 -- 8 * 8k segments */
	0x2000, 0x2000, 0x2000, 0x2000, 0x2000, 0x2000, 0x2000, 0x2000,
#else
	/* This supports the AMD 29lv800, 29lv160, etc -- 16k + 8k + 8k + 32k */
	0x4000, 0x2000, 0x2000, 0x8000,
#endif
};

#if defined(CONFIG_FLASH2MB)
#define	FLASHMASK	0x001fffff
#elif defined(CONFIG_FLASH4MB)
#define	FLASHMASK	0x003fffff
#elif defined(CONFIG_FLASH8MB)
#define	FLASHMASK	0x007fffff
#elif defined(CONFIG_FLASH16MB)
#define	FLASHMASK	0x00ffffff
#else
#define	FLASHMASK	0x000fffff
#endif

/*
 *	Support erasing and dumping to scratch FLASH segment.
 */
unsigned long	flash_dumpoffset = 0;

void flash_erasedump(void)
{
	flash_erase(arena + 7, 0);
}

void flash_writedump(char *buf, int len)
{
	flash_write(arena + 7, 0, len, (char *) buf);
}

/****************************************************************************/
/*                            AMD 8bit FLASH                                */
/****************************************************************************/

#if defined(CONFIG_FLASH8BIT)

/*
 *	FLASH erase routine for the AMD 29LVX00 family devices.
 */

static void flash_amd8_erase(struct arena_t *a, unsigned long pos)
{
  unsigned volatile char *address;
  unsigned long fbase = a->address;
  unsigned long flags;
  unsigned char status;
  int i;
  
#if 0
  printk("%s(%d): flash_amd8_erase(a=%x,pos=%x)\n", __FILE__, __LINE__,
    (int) a, (int) pos);
#endif

  if (pos >= a->length)
    return;

  address = (unsigned volatile char *) (fbase + pos);

  /* Mutex all access to FLASH memory */
  down(&spare_lock);
  save_flags(flags); cli();

#if defined(CONFIG_WATCHDOG)
  watchdog_disable();
#endif

  /* Erase this sector */
  *((volatile unsigned char *) (fbase | 0xaaa)) = 0xaa;
  *((volatile unsigned char *) (fbase | 0x555)) = 0x55;
  *((volatile unsigned char *) (fbase | 0xaaa)) = 0x80;
  *((volatile unsigned char *) (fbase | 0xaaa)) = 0xaa;
  *((volatile unsigned char *) (fbase | 0x555)) = 0x55;
  *address = 0x30;

  for (i = 0; (i < FTIMEOUT); i++) {
    status = *address;
    if ((status & 0x80) || (status & 0x20))
      break;
  }

  if (*address != 0xff) {
     printk("%s(%d): FLASH erase failed, address %p iteration=%d status=%x\n",
		__FILE__, __LINE__, address, i, status);
     *((unsigned volatile char *) fbase) = 0xf0; /* Reset */
  }

#if defined(CONFIG_WATCHDOG)
  watchdog_enable();
#endif

  restore_flags(flags);
  up(&spare_lock);
}

/*
 *	FLASH programming routine for the 29LVX00 device family.
 */

static void flash_amd8_write(struct arena_t * a, unsigned long pos, unsigned long length, char * buffer)
{
  volatile unsigned char *address;
  unsigned long flags, fbase = a->address;
  unsigned char *wbuf, status;
  int i;
  
#if 0
  printk("%s(%d): flash_amd8_write(a=%x,pos=%x,length=%d,buf=%x)\n",
	__FILE__, __LINE__, (int) a, (int) pos, (int) length, (int) buffer);
#endif

  down(&spare_lock);

#if defined(CONFIG_WATCHDOG)
  watchdog_disable();
#endif

  address = (unsigned volatile char *) (fbase + pos);
  wbuf = (unsigned char *) buffer;

  for (; (length > 0); length--, address++, wbuf++) {
  
    if (*address != *wbuf) {
      save_flags(flags); cli();

      *((volatile unsigned char *) (fbase | 0xaaa)) = 0xaa;
      *((volatile unsigned char *) (fbase | 0x555)) = 0x55;
      *((volatile unsigned char *) (fbase | 0xaaa)) = 0xa0;
      *address = *wbuf;

      for (i = 0; (i < FTIMEOUT); i++) {
	status = *address;
	if (status == *wbuf) {
	  /* Program complete */
	  break;
	}
      }

      if (*address != *wbuf) {
          printk("%s(%d): FLASH write failed i=%d, address %p -> %x(%x)\n",
		__FILE__, __LINE__, i, address, *wbuf, *address);
          *((unsigned volatile char *) fbase) = 0xf0; /* Reset */
      }

      restore_flags(flags);
    }
  }

#if defined(CONFIG_WATCHDOG)
  watchdog_enable();
#endif

  up(&spare_lock);
}


/*
 *	Program a complete FLASH image. This runs from DRAM, so no
 *	need to worry about writing to what we are running from...
 */

static void flash_amd8_writeall(struct arena_t * a, struct blkmem_program_t * prog)
{
  unsigned long		base, offset, erased;
  unsigned long		ptr, min, max;
  unsigned char		*w, status;
  int			failures;
  int			i, j, l;

#if defined(CONFIG_WATCHDOG)
  watchdog_disable();
#endif
  
#if 0
  printk("FLASH: programming");
#endif
  failures = 0;
  erased = 0;

  cli();
  
  for (i = 0; (i < prog->blocks); i++) {

#if 0
    printk("%s(%d): block=%d address=%x pos=%x length=%x range=%x-%x\n",
      __FILE__, __LINE__, i, (int) a->address, (int) prog->block[i].pos,
      (int) prog->block[i].length, (int) (prog->block[i].pos + a->address),
      (int) (prog->block[i].pos + prog->block[i].length - 1 + a->address));
#endif

    /*
     *	Erase FLASH sectors for this program block...
     */
    for (l = prog->block[i].pos / a->blksize;
	l <= ((prog->block[i].pos+prog->block[i].length-1) / a->blksize);
	l++) {

      if (erased & (0x1 << l))
	continue;

      ptr = l * a->blksize;
      offset = ptr % a->unitsize;
      base = ptr - offset;
	
      base += a->address;
      ptr += a->address;
      j = 0;

flash_redo:

#if 0
      printk("%s(%d): ERASE BLOCK sector=%d base=%x offset=%x ptr=%x\n",
	  __FILE__, __LINE__, l, (int) base, (int) offset, (int) ptr);
#endif

      /* Erase this sector */
      *((volatile unsigned char *) (base | 0xaaa)) = 0xaa;
      *((volatile unsigned char *) (base | 0x555)) = 0x55;
      *((volatile unsigned char *) (base | 0xaaa)) = 0x80;
      *((volatile unsigned char *) (base | 0xaaa)) = 0xaa;
      *((volatile unsigned char *) (base | 0x555)) = 0x55;
      *((volatile unsigned char *) ptr) = 0x30;

      for (;;) {
	status = *((volatile unsigned char *) ptr);
	if (status & 0x80) {
	  /* Erase complete */
	  break;
	}
	if (status & 0x20) {
	  printk("FLASH: (%d) erase failed\n", __LINE__);
	  /* Reset FLASH unit */
	  *((volatile unsigned char *) base) = 0xf0;
	  failures++;
	  /* Continue (with unerased sector) */
	  break;
	}
      }

      /*
       * Some flash have a set of small segments at the top or bottom.
       * These need to be erased individually, even better, different
       * devices uses different segment sizes :-(
       * This code currently only handles bottom booters...
       */
      if ((ptr & FLASHMASK) < 64*1024) {
	ptr += flash_bootsegs[j++];
        if ((ptr - base) < a->length)
	  goto flash_redo;
      }

      erased |= (0x1 << l);
    }

    /*
     *	Program FLASH with the block data...
     */
    min = prog->block[i].pos+a->address;
    max = prog->block[i].pos+prog->block[i].length+a->address;
    w = (unsigned char *) prog->block[i].data;

    /* Progress indicators... */
    printk(".");
#ifdef CONFIG_LEDMAN
	if (i & 1) {
		ledman_cmd(LEDMAN_CMD_OFF, LEDMAN_NVRAM_1);
		ledman_cmd(LEDMAN_CMD_ON,  LEDMAN_NVRAM_2);
	} else {
		ledman_cmd(LEDMAN_CMD_ON,  LEDMAN_NVRAM_1);
		ledman_cmd(LEDMAN_CMD_OFF, LEDMAN_NVRAM_2);
	}
#endif

#if 0
    printk("%s(%d): PROGRAM BLOCK min=%x max=%x\n", __FILE__, __LINE__,
      (int) min, (int) max);
#endif

    for (ptr = min; (ptr < max); ptr++, w++) {
      
      offset = (ptr - a->address) % a->unitsize;
      base = ptr - offset;

#if 0
      printk("%s(%d): PROGRAM base=%x offset=%x ptr=%x value=%x\n",
	  __FILE__, __LINE__, (int) base, (int) offset, (int) ptr, (int) *w);
#endif

      *((volatile unsigned char *) (base | 0xaaa)) = 0xaa;
      *((volatile unsigned char *) (base | 0x555)) = 0x55;
      *((volatile unsigned char *) (base | 0xaaa)) = 0xa0;
      *((volatile unsigned char *) ptr) = *w;

      for (j = 0; (j < FTIMEOUT); j++) {
	status = *((volatile unsigned char *) ptr);
	if (status == *w) {
	  /* Program complete */
	  break;
	}
      }

      status = *((volatile unsigned char *) ptr);
      if (status != *w) {
	printk("FLASH: (%d) write failed, addr=%x val=%x status=%x cnt=%d\n",
		__LINE__, (int) ptr, *w, status, j);
	/* Reset FLASH unit */
	*((volatile unsigned char *) ptr) = 0xf0;
	failures++;
      }
    }
  }

#ifdef CONFIG_LEDMAN
  ledman_cmd(LEDMAN_CMD_RESET, LEDMAN_NVRAM_1);
  ledman_cmd(LEDMAN_CMD_RESET, LEDMAN_NVRAM_2);
#endif

  if (failures > 0) {
    printk("FLASH: %d failures programming FLASH!\n", failures);
    return;
  }

#if 0
  printk("\nFLASH: programming successful!\n");
#endif
  if (prog->reset) {
    printk("FLASH: rebooting...\n\n");
    HARD_RESET_NOW();
  }
}

/****************************************************************************/
/*                            AMD 16bit FLASH                               */
/****************************************************************************/

#else

/*
 *	FLASH erase routine for the AMD 29LVxxx family devices.
 */

static void flash_amd16_erase(struct arena_t *a, unsigned long pos)
{
  unsigned volatile short *address;
  unsigned long fbase = a->address;
  unsigned long flags;
  unsigned short status;
  int i;
  
#if 0
  printk("%s(%d): flash_amd16_erase(a=%x,pos=%x)\n", __FILE__, __LINE__,
    (int) a, (int) pos);
#endif

  if (pos >= a->length)
    return;

  address = (unsigned volatile short *) (fbase + pos);

  /* Mutex all access to FLASH memory */
  down(&spare_lock);
  save_flags(flags); cli();

#if defined(CONFIG_WATCHDOG)
  watchdog_disable();
#endif

  /* Erase this sector */
  *((volatile unsigned short *) (fbase | (0x555 << 1))) = 0xaaaa;
  *((volatile unsigned short *) (fbase | (0x2aa << 1))) = 0x5555;
  *((volatile unsigned short *) (fbase | (0x555 << 1))) = 0x8080;
  *((volatile unsigned short *) (fbase | (0x555 << 1))) = 0xaaaa;
  *((volatile unsigned short *) (fbase | (0x2aa << 1))) = 0x5555;
  *address = 0x3030;

  for (i = 0; (i < FTIMEOUT); i++) {
    status = *address;
    if ((status & 0x0080) || (status & 0x0020))
      break;
  }

  if (*address != 0xffff) {
     printk("%s(%d): FLASH erase failed, address %p iteration=%d status=%x\n",
		__FILE__, __LINE__, address, i, status);
     *((unsigned volatile short *) fbase) = 0xf0f0; /* Reset */
  }

#if defined(CONFIG_WATCHDOG)
  watchdog_enable();
#endif

  restore_flags(flags);
  up(&spare_lock);
}

/*
 *	FLASH programming routine for the 29LVxxx device family.
 */

static void flash_amd16_write(struct arena_t * a, unsigned long pos, unsigned long length, char * buffer)
{
  volatile unsigned short *address;
  unsigned long flags, fbase = a->address;
  unsigned short *wbuf, status;
  int i;
  
#if 0
  printk("%s(%d): flash_amd16_write(a=%x,pos=%x,length=%d,buf=%x)\n",
	__FILE__, __LINE__, (int) a, (int) pos, (int) length, (int) buffer);
#endif

  down(&spare_lock);

#if defined(CONFIG_WATCHDOG)
  watchdog_disable();
#endif

  address = (unsigned volatile short *) (fbase + pos);
  wbuf = (unsigned short *) buffer;

  length += 1;   /* Fix it so that an odd number of bytes gets written correctly */
  for (length >>= 1; (length > 0); length--, address++, wbuf++) {
  
    if (*address != *wbuf) {
      save_flags(flags); cli();

      *((volatile unsigned short *) (fbase | (0x555 << 1))) = 0xaaaa;
      *((volatile unsigned short *) (fbase | (0x2aa << 1))) = 0x5555;
      *((volatile unsigned short *) (fbase | (0x555 << 1))) = 0xa0a0;
      *address = *wbuf;

      for (i = 0; (i < FTIMEOUT); i++) {
	status = *address;
	if (status == *wbuf) {
	  /* Program complete */
	  break;
	}
      }

      if (*address != *wbuf) {
          printk("%s(%d): FLASH write failed i=%d, address %p -> %x(%x)\n",
		__FILE__, __LINE__, i, address, *wbuf, *address);
          *((unsigned volatile short *) fbase) = 0xf0f0; /* Reset */
      }

      restore_flags(flags);
    }
  }

#if defined(CONFIG_WATCHDOG)
  watchdog_enable();
#endif

  up(&spare_lock);
}


/*
 *	Program a complete FLASH image. This runs from DRAM, so no
 *	need to worry about writing to what we are running from...
 */

static void flash_amd16_writeall(struct arena_t * a, struct blkmem_program_t * prog)
{
  unsigned long		base, offset, erased;
  unsigned long		ptr, min, max;
  unsigned short	*w, status;
  int			failures;
  int			i, j, l;

#if defined(CONFIG_WATCHDOG)
  watchdog_disable();
#endif
  
#if 0
  printk("FLASH: programming");
#endif
  failures = 0;
  erased = 0;

  cli();
  
  for (i = 0; (i < prog->blocks); i++) {

#if 0
    printk("%s(%d): block=%d address=%x pos=%x length=%x range=%x-%x\n",
      __FILE__, __LINE__, i, (int) a->address, (int) prog->block[i].pos,
      (int) prog->block[i].length, (int) (prog->block[i].pos + a->address),
      (int) (prog->block[i].pos + prog->block[i].length - 1 + a->address));
#endif

    /*
     *	Erase FLASH sectors for this program block...
     */
    for (l = prog->block[i].pos / a->blksize;
	l <= ((prog->block[i].pos+prog->block[i].length-1) / a->blksize);
	l++) {

      if (erased & (0x1 << l))
	continue;

      ptr = l * a->blksize;
      offset = ptr % a->unitsize;
      base = ptr - offset;
	
      base += a->address;
      ptr += a->address;
      j = 0;

flash_redo:

#if 0
      printk("%s(%d): ERASE BLOCK sector=%d base=%x offset=%x ptr=%x\n",
	  __FILE__, __LINE__, l, (int) base, (int) offset, (int) ptr);
#endif

      /* Erase this sector */
      /* FIX: check which byte lane the value needs to be on */
      *((volatile unsigned short *) (base | (0x555 << 1))) = 0xaaaa;
      *((volatile unsigned short *) (base | (0x2aa << 1))) = 0x5555;
      *((volatile unsigned short *) (base | (0x555 << 1))) = 0x8080;
      *((volatile unsigned short *) (base | (0x555 << 1))) = 0xaaaa;
      *((volatile unsigned short *) (base | (0x2aa << 1))) = 0x5555;
      *((volatile unsigned short *) ptr) = 0x3030;

      for (;;) {
	status = *((volatile unsigned short *) ptr);
	if (status & 0x0080) {
	  /* Erase complete */
	  break;
	}
	if (status & 0x0020) {
	  printk("FLASH: (%d) erase failed\n", __LINE__);
	  /* Reset FLASH unit */
	  *((volatile unsigned short *) base) = 0xf0f0;
	  failures++;
	  /* Continue (with unerased sector) */
	  break;
	}
      }

      /*
       * Some flash have a set of small segments at the top or bottom.
       * These need to be erased individually, even better, different
       * devices uses different segment sizes :-(
       * This code currently only handles bottom booters...
       */
      if ((ptr & FLASHMASK) < 64*1024) {
	ptr += flash_bootsegs[j++];
        if ((ptr - base) < a->length)
	  goto flash_redo;
      }

      erased |= (0x1 << l);
    }

    /*
     *	Program FLASH with the block data...
     */
    min = prog->block[i].pos+a->address;
    max = prog->block[i].pos+prog->block[i].length+a->address;
    w = (unsigned short *) prog->block[i].data;

    /* Progress indicators... */
    printk(".");
#ifdef CONFIG_LEDMAN
	if (i & 1) {
		ledman_cmd(LEDMAN_CMD_OFF, LEDMAN_NVRAM_1);
		ledman_cmd(LEDMAN_CMD_ON,  LEDMAN_NVRAM_2);
	} else {
		ledman_cmd(LEDMAN_CMD_ON,  LEDMAN_NVRAM_1);
		ledman_cmd(LEDMAN_CMD_OFF, LEDMAN_NVRAM_2);
	}
#endif

#if 0
    printk("%s(%d): PROGRAM BLOCK min=%x max=%x\n", __FILE__, __LINE__,
      (int) min, (int) max);
#endif

    for (ptr = min; (ptr < max); ptr += 2, w++) {
      
      offset = (ptr - a->address) % a->unitsize;
      base = ptr - offset;

#if 0
      printk("%s(%d): PROGRAM base=%x offset=%x ptr=%x value=%x\n",
	  __FILE__, __LINE__, (int) base, (int) offset, (int) ptr, (int) *w);
#endif

      *((volatile unsigned short *) (base | (0x555 << 1))) = 0xaaaa;
      *((volatile unsigned short *) (base | (0x2aa << 1))) = 0x5555;
      *((volatile unsigned short *) (base | (0x555 << 1))) = 0xa0a0;
      *((volatile unsigned short *) ptr) = *w;

      for (j = 0; (j < FTIMEOUT); j++) {
	status = *((volatile unsigned short *) ptr);
	if (status == *w) {
	  /* Program complete */
	  break;
	}
      }

      status = *((volatile unsigned short *) ptr);
      if (status != *w) {
	printk("FLASH: (%d) write failed, addr=%x val=%x status=%x cnt=%d\n",
		__LINE__, (int) ptr, *w, status, j);
	/* Reset FLASH unit */
	*((volatile unsigned short *) ptr) = 0xf0f0;
	failures++;
      }
    }
  }

#ifdef CONFIG_LEDMAN
  ledman_cmd(LEDMAN_CMD_RESET, LEDMAN_NVRAM_1);
  ledman_cmd(LEDMAN_CMD_RESET, LEDMAN_NVRAM_2);
#endif

  if (failures > 0) {
    printk("FLASH: %d failures programming FLASH!\n", failures);
    return;
  }

#if 0
  printk("\nFLASH: programming successful!\n");
#endif
  if (prog->reset) {
    printk("FLASH: rebooting...\n\n");
    HARD_RESET_NOW();
  }
}

#endif /* !CONFIG_FLASH8BIT */

/****************************************************************************/
/*                             INTEL FLASH                                  */
/****************************************************************************/
#if defined(CONFIG_INTELFLASH)
#if defined(CONFIG_FLASH16BIT)
	typedef unsigned short itype;
#else
	typedef unsigned char itype;
#endif

/*
 *	FLASH erase routine for the Intel Strata FLASH.
 */

void flash_intel_erase(struct arena_t *a, unsigned long pos)
{
  volatile itype *address;
  unsigned long fbase = a->address;
  unsigned long flags;
  itype status;
  int i;
  
#if 0
  printk("%s(%d): flash_intel_erase(a=%x,pos=%x)\n", __FILE__, __LINE__,
    (int) a, (int) pos);
#endif

  if (pos >= a->length)
    return;

#ifdef CONFIG_LEDMAN
    ledman_cmd(LEDMAN_CMD_OFF, (pos&0x20000) ? LEDMAN_NVRAM_1 : LEDMAN_NVRAM_2);
    ledman_cmd(LEDMAN_CMD_ON, (pos&0x20000) ? LEDMAN_NVRAM_2 : LEDMAN_NVRAM_1);
#endif

  address = (volatile itype *) (fbase + pos);

  /* Mutex all access to FLASH memory */
  down(&spare_lock);
  save_flags(flags); cli();

#if defined(CONFIG_WATCHDOG)
  watchdog_disable();
#endif

#if 0
  /* add this in if you want sectors unlocked as you erase them */
  /* Unlock this sector */
  *address = 0x60;
  *address = 0xd0;

  for (i = 0; (i < FTIMEOUT); i++) {
    status = *address;
    if (status & 0x80)
      break;
  }

  /* Restore FLASH to normal read mode */
  *address = 0xff;
#endif

  *address = 0x20;
  *address = 0xd0;

  for (i = 0; (i < FTIMEOUT); i++) {
    status = *address;
    if (status & 0x80)
      break;
  }

  /* Restore FLASH to normal read mode */
  *address = 0xff;

  if (*address != (itype) 0xffff) {
     printk("FLASH: (%d): erase failed, address %p iteration=%d "
		"status=%x\n", __LINE__, address, i, (int) status);
  }

#if defined(CONFIG_WATCHDOG)
  watchdog_enable();
#endif

  restore_flags(flags);
  up(&spare_lock);
}


/*
 *	FLASH programming routine for Intel Strata FLASH.
 */

void flash_intel_write(struct arena_t * a, unsigned long pos, unsigned long length, char * buffer)
{
  unsigned long		flags, ptr, min, max;
  itype			*lp, *lp0, status;
  int			failures, j, k, l;
  
#if 0
  printk("%s(%d): flash_intel_write(a=%x,pos=%x,length=%d,buf=%x)\n",
	__FILE__, __LINE__, (int) a, (int) pos, (int) length, (int) buffer);
#endif

  down(&spare_lock);

#if defined(CONFIG_WATCHDOG)
  watchdog_disable();
#endif

  min = (a->address + pos);
  max = min + length;
  lp = (itype *) buffer;

  for (ptr = min; (ptr < max); ptr += l) {

      save_flags(flags); cli();

      /* Determine write size */
      lp0 = lp;
      j = max - ptr;
      l = (j < 32) ? j : 32;
      if ((ptr & ~0x1f) != ptr) {
	j = 32 - (ptr & 0x1f);
	l = (l < j) ? l : j;
      }

      /* Program next buffer bytes */
      for (j = 0; (j < FTIMEOUT); j++) {
	*((volatile itype *) ptr) = 0xe8;
	status = *((volatile itype *) ptr);
	if (status & 0x80)
	  break;
      }
      if ((status & 0x80) == 0)
	goto writealldone;

      *((volatile itype *) ptr) = (l-1)/sizeof(itype);
      for (j = 0; j < l; j += sizeof(itype))
	*((volatile itype *) (ptr+j)) = *lp++;

      *((volatile itype *) ptr) = 0xd0;

      for (j = 0; (j < FTIMEOUT); j++) {
	status = *((volatile itype *) ptr);
	if (status & 0x80) {
	  /* Program complete */
	  break;
	}
      }

writealldone:
      /* Restore FLASH to normal read mode */
      *((volatile itype *) ptr) = 0xff;

      for (k = 0; k < l; k += sizeof(itype), lp0++) {
      	status = *((volatile itype *) (ptr+k));
        if (status != *lp0) {
		printk("FLASH: (%d): write failed, addr=%08x wrote=%x "
		"read=%x cnt=%d len=%d\n",
		__LINE__, (int) (ptr+k), (int) *lp0, (int) status, j, l);
		failures++;
	}
      }

      restore_flags(flags);
    }

#if defined(CONFIG_WATCHDOG)
  watchdog_enable();
#endif

  up(&spare_lock);
}


#if 0
/*
 *	Slow FLASH programming routine for Intel Strata FLASH.
 */
void flash_intel_writeslow(struct arena_t * a, unsigned long pos, unsigned long length, char * buffer)
{
  volatile unsigned char *address;
  unsigned long flags, fbase = a->address;
  unsigned char *lbuf, status;
  int i;
  
#if 0
  printk("%s(%d): flash_intel_write(a=%x,pos=%x,length=%d,buf=%x)\n",
	__FILE__, __LINE__, (int) a, (int) pos, (int) length, (int) buffer);
#endif

  down(&spare_lock);

#if defined(CONFIG_WATCHDOG)
  watchdog_disable();
#endif

  address = (unsigned volatile char *) (fbase + pos);
  lbuf = (unsigned char *) buffer;

  for (; (length > 0); length--, address++, lbuf++) {
  
    if (*address != *lbuf) {
      save_flags(flags); cli();

      *address = 0x40;
      *address = *lbuf;

      for (i = 0; (i < FTIMEOUT); i++) {
	status = *address;
	if (status & 0x80) {
	  /* Program complete */
	  break;
	}
      }

      /* Restore FLASH to normal read mode */
      *address = 0xff;

      if (*address != *lbuf) {
          printk("FLASH: (%d): write failed i=%d, address %p -> %x(%x)\n",
		__LINE__, i, address, (int) *lbuf, (int) *address);
      }

      restore_flags(flags);
    }
  }

#if defined(CONFIG_WATCHDOG)
  watchdog_enable();
#endif

  up(&spare_lock);
}
#endif


/*
 *	Program a complete FLASH image into an Intel Strata FLASH part.
 *	This runs from DRAM, so no need to worry about writing to what
 *	we are running from...
 */

void flash_intel_writeall(struct arena_t * a, struct blkmem_program_t * prog)
{
  unsigned long		erased[16];
  unsigned long		base, offset, ptr, min, max;
  unsigned char		*lp, *lp0, status;
  int			failures, i, j, k, l;

#if defined(CONFIG_WATCHDOG)
  watchdog_disable();
#endif

#if 0
  printk("FLASH: programming");
#endif
  failures = 0;
  memset(&erased[0], 0, sizeof(erased));

  cli();
  
  for (i = 0; (i < prog->blocks); i++) {

#if 0
    printk("%s(%d): block=%d address=%x pos=%x length=%x range=%x-%x\n",
      __FILE__, __LINE__, i, (int) a->address, (int) prog->block[i].pos,
      (int) prog->block[i].length, (int) (prog->block[i].pos + a->address),
      (int) (prog->block[i].pos + prog->block[i].length - 1 + a->address));
#endif

    /*
     *	Erase FLASH sectors for this program block...
     */
    for (l = prog->block[i].pos / a->blksize;
	l <= ((prog->block[i].pos+prog->block[i].length-1) / a->blksize);
	l++) {

      if (erased[(l / 32)] & (0x1 << (l % 32)))
	continue;

      ptr = l * a->blksize;
      offset = ptr % a->unitsize;
      base = ptr - offset;

      base += a->address;
      ptr += a->address;
      j = 0;

#if 0
      printk("%s(%d): ERASE BLOCK sector=%d ptr=%x\n",
	  __FILE__, __LINE__, l, (int) ptr);
#endif

      /* Erase this sector */
      *((volatile unsigned char *) ptr) = 0x20;
      *((volatile unsigned char *) ptr) = 0xd0;

      for (k = 0; (k < FTIMEOUT); k++) {
	status = *((volatile unsigned char *) ptr);
	if (status & 0x80) {
	  /* Erase complete */
	  status = *((volatile unsigned char *) ptr);
	  break;
	}
      }

      /* Restore FLASH to normal read mode */
      *((volatile unsigned char *) ptr) = 0xff;

      if (k >= FTIMEOUT) {
	  printk("FLASH: (%d) erase failed, status=%08x\n",
		__LINE__, (int) status);
	  failures++;
	  /* Continue (with unerased sector) */
	  break;
      }

      erased[(l / 32)] |= (0x1 << (l % 32));
    }

    /*
     *	Program FLASH with the block data...
     */
    min = prog->block[i].pos+a->address;
    max = prog->block[i].pos+prog->block[i].length+a->address;
    lp = (unsigned char *) prog->block[i].data;

    /* Progress indicators... */
    printk(".");
#ifdef CONFIG_LEDMAN
    ledman_cmd(LEDMAN_CMD_OFF, (i & 1) ? LEDMAN_NVRAM_1 : LEDMAN_NVRAM_2);
    ledman_cmd(LEDMAN_CMD_ON, (i & 1) ? LEDMAN_NVRAM_2 : LEDMAN_NVRAM_1);
#endif

#if 0
    printk("%s(%d): PROGRAM BLOCK min=%x max=%x\n", __FILE__, __LINE__,
      (int) min, (int) max);
#endif


    for (ptr = min; (ptr < max); ptr += l) {

      /* Determine write size */
      lp0 = lp;
      j = max - ptr;
      l = (j < 32) ? j : 32;
      if ((ptr & ~0x1f) != ptr) {
	j = 32 - (ptr & 0x1f);
	l = (l < j) ? l : j;
      }

#if 0
      printk("%s(%d): PROGRAM ptr=%x l=%d\n", __FILE__, __LINE__, (int) ptr, l);
#endif

      /* Program next buffer bytes */
      for (j = 0; (j < FTIMEOUT); j++) {
	*((volatile unsigned char *) ptr) = 0xe8;
	status = *((volatile unsigned char *) ptr);
	if (status & 0x80)
	  break;
      }
      if ((status & 0x80) == 0)
	goto intelwritealldone;

      *((volatile unsigned char *) ptr) = (l-1);
      for (j = 0; (j < l); j++)
	*((volatile unsigned char *) (ptr+j)) = *lp++;

      *((volatile unsigned char *) ptr) = 0xd0;

      for (j = 0; (j < FTIMEOUT); j++) {
	status = *((volatile unsigned char *) ptr);
	if (status & 0x80) {
	  /* Program complete */
	  break;
	}
      }

intelwritealldone:
      /* Restore FLASH to normal read mode */
      *((volatile unsigned char *) ptr) = 0xff;

      for (k = 0; (k < l); k++, lp0++) {
      	status = *((volatile unsigned char *) (ptr+k));
        if (status != *lp0) {
		printk("FLASH: (%d): write failed, addr=%08x wrote=%02x "
		"read=%02x cnt=%d len=%d\n",
		__LINE__, (int) (ptr+k), (int) *lp0, (int) status, j, l);
		failures++;
	}
      }
    }
  }

#ifdef CONFIG_LEDMAN
  ledman_cmd(LEDMAN_CMD_RESET, LEDMAN_NVRAM_1);
  ledman_cmd(LEDMAN_CMD_RESET, LEDMAN_NVRAM_2);
#endif

  if (failures > 0) {
    printk("FLASH: %d failures programming FLASH!\n", failures);
    return;
  }

#if 0
  printk("\nFLASH: programming successful!\n");
#endif
  if (prog->reset) {
    printk("FLASH: rebooting...\n\n");
    HARD_RESET_NOW();
  }
}

#endif /* defined(CONFIG_INTELFLASH) */
/****************************************************************************/
#endif /* CONFIG_FLASH_SNAPGEAR */
/****************************************************************************/


#ifdef CONFIG_SHGLCORE

static DECLARE_MUTEX(spare_lock);

void read_spare(struct arena_t * a, unsigned long pos, unsigned long length, char * buffer)
{

#ifdef DEBUG
  printk("rsl\n");
#endif
  
  /* Mutex all access to FLASH */
  down(&spare_lock);
  
#ifdef DEBUG
  printk("rsld\n");
#endif
  
  /* Just copy the data into target buffer */
  memcpy( buffer, (void*)(a->address+pos), length);

  /* Release MUTEX */
  up(&spare_lock);
  
#ifdef DEBUG
  printk("rsud\n");
#endif
}

void write_spare(struct arena_t * a, unsigned long pos, unsigned long length, char * buffer)
{
  unsigned long start;
  unsigned char c;
  volatile unsigned char * address;
  unsigned char result;
  unsigned long fbase = a->address;
  unsigned long flags;
  
#if 0
  for(i = pos / a->blksize; i <= ((pos+length-1) / a->blksize); i++) {
    if (test_bit(i, &a->auto_erase_bits)) {
      /* erase sector start */
      printk("Autoerase of sector %d\n", i);
      erase_spare(a, i * a->blksize);
      clear_bit(i, &a->auto_erase_bits);
    }
  }
#endif

#ifdef DEBUG
  printk("wsl\n");
#endif
  
  down(&spare_lock);
  
#ifdef DEBUG
  printk("wsld\n");
#endif
  
  start = jiffies;
  
  address = (unsigned volatile char*)(fbase+pos);
  
  while (length>0) {
  
    c = *buffer++;
    
    /*printk("Checking spare_flash program of byte %lx, at address %p, value %x (%c), current %x (%c)\n", pos, address, c, c, *address, *address);*/

    if (*address != c) {
  
      /*printk("Starting spare_flash program of byte %lx, at address %p\n", pos, address);*/
      
      
      if (c & ~*address) {
        printk("Unable to write byte at %p (impossible bit transition in %x, actual %x)\n", address, c, *address);
        /*continue;*/
      }

	save_flags(flags); cli();

      *(unsigned volatile char *)(fbase | 0x5555)=0x0aa;
      *(unsigned volatile char *)(fbase | 0x2aaa)=0x055;
      *(unsigned volatile char *)(fbase | 0x5555)=0x0a0;
      
      *address = c;
               
      for(;;) {
        result = *address;
        /*printk("Read value %x (%c)\n", result, result);*/
        if ((result & 0x80) == (c & 0x80))
          break;
        if (result & 0x20) {
          printk("timeout of FLASH write at address %p of value %x (actual %x)\n", address, c, *address);
          *(unsigned volatile char *)(fbase)=0x0f0; /* Reset */
          break;
        }
      }
      
      restore_flags(flags);

      /*printk("Completed spare_flash program of byte %lx, at address %p\n", pos, address);*/
        
#if 0
      if (jiffies != start) {
        /*printk("Spare_flash rescheduling in write\n");*/
        current->state = TASK_INTERRUPTIBLE;
        current->timeout = jiffies;
        schedule();
        current->timeout = 0;
        /*schedule();*/
        start = jiffies;
      }
#endif
    }

    address++;
    length--;
  }
  
  up(&spare_lock);
  
#ifdef DEBUG
  printk("wsud\n");
#endif
}

void erase_spare(struct arena_t * a, unsigned long pos)
{
  unsigned long fbase = a->address;
  int delay;
  unsigned volatile char * address;
  unsigned long flags;
  
  if (pos >= a->length)
    return;
  
  /* Mutex all access to FLASH memory */
  
#ifdef DEBUG
  printk("esl\n");
#endif
  
  down(&spare_lock);

#ifdef DEBUG
  printk("esld\n");
#endif

  address = (unsigned volatile char*)(fbase + pos);

  printk("Starting spare_flash erase of byte %lx, at address %p\n", pos, address);
  
  save_flags(flags); cli();

again:

  delay = HZ/4+1;
  
  /* Initiate erase of FLASH sector */
  
  *(unsigned volatile char *)(fbase | 0x5555)=0x0aa;
  *(unsigned volatile char *)(fbase | 0x2aaa)=0x055;
  *(unsigned volatile char *)(fbase | 0x5555)=0x080;
  *(unsigned volatile char *)(fbase | 0x5555)=0x0aa;
  *(unsigned volatile char *)(fbase | 0x2aaa)=0x055;
                       
  *address = 0x030;
  
  /* Delay until erase is complete */
     
  for (;;) {
    unsigned char result;
#ifdef original_spare_erase_delay
    struct wait_queue *wait = NULL;
#ifdef DEBUG
    printk("Spare_flash erase delaying for %d ticks, status is %x\n", delay, (unsigned int)*address);
#endif
    
    current->timeout = jiffies + delay;
#if 0    
    current->state = TASK_INTERRUPTIBLE;
    schedule();
    current->timeout = 0;
#endif
    interruptible_sleep_on(&wait);
#endif
    udelay(100000);
    
    result = *address;
    if (result & 0x80)
       break;
    if (result & 0x20) {
       printk("timeout of Spare_flash erase of address %p\n", address);
       *(unsigned volatile char *)(fbase)=0x0f0; /* Reset */
       printk("Sleeping a second and retrying\n");

       udelay(1000000);
       
       goto again;
    }
  }
  
  restore_flags(flags);

#ifdef DEBUG
  printk("Completed spare_flash erase of byte %lx, at address %p\n", pos, address);
#endif
  
  up(&spare_lock);

#ifdef DEBUG
  printk("esud\n");
#endif

}

#define VSP(X) (*(volatile unsigned short *)(X))
#define VSC(X) (*(volatile unsigned char *)(X))

#define SCSR  VSP(0xfffc0c)
#define SCSR_TDRE (1<<8)

#define SCDR  VSP(0xfffC0e)

#define print_char(x) ({			\
	while (!(SCSR & SCSR_TDRE))		\
		;				\
	SCDR = (x);				\
})

#define print_hexdigit(x) ({			\
	int digit = (x) & 0xf;			\
	if (digit>9)				\
		print_char('a'+digit-10);	\
	else					\
		print_char('0'+digit);		\
						\
})

#define print_num(x) ({				\
	unsigned long num = (x);		\
	print_hexdigit(num >> 28);		\
	print_hexdigit(num >> 24);		\
	print_hexdigit(num >> 20);		\
	print_hexdigit(num >> 16);		\
	print_hexdigit(num >> 12);		\
	print_hexdigit(num >> 8);		\
	print_hexdigit(num >> 4);		\
	print_hexdigit(num >> 0);		\
})

/* Note: sub_program_main must not reference _any_ data or code outside of itself,
   or leave interrupts enabled, due to the fact that it is probably erasing
   & reloading the kernel. */

#define SET_SHORT(x,y) VSP((x)) = (y)
/*#define SET_SHORT(x,y) ({})*/ /*print_char('>');print_num(x);*/ /*printk("%8.8lx <= %04x\n", (x), (y))*/

#define SET_CHAR(x,y) VSC((x)) = (y)
/*#define SET_CHAR(x,y) ({})*/ /*print_char('>');print_num(x);*/ /*printk("%8.8lx <= %02x\n", (x), (y))*/

#define GET_SHORT(x) VSP((x))
/*#define GET_SHORT(x) ({0;})*/ /*({print_char('<');print_num(x);0;})*/ /*(printk("%8.8lx => ....\n", (x)),0)*/

#define GET_CHAR(x) VSC((x))
/*#define GET_CHAR(x) ({0;})*/ /*({print_char('<');print_num(x);0;})*/ /*(printk("%8.8lx => ..\n", (x)),0)*/


void sub_program_main(struct arena_t * a, struct blkmem_program_t * prog)
{
  volatile int i,l;
  unsigned long base, offset, ptr, min, max;
  unsigned char * c;
  unsigned int erased = 0;
  int failures;
  int retry;
  
  cli();

  retry = 0;

again:

  SET_ALARM_LED(1);
  
  retry++;
  
  if (retry>5) {
  	goto give_up;
  }

    print_char('\r');
    print_char('\n');
    print_char('R');
    print_char('0' + retry);

  failures = 0;
  erased = 0;
  
/*  for(i=prog->blocks-1;i>=0;i--) {*/
  for(i=0;i<prog->blocks;i++) {

    SET_COMM_STATUS_LED(!GET_COMM_STATUS_LED());

    print_char('\r');
    print_char('\n');
    print_num(prog->block[i].pos+a->address);
    print_char('-');
    print_num(prog->block[i].pos+prog->block[i].length-1+a->address);
    print_char('\r');
    print_char('\n');

    if(prog->block[i].length > 0xE0000)
      break;

    for(l=prog->block[i].pos / a->blksize; l <= ((prog->block[i].pos+prog->block[i].length-1) / a->blksize); l++) {
      if (!test_bit(l, &erased)) {
        
 	print_char('E');
 	print_char('0' + l / 10);
 	print_char('0' + l % 10);
 	print_char('\r');
 	print_char('\n');
 	
 	if (l <  1)
 	  break;
 	/*if (l >= 8)
 	  break;*/

	ptr = l * a->blksize;
	offset = ptr % a->unitsize;
	base = ptr - offset;
	
	base += a->address;
	ptr += a->address;
	
	print_char('b');
	print_char('a');
	print_char('s');
	print_char('e');
	print_char(' ');
	print_num(base);
	print_char('\r');
	print_char('\n');
	print_char('o');
	print_char('f');
	print_char('f');
	print_char(' ');
	print_num(offset);
	print_char('\r');
	print_char('\n');
	print_char('p');
	print_char('t');
	print_char('r');
	print_char(' ');
	print_num(ptr);
	print_char('\r');
	print_char('\n');

        set_bit(l, &erased);

        if (ptr <  0x020000)
          break;
        /*if (ptr >= 0x100000)
          break;*/
        
        print_num(ptr);
        
 	SET_COMM_ERROR_LED(1);
 	
        /* Erase even half of sector */
        SET_SHORT( (base | (0x5555 << 1)), 0xaa00);
        SET_SHORT( (base | (0x2aaa << 1)), 0x5500);
        SET_SHORT( (base | (0x5555 << 1)), 0x8000);
        SET_SHORT( (base | (0x5555 << 1)), 0xaa00);
        SET_SHORT( (base | (0x2aaa << 1)), 0x5500);

        SET_SHORT( ptr, 0x3000);
#ifdef original_erase_logic
        while (!(GET_SHORT(ptr) & 0x8000))
          ;
#else
	for (;;) {
		unsigned int status = GET_SHORT(ptr);
		if (status & 0x8000) {
			/* Erase complete */
			break;
		}
		if (status & 0x2000) {
			/* Check again */
			status = GET_SHORT(ptr);
			if (status & 0x8000) {
				/* Erase complete */
				break;
			}
			
			/* Erase failed */
			print_char('F');
			
			/* Reset FLASH unit */
			SET_SHORT( base, 0xf000);
			
			failures++;
			
			/* Continue (with unerased sector) */
			break;
		}
        }
#endif

        print_char(':');

        /* Erase odd half of sector */
        SET_SHORT( (base | (0x5555 << 1)), 0x00aa);
        SET_SHORT( (base | (0x2aaa << 1)), 0x0055);
        SET_SHORT( (base | (0x5555 << 1)), 0x0080);
        SET_SHORT( (base | (0x5555 << 1)), 0x00aa);
        SET_SHORT( (base | (0x2aaa << 1)), 0x0055);

        SET_SHORT( ptr, 0x0030);
#ifdef original_erase_logic
        while (!(GET_SHORT(ptr) & 0x0080))
          ;
#else
	for (;;) {
		unsigned int status = GET_SHORT(ptr);
		if (status & 0x0080) {
			/* Erase complete */
			break;
		}
		if (status & 0x0020) {
		
			/* Check again */			
			status = GET_SHORT(ptr);
			if (status & 0x0080) {
				/* Erase complete */
				break;
			}

			/* Erase failed */
			print_char('F');
			
			/* Reset FLASH unit */
			SET_SHORT( base, 0x00f0);
			
			failures++;
			
			/* Continue (with unerased sector) */
			break;
		}
        }
#endif

        print_char(':');
          
#if 0
        probe = (volatile unsigned short*)(fbase + a->blksize * l);
        *probe = 0x3000;
        while (!(*probe & 0x8000))
          ;
          
        print_char('.');
        
        /* Erase odd half of sector */
        *(unsigned volatile short *)(fbase | (0x5555 << 1))=0x00aa;
        *(unsigned volatile short *)(fbase | (0x2aaa << 1))=0x0055;
        *(unsigned volatile short *)(fbase | (0x5555 << 1))=0x0080;
        *(unsigned volatile short *)(fbase | (0x5555 << 1))=0x00aa;
        *(unsigned volatile short *)(fbase | (0x2aaa << 1))=0x0055;

        probe = (volatile unsigned short*)(fbase + a->blksize * l);
        *probe = 0x0030;
        while (!(*probe & 0x0080))
          break;
          
        print_char('.');
#endif

 	SET_COMM_ERROR_LED(0);
      }
    }
    
    
    min = prog->block[i].pos+a->address;
    max = prog->block[i].pos+prog->block[i].length+a->address;
    for(ptr=min, c=prog->block[i].data; ptr<max; ptr++, c++) {
      
      offset = (ptr-a->address) % a->unitsize;
      base = ptr - offset;

      if (ptr <  0x020000)
        break;
      /*if (ptr >= 0x100000)
        break;*/

      /*print_char('.');*/
      
#if 0
      if ((fbase & 1) == 0) { /* Even bank */
        *(unsigned volatile short *)(fbase | (0x5555 << 1))=0xaa00;
        *(unsigned volatile short *)(fbase | (0x2aaa << 1))=0x5500;
        *(unsigned volatile short *)(fbase | (0x5555 << 1))=0xa000;
        *(unsigned volatile short *)(fbase + (b & ~1))     =*c << 8;
      } else { /* Odd bank */
        *(unsigned volatile short *)(fbase | (0x5555 << 1))=0x00aa;
        *(unsigned volatile short *)(fbase | (0x2aaa << 1))=0x0055;
        *(unsigned volatile short *)(fbase | (0x5555 << 1))=0x00a0;
        *(unsigned volatile short *)(fbase + (b & ~1))     =*c;
      }
            
      probe = (volatile unsigned char*)(fbase + b);
      while (*probe != *c)
          break;
#endif

      if ((ptr & 1) == 0) { /* Even bank */
        SET_SHORT( (base | (0x5555 << 1)), 0xaa00);
        SET_SHORT( (base | (0x2aaa << 1)), 0x5500);
        SET_SHORT( (base | (0x5555 << 1)), 0xa000);
        SET_SHORT( (ptr & ~1),      *c << 8);
      } else { /* Odd bank */
        SET_SHORT( (base | (0x5555 << 1)), 0x00aa);
        SET_SHORT( (base | (0x2aaa << 1)), 0x0055);
        SET_SHORT( (base | (0x5555 << 1)), 0x00a0);
        SET_SHORT( (ptr & ~1),      *c);
      }
      
#ifdef original_write_logic
      while (GET_CHAR(ptr) != *c)
        ;
#else
	for (;;) {
		unsigned char status = GET_CHAR(ptr);
		if ((status & 0x80) == (*c & 0x80)) {
			/* Program complete */
			break;
		}
		if (status & 0x20) {
			/* Check again */
			status = GET_CHAR(ptr);
			if ((status & 0x80) == (*c & 0x80)) {
				/* Program complete */
				break;
			}
			
			/* Program failed */
			print_char('F');
			
			/* Reset FLASH unit */
			if ((ptr & 1) == 0) { /* Even bank */
				SET_SHORT( base, 0xf000);
			} else { /* Odd bank */
				SET_SHORT( base, 0x00f0);
			}
			
			failures++;
			
			/* Continue */
			break;
		}
        }
#endif
      
      /*print_char(' ');*/
    }
  }
  
  if (failures > 0) {
  	/* There were failures erasing the FLASH, so go back to the beginning and
  	try it all again -- for lack of anything better to do. */
  	
  	print_char('!');
  	goto again;
  }

give_up:

  SET_ALARM_LED(1);
  
  
  HARD_RESET_NOW();
  
  i = 1;
  while(i)
    ;
  
}

void program_main(struct arena_t * a, struct blkmem_program_t * prog)
{
  int len;
  void (*code)(struct arena_t*, struct blkmem_program_t *);
  
  printk("program_main entered, blocks = %d\n", prog->blocks);
  
  len = &program_main-&sub_program_main;
  code = kmalloc(len, GFP_KERNEL);
  memcpy(code, &sub_program_main, len);
  
  code(a, prog);

  kfree(code);
  
  /*sub_program_main(a, prog);*/
}
#endif /* CONFIG_SHGLCORE */


int general_program_func(struct inode * inode, struct file * file, struct arena_t * a, struct blkmem_program_t * prog)
{
  int i,block;
  unsigned int erased = 0;

  /* Mandatory flush of all dirty buffers */
  fsync_dev(inode->i_rdev);
  invalidate_buffers(inode->i_rdev);

  for(i=0;i<prog->blocks;i++) {
    int min= prog->block[i].pos / a->blksize;
    int max = (prog->block[i].pos + prog->block[i].length - 1) / a->blksize;
    for(block=min; block <= max; block++) {
      if (!test_bit(block, &erased)) {
	printk("Erase of sector at pos %lx of arena %d (address %p)\n", block * a->blksize, MINOR(inode->i_rdev), (void*)(a->address+block*a->blksize));
    
        /* Invoke erase function */
        a->erase_func(a, block * a->blksize);
        set_bit(block, &erased);
      }
    }
    
    printk("Write of %lu bytes at pos %lu (data at %p)\n", prog->block[i].length, prog->block[i].pos, prog->block[i].data);
    
    a->write_func(a, prog->block[i].pos, prog->block[i].length, prog->block[i].data);
    
    schedule();
  }

#ifdef CONFIG_UCLINUX
  if (prog->reset)
  	HARD_RESET_NOW();
#endif
  return 0;
}


#if 0

static void complete_request(void * data);

static struct tq_struct complete_tq = {
		routine: complete_request,
		data: NULL
};

static void
delay_request( void )
{
  schedule_task(&complete_tq);
}


static void
complete_request( void * data)
{
  unsigned long start;
  unsigned long len;
  struct arena_t * a = arena + CURRENT_DEV;

  for(;;) {
  
  INIT_REQUEST;

  /* sectors are 512 bytes */
  start = CURRENT->sector << 9;
  len  = CURRENT->current_nr_sectors << 9;

  /*printk("blkmem: re-request %d\n", CURRENT->cmd);*/
    
  if ( CURRENT->cmd == READ ) {
    /*printk("BMre-Read: %lx:%lx > %p\n", a->address + start, len, * CURRENT->buffer);*/
	if (a->read_func)
    a->read_func(a, start, len, CURRENT->buffer);
	else {
	printk("read from unreadable !\n");
	asm volatile ("halt");
	}
  }
  else if (CURRENT->cmd == WRITE) {
    /*printk("BMre-Write: %p > %lx:%lx\n", CURRENT->buffer, a->address + start, len);*/
	if (a->write_func)
    a->write_func(a, start, len, CURRENT->buffer);
	else {
	printk("write to unwritable !\n");
	asm volatile ("halt");
	}
  }
  /*printk("ending blkmem request\n");*/
  end_request( TRUE );
  
  }
}

#endif


static void
do_blkmem_request(request_queue_t *q)
{
  unsigned long start;
  unsigned long len;
  struct arena_t * a = arena + CURRENT_DEV;

#if 0
  printk( KERN_ERR DEVICE_NAME ": request\n");
#endif

  while ( TRUE ) {
    INIT_REQUEST;
    
    /* sectors are 512 bytes */
    start = CURRENT->sector << 9;
    len  = CURRENT->current_nr_sectors << 9;

    if ((start + len) > a->length) {
      printk( KERN_ERR DEVICE_NAME ": bad access: block=%ld, count=%ld (pos=%lx, len=%lx)\n",
	      CURRENT->sector,
	      CURRENT->current_nr_sectors,
	      start+len,
	      a->length);
      end_request( FALSE );
      continue;
    }

    /*printk("blkmem: request %d\n", current->cmd);*/
    
    if ( ( CURRENT->cmd != READ ) 
	 && ( CURRENT->cmd != WRITE ) 
	 ) {
      printk( KERN_ERR DEVICE_NAME ": bad command: %d\n", CURRENT->cmd );
      end_request( FALSE );
      continue;
    }
    
    if ( CURRENT->cmd == READ ) {
      if (a->read_func) {
#if 0
        delay_request();
        return;
#else
        a->read_func(a, start, len, CURRENT->buffer);
#endif
      } else
        memcpy( CURRENT->buffer, (void*)(a->address + start), len );

    } else if (CURRENT->cmd == WRITE) {

      if (a->write_func) {
#if 0
        delay_request();
        return;
#else
        a->write_func(a, start, len, CURRENT->buffer);
#endif
      } else
        memcpy( (void*)(a->address + start), CURRENT->buffer, len );
    }

    /* printk("ending blkmem request\n"); */
    end_request( TRUE );
  }
}

#ifdef MAGIC_ROM_PTR
static int
blkmem_romptr( kdev_t dev, struct vm_area_struct * vma)
{
  struct arena_t * a = arena + MINOR(dev);

  if (a->read_func)
    return -ENOSYS; /* Can't do it, as this arena isn't in the main address space */

  vma->vm_start = a->address + vma->vm_offset;
  return 0;
}
#endif

static int blkmem_ioctl (struct inode *inode, struct file *file,
                         unsigned int cmd, unsigned long arg)
{
  struct arena_t * a = arena + MINOR(inode->i_rdev);

  switch (cmd) {

  case BMGETSIZES:   /* Return device size in sectors */
    if (!arg)
      return -EINVAL;
    return(put_user(a->blksize ? (a->length / a->blksize) : 0, (unsigned long *) arg));
    break;

  case BMGETSIZEB:   /* Return device size in bytes */
    return(put_user(a->length, (unsigned long *) arg));
    break;

  case BMSERASE:
    if (a->erase_func) {

      if (arg >= a->length)
        return -EINVAL;
    
      /* Mandatory flush of all dirty buffers */
      fsync_dev(inode->i_rdev);
      
      /* Invoke erase function */
      a->erase_func(a, arg);
    } else
      return -EINVAL;
    break;
    
  case BMSGSIZE:
    return(put_user(a->blksize, (unsigned long*)arg));
    break;

  case BMSGERASEVALUE:
    return(put_user(a->erasevalue, (unsigned char *) arg));
    break;

  case BMPROGRAM:
  {
    struct blkmem_program_t *prog = NULL, dummy;
    int i, rc = 0, size;

    if (copy_from_user(&dummy, (void *) arg, sizeof(dummy)))
      return(-EFAULT);

    if ((dummy.magic1 != BMPROGRAM_MAGIC_1) ||
        (dummy.magic2 != BMPROGRAM_MAGIC_2))
      return(-EINVAL);

	size = sizeof(dummy) + sizeof(dummy.block[0]) * dummy.blocks;

    prog = (struct blkmem_program_t *) kmalloc(size, GFP_KERNEL);
    if (prog == NULL)
      return(-ENOMEM);

    if (copy_from_user(prog, (void *) arg, size)) {
      rc = -EFAULT;
      goto early_exit;
	}

    for(i=0;i<prog->blocks;i++) {
      if(prog->block[i].magic3 != BMPROGRAM_MAGIC_3) {
        rc = -EINVAL;
        goto early_exit;
      }
    }

    for(i=0;i<prog->blocks;i++)
      if ((prog->block[i].pos > a->length) ||
          ((prog->block[i].pos+prog->block[i].length-1) > a->length)) {
        rc = -EINVAL;
        goto early_exit;
      }

    if (a->program_func)
      a->program_func(a, prog);
    else
      rc = general_program_func(inode, file, a, prog);

  early_exit:
    if (prog)
      kfree(prog);
    return(rc);
  }

  default:
    return -EINVAL;
  }
  return 0;
}

static int
blkmem_open( struct inode *inode, struct file *filp )
{
  int device;
  struct arena_t * a;

  device = DEVICE_NR( inode->i_rdev );

#if 0
  printk( KERN_ERR DEVICE_NAME ": open: %d\n", device );
#endif

  if ((MINOR(device) < 0) || (MINOR(device) >= arenas))  {
    printk("arena open of %d failed!\n", MINOR(device));
    return -ENODEV;
  }
  
  a = &arena[MINOR(device)];

#if defined(MODULE)
  MOD_INC_USE_COUNT;
#endif
  return 0;
}

static int 
blkmem_release( struct inode *inode, struct file *filp )
{
#if 0
  printk( KERN_ERR DEVICE_NAME ": release: %d\n", current_device );
#endif

  fsync_dev( inode->i_rdev );

#if defined(MODULE)
  MOD_DEC_USE_COUNT;
#endif

  return(0);
}

static struct block_device_operations blkmem_fops=
{
	open:		blkmem_open,
	release:	blkmem_release,
	ioctl:		blkmem_ioctl,
#ifdef MAGIC_ROM_PTR
	romptr:		blkmem_romptr,
#endif
};


int __init blkmem_init( void )
{
  int i;
  unsigned long realaddrs[arenas];

#ifdef FIXUP_ARENAS
  {
  FIXUP_ARENAS
  }
#endif

  for(i=0;i<arenas;i++) {
    if (arena[i].length == -1)
      arena[i].length = ntohl(*(volatile unsigned long *)(arena[i].address+8));
    blkmem_blocksizes[i] = 1024;
    blkmem_sizes[i] = (arena[i].length + (1 << 10) - 1) >> 10; /* Round up */
    arena[i].length = blkmem_sizes[i] << 10;

    realaddrs[i] = arena[i].address;
    arena[i].address = (unsigned long) ioremap_nocache((int) arena[i].address, arena[i].length);
  }


  printk("Blkmem copyright 1998,1999 D. Jeff Dionne\nBlkmem copyright 1998 Kenneth Albanowski\nBlkmem %d disk images:\n", (int) arenas);

  for(i=0;i<arenas;i++) {
    printk("%d: %lX-%lX [VIRTUAL %lX-%lX] (%s)\n", i,
	realaddrs[i], realaddrs[i]+arena[i].length-1,
	arena[i].address, arena[i].address+arena[i].length-1,
    	arena[i].rw 
    		? "RW"
    		: "RO"
    );
  }

  if (register_blkdev(MAJOR_NR, DEVICE_NAME, &blkmem_fops )) {
    printk( KERN_ERR DEVICE_NAME ": Unable to get major %d\n",
            MAJOR_NR );
    return -EBUSY;
  }

  devfs_handle = devfs_mk_dir(NULL, "blkmem", NULL);
  devfs_register_series(devfs_handle, "%u", arenas,
      DEVFS_FL_DEFAULT, MAJOR_NR, 0, S_IFBLK | S_IRUSR | S_IWUSR,
      &blkmem_fops, NULL);

  blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), &do_blkmem_request);

  read_ahead[ MAJOR_NR ] = 0;
  blksize_size[ MAJOR_NR ] = blkmem_blocksizes;
  blk_size[ MAJOR_NR ] = blkmem_sizes;
  
#ifdef ROOT_ARENA
  ROOT_DEV = MKDEV(MAJOR_NR,ROOT_ARENA);
#endif
#if !defined(MODULE)
#if !defined(CONFIG_COLDFIRE) && !defined(CONFIG_M68328) && !defined(CONFIG_EXCALIBUR) && !defined(CONFIG_ARCH_TA7S)
  /*if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {*/
  /*
   * This used to set the minor # to 1, which didn't work...
   * --gmcnutt
   */
    ROOT_DEV = MKDEV(MAJOR_NR,0);
  /*}*/
#endif
#endif
  return 0;
}

static void __exit
blkmem_exit(void )
{
  int i;

  for(i=0;i<arenas;i++)
      iounmap((void *) arena[i].address);

  if ( unregister_blkdev( MAJOR_NR, DEVICE_NAME ) != 0 )
    printk( KERN_ERR DEVICE_NAME ": unregister of device failed\n");
}

EXPORT_NO_SYMBOLS;

module_init(blkmem_init);
module_exit(blkmem_exit);