blob: fffcee92068f4bb15247abe64e8b6b39e7376a98 [file] [log] [blame]
Grzegorz Bernackid08c9d82009-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 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26#include <common.h>
27#include <command.h>
28#include <mpc5xxx.h>
29#include "spi.h"
30#include "cmd_mtc.h"
31
32DECLARE_GLOBAL_DATA_PTR;
33
Anatolij Gustschin4a9cc652011-05-29 21:18:51 +000034static uchar user_out;
35
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +020036static const char *led_names[] = {
37 "diag",
38 "can1",
39 "can2",
40 "can3",
41 "can4",
42 "usbpwr",
43 "usbbusy",
44 "user1",
45 "user2",
46 ""
47};
48
Detlev Zundel0253df32009-08-05 18:37:44 +020049static int msp430_xfer(const void *dout, void *din)
50{
51 int err;
52
53 err = spi_xfer(NULL, MTC_TRANSFER_SIZE, dout, din,
54 SPI_XFER_BEGIN | SPI_XFER_END);
55
56 /* The MSP chip needs time to ready itself for the next command */
57 udelay(1000);
58
59 return err;
60}
61
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +020062static void mtc_calculate_checksum(tx_msp_cmd *packet)
63{
64 int i;
65 uchar *buff;
66
67 buff = (uchar *) packet;
68
69 for (i = 0; i < 6; i++)
70 packet->cks += buff[i];
71}
72
Wolfgang Denk6262d0212010-06-28 22:00:46 +020073static int do_mtc_led(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +020074{
75 tx_msp_cmd pcmd;
76 rx_msp_cmd prx;
Detlev Zundel0253df32009-08-05 18:37:44 +020077 int err;
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +020078 int i;
79
Wolfgang Denk3b683112010-07-17 01:06:04 +020080 if (argc < 2)
81 return cmd_usage(cmdtp);
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +020082
83 memset(&pcmd, 0, sizeof(pcmd));
84 memset(&prx, 0, sizeof(prx));
85
86 pcmd.cmd = CMD_SET_LED;
87
88 pcmd.cmd_val0 = 0xff;
89 for (i = 0; strlen(led_names[i]) != 0; i++) {
90 if (strncmp(argv[1], led_names[i], strlen(led_names[i])) == 0) {
91 pcmd.cmd_val0 = i;
92 break;
93 }
94 }
95
96 if (pcmd.cmd_val0 == 0xff) {
97 printf("Usage:\n%s\n", cmdtp->help);
98 return -1;
99 }
100
101 if (argc >= 3) {
102 if (strncmp(argv[2], "red", 3) == 0)
103 pcmd.cmd_val1 = 1;
104 else if (strncmp(argv[2], "green", 5) == 0)
105 pcmd.cmd_val1 = 2;
106 else if (strncmp(argv[2], "orange", 6) == 0)
107 pcmd.cmd_val1 = 3;
108 else
109 pcmd.cmd_val1 = 0;
110 }
111
112 if (argc >= 4)
113 pcmd.cmd_val2 = simple_strtol(argv[3], NULL, 10);
114 else
115 pcmd.cmd_val2 = 0;
116
Anatolij Gustschin4a9cc652011-05-29 21:18:51 +0000117 pcmd.user_out = user_out;
118
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200119 mtc_calculate_checksum(&pcmd);
Detlev Zundel0253df32009-08-05 18:37:44 +0200120 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200121
122 return err;
123}
124
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200125static int do_mtc_key(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200126{
127 tx_msp_cmd pcmd;
128 rx_msp_cmd prx;
Detlev Zundel0253df32009-08-05 18:37:44 +0200129 int err;
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200130
131 memset(&pcmd, 0, sizeof(pcmd));
132 memset(&prx, 0, sizeof(prx));
133
134 pcmd.cmd = CMD_GET_VIM;
Anatolij Gustschin4a9cc652011-05-29 21:18:51 +0000135 pcmd.user_out = user_out;
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200136
137 mtc_calculate_checksum(&pcmd);
Detlev Zundel0253df32009-08-05 18:37:44 +0200138 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200139
140 if (!err) {
141 /* function returns '0' if key is pressed */
142 err = (prx.input & 0x80) ? 0 : 1;
143 }
144
145 return err;
146}
147
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200148static int do_mtc_digout(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200149{
150 tx_msp_cmd pcmd;
151 rx_msp_cmd prx;
Detlev Zundel0253df32009-08-05 18:37:44 +0200152 int err;
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200153 uchar channel_mask = 0;
154
Wolfgang Denk3b683112010-07-17 01:06:04 +0200155 if (argc < 3)
156 return cmd_usage(cmdtp);
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200157
158 if (strncmp(argv[1], "on", 2) == 0)
159 channel_mask |= 1;
160 if (strncmp(argv[2], "on", 2) == 0)
161 channel_mask |= 2;
162
163 memset(&pcmd, 0, sizeof(pcmd));
164 memset(&prx, 0, sizeof(prx));
165
166 pcmd.cmd = CMD_GET_VIM;
167 pcmd.user_out = channel_mask;
Anatolij Gustschin4a9cc652011-05-29 21:18:51 +0000168 user_out = channel_mask;
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200169
170 mtc_calculate_checksum(&pcmd);
Detlev Zundel0253df32009-08-05 18:37:44 +0200171 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200172
173 return err;
174}
175
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200176static int do_mtc_digin(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200177{
178 tx_msp_cmd pcmd;
179 rx_msp_cmd prx;
Detlev Zundel0253df32009-08-05 18:37:44 +0200180 int err;
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200181 uchar channel_num = 0;
182
Wolfgang Denk3b683112010-07-17 01:06:04 +0200183 if (argc < 2)
184 return cmd_usage(cmdtp);
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200185
186 channel_num = simple_strtol(argv[1], NULL, 10);
187 if ((channel_num != 1) && (channel_num != 2)) {
188 printf("mtc digin: invalid parameter - must be '1' or '2'\n");
189 return -1;
190 }
191
192 memset(&pcmd, 0, sizeof(pcmd));
193 memset(&prx, 0, sizeof(prx));
194
195 pcmd.cmd = CMD_GET_VIM;
Anatolij Gustschin4a9cc652011-05-29 21:18:51 +0000196 pcmd.user_out = user_out;
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200197
198 mtc_calculate_checksum(&pcmd);
Detlev Zundel0253df32009-08-05 18:37:44 +0200199 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200200
201 if (!err) {
202 /* function returns '0' when digin is on */
203 err = (prx.input & channel_num) ? 0 : 1;
204 }
205
206 return err;
207}
208
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200209static int do_mtc_appreg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200210{
211 tx_msp_cmd pcmd;
212 rx_msp_cmd prx;
213 int err;
214 char buf[5];
Anatolij Gustschin4a9cc652011-05-29 21:18:51 +0000215 uchar appreg;
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200216
217 /* read appreg */
218 memset(&pcmd, 0, sizeof(pcmd));
219 memset(&prx, 0, sizeof(prx));
220
221 pcmd.cmd = CMD_WD_PARA;
222 pcmd.cmd_val0 = 5; /* max. Count */
223 pcmd.cmd_val1 = 5; /* max. Time */
224 pcmd.cmd_val2 = 0; /* =0 means read appreg */
Anatolij Gustschin4a9cc652011-05-29 21:18:51 +0000225 pcmd.user_out = user_out;
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200226
227 mtc_calculate_checksum(&pcmd);
Detlev Zundel0253df32009-08-05 18:37:44 +0200228 err = msp430_xfer(&pcmd, &prx);
229
Anatolij Gustschin4a9cc652011-05-29 21:18:51 +0000230 /* on success decide between read or write */
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200231 if (!err) {
Anatolij Gustschin4a9cc652011-05-29 21:18:51 +0000232 if (argc == 2) {
233 appreg = simple_strtol(argv[1], NULL, 10);
234 if (appreg == 0) {
235 printf("mtc appreg: invalid parameter - "
236 "must be between 1 and 255\n");
237 return -1;
238 }
239 memset(&pcmd, 0, sizeof(pcmd));
240 pcmd.cmd = CMD_WD_PARA;
241 pcmd.cmd_val0 = prx.ack3; /* max. Count */
242 pcmd.cmd_val1 = prx.ack0; /* max. Time */
243 pcmd.cmd_val2 = appreg; /* !=0 means write appreg */
244 pcmd.user_out = user_out;
245 memset(&prx, 0, sizeof(prx));
246
247 mtc_calculate_checksum(&pcmd);
248 err = msp430_xfer(&pcmd, &prx);
249 } else {
250 sprintf(buf, "%d", prx.ack2);
251 setenv("appreg", buf);
252 }
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200253 }
254
255 return err;
256}
257
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200258static int do_mtc_version(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200259{
260 tx_msp_cmd pcmd;
261 rx_msp_cmd prx;
Detlev Zundel0253df32009-08-05 18:37:44 +0200262 int err;
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200263
264 memset(&pcmd, 0, sizeof(pcmd));
265 memset(&prx, 0, sizeof(prx));
266
267 pcmd.cmd = CMD_FW_VERSION;
Anatolij Gustschin4a9cc652011-05-29 21:18:51 +0000268 pcmd.user_out = user_out;
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200269
270 mtc_calculate_checksum(&pcmd);
Detlev Zundel0253df32009-08-05 18:37:44 +0200271 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200272
273 if (!err) {
274 printf("FW V%d.%d.%d / HW %d\n",
275 prx.ack0, prx.ack1, prx.ack3, prx.ack2);
276 }
277
278 return err;
279}
280
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200281static int do_mtc_state(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernackifb4b7922009-08-05 18:37:42 +0200282{
283 tx_msp_cmd pcmd;
284 rx_msp_cmd prx;
Detlev Zundel0253df32009-08-05 18:37:44 +0200285 int err;
Grzegorz Bernackifb4b7922009-08-05 18:37:42 +0200286
287 memset(&pcmd, 0, sizeof(pcmd));
288 memset(&prx, 0, sizeof(prx));
289
290 pcmd.cmd = CMD_WD_WDSTATE;
291 pcmd.cmd_val2 = 1;
Anatolij Gustschin4a9cc652011-05-29 21:18:51 +0000292 pcmd.user_out = user_out;
Grzegorz Bernackifb4b7922009-08-05 18:37:42 +0200293
294 mtc_calculate_checksum(&pcmd);
Detlev Zundel0253df32009-08-05 18:37:44 +0200295 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernackifb4b7922009-08-05 18:37:42 +0200296
297 if (!err) {
298 printf("State %02Xh\n", prx.state);
299 printf("Input %02Xh\n", prx.input);
300 printf("UserWD %02Xh\n", prx.ack2);
301 printf("Sys WD %02Xh\n", prx.ack3);
302 printf("WD Timout %02Xh\n", prx.ack0);
303 printf("eSysState %02Xh\n", prx.ack1);
304 }
305
306 return err;
307}
308
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200309static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200310
311cmd_tbl_t cmd_mtc_sub[] = {
312 U_BOOT_CMD_MKENT(led, 3, 1, do_mtc_led,
313 "set state of leds",
314 "[ledname] [state] [blink]\n"
315 " - lednames: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
316 " - state: off red green orange\n"
317 " - blink: blink interval in 100ms steps (1 - 10; 0 = static)\n"),
318 U_BOOT_CMD_MKENT(key, 0, 1, do_mtc_key,
Anatolij Gustschin9bbb5992009-08-05 18:37:43 +0200319 "returns state of user key", ""),
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200320 U_BOOT_CMD_MKENT(version, 0, 1, do_mtc_version,
Anatolij Gustschin9bbb5992009-08-05 18:37:43 +0200321 "returns firmware version of supervisor uC", ""),
Anatolij Gustschin4a9cc652011-05-29 21:18:51 +0000322 U_BOOT_CMD_MKENT(appreg, 1, 1, do_mtc_appreg,
323 "reads or writes appreg value and stores in environment "
324 "variable 'appreg'",
325 "[value] - value (1 - 255) to write to appreg"),
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200326 U_BOOT_CMD_MKENT(digin, 1, 1, do_mtc_digin,
327 "returns state of digital input",
328 "<channel_num> - get state of digital input (1 or 2)\n"),
329 U_BOOT_CMD_MKENT(digout, 2, 1, do_mtc_digout,
330 "sets digital outputs",
331 "<on|off> <on|off>- set state of digital output 1 and 2\n"),
Grzegorz Bernackifb4b7922009-08-05 18:37:42 +0200332 U_BOOT_CMD_MKENT(state, 0, 1, do_mtc_state,
Anatolij Gustschin9bbb5992009-08-05 18:37:43 +0200333 "displays state", ""),
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200334 U_BOOT_CMD_MKENT(help, 4, 1, do_mtc_help, "get help",
335 "[command] - get help for command\n"),
336};
337
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200338static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200339{
340 extern int _do_help(cmd_tbl_t *cmd_start, int cmd_items,
341 cmd_tbl_t *cmdtp, int flag,
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200342 int argc, char * const argv[]);
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200343#ifdef CONFIG_SYS_LONGHELP
344 puts("mtc ");
345#endif
346 return _do_help(&cmd_mtc_sub[0],
347 ARRAY_SIZE(cmd_mtc_sub), cmdtp, flag, argc, argv);
348}
349
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200350int cmd_mtc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200351{
352 cmd_tbl_t *c;
353 int err = 0;
354
355 c = find_cmd_tbl(argv[1], &cmd_mtc_sub[0], ARRAY_SIZE(cmd_mtc_sub));
356 if (c) {
357 argc--;
358 argv++;
359 return c->cmd(c, flag, argc, argv);
360 } else {
361 /* Unrecognized command */
Wolfgang Denk3b683112010-07-17 01:06:04 +0200362 return cmd_usage(cmdtp);
Grzegorz Bernackid08c9d82009-06-12 11:33:54 +0200363 }
364
365 return err;
366}
367
368U_BOOT_CMD(mtc, 5, 1, cmd_mtc,
Anatolij Gustschin9bbb5992009-08-05 18:37:43 +0200369 "special commands for digsyMTC",
Wolfgang Denk1d695be2009-07-07 22:35:02 +0200370 "[subcommand] [args...]\n"
371 "Subcommands list:\n"
372 "led [ledname] [state] [blink] - set state of leds\n"
373 " [ledname]: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
374 " [state]: off red green orange\n"
375 " [blink]: blink interval in 100ms steps (1 - 10; 0 = static)\n"
376 "key - returns state of user key\n"
377 "version - returns firmware version of supervisor uC\n"
Anatolij Gustschin4a9cc652011-05-29 21:18:51 +0000378 "appreg [value] - reads (in environment variable 'appreg') or writes"
379 " appreg value\n"
380 " [value]: value (1 - 255) to write to appreg\n"
Wolfgang Denk1d695be2009-07-07 22:35:02 +0200381 "digin [channel] - returns state of digital input (1 or 2)\n"
382 "digout <on|off> <on|off> - sets state of two digital outputs\n"
Anatolij Gustschin9bbb5992009-08-05 18:37:43 +0200383 "state - displays state\n"
Wolfgang Denk1d695be2009-07-07 22:35:02 +0200384 "help [subcommand] - get help for subcommand\n"
385);