]> git.sur5r.net Git - u-boot/blob - board/intercontrol/digsy_mtc/cmd_mtc.c
powerpc/83xx/km: make local functions and structs static
[u-boot] / board / intercontrol / digsy_mtc / cmd_mtc.c
1 /*
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
32 DECLARE_GLOBAL_DATA_PTR;
33
34 static uchar user_out;
35
36 static 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
49 static 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
62 static 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
73 static int do_mtc_led(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
74 {
75         tx_msp_cmd pcmd;
76         rx_msp_cmd prx;
77         int err;
78         int i;
79
80         if (argc < 2)
81                 return cmd_usage(cmdtp);
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         pcmd.user_out = user_out;
118
119         mtc_calculate_checksum(&pcmd);
120         err = msp430_xfer(&pcmd, &prx);
121
122         return err;
123 }
124
125 static int do_mtc_key(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
126 {
127         tx_msp_cmd pcmd;
128         rx_msp_cmd prx;
129         int err;
130
131         memset(&pcmd, 0, sizeof(pcmd));
132         memset(&prx, 0, sizeof(prx));
133
134         pcmd.cmd = CMD_GET_VIM;
135         pcmd.user_out = user_out;
136
137         mtc_calculate_checksum(&pcmd);
138         err = msp430_xfer(&pcmd, &prx);
139
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
148 static int do_mtc_digout(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
149 {
150         tx_msp_cmd pcmd;
151         rx_msp_cmd prx;
152         int err;
153         uchar channel_mask = 0;
154
155         if (argc < 3)
156                 return cmd_usage(cmdtp);
157
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;
168         user_out = channel_mask;
169
170         mtc_calculate_checksum(&pcmd);
171         err = msp430_xfer(&pcmd, &prx);
172
173         return err;
174 }
175
176 static int do_mtc_digin(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
177 {
178         tx_msp_cmd pcmd;
179         rx_msp_cmd prx;
180         int err;
181         uchar channel_num = 0;
182
183         if (argc < 2)
184                 return cmd_usage(cmdtp);
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         pcmd.user_out = user_out;
197
198         mtc_calculate_checksum(&pcmd);
199         err = msp430_xfer(&pcmd, &prx);
200
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
209 static int do_mtc_appreg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
210 {
211         tx_msp_cmd pcmd;
212         rx_msp_cmd prx;
213         int err;
214         char buf[5];
215         uchar appreg;
216
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 */
225         pcmd.user_out = user_out;
226
227         mtc_calculate_checksum(&pcmd);
228         err = msp430_xfer(&pcmd, &prx);
229
230         /* on success decide between read or write */
231         if (!err) {
232                 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                 }
253         }
254
255         return err;
256 }
257
258 static int do_mtc_version(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
259 {
260         tx_msp_cmd pcmd;
261         rx_msp_cmd prx;
262         int err;
263
264         memset(&pcmd, 0, sizeof(pcmd));
265         memset(&prx, 0, sizeof(prx));
266
267         pcmd.cmd = CMD_FW_VERSION;
268         pcmd.user_out = user_out;
269
270         mtc_calculate_checksum(&pcmd);
271         err = msp430_xfer(&pcmd, &prx);
272
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
281 static int do_mtc_state(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
282 {
283         tx_msp_cmd pcmd;
284         rx_msp_cmd prx;
285         int err;
286
287         memset(&pcmd, 0, sizeof(pcmd));
288         memset(&prx, 0, sizeof(prx));
289
290         pcmd.cmd = CMD_WD_WDSTATE;
291         pcmd.cmd_val2 = 1;
292         pcmd.user_out = user_out;
293
294         mtc_calculate_checksum(&pcmd);
295         err = msp430_xfer(&pcmd, &prx);
296
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
309 static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
310
311 cmd_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,
319         "returns state of user key", ""),
320         U_BOOT_CMD_MKENT(version, 0, 1, do_mtc_version,
321         "returns firmware version of supervisor uC", ""),
322         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"),
326         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"),
332         U_BOOT_CMD_MKENT(state, 0, 1, do_mtc_state,
333         "displays state", ""),
334         U_BOOT_CMD_MKENT(help, 4, 1, do_mtc_help, "get help",
335         "[command] - get help for command\n"),
336 };
337
338 static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
339 {
340         extern int _do_help(cmd_tbl_t *cmd_start, int cmd_items,
341                             cmd_tbl_t *cmdtp, int flag,
342                             int argc, char * const argv[]);
343 #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
350 int cmd_mtc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
351 {
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 */
362                 return cmd_usage(cmdtp);
363         }
364
365         return err;
366 }
367
368 U_BOOT_CMD(mtc, 5, 1, cmd_mtc,
369         "special commands for digsyMTC",
370         "[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"
378         "appreg [value] - reads (in environment variable 'appreg') or writes"
379         " appreg value\n"
380         "  [value]: value (1 - 255) to write to appreg\n"
381         "digin [channel] - returns state of digital input (1 or 2)\n"
382         "digout <on|off> <on|off> - sets state of two digital outputs\n"
383         "state - displays state\n"
384         "help [subcommand] - get help for subcommand\n"
385 );