Merge branch 'master' of git://git.denx.de/u-boot
[oweals/u-boot.git] / cpu / blackfin / jtag-console.c
1 /*
2  * jtag-console.c - console driver over Blackfin JTAG
3  *
4  * Copyright (c) 2008 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8
9 #include <common.h>
10 #include <devices.h>
11 #include <asm/blackfin.h>
12
13 #ifndef CONFIG_JTAG_CONSOLE_TIMEOUT
14 # define CONFIG_JTAG_CONSOLE_TIMEOUT 100
15 #endif
16
17 /* The Blackfin tends to be much much faster than the JTAG hardware. */
18 static void jtag_write_emudat(uint32_t emudat)
19 {
20         static bool overflowed = false;
21         ulong timeout = get_timer(0) + CONFIG_JTAG_CONSOLE_TIMEOUT;
22         while (bfin_read_DBGSTAT() & 0x1) {
23                 if (overflowed)
24                         return;
25                 if (timeout < get_timer(0))
26                         overflowed = true;
27         }
28         overflowed = false;
29         __asm__ __volatile__("emudat = %0;" : : "d"(emudat));
30 }
31 /* Transmit a buffer.  The format is:
32  * [32bit length][actual data]
33  */
34 static void jtag_send(const char *c, uint32_t len)
35 {
36         uint32_t i;
37
38         if (len == 0)
39                 return;
40
41         /* First send the length */
42         jtag_write_emudat(len);
43
44         /* Then send the data */
45         for (i = 0; i < len; i += 4)
46                 jtag_write_emudat((c[i] << 0) | (c[i+1] << 8) | (c[i+2] << 16) | (c[i+3] << 24));
47 }
48 static void jtag_putc(const char c)
49 {
50         jtag_send(&c, 1);
51 }
52 static void jtag_puts(const char *s)
53 {
54         jtag_send(s, strlen(s));
55 }
56
57 static int jtag_tstc(void)
58 {
59         return (bfin_read_DBGSTAT() & 0x2);
60 }
61
62 /* Receive a buffer.  The format is:
63  * [32bit length][actual data]
64  */
65 static size_t inbound_len;
66 static int leftovers_len;
67 static uint32_t leftovers;
68 static int jtag_getc(void)
69 {
70         int ret;
71         uint32_t emudat;
72
73         /* see if any data is left over */
74         if (leftovers_len) {
75                 --leftovers_len;
76                 ret = leftovers & 0xff;
77                 leftovers >>= 8;
78                 return ret;
79         }
80
81         /* wait for new data ! */
82         while (!jtag_tstc())
83                 continue;
84         __asm__("%0 = emudat;" : "=d"(emudat));
85
86         if (inbound_len == 0) {
87                 /* grab the length */
88                 inbound_len = emudat;
89         } else {
90                 /* store the bytes */
91                 leftovers_len = min(4, inbound_len);
92                 inbound_len -= leftovers_len;
93                 leftovers = emudat;
94         }
95
96         return jtag_getc();
97 }
98
99 int drv_jtag_console_init(void)
100 {
101         device_t dev;
102         int ret;
103
104         memset(&dev, 0x00, sizeof(dev));
105         strcpy(dev.name, "jtag");
106         dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
107         dev.putc = jtag_putc;
108         dev.puts = jtag_puts;
109         dev.tstc = jtag_tstc;
110         dev.getc = jtag_getc;
111
112         ret = device_register(&dev);
113         return (ret == 0 ? 1 : ret);
114 }
115
116 #ifdef CONFIG_UART_CONSOLE_IS_JTAG
117 /* Since the JTAG is always available (at power on), allow it to fake a UART */
118 void serial_set_baud(uint32_t baud) {}
119 void serial_setbrg(void)            {}
120 int serial_init(void)               { return 0; }
121 void serial_putc(const char c)      __attribute__((alias("jtag_putc")));
122 void serial_puts(const char *s)     __attribute__((alias("jtag_puts")));
123 int serial_tstc(void)               __attribute__((alias("jtag_tstc")));
124 int serial_getc(void)               __attribute__((alias("jtag_getc")));
125 #endif