blob: f17ec550951a4d377c9b3601bf014d1287acc7d9 [file] [log] [blame]
Grzegorz Bernacki12304872009-06-12 11:33:54 +02001/*
2 * (C) Copyright 2009
3 * Werner Pfister <Pfister_Werner@intercontrol.de>
4 *
5 * (C) Copyright 2009 Semihalf, Grzegorz Bernacki
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Grzegorz Bernacki12304872009-06-12 11:33:54 +02008 */
9
10#include <common.h>
11#include <command.h>
12#include <mpc5xxx.h>
13#include "spi.h"
14#include "cmd_mtc.h"
15
16DECLARE_GLOBAL_DATA_PTR;
17
Anatolij Gustschinceb39702011-05-29 21:18:51 +000018static uchar user_out;
19
Grzegorz Bernacki12304872009-06-12 11:33:54 +020020static const char *led_names[] = {
21 "diag",
22 "can1",
23 "can2",
24 "can3",
25 "can4",
26 "usbpwr",
27 "usbbusy",
28 "user1",
29 "user2",
30 ""
31};
32
Detlev Zundel0b40bd42009-08-05 18:37:44 +020033static int msp430_xfer(const void *dout, void *din)
34{
35 int err;
36
37 err = spi_xfer(NULL, MTC_TRANSFER_SIZE, dout, din,
38 SPI_XFER_BEGIN | SPI_XFER_END);
39
40 /* The MSP chip needs time to ready itself for the next command */
41 udelay(1000);
42
43 return err;
44}
45
Grzegorz Bernacki12304872009-06-12 11:33:54 +020046static void mtc_calculate_checksum(tx_msp_cmd *packet)
47{
48 int i;
49 uchar *buff;
50
51 buff = (uchar *) packet;
52
53 for (i = 0; i < 6; i++)
54 packet->cks += buff[i];
55}
56
Wolfgang Denk54841ab2010-06-28 22:00:46 +020057static int do_mtc_led(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki12304872009-06-12 11:33:54 +020058{
59 tx_msp_cmd pcmd;
60 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +020061 int err;
Grzegorz Bernacki12304872009-06-12 11:33:54 +020062 int i;
63
Wolfgang Denk47e26b12010-07-17 01:06:04 +020064 if (argc < 2)
65 return cmd_usage(cmdtp);
Grzegorz Bernacki12304872009-06-12 11:33:54 +020066
67 memset(&pcmd, 0, sizeof(pcmd));
68 memset(&prx, 0, sizeof(prx));
69
70 pcmd.cmd = CMD_SET_LED;
71
72 pcmd.cmd_val0 = 0xff;
73 for (i = 0; strlen(led_names[i]) != 0; i++) {
74 if (strncmp(argv[1], led_names[i], strlen(led_names[i])) == 0) {
75 pcmd.cmd_val0 = i;
76 break;
77 }
78 }
79
80 if (pcmd.cmd_val0 == 0xff) {
81 printf("Usage:\n%s\n", cmdtp->help);
82 return -1;
83 }
84
85 if (argc >= 3) {
86 if (strncmp(argv[2], "red", 3) == 0)
87 pcmd.cmd_val1 = 1;
88 else if (strncmp(argv[2], "green", 5) == 0)
89 pcmd.cmd_val1 = 2;
90 else if (strncmp(argv[2], "orange", 6) == 0)
91 pcmd.cmd_val1 = 3;
92 else
93 pcmd.cmd_val1 = 0;
94 }
95
96 if (argc >= 4)
97 pcmd.cmd_val2 = simple_strtol(argv[3], NULL, 10);
98 else
99 pcmd.cmd_val2 = 0;
100
Anatolij Gustschinceb39702011-05-29 21:18:51 +0000101 pcmd.user_out = user_out;
102
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200103 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200104 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200105
106 return err;
107}
108
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200109static int do_mtc_key(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200110{
111 tx_msp_cmd pcmd;
112 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200113 int err;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200114
115 memset(&pcmd, 0, sizeof(pcmd));
116 memset(&prx, 0, sizeof(prx));
117
118 pcmd.cmd = CMD_GET_VIM;
Anatolij Gustschinceb39702011-05-29 21:18:51 +0000119 pcmd.user_out = user_out;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200120
121 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200122 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200123
124 if (!err) {
125 /* function returns '0' if key is pressed */
126 err = (prx.input & 0x80) ? 0 : 1;
127 }
128
129 return err;
130}
131
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200132static int do_mtc_digout(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200133{
134 tx_msp_cmd pcmd;
135 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200136 int err;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200137 uchar channel_mask = 0;
138
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200139 if (argc < 3)
140 return cmd_usage(cmdtp);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200141
142 if (strncmp(argv[1], "on", 2) == 0)
143 channel_mask |= 1;
144 if (strncmp(argv[2], "on", 2) == 0)
145 channel_mask |= 2;
146
147 memset(&pcmd, 0, sizeof(pcmd));
148 memset(&prx, 0, sizeof(prx));
149
150 pcmd.cmd = CMD_GET_VIM;
151 pcmd.user_out = channel_mask;
Anatolij Gustschinceb39702011-05-29 21:18:51 +0000152 user_out = channel_mask;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200153
154 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200155 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200156
157 return err;
158}
159
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200160static int do_mtc_digin(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200161{
162 tx_msp_cmd pcmd;
163 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200164 int err;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200165 uchar channel_num = 0;
166
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200167 if (argc < 2)
168 return cmd_usage(cmdtp);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200169
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");
173 return -1;
174 }
175
176 memset(&pcmd, 0, sizeof(pcmd));
177 memset(&prx, 0, sizeof(prx));
178
179 pcmd.cmd = CMD_GET_VIM;
Anatolij Gustschinceb39702011-05-29 21:18:51 +0000180 pcmd.user_out = user_out;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200181
182 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200183 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200184
185 if (!err) {
186 /* function returns '0' when digin is on */
187 err = (prx.input & channel_num) ? 0 : 1;
188 }
189
190 return err;
191}
192
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200193static int do_mtc_appreg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200194{
195 tx_msp_cmd pcmd;
196 rx_msp_cmd prx;
197 int err;
198 char buf[5];
Anatolij Gustschinceb39702011-05-29 21:18:51 +0000199 uchar appreg;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200200
201 /* read appreg */
202 memset(&pcmd, 0, sizeof(pcmd));
203 memset(&prx, 0, sizeof(prx));
204
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 */
Anatolij Gustschinceb39702011-05-29 21:18:51 +0000209 pcmd.user_out = user_out;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200210
211 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200212 err = msp430_xfer(&pcmd, &prx);
213
Anatolij Gustschinceb39702011-05-29 21:18:51 +0000214 /* on success decide between read or write */
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200215 if (!err) {
Anatolij Gustschinceb39702011-05-29 21:18:51 +0000216 if (argc == 2) {
217 appreg = simple_strtol(argv[1], NULL, 10);
218 if (appreg == 0) {
219 printf("mtc appreg: invalid parameter - "
220 "must be between 1 and 255\n");
221 return -1;
222 }
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));
230
231 mtc_calculate_checksum(&pcmd);
232 err = msp430_xfer(&pcmd, &prx);
233 } else {
234 sprintf(buf, "%d", prx.ack2);
235 setenv("appreg", buf);
236 }
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200237 }
238
239 return err;
240}
241
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200242static int do_mtc_version(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200243{
244 tx_msp_cmd pcmd;
245 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200246 int err;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200247
248 memset(&pcmd, 0, sizeof(pcmd));
249 memset(&prx, 0, sizeof(prx));
250
251 pcmd.cmd = CMD_FW_VERSION;
Anatolij Gustschinceb39702011-05-29 21:18:51 +0000252 pcmd.user_out = user_out;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200253
254 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200255 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200256
257 if (!err) {
258 printf("FW V%d.%d.%d / HW %d\n",
259 prx.ack0, prx.ack1, prx.ack3, prx.ack2);
260 }
261
262 return err;
263}
264
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200265static int do_mtc_state(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki5cc69082009-08-05 18:37:42 +0200266{
267 tx_msp_cmd pcmd;
268 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200269 int err;
Grzegorz Bernacki5cc69082009-08-05 18:37:42 +0200270
271 memset(&pcmd, 0, sizeof(pcmd));
272 memset(&prx, 0, sizeof(prx));
273
274 pcmd.cmd = CMD_WD_WDSTATE;
275 pcmd.cmd_val2 = 1;
Anatolij Gustschinceb39702011-05-29 21:18:51 +0000276 pcmd.user_out = user_out;
Grzegorz Bernacki5cc69082009-08-05 18:37:42 +0200277
278 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200279 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki5cc69082009-08-05 18:37:42 +0200280
281 if (!err) {
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);
288 }
289
290 return err;
291}
292
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200293static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200294
295cmd_tbl_t cmd_mtc_sub[] = {
296 U_BOOT_CMD_MKENT(led, 3, 1, do_mtc_led,
297 "set state of leds",
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,
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200303 "returns state of user key", ""),
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200304 U_BOOT_CMD_MKENT(version, 0, 1, do_mtc_version,
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200305 "returns firmware version of supervisor uC", ""),
Anatolij Gustschinceb39702011-05-29 21:18:51 +0000306 U_BOOT_CMD_MKENT(appreg, 1, 1, do_mtc_appreg,
307 "reads or writes appreg value and stores in environment "
308 "variable 'appreg'",
309 "[value] - value (1 - 255) to write to appreg"),
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200310 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"),
Grzegorz Bernacki5cc69082009-08-05 18:37:42 +0200316 U_BOOT_CMD_MKENT(state, 0, 1, do_mtc_state,
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200317 "displays state", ""),
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200318 U_BOOT_CMD_MKENT(help, 4, 1, do_mtc_help, "get help",
319 "[command] - get help for command\n"),
320};
321
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200322static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200323{
324 extern int _do_help(cmd_tbl_t *cmd_start, int cmd_items,
325 cmd_tbl_t *cmdtp, int flag,
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200326 int argc, char * const argv[]);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200327#ifdef CONFIG_SYS_LONGHELP
328 puts("mtc ");
329#endif
330 return _do_help(&cmd_mtc_sub[0],
331 ARRAY_SIZE(cmd_mtc_sub), cmdtp, flag, argc, argv);
332}
333
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200334int cmd_mtc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200335{
336 cmd_tbl_t *c;
337 int err = 0;
338
339 c = find_cmd_tbl(argv[1], &cmd_mtc_sub[0], ARRAY_SIZE(cmd_mtc_sub));
340 if (c) {
341 argc--;
342 argv++;
343 return c->cmd(c, flag, argc, argv);
344 } else {
345 /* Unrecognized command */
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200346 return cmd_usage(cmdtp);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200347 }
348
349 return err;
350}
351
352U_BOOT_CMD(mtc, 5, 1, cmd_mtc,
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200353 "special commands for digsyMTC",
Wolfgang Denk8e5e9b92009-07-07 22:35:02 +0200354 "[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"
Anatolij Gustschinceb39702011-05-29 21:18:51 +0000362 "appreg [value] - reads (in environment variable 'appreg') or writes"
363 " appreg value\n"
364 " [value]: value (1 - 255) to write to appreg\n"
Wolfgang Denk8e5e9b92009-07-07 22:35:02 +0200365 "digin [channel] - returns state of digital input (1 or 2)\n"
366 "digout <on|off> <on|off> - sets state of two digital outputs\n"
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200367 "state - displays state\n"
Wolfgang Denk8e5e9b92009-07-07 22:35:02 +0200368 "help [subcommand] - get help for subcommand\n"
369);