blob: 0430eaf6c94b38f1892d56a5b013f1961f955849 [file] [log] [blame]
Ajay Dudani168f6cb2009-12-07 19:04:02 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <jtag.h>
30
31#define STATUS_NOMSG 0
32#define STATUS_OKAY 1
33#define STATUS_FAIL 2
34#define STATUS_PRINT 3
35
36volatile unsigned _jtag_cmd = 0;
37volatile unsigned _jtag_msg = 0;
38unsigned char _jtag_cmd_buffer[128];
39unsigned char _jtag_msg_buffer[128];
40
41volatile unsigned _jtag_arg0 = 0;
42volatile unsigned _jtag_arg1 = 0;
43volatile unsigned _jtag_arg2 = 0;
44
Ajay Dudani168f6cb2009-12-07 19:04:02 -080045void jtag_hook(void);
46
47static void jtag_msg(unsigned status, const char *msg)
48{
Ajay Dudanib01e5062011-12-03 23:23:42 -080049 unsigned char *out = _jtag_msg_buffer;
50 while ((*out++ = *msg++) != 0) ;
51 _jtag_msg = status;
52 do {
53 jtag_hook();
54 }
55 while (_jtag_msg != 0);
Ajay Dudani168f6cb2009-12-07 19:04:02 -080056}
57
58void jtag_okay(const char *msg)
59{
Ajay Dudanib01e5062011-12-03 23:23:42 -080060 if (msg == 0)
61 msg = "OKAY";
62 jtag_msg(STATUS_OKAY, msg);
Ajay Dudani168f6cb2009-12-07 19:04:02 -080063}
64
65void jtag_fail(const char *msg)
66{
Ajay Dudanib01e5062011-12-03 23:23:42 -080067 if (msg == 0)
68 msg = "FAIL";
69 jtag_msg(STATUS_FAIL, msg);
Ajay Dudani168f6cb2009-12-07 19:04:02 -080070}
71
72int jtag_cmd_pending()
73{
Ajay Dudanib01e5062011-12-03 23:23:42 -080074 jtag_hook();
75 return (int)_jtag_cmd;
Ajay Dudani168f6cb2009-12-07 19:04:02 -080076}
77
Ajay Dudanib01e5062011-12-03 23:23:42 -080078void jtag_cmd_loop(void (*do_cmd) (const char *, unsigned, unsigned, unsigned))
Ajay Dudani168f6cb2009-12-07 19:04:02 -080079{
Ajay Dudanib01e5062011-12-03 23:23:42 -080080 unsigned n;
81 for (;;) {
82 if (jtag_cmd_pending()) {
83 do_cmd((const char *)_jtag_cmd_buffer, _jtag_arg0,
84 _jtag_arg1, _jtag_arg2);
85 for (n = 0; n < 256; n++)
86 _jtag_cmd_buffer[n] = 0;
87 _jtag_arg0 = 0;
88 _jtag_arg1 = 0;
89 _jtag_arg2 = 0;
90 _jtag_cmd = 0;
91 }
92 }
Ajay Dudani168f6cb2009-12-07 19:04:02 -080093}
94
95static char jtag_putc_buffer[128];
96static unsigned jtag_putc_count = 0;
97
98static void jtag_push_buffer(void)
99{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800100 jtag_putc_buffer[jtag_putc_count] = 0;
101 jtag_putc_count = 0;
102 jtag_msg(STATUS_PRINT, jtag_putc_buffer);
Ajay Dudani168f6cb2009-12-07 19:04:02 -0800103}
104
105void jtag_dputc(unsigned c)
106{
Ajay Dudanib01e5062011-12-03 23:23:42 -0800107 if ((c < 32) || (c > 127)) {
108 if (c == '\n') {
109 jtag_push_buffer();
110 }
111 return;
112 }
Ajay Dudani168f6cb2009-12-07 19:04:02 -0800113
Ajay Dudanib01e5062011-12-03 23:23:42 -0800114 jtag_putc_buffer[jtag_putc_count++] = c;
115 if (jtag_putc_count == 127) {
116 jtag_push_buffer();
117 }
Ajay Dudani168f6cb2009-12-07 19:04:02 -0800118}