]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/hal/arm/mx27/karo/v1_0/src/tx27_diag.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / hal / arm / mx27 / karo / v1_0 / src / tx27_diag.c
1 /*=============================================================================
2 //
3 //      board_diag.c
4 //
5 //      HAL diagnostic output code
6 //
7 //=============================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //===========================================================================*/
41
42 #include <pkgconf/hal.h>
43
44 #include <cyg/infra/cyg_type.h>         // base types
45 #include <cyg/infra/cyg_trac.h>         // tracing macros
46 #include <cyg/infra/cyg_ass.h>          // assertion macros
47
48 #include <cyg/hal/hal_arch.h>           // basic machine info
49 #include <cyg/hal/hal_intr.h>           // interrupt macros
50 #include <cyg/hal/hal_io.h>             // IO macros
51 #include <cyg/hal/hal_diag.h>
52 #include <cyg/hal/hal_if.h>             // Calling-if API
53 #include <cyg/hal/drv_api.h>            // driver API
54 #include <cyg/hal/hal_misc.h>           // Helper functions
55 #include <cyg/hal/hal_soc.h>            // Hardware definitions
56 #include <cyg/hal/karo_tx27.h>          // Platform specifics
57
58 extern void cyg_hal_plf_serial_init(void);
59
60 void cyg_hal_plf_comms_init(void)
61 {
62         static int initialized = 0;
63
64         if (initialized) {
65                 return;
66         }
67         initialized = 1;
68         cyg_hal_plf_serial_init();
69 }
70
71 //-----------------------------------------------------------------------------
72 // Based on 3.6864 MHz xtal
73 #if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==9600
74 #define CYG_DEV_SERIAL_BAUD_MSB        0x00
75 #define CYG_DEV_SERIAL_BAUD_LSB        0x18
76 #endif
77 #if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==19200
78 #define CYG_DEV_SERIAL_BAUD_MSB        0x00
79 #define CYG_DEV_SERIAL_BAUD_LSB        0x0C
80 #endif
81 #if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==38400
82 #define CYG_DEV_SERIAL_BAUD_MSB        0x00
83 #define CYG_DEV_SERIAL_BAUD_LSB        0x06
84 #endif
85 #if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==57600
86 #define CYG_DEV_SERIAL_BAUD_MSB        0x00
87 #define CYG_DEV_SERIAL_BAUD_LSB        0x04
88 #endif
89 #if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==115200
90 #define CYG_DEV_SERIAL_BAUD_MSB        0x00
91 #define CYG_DEV_SERIAL_BAUD_LSB        0x02
92 #endif
93
94 #ifndef CYG_DEV_SERIAL_BAUD_MSB
95 #error Missing/incorrect serial baud rate defined - CDL error?
96 #endif
97
98 //-----------------------------------------------------------------------------
99 // Define the serial registers. The board is equipped with a 16552
100 // serial chip.
101
102 #if defined (EXT_UART_x16)
103 #define HAL_WRITE_UINT_UART HAL_WRITE_UINT16
104 #define HAL_READ_UINT_UART HAL_READ_UINT16
105 typedef cyg_uint16 uart_width;
106 #elif defined (EXT_UART_x32)
107 #define HAL_WRITE_UINT_UART HAL_WRITE_UINT32
108 #define HAL_READ_UINT_UART HAL_READ_UINT32
109 typedef cyg_uint32 uart_width;
110 #else  //_x8
111 #define HAL_WRITE_UINT_UART HAL_WRITE_UINT8
112 #define HAL_READ_UINT_UART HAL_READ_UINT8
113 typedef cyg_uint8 uart_width;
114 #endif
115
116 #define CYG_DEV_SERIAL_RHR   0x00 // receiver buffer register, read, dlab = 0
117 #define CYG_DEV_SERIAL_THR   0x00 // transmitter holding register, write, dlab = 0
118 #define CYG_DEV_SERIAL_DLL   0x00 // divisor latch (LS), read/write, dlab = 1
119 #define CYG_DEV_SERIAL_IER   0x01 // interrupt enable register, read/write, dlab = 0
120 #define CYG_DEV_SERIAL_DLM   0x01 // divisor latch (MS), read/write, dlab = 1
121 #define CYG_DEV_SERIAL_IIR   0x02 // interrupt identification register, read, dlab = 0
122 #define CYG_DEV_SERIAL_FCR   0x02 // fifo control register, write, dlab = 0
123 #define CYG_DEV_SERIAL_AFR   0x02 // alternate function register, read/write, dlab = 1
124 #define CYG_DEV_SERIAL_LCR   0x03 // line control register, read/write
125 #define CYG_DEV_SERIAL_MCR   0x04
126 #define CYG_DEV_SERIAL_MCR_A 0x04
127 #define CYG_DEV_SERIAL_MCR_B 0x04
128 #define CYG_DEV_SERIAL_LSR   0x05 // line status register, read
129 #define CYG_DEV_SERIAL_MSR   0x06 // modem status register, read
130 #define CYG_DEV_SERIAL_SCR   0x07 // scratch pad register
131
132 // The interrupt enable register bits.
133 #define SIO_IER_ERDAI   0x01            // enable received data available irq
134 #define SIO_IER_ETHREI  0x02            // enable THR empty interrupt
135 #define SIO_IER_ELSI    0x04            // enable receiver line status irq
136 #define SIO_IER_EMSI    0x08            // enable modem status interrupt
137
138 // The interrupt identification register bits.
139 #define SIO_IIR_IP      0x01            // 0 if interrupt pending
140 #define SIO_IIR_ID_MASK 0x0e            // mask for interrupt ID bits
141 #define ISR_Tx  0x02
142 #define ISR_Rx  0x04
143
144 // The line status register bits.
145 #define SIO_LSR_DR      0x01            // data ready
146 #define SIO_LSR_OE      0x02            // overrun error
147 #define SIO_LSR_PE      0x04            // parity error
148 #define SIO_LSR_FE      0x08            // framing error
149 #define SIO_LSR_BI      0x10            // break interrupt
150 #define SIO_LSR_THRE    0x20            // transmitter holding register empty
151 #define SIO_LSR_TEMT    0x40            // transmitter register empty
152 #define SIO_LSR_ERR     0x80            // any error condition
153
154 // The modem status register bits.
155 #define SIO_MSR_DCTS    0x01            // delta clear to send
156 #define SIO_MSR_DDSR    0x02            // delta data set ready
157 #define SIO_MSR_TERI    0x04            // trailing edge ring indicator
158 #define SIO_MSR_DDCD    0x08            // delta data carrier detect
159 #define SIO_MSR_CTS     0x10            // clear to send
160 #define SIO_MSR_DSR     0x20            // data set ready
161 #define SIO_MSR_RI      0x40            // ring indicator
162 #define SIO_MSR_DCD     0x80            // data carrier detect
163
164 // The line control register bits.
165 #define SIO_LCR_WLS0   0x01             // word length select bit 0
166 #define SIO_LCR_WLS1   0x02             // word length select bit 1
167 #define SIO_LCR_STB    0x04             // number of stop bits
168 #define SIO_LCR_PEN    0x08             // parity enable
169 #define SIO_LCR_EPS    0x10             // even parity select
170 #define SIO_LCR_SP     0x20             // stick parity
171 #define SIO_LCR_SB     0x40             // set break
172 #define SIO_LCR_DLAB   0x80             // divisor latch access bit
173
174 // The FIFO control register
175 #define SIO_FCR_FCR0   0x01             // enable xmit and rcvr fifos
176 #define SIO_FCR_FCR1   0x02             // clear RCVR FIFO
177 #define SIO_FCR_FCR2   0x04             // clear XMIT FIFO
178
179 //-----------------------------------------------------------------------------
180
181 //#define x_debug_uart_log_buf
182 #ifdef x_debug_uart_log_buf
183 #define x_DIAG_BUFSIZE 2048
184 static char __x_log_buf[x_DIAG_BUFSIZE];
185 static int x_diag_bp = 0;
186 #endif
187
188 //=============================================================================
189 // Compatibility with older stubs
190 //=============================================================================
191
192 #ifndef CYGSEM_HAL_VIRTUAL_VECTOR_DIAG
193
194 #include <cyg/hal/hal_stub.h>           // cyg_hal_gdb_interrupt
195
196 #ifdef __BASE
197
198 #ifdef CYGSEM_HAL_ROM_MONITOR
199 #define CYG_HAL_STARTUP_ROM
200 #define CYG_HAL_STARTUP_ROMRAM
201 #undef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
202 #endif
203
204 #if (defined(CYG_HAL_STARTUP_ROM) || defined(CYG_HAL_STARTUP_ROMRAM)) && !defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS)
205 #define HAL_DIAG_USES_HARDWARE
206 #elif !defined(CYGDBG_HAL_DIAG_TO_DEBUG_CHAN)
207 #define HAL_DIAG_USES_HARDWARE
208 #elif CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL != CYGNUM_HAL_VIRTUAL_VECTOR_DEBUG_CHANNEL
209 #define HAL_DIAG_USES_HARDWARE
210 #endif
211
212 static channel_data_t channel = {
213     (uart_width*) _BASE, 0, 0
214 };
215
216 #ifdef HAL_DIAG_USES_HARDWARE
217
218 void hal_diag_init(void)
219 {
220     static int init = 0;
221     char *msg = "\n\rARM eCos\n\r";
222     uart_width lcr;
223
224     if (init++) return;
225
226     init_duart_channel(&channel);
227
228     while (*msg) hal_diag_write_char(*msg++);
229 }
230
231 #ifdef DEBUG_DIAG
232 #if defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS)
233 #define DIAG_BUFSIZE 32
234 #else
235 #define DIAG_BUFSIZE 2048
236 #endif
237 static char diag_buffer[DIAG_BUFSIZE];
238 static int diag_bp = 0;
239 #endif
240
241 void hal_diag_write_char(char c)
242 {
243     uart_width lsr;
244
245     hal_diag_init();
246
247     cyg_hal_plf_duart_putc(&channel, c)
248
249 #ifdef DEBUG_DIAG
250     diag_buffer[diag_bp++] = c;
251     if (diag_bp == DIAG_BUFSIZE) {
252         while (1) ;
253         diag_bp = 0;
254     }
255 #endif
256 }
257
258 void hal_diag_read_char(char *c)
259 {
260     *c = cyg_hal_plf_duart_getc(&channel);
261 }
262
263 #else // HAL_DIAG relies on GDB
264
265 // Initialize diag port - assume GDB channel is already set up
266 void hal_diag_init(void)
267 {
268     if (0) init_duart_channel(&channel); // avoid warning
269 }
270
271 // Actually send character down the wire
272 static void hal_diag_write_char_serial(char c)
273 {
274     cyg_hal_plf_duart_putc(&channel, c);
275 }
276
277 static bool hal_diag_read_serial(char *c)
278 {
279     long timeout = 1000000000;  // A long time...
280
281     while (!cyg_hal_plf_duart_getc_nonblock(&channel, c))
282         if (0 == --timeout) return false;
283
284     return true;
285 }
286
287 void hal_diag_read_char(char *c)
288 {
289     while (!hal_diag_read_serial(c)) ;
290 }
291
292 void hal_diag_write_char(char c)
293 {
294     static char line[100];
295     static int pos = 0;
296
297     // No need to send CRs
298     if (c == '\r') return;
299
300     line[pos++] = c;
301
302     if (c == '\n' || pos == sizeof(line)) {
303         CYG_INTERRUPT_STATE old;
304
305         // Disable interrupts. This prevents GDB trying to interrupt us
306         // while we are in the middle of sending a packet. The serial
307         // receive interrupt will be seen when we re-enable interrupts
308         // later.
309
310 #ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
311         CYG_HAL_GDB_ENTER_CRITICAL_IO_REGION(old);
312 #else
313         HAL_DISABLE_INTERRUPTS(old);
314 #endif
315
316         while (1) {
317             static char hex[] = "0123456789ABCDEF";
318             cyg_uint8 csum = 0;
319             int i;
320             char c1;
321
322             hal_diag_write_char_serial('$');
323             hal_diag_write_char_serial('O');
324             csum += 'O';
325             for (i = 0; i < pos; i++) {
326                         char ch = line[i];
327                         char h = hex[(ch >> 4) & 0xF];
328                         char l = hex[ch & 0xF];
329                         hal_diag_write_char_serial(h);
330                         hal_diag_write_char_serial(l);
331                         csum += h;
332                         csum += l;
333             }
334             hal_diag_write_char_serial('#');
335             hal_diag_write_char_serial(hex[(csum >> 4) & 0xF]);
336             hal_diag_write_char_serial(hex[csum & 0xF]);
337
338             // Wait for the ACK character '+' from GDB here and handle
339             // receiving a ^C instead.  This is the reason for this clause
340             // being a loop.
341             if (!hal_diag_read_serial(&c1))
342                 continue;   // No response - try sending packet again
343
344             if (c1 == '+')
345                 break;          // a good acknowledge
346
347 #ifdef CYGDBG_HAL_DEBUG_GDB_BREAK_SUPPORT
348             cyg_drv_interrupt_acknowledge(CYG_DEV_SERIAL_INT);
349             if (c1 == 3) {
350                         // Ctrl-C: breakpoint.
351                         cyg_hal_gdb_interrupt (__builtin_return_address(0));
352                         break;
353             }
354 #endif
355             // otherwise, loop round again
356         }
357
358         pos = 0;
359
360         // And re-enable interrupts
361 #ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
362         CYG_HAL_GDB_LEAVE_CRITICAL_IO_REGION(old);
363 #else
364         HAL_RESTORE_INTERRUPTS(old);
365 #endif
366
367     }
368 }
369 #endif
370
371 #endif // __BASE
372
373 #endif // CYGSEM_HAL_VIRTUAL_VECTOR_DIAG
374
375 /*---------------------------------------------------------------------------*/