Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 1 | /* |
Sridhar Parasuram | 2301649 | 2014-09-11 21:28:35 -0700 | [diff] [blame] | 2 | * Copyright (c) 2008-2014, The Linux Foundation. All rights reserved. |
| 3 | * Not a contribution |
| 4 | * |
Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 5 | * Copyright (C) 2008 The Android Open Source Project |
| 6 | * All rights reserved. |
| 7 | * |
Sridhar Parasuram | 2301649 | 2014-09-11 21:28:35 -0700 | [diff] [blame] | 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Lijuan Gao | 87de70a | 2014-07-16 18:24:35 +0800 | [diff] [blame] | 11 | * |
Sridhar Parasuram | 2301649 | 2014-09-11 21:28:35 -0700 | [diff] [blame] | 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 13 | * |
Sridhar Parasuram | 2301649 | 2014-09-11 21:28:35 -0700 | [diff] [blame] | 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <jtag.h> |
| 22 | |
| 23 | #define STATUS_NOMSG 0 |
| 24 | #define STATUS_OKAY 1 |
| 25 | #define STATUS_FAIL 2 |
| 26 | #define STATUS_PRINT 3 |
Lijuan Gao | 87de70a | 2014-07-16 18:24:35 +0800 | [diff] [blame] | 27 | #define JTAG_CMD_NUM 256 |
Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 28 | |
| 29 | volatile unsigned _jtag_cmd = 0; |
| 30 | volatile unsigned _jtag_msg = 0; |
Lijuan Gao | 87de70a | 2014-07-16 18:24:35 +0800 | [diff] [blame] | 31 | unsigned char _jtag_cmd_buffer[JTAG_CMD_NUM]; |
Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 32 | unsigned char _jtag_msg_buffer[128]; |
| 33 | |
| 34 | volatile unsigned _jtag_arg0 = 0; |
| 35 | volatile unsigned _jtag_arg1 = 0; |
| 36 | volatile unsigned _jtag_arg2 = 0; |
| 37 | |
Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 38 | void jtag_hook(void); |
| 39 | |
| 40 | static void jtag_msg(unsigned status, const char *msg) |
| 41 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 42 | unsigned char *out = _jtag_msg_buffer; |
| 43 | while ((*out++ = *msg++) != 0) ; |
| 44 | _jtag_msg = status; |
| 45 | do { |
| 46 | jtag_hook(); |
| 47 | } |
| 48 | while (_jtag_msg != 0); |
Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void jtag_okay(const char *msg) |
| 52 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 53 | if (msg == 0) |
| 54 | msg = "OKAY"; |
| 55 | jtag_msg(STATUS_OKAY, msg); |
Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void jtag_fail(const char *msg) |
| 59 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 60 | if (msg == 0) |
| 61 | msg = "FAIL"; |
| 62 | jtag_msg(STATUS_FAIL, msg); |
Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | int jtag_cmd_pending() |
| 66 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 67 | jtag_hook(); |
| 68 | return (int)_jtag_cmd; |
Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 71 | void jtag_cmd_loop(void (*do_cmd) (const char *, unsigned, unsigned, unsigned)) |
Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 72 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 73 | unsigned n; |
| 74 | for (;;) { |
| 75 | if (jtag_cmd_pending()) { |
| 76 | do_cmd((const char *)_jtag_cmd_buffer, _jtag_arg0, |
| 77 | _jtag_arg1, _jtag_arg2); |
Lijuan Gao | 87de70a | 2014-07-16 18:24:35 +0800 | [diff] [blame] | 78 | for (n = 0; n < JTAG_CMD_NUM; n++) |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 79 | _jtag_cmd_buffer[n] = 0; |
| 80 | _jtag_arg0 = 0; |
| 81 | _jtag_arg1 = 0; |
| 82 | _jtag_arg2 = 0; |
| 83 | _jtag_cmd = 0; |
| 84 | } |
| 85 | } |
Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static char jtag_putc_buffer[128]; |
| 89 | static unsigned jtag_putc_count = 0; |
| 90 | |
| 91 | static void jtag_push_buffer(void) |
| 92 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 93 | jtag_putc_buffer[jtag_putc_count] = 0; |
| 94 | jtag_putc_count = 0; |
| 95 | jtag_msg(STATUS_PRINT, jtag_putc_buffer); |
Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void jtag_dputc(unsigned c) |
| 99 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 100 | if ((c < 32) || (c > 127)) { |
| 101 | if (c == '\n') { |
| 102 | jtag_push_buffer(); |
| 103 | } |
| 104 | return; |
| 105 | } |
Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 106 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 107 | jtag_putc_buffer[jtag_putc_count++] = c; |
| 108 | if (jtag_putc_count == 127) { |
| 109 | jtag_push_buffer(); |
| 110 | } |
Ajay Dudani | 168f6cb | 2009-12-07 19:04:02 -0800 | [diff] [blame] | 111 | } |