Commit 114f9fd223efb5008fbff581389b1d4e0f6054e6

Zefir Kurtisi 2014-06-03T18:47:29

A1: add support for updated product variants, small fixes This patch includes: * generalize I2C slave access * adapt CoinCraft Rig board selector to new design * add support for single chip chains * adapt verbosity levels * check for chip clocks below 100MHz

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
diff --git a/A1-board-selector-CCD.c b/A1-board-selector-CCD.c
index d9c8528..d0e173f 100644
--- a/A1-board-selector-CCD.c
+++ b/A1-board-selector-CCD.c
@@ -26,69 +26,66 @@
 #include "miner.h"
 
 #include "A1-board-selector.h"
+#include "i2c-context.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;
-};
+struct i2c_ctx *U1_tca9535;
+uint8_t chain_mask = 0xff;
+uint8_t active_chain = 255;
+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);
+	mutex_unlock(&lock);
 }
 
 static void ccd_exit(void)
 {
-	i2c_slave_close(&ccd_ctx.U1_tca9535);
+	if (U1_tca9535 != NULL)
+		U1_tca9535->exit(U1_tca9535);
 }
+uint8_t retval = 0;
 
 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);
+	mutex_init(&lock);
+	U1_tca9535 = i2c_slave_open(I2C_BUS, 0x27);
+	if (U1_tca9535 == NULL)
+		return NULL;
+	bool retval =	U1_tca9535->write(U1_tca9535, 0x06, 0xe0) &&
+			U1_tca9535->write(U1_tca9535, 0x07, 0xe0) &&
+			U1_tca9535->write(U1_tca9535, 0x02, 0x1f) &&
+			U1_tca9535->write(U1_tca9535, 0x03, 0x00);
 	if (retval)
 		return &ccd_selector;
 	ccd_exit();
 	return NULL;
 }
 
-static bool ccd_select(uint8_t board)
+static bool ccd_select(uint8_t chain)
 {
-	if (board >= CCD_MAX_CHAINS)
+	if (chain >= CCD_MAX_CHAINS)
 		return false;
 
-	mutex_lock(&ccd_ctx.lock);
-	if (ccd_ctx.active_board == board)
+	mutex_lock(&lock);
+	if (active_chain == chain)
 		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);
+	active_chain = chain;
+	chain_mask = 1 << active_chain;
+	return U1_tca9535->write(U1_tca9535, 0x02, ~chain_mask);
 }
 
 static bool __ccd_board_selector_reset(uint8_t mask)
 {
-	if (!i2c_slave_write(&ccd_ctx.U1_tca9535, 0x03, mask))
+	if (!U1_tca9535->write(U1_tca9535, 0x03, mask))
 		return false;
 	cgsleep_ms(RESET_LOW_TIME_MS);
-	if (!i2c_slave_write(&ccd_ctx.U1_tca9535, 0x03, 0x00))
+	if (!U1_tca9535->write(U1_tca9535, 0x03, 0x00))
 		return false;
 	cgsleep_ms(RESET_HI_TIME_MS);
 	return true;
@@ -96,14 +93,14 @@ static bool __ccd_board_selector_reset(uint8_t mask)
 // we assume we are already holding the mutex
 static bool ccd_reset(void)
 {
-	return __ccd_board_selector_reset(ccd_ctx.board_mask);
+	return __ccd_board_selector_reset(chain_mask);
 }
 
 static bool ccd_reset_all(void)
 {
-	mutex_lock(&ccd_ctx.lock);
+	mutex_lock(&lock);
 	bool retval = __ccd_board_selector_reset(0xff & ~UNUSED_BITS);
-	mutex_unlock(&ccd_ctx.lock);
+	mutex_unlock(&lock);
 	return retval;
 }
 
@@ -114,6 +111,7 @@ static struct board_selector ccd_selector = {
 	.exit = ccd_exit,
 	.reset = ccd_reset,
 	.reset_all = ccd_reset_all,
-	.get_temp = dummy_u8,
+	/* don't have a temp sensor dedicated to chain */
+	.get_temp = dummy_get_temp,
 };
 
diff --git a/A1-board-selector-CCR.c b/A1-board-selector-CCR.c
index b60d58e..1752d3c 100644
--- a/A1-board-selector-CCR.c
+++ b/A1-board-selector-CCR.c
@@ -10,221 +10,90 @@
  */
 
 
-#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"
+#include "i2c-context.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 board_selector ccr_selector;
 
-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,
-};
+static struct i2c_ctx *U1_tca9548;
+static struct i2c_ctx *U3_tca9535;
+static struct i2c_ctx *U4_tca9535;
+static uint8_t active_chain;
+static pthread_mutex_t lock;
 
 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, },
+	{  0, 0x01, 0x01, 0x00, },
+	{  1, 0x01, 0x00, 0x80, },
+	{  2, 0x02, 0x02, 0x00, },
+	{  3, 0x02, 0x00, 0x40, },
+	{  4, 0x04, 0x04, 0x00, },
+	{  5, 0x04, 0x00, 0x20, },
+	{  6, 0x08, 0x08, 0x00, },
+	{  7, 0x08, 0x00, 0x10, },
+	{  8, 0x10, 0x10, 0x00, },
+	{  9, 0x10, 0x00, 0x08, },
+	{ 10, 0x20, 0x20, 0x00, },
+	{ 11, 0x20, 0x00, 0x04, },
+	{ 12, 0x40, 0x40, 0x00, },
+	{ 13, 0x40, 0x00, 0x02, },
+	{ 14, 0x80, 0x80, 0x00, },
+	{ 15, 0x80, 0x00, 0x01, },
 };
 
 static void ccr_unlock(void)
 {
-	mutex_unlock(&ccr_ctx.lock);
+	mutex_unlock(&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);
+	if (U1_tca9548 != NULL)
+		U1_tca9548->exit(U1_tca9548);
+	if (U3_tca9535 != NULL)
+		U3_tca9535->exit(U3_tca9535);
+	if (U4_tca9535 != NULL)
+		U4_tca9535->exit(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);
+	mutex_init(&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) &&
+	/* detect all i2c slaves */
+	U1_tca9548 = i2c_slave_open(I2C_BUS, 0x70);
+	U3_tca9535 = i2c_slave_open(I2C_BUS, 0x23);
+	U4_tca9535 = i2c_slave_open(I2C_BUS, 0x22);
+	if (U1_tca9548 == NULL || U3_tca9535 == NULL || U4_tca9535 == NULL)
+		goto fail;
+
 			/* 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) &&
+	bool res =	U1_tca9548->write(U1_tca9548, 0x00, 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) &&
+			U3_tca9535->write(U3_tca9535, 0x06, 0x00) &&
+			U3_tca9535->write(U3_tca9535, 0x07, 0x00) &&
+			U3_tca9535->write(U3_tca9535, 0x02, 0x00) &&
+			U3_tca9535->write(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);
+			U4_tca9535->write(U4_tca9535, 0x06, 0x00) &&
+			U4_tca9535->write(U4_tca9535, 0x07, 0x00) &&
+			U4_tca9535->write(U4_tca9535, 0x02, 0x00) &&
+			U4_tca9535->write(U4_tca9535, 0x03, 0x00);
 
 	if (!res)
 		goto fail;
 
-	if (ccr_power_on_boards() == 0)
-		goto fail;
-
 	return &ccr_selector;
 
 fail:
@@ -237,29 +106,38 @@ 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)
+	mutex_lock(&lock);
+	if (active_chain == chain)
 		return true;
 
-	ccr_ctx.active_chain = chain;
+	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))
+
+	if (!U1_tca9548->write(U1_tca9548, cm->U1, cm->U1))
+		return false;
+
+	if (!U4_tca9535->write(U4_tca9535, 0x02, cm->U3p0) ||
+	    !U4_tca9535->write(U4_tca9535, 0x03, cm->U3p1))
 		return false;
 
+	/* sanity check: ensure i2c command has been written before we leave */
+	uint8_t tmp;
+	if (!U4_tca9535->read(U4_tca9535, 0x02, &tmp) || tmp != cm->U3p0) {
+		applog(LOG_ERR, "ccr_select: wrote 0x%02x, read 0x%02x",
+		       cm->U3p0, tmp);
+	}
 	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))
+	if (!U3_tca9535->write(U3_tca9535, 0x02, p0) ||
+	    !U3_tca9535->write(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))
+	if (!U3_tca9535->write(U3_tca9535, 0x02, 0x00) ||
+	    !U3_tca9535->write(U3_tca9535, 0x03, 0x00))
 		return false;
 	cgsleep_ms(RESET_HI_TIME_MS);
 	return true;
@@ -267,30 +145,33 @@ static bool __ccr_board_selector_reset(uint8_t p0, uint8_t p1)
 // we assume we are already holding the mutex
 static bool ccr_reset(void)
 {
-	const struct chain_mapping *cm = &chain_mapping[ccr_ctx.active_chain];
+	const struct chain_mapping *cm = &chain_mapping[active_chain];
+	applog(LOG_DEBUG, "resetting chain %d", cm->chain_id);
 	bool retval = __ccr_board_selector_reset(cm->U3p0, cm->U3p1);
 	return retval;
 }
 
 static bool ccr_reset_all(void)
 {
-	mutex_lock(&ccr_ctx.lock);
+	mutex_lock(&lock);
 	bool retval = __ccr_board_selector_reset(0xff, 0xff);
-	mutex_unlock(&ccr_ctx.lock);
+	mutex_unlock(&lock);
 	return retval;
 }
 
-static uint8_t ccr_get_temp(void)
+static uint8_t ccr_get_temp(uint8_t sensor_id)
 {
-	if (ccr_ctx.active_chain & 1)
+	if ((active_chain & 1) != 0 || sensor_id != 0)
+		return 0;
+
+	struct i2c_ctx *U7 = i2c_slave_open(I2C_BUS, 0x4c);
+	if (U7 == NULL)
 		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);
-	}
+	if (!U7->read(U7, 0, &retval))
+		retval = 0;
+	U7->exit(U7);
 	return retval;
 }
 
diff --git a/A1-board-selector.h b/A1-board-selector.h
index 9102f70..bcadf7d 100644
--- a/A1-board-selector.h
+++ b/A1-board-selector.h
@@ -8,26 +8,37 @@
 #define RESET_HI_TIME_MS  100
 
 struct board_selector {
-	bool (*select)(uint8_t board);
-	void (*release)(void);
+	/* destructor */
 	void (*exit)(void);
+	/* select board and chip chain for given chain index*/
+	bool (*select)(uint8_t chain);
+	/* release access to selected chain */
+	void (*release)(void);
+	/* reset currently selected chain */
 	bool (*reset)(void);
+	/* reset all chains on board */
 	bool (*reset_all)(void);
-	uint8_t (*get_temp)(void);
+	/* get temperature for selected chain at given sensor */
+	uint8_t (*get_temp)(uint8_t sensor);
+	/* prepare board (voltage) for given sys_clock */
+	bool (*prepare_clock)(int clock_khz);
 };
 
 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 uint8_t dummy_u8(void) { return 0; }
+static uint8_t dummy_get_temp(uint8_t s) { (void)s; return 0; }
+static bool dummy_prepare_clock(int c) { (void)c; return true; }
 
 static const struct board_selector dummy_board_selector = {
+	.exit = dummy_void,
 	.select = dummy_select,
 	.release = dummy_void,
-	.exit = dummy_void,
 	.reset = dummy_bool,
 	.reset_all = dummy_bool,
-	.get_temp = dummy_u8,
+	.get_temp = dummy_get_temp,
+	.prepare_clock = dummy_prepare_clock,
 };
 
 /* CoinCraft Desk and Rig board selector constructors */
@@ -36,19 +47,4 @@ static const struct board_selector dummy_board_selector = {
 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
index 27fd60d..b048a99 100644
--- a/A1-common.h
+++ b/A1-common.h
@@ -62,6 +62,8 @@ struct A1_chain {
 
 	struct work_queue active_wq;
 
+	/* mark chain disabled, do not try to re-enable it */
+	bool disabled;
 	uint8_t temp;
 	int last_temp_time;
 };
diff --git a/Makefile.am b/Makefile.am
index 184e599..49cc0f5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -89,6 +89,7 @@ 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
+cgminer_SOURCES += i2c-context.c i2c-context.h
 endif
 
 if HAS_DRILLBIT
diff --git a/driver-SPI-bitmine-A1.c b/driver-SPI-bitmine-A1.c
index 2ac50db..21d2545 100644
--- a/driver-SPI-bitmine-A1.c
+++ b/driver-SPI-bitmine-A1.c
@@ -25,8 +25,9 @@
 #include "A1-board-selector.h"
 #include "A1-trimpot-mcp4x.h"
 
-/* one global board_selector is enough */
+/* one global board_selector and spi context is enough */
 static struct board_selector *board_selector;
+static struct spi_ctx *spi;
 
 /********** work queue */
 static bool wq_enqueue(struct work_queue *wq, struct work *work)
@@ -152,7 +153,7 @@ static uint8_t *exec_cmd(struct A1_chain *a1,
 	int poll_len = resp_len;
 	if (chip_id == 0) {
 		if (a1->num_chips == 0) {
-			applog(LOG_ERR, "%d: unknown chips in chain, "
+			applog(LOG_INFO, "%d: unknown chips in chain, "
 			       "assuming 8", a1->chain_id);
 			poll_len += 32;
 		}
@@ -668,7 +669,7 @@ struct A1_chain *init_A1_chain(struct spi_ctx *ctx, int chain_id)
 	struct A1_chain *a1 = malloc(sizeof(*a1));
 	assert(a1 != NULL);
 
-	applog(LOG_DEBUG, "%d: A1 init chain", a1->chain_id);
+	applog(LOG_DEBUG, "%d: A1 init chain", chain_id);
 	memset(a1, 0, sizeof(*a1));
 	a1->spi_ctx = ctx;
 	a1->chain_id = chain_id;
@@ -716,24 +717,41 @@ failure:
 	return NULL;
 }
 
+static bool detect_single_chain(void)
+{
+	board_selector = (struct board_selector*)&dummy_board_selector;
+	applog(LOG_WARNING, "A1: checking single chain");
+	struct A1_chain *a1 = init_A1_chain(spi, 0);
+	if (a1 == NULL)
+		return false;
+
+	struct cgpu_info *cgpu = malloc(sizeof(*cgpu));
+	assert(cgpu != NULL);
+
+	memset(cgpu, 0, sizeof(*cgpu));
+	cgpu->drv = &bitmineA1_drv;
+	cgpu->name = "BitmineA1.SingleChain";
+	cgpu->threads = 1;
+
+	cgpu->device_data = a1;
+
+	a1->cgpu = cgpu;
+	add_cgpu(cgpu);
+	applog(LOG_WARNING, "Detected single A1 chain with %d chips / %d cores",
+	       a1->num_active_chips, a1->num_cores);
+	return true;
+}
 
 bool detect_coincraft_desk(void)
 {
 	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.");
+		applog(LOG_INFO, "No CoinCrafd Desk backplane detected.");
 		return false;
 	}
 	board_selector->reset_all();
 
-	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;
-
 	int boards_detected = 0;
 	int board_id;
 	for (board_id = 0; board_id < CCD_MAX_CHAINS; board_id++) {
@@ -764,14 +782,12 @@ bool detect_coincraft_desk(void)
 		cgpu->device_data = a1;
 
 		a1->cgpu = cgpu;
-		a1->trimpot = mcp;
 		add_cgpu(cgpu);
 		boards_detected++;
 	}
-	if (boards_detected == 0) {
-		spi_exit(spi);
+	if (boards_detected == 0)
 		return false;
-	}
+
 	applog(LOG_WARNING, "Detected CoinCraft Desk with %d boards",
 	       boards_detected);
 	return true;
@@ -783,30 +799,34 @@ bool detect_coincraft_rig_v3(void)
 	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);
-
-	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);
+		if (!board_selector->select(c))
+			continue;
+
+		struct A1_chain *a1 = init_A1_chain(spi, c);
 		board_selector->release();
+
 		if (a1 == NULL)
 			continue;
 
+		if (A1_config_options.wiper != 0 && (c & 1) == 0) {
+			struct mcp4x *mcp = mcp4x_init(0x28);
+			if (mcp == NULL) {
+				applog(LOG_ERR, "%d: Cant access poti", c);
+			} else {
+				mcp->set_wiper(mcp, 0, A1_config_options.wiper);
+				mcp->set_wiper(mcp, 1, A1_config_options.wiper);
+				mcp->exit(mcp);
+				applog(LOG_WARNING, "%d: set wiper to 0x%02x",
+					c, A1_config_options.wiper);
+			}
+		}
+
 		struct cgpu_info *cgpu = malloc(sizeof(*cgpu));
 		assert(cgpu != NULL);
 
@@ -832,7 +852,7 @@ bool detect_coincraft_rig_v3(void)
 /* Probe SPI channel and register chip chain */
 void A1_detect(bool hotplug)
 {
-	/* no hotplug support for now */
+	/* no hotplug support for SPI */
 	if (hotplug)
 		return;
 
@@ -842,27 +862,47 @@ void A1_detect(bool hotplug)
 		int sys_clk = 0;
 		int spi_clk = 0;
 		int override_chip_num = 0;
+		int wiper = 0;
 
-		sscanf(opt_bitmine_a1_options, "%d:%d:%d:%d",
-		       &ref_clk, &sys_clk, &spi_clk,  &override_chip_num);
+		sscanf(opt_bitmine_a1_options, "%d:%d:%d:%d:%d",
+		       &ref_clk, &sys_clk, &spi_clk,  &override_chip_num,
+		       &wiper);
 		if (ref_clk != 0)
 			A1_config_options.ref_clk_khz = ref_clk;
-		if (sys_clk != 0)
+		if (sys_clk != 0) {
+			if (sys_clk < 100000)
+				quit(1, "system clock must be above 100MHz");
 			A1_config_options.sys_clk_khz = sys_clk;
+		}
 		if (spi_clk != 0)
 			A1_config_options.spi_clk_khz = spi_clk;
 		if (override_chip_num != 0)
 			A1_config_options.override_chip_num = override_chip_num;
+		if (wiper != 0)
+			A1_config_options.wiper = wiper;
 
 		/* config options are global, scan them once */
 		parsed_config_options = &A1_config_options;
 	}
 	applog(LOG_DEBUG, "A1 detect");
+
+	/* register global SPI context */
+	struct spi_config cfg = default_spi_config;
+	cfg.mode = SPI_MODE_1;
+	cfg.speed = A1_config_options.spi_clk_khz * 1000;
+	spi = spi_init(&cfg);
+	if (spi == NULL)
+		return;
+
 	/* detect and register supported products */
 	if (detect_coincraft_desk())
 		return;
 	if (detect_coincraft_rig_v3())
 		return;
+	if (detect_single_chain())
+		return;
+	/* release SPI context if no A1 products found */
+	spi_exit(spi);
 }
 
 #define TEMP_UPDATE_INT_MS	2000
@@ -873,6 +913,10 @@ static int64_t A1_scanwork(struct thr_info *thr)
 	struct A1_chain *a1 = cgpu->device_data;
 	int32_t nonce_ranges_processed = 0;
 
+	if (a1->num_cores == 0) {
+		cgpu->deven = DEV_DISABLED;
+		return 0;
+	}
 	board_selector->select(a1->chain_id);
 
 	applog(LOG_DEBUG, "A1 running scanwork");
@@ -885,7 +929,7 @@ static int64_t A1_scanwork(struct thr_info *thr)
 	mutex_lock(&a1->lock);
 
 	if (a1->last_temp_time + TEMP_UPDATE_INT_MS < get_current_ms()) {
-		a1->temp = board_selector->get_temp();
+		a1->temp = board_selector->get_temp(0);
 		a1->last_temp_time = get_current_ms();
 	}
 	int cid = a1->chain_id;
@@ -956,7 +1000,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",
+				applog(LOG_INFO, "%d: chip %d: work underflow",
 				       cid, c);
 				break;
 			}
diff --git a/i2c-context.c b/i2c-context.c
new file mode 100644
index 0000000..b3fbe18
--- /dev/null
+++ b/i2c-context.c
@@ -0,0 +1,102 @@
+/*
+ * generic I2C slave access interface
+ *
+ * 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 <assert.h>
+
+#include "miner.h"
+#include "i2c-context.h"
+
+
+static 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_INFO, "i2c 0x%02x: failed to write to fdesc %d: %s",
+		       ctx->addr, ctx->file, strerror(errno));
+		return false;
+	}
+	applog(LOG_DEBUG, "I2C-W(0x%02x/0x%02x)=0x%02x", ctx->addr, reg, val);
+	return true;
+}
+
+static 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_INFO, "i2c 0x%02x: failed to read from fdesc %d: %s",
+		       ctx->addr, ctx->file, strerror(errno));
+		return false;
+	}
+	*val = data.byte;
+	applog(LOG_DEBUG, "I2C-R(0x%02x/0x%02x)=0x%02x", ctx->addr, reg, *val);
+	return true;
+}
+
+static void i2c_slave_exit(struct i2c_ctx *ctx)
+{
+	if (ctx->file == -1)
+		return;
+	close(ctx->file);
+	free(ctx);
+}
+
+extern struct i2c_ctx *i2c_slave_open(char *i2c_bus, uint8_t slave_addr)
+{
+	int file = open(i2c_bus, O_RDWR);
+	if (file < 0) {
+		applog(LOG_INFO, "Failed to open i2c-1: %s", strerror(errno));
+		return NULL;
+	}
+
+	if (ioctl(file, I2C_SLAVE, slave_addr) < 0) {
+		close(file);
+		return NULL;
+	}
+	struct i2c_ctx *ctx = malloc(sizeof(*ctx));
+	assert(ctx != NULL);
+
+	ctx->addr = slave_addr;
+	ctx->file = file;
+	ctx->exit = i2c_slave_exit;
+	ctx->read = i2c_slave_read;
+	ctx->write = i2c_slave_write;
+	return ctx;
+}
+
diff --git a/i2c-context.h b/i2c-context.h
new file mode 100644
index 0000000..e39d5da
--- /dev/null
+++ b/i2c-context.h
@@ -0,0 +1,26 @@
+#ifndef I2C_CONTEXT_H
+#define I2C_CONTEXT_H
+
+#include <stdbool.h>
+#include <stdint.h>
+
+/* common i2c context */
+struct i2c_ctx {
+	/* destructor */
+	void (*exit)(struct i2c_ctx *ctx);
+	/* write one byte to given register */
+	bool (*write)(struct i2c_ctx *ctx, uint8_t reg, uint8_t val);
+	/* read one byte from given register */
+	bool (*read)(struct i2c_ctx *ctx, uint8_t reg, uint8_t *val);
+
+	/* common data */
+	uint8_t addr;
+	int file;
+};
+
+/* the default I2C bus on RPi */
+#define I2C_BUS		"/dev/i2c-1"
+
+extern struct i2c_ctx *i2c_slave_open(char *i2c_bus, uint8_t slave_addr);
+
+#endif /* I2C_CONTEXT_H */