forked from luke-jr/bfgminer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver-nanofury.c
587 lines (489 loc) · 15.3 KB
/
driver-nanofury.c
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
/*
* Copyright 2013-2014 Luke Dashjr
* Copyright 2013-2014 Vladimir Strinski
*
* 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 "config.h"
#include <stdbool.h>
#include <stdint.h>
#include "deviceapi.h"
#include "driver-bitfury.h"
#include "libbitfury.h"
#include "logging.h"
#include "lowlevel.h"
#include "mcp2210.h"
#include "miner.h"
#include "util.h"
#define NANOFURY_USB_PRODUCT "NanoFury"
#define NANOFURY_GP_PIN_LED 0
#define NANOFURY_GP_PIN_SCK_OVR 5
#define NANOFURY_GP_PIN_PWR_EN 6
#define NANOFURY_GP_PIN_PWR_EN0 7
#define NANOFURY_MAX_BYTES_PER_SPI_TRANSFER 60 // due to MCP2210 limitation
BFG_REGISTER_DRIVER(nanofury_drv)
static const struct bfg_set_device_definition nanofury_set_device_funcs[];
struct nanofury_state {
struct lowlevel_device_info *lowl_info;
struct mcp2210_device *mcp;
struct timeval identify_started;
bool identify_requested;
unsigned long current_baud;
bool ledalternating;
bool ledvalue;
bool powered_off;
};
// Bit-banging reset, to reset more chips in chain - toggle for longer period... Each 3 reset cycles reset first chip in chain
static
bool nanofury_spi_reset(struct mcp2210_device * const mcp)
{
int r;
char tx[1] = {0x81}; // will send this waveform: - _ _ _ _ _ _ -
char buf[1];
// SCK_OVRRIDE
if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_SCK_OVR, BGV_HIGH))
return false;
for (r = 0; r < 16; ++r)
if (!mcp2210_spi_transfer(mcp, tx, buf, 1))
return false;
if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) == BGV_ERROR)
return false;
return true;
}
static void nanofury_device_off(struct mcp2210_device *, struct nanofury_state *);
static
bool nanofury_spi_txrx(struct spi_port * const port)
{
struct cgpu_info * const cgpu = port->cgpu;
struct nanofury_state * const state = port->userp;
struct mcp2210_device * const mcp = state->mcp;
const void *wrbuf = spi_gettxbuf(port);
void *rdbuf = spi_getrxbuf(port);
size_t bufsz = spi_getbufsz(port);
const uint8_t *ptrwrbuf = wrbuf;
uint8_t *ptrrdbuf = rdbuf;
if (state->current_baud != port->speed)
{
applog(LOG_NOTICE, "%"PRIpreprv": Changing baud from %lu to %lu",
cgpu ? cgpu->proc_repr : nanofury_drv.dname,
(unsigned long)state->current_baud, (unsigned long)port->speed);
if (!mcp2210_configure_spi(mcp, port->speed, 0xffff, 0xffef, 0, 0, 0))
goto err;
state->current_baud = port->speed;
}
nanofury_spi_reset(mcp);
// start by sending chunks of 60 bytes...
while (bufsz >= NANOFURY_MAX_BYTES_PER_SPI_TRANSFER)
{
if (!mcp2210_spi_transfer(mcp, ptrwrbuf, ptrrdbuf, NANOFURY_MAX_BYTES_PER_SPI_TRANSFER))
goto err;
ptrrdbuf += NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
ptrwrbuf += NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
bufsz -= NANOFURY_MAX_BYTES_PER_SPI_TRANSFER;
}
// send any remaining bytes...
if (bufsz > 0)
{
if (!mcp2210_spi_transfer(mcp, ptrwrbuf, ptrrdbuf, bufsz))
goto err;
}
return true;
err:
mcp2210_spi_cancel(mcp);
nanofury_device_off(mcp, state);
if (cgpu)
{
struct thr_info * const thr = cgpu->thr[0];
hashes_done2(thr, -1, NULL);
}
return false;
}
static
void nanofury_send_led_gpio(struct nanofury_state * const state)
{
struct mcp2210_device * const mcp = state->mcp;
mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, state->ledvalue ? BGV_HIGH : BGV_LOW);
}
static
void nanofury_do_led_alternating(struct nanofury_state * const state)
{
state->ledvalue = !state->ledvalue;
nanofury_send_led_gpio(state);
}
static
void nanofury_device_off(struct mcp2210_device * const mcp, struct nanofury_state * const state)
{
// Try to reset everything back to input
for (int i = 0; i < 9; ++i)
mcp2210_get_gpio_input(mcp, i);
if (state)
state->powered_off = true;
}
static
bool nanofury_power_enable(struct mcp2210_device * const mcp, const bool poweron, struct nanofury_state * const state)
{
if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_PWR_EN, poweron ? BGV_HIGH : BGV_LOW))
return false;
if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_PWR_EN0, poweron ? BGV_LOW : BGV_HIGH))
return false;
if (state)
state->powered_off = !poweron;
return true;
}
static
bool nanofury_checkport(struct mcp2210_device * const mcp, const unsigned long baud, struct nanofury_state * const state)
{
int i;
const char tmp = 0;
char tmprx;
// default: set everything to input
for (i = 0; i < 9; ++i)
if (BGV_ERROR == mcp2210_get_gpio_input(mcp, i))
goto fail;
// configure the pins that we need:
// LED
if (!mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, BGV_HIGH))
goto fail;
nanofury_power_enable(mcp, true, state);
// cancel any outstanding SPI transfers
mcp2210_spi_cancel(mcp);
// configure SPI
// This is the only place where speed, mode and other settings are configured!!!
if (!mcp2210_configure_spi(mcp, baud, 0xffff, 0xffef, 0, 0, 0))
goto fail;
if (!mcp2210_set_spimode(mcp, 0))
goto fail;
if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
goto fail;
// after this command SCK_OVRRIDE should read the same as current SCK value (which for mode 0 should be 0)
if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != BGV_LOW)
goto fail;
// switch SCK to polarity (default SCK=1 in mode 2)
if (!mcp2210_set_spimode(mcp, 2))
goto fail;
if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
goto fail;
// after this command SCK_OVRRIDE should read the same as current SCK value (which for mode 2 should be 1)
if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != BGV_HIGH)
goto fail;
// switch SCK to polarity (default SCK=0 in mode 0)
if (!mcp2210_set_spimode(mcp, 0))
goto fail;
if (!mcp2210_spi_transfer(mcp, &tmp, &tmprx, 1))
goto fail;
if (mcp2210_get_gpio_input(mcp, NANOFURY_GP_PIN_SCK_OVR) != BGV_LOW)
goto fail;
return true;
fail:
nanofury_device_off(mcp, state);
return false;
}
static
bool nanofury_lowl_match(const struct lowlevel_device_info * const info)
{
return lowlevel_match_lowlproduct(info, &lowl_mcp2210, NANOFURY_USB_PRODUCT);
}
static
bool nanofury_lowl_probe(const struct lowlevel_device_info * const info)
{
const char * const product = info->product;
const char * const serial = info->serial;
struct mcp2210_device *mcp;
struct spi_port *port;
struct nanofury_state *state;
int chips;
if (info->lowl != &lowl_mcp2210)
{
bfg_probe_result_flags = BPR_WRONG_DEVTYPE;
if (info->lowl != &lowl_hid && info->lowl != &lowl_usb)
applog(LOG_DEBUG, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not mcp2210!",
__func__, product, serial);
return false;
}
mcp = mcp2210_open(info);
if (!mcp)
{
applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but mcp2210 lowlevel driver failed to open it",
__func__, product, serial);
return false;
}
state = malloc(sizeof(*state));
*state = (struct nanofury_state){
.mcp = mcp,
.ledvalue = true,
};
port = calloc(1, sizeof(*port));
port->userp = state;
port->txrx = nanofury_spi_txrx;
port->repr = nanofury_drv.dname;
port->logprio = LOG_DEBUG;
port->speed = 200000;
{
struct bitfury_device dummy_bitfury = {
.spi = port,
};
drv_set_defaults(&nanofury_drv, bitfury_set_device_funcs_probe, &dummy_bitfury, NULL, NULL, 1);
}
if (!nanofury_checkport(mcp, port->speed, NULL))
{
applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but failed to detect nanofury",
__func__, product, serial);
mcp2210_close(mcp);
return false;
}
state->current_baud = port->speed;
chips = libbitfury_detectChips1(port);
free(port);
nanofury_device_off(mcp, NULL);
mcp2210_close(mcp);
if (lowlevel_claim(&nanofury_drv, true, info))
{
free(state);
return false;
}
state->lowl_info = lowlevel_ref(info);
struct cgpu_info *cgpu;
cgpu = malloc(sizeof(*cgpu));
*cgpu = (struct cgpu_info){
.drv = &nanofury_drv,
.set_device_funcs = nanofury_set_device_funcs,
.device_data = state,
.threads = 1,
.procs = chips,
// TODO: .name
.device_path = strdup(info->path),
.dev_manufacturer = maybe_strdup(info->manufacturer),
.dev_product = maybe_strdup(product),
.dev_serial = maybe_strdup(serial),
.deven = DEV_ENABLED,
// TODO: .cutofftemp
};
return add_cgpu(cgpu);
}
static
bool nanofury_init(struct thr_info * const thr)
{
struct cgpu_info * const cgpu = thr->cgpu, *proc;
struct nanofury_state * const state = cgpu->device_data;
struct lowlevel_device_info * const info = state->lowl_info;
struct spi_port *port;
struct bitfury_device *bitfury;
struct mcp2210_device *mcp;
mcp = mcp2210_open(info);
lowlevel_devinfo_free(info);
if (!mcp)
{
applog(LOG_ERR, "%"PRIpreprv": Failed to open mcp2210 device", cgpu->proc_repr);
return false;
}
if (!nanofury_checkport(mcp, state->current_baud, state))
{
applog(LOG_ERR, "%"PRIpreprv": checkport failed", cgpu->proc_repr);
mcp2210_close(mcp);
return false;
}
port = malloc(sizeof(*port));
bitfury = malloc(sizeof(*bitfury) * cgpu->procs);
if (!(port && bitfury && state))
{
applog(LOG_ERR, "%"PRIpreprv": Failed to allocate structures", cgpu->proc_repr);
free(port);
free(bitfury);
free(state);
mcp2210_close(mcp);
return false;
}
/* Be careful, read lowl-spi.h comments for warnings */
memset(port, 0, sizeof(*port));
port->txrx = nanofury_spi_txrx;
port->cgpu = cgpu;
port->repr = cgpu->proc_repr;
port->logprio = LOG_ERR;
port->speed = state->current_baud;
const int init_osc6_bits = 50;
const int ramp_osc6_bits = (cgpu->procs > 1) ? 5 : init_osc6_bits;
state->mcp = mcp;
port->userp = state;
for (proc = cgpu; proc; (proc = proc->next_proc), ++bitfury)
{
struct thr_info * const mythr = proc->thr[0];
*bitfury = (struct bitfury_device){
.spi = port,
.fasync = proc->proc_id,
};
proc->device_data = bitfury;
mythr->cgpu_data = state;
bitfury->osc6_bits = ramp_osc6_bits;
bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
bitfury_init_chip(proc);
}
--bitfury;
while (bitfury->osc6_bits < init_osc6_bits)
{
for (proc = cgpu; proc; proc = proc->next_proc)
{
bitfury = proc->device_data;
bitfury->osc6_bits += 5;
bitfury_send_freq(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
}
}
for (proc = cgpu; proc; proc = proc->next_proc)
{
bitfury_init_chip(proc);
proc->status = LIFE_INIT2;
}
nanofury_send_led_gpio(state);
timer_set_now(&thr->tv_poll);
return true;
}
static
void nanofury_disable(struct thr_info * const thr)
{
struct cgpu_info * const proc = thr->cgpu;
struct cgpu_info * const dev = proc->device;
struct nanofury_state * const state = thr->cgpu_data;
struct mcp2210_device * const mcp = state->mcp;
bitfury_disable(thr);
// Before powering off, ensure no other chip needs power
for_each_managed_proc(oproc, dev)
if (oproc->deven == DEV_ENABLED)
return;
applog(LOG_NOTICE, "%s: Last chip disabled, shutting off power",
dev->dev_repr);
nanofury_device_off(mcp, state);
}
static
void nanofury_enable(struct thr_info * const thr)
{
struct cgpu_info * const proc = thr->cgpu;
struct cgpu_info * const dev = proc->device;
struct nanofury_state * const state = thr->cgpu_data;
struct mcp2210_device * const mcp = state->mcp;
if (state->powered_off)
{
// All chips were disabled, so we need to power back on
applog(LOG_DEBUG, "%s: Enabling power",
dev->dev_repr);
nanofury_checkport(mcp, state->current_baud, state);
nanofury_send_led_gpio(state);
}
bitfury_enable(thr);
}
static
void nanofury_reinit(struct cgpu_info * const proc)
{
struct thr_info * const thr = proc->thr[0];
struct cgpu_info * const dev = proc->device;
struct nanofury_state * const state = thr->cgpu_data;
struct mcp2210_device * const mcp = state->mcp;
nanofury_device_off(mcp, state);
cgsleep_ms(1);
for_each_managed_proc(oproc, dev)
if (oproc->deven == DEV_ENABLED)
nanofury_enable(oproc->thr[0]);
}
static
double _nanofury_total_diff1(struct cgpu_info * const dev)
{
double d = 0.;
for (struct cgpu_info *proc = dev; proc; proc = proc->next_proc)
d += proc->diff1;
return d;
}
static
void nanofury_poll(struct thr_info * const thr)
{
struct cgpu_info * const dev = thr->cgpu;
struct nanofury_state * const state = thr->cgpu_data;
struct mcp2210_device * const mcp = state->mcp;
double diff1_before = 0.;
if (state->identify_requested)
{
if (!timer_isset(&state->identify_started))
mcp2210_set_gpio_output(mcp, NANOFURY_GP_PIN_LED, state->ledvalue ? BGV_LOW : BGV_HIGH);
timer_set_delay_from_now(&state->identify_started, 5000000);
state->identify_requested = false;
}
if (state->ledalternating && !timer_isset(&state->identify_started))
diff1_before = _nanofury_total_diff1(dev);
bitfury_do_io(thr);
if (state->ledalternating && (timer_isset(&state->identify_started) || diff1_before != _nanofury_total_diff1(dev)))
nanofury_do_led_alternating(state);
if (timer_passed(&state->identify_started, NULL))
{
// Also used when setting ledmode
nanofury_send_led_gpio(state);
timer_unset(&state->identify_started);
}
}
static
bool nanofury_identify(struct cgpu_info * const cgpu)
{
struct nanofury_state * const state = cgpu->thr[0]->cgpu_data;
state->identify_requested = true;
return true;
}
static
void nanofury_shutdown(struct thr_info * const thr)
{
struct nanofury_state * const state = thr->cgpu_data;
if (!state)
return;
struct mcp2210_device * const mcp = state->mcp;
if (mcp)
nanofury_device_off(mcp, state);
}
const char *nanofury_set_ledmode(struct cgpu_info * const proc, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
{
struct thr_info * const thr = proc->thr[0];
struct nanofury_state * const state = thr->cgpu_data;
if (!strcasecmp(setting, "on"))
{
state->ledvalue = true;
state->ledalternating = false;
}
else
if (!strcasecmp(setting, "off"))
state->ledvalue = state->ledalternating = false;
else
if (!strcasecmp(setting, "alternating"))
state->ledalternating = true;
else
return "Invalid LED mode; must be on/off/alternating";
if (!timer_isset(&state->identify_started))
timer_set_now(&state->identify_started);
return NULL;
}
static const struct bfg_set_device_definition nanofury_set_device_funcs[] = {
{"baud", bitfury_set_baud, "SPI baud rate"},
{"osc6_bits", bitfury_set_osc6_bits, "range 1-"BITFURY_MAX_OSC6_BITS_S" (slow to fast)"},
{"ledmode", nanofury_set_ledmode, "on/off/alternating"},
{NULL},
};
struct device_drv nanofury_drv = {
.dname = "nanofury",
.name = "NFY",
.lowl_match = nanofury_lowl_match,
.lowl_probe = nanofury_lowl_probe,
.thread_init = nanofury_init,
.thread_disable = nanofury_disable,
.thread_enable = nanofury_enable,
.reinit_device = nanofury_reinit,
.thread_shutdown = nanofury_shutdown,
.minerloop = minerloop_async,
.job_prepare = bitfury_job_prepare,
.job_start = bitfury_noop_job_start,
.poll = nanofury_poll,
.job_process_results = bitfury_job_process_results,
.get_api_extra_device_detail = bitfury_api_device_detail,
.get_api_extra_device_status = bitfury_api_device_status,
.identify_device = nanofury_identify,
#ifdef HAVE_CURSES
.proc_wlogprint_status = bitfury_wlogprint_status,
.proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
.proc_tui_handle_choice = bitfury_tui_handle_choice,
#endif
};