Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 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 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 Zundel | 0b40bd4 | 2009-08-05 18:37:44 +0200 | [diff] [blame] | 47 | static 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 Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 60 | static 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 Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 71 | static int do_mtc_led(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 72 | { |
| 73 | tx_msp_cmd pcmd; |
| 74 | rx_msp_cmd prx; |
Detlev Zundel | 0b40bd4 | 2009-08-05 18:37:44 +0200 | [diff] [blame] | 75 | int err; |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 76 | int i; |
| 77 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 78 | if (argc < 2) |
| 79 | return cmd_usage(cmdtp); |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 80 | |
| 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 Zundel | 0b40bd4 | 2009-08-05 18:37:44 +0200 | [diff] [blame] | 116 | err = msp430_xfer(&pcmd, &prx); |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 117 | |
| 118 | return err; |
| 119 | } |
| 120 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 121 | static int do_mtc_key(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 122 | { |
| 123 | tx_msp_cmd pcmd; |
| 124 | rx_msp_cmd prx; |
Detlev Zundel | 0b40bd4 | 2009-08-05 18:37:44 +0200 | [diff] [blame] | 125 | int err; |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 126 | |
| 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 Zundel | 0b40bd4 | 2009-08-05 18:37:44 +0200 | [diff] [blame] | 133 | err = msp430_xfer(&pcmd, &prx); |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 134 | |
| 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 Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 143 | static int do_mtc_digout(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 144 | { |
| 145 | tx_msp_cmd pcmd; |
| 146 | rx_msp_cmd prx; |
Detlev Zundel | 0b40bd4 | 2009-08-05 18:37:44 +0200 | [diff] [blame] | 147 | int err; |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 148 | uchar channel_mask = 0; |
| 149 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 150 | if (argc < 3) |
| 151 | return cmd_usage(cmdtp); |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 152 | |
| 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 Zundel | 0b40bd4 | 2009-08-05 18:37:44 +0200 | [diff] [blame] | 165 | err = msp430_xfer(&pcmd, &prx); |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 166 | |
| 167 | return err; |
| 168 | } |
| 169 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 170 | static int do_mtc_digin(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 171 | { |
| 172 | tx_msp_cmd pcmd; |
| 173 | rx_msp_cmd prx; |
Detlev Zundel | 0b40bd4 | 2009-08-05 18:37:44 +0200 | [diff] [blame] | 174 | int err; |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 175 | uchar channel_num = 0; |
| 176 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 177 | if (argc < 2) |
| 178 | return cmd_usage(cmdtp); |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 179 | |
| 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 Zundel | 0b40bd4 | 2009-08-05 18:37:44 +0200 | [diff] [blame] | 192 | err = msp430_xfer(&pcmd, &prx); |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 193 | |
| 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 Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 202 | static int do_mtc_appreg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 203 | { |
| 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 Zundel | 0b40bd4 | 2009-08-05 18:37:44 +0200 | [diff] [blame] | 219 | err = msp430_xfer(&pcmd, &prx); |
| 220 | |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 221 | if (!err) { |
| 222 | sprintf(buf, "%d", prx.ack2); |
| 223 | setenv("appreg", buf); |
| 224 | } |
| 225 | |
| 226 | return err; |
| 227 | } |
| 228 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 229 | static int do_mtc_version(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 230 | { |
| 231 | tx_msp_cmd pcmd; |
| 232 | rx_msp_cmd prx; |
Detlev Zundel | 0b40bd4 | 2009-08-05 18:37:44 +0200 | [diff] [blame] | 233 | int err; |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 234 | |
| 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 Zundel | 0b40bd4 | 2009-08-05 18:37:44 +0200 | [diff] [blame] | 241 | err = msp430_xfer(&pcmd, &prx); |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 242 | |
| 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 Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 251 | static int do_mtc_state(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Grzegorz Bernacki | 5cc6908 | 2009-08-05 18:37:42 +0200 | [diff] [blame] | 252 | { |
| 253 | tx_msp_cmd pcmd; |
| 254 | rx_msp_cmd prx; |
Detlev Zundel | 0b40bd4 | 2009-08-05 18:37:44 +0200 | [diff] [blame] | 255 | int err; |
Grzegorz Bernacki | 5cc6908 | 2009-08-05 18:37:42 +0200 | [diff] [blame] | 256 | |
| 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 Zundel | 0b40bd4 | 2009-08-05 18:37:44 +0200 | [diff] [blame] | 264 | err = msp430_xfer(&pcmd, &prx); |
Grzegorz Bernacki | 5cc6908 | 2009-08-05 18:37:42 +0200 | [diff] [blame] | 265 | |
| 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 Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 278 | static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 279 | |
| 280 | cmd_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 Gustschin | 0bf0075 | 2009-08-05 18:37:43 +0200 | [diff] [blame] | 288 | "returns state of user key", ""), |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 289 | U_BOOT_CMD_MKENT(version, 0, 1, do_mtc_version, |
Anatolij Gustschin | 0bf0075 | 2009-08-05 18:37:43 +0200 | [diff] [blame] | 290 | "returns firmware version of supervisor uC", ""), |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 291 | U_BOOT_CMD_MKENT(appreg, 0, 1, do_mtc_appreg, |
Anatolij Gustschin | 0bf0075 | 2009-08-05 18:37:43 +0200 | [diff] [blame] | 292 | "reads appreg value and stores in environment variable 'appreg'", ""), |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 293 | 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 Bernacki | 5cc6908 | 2009-08-05 18:37:42 +0200 | [diff] [blame] | 299 | U_BOOT_CMD_MKENT(state, 0, 1, do_mtc_state, |
Anatolij Gustschin | 0bf0075 | 2009-08-05 18:37:43 +0200 | [diff] [blame] | 300 | "displays state", ""), |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 301 | U_BOOT_CMD_MKENT(help, 4, 1, do_mtc_help, "get help", |
| 302 | "[command] - get help for command\n"), |
| 303 | }; |
| 304 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 305 | static int do_mtc_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 306 | { |
| 307 | extern int _do_help(cmd_tbl_t *cmd_start, int cmd_items, |
| 308 | cmd_tbl_t *cmdtp, int flag, |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 309 | int argc, char * const argv[]); |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 310 | #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 Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 317 | int cmd_mtc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 318 | { |
| 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 Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 329 | return cmd_usage(cmdtp); |
Grzegorz Bernacki | 1230487 | 2009-06-12 11:33:54 +0200 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | return err; |
| 333 | } |
| 334 | |
| 335 | U_BOOT_CMD(mtc, 5, 1, cmd_mtc, |
Anatolij Gustschin | 0bf0075 | 2009-08-05 18:37:43 +0200 | [diff] [blame] | 336 | "special commands for digsyMTC", |
Wolfgang Denk | 8e5e9b9 | 2009-07-07 22:35:02 +0200 | [diff] [blame] | 337 | "[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 Gustschin | 0bf0075 | 2009-08-05 18:37:43 +0200 | [diff] [blame] | 349 | "state - displays state\n" |
Wolfgang Denk | 8e5e9b9 | 2009-07-07 22:35:02 +0200 | [diff] [blame] | 350 | "help [subcommand] - get help for subcommand\n" |
| 351 | ); |