5 * (C) Copyright 2009 Semihalf, Grzegorz Bernacki
7 * SPDX-License-Identifier: GPL-2.0+
16 DECLARE_GLOBAL_DATA_PTR;
18 static uchar user_out;
20 static const char *led_names[] = {
33 static int msp430_xfer(const void *dout, void *din)
37 err = spi_xfer(NULL, MTC_TRANSFER_SIZE, dout, din,
38 SPI_XFER_BEGIN | SPI_XFER_END);
40 /* The MSP chip needs time to ready itself for the next command */
46 static void mtc_calculate_checksum(tx_msp_cmd *packet)
51 buff = (uchar *) packet;
53 for (i = 0; i < 6; i++)
54 packet->cks += buff[i];
57 static int do_mtc_led(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
65 return cmd_usage(cmdtp);
67 memset(&pcmd, 0, sizeof(pcmd));
68 memset(&prx, 0, sizeof(prx));
70 pcmd.cmd = CMD_SET_LED;
73 for (i = 0; strlen(led_names[i]) != 0; i++) {
74 if (strncmp(argv[1], led_names[i], strlen(led_names[i])) == 0) {
80 if (pcmd.cmd_val0 == 0xff) {
81 printf("Usage:\n%s\n", cmdtp->help);
86 if (strncmp(argv[2], "red", 3) == 0)
88 else if (strncmp(argv[2], "green", 5) == 0)
90 else if (strncmp(argv[2], "orange", 6) == 0)
97 pcmd.cmd_val2 = simple_strtol(argv[3], NULL, 10);
101 pcmd.user_out = user_out;
103 mtc_calculate_checksum(&pcmd);
104 err = msp430_xfer(&pcmd, &prx);
109 static int do_mtc_key(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
115 memset(&pcmd, 0, sizeof(pcmd));
116 memset(&prx, 0, sizeof(prx));
118 pcmd.cmd = CMD_GET_VIM;
119 pcmd.user_out = user_out;
121 mtc_calculate_checksum(&pcmd);
122 err = msp430_xfer(&pcmd, &prx);
125 /* function returns '0' if key is pressed */
126 err = (prx.input & 0x80) ? 0 : 1;
132 static int do_mtc_digout(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
137 uchar channel_mask = 0;
140 return cmd_usage(cmdtp);
142 if (strncmp(argv[1], "on", 2) == 0)
144 if (strncmp(argv[2], "on", 2) == 0)
147 memset(&pcmd, 0, sizeof(pcmd));
148 memset(&prx, 0, sizeof(prx));
150 pcmd.cmd = CMD_GET_VIM;
151 pcmd.user_out = channel_mask;
152 user_out = channel_mask;
154 mtc_calculate_checksum(&pcmd);
155 err = msp430_xfer(&pcmd, &prx);
160 static int do_mtc_digin(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
165 uchar channel_num = 0;
168 return cmd_usage(cmdtp);
170 channel_num = simple_strtol(argv[1], NULL, 10);
171 if ((channel_num != 1) && (channel_num != 2)) {
172 printf("mtc digin: invalid parameter - must be '1' or '2'\n");
176 memset(&pcmd, 0, sizeof(pcmd));
177 memset(&prx, 0, sizeof(prx));
179 pcmd.cmd = CMD_GET_VIM;
180 pcmd.user_out = user_out;
182 mtc_calculate_checksum(&pcmd);
183 err = msp430_xfer(&pcmd, &prx);
186 /* function returns '0' when digin is on */
187 err = (prx.input & channel_num) ? 0 : 1;
193 static int do_mtc_appreg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
202 memset(&pcmd, 0, sizeof(pcmd));
203 memset(&prx, 0, sizeof(prx));
205 pcmd.cmd = CMD_WD_PARA;
206 pcmd.cmd_val0 = 5; /* max. Count */
207 pcmd.cmd_val1 = 5; /* max. Time */
208 pcmd.cmd_val2 = 0; /* =0 means read appreg */
209 pcmd.user_out = user_out;
211 mtc_calculate_checksum(&pcmd);
212 err = msp430_xfer(&pcmd, &prx);
214 /* on success decide between read or write */
217 appreg = simple_strtol(argv[1], NULL, 10);
219 printf("mtc appreg: invalid parameter - "
220 "must be between 1 and 255\n");
223 memset(&pcmd, 0, sizeof(pcmd));
224 pcmd.cmd = CMD_WD_PARA;
225 pcmd.cmd_val0 = prx.ack3; /* max. Count */
226 pcmd.cmd_val1 = prx.ack0; /* max. Time */
227 pcmd.cmd_val2 = appreg; /* !=0 means write appreg */
228 pcmd.user_out = user_out;
229 memset(&prx, 0, sizeof(prx));
231 mtc_calculate_checksum(&pcmd);
232 err = msp430_xfer(&pcmd, &prx);
234 sprintf(buf, "%d", prx.ack2);
235 setenv("appreg", buf);
242 static int do_mtc_version(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
248 memset(&pcmd, 0, sizeof(pcmd));
249 memset(&prx, 0, sizeof(prx));
251 pcmd.cmd = CMD_FW_VERSION;
252 pcmd.user_out = user_out;
254 mtc_calculate_checksum(&pcmd);
255 err = msp430_xfer(&pcmd, &prx);
258 printf("FW V%d.%d.%d / HW %d\n",
259 prx.ack0, prx.ack1, prx.ack3, prx.ack2);
265 static int do_mtc_state(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
271 memset(&pcmd, 0, sizeof(pcmd));
272 memset(&prx, 0, sizeof(prx));
274 pcmd.cmd = CMD_WD_WDSTATE;
276 pcmd.user_out = user_out;
278 mtc_calculate_checksum(&pcmd);
279 err = msp430_xfer(&pcmd, &prx);
282 printf("State %02Xh\n", prx.state);
283 printf("Input %02Xh\n", prx.input);
284 printf("UserWD %02Xh\n", prx.ack2);
285 printf("Sys WD %02Xh\n", prx.ack3);
286 printf("WD Timout %02Xh\n", prx.ack0);
287 printf("eSysState %02Xh\n", prx.ack1);
293 static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
295 cmd_tbl_t cmd_mtc_sub[] = {
296 U_BOOT_CMD_MKENT(led, 3, 1, do_mtc_led,
298 "[ledname] [state] [blink]\n"
299 " - lednames: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
300 " - state: off red green orange\n"
301 " - blink: blink interval in 100ms steps (1 - 10; 0 = static)\n"),
302 U_BOOT_CMD_MKENT(key, 0, 1, do_mtc_key,
303 "returns state of user key", ""),
304 U_BOOT_CMD_MKENT(version, 0, 1, do_mtc_version,
305 "returns firmware version of supervisor uC", ""),
306 U_BOOT_CMD_MKENT(appreg, 1, 1, do_mtc_appreg,
307 "reads or writes appreg value and stores in environment "
309 "[value] - value (1 - 255) to write to appreg"),
310 U_BOOT_CMD_MKENT(digin, 1, 1, do_mtc_digin,
311 "returns state of digital input",
312 "<channel_num> - get state of digital input (1 or 2)\n"),
313 U_BOOT_CMD_MKENT(digout, 2, 1, do_mtc_digout,
314 "sets digital outputs",
315 "<on|off> <on|off>- set state of digital output 1 and 2\n"),
316 U_BOOT_CMD_MKENT(state, 0, 1, do_mtc_state,
317 "displays state", ""),
318 U_BOOT_CMD_MKENT(help, 4, 1, do_mtc_help, "get help",
319 "[command] - get help for command\n"),
322 static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
324 extern int _do_help(cmd_tbl_t *cmd_start, int cmd_items,
325 cmd_tbl_t *cmdtp, int flag,
326 int argc, char * const argv[]);
327 #ifdef CONFIG_SYS_LONGHELP
330 return _do_help(&cmd_mtc_sub[0],
331 ARRAY_SIZE(cmd_mtc_sub), cmdtp, flag, argc, argv);
334 int cmd_mtc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
339 c = find_cmd_tbl(argv[1], &cmd_mtc_sub[0], ARRAY_SIZE(cmd_mtc_sub));
343 return c->cmd(c, flag, argc, argv);
345 /* Unrecognized command */
346 return cmd_usage(cmdtp);
352 U_BOOT_CMD(mtc, 5, 1, cmd_mtc,
353 "special commands for digsyMTC",
354 "[subcommand] [args...]\n"
355 "Subcommands list:\n"
356 "led [ledname] [state] [blink] - set state of leds\n"
357 " [ledname]: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
358 " [state]: off red green orange\n"
359 " [blink]: blink interval in 100ms steps (1 - 10; 0 = static)\n"
360 "key - returns state of user key\n"
361 "version - returns firmware version of supervisor uC\n"
362 "appreg [value] - reads (in environment variable 'appreg') or writes"
364 " [value]: value (1 - 255) to write to appreg\n"
365 "digin [channel] - returns state of digital input (1 or 2)\n"
366 "digout <on|off> <on|off> - sets state of two digital outputs\n"
367 "state - displays state\n"
368 "help [subcommand] - get help for subcommand\n"