diff --git a/README.md b/README.md index 4a79d6e..2fae991 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,8 @@ Where: dump data from I2C/SPI device i2cdetect [-b, --bus] attempt to detect devices on I2C bus reginfo [--script] dump current status of pinmux registers - gpio (scan|mux) GPIO utilities + gpio (dump|scan|mux) GPIO utilities; dump [--json] prints + every pin's mux and level, read-only trace [--skip=usleep] [program arguments] dump original firmware calls and data structures -h, --help this help diff --git a/src/main.c b/src/main.c index 78c3332..41b14f2 100644 --- a/src/main.c +++ b/src/main.c @@ -118,7 +118,8 @@ void print_usage() { " reginfo [--script] dump current status of pinmux registers\n" " reginfo --pads what each pad carries, and what else it " "could\n" - " gpio (scan|mux) GPIO utilities\n" + " gpio (dump|scan|mux) GPIO utilities; dump [--json] prints\n" + " every pin's mux and level, read-only\n" " clocks|freq [--json] show CPU/peripheral PLL, DDR clock and\n" " per-die HPM bin (Hisilicon V4 / Goke " "V300\n" diff --git a/src/reginfo.c b/src/reginfo.c index 55ef8d5..e6ef730 100644 --- a/src/reginfo.c +++ b/src/reginfo.c @@ -4508,9 +4508,160 @@ static int gpio_scan_cmd() { return EXIT_SUCCESS; } +/* `ipctool gpio dump [--json]`: every GPIO pin of the SoC, once -- the + * function its pad carries now and, where that is the GPIO, the direction + * and level. + * + * `gpio scan` answers "what changes", loops until Ctrl-C and prints only + * pins already muxed to GPIO; `gpio mux` writes. Neither answers "what is + * this board's GPIO state", which is the question when one firmware drives + * a peripheral and another does not: dump both and diff. Nothing here + * writes a register, and it exits on its own, so it runs under the bare + * busybox of a stock firmware with no `timeout` to stop it. */ +static int gpio_dump_cmd(int argc, char **argv) { + const struct option long_options[] = { + {"json", no_argument, NULL, 'j'}, + {NULL, 0, NULL, 0}, + }; + bool json = false; + int res; + + optind = 1; + while ((res = getopt_long_only(argc, argv, "j", long_options, NULL)) != + -1) { + switch (res) { + case 'j': + json = true; + break; + default: + fprintf(stderr, "Usage: ipctool gpio dump [--json]\n"); + return EXIT_FAILURE; + } + } + + getchipname(); + int GPIO_Groups = 0; + size_t GPIO_Base = 0; + size_t GPIO_Offset = 0; + /* SigmaStar and Novatek keep their GPIO state elsewhere than the PL061 + * banks walked below; refuse rather than print a plausible lie. */ + if (sstar_gpio_supported() || novatek_gpio_supported() || + !get_chip_gpio_adress(&GPIO_Base, &GPIO_Offset, &GPIO_Groups)) { + fprintf(stderr, "gpio dump: not supported on this SoC yet\n"); + return EXIT_FAILURE; + } + + /* No pin-mux table at all: every pad would come back unclassified, and + * a dump that cannot say which pins are GPIOs is not one. */ + if (ipchw_padmux_by_prefix("", NULL, 0) < 0) { + fprintf(stderr, "gpio dump: no pin-mux table for this SoC\n"); + return EXIT_FAILURE; + } + + bool failed = false; + if (json) + printf("["); + bool first = true; + for (int group = 0; group < GPIO_Groups; group++) { + size_t bank = GPIO_Base + group * GPIO_Offset; + uint32_t dir = 0, data = 0; + /* PL061: the data register is masked by address bits 9..2, so +0x3fc + * reads all eight pins at once. */ + bool readable = mem_reg(bank + 0x400, &dir, OP_READ) && + mem_reg(bank + 0x3fc, &data, OP_READ); + if (!readable) { + fprintf(stderr, "gpio dump: GPIO%d bank at %#zx unreadable\n", + group, bank); + failed = true; + } + + for (int pin = 0; pin < 8; pin++) { + int pad = group * 8 + pin; + char gpio[16]; + snprintf(gpio, sizeof(gpio), "GPIO%d_%d", group, pin); + + /* What the pad carries, per ipchw_padmux_get(): + * 1 a named row; the GPIO one is flagged F_GPIO + * 0 a selector the table does not name: unknown, and + * certainly not proof of GPIO + * NO_PAD the table has no row for the pad, so nothing can + * select another function onto it: it is the GPIO + * other <0 the mux register could not be read */ + ipchw_padmux_t now; + int got = ipchw_padmux_get(pad, &now); + const char *mux, *func; + bool is_gpio; + if (got == 1) { + is_gpio = now.flags & IPCHW_PADMUX_F_GPIO; + mux = is_gpio ? "gpio" : "function"; + func = now.func_name; + } else if (got == 0) { + is_gpio = false; + mux = "unknown"; + func = NULL; + } else if (got == IPCHW_PADMUX_NO_PAD) { + is_gpio = true; + mux = "none"; + func = NULL; + } else { + is_gpio = false; + mux = "unreadable"; + func = NULL; + fprintf(stderr, "gpio dump: %s pad mux unreadable (%d)\n", gpio, + got); + failed = true; + } + bool show = readable && is_gpio; + int out = dir >> pin & 1, level = data >> pin & 1; + + if (json) { + printf("%s\n {\"gpio\": \"%s\", \"pad\": %d, \"mux\": \"%s\", " + "\"function\": ", + first ? "" : ",", gpio, pad, mux); + if (func) + printf("\"%s\"", func); + else + printf("null"); + printf(", \"gpio_muxed\": %s", is_gpio ? "true" : "false"); + if (show) + printf(", \"dir\": \"%s\", \"value\": %d", + out ? "out" : "in", level); + else if (is_gpio) + printf(", \"error\": \"bank unreadable\""); + printf("}"); + } else { + /* The function, or the mux state in brackets when there is + * no named function. Pad the column only when a state follows + * it, so no line ends in spaces and two dumps diff cleanly. */ + char label[40]; + if (func) + snprintf(label, sizeof(label), "%s", func); + else + snprintf(label, sizeof(label), "[%s]", mux); + printf("%-8s pad %-4d ", gpio, pad); + if (show) + printf("%-20s %-3s %d\n", label, out ? "out" : "in", level); + else if (is_gpio) + printf("%-20s bank unreadable\n", label); + else + printf("%s\n", label); + } + first = false; + } + } + if (json) + printf("\n]\n"); + + /* A partial dump is still printed, well-formed, but never reported as a + * complete one. */ + return failed ? EXIT_FAILURE : EXIT_SUCCESS; +} + int gpio_cmd(int argc, char **argv) { if (argc > 1) { - if (!strcmp(argv[1], "scan")) + if (!strcmp(argv[1], "dump")) + return gpio_dump_cmd(argc - 1, argv + 1); + else if (!strcmp(argv[1], "scan")) return gpio_scan_cmd(); else if (!strcmp(argv[1], "mux")) return gpio_mux_cmd(argc - 1, argv + 1);