1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2021 Gateworks Corporation
4  */
5 
6 #include <common.h>
7 #include <command.h>
8 #include <hang.h>
9 #include <hexdump.h>
10 #include <i2c.h>
11 #include <linux/delay.h>
12 #include <dm/uclass.h>
13 
14 #include "gsc.h"
15 
16 DECLARE_GLOBAL_DATA_PTR;
17 
18 struct venice_board_info som_info;
19 struct venice_board_info base_info;
20 char venice_model[32];
21 
22 /* return a mac address from EEPROM info */
gsc_getmac(int index,uint8_t * address)23 int gsc_getmac(int index, uint8_t *address)
24 {
25 	int i, j;
26 	u32 maclow, machigh;
27 	u64 mac;
28 
29 	j = 0;
30 	if (som_info.macno) {
31 		maclow = som_info.mac[5];
32 		maclow |= som_info.mac[4] << 8;
33 		maclow |= som_info.mac[3] << 16;
34 		maclow |= som_info.mac[2] << 24;
35 		machigh = som_info.mac[1];
36 		machigh |= som_info.mac[0] << 8;
37 		mac = machigh;
38 		mac <<= 32;
39 		mac |= maclow;
40 		for (i = 0; i < som_info.macno; i++, j++) {
41 			if (index == j)
42 				goto out;
43 		}
44 	}
45 
46 	maclow = base_info.mac[5];
47 	maclow |= base_info.mac[4] << 8;
48 	maclow |= base_info.mac[3] << 16;
49 	maclow |= base_info.mac[2] << 24;
50 	machigh = base_info.mac[1];
51 	machigh |= base_info.mac[0] << 8;
52 	mac = machigh;
53 	mac <<= 32;
54 	mac |= maclow;
55 	for (i = 0; i < base_info.macno; i++, j++) {
56 		if (index == j)
57 			goto out;
58 	}
59 
60 	return -EINVAL;
61 
62 out:
63 	mac += i;
64 	address[0] = (mac >> 40) & 0xff;
65 	address[1] = (mac >> 32) & 0xff;
66 	address[2] = (mac >> 24) & 0xff;
67 	address[3] = (mac >> 16) & 0xff;
68 	address[4] = (mac >> 8) & 0xff;
69 	address[5] = (mac >> 0) & 0xff;
70 
71 	return 0;
72 }
73 
74 /* System Controller registers */
75 enum {
76 	GSC_SC_CTRL0		= 0,
77 	GSC_SC_CTRL1		= 1,
78 	GSC_SC_STATUS		= 10,
79 	GSC_SC_FWCRC		= 12,
80 	GSC_SC_FWVER		= 14,
81 	GSC_SC_WP		= 15,
82 	GSC_SC_RST_CAUSE	= 16,
83 	GSC_SC_THERM_PROTECT	= 19,
84 };
85 
86 /* System Controller Control1 bits */
87 enum {
88 	GSC_SC_CTRL1_WDTIME	= 4, /* 1 = 60s timeout, 0 = 30s timeout */
89 	GSC_SC_CTRL1_WDEN	= 5, /* 1 = enable, 0 = disable */
90 	GSC_SC_CTRL1_BOOT_CHK   = 6, /* 1 = enable alt boot check */
91 	GSC_SC_CTRL1_WDDIS	= 7, /* 1 = disable boot watchdog */
92 };
93 
94 /* System Controller Interrupt bits */
95 enum {
96 	GSC_SC_IRQ_PB		= 0, /* Pushbutton switch */
97 	GSC_SC_IRQ_SECURE	= 1, /* Secure Key erase operation complete */
98 	GSC_SC_IRQ_EEPROM_WP	= 2, /* EEPROM write violation */
99 	GSC_SC_IRQ_GPIO		= 4, /* GPIO change */
100 	GSC_SC_IRQ_TAMPER	= 5, /* Tamper detect */
101 	GSC_SC_IRQ_WATCHDOG	= 6, /* Watchdog trip */
102 	GSC_SC_IRQ_PBLONG	= 7, /* Pushbutton long hold */
103 };
104 
105 /* System Controller WP bits */
106 enum {
107 	GSC_SC_WP_ALL		= 0, /* Write Protect All EEPROM regions */
108 	GSC_SC_WP_BOARDINFO	= 1, /* Write Protect Board Info region */
109 };
110 
111 /* System Controller Reset Cause */
112 enum {
113 	GSC_SC_RST_CAUSE_VIN		= 0,
114 	GSC_SC_RST_CAUSE_PB		= 1,
115 	GSC_SC_RST_CAUSE_WDT		= 2,
116 	GSC_SC_RST_CAUSE_CPU		= 3,
117 	GSC_SC_RST_CAUSE_TEMP_LOCAL	= 4,
118 	GSC_SC_RST_CAUSE_TEMP_REMOTE	= 5,
119 	GSC_SC_RST_CAUSE_SLEEP		= 6,
120 	GSC_SC_RST_CAUSE_BOOT_WDT	= 7,
121 	GSC_SC_RST_CAUSE_BOOT_WDT_MAN	= 8,
122 	GSC_SC_RST_CAUSE_SOFT_PWR	= 9,
123 	GSC_SC_RST_CAUSE_MAX		= 10,
124 };
125 
gsc_get_dev(int busno,int slave)126 static struct udevice *gsc_get_dev(int busno, int slave)
127 {
128 	struct udevice *dev;
129 	int ret;
130 
131 #if (IS_ENABLED(CONFIG_SPL_BUILD))
132 	ret = i2c_get_chip_for_busnum(busno + 1, slave, 1, &dev);
133 	if (ret)
134 		return NULL;
135 #else
136 	struct udevice *bus;
137 
138 	busno--;
139 
140 	ret = uclass_get_device_by_seq(UCLASS_I2C, busno, &bus);
141 	if (ret) {
142 		printf("i2c%d: no bus %d\n", busno + 1, ret);
143 		return NULL;
144 	}
145 	ret = i2c_get_chip(bus, slave, 1, &dev);
146 	if (ret) {
147 		printf("i2c%d@0x%02x: no chip %d\n", busno + 1, slave, ret);
148 		return NULL;
149 	}
150 #endif
151 
152 	return dev;
153 }
154 
gsc_read_eeprom(int bus,int slave,int alen,struct venice_board_info * info)155 static int gsc_read_eeprom(int bus, int slave, int alen, struct venice_board_info *info)
156 {
157 	int i;
158 	int chksum;
159 	unsigned char *buf = (unsigned char *)info;
160 	struct udevice *dev;
161 	int ret;
162 
163 	/* probe device */
164 	dev = gsc_get_dev(bus, slave);
165 	if (!dev) {
166 		if (slave == GSC_EEPROM_ADDR)
167 			puts("ERROR: Failed to probe EEPROM\n");
168 		return -ENODEV;
169 	}
170 
171 	/* read eeprom config section */
172 	memset(info, 0, sizeof(*info));
173 	ret = i2c_set_chip_offset_len(dev, alen);
174 	if (ret) {
175 		puts("EEPROM: Failed to set alen\n");
176 		return ret;
177 	}
178 	ret = dm_i2c_read(dev, 0x00, buf, sizeof(*info));
179 	if (ret) {
180 		if (slave == GSC_EEPROM_ADDR)
181 			printf("EEPROM: Failed to read EEPROM\n");
182 		return ret;
183 	}
184 
185 	/* validate checksum */
186 	for (chksum = 0, i = 0; i < (int)sizeof(*info) - 2; i++)
187 		chksum += buf[i];
188 	if ((info->chksum[0] != chksum >> 8) ||
189 	    (info->chksum[1] != (chksum & 0xff))) {
190 		printf("EEPROM: I2C%d@0x%02x: Invalid Model in EEPROM\n", bus, slave);
191 		print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, sizeof(*info));
192 		memset(info, 0, sizeof(*info));
193 		return -EINVAL;
194 	}
195 
196 	/* sanity check valid model */
197 	if (info->model[0] != 'G' || info->model[1] != 'W') {
198 		printf("EEPROM: I2C%d@0x%02x: Invalid Model in EEPROM\n", bus, slave);
199 		print_hex_dump_bytes("", DUMP_PREFIX_NONE, buf, sizeof(*info));
200 		memset(info, 0, sizeof(*info));
201 		return -EINVAL;
202 	}
203 
204 	return 0;
205 }
206 
gsc_get_rst_cause(struct udevice * dev)207 static const char *gsc_get_rst_cause(struct udevice *dev)
208 {
209 	static char str[64];
210 	static const char * const names[] = {
211 		"VIN",
212 		"PB",
213 		"WDT",
214 		"CPU",
215 		"TEMP_L",
216 		"TEMP_R",
217 		"SLEEP",
218 		"BOOT_WDT1",
219 		"BOOT_WDT2",
220 		"SOFT_PWR",
221 	};
222 	unsigned char reg;
223 
224 	/* reset cause */
225 	str[0] = 0;
226 	if (!dm_i2c_read(dev, GSC_SC_RST_CAUSE, &reg, 1)) {
227 		if (reg < ARRAY_SIZE(names))
228 			sprintf(str, "%s", names[reg]);
229 		else
230 			sprintf(str, "0x%02x", reg);
231 	}
232 
233 	/* thermal protection */
234 	if (!dm_i2c_read(dev, GSC_SC_THERM_PROTECT, &reg, 1)) {
235 		reg |= 1;
236 		dm_i2c_write(dev, GSC_SC_THERM_PROTECT, &reg, 1);
237 		strcat(str, " Thermal Protection Enabled");
238 	}
239 
240 	return str;
241 }
242 
243 /* display hardware monitor ADC channels */
gsc_hwmon(void)244 int gsc_hwmon(void)
245 {
246 	const void *fdt = gd->fdt_blob;
247 	struct udevice *dev;
248 	int node, reg, mode, len, val, offset;
249 	const char *label;
250 	u8 buf[2];
251 	int ret;
252 
253 	node = fdt_node_offset_by_compatible(fdt, -1, "gw,gsc-adc");
254 	if (node <= 0)
255 		return node;
256 
257 	/* probe device */
258 	dev = gsc_get_dev(1, GSC_HWMON_ADDR);
259 	if (!dev) {
260 		puts("ERROR: Failed to probe GSC HWMON\n");
261 		return -ENODEV;
262 	}
263 
264 	/* iterate over hwmon nodes */
265 	node = fdt_first_subnode(fdt, node);
266 	while (node > 0) {
267 		reg = fdtdec_get_int(fdt, node, "reg", -1);
268 		mode = fdtdec_get_int(fdt, node, "gw,mode", -1);
269 		offset = fdtdec_get_int(fdt, node, "gw,voltage-offset-microvolt", 0);
270 		label = fdt_stringlist_get(fdt, node, "label", 0, NULL);
271 
272 		if ((reg == -1) || (mode == -1) || !label)
273 			printf("invalid dt:%s\n", fdt_get_name(fdt, node, NULL));
274 
275 		memset(buf, 0, sizeof(buf));
276 		ret = dm_i2c_read(dev, reg, buf, sizeof(buf));
277 		if (ret) {
278 			printf("i2c error: %d\n", ret);
279 			continue;
280 		}
281 		val = buf[0] | buf[1] << 8;
282 		if (val >= 0) {
283 			const u32 *div;
284 			int r[2];
285 
286 			switch (mode) {
287 			case 0: /* temperature (C*10) */
288 				if (val > 0x8000)
289 					val -= 0xffff;
290 				printf("%-8s: %d.%ldC\n", label, val / 10, abs(val % 10));
291 				break;
292 			case 1: /* prescaled voltage */
293 				if (val != 0xffff)
294 					printf("%-8s: %d.%03dV\n", label, val / 1000, val % 1000);
295 				break;
296 			case 2: /* scaled based on ref volt and resolution */
297 				val *= 2500;
298 				val /= 1 << 12;
299 
300 				/* apply pre-scaler voltage divider */
301 				div  = fdt_getprop(fdt, node, "gw,voltage-divider-ohms", &len);
302 				if (div && (len == sizeof(uint32_t) * 2)) {
303 					r[0] = fdt32_to_cpu(div[0]);
304 					r[1] = fdt32_to_cpu(div[1]);
305 					if (r[0] && r[1]) {
306 						val *= (r[0] + r[1]);
307 						val /= r[1];
308 					}
309 				}
310 
311 				/* adjust by offset */
312 				val += offset;
313 
314 				printf("%-8s: %d.%03dV\n", label, val / 1000, val % 1000);
315 				break;
316 			}
317 		}
318 		node = fdt_next_subnode(fdt, node);
319 	}
320 
321 	return 0;
322 }
323 
324 /* determine BOM revision from model */
get_bom_rev(const char * str)325 int get_bom_rev(const char *str)
326 {
327 	int  rev_bom = 0;
328 	int i;
329 
330 	for (i = strlen(str) - 1; i > 0; i--) {
331 		if (str[i] == '-')
332 			break;
333 		if (str[i] >= '1' && str[i] <= '9') {
334 			rev_bom = str[i] - '0';
335 			break;
336 		}
337 	}
338 	return rev_bom;
339 }
340 
341 /* determine PCB revision from model */
get_pcb_rev(const char * str)342 char get_pcb_rev(const char *str)
343 {
344 	char rev_pcb = 'A';
345 	int i;
346 
347 	for (i = strlen(str) - 1; i > 0; i--) {
348 		if (str[i] == '-')
349 			break;
350 		if (str[i] >= 'A') {
351 			rev_pcb = str[i];
352 			break;
353 		}
354 	}
355 	return rev_pcb;
356 }
357 
358 /*
359  * get dt name based on model and detail level:
360  *
361  * For boards that are a combination of a SoM plus a Baseboard:
362  *   Venice SoM part numbers are GW70xx where xx is:
363  *    7000-7019: same PCB with som dt of '0x'
364  *    7020-7039: same PCB with som dt of '2x'
365  *    7040-7059: same PCB with som dt of '4x'
366  *    7060-7079: same PCB with som dt of '6x'
367  *    7080-7099: same PCB with som dt of '8x'
368  *   Venice Baseboard part numbers are GW7xxx where xxx is:
369  *    7100-7199: same PCB with base dt of '71xx'
370  *    7200-7299: same PCB with base dt of '72xx'
371  *    7300-7399: same PCB with base dt of '73xx'
372  *    7400-7499: same PCB with base dt of '74xx'
373  *    7500-7599: same PCB with base dt of '75xx'
374  *    7600-7699: same PCB with base dt of '76xx'
375  *    7700-7799: same PCB with base dt of '77xx'
376  *    7800-7899: same PCB with base dt of '78xx'
377  *   DT name is comprised of:
378  *    gw<base dt>-<som dt>-[base-pcb-rev][base-bom-rev][som-pcb-rev][som-bom-rev]
379  *
380  * For board models from 7900-7999 each PCB is unique with its own dt:
381  *   DT name is comprised:
382  *    gw<model>-[pcb-rev][bom-rev]
383  *
384  */
385 #define snprintfcat(dest, sz, fmt, ...) \
386 	snprintf((dest) + strlen(dest), (sz) - strlen(dest), fmt, ##__VA_ARGS__)
gsc_get_dtb_name(int level,char * buf,int sz)387 const char *gsc_get_dtb_name(int level, char *buf, int sz)
388 {
389 	const char *pre = "imx8mm-venice-gw";
390 	int model, rev_pcb, rev_bom;
391 
392 	model = ((som_info.model[2] - '0') * 1000)
393 		+ ((som_info.model[3] - '0') * 100)
394 		+ ((som_info.model[4] - '0') * 10)
395 		+ (som_info.model[5] - '0');
396 	rev_pcb = tolower(get_pcb_rev(som_info.model));
397 	rev_bom = get_bom_rev(som_info.model);
398 
399 	/* som + baseboard*/
400 	if (base_info.model[0]) {
401 		/* baseboard id: 7100-7199->71; 7200-7299->72; etc */
402 		int base = ((base_info.model[2] - '0') * 10) + (base_info.model[3] - '0');
403 		/* som id: 7000-7019->1; 7020-7039->2; etc */
404 		int som = ((model % 100) / 20) * 2;
405 		int rev_base_pcb = tolower(get_pcb_rev(base_info.model));
406 		int rev_base_bom = get_bom_rev(base_info.model);
407 
408 		snprintf(buf, sz, "%s%2dxx-%dx", pre, base, som);
409 		switch (level) {
410 		case 0: /* full model (ie gw73xx-0x-a1a1) */
411 			if (rev_base_bom)
412 				snprintfcat(buf, sz, "-%c%d", rev_base_pcb, rev_base_bom);
413 			else
414 				snprintfcat(buf, sz, "-%c", rev_base_pcb);
415 			if (rev_bom)
416 				snprintfcat(buf, sz, "%c%d", rev_pcb, rev_bom);
417 			else
418 				snprintfcat(buf, sz, "%c", rev_pcb);
419 			break;
420 		case 1: /* don't care about SoM revision */
421 			if (rev_base_bom)
422 				snprintfcat(buf, sz, "-%c%d", rev_base_pcb, rev_base_bom);
423 			else
424 				snprintfcat(buf, sz, "-%c", rev_base_pcb);
425 			snprintfcat(buf, sz, "xx");
426 			break;
427 		case 2: /* don't care about baseboard revision */
428 			snprintfcat(buf, sz, "-xx");
429 			if (rev_bom)
430 				snprintfcat(buf, sz, "%c%d", rev_pcb, rev_bom);
431 			else
432 				snprintfcat(buf, sz, "%c", rev_pcb);
433 			break;
434 		case 3: /* don't care about SoM/baseboard revision */
435 			break;
436 		default:
437 			return NULL;
438 		}
439 	} else {
440 		snprintf(buf, sz, "%s%04d", pre, model);
441 		switch (level) {
442 		case 0: /* full model wth PCB and BOM revision first (ie gw7901-a1) */
443 			if (rev_bom)
444 				snprintfcat(buf, sz, "-%c%d", rev_pcb, rev_bom);
445 			else
446 				snprintfcat(buf, sz, "-%c", rev_pcb);
447 			break;
448 		case 1: /* don't care about BOM revision */
449 			snprintfcat(buf, sz, "-%c", rev_pcb);
450 			break;
451 		case 2: /* don't care about PCB or BOM revision */
452 			break;
453 		default:
454 			return NULL;
455 		}
456 	}
457 
458 	return buf;
459 }
460 
gsc_read(void)461 static int gsc_read(void)
462 {
463 	int ret;
464 
465 	ret = gsc_read_eeprom(1, GSC_EEPROM_ADDR, 1, &som_info);
466 	if (ret) {
467 		memset(&som_info, 0, sizeof(som_info));
468 		return ret;
469 	}
470 
471 	/* read optional baseboard EEPROM */
472 	return gsc_read_eeprom(2, 0x52, 2, &base_info);
473 }
474 
gsc_info(int verbose)475 static int gsc_info(int verbose)
476 {
477 	struct udevice *dev;
478 	unsigned char buf[16];
479 	char rev_pcb;
480 	int rev_bom;
481 
482 	if (!base_info.model[0]) {
483 		strcpy(venice_model, som_info.model);
484 		printf("Model   : %s\n", som_info.model);
485 		printf("Serial  : %d\n", som_info.serial);
486 		printf("MFGDate : %02x-%02x-%02x%02x\n",
487 		       som_info.mfgdate[0], som_info.mfgdate[1],
488 		       som_info.mfgdate[2], som_info.mfgdate[3]);
489 	} else {
490 		sprintf(venice_model, "GW%c%c%c%c-%c%c-",
491 			som_info.model[2], /* family */
492 			base_info.model[3], /* baseboard */
493 			base_info.model[4], base_info.model[5], /* subload of baseboard */
494 			som_info.model[4], som_info.model[5]); /* last 2digits of SOM */
495 
496 		/* baseboard revision */
497 		rev_pcb = get_pcb_rev(base_info.model);
498 		rev_bom = get_bom_rev(base_info.model);
499 		if (rev_bom)
500 			sprintf(venice_model + strlen(venice_model), "%c%d", rev_pcb, rev_bom);
501 		else
502 			sprintf(venice_model + strlen(venice_model), "%c", rev_pcb);
503 		/* som revision */
504 		rev_pcb = get_pcb_rev(som_info.model);
505 		rev_bom = get_bom_rev(som_info.model);
506 		if (rev_bom)
507 			sprintf(venice_model + strlen(venice_model), "%c%d", rev_pcb, rev_bom);
508 		else
509 			sprintf(venice_model + strlen(venice_model), "%c", rev_pcb);
510 
511 		if (verbose > 1) {
512 			printf("SOM     : %s %d %02x-%02x-%02x%02x\n",
513 			       som_info.model, som_info.serial,
514 			       som_info.mfgdate[0], som_info.mfgdate[1],
515 			       som_info.mfgdate[2], som_info.mfgdate[3]);
516 			printf("BASE    : %s %d %02x-%02x-%02x%02x\n",
517 			       base_info.model, base_info.serial,
518 			       base_info.mfgdate[0], base_info.mfgdate[1],
519 			       base_info.mfgdate[2], base_info.mfgdate[3]);
520 		}
521 		printf("Model   : %s\n", venice_model);
522 		printf("Serial  : %d\n", som_info.serial);
523 		printf("MFGDate : %02x-%02x-%02x%02x\n",
524 		       som_info.mfgdate[0], som_info.mfgdate[1],
525 		       som_info.mfgdate[2], som_info.mfgdate[3]);
526 	}
527 
528 	/* Display RTC */
529 	puts("RTC     : ");
530 	dev = gsc_get_dev(1, GSC_RTC_ADDR);
531 	if (!dev) {
532 		puts("Failed to probe GSC RTC\n");
533 	} else {
534 		dm_i2c_read(dev, 0, buf, 6);
535 		printf("%d\n", buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24);
536 	}
537 
538 	return 0;
539 }
540 
gsc_init(int quiet)541 int gsc_init(int quiet)
542 {
543 	unsigned char buf[16];
544 	struct udevice *dev;
545 	int ret;
546 
547 	/*
548 	 * On a board with a missing/depleted backup battery for GSC, the
549 	 * board may be ready to probe the GSC before its firmware is
550 	 * running.  We will wait here indefinately for the GSC/EEPROM.
551 	 */
552 	while (1) {
553 		/* probe device */
554 		dev = gsc_get_dev(1, GSC_SC_ADDR);
555 		if (dev)
556 			break;
557 		mdelay(1);
558 	}
559 
560 	ret = dm_i2c_read(dev, 0, buf, sizeof(buf));
561 	if (ret) {
562 		puts("ERROR: Failed reading GSC\n");
563 		return ret;
564 	}
565 	gsc_read();
566 
567 	/* banner */
568 	if (!quiet) {
569 		printf("GSC     : v%d 0x%04x", buf[GSC_SC_FWVER],
570 		       buf[GSC_SC_FWCRC] | buf[GSC_SC_FWCRC + 1] << 8);
571 		printf(" RST:%s", gsc_get_rst_cause(dev));
572 		printf("\n");
573 		gsc_info(1);
574 	}
575 
576 	if (ret)
577 		hang();
578 
579 	return ((16 << som_info.sdram_size) / 1024);
580 }
581 
gsc_get_model(void)582 const char *gsc_get_model(void)
583 {
584 	return venice_model;
585 }
586 
587 #if !(IS_ENABLED(CONFIG_SPL_BUILD))
gsc_sleep(unsigned long secs)588 static int gsc_sleep(unsigned long secs)
589 {
590 	unsigned char reg;
591 	struct udevice *dev;
592 	int ret;
593 
594 	/* probe device */
595 	dev = gsc_get_dev(1, GSC_SC_ADDR);
596 	if (!dev)
597 		return -ENODEV;
598 
599 	printf("GSC Sleeping for %ld seconds\n", secs);
600 	reg = (secs >> 24) & 0xff;
601 	ret = dm_i2c_write(dev, 9, &reg, 1);
602 	if (ret)
603 		goto err;
604 	reg = (secs >> 16) & 0xff;
605 	ret = dm_i2c_write(dev, 8, &reg, 1);
606 	if (ret)
607 		goto err;
608 	reg = (secs >> 8) & 0xff;
609 	ret = dm_i2c_write(dev, 7, &reg, 1);
610 	if (ret)
611 		goto err;
612 	reg = secs & 0xff;
613 	ret = dm_i2c_write(dev, 6, &reg, 1);
614 	if (ret)
615 		goto err;
616 	ret = dm_i2c_read(dev, GSC_SC_CTRL1, &reg, 1);
617 	if (ret)
618 		goto err;
619 	reg |= (1 << 2);
620 	ret = dm_i2c_write(dev, GSC_SC_CTRL1, &reg, 1);
621 	if (ret)
622 		goto err;
623 	reg &= ~(1 << 2);
624 	reg |= 0x3;
625 	ret = dm_i2c_write(dev, GSC_SC_CTRL1, &reg, 1);
626 	if (ret)
627 		goto err;
628 
629 	return 0;
630 
631 err:
632 	printf("i2c error\n");
633 	return ret;
634 }
635 
gsc_boot_wd_disable(void)636 static int gsc_boot_wd_disable(void)
637 {
638 	u8 reg;
639 	struct udevice *dev;
640 	int ret;
641 
642 	/* probe device */
643 	dev = gsc_get_dev(1, GSC_SC_ADDR);
644 	if (!dev)
645 		return -ENODEV;
646 
647 	ret = dm_i2c_read(dev, GSC_SC_CTRL1, &reg, 1);
648 	if (ret)
649 		goto err;
650 	reg |= (1 << GSC_SC_CTRL1_WDDIS);
651 	reg &= ~(1 << GSC_SC_CTRL1_BOOT_CHK);
652 	ret = dm_i2c_write(dev, GSC_SC_CTRL1, &reg, 1);
653 	if (ret)
654 		goto err;
655 	puts("GSC     : boot watchdog disabled\n");
656 
657 	return 0;
658 
659 err:
660 	printf("i2c error");
661 	return ret;
662 }
663 
do_gsc(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])664 static int do_gsc(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
665 {
666 	if (argc < 2)
667 		return gsc_info(2);
668 
669 	if (strcasecmp(argv[1], "sleep") == 0) {
670 		if (argc < 3)
671 			return CMD_RET_USAGE;
672 		if (!gsc_sleep(simple_strtoul(argv[2], NULL, 10)))
673 			return CMD_RET_SUCCESS;
674 	} else if (strcasecmp(argv[1], "hwmon") == 0) {
675 		if (!gsc_hwmon())
676 			return CMD_RET_SUCCESS;
677 	} else if (strcasecmp(argv[1], "wd-disable") == 0) {
678 		if (!gsc_boot_wd_disable())
679 			return CMD_RET_SUCCESS;
680 	}
681 
682 	return CMD_RET_USAGE;
683 }
684 
685 U_BOOT_CMD(gsc, 4, 1, do_gsc, "Gateworks System Controller",
686 	   "[sleep <secs>]|[hwmon]|[wd-disable]\n");
687 #endif
688