blob: ecea5b3c547a00393d2130817d8f4495c17158fa [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
71static int do_mtc_led(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
72{
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
78 if (argc < 2) {
79 cmd_usage(cmdtp);
80 return -1;
81 }
82
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
117 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200118 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200119
120 return err;
121}
122
123static int do_mtc_key(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
124{
125 tx_msp_cmd pcmd;
126 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200127 int err;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200128
129 memset(&pcmd, 0, sizeof(pcmd));
130 memset(&prx, 0, sizeof(prx));
131
132 pcmd.cmd = CMD_GET_VIM;
133
134 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200135 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200136
137 if (!err) {
138 /* function returns '0' if key is pressed */
139 err = (prx.input & 0x80) ? 0 : 1;
140 }
141
142 return err;
143}
144
145static int do_mtc_digout(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
146{
147 tx_msp_cmd pcmd;
148 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200149 int err;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200150 uchar channel_mask = 0;
151
152 if (argc < 3) {
153 cmd_usage(cmdtp);
154 return -1;
155 }
156
157 if (strncmp(argv[1], "on", 2) == 0)
158 channel_mask |= 1;
159 if (strncmp(argv[2], "on", 2) == 0)
160 channel_mask |= 2;
161
162 memset(&pcmd, 0, sizeof(pcmd));
163 memset(&prx, 0, sizeof(prx));
164
165 pcmd.cmd = CMD_GET_VIM;
166 pcmd.user_out = channel_mask;
167
168 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200169 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200170
171 return err;
172}
173
174static int do_mtc_digin(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
175{
176 tx_msp_cmd pcmd;
177 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200178 int err;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200179 uchar channel_num = 0;
180
181 if (argc < 2) {
182 cmd_usage(cmdtp);
183 return -1;
184 }
185
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;
196
197 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200198 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200199
200 if (!err) {
201 /* function returns '0' when digin is on */
202 err = (prx.input & channel_num) ? 0 : 1;
203 }
204
205 return err;
206}
207
208static int do_mtc_appreg(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
209{
210 tx_msp_cmd pcmd;
211 rx_msp_cmd prx;
212 int err;
213 char buf[5];
214
215 /* read appreg */
216 memset(&pcmd, 0, sizeof(pcmd));
217 memset(&prx, 0, sizeof(prx));
218
219 pcmd.cmd = CMD_WD_PARA;
220 pcmd.cmd_val0 = 5; /* max. Count */
221 pcmd.cmd_val1 = 5; /* max. Time */
222 pcmd.cmd_val2 = 0; /* =0 means read appreg */
223
224 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200225 err = msp430_xfer(&pcmd, &prx);
226
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200227 if (!err) {
228 sprintf(buf, "%d", prx.ack2);
229 setenv("appreg", buf);
230 }
231
232 return err;
233}
234
235static int do_mtc_version(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
236{
237 tx_msp_cmd pcmd;
238 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200239 int err;
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200240
241 memset(&pcmd, 0, sizeof(pcmd));
242 memset(&prx, 0, sizeof(prx));
243
244 pcmd.cmd = CMD_FW_VERSION;
245
246 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200247 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200248
249 if (!err) {
250 printf("FW V%d.%d.%d / HW %d\n",
251 prx.ack0, prx.ack1, prx.ack3, prx.ack2);
252 }
253
254 return err;
255}
256
Grzegorz Bernacki5cc69082009-08-05 18:37:42 +0200257static int do_mtc_state(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
258{
259 tx_msp_cmd pcmd;
260 rx_msp_cmd prx;
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200261 int err;
Grzegorz Bernacki5cc69082009-08-05 18:37:42 +0200262
263 memset(&pcmd, 0, sizeof(pcmd));
264 memset(&prx, 0, sizeof(prx));
265
266 pcmd.cmd = CMD_WD_WDSTATE;
267 pcmd.cmd_val2 = 1;
268
269 mtc_calculate_checksum(&pcmd);
Detlev Zundel0b40bd42009-08-05 18:37:44 +0200270 err = msp430_xfer(&pcmd, &prx);
Grzegorz Bernacki5cc69082009-08-05 18:37:42 +0200271
272 if (!err) {
273 printf("State %02Xh\n", prx.state);
274 printf("Input %02Xh\n", prx.input);
275 printf("UserWD %02Xh\n", prx.ack2);
276 printf("Sys WD %02Xh\n", prx.ack3);
277 printf("WD Timout %02Xh\n", prx.ack0);
278 printf("eSysState %02Xh\n", prx.ack1);
279 }
280
281 return err;
282}
283
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200284static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
285
286cmd_tbl_t cmd_mtc_sub[] = {
287 U_BOOT_CMD_MKENT(led, 3, 1, do_mtc_led,
288 "set state of leds",
289 "[ledname] [state] [blink]\n"
290 " - lednames: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
291 " - state: off red green orange\n"
292 " - blink: blink interval in 100ms steps (1 - 10; 0 = static)\n"),
293 U_BOOT_CMD_MKENT(key, 0, 1, do_mtc_key,
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200294 "returns state of user key", ""),
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200295 U_BOOT_CMD_MKENT(version, 0, 1, do_mtc_version,
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200296 "returns firmware version of supervisor uC", ""),
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200297 U_BOOT_CMD_MKENT(appreg, 0, 1, do_mtc_appreg,
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200298 "reads appreg value and stores in environment variable 'appreg'", ""),
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200299 U_BOOT_CMD_MKENT(digin, 1, 1, do_mtc_digin,
300 "returns state of digital input",
301 "<channel_num> - get state of digital input (1 or 2)\n"),
302 U_BOOT_CMD_MKENT(digout, 2, 1, do_mtc_digout,
303 "sets digital outputs",
304 "<on|off> <on|off>- set state of digital output 1 and 2\n"),
Grzegorz Bernacki5cc69082009-08-05 18:37:42 +0200305 U_BOOT_CMD_MKENT(state, 0, 1, do_mtc_state,
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200306 "displays state", ""),
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200307 U_BOOT_CMD_MKENT(help, 4, 1, do_mtc_help, "get help",
308 "[command] - get help for command\n"),
309};
310
311static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
312{
313 extern int _do_help(cmd_tbl_t *cmd_start, int cmd_items,
314 cmd_tbl_t *cmdtp, int flag,
315 int argc, char *argv[]);
316#ifdef CONFIG_SYS_LONGHELP
317 puts("mtc ");
318#endif
319 return _do_help(&cmd_mtc_sub[0],
320 ARRAY_SIZE(cmd_mtc_sub), cmdtp, flag, argc, argv);
321}
322
Grzegorz Bernacki12304872009-06-12 11:33:54 +0200323int cmd_mtc(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
324{
325 cmd_tbl_t *c;
326 int err = 0;
327
328 c = find_cmd_tbl(argv[1], &cmd_mtc_sub[0], ARRAY_SIZE(cmd_mtc_sub));
329 if (c) {
330 argc--;
331 argv++;
332 return c->cmd(c, flag, argc, argv);
333 } else {
334 /* Unrecognized command */
335 cmd_usage(cmdtp);
336 return 1;
337 }
338
339 return err;
340}
341
342U_BOOT_CMD(mtc, 5, 1, cmd_mtc,
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200343 "special commands for digsyMTC",
Wolfgang Denk8e5e9b92009-07-07 22:35:02 +0200344 "[subcommand] [args...]\n"
345 "Subcommands list:\n"
346 "led [ledname] [state] [blink] - set state of leds\n"
347 " [ledname]: diag can1 can2 can3 can4 usbpwr usbbusy user1 user2\n"
348 " [state]: off red green orange\n"
349 " [blink]: blink interval in 100ms steps (1 - 10; 0 = static)\n"
350 "key - returns state of user key\n"
351 "version - returns firmware version of supervisor uC\n"
352 "appreg - reads appreg value and stores in environment variable"
353 " 'appreg'\n"
354 "digin [channel] - returns state of digital input (1 or 2)\n"
355 "digout <on|off> <on|off> - sets state of two digital outputs\n"
Anatolij Gustschin0bf00752009-08-05 18:37:43 +0200356 "state - displays state\n"
Wolfgang Denk8e5e9b92009-07-07 22:35:02 +0200357 "help [subcommand] - get help for subcommand\n"
358);