minion - chip power cycle option
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
diff --git a/driver-minion.c b/driver-minion.c
index d5fce4e..2418275 100644
--- a/driver-minion.c
+++ b/driver-minion.c
@@ -37,6 +37,17 @@ static void minion_detect(__maybe_unused bool hotplug)
// The code is always in - this just decides if it does it
static bool minreread = false;
+#if MINION_ROCKCHIP == 1
+#define MINION_POWERCYCLE_GPIO 173
+#define MINION_CHIP_OFF "1"
+#define MINION_CHIP_ON "0"
+#define MINION_CHIP_DELAY 100
+#endif
+
+// Power cycle if the xff_list is full and the tail is less than
+// this long ago
+#define MINION_POWER_TIME 60
+
/*
* Use pins for board selection
* If disabled, it will test chips just as 'pin 0'
@@ -601,7 +612,6 @@ typedef struct perf_item {
// *** 0xff error history - uses max 20 and rolls over
typedef struct xff_item {
struct timeval when;
- const char *what;
} XFF_ITEM;
#define ALLOC_XFF_ITEMS 20
@@ -881,6 +891,122 @@ struct minion_info {
bool initialised;
};
+#if MINION_ROCKCHIP == 1
+static bool minion_toggle_gpio(struct cgpu_info *minioncgpu, struct minion_info *minioninfo, int gpionum)
+{
+ char pindir[64], ena[64], pin[8], dir[64];
+ char gpiointvalue[64];
+ struct stat st;
+ int file, err;
+ ssize_t ret;
+
+ snprintf(pindir, sizeof(pindir), MINION_GPIO_SYS MINION_GPIO_PIN, gpionum);
+ memset(&st, 0, sizeof(st));
+
+ if (stat(pindir, &st) == 0) { // already exists
+ if (!S_ISDIR(st.st_mode)) {
+ applog(LOG_ERR, "%s: failed1 to enable GPIO pin %d"
+ " - not a directory",
+ minioncgpu->drv->dname, gpionum);
+ return false;
+ }
+ } else {
+ snprintf(ena, sizeof(ena), MINION_GPIO_SYS MINION_GPIO_ENA);
+ file = open(ena, O_WRONLY | O_SYNC);
+ if (file == -1) {
+ applog(LOG_ERR, "%s: failed2 to export GPIO pin %d (%d)"
+ " - you need to be root?",
+ minioncgpu->drv->dname,
+ gpionum, errno);
+ return false;
+ }
+ snprintf(pin, sizeof(pin), MINION_GPIO_ENA_VAL, gpionum);
+ ret = write(file, pin, (size_t)strlen(pin));
+ if (ret != (ssize_t)strlen(pin)) {
+ if (ret < 0)
+ err = errno;
+ else
+ err = (int)ret;
+ close(file);
+ applog(LOG_ERR, "%s: failed3 to export GPIO pin %d (%d:%d)",
+ minioncgpu->drv->dname,
+ gpionum, err, (int)strlen(pin));
+ return false;
+ }
+ close(file);
+
+ // Check again if it exists
+ memset(&st, 0, sizeof(st));
+ if (stat(pindir, &st) != 0) {
+ applog(LOG_ERR, "%s: failed4 to export GPIO pin %d (%d)",
+ minioncgpu->drv->dname,
+ gpionum, errno);
+ return false;
+ }
+ }
+
+ // Set the pin attributes
+ // Direction
+ snprintf(dir, sizeof(dir), MINION_GPIO_SYS MINION_GPIO_PIN MINION_GPIO_DIR, gpionum);
+ file = open(dir, O_WRONLY | O_SYNC);
+ if (file == -1) {
+ applog(LOG_ERR, "%s: failed5 to configure GPIO pin %d (%d)"
+ " - you need to be root?",
+ minioncgpu->drv->dname,
+ gpionum, errno);
+ return false;
+ }
+ ret = write(file, MINION_GPIO_DIR_WRITE, (size_t)strlen(MINION_GPIO_DIR_WRITE));
+ if (ret != (ssize_t)strlen(MINION_GPIO_DIR_READ)) {
+ if (ret < 0)
+ err = errno;
+ else
+ err = (int)ret;
+ close(file);
+ applog(LOG_ERR, "%s: failed6 to configure GPIO pin %d (%d:%d)",
+ minioncgpu->drv->dname, gpionum,
+ err, (int)strlen(MINION_GPIO_DIR_READ));
+ return false;
+ }
+ close(file);
+
+ // Open it
+ snprintf(gpiointvalue, sizeof(gpiointvalue),
+ MINION_GPIO_SYS MINION_GPIO_PIN MINION_GPIO_VALUE,
+ gpionum);
+ fd = open(gpiointvalue, O_WRONLY);
+ if (fd == -1) {
+ applog(LOG_ERR, "%s: failed7 to access GPIO pin %d (%d)",
+ minioncgpu->drv->dname,
+ gpionum, errno);
+ return false;
+ }
+
+ ret = write(fd, MINION_CHIP_OFF, sizeof(MINION_CHIP_OFF)-1);
+ if (ret != sizeof(MINION_CHIP_OFF)-1) {
+ close(fd);
+ applog(LOG_ERR, "%s: failed8 to toggle off GPIO pin %d (%d:%d)",
+ minioncgpu->drv->dname,
+ gpionum, (int)ret, errno);
+ return false;
+ }
+
+ cgsleep_ms(MINION_CHIP_DELAY);
+
+ ret = write(fd, MINION_CHIP_ON, sizeof(MINION_CHIP_ON)-1);
+ if (ret != sizeof(MINION_CHIP_OFF)-1) {
+ close(fd);
+ applog(LOG_ERR, "%s: failed9 to toggle on GPIO pin %d (%d:%d)",
+ minioncgpu->drv->dname,
+ gpionum, (int)ret, errno);
+ return false;
+ }
+
+ close(fd);
+ return true;
+}
+#endif
+
static void ready_work(struct cgpu_info *minioncgpu, struct work *work, bool rolled)
{
struct minion_info *minioninfo = (struct minion_info *)(minioncgpu->device_data);
@@ -1160,7 +1286,9 @@ static int __do_ioctl(struct cgpu_info *minioncgpu, struct minion_info *minionin
uint32_t rsiz, uint64_t *ioseq, MINION_FFL_ARGS)
{
struct spi_ioc_transfer tran;
- bool fail = false;
+ bool fail = false, powercycle = false, show = false;
+ double lastshow, total;
+ K_ITEM *xitem;
int ret;
#if MINION_SHOW_IO
char dataw[DATA_ALL], datar[DATA_ALL];
@@ -1243,8 +1371,46 @@ static int __do_ioctl(struct cgpu_info *minioncgpu, struct minion_info *minionin
break;
}
}
- if (fail)
+ if (fail) {
+ powercycle = show = false;
+
+ if (minioninfo->xfree_list->count > 0)
+ xitem = k_unlink_head(minioninfo->xfree_list);
+ else
+ xitem = k_unlink_tail(minioninfo->xff_list);
+ cgtime(&(DATA_XFF(xitem)->when));
+ if (!minioninfo->xff_list->head)
+ show = true;
+ else {
+ // xff_list is full
+ if (minioninfo->xfree_list->count == 0) {
+ total = tdiff(&(DATA_XFF(xitem)->when),
+ &(DATA_XFF(minioninfo->xff_list->tail)->when));
+ if (total <= MINION_POWER_TIME) {
+ powercycle = true;
+ // Discard the history
+ k_list_transfer_to_head(minioninfo->xff_list,
+ minioninfo->xfree_list);
+ k_add_head(minioninfo->xfree_list, xitem);
+ xitem = NULL;
+ }
+ }
+
+ if (!powercycle) {
+ lastshow = tdiff(&(DATA_XFF(xitem)->when),
+ &(DATA_XFF(minioninfo->xff_list->head)->when));
+ show = (lastshow >= 5.0);
+ }
+ }
+ if (xitem)
+ k_add_head(minioninfo->xff_list, xitem);
+
+#if MINION_ROCKCHIP == 1
+ if (powercycle)
+ minion_toggle_gpio(minioncgpu, minioninfo, MINION_POWERCYCLE_GPIO);
+#endif
minion_init_spi(minioncgpu, minioninfo, 0, 0, true);
+ }
} else if (minioninfo->spi_reset_count) {
if (minioninfo->spi_reset_io) {
if (*ioseq > 0 && (*ioseq % minioninfo->spi_reset_count) == 0)
@@ -1270,36 +1436,20 @@ static int __do_ioctl(struct cgpu_info *minioncgpu, struct minion_info *minionin
IO_STAT_STORE(&sta, &fin, &lsta, &lfin, &tsd, obuf, osiz, ret, 1);
if (fail) {
- char *what = "unk";
- K_ITEM *xitem;
- bool show = false;
- double lastshow;
- switch (obuf[1]) {
- case READ_ADDR(MINION_RES_DATA):
- what = "nonce";
- break;
- case READ_ADDR(MINION_SYS_FIFO_STA):
- what = "fifo";
- break;
- }
- K_WLOCK(minioninfo->xfree_list);
- if (minioninfo->xfree_list->count > 0)
- xitem = k_unlink_head(minioninfo->xfree_list);
- else
- xitem = k_unlink_tail(minioninfo->xff_list);
- DATA_XFF(xitem)->what = what;
- cgtime(&(DATA_XFF(xitem)->when));
- if (!minioninfo->xff_list->head)
- show = true;
- else {
- lastshow = tdiff(&(DATA_XFF(xitem)->when),
- &(DATA_XFF(minioninfo->xff_list->head)->when));
- show = (lastshow >= 5.0);
- }
- k_add_head(minioninfo->xff_list, xitem);
- K_WUNLOCK(minioninfo->xfree_list);
- if (show) {
- applog(LOG_ERR, "%s%d: ioctl %"PRIu64" %s returned all 0xff - resetting",
+ if (powercycle) {
+ applog(LOG_ERR, "%s%d: power cycling ioctl %"PRIu64,
+ minioncgpu->drv->name, minioncgpu->device_id, *ioseq);
+ } else if (show) {
+ char *what = "unk";
+ switch (obuf[1]) {
+ case READ_ADDR(MINION_RES_DATA):
+ what = "nonce";
+ break;
+ case READ_ADDR(MINION_SYS_FIFO_STA):
+ what = "fifo";
+ break;
+ }
+ applog(LOG_ERR, "%s%d: resetting ioctl %"PRIu64" %s returned all 0xff",
minioncgpu->drv->name, minioncgpu->device_id,
*ioseq, what);
}