Commit 9712ebba9f4323107e4100def1a849f0f6ed4470

Con Kolivas 2014-05-02T10:53:09

Merge pull request #580 from zefir-k/A1.upstream.merge A1.upstream.merge

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
diff --git a/A1-board-selector-CCD.c b/A1-board-selector-CCD.c
new file mode 100644
index 0000000..d9c8528
--- /dev/null
+++ b/A1-board-selector-CCD.c
@@ -0,0 +1,119 @@
+/*
+ * board selector support for TCA9535 used in Bitmine's CoinCraft Desk
+ *
+ * Copyright 2014 Zefir Kurtisi <zefir.kurtisi@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+
+#include <sys/ioctl.h>
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <linux/i2c.h>
+#include <linux/i2c-dev.h>
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <fcntl.h>
+
+#include "miner.h"
+
+#include "A1-board-selector.h"
+
+static struct board_selector ccd_selector;
+
+struct ccd_ctx {
+	struct i2c_ctx U1_tca9535;
+	uint8_t board_mask;
+	uint8_t active_board;
+	pthread_mutex_t lock;
+};
+
+static struct ccd_ctx ccd_ctx = {
+	.U1_tca9535 = { .addr = 0x27, .file = -1 },
+	.board_mask = 0xff,
+	.active_board = 255,
+};
+
+#define UNUSED_BITS 0xe0
+
+static void ccd_unlock(void)
+{
+	mutex_unlock(&ccd_ctx.lock);
+}
+
+static void ccd_exit(void)
+{
+	i2c_slave_close(&ccd_ctx.U1_tca9535);
+}
+
+extern struct board_selector *ccd_board_selector_init(void)
+{
+	mutex_init(&ccd_ctx.lock);
+	struct i2c_ctx *ctx = &ccd_ctx.U1_tca9535;
+	bool retval =	i2c_slave_open(ctx, I2C_BUS) &&
+			i2c_slave_write(ctx, 0x06, 0xe0) &&
+			i2c_slave_write(ctx, 0x07, 0xe0) &&
+			i2c_slave_write(ctx, 0x02, 0x1f) &&
+			i2c_slave_write(ctx, 0x03, 0x00);
+	if (retval)
+		return &ccd_selector;
+	ccd_exit();
+	return NULL;
+}
+
+static bool ccd_select(uint8_t board)
+{
+	if (board >= CCD_MAX_CHAINS)
+		return false;
+
+	mutex_lock(&ccd_ctx.lock);
+	if (ccd_ctx.active_board == board)
+		return true;
+
+	ccd_ctx.active_board = board;
+	ccd_ctx.board_mask = 1 << ccd_ctx.active_board;
+	return i2c_slave_write(&ccd_ctx.U1_tca9535, 0x02, ~ccd_ctx.board_mask);
+}
+
+static bool __ccd_board_selector_reset(uint8_t mask)
+{
+	if (!i2c_slave_write(&ccd_ctx.U1_tca9535, 0x03, mask))
+		return false;
+	cgsleep_ms(RESET_LOW_TIME_MS);
+	if (!i2c_slave_write(&ccd_ctx.U1_tca9535, 0x03, 0x00))
+		return false;
+	cgsleep_ms(RESET_HI_TIME_MS);
+	return true;
+}
+// we assume we are already holding the mutex
+static bool ccd_reset(void)
+{
+	return __ccd_board_selector_reset(ccd_ctx.board_mask);
+}
+
+static bool ccd_reset_all(void)
+{
+	mutex_lock(&ccd_ctx.lock);
+	bool retval = __ccd_board_selector_reset(0xff & ~UNUSED_BITS);
+	mutex_unlock(&ccd_ctx.lock);
+	return retval;
+}
+
+
+static struct board_selector ccd_selector = {
+	.select = ccd_select,
+	.release = ccd_unlock,
+	.exit = ccd_exit,
+	.reset = ccd_reset,
+	.reset_all = ccd_reset_all,
+	.get_temp = dummy_u8,
+};
+
diff --git a/A1-board-selector-CCR.c b/A1-board-selector-CCR.c
new file mode 100644
index 0000000..b60d58e
--- /dev/null
+++ b/A1-board-selector-CCR.c
@@ -0,0 +1,305 @@
+/*
+ * board selector support for TCA9535 used in Bitmine's CoinCraft Desk
+ *
+ * Copyright 2014 Zefir Kurtisi <zefir.kurtisi@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+
+#include <sys/ioctl.h>
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <linux/i2c.h>
+#include <linux/i2c-dev.h>
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <fcntl.h>
+
+#include "miner.h"
+
+#include "A1-board-selector.h"
+
+static struct board_selector ccr_selector;
+
+
+bool i2c_slave_write(struct i2c_ctx *ctx, uint8_t reg, uint8_t val)
+{
+	union i2c_smbus_data data;
+	data.byte = val;
+
+	struct i2c_smbus_ioctl_data args;
+
+	args.read_write = I2C_SMBUS_WRITE;
+	args.command = reg;
+	args.size = I2C_SMBUS_BYTE_DATA;
+	args.data = &data;
+
+	if (ioctl(ctx->file, I2C_SMBUS, &args) == -1) {
+		applog(LOG_ERR, "Failed to write to fdesc %d: %s",
+		       ctx->file, strerror(errno));
+		return false;
+	}
+	applog(LOG_DEBUG, "W(0x%02x/0x%02x)=0x%02x", ctx->addr, reg, val);
+	return true;
+}
+
+bool i2c_slave_read(struct i2c_ctx *ctx, uint8_t reg, uint8_t *val)
+{
+	union i2c_smbus_data data;
+	struct i2c_smbus_ioctl_data args;
+
+	args.read_write = I2C_SMBUS_READ;
+	args.command = reg;
+	args.size = I2C_SMBUS_BYTE_DATA;
+	args.data = &data;
+
+	if (ioctl(ctx->file, I2C_SMBUS, &args) == -1) {
+		applog(LOG_ERR, "Failed to read from fdesc %d: %s",
+		       ctx->file, strerror(errno));
+		return false;
+	}
+	*val = data.byte;
+	applog(LOG_DEBUG, "R(0x%02x/0x%02x)=0x%02x", ctx->addr, reg, *val);
+	return true;
+}
+
+bool i2c_slave_open(struct i2c_ctx *ctx, char *i2c_bus)
+{
+	ctx->file = open(i2c_bus, O_RDWR);
+	if (ctx->file < 0) {
+		applog(LOG_INFO, "Failed to open i2c-1: %s", strerror(errno));
+		return false;
+	}
+
+	if (ioctl(ctx->file, I2C_SLAVE, ctx->addr) < 0) {
+		close(ctx->file);
+		return false;
+	}
+	return true;
+}
+
+void i2c_slave_close(struct i2c_ctx *ctx)
+{
+	if (ctx->file == -1)
+		return;
+	close(ctx->file);
+	ctx->file = -1;
+}
+
+
+struct ccr_ctx {
+	struct i2c_ctx U1_tca9548;
+	struct i2c_ctx U2_tca9535;
+	struct i2c_ctx U3_tca9535;
+	struct i2c_ctx U4_tca9535;
+	uint16_t chain_mask;
+	uint8_t active_chain;
+	pthread_mutex_t lock;
+	uint8_t boards_available;
+};
+
+static struct ccr_ctx ccr_ctx = {
+	.U1_tca9548 = { .addr = 0x70, .file = -1, },
+	.U2_tca9535 = { .addr = 0x20, .file = -1, },
+	.U3_tca9535 = { .addr = 0x23, .file = -1, },
+	.U4_tca9535 = { .addr = 0x22, .file = -1, },
+	.chain_mask = 0xffff,
+	.active_chain = 255,
+	.boards_available = 0x00,
+};
+
+struct chain_mapping {
+	uint8_t chain_id;
+	uint8_t U1;
+	uint8_t U2p0;
+	uint8_t U2p1;
+	uint8_t U3p0;
+	uint8_t U3p1;
+};
+
+static const struct chain_mapping chain_mapping[CCR_MAX_CHAINS] = {
+	{  0, 0x01, 0x01, 0x80, 0x01, 0x00, },
+	{  1, 0x01, 0x01, 0x80, 0x00, 0x80, },
+	{  2, 0x02, 0x02, 0x40, 0x02, 0x00, },
+	{  3, 0x02, 0x02, 0x40, 0x00, 0x40, },
+	{  4, 0x04, 0x04, 0x20, 0x04, 0x00, },
+	{  5, 0x04, 0x04, 0x20, 0x00, 0x20, },
+	{  6, 0x08, 0x08, 0x10, 0x08, 0x00, },
+	{  7, 0x08, 0x08, 0x10, 0x00, 0x10, },
+	{  8, 0x10, 0x10, 0x08, 0x10, 0x00, },
+	{  9, 0x10, 0x10, 0x08, 0x00, 0x08, },
+	{ 10, 0x20, 0x20, 0x04, 0x20, 0x00, },
+	{ 11, 0x20, 0x20, 0x04, 0x00, 0x04, },
+	{ 12, 0x40, 0x40, 0x02, 0x40, 0x00, },
+	{ 13, 0x40, 0x40, 0x02, 0x00, 0x02, },
+	{ 14, 0x80, 0x80, 0x01, 0x80, 0x00, },
+	{ 15, 0x80, 0x80, 0x01, 0x00, 0x01, },
+};
+
+static void ccr_unlock(void)
+{
+	mutex_unlock(&ccr_ctx.lock);
+}
+
+static void ccr_exit(void)
+{
+	i2c_slave_close(&ccr_ctx.U1_tca9548);
+	i2c_slave_close(&ccr_ctx.U2_tca9535);
+	i2c_slave_close(&ccr_ctx.U3_tca9535);
+	i2c_slave_close(&ccr_ctx.U4_tca9535);
+}
+
+static bool ccr_power_on_one_board(uint8_t chain)
+{
+	const struct chain_mapping *cm = &chain_mapping[chain];
+	if (chain & 1)
+		return false;
+	uint8_t new_power_mask = ccr_ctx.boards_available | cm->U2p0;
+	if (!i2c_slave_write(&ccr_ctx.U2_tca9535, 0x03, new_power_mask))
+		return false;
+	int i;
+	for (i = 0; i < 8; i ++) {
+		uint8_t val;
+		if (!i2c_slave_read(&ccr_ctx.U2_tca9535, 0x00, &val))
+			return false;
+		if (val & cm->U2p1) {
+			applog(LOG_INFO, "Power OK for chain %d after %d",
+			       chain, i);
+			ccr_ctx.boards_available = new_power_mask;
+			return true;
+		}
+		cgsleep_ms(10);
+	}
+	applog(LOG_INFO, "Power NOK for chain %d", chain);
+	return false;
+}
+static int ccr_power_on_boards(void)
+{
+	int i;
+	int boards = 0;
+	for (i = 0; i < CCR_MAX_CHAINS / 2; i++) {
+		if (ccr_power_on_one_board(i * 2))
+			boards++;
+	}
+	return boards;
+}
+
+extern struct board_selector *ccr_board_selector_init(void)
+{
+	mutex_init(&ccr_ctx.lock);
+	applog(LOG_INFO, "ccr_board_selector_init()");
+
+			/* detect all i2c slaves */
+	bool res =	i2c_slave_open(&ccr_ctx.U1_tca9548, I2C_BUS) &&
+			i2c_slave_open(&ccr_ctx.U2_tca9535, I2C_BUS) &&
+			i2c_slave_open(&ccr_ctx.U3_tca9535, I2C_BUS) &&
+			i2c_slave_open(&ccr_ctx.U4_tca9535, I2C_BUS) &&
+			/* init I2C multiplexer */
+			i2c_slave_write(&ccr_ctx.U1_tca9548, 0x00, 0x00) &&
+			/* init power selector */
+			i2c_slave_write(&ccr_ctx.U2_tca9535, 0x06, 0xff) &&
+			i2c_slave_write(&ccr_ctx.U2_tca9535, 0x07, 0x00) &&
+			i2c_slave_write(&ccr_ctx.U2_tca9535, 0x03, 0x00) &&
+			/* init reset selector */
+			i2c_slave_write(&ccr_ctx.U3_tca9535, 0x06, 0x00) &&
+			i2c_slave_write(&ccr_ctx.U3_tca9535, 0x07, 0x00) &&
+			i2c_slave_write(&ccr_ctx.U3_tca9535, 0x02, 0x00) &&
+			i2c_slave_write(&ccr_ctx.U3_tca9535, 0x03, 0x00) &&
+			/* init chain selector */
+			i2c_slave_write(&ccr_ctx.U4_tca9535, 0x06, 0x00) &&
+			i2c_slave_write(&ccr_ctx.U4_tca9535, 0x07, 0x00) &&
+			i2c_slave_write(&ccr_ctx.U4_tca9535, 0x02, 0x00) &&
+			i2c_slave_write(&ccr_ctx.U4_tca9535, 0x03, 0x00);
+
+	if (!res)
+		goto fail;
+
+	if (ccr_power_on_boards() == 0)
+		goto fail;
+
+	return &ccr_selector;
+
+fail:
+	ccr_exit();
+	return NULL;
+}
+
+static bool ccr_select(uint8_t chain)
+{
+	if (chain >= CCR_MAX_CHAINS)
+		return false;
+
+	mutex_lock(&ccr_ctx.lock);
+	if (ccr_ctx.active_chain == chain)
+		return true;
+
+	ccr_ctx.active_chain = chain;
+	const struct chain_mapping *cm = &chain_mapping[chain];
+	if (!i2c_slave_write(&ccr_ctx.U4_tca9535, 0x02, cm->U3p0) ||
+	    !i2c_slave_write(&ccr_ctx.U4_tca9535, 0x03, cm->U3p1) ||
+	    !i2c_slave_write(&ccr_ctx.U1_tca9548, cm->U1, cm->U1))
+		return false;
+
+	applog(LOG_DEBUG, "selected chain %d", chain);
+	return true;
+}
+
+static bool __ccr_board_selector_reset(uint8_t p0, uint8_t p1)
+{
+	if (!i2c_slave_write(&ccr_ctx.U3_tca9535, 0x02, p0) ||
+	    !i2c_slave_write(&ccr_ctx.U3_tca9535, 0x03, p1))
+		return false;
+	cgsleep_ms(RESET_LOW_TIME_MS);
+	if (!i2c_slave_write(&ccr_ctx.U3_tca9535, 0x02, 0x00) ||
+	    !i2c_slave_write(&ccr_ctx.U3_tca9535, 0x03, 0x00))
+		return false;
+	cgsleep_ms(RESET_HI_TIME_MS);
+	return true;
+}
+// we assume we are already holding the mutex
+static bool ccr_reset(void)
+{
+	const struct chain_mapping *cm = &chain_mapping[ccr_ctx.active_chain];
+	bool retval = __ccr_board_selector_reset(cm->U3p0, cm->U3p1);
+	return retval;
+}
+
+static bool ccr_reset_all(void)
+{
+	mutex_lock(&ccr_ctx.lock);
+	bool retval = __ccr_board_selector_reset(0xff, 0xff);
+	mutex_unlock(&ccr_ctx.lock);
+	return retval;
+}
+
+static uint8_t ccr_get_temp(void)
+{
+	if (ccr_ctx.active_chain & 1)
+		return 0;
+
+	uint8_t retval = 0;
+	static struct i2c_ctx U7 = { .addr = 0x4c, .file = -1 };
+	if (i2c_slave_open(&U7, I2C_BUS)) {
+		i2c_slave_read(&U7, 0, &retval);
+		i2c_slave_close(&U7);
+	}
+	return retval;
+}
+
+static struct board_selector ccr_selector = {
+	.select = ccr_select,
+	.release = ccr_unlock,
+	.exit = ccr_exit,
+	.reset = ccr_reset,
+	.reset_all = ccr_reset_all,
+	.get_temp = ccr_get_temp,
+};
+
diff --git a/A1-board-selector.h b/A1-board-selector.h
new file mode 100644
index 0000000..9102f70
--- /dev/null
+++ b/A1-board-selector.h
@@ -0,0 +1,54 @@
+#ifndef A1_BOARD_SELECTOR_H
+#define A1_BOARD_SELECTOR_H
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#define RESET_LOW_TIME_MS 200
+#define RESET_HI_TIME_MS  100
+
+struct board_selector {
+	bool (*select)(uint8_t board);
+	void (*release)(void);
+	void (*exit)(void);
+	bool (*reset)(void);
+	bool (*reset_all)(void);
+	uint8_t (*get_temp)(void);
+};
+
+static bool dummy_select(uint8_t b) { (void)b; return true; }
+static void dummy_void(void) { };
+static bool dummy_bool(void) { return true; }
+static uint8_t dummy_u8(void) { return 0; }
+
+static const struct board_selector dummy_board_selector = {
+	.select = dummy_select,
+	.release = dummy_void,
+	.exit = dummy_void,
+	.reset = dummy_bool,
+	.reset_all = dummy_bool,
+	.get_temp = dummy_u8,
+};
+
+/* CoinCraft Desk and Rig board selector constructors */
+#define CCD_MAX_CHAINS	5
+#define CCR_MAX_CHAINS	16
+extern struct board_selector *ccd_board_selector_init(void);
+extern struct board_selector *ccr_board_selector_init(void);
+
+
+/* common i2c context */
+struct i2c_ctx {
+	uint8_t addr;
+	int file;
+};
+
+/* the default I2C bus on RPi */
+#define I2C_BUS		"/dev/i2c-1"
+
+extern bool i2c_slave_write(struct i2c_ctx *ctx, uint8_t reg, uint8_t val);
+extern bool i2c_slave_read(struct i2c_ctx *ctx, uint8_t reg, uint8_t *val);
+extern bool i2c_slave_open(struct i2c_ctx *ctx, char *i2c_bus);
+extern void i2c_slave_close(struct i2c_ctx *ctx);
+
+#endif /* A1_BOARD_SELECTOR_H */
diff --git a/A1-common.h b/A1-common.h
new file mode 100644
index 0000000..27fd60d
--- /dev/null
+++ b/A1-common.h
@@ -0,0 +1,89 @@
+#ifndef A1_COMMON_H
+#define A1_COMMON_H
+
+#include <stddef.h>
+#include <stdbool.h>
+#include <stdint.h>
+
+/********** work queue */
+struct work_ent {
+	struct work *work;
+	struct list_head head;
+};
+
+struct work_queue {
+	int num_elems;
+	struct list_head head;
+};
+
+/********** chip and chain context structures */
+/* the WRITE_JOB command is the largest (2 bytes command, 56 bytes payload) */
+#define WRITE_JOB_LENGTH	58
+#define MAX_CHAIN_LENGTH	64
+/*
+ * For commands to traverse the chain, we need to issue dummy writes to
+ * keep SPI clock running. To reach the last chip in the chain, we need to
+ * write the command, followed by chain-length words to pass it through the
+ * chain and another chain-length words to get the ACK back to host
+ */
+#define MAX_CMD_LENGTH		(WRITE_JOB_LENGTH + MAX_CHAIN_LENGTH * 2 * 2)
+
+struct A1_chip {
+	int num_cores;
+	int last_queued_id;
+	struct work *work[4];
+	/* stats */
+	int hw_errors;
+	int stales;
+	int nonces_found;
+	int nonce_ranges_done;
+
+	/* systime in ms when chip was disabled */
+	int cooldown_begin;
+	/* number of consecutive failures to access the chip */
+	int fail_count;
+	/* mark chip disabled, do not try to re-enable it */
+	bool disabled;
+};
+
+struct A1_chain {
+	int chain_id;
+	struct cgpu_info *cgpu;
+	struct mcp4x *trimpot;
+	int num_chips;
+	int num_cores;
+	int num_active_chips;
+	int chain_skew;
+	uint8_t spi_tx[MAX_CMD_LENGTH];
+	uint8_t spi_rx[MAX_CMD_LENGTH];
+	struct spi_ctx *spi_ctx;
+	struct A1_chip *chips;
+	pthread_mutex_t lock;
+
+	struct work_queue active_wq;
+
+	uint8_t temp;
+	int last_temp_time;
+};
+
+#define MAX_CHAINS_PER_BOARD	2
+struct A1_board {
+	int board_id;
+	int num_chains;
+	struct A1_chain *chain[MAX_CHAINS_PER_BOARD];
+};
+
+/********** config paramters */
+struct A1_config_options {
+	int ref_clk_khz;
+	int sys_clk_khz;
+	int spi_clk_khz;
+	/* limit chip chain to this number of chips (testing only) */
+	int override_chip_num;
+	int wiper;
+};
+
+/* global configuration instance */
+extern struct A1_config_options A1_config_options;
+
+#endif /* A1_COMMON_H */
diff --git a/A1-desk-board-selector-tca9535.c b/A1-desk-board-selector-tca9535.c
deleted file mode 100644
index 5a451f6..0000000
--- a/A1-desk-board-selector-tca9535.c
+++ /dev/null
@@ -1,147 +0,0 @@
-#include <sys/ioctl.h>
-#include <errno.h>
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <linux/i2c.h>
-#include <linux/i2c-dev.h>
-
-#include <stdint.h>
-#include <stdbool.h>
-#include <fcntl.h>
-
-#include "miner.h"
-
-struct tca9535_ctx {
-	uint8_t addr;
-	uint8_t board_mask;
-	int file;
-	uint8_t active_board;
-	pthread_mutex_t lock;
-};
-
-static struct tca9535_ctx board_ctx = {
-	.addr = 0x27,
-	.board_mask = 0xff,
-	.file = -1,
-	.active_board = 255,
-};
-
-
-#define UNUSED_BITS 0xe0
-#define SLEEP_US_AFTER_CS 200
-#define RESET_LOW_TIME_MS 200
-#define RESET_HI_TIME_MS  100
-
-static bool tca9535_write(int fdesc, uint8_t p0, uint8_t p1)
-{
-	union i2c_smbus_data data;
-	data.byte = p1;
-
-	struct i2c_smbus_ioctl_data args;
-	__s32 err;
-
-	args.read_write = I2C_SMBUS_WRITE;
-	args.command = p0;
-	args.size = I2C_SMBUS_BYTE_DATA;
-	args.data = &data;
-
-	err = ioctl(fdesc, I2C_SMBUS, &args);
-	if (err == -1) {
-		applog(LOG_ERR, "Failed to write to fdesc %d: %s\n",
-		       fdesc, strerror(errno));
-		err = -errno;
-	} else {
-		applog(LOG_DEBUG, "written: 0x%02x, 0x%02x", p0, p1);
-		cgsleep_us(SLEEP_US_AFTER_CS);
-	}
-	return err == 0;
-}
-
-void lock_board_selector(void)
-{
-//	applog(LOG_WARNING, "lock_board_selector()");
-	mutex_lock(&board_ctx.lock);
-}
-
-void unlock_board_selector(void)
-{
-//	applog(LOG_WARNING, "unlock_board_selector()");
-	mutex_unlock(&board_ctx.lock);
-}
-
-bool a1_board_selector_init(void)
-{
-	mutex_init(&board_ctx.lock);
-	applog(LOG_WARNING, "a1_board_selector_init()");
-
-	board_ctx.file = open("/dev/i2c-1", O_RDWR);
-	if (board_ctx.file < 0) {
-		fprintf(stderr,
-			"Error: Could not open i2c-1.%d: %s\n",
-			board_ctx.addr, strerror(errno));
-		return false;
-	}
-
-	if (ioctl(board_ctx.file, I2C_SLAVE, board_ctx.addr) < 0) {
-		fprintf(stderr,
-			"Error: Could not set address to 0x%02x: %s\n",
-			board_ctx.addr, strerror(errno));
-		return false;
-	}
-	bool retval =	tca9535_write(board_ctx.file, 0x06, 0xe0) &&
-			tca9535_write(board_ctx.file, 0x07, 0xe0) &&
-			tca9535_write(board_ctx.file, 0x02, 0x1f) &&
-			tca9535_write(board_ctx.file, 0x03, 0x00);
-	return retval;
-}
-
-void a1_board_selector_exit(void)
-{
-	close(board_ctx.file);
-	board_ctx.file = -1;
-}
-
-bool a1_board_selector_select_board(uint8_t board)
-{
-	if (board > 7)
-		return false;
-
-//	applog(LOG_WARNING, "board_selector_select_board(%d)", board);
-	lock_board_selector();
-	if (board_ctx.active_board == board)
-		return true;
-
-	board_ctx.active_board = board;
-	board_ctx.board_mask = 1 << board_ctx.active_board;
-	return tca9535_write(board_ctx.file, 0x02, ~board_ctx.board_mask);
-}
-
-static bool __board_selector_reset(void)
-{
-	if (!tca9535_write(board_ctx.file, 0x03, board_ctx.board_mask))
-		return false;
-	cgsleep_ms(RESET_LOW_TIME_MS);
-	if (!tca9535_write(board_ctx.file, 0x03, 0x00))
-		return false;
-	cgsleep_ms(RESET_HI_TIME_MS);
-	return true;
-}
-// we assume we are already holding the mutex
-bool a1_board_selector_reset_board(void)
-{
-	bool retval = __board_selector_reset();
-	return retval;
-}
-
-bool a1_board_selector_reset_all_boards(void)
-{
-	lock_board_selector();
-	board_ctx.board_mask = 0xff & ~UNUSED_BITS;
-	bool retval = __board_selector_reset();
-	unlock_board_selector();
-	return retval;
-}
-
-/////////////////////////////////////////////////////////////////////////////
diff --git a/A1-trimpot-mcp4x.c b/A1-trimpot-mcp4x.c
new file mode 100644
index 0000000..a9244fd
--- /dev/null
+++ b/A1-trimpot-mcp4x.c
@@ -0,0 +1,116 @@
+/*
+ * support for MCP46x digital trimpot used in Bitmine's products
+ *
+ * Copyright 2014 Zefir Kurtisi <zefir.kurtisi@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <linux/i2c.h>
+#include <linux/i2c-dev.h>
+#include <sys/ioctl.h>
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <fcntl.h>
+
+#include "miner.h"
+
+#include "A1-trimpot-mcp4x.h"
+
+
+static bool mcp4x_check_status(int file)
+{
+	union i2c_smbus_data data;
+	struct i2c_smbus_ioctl_data args;
+
+	args.read_write = I2C_SMBUS_READ;
+	args.command = ((5 & 0x0f) << 4) | 0x0c;
+	args.size = I2C_SMBUS_WORD_DATA;
+	args.data = &data;
+
+	return ioctl(file, I2C_SMBUS, &args) >= 0;
+}
+
+static uint16_t mcp4x_get_wiper(struct mcp4x *me, uint8_t id)
+{
+	assert(id < 2);
+	union i2c_smbus_data data;
+	struct i2c_smbus_ioctl_data args;
+
+	args.read_write = I2C_SMBUS_READ;
+	args.command = ((id & 0x0f) << 4) | 0x0c;
+	args.size = I2C_SMBUS_WORD_DATA;
+	args.data = &data;
+
+	if (ioctl(me->file, I2C_SMBUS, &args) < 0) {
+		applog(LOG_ERR, "Failed to read id %d: %s\n", id,
+		       strerror(errno));
+		return 0xffff;
+	}
+	return htobe16(data.word & 0xffff);
+}
+
+static bool mcp4x_set_wiper(struct mcp4x *me, uint8_t id, uint16_t w)
+{
+	assert(id < 2);
+	union i2c_smbus_data data;
+	data.word = w;
+
+	struct i2c_smbus_ioctl_data args;
+
+	args.read_write = I2C_SMBUS_WRITE;
+	args.command = (id & 0x0f) << 4;
+	args.size = I2C_SMBUS_WORD_DATA;
+	args.data = &data;
+
+	if (ioctl(me->file, I2C_SMBUS, &args) < 0) {
+		applog(LOG_ERR, "Failed to read id %d: %s\n", id,
+		       strerror(errno));
+		return false;
+	}
+	return me->get_wiper(me, id) == w;
+}
+
+void mcp4x_exit(struct mcp4x *me)
+{
+	close(me->file);
+	free(me);
+}
+
+struct mcp4x *mcp4x_init(uint8_t addr)
+{
+	struct mcp4x *me;
+	int file = open("/dev/i2c-1", O_RDWR);
+	if (file < 0) {
+		applog(LOG_INFO, "Failed to open i2c-1: %s\n", strerror(errno));
+		return NULL;
+	}
+
+	if (ioctl(file, I2C_SLAVE, addr) < 0)
+		return NULL;
+
+	if (!mcp4x_check_status(file))
+		return NULL;
+
+	me = malloc(sizeof(*me));
+	assert(me != NULL);
+
+	me->addr = addr;
+	me->file = file;
+	me->exit = mcp4x_exit;
+	me->get_wiper = mcp4x_get_wiper;
+	me->set_wiper = mcp4x_set_wiper;
+	return me;
+}
+
diff --git a/A1-trimpot-mcp4x.h b/A1-trimpot-mcp4x.h
new file mode 100644
index 0000000..b7ff600
--- /dev/null
+++ b/A1-trimpot-mcp4x.h
@@ -0,0 +1,19 @@
+#ifndef TRIMPOT_MPC4X_H
+#define TRIMPOT_MPC4X_H
+
+#include <stdint.h>
+#include <stdbool.h>
+
+
+struct mcp4x {
+	uint16_t (*get_wiper)(struct mcp4x *me, uint8_t id);
+	bool (*set_wiper)(struct mcp4x *me, uint8_t id, uint16_t w);
+	void (*exit)(struct mcp4x *me);
+	uint8_t addr;
+	int file;
+};
+
+/* constructor */
+extern struct mcp4x *mcp4x_init(uint8_t addr);
+
+#endif /* TRIMPOT_MPC4X_H */
diff --git a/Makefile.am b/Makefile.am
index 573b8d5..184e599 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -14,7 +14,6 @@ EXTRA_DIST	= example.conf linux-usb-cgminer \
 		  API.class API.java api-example.c windows-build.txt \
 		  bitstreams/README API-README FPGA-README \
 		  bitforce-firmware-flash.c hexdump.c ASIC-README \
-		  A1-desk-board-selector.c A1-desk-board-selector-tca9535.c \
 		  01-cgminer.rules
 
 SUBDIRS		= lib compat ccan
@@ -86,6 +85,10 @@ endif
 if HAS_BITMINE_A1
 cgminer_SOURCES += driver-SPI-bitmine-A1.c
 cgminer_SOURCES += spi-context.c spi-context.h
+cgminer_SOURCES += A1-common.h
+cgminer_SOURCES += A1-board-selector.h
+cgminer_SOURCES += A1-board-selector-CCD.c A1-board-selector-CCR.c
+cgminer_SOURCES += A1-trimpot-mcp4x.h A1-trimpot-mcp4x.c
 endif
 
 if HAS_DRILLBIT
diff --git a/driver-SPI-bitmine-A1.c b/driver-SPI-bitmine-A1.c
index 8ba4a6e..2ac50db 100644
--- a/driver-SPI-bitmine-A1.c
+++ b/driver-SPI-bitmine-A1.c
@@ -21,37 +21,14 @@
 #include "miner.h"
 #include "util.h"
 
-///////////////////////////////////////////////////////////////////////////
-#if 1
-#define MAX_BOARDS 5
-
-/*
- * TODO: yes, we include a C file for now until we set up a framework
- * to support different variants of A1 products
- */
-#include "A1-desk-board-selector-tca9535.c"
-#else
-#define MAX_BOARDS 1
-bool a1_board_selector_init() { return true; }
-void a1_board_selector_exit() {}
-void a1_board_selector_reset_all_boards() {}
-void a1_board_selector_select_board(uint8_t b) {};
-void unlock_board_selector() {}
-#endif
-///////////////////////////////////////////////////////////////////////////
+#include "A1-common.h"
+#include "A1-board-selector.h"
+#include "A1-trimpot-mcp4x.h"
 
+/* one global board_selector is enough */
+static struct board_selector *board_selector;
 
 /********** work queue */
-struct work_ent {
-	struct work *work;
-	struct list_head head;
-};
-
-struct work_queue {
-	int num_elems;
-	struct list_head head;
-};
-
 static bool wq_enqueue(struct work_queue *wq, struct work *work)
 {
 	if (work == NULL)
@@ -82,7 +59,6 @@ static struct work *wq_dequeue(struct work_queue *wq)
 	return work;
 }
 
-/********** chip and chain context structures */
 /*
  * if not cooled sufficiently, communication fails and chip is temporary
  * disabled. we let it inactive for 30 seconds to cool down
@@ -91,52 +67,8 @@ static struct work *wq_dequeue(struct work_queue *wq)
  */
 #define COOLDOWN_MS (30 * 1000)
 /* if after this number of retries a chip is still inaccessible, disable it */
-#define DISABLE_CHIP_FAIL_THRESHOLD	7
+#define DISABLE_CHIP_FAIL_THRESHOLD	3
 
-/* the WRITE_JOB command is the largest (2 bytes command, 56 bytes payload) */
-#define WRITE_JOB_LENGTH	58
-#define MAX_CHAIN_LENGTH	64
-/*
- * For commands to traverse the chain, we need to issue dummy writes to
- * keep SPI clock running. To reach the last chip in the chain, we need to
- * write the command, followed by chain-length words to pass it through the
- * chain and another chain-length words to get the ACK back to host
- */
-#define MAX_CMD_LENGTH		(WRITE_JOB_LENGTH + MAX_CHAIN_LENGTH * 2 * 2)
-
-struct A1_chip {
-	int num_cores;
-	int last_queued_id;
-	struct work *work[4];
-	/* stats */
-	int hw_errors;
-	int stales;
-	int nonces_found;
-	int nonce_ranges_done;
-
-	/* systime in ms when chip was disabled */
-	int cooldown_begin;
-	/* number of consecutive failures to access the chip */
-	int fail_count;
-	/* mark chip disabled, do not try to re-enable it */
-	bool disabled;
-};
-
-struct A1_chain {
-	int board_id;
-	struct cgpu_info *cgpu;
-	int num_chips;
-	int num_cores;
-	int num_active_chips;
-	int chain_skew;
-	uint8_t spi_tx[MAX_CMD_LENGTH];
-	uint8_t spi_rx[MAX_CMD_LENGTH];
-	struct spi_ctx *spi_ctx;
-	struct A1_chip *chips;
-	pthread_mutex_t lock;
-
-	struct work_queue active_wq;
-};
 
 enum A1_command {
 	A1_BIST_START		= 0x01,
@@ -149,21 +81,12 @@ enum A1_command {
 	A1_READ_REG_RESP	= 0x1a,
 };
 
-/********** config paramters */
-struct A1_config_options {
-	int ref_clk_khz;
-	int sys_clk_khz;
-	int spi_clk_khz;
-	/* limit chip chain to this number of chips (testing only) */
-	int override_chip_num;
-};
-
 /*
  * for now, we have one global config, defaulting values:
  * - ref_clk 16MHz / sys_clk 800MHz
  * - 2000 kHz SPI clock
  */
-static struct A1_config_options config_options = {
+struct A1_config_options A1_config_options = {
 	.ref_clk_khz = 16000, .sys_clk_khz = 800000, .spi_clk_khz = 2000,
 };
 
@@ -188,7 +111,7 @@ static void applog_hexdump(char *prefix, uint8_t *buff, int len, int level)
 		}
 		pos += sprintf(pos, "%.2X ", buff[i]);
 	}
-	applog(LOG_DEBUG, "%s", line);
+	applog(level, "%s", line);
 }
 
 static void hexdump(char *prefix, uint8_t *buff, int len)
@@ -222,15 +145,15 @@ static uint8_t *exec_cmd(struct A1_chain *a1,
 	if (data != NULL)
 		memcpy(a1->spi_tx + 2, data, len);
 
-	bool retval = spi_transfer(a1->spi_ctx, a1->spi_tx, a1->spi_rx, tx_len);
+	assert(spi_transfer(a1->spi_ctx, a1->spi_tx, a1->spi_rx, tx_len));
 	hexdump("send: TX", a1->spi_tx, tx_len);
 	hexdump("send: RX", a1->spi_rx, tx_len);
 
 	int poll_len = resp_len;
 	if (chip_id == 0) {
 		if (a1->num_chips == 0) {
-			applog(LOG_ERR, "%d: unknown chips in chain, assuming 8",
-			       a1->board_id);
+			applog(LOG_ERR, "%d: unknown chips in chain, "
+			       "assuming 8", a1->chain_id);
 			poll_len += 32;
 		}
 		poll_len += 4 * a1->num_chips;
@@ -254,7 +177,7 @@ static uint8_t *cmd_BIST_FIX_BCAST(struct A1_chain *a1)
 {
 	uint8_t *ret = exec_cmd(a1, A1_BIST_FIX, 0x00, NULL, 0, 0);
 	if (ret == NULL || ret[0] != A1_BIST_FIX) {
-		applog(LOG_ERR, "%d: cmd_BIST_FIX_BCAST failed", a1->board_id);
+		applog(LOG_ERR, "%d: cmd_BIST_FIX_BCAST failed", a1->chain_id);
 		return NULL;
 	}
 	return ret;
@@ -267,7 +190,7 @@ static uint8_t *cmd_RESET_BCAST(struct A1_chain *a1, uint8_t strategy)
 	s[1] = strategy;
 	uint8_t *ret = exec_cmd(a1, A1_RESET, 0x00, s, 2, 0);
 	if (ret == NULL || (ret[0] != A1_RESET && a1->num_chips != 0)) {
-		applog(LOG_ERR, "%d: cmd_RESET_BCAST failed", a1->board_id);
+		applog(LOG_ERR, "%d: cmd_RESET_BCAST failed", a1->chain_id);
 		return NULL;
 	}
 	return ret;
@@ -279,22 +202,21 @@ static uint8_t *cmd_READ_RESULT_BCAST(struct A1_chain *a1)
 	memset(a1->spi_tx, 0, tx_len);
 	a1->spi_tx[0] = A1_READ_RESULT;
 
-	bool retval = spi_transfer(a1->spi_ctx, a1->spi_tx, a1->spi_rx, tx_len);
+	assert(spi_transfer(a1->spi_ctx, a1->spi_tx, a1->spi_rx, tx_len));
 	hexdump("send: TX", a1->spi_tx, tx_len);
 	hexdump("send: RX", a1->spi_rx, tx_len);
 
 	int poll_len = tx_len + 4 * a1->num_chips;
-	retval = spi_transfer(a1->spi_ctx, NULL, a1->spi_rx + tx_len, poll_len);
+	assert(spi_transfer(a1->spi_ctx, NULL, a1->spi_rx + tx_len, poll_len));
 	hexdump("poll: RX", a1->spi_rx + tx_len, poll_len);
 
 	uint8_t *scan = a1->spi_rx;
 	int i;
 	for (i = 0; i < poll_len; i += 2) {
-		if ((scan[i] & 0x0f) == A1_READ_RESULT) {
+		if ((scan[i] & 0x0f) == A1_READ_RESULT)
 			return scan + i;
-		}
 	}
-	applog(LOG_ERR, "%d: cmd_READ_RESULT_BCAST failed", a1->board_id);
+	applog(LOG_ERR, "%d: cmd_READ_RESULT_BCAST failed", a1->chain_id);
 	return NULL;
 }
 
@@ -302,7 +224,7 @@ static uint8_t *cmd_WRITE_REG(struct A1_chain *a1, uint8_t chip, uint8_t *reg)
 {
 	uint8_t *ret = exec_cmd(a1, A1_WRITE_REG, chip, reg, 6, 0);
 	if (ret == NULL || ret[0] != A1_WRITE_REG) {
-		applog(LOG_ERR, "%d: cmd_WRITE_REG failed", a1->board_id);
+		applog(LOG_ERR, "%d: cmd_WRITE_REG failed", a1->chain_id);
 		return NULL;
 	}
 	return ret;
@@ -313,7 +235,7 @@ static uint8_t *cmd_READ_REG(struct A1_chain *a1, uint8_t chip)
 	uint8_t *ret = exec_cmd(a1, A1_READ_REG, chip, NULL, 0, 6);
 	if (ret == NULL || ret[0] != A1_READ_REG_RESP || ret[1] != chip) {
 		applog(LOG_ERR, "%d: cmd_READ_REG chip %d failed",
-		       a1->board_id, chip);
+		       a1->chain_id, chip);
 		return NULL;
 	}
 	memcpy(a1->spi_rx, ret, 8);
@@ -323,20 +245,18 @@ static uint8_t *cmd_READ_REG(struct A1_chain *a1, uint8_t chip)
 static uint8_t *cmd_WRITE_JOB(struct A1_chain *a1, uint8_t chip_id,
 			      uint8_t *job)
 {
-
-	uint8_t cmd = (a1->spi_tx[0] << 8) | a1->spi_tx[1];
 	/* ensure we push the SPI command to the last chip in chain */
 	int tx_len = WRITE_JOB_LENGTH + 2;
 	memcpy(a1->spi_tx, job, WRITE_JOB_LENGTH);
 	memset(a1->spi_tx + WRITE_JOB_LENGTH, 0, tx_len - WRITE_JOB_LENGTH);
 
-	bool retval = spi_transfer(a1->spi_ctx, a1->spi_tx, a1->spi_rx, tx_len);
+	assert(spi_transfer(a1->spi_ctx, a1->spi_tx, a1->spi_rx, tx_len));
 	hexdump("send: TX", a1->spi_tx, tx_len);
 	hexdump("send: RX", a1->spi_rx, tx_len);
 
 	int poll_len = 4 * chip_id - 2;
 
-	retval = spi_transfer(a1->spi_ctx, NULL, a1->spi_rx + tx_len, poll_len);
+	assert(spi_transfer(a1->spi_ctx, NULL, a1->spi_rx + tx_len, poll_len));
 	hexdump("poll: RX", a1->spi_rx + tx_len, poll_len);
 
 	int ack_len = tx_len;
@@ -346,7 +266,7 @@ static uint8_t *cmd_WRITE_JOB(struct A1_chain *a1, uint8_t chip_id,
 	uint8_t *ret = a1->spi_rx + ack_pos;
 	if (ret[0] != a1->spi_tx[0] || ret[1] != a1->spi_tx[1]){
 		applog(LOG_ERR, "%d: cmd_WRITE_JOB failed: "
-			"0x%02x%02x/0x%02x%02x", a1->board_id,
+			"0x%02x%02x/0x%02x%02x", a1->chain_id,
 			ret[0], ret[1], a1->spi_tx[0], a1->spi_tx[1]);
 		return NULL;
 	}
@@ -354,25 +274,6 @@ static uint8_t *cmd_WRITE_JOB(struct A1_chain *a1, uint8_t chip_id,
 }
 
 /********** A1 low level functions */
-static bool A1_hw_reset(void)
-{
-	/*
-	 * TODO: issue cold reset
-	 *
-	 * NOTE: suggested sequence
-	 * a) reset the RSTN pin for at least 1s
-	 * b) release the RSTN pin
-	 * c) wait at least 1s before sending the first CMD
-	 */
-	if (!a1_board_selector_init()) {
-		applog(LOG_ERR, "Failed to init board selector");
-		a1_board_selector_exit();
-		return false;
-	}
-	a1_board_selector_reset_all_boards();
-	return true;
-}
-
 #define MAX_PLL_WAIT_CYCLES 25
 #define PLL_CYCLE_WAIT_TIME 40
 static bool check_chip_pll_lock(struct A1_chain *a1, int chip_id, uint8_t *wr)
@@ -386,7 +287,7 @@ static bool check_chip_pll_lock(struct A1_chain *a1, int chip_id, uint8_t *wr)
 
 		cgsleep_ms(PLL_CYCLE_WAIT_TIME);
 	}
-	applog(LOG_ERR, "%d: chip %d failed PLL lock", a1->board_id, chip_id);
+	applog(LOG_ERR, "%d: chip %d failed PLL lock", a1->chain_id, chip_id);
 	return false;
 }
 
@@ -394,12 +295,7 @@ static uint8_t *get_pll_reg(struct A1_chain *a1, int ref_clock_khz,
 			    int sys_clock_khz)
 {
 	/*
-	 * TODO: this is only an initial approach with binary adjusted
-	 * dividers and thus not exploiting the whole divider range.
-	 *
-	 * If required, the algorithm can be adapted to find the PLL
-	 * parameters after:
-	 *
+	 * PLL parameters after:
 	 * sys_clk = (ref_clk * pll_fbdiv) / (pll_prediv * 2^(pll_postdiv - 1))
 	 *
 	 * with a higher pll_postdiv being desired over a higher pll_prediv
@@ -408,27 +304,49 @@ static uint8_t *get_pll_reg(struct A1_chain *a1, int ref_clock_khz,
 	static uint8_t writereg[6] = { 0x00, 0x00, 0x21, 0x84, };
 	uint8_t pre_div = 1;
 	uint8_t post_div = 1;
-	uint32_t fb_div = sys_clock_khz / ref_clock_khz;
-	int bid = a1->board_id;
+	uint32_t fb_div;
+
+	int cid = a1->chain_id;
 
 	applog(LOG_WARNING, "%d: Setting PLL: CLK_REF=%dMHz, SYS_CLK=%dMHz",
-	       bid, ref_clock_khz / 1000, sys_clock_khz / 1000);
-
-	while (fb_div > 511) {
-		if (post_div < 4)
-			post_div++;
-		else
-			pre_div <<= 1;
-		fb_div >>= 1;
+	       cid, ref_clock_khz / 1000, sys_clock_khz / 1000);
+
+	/* Euclidean search for GCD */
+	int a = ref_clock_khz;
+	int b = sys_clock_khz;
+	while (b != 0) {
+		int h = a % b;
+		a = b;
+		b = h;
 	}
+	fb_div = sys_clock_khz / a;
+	int n = ref_clock_khz / a;
+	/* approximate multiplier if not exactly matchable */
+	if (fb_div > 511) {
+		int f = fb_div / n;
+		int m = (f < 32) ? 16 : (f < 64) ? 8 :
+			(f < 128) ? 4 : (256 < 2) ? 2 : 1;
+		fb_div = m * fb_div / n;
+		n =  m;
+	}
+	/* try to maximize post divider */
+	if ((n & 3) == 0)
+		post_div = 3;
+	else if ((n & 1) == 0)
+		post_div = 2;
+	else
+		post_div = 1;
+	/* remainder goes to pre_div */
+	pre_div = n / (1 << (post_div - 1));
+	/* correct pre_div overflow */
 	if (pre_div > 31) {
-		applog(LOG_WARNING, "%d: can't set PLL parameters", bid);
-		return NULL;
+		fb_div = 31 * fb_div / pre_div;
+		pre_div = 31;
 	}
 	writereg[0] = (post_div << 6) | (pre_div << 1) | (fb_div >> 8);
 	writereg[1] = fb_div & 0xff;
-	applog(LOG_WARNING, "%d: setting PLL: pre_div=%d, post_div=%d, fb_div=%d"
-	       ": 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x", bid,
+	applog(LOG_WARNING, "%d: setting PLL: pre_div=%d, post_div=%d, "
+	       "fb_div=%d: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x", cid,
 	       pre_div, post_div, fb_div,
 	       writereg[0], writereg[1], writereg[2],
 	       writereg[3], writereg[4], writereg[5]);
@@ -444,20 +362,15 @@ static bool set_pll_config(struct A1_chain *a1, int chip_id,
 	if (!cmd_WRITE_REG(a1, chip_id, writereg))
 		return false;
 
-	int from, to;
-	if (chip_id == 0) {
-		from = 0;
-		to = a1->num_active_chips;
-	} else {
-		from = chip_id - 1;
-		to = chip_id - 1;
-	}
+	int from = (chip_id == 0) ? 0 : chip_id - 1;
+	int to = (chip_id == 0) ? a1->num_active_chips : chip_id - 1;
+
 	int i;
 	for (i = from; i < to; i++) {
 		int cid = i + 1;
 		if (!check_chip_pll_lock(a1, chip_id, writereg)) {
 			applog(LOG_ERR, "%d: chip %d failed PLL lock",
-			       a1->board_id, cid);
+			       a1->chain_id, cid);
 			return false;
 		}
 	}
@@ -471,10 +384,10 @@ static bool set_pll_config(struct A1_chain *a1, int chip_id,
 static bool check_chip(struct A1_chain *a1, int i)
 {
 	int chip_id = i + 1;
-	int bid = a1->board_id;
+	int cid = a1->chain_id;
 	if (!cmd_READ_REG(a1, chip_id)) {
 		applog(LOG_WARNING, "%d: Failed to read register for "
-		       "chip %d -> disabling", bid, chip_id);
+		       "chip %d -> disabling", cid, chip_id);
 		a1->chips[i].num_cores = 0;
 		a1->chips[i].disabled = 1;
 		return false;;
@@ -482,12 +395,12 @@ static bool check_chip(struct A1_chain *a1, int i)
 	a1->chips[i].num_cores = a1->spi_rx[7];
 	a1->num_cores += a1->chips[i].num_cores;
 	applog(LOG_WARNING, "%d: Found chip %d with %d active cores",
-	       bid, chip_id, a1->chips[i].num_cores);
+	       cid, chip_id, a1->chips[i].num_cores);
 	if (a1->chips[i].num_cores < BROKEN_CHIP_THRESHOLD) {
 		applog(LOG_WARNING, "%d: broken chip %d with %d active "
-		       "cores (threshold = %d)", bid, chip_id,
+		       "cores (threshold = %d)", cid, chip_id,
 		       a1->chips[i].num_cores, BROKEN_CHIP_THRESHOLD);
-		set_pll_config(a1, chip_id, config_options.ref_clk_khz,
+		set_pll_config(a1, chip_id, A1_config_options.ref_clk_khz,
 				BROKEN_CHIP_SYS_CLK);
 		cmd_READ_REG(a1, chip_id);
 		hexdump_error("new.PLL", a1->spi_rx, 8);
@@ -498,9 +411,9 @@ static bool check_chip(struct A1_chain *a1, int i)
 
 	if (a1->chips[i].num_cores < WEAK_CHIP_THRESHOLD) {
 		applog(LOG_WARNING, "%d: weak chip %d with %d active "
-		       "cores (threshold = %d)", bid,
+		       "cores (threshold = %d)", cid,
 		       chip_id, a1->chips[i].num_cores, WEAK_CHIP_THRESHOLD);
-		set_pll_config(a1, chip_id, config_options.ref_clk_khz,
+		set_pll_config(a1, chip_id, A1_config_options.ref_clk_khz,
 			       WEAK_CHIP_SYS_CLK);
 		cmd_READ_REG(a1, chip_id);
 		hexdump_error("new.PLL", a1->spi_rx, 8);
@@ -527,7 +440,7 @@ static int chain_detect(struct A1_chain *a1)
 	hexdump("RX", a1->spi_rx, 6);
 
 	int i;
-	int bid = a1->board_id;
+	int cid = a1->chain_id;
 	int max_poll_words = MAX_CHAIN_LENGTH * 2;
 	for(i = 1; i < max_poll_words; i++) {
 		if (a1->spi_rx[0] == A1_BIST_START && a1->spi_rx[1] == 0) {
@@ -537,12 +450,12 @@ static int chain_detect(struct A1_chain *a1)
 			a1->num_chips = (i / 2) + 1;
 			if (a1->num_chips != n) {
 				applog(LOG_ERR, "%d: enumeration: %d <-> %d",
-				       bid, a1->num_chips, n);
+				       cid, a1->num_chips, n);
 				if (n != 0)
 					a1->num_chips = n;
 			}
 			applog(LOG_WARNING, "%d: detected %d chips",
-			       bid, a1->num_chips);
+			       cid, a1->num_chips);
 			return a1->num_chips;
 		}
 		bool s = spi_transfer(a1->spi_ctx, NULL, a1->spi_rx, 2);
@@ -550,7 +463,7 @@ static int chain_detect(struct A1_chain *a1)
 		if (!s)
 			return 0;
 	}
-	applog(LOG_WARNING, "%d: no A1 chip-chain detected", bid);
+	applog(LOG_WARNING, "%d: no A1 chip-chain detected", cid);
 	return 0;
 }
 
@@ -573,13 +486,13 @@ static void disable_chip(struct A1_chain *a1, uint8_t chip_id)
 {
 	flush_spi(a1);
 	struct A1_chip *chip = &a1->chips[chip_id - 1];
-	int bid = a1->board_id;
+	int cid = a1->chain_id;
 	if (is_chip_disabled(a1, chip_id)) {
 		applog(LOG_WARNING, "%d: chip %d already disabled",
-		       bid, chip_id);
+		       cid, chip_id);
 		return;
 	}
-	applog(LOG_WARNING, "%d: temporary disabling chip %d", bid, chip_id);
+	applog(LOG_WARNING, "%d: temporary disabling chip %d", cid, chip_id);
 	chip->cooldown_begin = get_current_ms();
 }
 
@@ -587,7 +500,7 @@ static void disable_chip(struct A1_chain *a1, uint8_t chip_id)
 void check_disabled_chips(struct A1_chain *a1)
 {
 	int i;
-	int bid = a1->board_id;
+	int cid = a1->chain_id;
 	for (i = 0; i < a1->num_active_chips; i++) {
 		int chip_id = i + 1;
 		struct A1_chip *chip = &a1->chips[i];
@@ -601,11 +514,11 @@ void check_disabled_chips(struct A1_chain *a1)
 		if (!cmd_READ_REG(a1, chip_id)) {
 			chip->fail_count++;
 			applog(LOG_WARNING, "%d: chip %d not yet working - %d",
-			       bid, chip_id, chip->fail_count);
+			       cid, chip_id, chip->fail_count);
 			if (chip->fail_count > DISABLE_CHIP_FAIL_THRESHOLD) {
 				applog(LOG_WARNING,
 				       "%d: completely disabling chip %d at %d",
-				       bid, chip_id, chip->fail_count);
+				       cid, chip_id, chip->fail_count);
 				chip->disabled = true;
 				a1->num_cores -= chip->num_cores;
 				continue;
@@ -615,7 +528,7 @@ void check_disabled_chips(struct A1_chain *a1)
 			continue;
 		}
 		applog(LOG_WARNING, "%d: chip %d is working again",
-		       bid, chip_id);
+		       cid, chip_id);
 		chip->cooldown_begin = 0;
 		chip->fail_count = 0;
 	}
@@ -672,7 +585,9 @@ static uint8_t *create_job(uint8_t chip_id, uint8_t job_id, struct work *work)
 	p1[0] = bswap_32(p2[0]);
 	p1[1] = bswap_32(p2[1]);
 	p1[2] = bswap_32(p2[2]);
+#ifdef USE_REAL_DIFF
 	p1[4] = get_diff(work->sdiff);
+#endif
 	return job;
 }
 
@@ -680,17 +595,17 @@ static uint8_t *create_job(uint8_t chip_id, uint8_t job_id, struct work *work)
 static bool set_work(struct A1_chain *a1, uint8_t chip_id, struct work *work,
 		     uint8_t queue_states)
 {
-	int bid = a1->board_id;
+	int cid = a1->chain_id;
 	struct A1_chip *chip = &a1->chips[chip_id - 1];
 	bool retval = false;
 
 	int job_id = chip->last_queued_id + 1;
 
 	applog(LOG_INFO, "%d: queuing chip %d with job_id %d, state=0x%02x",
-	       bid, chip_id, job_id, queue_states);
+	       cid, chip_id, job_id, queue_states);
 	if (job_id == (queue_states & 0x0f) || job_id == (queue_states >> 4))
 		applog(LOG_WARNING, "%d: job overlap: %d, 0x%02x",
-		       bid, job_id, queue_states);
+		       cid, job_id, queue_states);
 
 	if (chip->work[chip->last_queued_id] != NULL) {
 		work_completed(a1->cgpu, chip->work[chip->last_queued_id]);
@@ -703,8 +618,8 @@ static bool set_work(struct A1_chain *a1, uint8_t chip_id, struct work *work,
 		work_completed(a1->cgpu, work);
 
 		applog(LOG_ERR, "%d: failed to set work for chip %d.%d",
-		       bid, chip_id, job_id);
-		// TODO: what else?
+		       cid, chip_id, job_id);
+		disable_chip(a1, chip_id);
 	} else {
 		chip->work[chip->last_queued_id] = work;
 		chip->last_queued_id++;
@@ -720,7 +635,7 @@ static bool get_nonce(struct A1_chain *a1, uint8_t *nonce,
 	if (ret == NULL)
 		return false;
 	if (ret[1] == 0) {
-		applog(LOG_DEBUG, "%d: output queue empty", a1->board_id);
+		applog(LOG_DEBUG, "%d: output queue empty", a1->chain_id);
 		return false;
 	}
 	*job_id = ret[0] >> 4;
@@ -743,21 +658,20 @@ void exit_A1_chain(struct A1_chain *a1)
 		return;
 	free(a1->chips);
 	a1->chips = NULL;
-	spi_exit(a1->spi_ctx);
 	a1->spi_ctx = NULL;
 	free(a1);
 }
 
-struct A1_chain *init_A1_chain(struct spi_ctx *ctx, int board_id)
+struct A1_chain *init_A1_chain(struct spi_ctx *ctx, int chain_id)
 {
 	int i;
 	struct A1_chain *a1 = malloc(sizeof(*a1));
 	assert(a1 != NULL);
 
-	applog(LOG_DEBUG, "%d: A1 init chain", a1->board_id);
+	applog(LOG_DEBUG, "%d: A1 init chain", a1->chain_id);
 	memset(a1, 0, sizeof(*a1));
 	a1->spi_ctx = ctx;
-	a1->board_id = board_id;
+	a1->chain_id = chain_id;
 
 	a1->num_chips = chain_detect(a1);
 	if (a1->num_chips == 0)
@@ -765,19 +679,19 @@ struct A1_chain *init_A1_chain(struct spi_ctx *ctx, int board_id)
 
 	applog(LOG_WARNING, "spidev%d.%d: %d: Found %d A1 chips",
 	       a1->spi_ctx->config.bus, a1->spi_ctx->config.cs_line,
-	       a1->board_id, a1->num_chips);
+	       a1->chain_id, a1->num_chips);
 
-	if (!set_pll_config(a1, 0, config_options.ref_clk_khz,
-			    config_options.sys_clk_khz))
+	if (!set_pll_config(a1, 0, A1_config_options.ref_clk_khz,
+			    A1_config_options.sys_clk_khz))
 		goto failure;
 
 	/* override max number of active chips if requested */
 	a1->num_active_chips = a1->num_chips;
-	if (config_options.override_chip_num > 0 &&
-	    a1->num_chips > config_options.override_chip_num) {
-		a1->num_active_chips = config_options.override_chip_num;
+	if (A1_config_options.override_chip_num > 0 &&
+	    a1->num_chips > A1_config_options.override_chip_num) {
+		a1->num_active_chips = A1_config_options.override_chip_num;
 		applog(LOG_WARNING, "%d: limiting chain to %d chips",
-		       a1->board_id, a1->num_active_chips);
+		       a1->chain_id, a1->num_active_chips);
 	}
 
 	a1->chips = calloc(a1->num_active_chips, sizeof(struct A1_chip));
@@ -790,7 +704,7 @@ struct A1_chain *init_A1_chain(struct spi_ctx *ctx, int board_id)
 		check_chip(a1, i);
 
 	applog(LOG_WARNING, "%d: found %d chips with total %d active cores",
-	       a1->board_id, a1->num_active_chips, a1->num_cores);
+	       a1->chain_id, a1->num_active_chips, a1->num_cores);
 
 	mutex_init(&a1->lock);
 	INIT_LIST_HEAD(&a1->active_wq.head);
@@ -802,55 +716,127 @@ failure:
 	return NULL;
 }
 
-static bool A1_detect_one_chain(struct spi_config *cfg)
+
+bool detect_coincraft_desk(void)
 {
-	struct cgpu_info *cgpu;
-	int board_id;
+	static const uint8_t mcp4x_mapping[] = { 0x2c, 0x2b, 0x2a, 0x29, 0x28 };
+	board_selector = ccd_board_selector_init();
+	if (board_selector == NULL) {
+		applog(LOG_ERR, "No CoinCrafd Desk backplane detected.");
+		return false;
+	}
+	board_selector->reset_all();
 
-	for (board_id = 0; board_id < MAX_BOARDS; board_id++) {
-		struct spi_ctx *ctx = spi_init(cfg);
+	struct spi_config cfg = default_spi_config;
+	cfg.mode = SPI_MODE_1;
+	cfg.speed = A1_config_options.spi_clk_khz * 1000;
+	struct spi_ctx *spi = spi_init(&cfg);
+	if (spi == NULL)
+		return false;
 
-		if (ctx == NULL)
-			return false;
+	int boards_detected = 0;
+	int board_id;
+	for (board_id = 0; board_id < CCD_MAX_CHAINS; board_id++) {
+		uint8_t mcp_slave = mcp4x_mapping[board_id];
+		struct mcp4x *mcp = mcp4x_init(mcp_slave);
+		if (mcp == NULL)
+			continue;
+
+		if (A1_config_options.wiper != 0)
+			mcp->set_wiper(mcp, 0, A1_config_options.wiper);
 
 		applog(LOG_WARNING, "checking board %d...", board_id);
-		a1_board_selector_select_board(board_id);
-//		a1_board_selector_reset_board();
+		board_selector->select(board_id);
+
+		struct A1_chain *a1 = init_A1_chain(spi, board_id);
+		board_selector->release();
+		if (a1 == NULL)
+			continue;
+
+		struct cgpu_info *cgpu = malloc(sizeof(*cgpu));
+		assert(cgpu != NULL);
+
+		memset(cgpu, 0, sizeof(*cgpu));
+		cgpu->drv = &bitmineA1_drv;
+		cgpu->name = "BitmineA1.CCD";
+		cgpu->threads = 1;
+
+		cgpu->device_data = a1;
+
+		a1->cgpu = cgpu;
+		a1->trimpot = mcp;
+		add_cgpu(cgpu);
+		boards_detected++;
+	}
+	if (boards_detected == 0) {
+		spi_exit(spi);
+		return false;
+	}
+	applog(LOG_WARNING, "Detected CoinCraft Desk with %d boards",
+	       boards_detected);
+	return true;
+}
+
+bool detect_coincraft_rig_v3(void)
+{
+	board_selector = ccr_board_selector_init();
+	if (board_selector == NULL)
+		return false;
+
+	struct spi_config cfg = default_spi_config;
+	cfg.mode = SPI_MODE_1;
+	cfg.speed = A1_config_options.spi_clk_khz * 1000;
+	struct spi_ctx *spi[2];
+	spi[0]= spi_init(&cfg);
+	cfg.cs_line = 1;
+	spi[1]= spi_init(&cfg);
 
-		struct A1_chain *a1 = init_A1_chain(ctx, board_id);
-		unlock_board_selector();
+	if (spi[0] == NULL || spi[1] == 0)
+		return false;
+
+
+	board_selector->reset_all();
+	int chains_detected = 0;
+	int c;
+	for (c = 0; c < CCR_MAX_CHAINS; c++) {
+		applog(LOG_WARNING, "checking RIG chain %d...", c);
+
+		board_selector->select(c);
+		struct A1_chain *a1 = init_A1_chain(spi[c & 1], c);
+		board_selector->release();
 		if (a1 == NULL)
 			continue;
 
-		cgpu = malloc(sizeof(*cgpu));
+		struct cgpu_info *cgpu = malloc(sizeof(*cgpu));
 		assert(cgpu != NULL);
 
 		memset(cgpu, 0, sizeof(*cgpu));
 		cgpu->drv = &bitmineA1_drv;
-		cgpu->name = "BitmineA1";
+		cgpu->name = "BitmineA1.CCR";
 		cgpu->threads = 1;
 
 		cgpu->device_data = a1;
 
 		a1->cgpu = cgpu;
 		add_cgpu(cgpu);
+		chains_detected++;
 	}
+	if (chains_detected == 0)
+		return false;
+
+	applog(LOG_WARNING, "Detected CoinCraft Rig with %d chains",
+	       chains_detected);
 	return true;
 }
 
-#define MAX_SPI_BUS	1
-#define MAX_SPI_CS	1
 /* Probe SPI channel and register chip chain */
 void A1_detect(bool hotplug)
 {
-	int bus;
-	int cs_line;
-	int board_id;
-
 	/* no hotplug support for now */
 	if (hotplug)
 		return;
 
+	/* parse bimine-a1-options */
 	if (opt_bitmine_a1_options != NULL && parsed_config_options == NULL) {
 		int ref_clk = 0;
 		int sys_clk = 0;
@@ -860,32 +846,26 @@ void A1_detect(bool hotplug)
 		sscanf(opt_bitmine_a1_options, "%d:%d:%d:%d",
 		       &ref_clk, &sys_clk, &spi_clk,  &override_chip_num);
 		if (ref_clk != 0)
-			config_options.ref_clk_khz = ref_clk;
+			A1_config_options.ref_clk_khz = ref_clk;
 		if (sys_clk != 0)
-			config_options.sys_clk_khz = sys_clk;
+			A1_config_options.sys_clk_khz = sys_clk;
 		if (spi_clk != 0)
-			config_options.spi_clk_khz = spi_clk;
+			A1_config_options.spi_clk_khz = spi_clk;
 		if (override_chip_num != 0)
-			config_options.override_chip_num = override_chip_num;
+			A1_config_options.override_chip_num = override_chip_num;
 
 		/* config options are global, scan them once */
-		parsed_config_options = &config_options;
+		parsed_config_options = &A1_config_options;
 	}
-
 	applog(LOG_DEBUG, "A1 detect");
-	A1_hw_reset();
-	for (bus = 0; bus < MAX_SPI_BUS; bus++) {
-		for (cs_line = 0; cs_line < MAX_SPI_CS; cs_line++) {
-			struct spi_config cfg = default_spi_config;
-			cfg.mode = SPI_MODE_1;
-			cfg.speed = config_options.spi_clk_khz * 1000;
-			cfg.bus = bus;
-			cfg.cs_line = cs_line;
-			A1_detect_one_chain(&cfg);
-		}
-	}
+	/* detect and register supported products */
+	if (detect_coincraft_desk())
+		return;
+	if (detect_coincraft_rig_v3())
+		return;
 }
 
+#define TEMP_UPDATE_INT_MS	2000
 static int64_t A1_scanwork(struct thr_info *thr)
 {
 	int i;
@@ -893,7 +873,7 @@ static int64_t A1_scanwork(struct thr_info *thr)
 	struct A1_chain *a1 = cgpu->device_data;
 	int32_t nonce_ranges_processed = 0;
 
-	a1_board_selector_select_board(a1->board_id);
+	board_selector->select(a1->chain_id);
 
 	applog(LOG_DEBUG, "A1 running scanwork");
 
@@ -904,26 +884,25 @@ static int64_t A1_scanwork(struct thr_info *thr)
 
 	mutex_lock(&a1->lock);
 
-	int bid = a1->board_id;
+	if (a1->last_temp_time + TEMP_UPDATE_INT_MS < get_current_ms()) {
+		a1->temp = board_selector->get_temp();
+		a1->last_temp_time = get_current_ms();
+	}
+	int cid = a1->chain_id;
 	/* poll queued results */
 	while (true) {
-		int res = get_nonce(a1, (uint8_t*)&nonce, &chip_id, &job_id);
-		if (res < 0) {
-			flush_spi(a1);
-			break;
-		}
-		if (res == 0)
+		if (!get_nonce(a1, (uint8_t*)&nonce, &chip_id, &job_id))
 			break;
 		nonce = bswap_32(nonce);
 		work_updated = true;
 		if (chip_id < 1 || chip_id > a1->num_active_chips) {
 			applog(LOG_WARNING, "%d: wrong chip_id %d",
-			       bid, chip_id);
+			       cid, chip_id);
 			continue;
 		}
 		if (job_id < 1 && job_id > 4) {
-			applog(LOG_WARNING, "%d: chip %d: result has wrong job_id %d",
-			       bid, chip_id, job_id);
+			applog(LOG_WARNING, "%d: chip %d: result has wrong "
+			       "job_id %d", cid, chip_id, job_id);
 			flush_spi(a1);
 			continue;
 		}
@@ -933,20 +912,20 @@ static int64_t A1_scanwork(struct thr_info *thr)
 		if (work == NULL) {
 			/* already been flushed => stale */
 			applog(LOG_WARNING, "%d: chip %d: stale nonce 0x%08x",
-			       bid, chip_id, nonce);
+			       cid, chip_id, nonce);
 			chip->stales++;
 			continue;
 		}
 		if (!submit_nonce(thr, work, nonce)) {
 			applog(LOG_WARNING, "%d: chip %d: invalid nonce 0x%08x",
-			       bid, chip_id, nonce);
+			       cid, chip_id, nonce);
 			chip->hw_errors++;
 			/* add a penalty of a full nonce range on HW errors */
 			nonce_ranges_processed--;
 			continue;
 		}
 		applog(LOG_DEBUG, "YEAH: %d: chip %d / job_id %d: nonce 0x%08x",
-		       bid, chip_id, job_id, nonce);
+		       cid, chip_id, job_id, nonce);
 		chip->nonces_found++;
 	}
 
@@ -968,7 +947,7 @@ static int64_t A1_scanwork(struct thr_info *thr)
 			continue;
 		case 2:
 			applog(LOG_ERR, "%d: chip %d: invalid state = 2",
-			       bid, c);
+			       cid, c);
 			continue;
 		case 1:
 			/* fall through */
@@ -978,7 +957,7 @@ static int64_t A1_scanwork(struct thr_info *thr)
 			work = wq_dequeue(&a1->active_wq);
 			if (work == NULL) {
 				applog(LOG_ERR, "%d: chip %d: work underflow",
-				       bid, c);
+				       cid, c);
 				break;
 			}
 			if (set_work(a1, c, work, qbuff)) {
@@ -986,7 +965,7 @@ static int64_t A1_scanwork(struct thr_info *thr)
 				nonce_ranges_processed++;
 			}
 			applog(LOG_DEBUG, "%d: chip %d: job done: %d/%d/%d/%d",
-			       bid, c,
+			       cid, c,
 			       chip->nonce_ranges_done, chip->nonces_found,
 			       chip->hw_errors, chip->stales);
 			break;
@@ -995,18 +974,18 @@ static int64_t A1_scanwork(struct thr_info *thr)
 	check_disabled_chips(a1);
 	mutex_unlock(&a1->lock);
 
-	unlock_board_selector();
+	board_selector->release();
 
 	if (nonce_ranges_processed < 0)
 		nonce_ranges_processed = 0;
 
 	if (nonce_ranges_processed != 0) {
 		applog(LOG_DEBUG, "%d, nonces processed %d",
-		       bid, nonce_ranges_processed);
+		       cid, nonce_ranges_processed);
 	}
 	/* in case of no progress, prevent busy looping */
 	if (!work_updated)
-		cgsleep_ms(160);
+		cgsleep_ms(40);
 
 	return (int64_t)nonce_ranges_processed << 32;
 }
@@ -1017,11 +996,10 @@ static bool A1_queue_full(struct cgpu_info *cgpu)
 {
 	struct A1_chain *a1 = cgpu->device_data;
 	int queue_full = false;
-	struct work *work;
 
 	mutex_lock(&a1->lock);
 	applog(LOG_DEBUG, "%d, A1 running queue_full: %d/%d",
-	       a1->board_id, a1->active_wq.num_elems, a1->num_active_chips);
+	       a1->chain_id, a1->active_wq.num_elems, a1->num_active_chips);
 
 	if (a1->active_wq.num_elems >= a1->num_active_chips * 2)
 		queue_full = true;
@@ -1036,17 +1014,17 @@ static bool A1_queue_full(struct cgpu_info *cgpu)
 static void A1_flush_work(struct cgpu_info *cgpu)
 {
 	struct A1_chain *a1 = cgpu->device_data;
-	int bid = a1->board_id;
-	a1_board_selector_select_board(bid);
+	int cid = a1->chain_id;
+	board_selector->select(cid);
 
-	applog(LOG_DEBUG, "%d: A1 running flushwork", bid);
+	applog(LOG_DEBUG, "%d: A1 running flushwork", cid);
 
 	int i;
 
 	mutex_lock(&a1->lock);
 	/* stop chips hashing current work */
 	if (!abort_work(a1)) {
-		applog(LOG_ERR, "%d: failed to abort work in chip chain!", bid);
+		applog(LOG_ERR, "%d: failed to abort work in chip chain!", cid);
 	}
 	/* flush the work chips were currently hashing */
 	for (i = 0; i < a1->num_active_chips; i++) {
@@ -1057,14 +1035,14 @@ static void A1_flush_work(struct cgpu_info *cgpu)
 			if (work == NULL)
 				continue;
 			applog(LOG_DEBUG, "%d: flushing chip %d, work %d: 0x%p",
-			       bid, i, j + 1, work);
+			       cid, i, j + 1, work);
 			work_completed(cgpu, work);
 			chip->work[j] = NULL;
 		}
 		chip->last_queued_id = 0;
 	}
 	/* flush queued work */
-	applog(LOG_DEBUG, "%d: flushing queued work...", bid);
+	applog(LOG_DEBUG, "%d: flushing queued work...", cid);
 	while (a1->active_wq.num_elems > 0) {
 		struct work *work = wq_dequeue(&a1->active_wq);
 		assert(work != NULL);
@@ -1072,14 +1050,19 @@ static void A1_flush_work(struct cgpu_info *cgpu)
 	}
 	mutex_unlock(&a1->lock);
 
-	unlock_board_selector();
+	board_selector->release();
 }
 
-static void A1_get_statline_before(char *buf, size_t len, struct cgpu_info *cgpu)
+static void A1_get_statline_before(char *buf, size_t len,
+				   struct cgpu_info *cgpu)
 {
 	struct A1_chain *a1 = cgpu->device_data;
-	tailsprintf(buf, len, " %2d:%2d/%3d ",
-		    a1->board_id, a1->num_active_chips, a1->num_cores);
+	char temp[10];
+	if (a1->temp != 0)
+		snprintf(temp, 9, "%2dC", a1->temp);
+	tailsprintf(buf, len, " %2d:%2d/%3d %s",
+		    a1->chain_id, a1->num_active_chips, a1->num_cores,
+		    a1->temp == 0 ? "   " : temp);
 }
 
 struct device_drv bitmineA1_drv = {