blob: d01105e8e691acff277cca402b70f2c3022f4acd [file] [log] [blame]
Ajay Dudani168f6cb2009-12-07 19:04:02 -08001/*
Sridhar Parasuram23016492014-09-11 21:28:35 -07002 * Copyright (c) 2008-2014, The Linux Foundation. All rights reserved.
3 * Not a contribution
4 *
Ajay Dudani168f6cb2009-12-07 19:04:02 -08005 * Copyright (C) 2008 The Android Open Source Project
6 * All rights reserved.
7 *
Sridhar Parasuram23016492014-09-11 21:28:35 -07008 * 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 Gao87de70a2014-07-16 18:24:35 +080011 *
Sridhar Parasuram23016492014-09-11 21:28:35 -070012 * http://www.apache.org/licenses/LICENSE-2.0
Ajay Dudani168f6cb2009-12-07 19:04:02 -080013 *
Sridhar Parasuram23016492014-09-11 21:28:35 -070014 * 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 Dudani168f6cb2009-12-07 19:04:02 -080019 */
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 Gao87de70a2014-07-16 18:24:35 +080027#define JTAG_CMD_NUM 256
Ajay Dudani168f6cb2009-12-07 19:04:02 -080028
29volatile unsigned _jtag_cmd = 0;
30volatile unsigned _jtag_msg = 0;
Lijuan Gao87de70a2014-07-16 18:24:35 +080031unsigned char _jtag_cmd_buffer[JTAG_CMD_NUM];
Ajay Dudani168f6cb2009-12-07 19:04:02 -080032unsigned char _jtag_msg_buffer[128];
33
34volatile unsigned _jtag_arg0 = 0;
35volatile unsigned _jtag_arg1 = 0;
36volatile unsigned _jtag_arg2 = 0;
37
Ajay Dudani168f6cb2009-12-07 19:04:02 -080038void jtag_hook(void);
39
40static void jtag_msg(unsigned status, const char *msg)
41{
Ajay Dudanib01e5062011-12-03 23:23:42 -080042 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 Dudani168f6cb2009-12-07 19:04:02 -080049}
50
51void jtag_okay(const char *msg)
52{
Ajay Dudanib01e5062011-12-03 23:23:42 -080053 if (msg == 0)
54 msg = "OKAY";
55 jtag_msg(STATUS_OKAY, msg);
Ajay Dudani168f6cb2009-12-07 19:04:02 -080056}
57
58void jtag_fail(const char *msg)
59{
Ajay Dudanib01e5062011-12-03 23:23:42 -080060 if (msg == 0)
61 msg = "FAIL";
62 jtag_msg(STATUS_FAIL, msg);
Ajay Dudani168f6cb2009-12-07 19:04:02 -080063}
64
65int jtag_cmd_pending()
66{
Ajay Dudanib01e5062011-12-03 23:23:42 -080067 jtag_hook();
68 return (int)_jtag_cmd;
Ajay Dudani168f6cb2009-12-07 19:04:02 -080069}
70
Ajay Dudanib01e5062011-12-03 23:23:42 -080071void jtag_cmd_loop(void (*do_cmd) (const char *, unsigned, unsigned, unsigned))
Ajay Dudani168f6cb2009-12-07 19:04:02 -080072{
Ajay Dudanib01e5062011-12-03 23:23:42 -080073 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 Gao87de70a2014-07-16 18:24:35 +080078 for (n = 0; n < JTAG_CMD_NUM; n++)
Ajay Dudanib01e5062011-12-03 23:23:42 -080079 _jtag_cmd_buffer[n] = 0;
80 _jtag_arg0 = 0;
81 _jtag_arg1 = 0;
82 _jtag_arg2 = 0;
83 _jtag_cmd = 0;
84 }
85 }
Ajay Dudani168f6cb2009-12-07 19:04:02 -080086}
87
88static char jtag_putc_buffer[128];
89static unsigned jtag_putc_count = 0;
90
91static void jtag_push_buffer(void)
92{
Ajay Dudanib01e5062011-12-03 23:23:42 -080093 jtag_putc_buffer[jtag_putc_count] = 0;
94 jtag_putc_count = 0;
95 jtag_msg(STATUS_PRINT, jtag_putc_buffer);
Ajay Dudani168f6cb2009-12-07 19:04:02 -080096}
97
98void jtag_dputc(unsigned c)
99{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800100 if ((c < 32) || (c > 127)) {
101 if (c == '\n') {
102 jtag_push_buffer();
103 }
104 return;
105 }
Ajay Dudani168f6cb2009-12-07 19:04:02 -0800106
Ajay Dudanib01e5062011-12-03 23:23:42 -0800107 jtag_putc_buffer[jtag_putc_count++] = c;
108 if (jtag_putc_count == 127) {
109 jtag_push_buffer();
110 }
Ajay Dudani168f6cb2009-12-07 19:04:02 -0800111}