summaryrefslogtreecommitdiff
path: root/drivers/mxc/security/mxc_scc.c
blob: 20d54bfb5362f295c51a2c1efa9a113aac8020ff (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
/*
 * Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

/*!
 * @file mxc_scc.c
 *
 * This is the driver code for the Security Controller (SCC).  It has no device
 * driver interface, so no user programs may access it.  Its interaction with
 * the Linux kernel is from calls to #scc_init() when the driver is loaded, and
 * #scc_cleanup() should the driver be unloaded.  The driver uses locking and
 * (task-sleep/task-wakeup) functions of the kernel.  It also registers itself
 * to handle the interrupt line(s) from the SCC.
 *
 * Other drivers in the kernel may use the remaining API functions to get at
 * the services of the SCC.  The main service provided is the Secure Memory,
 * which allows encoding and decoding of secrets with a per-chip secret key.
 *
 * The SCC is single-threaded, and so is this module.  When the scc_crypt()
 * routine is called, it will lock out other accesses to the function.  If
 * another task is already in the module, the subsequent caller will spin on a
 * lock waiting for the other access to finish.
 *
 * Note that long crypto operations could cause a task to spin for a while,
 * preventing other kernel work (other than interrupt processing) to get done.
 *
 * The external (kernel module) interface is through the following functions:
 * @li scc_get_configuration()
 * @li scc_crypt()
 * @li scc_zeroize_memories()
 * @li scc_monitor_security_failure()
 * @li scc_stop_monitoring_security_failure()
 * @li scc_set_sw_alarm()
 * @li scc_read_register()
 * @li scc_write_register()
 *
 * All other functions are internal to the driver.
 *
 * @ingroup MXCSCC
*/
#include "sahara2/include/fsl_platform.h"
#include "sahara2/include/portable_os.h"
#include "mxc_scc_internals.h"

#include <linux/delay.h>

#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))

#include <linux/device.h>
#include <mach/clock.h>
#include <linux/device.h>

#else
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/err.h>

#endif

/*!
 * This is the set of errors which signal that access to the SCM RAM has
 * failed or will fail.
 */
#define SCM_ACCESS_ERRORS                                                  \
       (SCM_ERR_USER_ACCESS | SCM_ERR_ILLEGAL_ADDRESS |                    \
        SCM_ERR_ILLEGAL_MASTER | SCM_ERR_CACHEABLE_ACCESS |                \
        SCM_ERR_UNALIGNED_ACCESS | SCM_ERR_BYTE_ACCESS |                   \
        SCM_ERR_INTERNAL_ERROR | SCM_ERR_SMN_BLOCKING_ACCESS |             \
        SCM_ERR_CIPHERING | SCM_ERR_ZEROIZING | SCM_ERR_BUSY)
/******************************************************************************
 *
 *  Global / Static Variables
 *
 *****************************************************************************/

/*!
 * This is type void* so that a) it cannot directly be dereferenced,
 * and b) pointer arithmetic on it will function in a 'normal way' for
 * the offsets in scc_defines.h
 *
 * scc_base is the location in the iomap where the SCC's registers
 * (and memory) start.
 *
 * The referenced data is declared volatile so that the compiler will
 * not make any assumptions about the value of registers in the SCC,
 * and thus will always reload the register into CPU memory before
 * using it (i.e. wherever it is referenced in the driver).
 *
 * This value should only be referenced by the #SCC_READ_REGISTER and
 * #SCC_WRITE_REGISTER macros and their ilk.  All dereferences must be
 * 32 bits wide.
 */
static volatile void *scc_base;

/*! Array to hold function pointers registered by
    #scc_monitor_security_failure() and processed by
    #scc_perform_callbacks() */
static void (*scc_callbacks[SCC_CALLBACK_SIZE]) (void);

/*! Structure returned by #scc_get_configuration() */
static scc_config_t scc_configuration = {
	.driver_major_version = SCC_DRIVER_MAJOR_VERSION_1,
	.driver_minor_version = SCC_DRIVER_MINOR_VERSION_8,
	.scm_version = -1,
	.smn_version = -1,
	.block_size_bytes = -1,
	.black_ram_size_blocks = -1,
	.red_ram_size_blocks = -1
};

/*! Key Control Information.  Integrity is controlled by use of
    #scc_crypto_lock. */
static struct scc_key_slot scc_key_info[SCC_KEY_SLOTS];

/*! Internal flag to know whether SCC is in Failed state (and thus many
 *  registers are unavailable).  Once it goes failed, it never leaves it. */
static volatile enum scc_status scc_availability = SCC_STATUS_INITIAL;

/*! Flag to say whether interrupt handler has been registered for
 * SMN interrupt */
static int smn_irq_set = 0;

/*! Flag to say whether interrupt handler has been registered for
 * SCM interrupt */
static int scm_irq_set = 0;

/*! This lock protects the #scc_callbacks list as well as the @c
 * callbacks_performed flag in #scc_perform_callbacks.  Since the data this
 * protects may be read or written from either interrupt or base level, all
 * operations should use the irqsave/irqrestore or similar to make sure that
 * interrupts are inhibited when locking from base level.
 */
static spinlock_t scc_callbacks_lock = SPIN_LOCK_UNLOCKED;

/*!
 * Ownership of this lock prevents conflicts on the crypto operation in the SCC
 * and the integrity of the #scc_key_info.
 */
static spinlock_t scc_crypto_lock = SPIN_LOCK_UNLOCKED;

/*! Calculated once for quick reference to size of the unreserved space in one
 *  RAM in SCM.
 */
static uint32_t scc_memory_size_bytes;

/*! Calculated once for quick reference to size of SCM address space */
static uint32_t scm_highest_memory_address;

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,18))
#ifndef SCC_CLOCK_NOT_GATED
/*! Pointer to SCC's clock information.  Initialized during scc_init(). */
static struct clk *scc_clk = NULL;
#endif
#endif

/*! The lookup table for an 8-bit value.  Calculated once
 * by #scc_init_ccitt_crc().
 */
static uint16_t scc_crc_lookup_table[256];

/*! Fixed padding for appending to plaintext to fill out a block */
static uint8_t scc_block_padding[8] =
    { SCC_DRIVER_PAD_CHAR, 0, 0, 0, 0, 0, 0, 0 };

/******************************************************************************
 *
 *  Function Implementations - Externally Accessible
 *
 *****************************************************************************/

/*****************************************************************************/
/* fn scc_init()                                                             */
/*****************************************************************************/
/*!
 *  Initialize the driver at boot time or module load time.
 *
 *  Register with the kernel as the interrupt handler for the SCC interrupt
 *  line(s).
 *
 *  Map the SCC's register space into the driver's memory space.
 *
 *  Query the SCC for its configuration and status.  Save the configuration in
 *  #scc_configuration and save the status in #scc_availability.  Called by the
 *  kernel.
 *
 *  Do any locking/wait queue initialization which may be necessary.
 *
 *  The availability fuse may be checked, depending on platform.
 */
static int scc_init(void)
{
	uint32_t smn_status;
	int i;
	int return_value = -EIO;	/* assume error */
	if (scc_availability == SCC_STATUS_INITIAL) {

		/* Set this until we get an initial reading */
		scc_availability = SCC_STATUS_CHECKING;

		/* Initialize the constant for the CRC function */
		scc_init_ccitt_crc();

		/* initialize the callback table */
		for (i = 0; i < SCC_CALLBACK_SIZE; i++) {
			scc_callbacks[i] = 0;
		}

		/* Initialize key slots */
		for (i = 0; i < SCC_KEY_SLOTS; i++) {
			scc_key_info[i].offset = i * SCC_KEY_SLOT_SIZE;
			scc_key_info[i].status = 0;	/* unassigned */
		}

		/* Enable the SCC clock on platforms where it is gated */
#ifndef SCC_CLOCK_NOT_GATED

#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
		mxc_clks_enable(SCC_CLK);
#else

		scc_clk = clk_get(NULL, "scc_clk");
		if (scc_clk != ERR_PTR(ENOENT)) {
			clk_enable(scc_clk);
		}
#endif				/* LINUX_VERSION_CODE */

#endif				/* SCC_CLOCK_NOT_GATED */
		/* See whether there is an SCC available */
		if (0 && !SCC_ENABLED()) {
			os_printk(KERN_ERR
				  "SCC: Fuse for SCC is set to disabled.  Exiting.\n");
		} else {
			/* Map the SCC (SCM and SMN) memory on the internal bus into
			   kernel address space */
			scc_base = (void *)IO_ADDRESS(SCC_BASE);

			/* If that worked, we can try to use the SCC */
			if (scc_base == NULL) {
				os_printk(KERN_ERR
					  "SCC: Register mapping failed.  Exiting.\n");
			} else {
				/* Get SCM into 'clean' condition w/interrupts cleared &
				   disabled */
				SCC_WRITE_REGISTER(SCM_INTERRUPT_CTRL,
						   SCM_INTERRUPT_CTRL_CLEAR_INTERRUPT
						   |
						   SCM_INTERRUPT_CTRL_MASK_INTERRUPTS);

				/* Clear error status register (any write will do it) */
				SCC_WRITE_REGISTER(SCM_ERROR_STATUS, 0);

				/*
				 * There is an SCC.  Determine its current state.  Side effect
				 * is to populate scc_config and scc_availability
				 */
				smn_status = scc_grab_config_values();

				/* Try to set up interrupt handler(s) */
				if (scc_availability == SCC_STATUS_OK) {
					if (setup_interrupt_handling() != 0) {
						unsigned condition;
			/*!
			 * The error could be only that the SCM interrupt was
			 * not set up.  This interrupt is always masked, so
			 * that is not an issue.
			 *
			 * The SMN's interrupt may be shared on that line, it
			 * may be separate, or it may not be wired.  Do what
			 * is necessary to check its status.
			 *
			 * Although the driver is coded for possibility of not
			 * having SMN interrupt, the fact that there is one
			 * means it should be available and used.
			 */
#ifdef USE_SMN_INTERRUPT
						condition = !smn_irq_set;	/* Separate. Check SMN binding */
#elif !defined(NO_SMN_INTERRUPT)
						condition = !scm_irq_set;	/* Shared. Check SCM binding */
#else
						condition = FALSE;	/*  SMN not wired at all.  Ignore. */
#endif
						/* setup was not able to set up SMN interrupt */
						scc_availability =
						    SCC_STATUS_UNIMPLEMENTED;
					}	/* interrupt handling returned non-zero */
				}	/* availability is OK */
				if (scc_availability == SCC_STATUS_OK) {
					/* Get SMN into 'clean' condition w/interrupts cleared &
					   enabled */
					SCC_WRITE_REGISTER(SMN_COMMAND,
							   SMN_COMMAND_CLEAR_INTERRUPT
							   |
							   SMN_COMMAND_ENABLE_INTERRUPT);
				}
				/* availability is still OK */
			}	/* if scc_base != NULL */

		}		/* if SCC_ENABLED() */

		/*
		 * If status is SCC_STATUS_UNIMPLEMENTED or is still
		 * SCC_STATUS_CHECKING, could be leaving here with the driver partially
		 * initialized.  In either case, cleanup (which will mark the SCC as
		 * UNIMPLEMENTED).
		 */
		if (scc_availability == SCC_STATUS_CHECKING ||
		    scc_availability == SCC_STATUS_UNIMPLEMENTED) {
			scc_cleanup();
		} else {
			return_value = 0;	/* All is well */
		}
	}
	/* ! STATUS_INITIAL */
	pr_debug("SCC: Driver Status is %s\n",
		 (scc_availability == SCC_STATUS_INITIAL) ? "INITIAL" :
		 (scc_availability == SCC_STATUS_CHECKING) ? "CHECKING" :
		 (scc_availability ==
		  SCC_STATUS_UNIMPLEMENTED) ? "UNIMPLEMENTED"
		 : (scc_availability ==
		    SCC_STATUS_OK) ? "OK" : (scc_availability ==
					     SCC_STATUS_FAILED) ? "FAILED" :
		 "UNKNOWN");

	return return_value;
}				/* scc_init */

/*****************************************************************************/
/* fn scc_cleanup()                                                          */
/*****************************************************************************/
/*!
 * Perform cleanup before driver/module is unloaded by setting the machine
 * state close to what it was when the driver was loaded.  This function is
 * called when the kernel is shutting down or when this driver is being
 * unloaded.
 *
 * A driver like this should probably never be unloaded, especially if there
 * are other module relying upon the callback feature for monitoring the SCC
 * status.
 *
 * In any case, cleanup the callback table (by clearing out all of the
 * pointers).  Deregister the interrupt handler(s).  Unmap SCC registers.
 *
 */
static void scc_cleanup(void)
{
	int i;

	/* Mark the driver / SCC as unusable. */
	scc_availability = SCC_STATUS_UNIMPLEMENTED;

	/* Clear out callback table */
	for (i = 0; i < SCC_CALLBACK_SIZE; i++) {
		scc_callbacks[i] = 0;
	}

	/* If SCC has been mapped in, clean it up and unmap it */
	if (scc_base) {
		/* For the SCM, disable interrupts, zeroize RAMs.  Interrupt
		 * status will appear because zeroize will complete. */
		SCC_WRITE_REGISTER(SCM_INTERRUPT_CTRL,
				   SCM_INTERRUPT_CTRL_MASK_INTERRUPTS |
				   SCM_INTERRUPT_CTRL_ZEROIZE_MEMORY);

		/* For the SMN, clear and disable interrupts */
		SCC_WRITE_REGISTER(SMN_COMMAND, SMN_COMMAND_CLEAR_INTERRUPT);

		/* remove virtual mapping */
		iounmap((void *)scc_base);
	}

	/* Now that interrupts cannot occur, disassociate driver from the interrupt
	 * lines.
	 */

	/* Deregister SCM interrupt handler */
	if (scm_irq_set) {
		os_deregister_interrupt(INT_SCC_SCM);
	}

	/* Deregister SMN interrupt handler */
	if (smn_irq_set) {
#ifdef USE_SMN_INTERRUPT
		os_deregister_interrupt(INT_SCC_SMN);
#endif
	}
	pr_debug("SCC driver cleaned up.\n");

}				/* scc_cleanup */

/*****************************************************************************/
/* fn scc_get_configuration()                                                */
/*****************************************************************************/
scc_config_t *scc_get_configuration(void)
{
	/*
	 * If some other driver calls scc before the kernel does, make sure that
	 * this driver's initialization is performed.
	 */
	if (scc_availability == SCC_STATUS_INITIAL) {
		scc_init();
	}

  /*!
     * If there is no SCC, yet the driver exists, the value -1 will be in
     * the #scc_config_t fields for other than the driver versions.
     */
	return &scc_configuration;
}				/* scc_get_configuration */

/*****************************************************************************/
/* fn scc_zeroize_memories()                                                 */
/*****************************************************************************/
scc_return_t scc_zeroize_memories(void)
{
	scc_return_t return_status = SCC_RET_FAIL;
	uint32_t status;

	if (scc_availability == SCC_STATUS_INITIAL) {
		scc_init();
	}

	if (scc_availability == SCC_STATUS_OK) {
		unsigned long irq_flags;	/* for IRQ save/restore */

		/* Lock access to crypto memory of the SCC */
		spin_lock_irqsave(&scc_crypto_lock, irq_flags);

		/* Start the Zeroize by setting a bit in the SCM_INTERRUPT_CTRL
		 * register */
		SCC_WRITE_REGISTER(SCM_INTERRUPT_CTRL,
				   SCM_INTERRUPT_CTRL_MASK_INTERRUPTS
				   | SCM_INTERRUPT_CTRL_ZEROIZE_MEMORY);

		scc_wait_completion();

		/* Get any error info */
		status = SCC_READ_REGISTER(SCM_ERROR_STATUS);

		/* unlock the SCC */
		spin_unlock_irqrestore(&scc_crypto_lock, irq_flags);

		if (!(status & SCM_ERR_ZEROIZE_FAILED)) {
			return_status = SCC_RET_OK;
		} else {
			pr_debug
			    ("SCC: Zeroize failed.  SCM Error Status is 0x%08x\n",
			     status);
		}

		/* Clear out any status. */
		SCC_WRITE_REGISTER(SCM_INTERRUPT_CTRL,
				   SCM_INTERRUPT_CTRL_CLEAR_INTERRUPT
				   | SCM_INTERRUPT_CTRL_MASK_INTERRUPTS);

		/* and any error status */
		SCC_WRITE_REGISTER(SCM_ERROR_STATUS, 0);
	}

	return return_status;
}				/* scc_zeroize_memories */

/*****************************************************************************/
/* fn scc_crypt()                                                            */
/*****************************************************************************/
scc_return_t
scc_crypt(unsigned long count_in_bytes, const uint8_t * data_in,
	  const uint8_t * init_vector,
	  scc_enc_dec_t direction, scc_crypto_mode_t crypto_mode,
	  scc_verify_t check_mode, uint8_t * data_out,
	  unsigned long *count_out_bytes)

{
	scc_return_t return_code = SCC_RET_FAIL;

	if (scc_availability == SCC_STATUS_INITIAL) {
		scc_init();
	}

	(void)scc_update_state();	/* in case no interrupt line from SMN */

	/* make initial error checks */
	if (scc_availability != SCC_STATUS_OK
	    || count_in_bytes == 0
	    || data_in == 0
	    || data_out == 0
	    || (crypto_mode != SCC_CBC_MODE && crypto_mode != SCC_ECB_MODE)
	    || (crypto_mode == SCC_CBC_MODE && init_vector == NULL)
	    || (direction != SCC_ENCRYPT && direction != SCC_DECRYPT)
	    || (check_mode == SCC_VERIFY_MODE_NONE &&
		count_in_bytes % SCC_BLOCK_SIZE_BYTES() != 0)
	    || (direction == SCC_DECRYPT &&
		count_in_bytes % SCC_BLOCK_SIZE_BYTES() != 0)
	    || (check_mode != SCC_VERIFY_MODE_NONE &&
		check_mode != SCC_VERIFY_MODE_CCITT_CRC)) {
		pr_debug
		    ("SCC: scc_crypt() count_in_bytes_ok = %d; data_in_ok = %d;"
		     " data_out_ok = %d; iv_ok = %d\n", !(count_in_bytes == 0),
		     !(data_in == 0), !(data_out == 0),
		     !(crypto_mode == SCC_CBC_MODE && init_vector == NULL));
		pr_debug("SCC: scc_crypt() mode_ok=%d; direction_ok=%d;"
			 " size_ok=%d, check_mode_ok=%d\n",
			 !(crypto_mode != SCC_CBC_MODE
			   && crypto_mode != SCC_ECB_MODE),
			 !(direction != SCC_ENCRYPT
			   && direction != SCC_DECRYPT),
			 !((check_mode == SCC_VERIFY_MODE_NONE
			    && count_in_bytes % SCC_BLOCK_SIZE_BYTES() != 0)
			   || (direction == SCC_DECRYPT
			       && count_in_bytes % SCC_BLOCK_SIZE_BYTES() !=
			       0)), !(check_mode != SCC_VERIFY_MODE_NONE
				      && check_mode !=
				      SCC_VERIFY_MODE_CCITT_CRC));
		pr_debug("SCC: scc_crypt() detected bad argument\n");
	} else {
		/* Start settings for write to SCM_CONTROL register  */
		uint32_t scc_control = SCM_CONTROL_START_CIPHER;
		unsigned long irq_flags;	/* for IRQ save/restore */

		/* Lock access to crypto memory of the SCC */
		spin_lock_irqsave(&scc_crypto_lock, irq_flags);

		/* Special needs for CBC Mode */
		if (crypto_mode == SCC_CBC_MODE) {
			scc_control |= SCM_CBC_MODE;	/* change default of ECB */
			/* Put in Initial Context.  Vector registers are contiguous */
			copy_to_scc(init_vector, SCM_INIT_VECTOR_0,
				    SCC_BLOCK_SIZE_BYTES(), NULL);
		}

		/* Fill the RED_START register */
		SCC_WRITE_REGISTER(SCM_RED_START,
				   SCM_NON_RESERVED_OFFSET /
				   SCC_BLOCK_SIZE_BYTES());

		/* Fill the BLACK_START register */
		SCC_WRITE_REGISTER(SCM_BLACK_START,
				   SCM_NON_RESERVED_OFFSET /
				   SCC_BLOCK_SIZE_BYTES());

		if (direction == SCC_ENCRYPT) {
			/* Check for sufficient space in data_out */
			if (check_mode == SCC_VERIFY_MODE_NONE) {
				if (*count_out_bytes < count_in_bytes) {
					return_code =
					    SCC_RET_INSUFFICIENT_SPACE;
				}
			} else {	/* SCC_VERIFY_MODE_CCITT_CRC */
				/* Calculate extra bytes needed for crc (2) and block
				   padding */
				int padding_needed =
				    CRC_SIZE_BYTES + SCC_BLOCK_SIZE_BYTES() -
				    ((count_in_bytes + CRC_SIZE_BYTES)
				     % SCC_BLOCK_SIZE_BYTES());

				/* Verify space is available */
				if (*count_out_bytes <
				    count_in_bytes + padding_needed) {
					return_code =
					    SCC_RET_INSUFFICIENT_SPACE;
				}
			}
			/* If did not detect space error, do the encryption */
			if (return_code != SCC_RET_INSUFFICIENT_SPACE) {
				return_code =
				    scc_encrypt(count_in_bytes, data_in,
						scc_control, data_out,
						check_mode ==
						SCC_VERIFY_MODE_CCITT_CRC,
						count_out_bytes);
			}

		}
		/* direction == SCC_ENCRYPT */
		else {		/* SCC_DECRYPT */
			/* Check for sufficient space in data_out */
			if (check_mode == SCC_VERIFY_MODE_NONE) {
				if (*count_out_bytes < count_in_bytes) {
					return_code =
					    SCC_RET_INSUFFICIENT_SPACE;
				}
			} else {	/* SCC_VERIFY_MODE_CCITT_CRC */
				/* Do initial check.  Assume last block (of padding) and CRC
				 * will get stripped.  After decipher is done and padding is
				 * removed, will know exact value.
				 */
				int possible_size =
				    (int)count_in_bytes - CRC_SIZE_BYTES -
				    SCC_BLOCK_SIZE_BYTES();
				if ((int)*count_out_bytes < possible_size) {
					pr_debug
					    ("SCC: insufficient decrypt space %ld/%d.\n",
					     *count_out_bytes, possible_size);
					return_code =
					    SCC_RET_INSUFFICIENT_SPACE;
				}
			}

			/* If did not detect space error, do the decryption */
			if (return_code != SCC_RET_INSUFFICIENT_SPACE) {
				return_code =
				    scc_decrypt(count_in_bytes, data_in,
						scc_control, data_out,
						check_mode ==
						SCC_VERIFY_MODE_CCITT_CRC,
						count_out_bytes);
			}

		}		/* SCC_DECRYPT */

		/* unlock the SCC */
		spin_unlock_irqrestore(&scc_crypto_lock, irq_flags);

	}			/* else no initial error */

	return return_code;
}				/* scc_crypt */

/*****************************************************************************/
/* fn scc_set_sw_alarm()                                                     */
/*****************************************************************************/
void scc_set_sw_alarm(void)
{

	if (scc_availability == SCC_STATUS_INITIAL) {
		scc_init();
	}

	/* Update scc_availability based on current SMN status.  This might
	 * perform callbacks.
	 */
	(void)scc_update_state();

	/* if everything is OK, make it fail */
	if (scc_availability == SCC_STATUS_OK) {

		/* sound the alarm (and disable SMN interrupts */
		SCC_WRITE_REGISTER(SMN_COMMAND, SMN_COMMAND_SET_SOFTWARE_ALARM);

		scc_availability = SCC_STATUS_FAILED;	/* Remember what we've done */

		/* In case SMN interrupt is not available, tell the world */
		scc_perform_callbacks();
	}

	return;
}				/* scc_set_sw_alarm */

/*****************************************************************************/
/* fn scc_monitor_security_failure()                                         */
/*****************************************************************************/
scc_return_t scc_monitor_security_failure(void callback_func(void))
{
	int i;
	unsigned long irq_flags;	/* for IRQ save/restore */
	scc_return_t return_status = SCC_RET_TOO_MANY_FUNCTIONS;
	int function_stored = FALSE;

	if (scc_availability == SCC_STATUS_INITIAL) {
		scc_init();
	}

	/* Acquire lock of callbacks table.  Could be spin_lock_irq() if this
	 * routine were just called from base (not interrupt) level
	 */
	spin_lock_irqsave(&scc_callbacks_lock, irq_flags);

	/* Search through table looking for empty slot */
	for (i = 0; i < SCC_CALLBACK_SIZE; i++) {
		if (scc_callbacks[i] == callback_func) {
			if (function_stored) {
				/* Saved duplicate earlier.  Clear this later one. */
				scc_callbacks[i] = NULL;
			}
			/* Exactly one copy is now stored */
			return_status = SCC_RET_OK;
			break;
		} else if (scc_callbacks[i] == NULL && !function_stored) {
			/* Found open slot.  Save it and remember */
			scc_callbacks[i] = callback_func;
			return_status = SCC_RET_OK;
			function_stored = TRUE;
		}
	}

	/* Free the lock */
	spin_unlock_irqrestore(&scc_callbacks_lock, irq_flags);

	return return_status;
}				/* scc_monitor_security_failure */

/*****************************************************************************/
/* fn scc_stop_monitoring_security_failure()                                 */
/*****************************************************************************/
void scc_stop_monitoring_security_failure(void callback_func(void))
{
	unsigned long irq_flags;	/* for IRQ save/restore */
	int i;

	if (scc_availability == SCC_STATUS_INITIAL) {
		scc_init();
	}

	/* Acquire lock of callbacks table.  Could be spin_lock_irq() if this
	 * routine were just called from base (not interrupt) level
	 */
	spin_lock_irqsave(&scc_callbacks_lock, irq_flags);

	/* Search every entry of the table for this function */
	for (i = 0; i < SCC_CALLBACK_SIZE; i++) {
		if (scc_callbacks[i] == callback_func) {
			scc_callbacks[i] = NULL;	/* found instance - clear it out */
			break;
		}
	}

	/* Free the lock */
	spin_unlock_irqrestore(&scc_callbacks_lock, irq_flags);

	return;
}				/* scc_stop_monitoring_security_failure */

/*****************************************************************************/
/* fn scc_read_register()                                                    */
/*****************************************************************************/
scc_return_t scc_read_register(int register_offset, uint32_t * value)
{
	scc_return_t return_status = SCC_RET_FAIL;
	uint32_t smn_status;
	uint32_t scm_status;

	if (scc_availability == SCC_STATUS_INITIAL) {
		scc_init();
	}

	/* First layer of protection -- completely unaccessible SCC */
	if (scc_availability != SCC_STATUS_UNIMPLEMENTED) {

		/* Second layer -- that offset is valid */
		if (register_offset != SMN_BITBANK_DECREMENT &&	/* write only! */
		    check_register_offset(register_offset) == SCC_RET_OK) {

			/* Get current status / update local state */
			smn_status = scc_update_state();
			scm_status = SCC_READ_REGISTER(SCM_STATUS);

			/*
			 * Third layer - verify that the register being requested is
			 * available in the current state of the SCC.
			 */
			if ((return_status =
			     check_register_accessible(register_offset,
						       smn_status,
						       scm_status)) ==
			    SCC_RET_OK) {
				*value = SCC_READ_REGISTER(register_offset);
			}
		}
	}

	return return_status;
}				/* scc_read_register */

/*****************************************************************************/
/* fn scc_write_register()                                                   */
/*****************************************************************************/
scc_return_t scc_write_register(int register_offset, uint32_t value)
{
	scc_return_t return_status = SCC_RET_FAIL;
	uint32_t smn_status;
	uint32_t scm_status;

	if (scc_availability == SCC_STATUS_INITIAL) {
		scc_init();
	}

	/* First layer of protection -- completely unaccessible SCC */
	if (scc_availability != SCC_STATUS_UNIMPLEMENTED) {

		/* Second layer -- that offset is valid */
		if (!(register_offset == SCM_STATUS ||	/* These registers are */
		      register_offset == SCM_CONFIGURATION ||	/*  Read Only */
		      register_offset == SMN_BIT_COUNT ||
		      register_offset == SMN_TIMER) &&
		    check_register_offset(register_offset) == SCC_RET_OK) {

			/* Get current status / update local state */
			smn_status = scc_update_state();
			scm_status = SCC_READ_REGISTER(SCM_STATUS);

			/*
			 * Third layer - verify that the register being requested is
			 * available in the current state of the SCC.
			 */
			if (check_register_accessible
			    (register_offset, smn_status, scm_status) == 0) {
				SCC_WRITE_REGISTER(register_offset, value);
				return_status = SCC_RET_OK;
			}
		}
	}

	return return_status;
}				/* scc_write_register() */

/******************************************************************************
 *
 *  Function Implementations - Internal
 *
 *****************************************************************************/

/*****************************************************************************/
/* fn scc_irq()                                                              */
/*****************************************************************************/
/*!
 * This is the interrupt handler for the SCC.
 *
 * This function checks the SMN Status register to see whether it
 * generated the interrupt, then it checks the SCM Status register to
 * see whether it needs attention.
 *
 * If an SMN Interrupt is active, then the SCC state set to failure, and
 * #scc_perform_callbacks() is invoked to notify any interested parties.
 *
 * The SCM Interrupt should be masked, as this driver uses polling to determine
 * when the SCM has completed a crypto or zeroing operation.  Therefore, if the
 * interrupt is active, the driver will just clear the interrupt and (re)mask.
 *
 */
OS_DEV_ISR(scc_irq)
{
	uint32_t smn_status;
	uint32_t scm_status;
	int handled = 0;	/* assume interrupt isn't from SMN */
#if defined(USE_SMN_INTERRUPT)
	int smn_irq = INT_SCC_SMN;	/* SMN interrupt is on a line by itself */
#elif defined (NO_SMN_INTERRUPT)
	int smn_irq = -1;	/* not wired to CPU at all */
#else
	int smn_irq = INT_SCC_SCM;	/* SMN interrupt shares a line with SCM */
#endif

	/* Update current state... This will perform callbacks... */
	smn_status = scc_update_state();

	/* SMN is on its own interrupt line.  Verify the IRQ was triggered
	 * before clearing the interrupt and marking it handled.  */
	if ((os_dev_get_irq() == smn_irq) &&
	    (smn_status & SMN_STATUS_SMN_STATUS_IRQ)) {
		SCC_WRITE_REGISTER(SMN_COMMAND, SMN_COMMAND_CLEAR_INTERRUPT);
		handled++;	/* tell kernel that interrupt was handled */
	}

	/* Check on the health of the SCM */
	scm_status = SCC_READ_REGISTER(SCM_STATUS);

	/* The driver masks interrupts, so this should never happen. */
	if (os_dev_get_irq() == INT_SCC_SCM) {
		/* but if it does, try to prevent it in the future */
		SCC_WRITE_REGISTER(SCM_INTERRUPT_CTRL,
				   SCM_INTERRUPT_CTRL_CLEAR_INTERRUPT
				   | SCM_INTERRUPT_CTRL_MASK_INTERRUPTS);
		handled++;
	}

	/* Any non-zero value of handled lets kernel know we got something */
	return IRQ_RETVAL(handled);
}

/*****************************************************************************/
/* fn scc_perform_callbacks()                                                */
/*****************************************************************************/
/*! Perform callbacks registered by #scc_monitor_security_failure().
 *
 *  Make sure callbacks only happen once...  Since there may be some reason why
 *  the interrupt isn't generated, this routine could be called from base(task)
 *  level.
 *
 *  One at a time, go through #scc_callbacks[] and call any non-null pointers.
 */
static void scc_perform_callbacks(void)
{
	static int callbacks_performed = 0;
	unsigned long irq_flags;	/* for IRQ save/restore */
	int i;

	/* Acquire lock of callbacks table and callbacks_performed flag */
	spin_lock_irqsave(&scc_callbacks_lock, irq_flags);

	if (!callbacks_performed) {
		callbacks_performed = 1;

		/* Loop over all of the entries in the table */
		for (i = 0; i < SCC_CALLBACK_SIZE; i++) {
			/* If not null, ... */
			if (scc_callbacks[i]) {
				scc_callbacks[i] ();	/* invoke the callback routine */
			}
		}
	}

	spin_unlock_irqrestore(&scc_callbacks_lock, irq_flags);

	return;
}

/*****************************************************************************/
/* fn copy_to_scc()                                                          */
/*****************************************************************************/
/*!
 *  Move data from possibly unaligned source and realign for SCC, possibly
 *  while calculating CRC.
 *
 *  Multiple calls can be made to this routine (without intervening calls to
 *  #copy_from_scc(), as long as the sum total of bytes copied is a multiple of
 *  four (SCC native word size).
 *
 *  @param[in]     from        Location in memory
 *  @param[out]    to          Location in SCC
 *  @param[in]     count_bytes Number of bytes to copy
 *  @param[in,out] crc         Pointer to CRC.  Initial value must be
 *                             #CRC_CCITT_START if this is the start of
 *                             message.  Output is the resulting (maybe
 *                             partial) CRC.  If NULL, no crc is calculated.
 *
 * @return  Zero - success.  Non-zero - SCM status bits defining failure.
 */
static uint32_t
copy_to_scc(const uint8_t * from, uint32_t to, unsigned long count_bytes,
	    uint16_t * crc)
{
	int i;
	uint32_t scm_word;
	uint16_t current_crc = 0;	/* local copy for fast access */
	uint32_t status;

	pr_debug("SCC: copying %ld bytes to 0x%0x.\n", count_bytes, to);

	status = SCC_READ_REGISTER(SCM_ERROR_STATUS) & SCM_ACCESS_ERRORS;
	if (status != 0) {
		pr_debug
		    ("SCC copy_to_scc(): Error status detected (before copy):"
		     " %08x\n", status);
		/* clear out errors left behind by somebody else */
		SCC_WRITE_REGISTER(SCM_ERROR_STATUS, status);
	}

	if (crc) {
		current_crc = *crc;
	}

	/* Initialize value being built for SCM.  If we are starting 'clean',
	 * set it to zero.  Otherwise pick up partial value which had been saved
	 * earlier. */
	if (SCC_BYTE_OFFSET(to) == 0) {
		scm_word = 0;
	} else {
		scm_word = SCC_READ_REGISTER(SCC_WORD_PTR(to));	/* recover */
	}

	/* Now build up SCM words and write them out when each is full */
	for (i = 0; i < count_bytes; i++) {
		uint8_t byte = *from++;	/* value from plaintext */

#if defined(__BIG_ENDIAN) || defined(FSL_HAVE_DRYICE)
		scm_word = (scm_word << 8) | byte;	/* add byte to SCM word */
#else
		scm_word = (byte << 24) | (scm_word >> 8);
#endif
		/* now calculate CCITT CRC */
		if (crc) {
			CALC_CRC(byte, current_crc);
		}

		to++;		/* bump location in SCM */

		/* check for full word */
		if (SCC_BYTE_OFFSET(to) == 0) {
			SCC_WRITE_REGISTER((uint32_t) (to - 4), scm_word);	/* write it out */
		}
	}

	/* If at partial word after previous loop, save it in SCM memory for
	   next time. */
	if (SCC_BYTE_OFFSET(to) != 0) {
		SCC_WRITE_REGISTER(SCC_WORD_PTR(to), scm_word);	/* save */
	}

	/* Copy CRC back */
	if (crc) {
		*crc = current_crc;
	}

	status = SCC_READ_REGISTER(SCM_ERROR_STATUS) & SCM_ACCESS_ERRORS;
	if (status != 0) {
		pr_debug("SCC copy_to_scc(): Error status detected: %08x\n",
			 status);
		/* Clear any/all bits. */
		SCC_WRITE_REGISTER(SCM_ERROR_STATUS, status);
	}
	return status;
}

/*****************************************************************************/
/* fn copy_from_scc()                                                        */
/*****************************************************************************/
/*!
 *  Move data from aligned 32-bit source and place in (possibly unaligned)
 *  target, and maybe calculate CRC at the same time.
 *
 *  Multiple calls can be made to this routine (without intervening calls to
 *  #copy_to_scc(), as long as the sum total of bytes copied is be a multiple
 *  of four.
 *
 *  @param[in]     from        Location in SCC
 *  @param[out]    to          Location in memory
 *  @param[in]     count_bytes Number of bytes to copy
 *  @param[in,out] crc         Pointer to CRC.  Initial value must be
 *                             #CRC_CCITT_START if this is the start of
 *                             message.  Output is the resulting (maybe
 *                             partial) CRC.  If NULL, crc is not calculated.
 *
 * @return  Zero - success.  Non-zero - SCM status bits defining failure.
 */
static uint32_t
copy_from_scc(const uint32_t from, uint8_t * to, unsigned long count_bytes,
	      uint16_t * crc)
{
	uint32_t running_from = from;
	uint32_t scm_word;
	uint16_t current_crc = 0;	/* local copy for fast access */
	uint32_t status;
	pr_debug("SCC: copying %ld bytes from 0x%x.\n", count_bytes, from);
	status = SCC_READ_REGISTER(SCM_ERROR_STATUS) & SCM_ACCESS_ERRORS;
	if (status != 0) {
		pr_debug
		    ("SCC copy_from_scc(): Error status detected (before copy):"
		     " %08x\n", status);
		/* clear out errors left behind by somebody else */
		SCC_WRITE_REGISTER(SCM_ERROR_STATUS, status);
	}

	if (crc) {
		current_crc = *crc;
	}

	/* Read word which is sitting in SCM memory.  Ignore byte offset */
	scm_word = SCC_READ_REGISTER(SCC_WORD_PTR(running_from));

	/* If necessary, move the 'first' byte into place */
	if (SCC_BYTE_OFFSET(running_from) != 0) {
#if defined(__BIG_ENDIAN) || defined(FSL_HAVE_DRYICE)
		scm_word <<= 8 * SCC_BYTE_OFFSET(running_from);
#else
		scm_word >>= 8 * SCC_BYTE_OFFSET(running_from);
#endif
	}

	/* Now build up SCM words and write them out when each is full */
	while (count_bytes--) {
		uint8_t byte;	/* value from plaintext */

#if defined(__BIG_ENDIAN) || defined(FSL_HAVE_DRYICE)
		byte = (scm_word & 0xff000000) >> 24;	/* pull byte out of SCM word */
		scm_word <<= 8;	/* shift over to remove the just-pulled byte */
#else
		byte = (scm_word & 0xff);
		scm_word >>= 8;	/* shift over to remove the just-pulled byte */
#endif
		*to++ = byte;	/* send byte to memory */

		/* now calculate CRC */
		if (crc) {
			CALC_CRC(byte, current_crc);
		}

		running_from++;
		/* check for empty word */
		if (count_bytes && SCC_BYTE_OFFSET(running_from) == 0) {
			/* read one in */
			scm_word = SCC_READ_REGISTER((uint32_t) running_from);
		}
	}

	if (crc) {
		*crc = current_crc;
	}

	status = SCC_READ_REGISTER(SCM_ERROR_STATUS) & SCM_ACCESS_ERRORS;
	if (status != 0) {
		pr_debug("SCC copy_from_scc(): Error status detected: %08x\n",
			 status);
		/* Clear any/all bits. */
		SCC_WRITE_REGISTER(SCM_ERROR_STATUS, status);
	}

	return status;
}

/*****************************************************************************/
/* fn scc_strip_padding()                                                    */
/*****************************************************************************/
/*!
 *  Remove padding from plaintext.  Search backwards for #SCC_DRIVER_PAD_CHAR,
 *  verifying that each byte passed over is zero (0).  Maximum number of bytes
 *  to examine is 8.
 *
 *  @param[in]     from           Pointer to byte after end of message
 *  @param[out]    count_bytes_stripped Number of padding bytes removed by this
 *                                      function.
 *
 *  @return   #SCC_RET_OK if all goes, well, #SCC_RET_FAIL if padding was
 *            not present.
*/
static scc_return_t
scc_strip_padding(uint8_t * from, unsigned *count_bytes_stripped)
{
	int i = SCC_BLOCK_SIZE_BYTES();
	scc_return_t return_code = SCC_RET_VERIFICATION_FAILED;

	/*
	 * Search backwards looking for the magic marker.  If it isn't found,
	 * make sure that a 0 byte is there in its place.  Stop after the maximum
	 * amount of padding (8 bytes) has been searched);
	 */
	while (i-- > 0) {
		if (*--from == SCC_DRIVER_PAD_CHAR) {
			*count_bytes_stripped = SCC_BLOCK_SIZE_BYTES() - i;
			return_code = SCC_RET_OK;
			break;
		} else if (*from != 0) {	/* if not marker, check for 0 */
			pr_debug("SCC: Found non-zero interim pad: 0x%x\n",
				 *from);
			break;
		}
	}

	return return_code;
}

/*****************************************************************************/
/* fn scc_update_state()                                                     */
/*****************************************************************************/
/*!
 * Make certain SCC is still running.
 *
 * Side effect is to update #scc_availability and, if the state goes to failed,
 * run #scc_perform_callbacks().
 *
 * (If #SCC_BRINGUP is defined, bring SCC to secure state if it is found to be
 * in health check state)
 *
 * @return Current value of #SMN_STATUS register.
 */
static uint32_t scc_update_state(void)
{
	uint32_t smn_status_register = SMN_STATE_FAIL;
	int smn_state;

	/* if FAIL or UNIMPLEMENTED, don't bother */
	if (scc_availability == SCC_STATUS_CHECKING ||
	    scc_availability == SCC_STATUS_OK) {

		smn_status_register = SCC_READ_REGISTER(SMN_STATUS);
		smn_state = smn_status_register & SMN_STATUS_STATE_MASK;

#ifdef SCC_BRINGUP
		/* If in Health Check while booting, try to 'bringup' to Secure mode */
		if (scc_availability == SCC_STATUS_CHECKING &&
		    smn_state == SMN_STATE_HEALTH_CHECK) {
			/* Code up a simple algorithm for the ASC */
			SCC_WRITE_REGISTER(SMN_SEQUENCE_START, 0xaaaa);
			SCC_WRITE_REGISTER(SMN_SEQUENCE_END, 0x5555);
			SCC_WRITE_REGISTER(SMN_SEQUENCE_CHECK, 0x5555);
			/* State should be SECURE now */
			smn_status_register = SCC_READ_REGISTER(SMN_STATUS);
			smn_state = smn_status_register & SMN_STATUS_STATE_MASK;
		}
#endif

		/*
		 * State should be SECURE or NON_SECURE for operation of the part.  If
		 * FAIL, mark failed (i.e. limited access to registers).  Any other
		 * state, mark unimplemented, as the SCC is unuseable.
		 */
		if (smn_state == SMN_STATE_SECURE
		    || smn_state == SMN_STATE_NON_SECURE) {
			/* Healthy */
			scc_availability = SCC_STATUS_OK;
		} else if (smn_state == SMN_STATE_FAIL) {
			scc_availability = SCC_STATUS_FAILED;	/* uh oh - unhealthy */
			scc_perform_callbacks();
			os_printk(KERN_ERR "SCC: SCC went into FAILED mode\n");
		} else {
			/* START, ZEROIZE RAM, HEALTH CHECK, or unknown */
			scc_availability = SCC_STATUS_UNIMPLEMENTED;	/* unuseable */
			os_printk(KERN_ERR "SCC: SCC declared UNIMPLEMENTED\n");
		}
	}
	/* if availability is initial or ok */
	return smn_status_register;
}

/*****************************************************************************/
/* fn scc_init_ccitt_crc()                                                   */
/*****************************************************************************/
/*!
 * Populate the partial CRC lookup table.
 *
 * @return   none
 *
 */
static void scc_init_ccitt_crc(void)
{
	int dividend;		/* index for lookup table */
	uint16_t remainder;	/* partial value for a given dividend */
	int bit;		/* index into bits of a byte */

	/*
	 * Compute the remainder of each possible dividend.
	 */
	for (dividend = 0; dividend < 256; ++dividend) {
		/*
		 * Start with the dividend followed by zeros.
		 */
		remainder = dividend << (8);

		/*
		 * Perform modulo-2 division, a bit at a time.
		 */
		for (bit = 8; bit > 0; --bit) {
			/*
			 * Try to divide the current data bit.
			 */
			if (remainder & 0x8000) {
				remainder = (remainder << 1) ^ CRC_POLYNOMIAL;
			} else {
				remainder = (remainder << 1);
			}
		}

		/*
		 * Store the result into the table.
		 */
		scc_crc_lookup_table[dividend] = remainder;
	}

}				/* scc_init_ccitt_crc() */

/*****************************************************************************/
/* fn grab_config_values()                                                   */
/*****************************************************************************/
/*!
 * grab_config_values() will read the SCM Configuration and SMN Status
 * registers and store away version and size information for later use.
 *
 * @return The current value of the SMN Status register.
 */
static uint32_t scc_grab_config_values(void)
{
	uint32_t config_register;
	uint32_t smn_status_register = SMN_STATE_FAIL;

	if (scc_availability != SCC_STATUS_UNIMPLEMENTED) {
		/* access the SCC - these are 'safe' registers */
		config_register = SCC_READ_REGISTER(SCM_CONFIGURATION);
		pr_debug("SCC Driver: SCM config is 0x%08x\n", config_register);

		/* Get SMN status and update scc_availability */
		smn_status_register = scc_update_state();
		pr_debug("SCC Driver: SMN status is 0x%08x\n",
			 smn_status_register);

		/* save sizes and versions information for later use */
		scc_configuration.block_size_bytes = (config_register &
						      SCM_CFG_BLOCK_SIZE_MASK)
		    >> SCM_CFG_BLOCK_SIZE_SHIFT;

		scc_configuration.red_ram_size_blocks = (config_register &
							 SCM_CFG_RED_SIZE_MASK)
		    >> SCM_CFG_RED_SIZE_SHIFT;

		scc_configuration.black_ram_size_blocks = (config_register &
							   SCM_CFG_BLACK_SIZE_MASK)
		    >> SCM_CFG_BLACK_SIZE_SHIFT;

		scc_configuration.scm_version = (config_register
						 & SCM_CFG_VERSION_ID_MASK)
		    >> SCM_CFG_VERSION_ID_SHIFT;

		scc_configuration.smn_version = (smn_status_register &
						 SMN_STATUS_VERSION_ID_MASK)
		    >> SMN_STATUS_VERSION_ID_SHIFT;

		if (scc_configuration.scm_version != SCM_VERSION_1) {
			scc_availability = SCC_STATUS_UNIMPLEMENTED;	/* Unknown version */
		}

		scc_memory_size_bytes = (SCC_BLOCK_SIZE_BYTES() *
					 scc_configuration.
					 black_ram_size_blocks)
		    - SCM_NON_RESERVED_OFFSET;

		/* This last is for driver consumption only */
		scm_highest_memory_address = SCM_BLACK_MEMORY +
		    (SCC_BLOCK_SIZE_BYTES() *
		     scc_configuration.black_ram_size_blocks);
	}

	return smn_status_register;
}				/* grab_config_values */

/*****************************************************************************/
/* fn setup_interrupt_handling()                                             */
/*****************************************************************************/
/*!
 * Register the SCM and SMN interrupt handlers.
 *
 * Called from #scc_init()
 *
 * @return 0 on success
 */
static int setup_interrupt_handling(void)
{
	int smn_error_code = -1;
	int scm_error_code = -1;

	/* Disnable SCM interrupts */
	SCC_WRITE_REGISTER(SCM_INTERRUPT_CTRL,
			   SCM_INTERRUPT_CTRL_CLEAR_INTERRUPT
			   | SCM_INTERRUPT_CTRL_MASK_INTERRUPTS);

#ifdef USE_SMN_INTERRUPT
	/* Install interrupt service routine for SMN. */
	smn_error_code = os_register_interrupt(SCC_DRIVER_NAME,
					       INT_SCC_SMN, scc_irq);
	if (smn_error_code != 0) {
		os_printk
		    ("SCC Driver: Error installing SMN Interrupt Handler: %d\n",
		     smn_error_code);
	} else {
		smn_irq_set = 1;	/* remember this for cleanup */
		/* Enable SMN interrupts */
		SCC_WRITE_REGISTER(SMN_COMMAND,
				   SMN_COMMAND_CLEAR_INTERRUPT |
				   SMN_COMMAND_ENABLE_INTERRUPT);
	}
#else
	smn_error_code = 0;	/* no problems... will handle later */
#endif

	/*
	 * Install interrupt service routine for SCM (or both together).
	 */
	scm_error_code = os_register_interrupt(SCC_DRIVER_NAME,
					       INT_SCC_SCM, scc_irq);
	if (scm_error_code != 0) {
#ifndef MXC
		os_printk
		    ("SCC Driver: Error installing SCM Interrupt Handler: %d\n",
		     scm_error_code);
#else
		os_printk
		    ("SCC Driver: Error installing SCC Interrupt Handler: %d\n",
		     scm_error_code);
#endif
	} else {
		scm_irq_set = 1;	/* remember this for cleanup */
#if defined(USE_SMN_INTERRUPT) && !defined(NO_SMN_INTERRUPT)
		/* Enable SMN interrupts */
		SCC_WRITE_REGISTER(SMN_COMMAND,
				   SMN_COMMAND_CLEAR_INTERRUPT |
				   SMN_COMMAND_ENABLE_INTERRUPT);
#endif
	}

	/* Return an error if one was encountered */
	return scm_error_code ? scm_error_code : smn_error_code;
}				/* setup_interrupt_handling */

/*****************************************************************************/
/* fn scc_do_crypto()                                                        */
/*****************************************************************************/
/*! Have the SCM perform the crypto function.
 *
 * Set up length register, and the store @c scm_control into control register
 * to kick off the operation.  Wait for completion, gather status, clear
 * interrupt / status.
 *
 * @param byte_count  number of bytes to perform in this operation
 * @param scm_control Bit values to be set in @c SCM_CONTROL register
 *
 * @return 0 on success, value of #SCM_ERROR_STATUS on failure
 */
static uint32_t scc_do_crypto(int byte_count, uint32_t scm_control)
{
	int block_count = byte_count / SCC_BLOCK_SIZE_BYTES();
	uint32_t crypto_status;

	/* clear any outstanding flags */
	SCC_WRITE_REGISTER(SCM_INTERRUPT_CTRL,
			   SCM_INTERRUPT_CTRL_CLEAR_INTERRUPT
			   | SCM_INTERRUPT_CTRL_MASK_INTERRUPTS);

	/* In length register, 0 means 1, etc. */
	SCC_WRITE_REGISTER(SCM_LENGTH, block_count - 1);

	/* set modes and kick off the operation */
	SCC_WRITE_REGISTER(SCM_CONTROL, scm_control);

	scc_wait_completion();

	/* Mask for done and error bits */
	crypto_status = SCC_READ_REGISTER(SCM_STATUS)
	    & (SCM_STATUS_CIPHERING_DONE
	       | SCM_STATUS_LENGTH_ERROR | SCM_STATUS_INTERNAL_ERROR);

	/* Only done bit should be on */
	if (crypto_status != SCM_STATUS_CIPHERING_DONE) {
		/* Replace with error status instead */
		crypto_status = SCC_READ_REGISTER(SCM_ERROR_STATUS);
		pr_debug("SCM Failure: 0x%x\n", crypto_status);
		if (crypto_status == 0) {
			/* That came up 0.  Turn on arbitrary bit to signal error. */
			crypto_status = SCM_ERR_INTERNAL_ERROR;
		}
	} else {
		crypto_status = 0;
	}

	pr_debug("SCC: Done waiting.\n");

	/* Clear out any status. */
	SCC_WRITE_REGISTER(SCM_INTERRUPT_CTRL,
			   SCM_INTERRUPT_CTRL_CLEAR_INTERRUPT
			   | SCM_INTERRUPT_CTRL_MASK_INTERRUPTS);

	/* And clear any error status */
	SCC_WRITE_REGISTER(SCM_ERROR_STATUS, 0);

	return crypto_status;
}

/*****************************************************************************/
/* fn scc_encrypt()                                                          */
/*****************************************************************************/
/*!
 * Perform an encryption on the input.  If @c verify_crc is true, a CRC must be
 * calculated on the plaintext, and appended, with padding, before computing
 * the ciphertext.
 *
 * @param[in]     count_in_bytes  Count of bytes of plaintext
 * @param[in]     data_in         Pointer to the plaintext
 * @param[in]     scm_control     Bit values for the SCM_CONTROL register
 * @param[in,out] data_out        Pointer for storing ciphertext
 * @param[in]     add_crc         Flag for computing CRC - 0 no, else yes
 * @param[in,out] count_out_bytes Number of bytes available at @c data_out
 */
static scc_return_t
scc_encrypt(uint32_t count_in_bytes, const uint8_t * data_in,
	    uint32_t scm_control,
	    uint8_t * data_out, int add_crc, unsigned long *count_out_bytes)

{
	scc_return_t return_code = SCC_RET_FAIL;	/* initialised for failure */
	uint32_t input_bytes_left = count_in_bytes;	/* local copy */
	uint32_t output_bytes_copied = 0;	/* running total */
	uint32_t bytes_to_process;	/* multi-purpose byte counter */
	uint16_t crc = CRC_CCITT_START;	/* running CRC value */
	crc_t *crc_ptr = NULL;	/* Reset if CRC required */
	/* byte address  into SCM RAM */
	uint32_t scm_location = SCM_RED_MEMORY + SCM_NON_RESERVED_OFFSET;
	/* free RED RAM */
	uint32_t scm_bytes_remaining = scc_memory_size_bytes;
	/* CRC+padding holder */
	uint8_t padding_buffer[PADDING_BUFFER_MAX_BYTES];
	unsigned padding_byte_count = 0;	/* Reset if padding required */
	uint32_t scm_error_status = 0;	/* No known SCM error initially */
	uint32_t i; 	/* Counter for clear data loop */
	uint32_t dirty_bytes;	/* Number of bytes of memory used
					   temporarily during encryption,
					   which need to be wiped after
					   completion of the operation. */

	/* Set location of CRC and prepare padding bytes if required */
	if (add_crc != 0) {
		crc_ptr = &crc;
		padding_byte_count = SCC_BLOCK_SIZE_BYTES()
		    - (count_in_bytes +
		       CRC_SIZE_BYTES) % SCC_BLOCK_SIZE_BYTES();
		memcpy(padding_buffer + CRC_SIZE_BYTES, scc_block_padding,
		       padding_byte_count);
	}

	/* Process remaining input or padding data */
	while (input_bytes_left > 0) {

		/* Determine how much work to do this pass */
		bytes_to_process = (input_bytes_left > scm_bytes_remaining) ?
		    scm_bytes_remaining : input_bytes_left;

		/* Copy plaintext into SCM RAM, calculating CRC if required */
		copy_to_scc(data_in, scm_location, bytes_to_process, crc_ptr);

		/* Adjust pointers & counters */
		input_bytes_left -= bytes_to_process;
		data_in += bytes_to_process;
		scm_location += bytes_to_process;
		scm_bytes_remaining -= bytes_to_process;

		/* Add CRC and padding after the last byte is copied if required */
		if ((input_bytes_left == 0) && (crc_ptr != NULL)) {

			/* Copy CRC into padding buffer MSB first */
			padding_buffer[0] = (crc >> 8) & 0xFF;
			padding_buffer[1] = crc & 0xFF;

			/* Reset pointers and counter */
			data_in = padding_buffer;
			input_bytes_left = CRC_SIZE_BYTES + padding_byte_count;
			crc_ptr = NULL;	/* CRC no longer required */

			/* Go round loop again to copy CRC and padding to SCM */
			continue;
		}

		/* if no input and crc_ptr */
		/* Now have block-sized plaintext in SCM to encrypt */
		/* Encrypt plaintext; exit loop on error */
		bytes_to_process = scm_location -
		    (SCM_RED_MEMORY + SCM_NON_RESERVED_OFFSET);

		if (output_bytes_copied + bytes_to_process > *count_out_bytes) {
			return_code = SCC_RET_INSUFFICIENT_SPACE;
			scm_error_status = -1;	/* error signal */
			pr_debug
			    ("SCC: too many ciphertext bytes for space available\n");
			break;
		}
		pr_debug("SCC: Starting encryption. %x for %d bytes (%p/%p)\n",
			 scm_control, bytes_to_process,
			 (void *)SCC_READ_REGISTER(SCM_RED_START),
			 (void *)SCC_READ_REGISTER(SCM_BLACK_START));
		scm_error_status = scc_do_crypto(bytes_to_process, scm_control);
		if (scm_error_status != 0) {
			break;
		}

		/* Copy out ciphertext */
		copy_from_scc(SCM_BLACK_MEMORY + SCM_NON_RESERVED_OFFSET,
			      data_out, bytes_to_process, NULL);

		/* Adjust pointers and counters for next loop */
		output_bytes_copied += bytes_to_process;
		data_out += bytes_to_process;
		scm_location = SCM_RED_MEMORY + SCM_NON_RESERVED_OFFSET;
		scm_bytes_remaining = scc_memory_size_bytes;

	}			/* input_bytes_left > 0 */
	/* Clear all red and black memory used during ephemeral encryption */
	dirty_bytes = (count_in_bytes > scc_memory_size_bytes) ?
			scc_memory_size_bytes : count_in_bytes;

	for (i = 0; i < dirty_bytes; i += 4) {
		SCC_WRITE_REGISTER(SCM_RED_MEMORY + SCM_NON_RESERVED_OFFSET + i,
					   0);
		SCC_WRITE_REGISTER(SCM_BLACK_MEMORY + SCM_NON_RESERVED_OFFSET +
					   i, 0);
	}

	/* If no SCM error, set OK status and save ouput byte count */
	if (scm_error_status == 0) {
		return_code = SCC_RET_OK;
		*count_out_bytes = output_bytes_copied;
	}

	return return_code;
}				/* scc_encrypt */

/*****************************************************************************/
/* fn scc_decrypt()                                                          */
/*****************************************************************************/
/*!
 * Perform a decryption on the input.  If @c verify_crc is true, the last block
 * (maybe the two last blocks) is special - it should contain a CRC and
 * padding.  These must be stripped and verified.
 *
 * @param[in]     count_in_bytes  Count of bytes of ciphertext
 * @param[in]     data_in         Pointer to the ciphertext
 * @param[in]     scm_control     Bit values for the SCM_CONTROL register
 * @param[in,out] data_out        Pointer for storing plaintext
 * @param[in]     verify_crc      Flag for running CRC - 0 no, else yes
 * @param[in,out] count_out_bytes Number of bytes available at @c data_out

 */
static scc_return_t
scc_decrypt(uint32_t count_in_bytes, const uint8_t * data_in,
	    uint32_t scm_control,
	    uint8_t * data_out, int verify_crc, unsigned long *count_out_bytes)
{
	scc_return_t return_code = SCC_RET_FAIL;
	uint32_t bytes_left = count_in_bytes;	/* local copy */
	uint32_t bytes_copied = 0;	/* running total of bytes going to user */
	uint32_t bytes_to_copy = 0;	/* Number in this encryption 'chunk' */
	uint16_t crc = CRC_CCITT_START;	/* running CRC value */
	/* next target for	ctext */
	uint32_t scm_location = SCM_BLACK_MEMORY + SCM_NON_RESERVED_OFFSET;
	unsigned padding_byte_count;	/* number of bytes of padding stripped */
	uint8_t last_two_blocks[2 * SCC_BLOCK_SIZE_BYTES()];	/* temp */
	uint32_t scm_error_status = 0;	/* register value */
	uint32_t i; 	/* Counter for clear data loop */
	uint32_t dirty_bytes;	/* Number of bytes of memory used
					   temporarily during decryption,
					   which need to be wiped after
					   completion of the operation. */

	scm_control |= SCM_DECRYPT_MODE;

	if (verify_crc) {
		/* Save last two blocks (if there are at least two) of ciphertext for
		   special treatment. */
		bytes_left -= SCC_BLOCK_SIZE_BYTES();
		if (bytes_left >= SCC_BLOCK_SIZE_BYTES()) {
			bytes_left -= SCC_BLOCK_SIZE_BYTES();
		}
	}

	/* Copy ciphertext into SCM BLACK memory */
	while (bytes_left && scm_error_status == 0) {

		/* Determine how much work to do this pass */
		if (bytes_left > (scc_memory_size_bytes)) {
			bytes_to_copy = scc_memory_size_bytes;
		} else {
			bytes_to_copy = bytes_left;
		}

		if (bytes_copied + bytes_to_copy > *count_out_bytes) {
			scm_error_status = -1;
			break;
		}
		copy_to_scc(data_in, scm_location, bytes_to_copy, NULL);
		data_in += bytes_to_copy;	/* move pointer */

		pr_debug("SCC: Starting decryption of %d bytes.\n",
			 bytes_to_copy);

		/*  Do the work, wait for completion */
		scm_error_status = scc_do_crypto(bytes_to_copy, scm_control);

		copy_from_scc(SCM_RED_MEMORY + SCM_NON_RESERVED_OFFSET,
			      data_out, bytes_to_copy, &crc);
		bytes_copied += bytes_to_copy;
		data_out += bytes_to_copy;
		scm_location = SCM_BLACK_MEMORY + SCM_NON_RESERVED_OFFSET;

		/* Do housekeeping */
		bytes_left -= bytes_to_copy;

	}			/* while bytes_left */

	/* At this point, either the process is finished, or this is verify mode */

	if (scm_error_status == 0) {
		if (!verify_crc) {
			*count_out_bytes = bytes_copied;
			return_code = SCC_RET_OK;
		} else {
			/* Verify mode.  There are one or two blocks of unprocessed
			 * ciphertext sitting at data_in.  They need to be moved to the
			 * SCM, decrypted, searched to remove padding, then the plaintext
			 * copied back to the user (while calculating CRC, of course).
			 */

			/* Calculate ciphertext still left */
			bytes_to_copy = count_in_bytes - bytes_copied;

			copy_to_scc(data_in, scm_location, bytes_to_copy, NULL);
			data_in += bytes_to_copy;	/* move pointer */

			pr_debug("SCC: Finishing decryption (%d bytes).\n",
				 bytes_to_copy);

			/*  Do the work, wait for completion */
			scm_error_status =
			    scc_do_crypto(bytes_to_copy, scm_control);

			if (scm_error_status == 0) {
				/* Copy decrypted data back from SCM RED memory */
				copy_from_scc(SCM_RED_MEMORY +
					      SCM_NON_RESERVED_OFFSET,
					      last_two_blocks, bytes_to_copy,
					      NULL);

				/* (Plaintext) + crc + padding should be in temp buffer */
				if (scc_strip_padding
				    (last_two_blocks + bytes_to_copy,
				     &padding_byte_count) == SCC_RET_OK) {
					bytes_to_copy -=
					    padding_byte_count + CRC_SIZE_BYTES;

					/* verify enough space in user buffer */
					if (bytes_copied + bytes_to_copy <=
					    *count_out_bytes) {
						int i = 0;

						/* Move out last plaintext and calc CRC */
						while (i < bytes_to_copy) {
							CALC_CRC(last_two_blocks
								 [i], crc);
							*data_out++ =
							    last_two_blocks
							    [i++];
							bytes_copied++;
						}

						/* Verify the CRC by running over itself */
						CALC_CRC(last_two_blocks
							 [bytes_to_copy], crc);
						CALC_CRC(last_two_blocks
							 [bytes_to_copy + 1],
							 crc);
						if (crc == 0) {
							/* Just fine ! */
							*count_out_bytes =
							    bytes_copied;
							return_code =
							    SCC_RET_OK;
						} else {
							return_code =
							    SCC_RET_VERIFICATION_FAILED;
							pr_debug
							    ("SCC:  CRC values are %04x, %02x%02x\n",
							     crc,
							     last_two_blocks
							     [bytes_to_copy],
							     last_two_blocks
							     [bytes_to_copy +
							      1]);
						}
					}	/* if space available */
				} /* if scc_strip_padding... */
				else {
					/* bad padding means bad verification */
					return_code =
					    SCC_RET_VERIFICATION_FAILED;
				}
			}
			/* scm_error_status == 0 */
		}		/* verify_crc */
	}

	/* scm_error_status == 0 */
	/* Clear all red and black memory used during ephemeral decryption */
	dirty_bytes = (count_in_bytes > scc_memory_size_bytes) ?
	    scc_memory_size_bytes : count_in_bytes;

	for (i = 0; i < dirty_bytes; i += 4) {
		SCC_WRITE_REGISTER(SCM_RED_MEMORY + SCM_NON_RESERVED_OFFSET + i,
				   0);
		SCC_WRITE_REGISTER(SCM_BLACK_MEMORY + SCM_NON_RESERVED_OFFSET +
				   i, 0);
	}
	return return_code;
}				/* scc_decrypt */

/*****************************************************************************/
/* fn scc_alloc_slot()                                                       */
/*****************************************************************************/
/*!
 * Allocate a key slot to fit the requested size.
 *
 * @param value_size_bytes   Size of the key or other secure data
 * @param owner_id           Value to tie owner to slot
 * @param[out] slot          Handle to access or deallocate slot
 *
 * @return SCC_RET_OK on success, SCC_RET_INSUFFICIENT_SPACE if not slots of
 *         requested size are available.
 */
scc_return_t
scc_alloc_slot(uint32_t value_size_bytes, uint64_t owner_id, uint32_t * slot)
{
	scc_return_t status = SCC_RET_FAIL;
	unsigned long irq_flags;

	if (scc_availability != SCC_STATUS_OK) {
		goto out;
	}
	/* ACQUIRE LOCK to prevent others from using SCC crypto */
	spin_lock_irqsave(&scc_crypto_lock, irq_flags);

	pr_debug("SCC: Allocating %d-byte slot for 0x%Lx\n",
		 value_size_bytes, owner_id);

	if ((value_size_bytes != 0) && (value_size_bytes <= SCC_MAX_KEY_SIZE)) {
		int i;

		for (i = 0; i < SCC_KEY_SLOTS; i++) {
			if (scc_key_info[i].status == 0) {
				scc_key_info[i].owner_id = owner_id;
				scc_key_info[i].length = value_size_bytes;
				scc_key_info[i].status = 1;	/* assigned! */
				*slot = i;
				status = SCC_RET_OK;
				break;	/* exit 'for' loop */
			}
		}

		if (status != SCC_RET_OK) {
			status = SCC_RET_INSUFFICIENT_SPACE;
		} else {
			pr_debug("SCC: Allocated slot %d (0x%Lx)\n", i,
				 owner_id);
		}
	}

	spin_unlock_irqrestore(&scc_crypto_lock, irq_flags);

      out:
	return status;
}

/*****************************************************************************/
/* fn verify_slot_access()                                                   */
/*****************************************************************************/
inline static scc_return_t
verify_slot_access(uint64_t owner_id, uint32_t slot, uint32_t access_len)
{
	scc_return_t status = SCC_RET_FAIL;
	if (scc_availability != SCC_STATUS_OK) {
		goto out;
	}

	if ((slot < SCC_KEY_SLOTS) && scc_key_info[slot].status
	    && (scc_key_info[slot].owner_id == owner_id)
	    && (access_len <= SCC_KEY_SLOT_SIZE)) {
		status = SCC_RET_OK;
		pr_debug("SCC: Verify on slot %d succeeded\n", slot);
	} else {
		if (slot >= SCC_KEY_SLOTS) {
			pr_debug("SCC: Verify on bad slot (%d) failed\n", slot);
		} else if (scc_key_info[slot].status) {
			pr_debug("SCC: Verify on slot %d failed (%Lx) \n", slot,
				 owner_id);
		} else {
			pr_debug
			    ("SCC: Verify on slot %d failed: not allocated\n",
			     slot);
		}
	}

      out:
	return status;
}

scc_return_t
scc_verify_slot_access(uint64_t owner_id, uint32_t slot, uint32_t access_len)
{
	return verify_slot_access(owner_id, slot, access_len);
}

/*****************************************************************************/
/* fn scc_dealloc_slot()                                                     */
/*****************************************************************************/
scc_return_t scc_dealloc_slot(uint64_t owner_id, uint32_t slot)
{
	scc_return_t status;
	unsigned long irq_flags;
	int i;

	/* ACQUIRE LOCK to prevent others from using SCC crypto */
	spin_lock_irqsave(&scc_crypto_lock, irq_flags);

	status = verify_slot_access(owner_id, slot, 0);

	if (status == SCC_RET_OK) {
		scc_key_info[slot].owner_id = 0;
		scc_key_info[slot].status = 0;	/* unassign */

		/* clear old info */
		for (i = 0; i < SCC_KEY_SLOT_SIZE; i += 4) {
			SCC_WRITE_REGISTER(SCM_RED_MEMORY +
					   scc_key_info[slot].offset + i, 0);
			SCC_WRITE_REGISTER(SCM_BLACK_MEMORY +
					   scc_key_info[slot].offset + i, 0);
		}
		pr_debug("SCC: Deallocated slot %d\n", slot);
	}

	spin_unlock_irqrestore(&scc_crypto_lock, irq_flags);

	return status;
}

/*****************************************************************************/
/* fn scc_load_slot()                                                        */
/*****************************************************************************/
/*!
 * Load a value into a slot.
 *
 * @param owner_id      Value of owner of slot
 * @param slot          Handle of slot
 * @param key_data      Data to load into the slot
 * @param key_length    Length, in bytes, of @c key_data to copy to SCC.
 *
 * @return SCC_RET_OK on success.  SCC_RET_FAIL will be returned if slot
 * specified cannot be accessed for any reason, or SCC_RET_INSUFFICIENT_SPACE
 * if @c key_length exceeds the size of the slot.
 */
scc_return_t
scc_load_slot(uint64_t owner_id, uint32_t slot, const uint8_t * key_data,
	      uint32_t key_length)
{
	scc_return_t status;
	unsigned long irq_flags;

	/* ACQUIRE LOCK to prevent others from using SCC crypto */
	spin_lock_irqsave(&scc_crypto_lock, irq_flags);

	status = verify_slot_access(owner_id, slot, key_length);
	if ((status == SCC_RET_OK) && (key_data != NULL)) {
		status = SCC_RET_FAIL;	/* reset expectations */

		if (key_length > SCC_KEY_SLOT_SIZE) {
			pr_debug
			    ("SCC: scc_load_slot() rejecting key of %d bytes.\n",
			     key_length);
			status = SCC_RET_INSUFFICIENT_SPACE;
		} else {
			if (copy_to_scc(key_data,
					SCM_RED_MEMORY +
					scc_key_info[slot].offset, key_length,
					NULL)) {
				pr_debug("SCC: RED copy_to_scc() failed for"
					 " scc_load_slot()\n");
			} else {
				if ((key_length % 4) != 0) {
					uint32_t zeros = 0;

					/* zero-pad to get remainder bytes in correct place */
					copy_to_scc((uint8_t *) & zeros,
						    SCM_RED_MEMORY
						    +
						    scc_key_info[slot].offset +
						    key_length,
						    4 - (key_length % 4), NULL);
				}
				status = SCC_RET_OK;
			}
		}
	}

	spin_unlock_irqrestore(&scc_crypto_lock, irq_flags);

	return status;
}				/* scc_load_slot */

scc_return_t
scc_read_slot(uint64_t owner_id, uint32_t slot, uint32_t key_length,
	      uint8_t * key_data)
{
	scc_return_t status;
	unsigned long irq_flags;

	/* ACQUIRE LOCK to prevent others from using SCC crypto */
	spin_lock_irqsave(&scc_crypto_lock, irq_flags);

	status = verify_slot_access(owner_id, slot, key_length);
	if ((status == SCC_RET_OK) && (key_data != NULL)) {
		status = SCC_RET_FAIL;	/* reset expectations */

		if (key_length > SCC_KEY_SLOT_SIZE) {
			pr_debug
			    ("SCC: scc_read_slot() rejecting key of %d bytes.\n",
			     key_length);
			status = SCC_RET_INSUFFICIENT_SPACE;
		} else {
			if (copy_from_scc
			    (SCM_RED_MEMORY + scc_key_info[slot].offset,
			     key_data, key_length, NULL)) {
				pr_debug("SCC: RED copy_from_scc() failed for"
					 " scc_read_slot()\n");
			} else {
				status = SCC_RET_OK;
			}
		}
	}

	spin_unlock_irqrestore(&scc_crypto_lock, irq_flags);

	return status;
}				/* scc_read_slot */

/*****************************************************************************/
/* fn scc_encrypt_slot()                                                     */
/*****************************************************************************/
/*!
 * Encrypt the key data stored in a slot.
 *
 * @param owner_id      Value of owner of slot
 * @param slot          Handle of slot
 * @param length        Length, in bytes, of @c black_data
 * @param black_data    Location to store result of encrypting RED data in slot
 *
 * @return SCC_RET_OK on success, SCC_RET_FAIL if slot specified cannot be
 *         accessed for any reason.
 */
scc_return_t scc_encrypt_slot(uint64_t owner_id, uint32_t slot,
			      uint32_t length, uint8_t * black_data)
{
	unsigned long irq_flags;
	scc_return_t status;
	uint32_t crypto_status;
	uint32_t slot_offset =
	    scc_key_info[slot].offset / SCC_BLOCK_SIZE_BYTES();

	/* ACQUIRE LOCK to prevent others from using crypto or releasing slot */
	spin_lock_irqsave(&scc_crypto_lock, irq_flags);

	status = verify_slot_access(owner_id, slot, length);
	if (status == SCC_RET_OK) {
		SCC_WRITE_REGISTER(SCM_BLACK_START, slot_offset);
		SCC_WRITE_REGISTER(SCM_RED_START, slot_offset);

		/* Use OwnerID as CBC IV to tie Owner to data */
		SCC_WRITE_REGISTER(SCM_INIT_VECTOR_0, *(uint32_t *) & owner_id);
		SCC_WRITE_REGISTER(SCM_INIT_VECTOR_1,
				   *(((uint32_t *) & owner_id) + 1));

		/* Set modes and kick off the encryption */
		crypto_status = scc_do_crypto(length,
					      SCM_CONTROL_START_CIPHER |
					      SCM_CBC_MODE);

		if (crypto_status != 0) {
			pr_debug("SCM encrypt red crypto failure: 0x%x\n",
				 crypto_status);
		} else {

			/* Give blob back to caller */
			if (!copy_from_scc
			    (SCM_BLACK_MEMORY + scc_key_info[slot].offset,
			     black_data, length, NULL)) {
				status = SCC_RET_OK;
				pr_debug("SCC: Encrypted slot %d\n", slot);
			}
		}
	}

	spin_unlock_irqrestore(&scc_crypto_lock, irq_flags);

	return status;
}

/*****************************************************************************/
/* fn scc_decrypt_slot()                                                     */
/*****************************************************************************/
/*!
 * Decrypt some black data and leave result in the slot.
 *
 * @param owner_id      Value of owner of slot
 * @param slot          Handle of slot
 * @param length        Length, in bytes, of @c black_data
 * @param black_data    Location of data to dencrypt and store in slot
 *
 * @return SCC_RET_OK on success, SCC_RET_FAIL if slot specified cannot be
 *         accessed for any reason.
 */
scc_return_t scc_decrypt_slot(uint64_t owner_id, uint32_t slot,
			      uint32_t length, const uint8_t * black_data)
{
	unsigned long irq_flags;
	scc_return_t status;
	uint32_t crypto_status;
	uint32_t slot_offset =
	    scc_key_info[slot].offset / SCC_BLOCK_SIZE_BYTES();

	/* ACQUIRE LOCK to prevent others from using crypto or releasing slot */
	spin_lock_irqsave(&scc_crypto_lock, irq_flags);

	status = verify_slot_access(owner_id, slot, length);
	if (status == SCC_RET_OK) {
		status = SCC_RET_FAIL;	/* reset expectations */

		/* Place black key in to BLACK RAM and set up the SCC */
		copy_to_scc(black_data,
			    SCM_BLACK_MEMORY + scc_key_info[slot].offset,
			    length, NULL);

		SCC_WRITE_REGISTER(SCM_BLACK_START, slot_offset);
		SCC_WRITE_REGISTER(SCM_RED_START, slot_offset);

		/* Use OwnerID as CBC IV to tie Owner to data */
		SCC_WRITE_REGISTER(SCM_INIT_VECTOR_0, *(uint32_t *) & owner_id);
		SCC_WRITE_REGISTER(SCM_INIT_VECTOR_1,
				   *(((uint32_t *) & owner_id) + 1));

		/* Set modes and kick off the decryption */
		crypto_status = scc_do_crypto(length,
					      SCM_CONTROL_START_CIPHER
					      | SCM_CBC_MODE |
					      SCM_DECRYPT_MODE);

		if (crypto_status != 0) {
			pr_debug("SCM decrypt black crypto failure: 0x%x\n",
				 crypto_status);
		} else {
			status = SCC_RET_OK;
		}
	}

	spin_unlock_irqrestore(&scc_crypto_lock, irq_flags);

	return status;
}

/*****************************************************************************/
/* fn scc_get_slot_info()                                                    */
/*****************************************************************************/
/*!
 * Determine address and value length for a give slot.
 *
 * @param owner_id      Value of owner of slot
 * @param slot          Handle of slot
 * @param address       Location to store kernel address of slot data
 * @param value_size_bytes Location to store allocated length of data in slot.
 *                         May be NULL if value is not needed by caller.
 * @param slot_size_bytes  Location to store max length data in slot
 *                         May be NULL if value is not needed by caller.
 *
 * @return SCC_RET_OK or error indication
 */
scc_return_t
scc_get_slot_info(uint64_t owner_id, uint32_t slot, uint32_t * address,
		  uint32_t * value_size_bytes, uint32_t * slot_size_bytes)
{
	scc_return_t status = verify_slot_access(owner_id, slot, 0);

	if (status == SCC_RET_OK) {
		*address =
		    SCC_BASE + SCM_RED_MEMORY + scc_key_info[slot].offset;
		if (value_size_bytes != NULL) {
			*value_size_bytes = scc_key_info[slot].length;
		}
		if (slot_size_bytes != NULL) {
			*slot_size_bytes = SCC_KEY_SLOT_SIZE;
		}
	}

	return status;
}

/*****************************************************************************/
/* fn scc_wait_completion()                                                  */
/*****************************************************************************/
/*!
 * Poll looking for end-of-cipher indication. Only used
 * if @c SCC_SCM_SLEEP is not defined.
 *
 * @internal
 *
 * On a Tahiti, crypto under 230 or so bytes is done after the first loop, all
 * the way up to five sets of spins for 1024 bytes.  (8- and 16-byte functions
 * are done when we first look.  Zeroizing takes one pass around.
 */
static void scc_wait_completion(void)
{
	int i = 0;

	/* check for completion by polling */
	while (!is_cipher_done() && (i++ < SCC_CIPHER_MAX_POLL_COUNT)) {
		udelay(10);
	}
	pr_debug("SCC: Polled DONE %d times\n", i);
}				/* scc_wait_completion() */

/*****************************************************************************/
/* fn is_cipher_done()                                                       */
/*****************************************************************************/
/*!
 * This function returns non-zero if SCM Status register indicates
 * that a cipher has terminated or some other interrupt-generating
 * condition has occurred.
 */
static int is_cipher_done(void)
{
	register uint32_t scm_status;
	register int cipher_done;

	scm_status = SCC_READ_REGISTER(SCM_STATUS);

	/*
	 * Done when 'SCM is currently performing a function' bits are zero
	 */
	cipher_done = !(scm_status & (SCM_STATUS_ZEROIZING |
				      SCM_STATUS_CIPHERING));

	return cipher_done;
}				/* is_cipher_done() */

/*****************************************************************************/
/* fn offset_within_smn()                                                    */
/*****************************************************************************/
/*!
 *  Check that the offset is with the bounds of the SMN register set.
 *
 *  @param[in]  register_offset    register offset of SMN.
 *
 *  @return   1 if true, 0 if false (not within SMN)
 */
static inline int offset_within_smn(uint32_t register_offset)
{
	return register_offset >= SMN_STATUS && register_offset <= SMN_TIMER;
}

/*****************************************************************************/
/* fn offset_within_scm()                                                    */
/*****************************************************************************/
/*!
 *  Check that the offset is with the bounds of the SCM register set.
 *
 *  @param[in]  register_offset    Register offset of SCM
 *
 *  @return   1 if true, 0 if false (not within SCM)
 */
static inline int offset_within_scm(uint32_t register_offset)
{
	return (register_offset >= SCM_RED_START)
	    && (register_offset < scm_highest_memory_address);
	/* Although this would cause trouble for zeroize testing, this change would
	 * close a security whole which currently allows any kernel program to access
	 * any location in RED RAM.  Perhaps enforce in non-SCC_DEBUG compiles?
	 && (register_offset <= SCM_INIT_VECTOR_1); */
}

/*****************************************************************************/
/* fn check_register_accessible()                                            */
/*****************************************************************************/
/*!
 *  Given the current SCM and SMN status, verify that access to the requested
 *  register should be OK.
 *
 *  @param[in]   register_offset  register offset within SCC
 *  @param[in]   smn_status  recent value from #SMN_STATUS
 *  @param[in]   scm_status  recent value from #SCM_STATUS
 *
 *  @return   #SCC_RET_OK if ok, #SCC_RET_FAIL if not
 */
static scc_return_t
check_register_accessible(uint32_t register_offset, uint32_t smn_status,
			  uint32_t scm_status)
{
	int error_code = SCC_RET_FAIL;

	/* Verify that the register offset passed in is not among the verboten set
	 * if the SMN is in Fail mode.
	 */
	if (offset_within_smn(register_offset)) {
		if ((smn_status & SMN_STATUS_STATE_MASK) == SMN_STATE_FAIL) {
			if (!((register_offset == SMN_STATUS) ||
			      (register_offset == SMN_COMMAND) ||
			      (register_offset == SMN_DEBUG_DETECT_STAT))) {
				pr_debug
				    ("SCC Driver: Note: Security State is in FAIL state.\n");
			} /* register not a safe one */
			else {
				/* SMN is in  FAIL, but register is a safe one */
				error_code = SCC_RET_OK;
			}
		} /* State is FAIL */
		else {
			/* State is not fail.  All registers accessible. */
			error_code = SCC_RET_OK;
		}
	}
	/* offset within SMN */
	/*  Not SCM register.  Check for SCM busy. */
	else if (offset_within_scm(register_offset)) {
		/* This is the 'cannot access' condition in the SCM */
		if ((scm_status & SCM_STATUS_BUSY)
		    /* these are always available  - rest fail on busy */
		    && !((register_offset == SCM_STATUS) ||
			 (register_offset == SCM_ERROR_STATUS) ||
			 (register_offset == SCM_INTERRUPT_CTRL) ||
			 (register_offset == SCM_CONFIGURATION))) {
			pr_debug
			    ("SCC Driver: Note: Secure Memory is in BUSY state.\n");
		} /* status is busy & register inaccessible */
		else {
			error_code = SCC_RET_OK;
		}
	}
	/* offset within SCM */
	return error_code;

}				/* check_register_accessible() */

/*****************************************************************************/
/* fn check_register_offset()                                                */
/*****************************************************************************/
/*!
 *  Check that the offset is with the bounds of the SCC register set.
 *
 *  @param[in]  register_offset    register offset of SMN.
 *
 * #SCC_RET_OK if ok, #SCC_RET_FAIL if not
 */
static scc_return_t check_register_offset(uint32_t register_offset)
{
	int return_value = SCC_RET_FAIL;

	/* Is it valid word offset ? */
	if (SCC_BYTE_OFFSET(register_offset) == 0) {
		/* Yes. Is register within SCM? */
		if (offset_within_scm(register_offset)) {
			return_value = SCC_RET_OK;	/* yes, all ok */
		}
		/* Not in SCM.  Now look within the SMN */
		else if (offset_within_smn(register_offset)) {
			return_value = SCC_RET_OK;	/* yes, all ok */
		}
	}

	return return_value;
}

#ifdef SCC_REGISTER_DEBUG

/*****************************************************************************/
/* fn dbg_scc_read_register()                                                */
/*****************************************************************************/
/*!
 * Noisily read a 32-bit value to an SCC register.
 * @param offset        The address of the register to read.
 *
 * @return  The register value
 * */
static uint32_t dbg_scc_read_register(uint32_t offset)
{
	uint32_t value;

	value = readl(scc_base + offset);

#ifndef SCC_RAM_DEBUG		/* print no RAM references */
	if ((offset < SCM_RED_MEMORY) || (offset >= scm_highest_memory_address)) {
#endif
		pr_debug("SCC RD: 0x%4x : 0x%08x\n", offset, value);
#ifndef SCC_RAM_DEBUG
	}
#endif

	return value;
}

/*****************************************************************************/
/* fn dbg_scc_write_register()                                               */
/*****************************************************************************/
/*
 * Noisily read a 32-bit value to an SCC register.
 * @param offset        The address of the register to written.
 *
 * @param value         The new register value
 */
static void dbg_scc_write_register(uint32_t offset, uint32_t value)
{

#ifndef SCC_RAM_DEBUG		/* print no RAM references */
	if ((offset < SCM_RED_MEMORY) || (offset >= scm_highest_memory_address)) {
#endif
		pr_debug("SCC WR: 0x%4x : 0x%08x\n", offset, value);
#ifndef SCC_RAM_DEBUG
	}
#endif

	(void)writel(value, scc_base + offset);
}

#endif				/* SCC_REGISTER_DEBUG */