blob: a120c3123ffb3089b41b1c4e181841737b14beaf [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Phil Suttere057b1b2015-12-25 14:41:26 +01002/*
3 * Commands to deal with Synology specifics.
4 *
5 * Copyright (C) 2015 Phil Sutter <phil@nwl.cc>
Phil Suttere057b1b2015-12-25 14:41:26 +01006 */
7
8#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -06009#include <command.h>
Phil Suttere057b1b2015-12-25 14:41:26 +010010#include <div64.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060011#include <env.h>
Simon Glass274e0b02020-05-10 11:39:56 -060012#include <net.h>
Phil Suttere057b1b2015-12-25 14:41:26 +010013#include <spi.h>
14#include <spi_flash.h>
15#include <linux/mtd/mtd.h>
16
17#include <asm/io.h>
18#include "../drivers/ddr/marvell/axp/ddr3_init.h"
19
Phil Suttere057b1b2015-12-25 14:41:26 +010020#define ETHADDR_MAX 4
21#define SYNO_SN_TAG "SN="
22#define SYNO_CHKSUM_TAG "CHK="
23
24
Simon Glassed38aef2020-05-10 11:40:03 -060025static int do_syno_populate(int argc, char *const argv[])
Phil Suttere057b1b2015-12-25 14:41:26 +010026{
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;
35 char *buf, *bufp;
36 char var[128];
37 char val[128];
38 int ret, n;
39
40 /* XXX: arg parsing to select flash here? */
41
42 flash = spi_flash_probe(bus, cs, speed, mode);
43 if (!flash) {
44 printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
45 return 1;
46 }
47
48 buf = map_physmem(addr, len, MAP_WRBACK);
49 if (!buf) {
50 puts("Failed to map physical memory\n");
51 return 1;
52 }
53
54 ret = spi_flash_read(flash, offset, len, buf);
55 if (ret) {
56 puts("Failed to read from SPI flash\n");
57 goto out_unmap;
58 }
59
60 for (n = 0; n < ETHADDR_MAX; n++) {
61 char ethaddr[ETH_ALEN];
62 int i, sum = 0;
63 unsigned char csum = 0;
64
65 for (i = 0, bufp = buf + n * 7; i < ETH_ALEN; i++) {
66 sum += bufp[i];
67 csum += bufp[i];
68 ethaddr[i] = bufp[i];
69 }
70 if (!sum) /* MAC address empty */
71 continue;
72 if (csum != bufp[i]) { /* seventh byte is checksum value */
73 printf("Invalid MAC address for interface %d!\n", n);
74 continue;
75 }
76 if (n == 0)
77 sprintf(var, "ethaddr");
78 else
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);
Simon Glass6a38e412017-08-03 12:22:09 -060085 env_set(var, val);
Phil Suttere057b1b2015-12-25 14:41:26 +010086 }
87 if (!strncmp(buf + 32, SYNO_SN_TAG, strlen(SYNO_SN_TAG))) {
88 char *snp, *csump;
89 int csum = 0;
90 unsigned long c;
91
92 snp = bufp = buf + 32 + strlen(SYNO_SN_TAG);
93 for (n = 0; bufp[n] && bufp[n] != ','; n++)
94 csum += bufp[n];
95 bufp[n] = '\0';
96
97 /* should come right after, but you never know */
98 bufp = strstr(bufp + n + 1, SYNO_CHKSUM_TAG);
99 if (!bufp) {
100 printf("Serial number checksum tag missing!\n");
101 goto out_unmap;
102 }
103
104 csump = bufp += strlen(SYNO_CHKSUM_TAG);
105 for (n = 0; bufp[n] && bufp[n] != ','; n++)
106 ;
107 bufp[n] = '\0';
108
109 if (strict_strtoul(csump, 10, &c) || c != csum) {
110 puts("Invalid serial number found!\n");
111 ret = 1;
112 goto out_unmap;
113 }
114 printf("parsed SN = %s\n", snp);
Simon Glass6a38e412017-08-03 12:22:09 -0600115 env_set("SN", snp);
Phil Suttere057b1b2015-12-25 14:41:26 +0100116 } else { /* old style format */
117 unsigned char csum = 0;
118
119 for (n = 0, bufp = buf + 32; n < 10; n++)
120 csum += bufp[n];
121
122 if (csum != bufp[n]) {
123 puts("Invalid serial number found!\n");
124 ret = 1;
125 goto out_unmap;
126 }
127 bufp[n] = '\0';
128 printf("parsed SN = %s\n", buf + 32);
Simon Glass6a38e412017-08-03 12:22:09 -0600129 env_set("SN", buf + 32);
Phil Suttere057b1b2015-12-25 14:41:26 +0100130 }
131out_unmap:
132 unmap_physmem(buf, len);
133 return ret;
134}
135
136/* map bit position to function in POWER_MNG_CTRL_REG */
137static const char * const pwr_mng_bit_func[] = {
138 "audio",
139 "ge3", "ge2", "ge1", "ge0",
140 "pcie00", "pcie01", "pcie02", "pcie03",
141 "pcie10", "pcie11", "pcie12", "pcie13",
142 "bp",
143 "sata0_link", "sata0_core",
144 "lcd",
145 "sdio",
146 "usb0", "usb1", "usb2",
147 "idma", "xor0", "crypto",
148 NULL,
149 "tdm",
150 "pcie20", "pcie30",
151 "xor1",
152 "sata1_link", "sata1_core",
153 NULL,
154};
155
Simon Glassed38aef2020-05-10 11:40:03 -0600156static int do_syno_clk_gate(int argc, char *const argv[])
Phil Suttere057b1b2015-12-25 14:41:26 +0100157{
158 u32 pwr_mng_ctrl_reg = reg_read(POWER_MNG_CTRL_REG);
159 const char *func, *state;
160 int i, val;
161
162 if (argc < 2)
163 return -1;
164
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];
169 if (!func)
170 continue;
171 state = pwr_mng_ctrl_reg & (1 << i) ? "ON" : "OFF";
172 printf("%s:\t\t%s\n", func, state);
173 }
174 return 0;
175 }
176 if (argc < 4)
177 return -1;
178 if (!strcmp(argv[1], "set")) {
179 func = argv[2];
180 state = argv[3];
181 for (i = 0; i < 32; i++) {
182 if (!pwr_mng_bit_func[i])
183 continue;
184 if (!strcmp(func, pwr_mng_bit_func[i]))
185 break;
186 }
187 if (i == 32) {
188 printf("Error: name '%s' not known\n", func);
189 return -1;
190 }
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);
195 }
196 return 0;
197}
198
Simon Glassed38aef2020-05-10 11:40:03 -0600199static int do_syno(struct cmd_tbl *cmdtp, int flag, int argc,
200 char *const argv[])
Phil Suttere057b1b2015-12-25 14:41:26 +0100201{
202 const char *cmd;
203 int ret = 0;
204
205 if (argc < 2)
206 goto usage;
207
208 cmd = argv[1];
209 --argc;
210 ++argv;
211
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);
216
217 if (ret != -1)
218 return ret;
219usage:
220 return CMD_RET_USAGE;
221}
222
223U_BOOT_CMD(
224 syno, 5, 1, do_syno,
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"
228);