blob: ba0c36770e107562410321fd28853696f44ab02d [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 *
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
34static const char *led_names[] = {
35 "diag",
36 "can1",
37 "can2",
38 "can3",
39 "can4",
40 "usbpwr",
41 "usbbusy",
42 "user1",
43 "user2",
44 ""
45};
46
Detlev Zundel0b40bd42009-08-05 18:37:44 +020047static int msp430_xfer(const void *dout, void *din)
48{
49 int err;
50
51 err = spi_xfer(NULL, MTC_TRANSFER_SIZE, dout, din,
52 SPI_XFER_BEGIN | SPI_XFER_END);
53
54 /* The MSP chip needs time to ready itself for the next command */
55 udelay(1000);
56
57 return err;
58}
59
Grzegorz Bernacki12304872009-06-12 11:33:54 +020060static void mtc_calculate_checksum(tx_msp_cmd *packet)
61{
62 int i;
63 uchar *buff;
64
65 buff = (uchar *) packet;
66
67 for (i = 0; i < 6; i++)
68 packet->cks += buff[i];
69}
70
Wolfgang Denk54841ab2010-06-28 22:00:46 +020071static int do_mtc_led(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki12304872009-06-12 11:33:54 +020072{
73 tx_msp_cmd pcmd;
74 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +020075 int err;
Grzegorz Bernacki12304872009-06-12 11:33:54 +020076 int i;
77
Wolfgang Denk47e26b12010-07-17 01:06:04 +020078 if (argc < 2)
79 return cmd_usage(cmdtp);
Grzegorz Bernacki12304872009-06-12 11:33:54 +020080
81 memset(&pcmd, 0, sizeof(pcmd));
82 memset(&prx, 0, sizeof(prx));
83
84 pcmd.cmd = CMD_SET_LED;
85
86 pcmd.cmd_val0 = 0xff;
87 for (i = 0; strlen(led_names[i]) != 0; i++) {
88 if (strncmp(argv[1], led_names[i], strlen(led_names[i])) == 0) {
89 pcmd.cmd_val0 = i;
90 break;
91 }
92 }
93
94 if (pcmd.cmd_val0 == 0xff) {
95 printf("Usage:\n%s\n", cmdtp->help);
96 return -1;
97 }
98
99 if (argc >= 3) {
100 if (strncmp(argv[2], "red", 3) == 0)
101 pcmd.cmd_val1 = 1;
102 else if (strncmp(argv[2], "green", 5) == 0)
103 pcmd.cmd_val1 = 2;
104 else if (strncmp(argv[2], "orange", 6) == 0)
105 pcmd.cmd_val1 = 3;
106 else
107 pcmd.cmd_val1 = 0;
108 }
109
110 if (argc >= 4)
111 pcmd.cmd_val2 = simple_strtol(argv[3], NULL, 10);
112 else
113 pcmd.cmd_val2 = 0;
114
115 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200116 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200117
118 return err;
119}
120
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200121static int do_mtc_key(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200122{
123 tx_msp_cmd pcmd;
124 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200125 int err;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200126
127 memset(&pcmd, 0, sizeof(pcmd));
128 memset(&prx, 0, sizeof(prx));
129
130 pcmd.cmd = CMD_GET_VIM;
131
132 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200133 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200134
135 if (!err) {
136 /* function returns '0' if key is pressed */
137 err = (prx.input & 0x80) ? 0 : 1;
138 }
139
140 return err;
141}
142
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200143static int do_mtc_digout(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200144{
145 tx_msp_cmd pcmd;
146 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200147 int err;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200148 uchar channel_mask = 0;
149
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200150 if (argc < 3)
151 return cmd_usage(cmdtp);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200152
153 if (strncmp(argv[1], "on", 2) == 0)
154 channel_mask |= 1;
155 if (strncmp(argv[2], "on", 2) == 0)
156 channel_mask |= 2;
157
158 memset(&pcmd, 0, sizeof(pcmd));
159 memset(&prx, 0, sizeof(prx));
160
161 pcmd.cmd = CMD_GET_VIM;
162 pcmd.user_out = channel_mask;
163
164 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200165 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200166
167 return err;
168}
169
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200170static int do_mtc_digin(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200171{
172 tx_msp_cmd pcmd;
173 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200174 int err;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200175 uchar channel_num = 0;
176
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200177 if (argc < 2)
178 return cmd_usage(cmdtp);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200179
180 channel_num = simple_strtol(argv[1], NULL, 10);
181 if ((channel_num != 1) && (channel_num != 2)) {
182 printf("mtc digin: invalid parameter - must be '1' or '2'\n");
183 return -1;
184 }
185
186 memset(&pcmd, 0, sizeof(pcmd));
187 memset(&prx, 0, sizeof(prx));
188
189 pcmd.cmd = CMD_GET_VIM;
190
191 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200192 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200193
194 if (!err) {
195 /* function returns '0' when digin is on */
196 err = (prx.input & channel_num) ? 0 : 1;
197 }
198
199 return err;
200}
201
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200202static int do_mtc_appreg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200203{
204 tx_msp_cmd pcmd;
205 rx_msp_cmd prx;
206 int err;
207 char buf[5];
208
209 /* read appreg */
210 memset(&pcmd, 0, sizeof(pcmd));
211 memset(&prx, 0, sizeof(prx));
212
213 pcmd.cmd = CMD_WD_PARA;
214 pcmd.cmd_val0 = 5; /* max. Count */
215 pcmd.cmd_val1 = 5; /* max. Time */
216 pcmd.cmd_val2 = 0; /* =0 means read appreg */
217
218 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200219 err = msp430_xfer(&pcmd, &prx);
220
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200221 if (!err) {
222 sprintf(buf, "%d", prx.ack2);
223 setenv("appreg", buf);
224 }
225
226 return err;
227}
228
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200229static int do_mtc_version(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200230{
231 tx_msp_cmd pcmd;
232 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200233 int err;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200234
235 memset(&pcmd, 0, sizeof(pcmd));
236 memset(&prx, 0, sizeof(prx));
237
238 pcmd.cmd = CMD_FW_VERSION;
239
240 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200241 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200242
243 if (!err) {
244 printf("FW V%d.%d.%d / HW %d\n",
245 prx.ack0, prx.ack1, prx.ack3, prx.ack2);
246 }
247
248 return err;
249}
250
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200251static int do_mtc_state(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki5cc69082009-08-05 18:37:42 +0200252{
253 tx_msp_cmd pcmd;
254 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200255 int err;
Grzegorz Bernacki5cc69082009-08-05 18:37:42 +0200256
257 memset(&pcmd, 0, sizeof(pcmd));
258 memset(&prx, 0, sizeof(prx));
259
260 pcmd.cmd = CMD_WD_WDSTATE;
261 pcmd.cmd_val2 = 1;
262
263 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200264 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki5cc69082009-08-05 18:37:42 +0200265
266 if (!err) {
267 printf("State %02Xh\n", prx.state);
268 printf("Input %02Xh\n", prx.input);
269 printf("UserWD %02Xh\n", prx.ack2);
270 printf("Sys WD %02Xh\n", prx.ack3);
271 printf("WD Timout %02Xh\n", prx.ack0);
272 printf("eSysState %02Xh\n", prx.ack1);
273 }
274
275 return err;
276}
277
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200278static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200279
280cmd_tbl_t cmd_mtc_sub[] = {
281 U_BOOT_CMD_MKENT(led, 3, 1, do_mtc_led,
282 "set state of leds",
283 "[ledname] [state] [blink]\n"
284 " - lednames: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
285 " - state: off red green orange\n"
286 " - blink: blink interval in 100ms steps (1 - 10; 0 = static)\n"),
287 U_BOOT_CMD_MKENT(key, 0, 1, do_mtc_key,
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200288 "returns state of user key", ""),
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200289 U_BOOT_CMD_MKENT(version, 0, 1, do_mtc_version,
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200290 "returns firmware version of supervisor uC", ""),
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200291 U_BOOT_CMD_MKENT(appreg, 0, 1, do_mtc_appreg,
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200292 "reads appreg value and stores in environment variable 'appreg'", ""),
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200293 U_BOOT_CMD_MKENT(digin, 1, 1, do_mtc_digin,
294 "returns state of digital input",
295 "<channel_num> - get state of digital input (1 or 2)\n"),
296 U_BOOT_CMD_MKENT(digout, 2, 1, do_mtc_digout,
297 "sets digital outputs",
298 "<on|off> <on|off>- set state of digital output 1 and 2\n"),
Grzegorz Bernacki5cc69082009-08-05 18:37:42 +0200299 U_BOOT_CMD_MKENT(state, 0, 1, do_mtc_state,
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200300 "displays state", ""),
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200301 U_BOOT_CMD_MKENT(help, 4, 1, do_mtc_help, "get help",
302 "[command] - get help for command\n"),
303};
304
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200305static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200306{
307 extern int _do_help(cmd_tbl_t *cmd_start, int cmd_items,
308 cmd_tbl_t *cmdtp, int flag,
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200309 int argc, char * const argv[]);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200310#ifdef CONFIG_SYS_LONGHELP
311 puts("mtc ");
312#endif
313 return _do_help(&cmd_mtc_sub[0],
314 ARRAY_SIZE(cmd_mtc_sub), cmdtp, flag, argc, argv);
315}
316
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200317int cmd_mtc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200318{
319 cmd_tbl_t *c;
320 int err = 0;
321
322 c = find_cmd_tbl(argv[1], &cmd_mtc_sub[0], ARRAY_SIZE(cmd_mtc_sub));
323 if (c) {
324 argc--;
325 argv++;
326 return c->cmd(c, flag, argc, argv);
327 } else {
328 /* Unrecognized command */
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200329 return cmd_usage(cmdtp);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200330 }
331
332 return err;
333}
334
335U_BOOT_CMD(mtc, 5, 1, cmd_mtc,
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200336 "special commands for digsyMTC",
Wolfgang Denk8e5e9b92009-07-07 22:35:02 +0200337 "[subcommand] [args...]\n"
338 "Subcommands list:\n"
339 "led [ledname] [state] [blink] - set state of leds\n"
340 " [ledname]: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
341 " [state]: off red green orange\n"
342 " [blink]: blink interval in 100ms steps (1 - 10; 0 = static)\n"
343 "key - returns state of user key\n"
344 "version - returns firmware version of supervisor uC\n"
345 "appreg - reads appreg value and stores in environment variable"
346 " 'appreg'\n"
347 "digin [channel] - returns state of digital input (1 or 2)\n"
348 "digout <on|off> <on|off> - sets state of two digital outputs\n"
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200349 "state - displays state\n"
Wolfgang Denk8e5e9b92009-07-07 22:35:02 +0200350 "help [subcommand] - get help for subcommand\n"
351);