1 // SPDX-License-Identifier: GPL-2.0+
3 * Commands to deal with Synology specifics.
14 #include <spi_flash.h>
15 #include <linux/mtd/mtd.h>
18 #include "../drivers/ddr/marvell/axp/ddr3_init.h"
21 #define SYNO_SN_TAG "SN="
22 #define SYNO_CHKSUM_TAG "CHK="
25 static int do_syno_populate(int argc, char *const argv[])
27 unsigned int bus = CONFIG_SF_DEFAULT_BUS;
28 unsigned int cs = CONFIG_SF_DEFAULT_CS;
29 unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
30 unsigned int mode = CONFIG_SF_DEFAULT_MODE;
31 struct spi_flash *flash;
32 unsigned long addr = 0x80000; /* XXX: parameterize this? */
33 loff_t offset = 0x007d0000;
34 loff_t len = 0x00010000;
40 /* XXX: arg parsing to select flash here? */
42 flash = spi_flash_probe(bus, cs, speed, mode);
44 printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
48 buf = map_physmem(addr, len, MAP_WRBACK);
50 puts("Failed to map physical memory\n");
54 ret = spi_flash_read(flash, offset, len, buf);
56 puts("Failed to read from SPI flash\n");
60 for (n = 0; n < ETHADDR_MAX; n++) {
61 char ethaddr[ETH_ALEN];
63 unsigned char csum = 0;
65 for (i = 0, bufp = buf + n * 7; i < ETH_ALEN; i++) {
70 if (!sum) /* MAC address empty */
72 if (csum != bufp[i]) { /* seventh byte is checksum value */
73 printf("Invalid MAC address for interface %d!\n", n);
77 sprintf(var, "ethaddr");
79 sprintf(var, "eth%daddr", n);
80 snprintf(val, sizeof(val) - 1,
81 "%02x:%02x:%02x:%02x:%02x:%02x",
82 ethaddr[0], ethaddr[1], ethaddr[2],
83 ethaddr[3], ethaddr[4], ethaddr[5]);
84 printf("parsed %s = %s\n", var, val);
87 if (!strncmp(buf + 32, SYNO_SN_TAG, strlen(SYNO_SN_TAG))) {
92 snp = bufp = buf + 32 + strlen(SYNO_SN_TAG);
93 for (n = 0; bufp[n] && bufp[n] != ','; n++)
97 /* should come right after, but you never know */
98 bufp = strstr(bufp + n + 1, SYNO_CHKSUM_TAG);
100 printf("Serial number checksum tag missing!\n");
104 csump = bufp += strlen(SYNO_CHKSUM_TAG);
105 for (n = 0; bufp[n] && bufp[n] != ','; n++)
109 if (strict_strtoul(csump, 10, &c) || c != csum) {
110 puts("Invalid serial number found!\n");
114 printf("parsed SN = %s\n", snp);
116 } else { /* old style format */
117 unsigned char csum = 0;
119 for (n = 0, bufp = buf + 32; n < 10; n++)
122 if (csum != bufp[n]) {
123 puts("Invalid serial number found!\n");
128 printf("parsed SN = %s\n", buf + 32);
129 env_set("SN", buf + 32);
132 unmap_physmem(buf, len);
136 /* map bit position to function in POWER_MNG_CTRL_REG */
137 static const char * const pwr_mng_bit_func[] = {
139 "ge3", "ge2", "ge1", "ge0",
140 "pcie00", "pcie01", "pcie02", "pcie03",
141 "pcie10", "pcie11", "pcie12", "pcie13",
143 "sata0_link", "sata0_core",
146 "usb0", "usb1", "usb2",
147 "idma", "xor0", "crypto",
152 "sata1_link", "sata1_core",
156 static int do_syno_clk_gate(int argc, char *const argv[])
158 u32 pwr_mng_ctrl_reg = reg_read(POWER_MNG_CTRL_REG);
159 const char *func, *state;
165 if (!strcmp(argv[1], "get")) {
166 puts("Clock Gating:\n");
167 for (i = 0; i < 32; i++) {
168 func = pwr_mng_bit_func[i];
171 state = pwr_mng_ctrl_reg & (1 << i) ? "ON" : "OFF";
172 printf("%s:\t\t%s\n", func, state);
178 if (!strcmp(argv[1], "set")) {
181 for (i = 0; i < 32; i++) {
182 if (!pwr_mng_bit_func[i])
184 if (!strcmp(func, pwr_mng_bit_func[i]))
188 printf("Error: name '%s' not known\n", func);
191 val = state[0] != '0';
192 pwr_mng_ctrl_reg |= (val << i);
193 pwr_mng_ctrl_reg &= ~(!val << i);
194 reg_write(POWER_MNG_CTRL_REG, pwr_mng_ctrl_reg);
199 static int do_syno(struct cmd_tbl *cmdtp, int flag, int argc,
212 if (!strcmp(cmd, "populate_env"))
213 ret = do_syno_populate(argc, argv);
214 else if (!strcmp(cmd, "clk_gate"))
215 ret = do_syno_clk_gate(argc, argv);
220 return CMD_RET_USAGE;
225 "Synology specific commands",
226 "populate_env - Read vendor data from SPI flash into environment\n"
227 "clk_gate (get|set name 1|0) - Manage clock gating\n"