]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/hal/arm/mx21/var/v2_0/src/soc_diag.c
Initial revision
[karo-tx-redboot.git] / packages / hal / arm / mx21 / var / v2_0 / src / soc_diag.c
1 /*=============================================================================
2 //
3 //      hal_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 #include <pkgconf/system.h>
44 #include CYGBLD_HAL_PLATFORM_H
45
46 #include <cyg/infra/cyg_type.h>         // base types
47 #include <cyg/infra/cyg_trac.h>         // tracing macros
48 #include <cyg/infra/cyg_ass.h>          // assertion macros
49
50 #include <cyg/hal/hal_arch.h>           // basic machine info
51 #include <cyg/hal/hal_intr.h>           // interrupt macros
52 #include <cyg/hal/hal_io.h>             // IO macros
53 #include <cyg/hal/hal_if.h>             // Calling interface definitions
54 #include <cyg/hal/hal_diag.h>
55 #include <cyg/hal/drv_api.h>            // cyg_drv_interrupt_acknowledge
56 #include <cyg/hal/hal_misc.h>           // Helper functions
57 #include <cyg/hal/hal_soc.h>         // Hardware definitions
58
59 #define MX21_UART_RFDIV                         2                       /* Ref Clock Divisor */
60 #define MX21_UART_REFFREQ                       (MX21_PERCLK1 / MX21_UART_RFDIV)
61
62 /*
63  * UART Control Register 0 Bit Fields.
64  */
65 #define EUartUCR1_ADEN      (1 << 15)           // Auto dectect interrupt
66 #define EUartUCR1_ADBR      (1 << 14)           // Auto detect baud rate
67 #define EUartUCR1_TRDYEN    (1 << 13)           // Transmitter ready interrupt enable
68 #define EUartUCR1_IDEN      (1 << 12)           // Idle condition interrupt
69 #define EUartUCR1_RRDYEN    (1 << 9)            // Recv ready interrupt enable
70 #define EUartUCR1_RDMAEN    (1 << 8)            // Recv ready DMA enable
71 #define EUartUCR1_IREN      (1 << 7)            // Infrared interface enable
72 #define EUartUCR1_TXMPTYEN  (1 << 6)            // Transimitter empty interrupt enable
73 #define EUartUCR1_RTSDEN    (1 << 5)            // RTS delta interrupt enable
74 #define EUartUCR1_SNDBRK    (1 << 4)            // Send break
75 #define EUartUCR1_TDMAEN    (1 << 3)            // Transmitter ready DMA enable
76 #define EUartUCR1_DOZE      (1 << 1)            // Doze
77 #define EUartUCR1_UARTEN    (1 << 0)            // UART enabled
78 #define EUartUCR2_ESCI      (1 << 15)           // Escape seq interrupt enable
79 #define EUartUCR2_IRTS      (1 << 14)           // Ignore RTS pin
80 #define EUartUCR2_CTSC      (1 << 13)           // CTS pin control
81 #define EUartUCR2_CTS       (1 << 12)           // Clear to send
82 #define EUartUCR2_ESCEN     (1 << 11)           // Escape enable
83 #define EUartUCR2_PREN      (1 << 8)            // Parity enable
84 #define EUartUCR2_PROE      (1 << 7)            // Parity odd/even
85 #define EUartUCR2_STPB      (1 << 6)            // Stop
86 #define EUartUCR2_WS        (1 << 5)            // Word size
87 #define EUartUCR2_RTSEN     (1 << 4)            // Request to send interrupt enable
88 #define EUartUCR2_ATEN      (1 << 3)            // Aging timer enable
89 #define EUartUCR2_TXEN      (1 << 2)            // Transmitter enabled
90 #define EUartUCR2_RXEN      (1 << 1)            // Receiver enabled
91 #define EUartUCR2_SRST_     (1 << 0)            // SW reset
92 #define EUartUCR3_PARERREN  (1 << 12)           // Parity enable
93 #define EUartUCR3_FRAERREN  (1 << 11)           // Frame error interrupt enable
94 #define EUartUCR3_ADNIMP    (1 << 7)            // Autobaud detection not improved
95 #define EUartUCR3_RXDSEN    (1 << 6)            // Receive status interrupt enable
96 #define EUartUCR3_AIRINTEN  (1 << 5)            // Async IR wake interrupt enable
97 #define EUartUCR3_AWAKEN    (1 << 4)            // Async wake interrupt enable
98 #define EUartUCR3_RXDMUXSEL (1 << 2)            // RXD muxed input selected
99 #define EUartUCR3_INVT      (1 << 1)            // Inverted Infrared transmission
100 #define EUartUCR3_ACIEN     (1 << 0)            // Autobaud counter interrupt enable
101 #define EUartUCR4_CTSTL_32  (32 << 10)          // CTS trigger level (32 chars)
102 #define EUartUCR4_INVR      (1 << 9)            // Inverted infrared reception
103 #define EUartUCR4_ENIRI     (1 << 8)            // Serial infrared interrupt enable
104 #define EUartUCR4_WKEN      (1 << 7)            // Wake interrupt enable
105 #define EUartUCR4_IRSC      (1 << 5)            // IR special case
106 #define EUartUCR4_LPBYP     (1 << 4)            // Low power bypass
107 #define EUartUCR4_TCEN      (1 << 3)            // Transmit complete interrupt enable
108 #define EUartUCR4_BKEN      (1 << 2)            // Break condition interrupt enable
109 #define EUartUCR4_OREN      (1 << 1)            // Receiver overrun interrupt enable
110 #define EUartUCR4_DREN      (1 << 0)            // Recv data ready interrupt enable
111 #define EUartUFCR_RXTL_SHF  0                   // Receiver trigger level shift
112 #define EUartUFCR_RFDIV_1   (5 << 7)            // Reference freq divider (div 1)
113 #define EUartUFCR_RFDIV_2   (4 << 7)            // Reference freq divider (div 2)
114 #define EUartUFCR_RFDIV_3   (3 << 7)            // Reference freq divider (div 3)
115 #define EUartUFCR_RFDIV_4   (2 << 7)            // Reference freq divider (div 4)
116 #define EUartUFCR_RFDIV_5   (1 << 7)            // Reference freq divider (div 5)
117 #define EUartUFCR_RFDIV_6   (0 << 7)            // Reference freq divider (div 6)
118 #define EUartUFCR_RFDIV_7   (6 << 7)            // Reference freq divider (div 7)
119 #define EUartUFCR_TXTL_SHF  10                  // Transmitter trigger level shift
120 #define EUartUSR1_PARITYERR (1 << 15)           // Parity error interrupt flag
121 #define EUartUSR1_RTSS      (1 << 14)           // RTS pin status
122 #define EUartUSR1_TRDY      (1 << 13)           // Transmitter ready interrupt/dma flag
123 #define EUartUSR1_RTSD      (1 << 12)           // RTS delta
124 #define EUartUSR1_ESCF      (1 << 11)           // Escape seq interrupt flag
125 #define EUartUSR1_FRAMERR   (1 << 10)           // Frame error interrupt flag
126 #define EUartUSR1_RRDY      (1 << 9)            // Receiver ready interrupt/dma flag
127 #define EUartUSR1_AGTIM     (1 << 8)            // Aging timeout interrupt status
128 #define EUartUSR1_RXDS      (1 << 6)            // Receiver idle interrupt flag
129 #define EUartUSR1_AIRINT    (1 << 5)            // Async IR wake interrupt flag
130 #define EUartUSR1_AWAKE     (1 << 4)            // Aysnc wake interrupt flag
131 #define EUartUSR2_ADET      (1 << 15)           // Auto baud rate detect complete
132 #define EUartUSR2_TXFE      (1 << 14)           // Transmit buffer FIFO empty
133 #define EUartUSR2_IDLE      (1 << 12)           // Idle condition
134 #define EUartUSR2_ACST      (1 << 11)           // Autobaud counter stopped
135 #define EUartUSR2_IRINT     (1 << 8)            // Serial infrared interrupt flag
136 #define EUartUSR2_WAKE      (1 << 7)            // Wake
137 #define EUartUSR2_RTSF      (1 << 4)            // RTS edge interrupt flag
138 #define EUartUSR2_TXDC      (1 << 3)            // Transmitter complete
139 #define EUartUSR2_BRCD      (1 << 2)            // Break condition
140 #define EUartUSR2_ORE       (1 << 1)            // Overrun error
141 #define EUartUSR2_RDR       (1 << 0)            // Recv data ready
142 #define EUartUTS_FRCPERR    (1 << 13)           // Force parity error
143 #define EUartUTS_LOOP       (1 << 12)           // Loop tx and rx
144 #define EUartUTS_TXEMPTY    (1 << 6)            // TxFIFO empty
145 #define EUartUTS_RXEMPTY    (1 << 5)            // RxFIFO empty
146 #define EUartUTS_TXFULL     (1 << 4)            // TxFIFO full
147 #define EUartUTS_RXFULL     (1 << 3)            // RxFIFO full
148 #define EUartUTS_SOFTRST    (1 << 0)            // Software reset
149
150 void cyg_hal_plf_serial_init(void);
151
152 #if 0
153 void
154 cyg_hal_plf_comms_init(void)
155 {
156     static int initialized = 0;
157
158     if (initialized)
159         return;
160
161     initialized = 1;
162
163     cyg_hal_plf_serial_init();
164 }
165 #endif
166
167 //=============================================================================
168 // MXC Serial Port (UARTx) for Debug
169 //=============================================================================
170
171 struct mxc_serial {
172     volatile cyg_uint32 urxd[16];
173     volatile cyg_uint32 utxd[16];
174     volatile cyg_uint32 ucr1;
175     volatile cyg_uint32 ucr2;
176     volatile cyg_uint32 ucr3;
177     volatile cyg_uint32 ucr4;
178     volatile cyg_uint32 ufcr;
179     volatile cyg_uint32 usr1;
180     volatile cyg_uint32 usr2;
181     volatile cyg_uint32 uesc;
182     volatile cyg_uint32 utim;
183     volatile cyg_uint32 ubir;
184     volatile cyg_uint32 ubmr;
185     volatile cyg_uint32 ubrc;
186     volatile cyg_uint32 onems;
187     volatile cyg_uint32 uts;
188 };
189
190 typedef struct {
191     volatile struct mxc_serial* base;
192     cyg_int32 msec_timeout;
193     int isr_vector;
194     int baud_rate;
195 } channel_data_t;
196
197 static channel_data_t channels[] = {
198 #if CYGHWR_HAL_ARM_SOC_UART1 != 0
199     {(volatile struct mxc_serial*)MX21_UART1_BASE, 1000,
200       CYGNUM_HAL_INTERRUPT_UART1, CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD },
201 #endif
202 #if CYGHWR_HAL_ARM_SOC_UART2 != 0
203     {(volatile struct mxc_serial*)MX21_UART2_BASE, 1000,
204      CYGNUM_HAL_INTERRUPT_UART2, CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD },
205 #endif
206 #if CYGHWR_HAL_ARM_SOC_UART3 != 0
207     {(volatile struct mxc_serial*)MX21_UART3_BASE, 1000,
208      CYGNUM_HAL_INTERRUPT_UART3, CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD },
209 #endif
210 #if CYGHWR_HAL_ARM_SOC_UART4 != 0
211     {(volatile struct mxc_serial*)MX21_UART4_BASE, 1000,
212      CYGNUM_HAL_INTERRUPT_UART4, CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD },
213 #endif
214 };
215
216 /*---------------------------------------------------------------------------*/
217
218 static void init_serial_channel(channel_data_t* __ch_data)
219 {
220     volatile struct mxc_serial* base = __ch_data->base;
221
222     /* Wait for UART to finish transmitting */
223     while (!(base->uts & EUartUTS_TXEMPTY));
224
225     /* Disable UART */
226     base->ucr1 &= ~EUartUCR1_UARTEN;
227
228     /* Set to default POR state */
229     base->ucr1 = 0x00000000;
230     base->ucr2 = 0x00000000;
231     base->ucr3 = 0x00000004;
232     base->ucr4 = 0x00008000;
233     base->ufcr = 0x00000801;
234     base->uesc = 0x0000002B;
235     base->utim = 0x00000000;
236     base->ubir = 0x00000000;
237     base->ubmr = 0x00000000;
238     base->onems = 0x00000000;
239     base->uts  = 0x00000000;
240
241     /* Configure FIFOs */
242     base->ufcr = (1 << EUartUFCR_RXTL_SHF) | EUartUFCR_RFDIV_2
243                  | (2 << EUartUFCR_TXTL_SHF);
244
245     /* Setup One MS timer */
246     base->onems  = (MX21_UART_REFFREQ / 1000);
247
248     /* Set the numerator value minus one of the BRM ratio */
249     base->ubir = (__ch_data->baud_rate / 100) - 1;
250
251     /* Set the denominator value minus one of the BRM ratio     */
252     base->ubmr = ((MX21_UART_REFFREQ / 1600) - 1);
253
254     /* Set to 8N1 */
255     base->ucr2 &= ~EUartUCR2_PREN;
256     base->ucr2 |= EUartUCR2_WS;
257     base->ucr2 &= ~EUartUCR2_STPB;
258
259     /* Ignore RTS */
260     base->ucr2 |= EUartUCR2_IRTS;
261
262     /* Enable UART */
263     base->ucr1 |= EUartUCR1_UARTEN;
264
265     /* Enable FIFOs */
266     base->ucr2 |= EUartUCR2_SRST_ | EUartUCR2_RXEN | EUartUCR2_TXEN;
267
268     /* Clear status flags */
269     base->usr2 |= EUartUSR2_ADET  |
270                   EUartUSR2_IDLE  |
271                   EUartUSR2_IRINT |
272                   EUartUSR2_WAKE  |
273                   EUartUSR2_RTSF  |
274                   EUartUSR2_BRCD  |
275                   EUartUSR2_ORE   |
276                   EUartUSR2_RDR;
277
278     /* Clear status flags */
279     base->usr1 |= EUartUSR1_PARITYERR |
280                   EUartUSR1_RTSD      |
281                   EUartUSR1_ESCF      |
282                   EUartUSR1_FRAMERR   |
283                   EUartUSR1_AIRINT    |
284                   EUartUSR1_AWAKE;
285 }
286
287 static void stop_serial_channel(channel_data_t* __ch_data)
288 {
289     volatile struct mxc_serial* base = __ch_data->base;
290
291     /* Wait for UART to finish transmitting */
292     while (!(base->uts & EUartUTS_TXEMPTY));
293
294     /* Disable UART */
295     base->ucr1 &= ~EUartUCR1_UARTEN;
296 }
297
298 void cyg_hal_plf_serial_putc(void *__ch_data, char c)
299 {
300     volatile struct mxc_serial* base = ((channel_data_t*)__ch_data)->base;
301     CYGARC_HAL_SAVE_GP();
302
303     // Wait for Tx FIFO not full
304     while (base->uts & EUartUTS_TXFULL)
305         ;
306     base->utxd[0] = c;
307
308     CYGARC_HAL_RESTORE_GP();
309 }
310
311 static cyg_bool cyg_hal_plf_serial_getc_nonblock(void* __ch_data, 
312                                                  cyg_uint8* ch)
313 {
314     volatile struct mxc_serial* base = ((channel_data_t*)__ch_data)->base;
315
316     // If receive fifo is empty, return false
317     if (base->uts & EUartUTS_RXEMPTY)
318         return false;
319
320     *ch = (char)base->urxd[0];
321
322     return true;
323 }
324
325 cyg_uint8 cyg_hal_plf_serial_getc(void* __ch_data)
326 {
327     cyg_uint8 ch;
328     CYGARC_HAL_SAVE_GP();
329
330     while (!cyg_hal_plf_serial_getc_nonblock(__ch_data, &ch));
331
332     CYGARC_HAL_RESTORE_GP();
333     return ch;
334 }
335
336 static void cyg_hal_plf_serial_write(void* __ch_data, const cyg_uint8* __buf,
337                          cyg_uint32 __len)
338 {
339     CYGARC_HAL_SAVE_GP();
340
341     while(__len-- > 0)
342         cyg_hal_plf_serial_putc(__ch_data, *__buf++);
343
344     CYGARC_HAL_RESTORE_GP();
345 }
346
347 static void cyg_hal_plf_serial_read(void* __ch_data, cyg_uint8* __buf, 
348                                     cyg_uint32 __len)
349 {
350     CYGARC_HAL_SAVE_GP();
351
352     while (__len-- > 0)
353         *__buf++ = cyg_hal_plf_serial_getc(__ch_data);
354
355     CYGARC_HAL_RESTORE_GP();
356 }
357
358 cyg_bool cyg_hal_plf_serial_getc_timeout(void* __ch_data, 
359                                          cyg_uint8* ch) 
360 {
361     int delay_count;
362     channel_data_t* chan = (channel_data_t*)__ch_data;
363     cyg_bool res;
364     CYGARC_HAL_SAVE_GP();
365
366     delay_count = chan->msec_timeout * 10; // delay in .1 ms steps
367
368     for(;;) {
369         res = cyg_hal_plf_serial_getc_nonblock(__ch_data, ch);
370         if (res || 0 == delay_count--)
371             break;
372
373         CYGACC_CALL_IF_DELAY_US(100);
374     }
375
376     CYGARC_HAL_RESTORE_GP();
377     return res;
378 }
379
380 static int cyg_hal_plf_serial_control(void *__ch_data, 
381                                       __comm_control_cmd_t __func, ...) 
382 {
383     static int irq_state = 0;
384     channel_data_t* chan = (channel_data_t*)__ch_data;
385     int ret = -1;
386     va_list ap;
387
388     CYGARC_HAL_SAVE_GP();
389     va_start(ap, __func);
390
391     switch (__func) {
392     case __COMMCTL_GETBAUD:
393         ret = chan->baud_rate;
394         break;
395     case __COMMCTL_SETBAUD:
396         chan->baud_rate = va_arg(ap, cyg_int32);
397         // Should we verify this value here?
398         init_serial_channel(chan);
399         ret = 0;
400         break;
401     case __COMMCTL_IRQ_ENABLE:
402         irq_state = 1;
403
404         chan->base->ucr1 |= EUartUCR1_RRDYEN;
405
406         HAL_INTERRUPT_UNMASK(chan->isr_vector);
407         break;
408     case __COMMCTL_IRQ_DISABLE:
409         ret = irq_state;
410         irq_state = 0;
411
412         chan->base->ucr1 &= ~EUartUCR1_RRDYEN;
413
414         HAL_INTERRUPT_MASK(chan->isr_vector);
415         break;
416     case __COMMCTL_DBG_ISR_VECTOR:
417         ret = chan->isr_vector;
418         break;
419     case __COMMCTL_SET_TIMEOUT:
420         ret = chan->msec_timeout;
421         chan->msec_timeout = va_arg(ap, cyg_uint32);
422         break;
423     default:
424         break;
425     }
426     va_end(ap);
427     CYGARC_HAL_RESTORE_GP();
428     return ret;
429 }
430
431 static int cyg_hal_plf_serial_isr(void *__ch_data, int* __ctrlc,
432                        CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
433 {
434     int res = 0;
435     channel_data_t* chan = (channel_data_t*)__ch_data;
436     char c;
437
438     CYGARC_HAL_SAVE_GP();
439
440     cyg_drv_interrupt_acknowledge(chan->isr_vector);
441
442     *__ctrlc = 0;
443     if (!(chan->base->uts & EUartUTS_RXEMPTY)) {
444         c = (char)chan->base->urxd[0];
445
446         if (cyg_hal_is_break( &c , 1 ))
447             *__ctrlc = 1;
448
449         res = CYG_ISR_HANDLED;
450     }
451
452     CYGARC_HAL_RESTORE_GP();
453     return res;
454 }
455
456 void cyg_hal_plf_serial_init(void)
457 {
458     hal_virtual_comm_table_t* comm;
459     int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
460     int i;
461     static int jjj = 0;
462
463     // Init channels
464 #define NUMOF(x) (sizeof(x)/sizeof(x[0]))
465     for (i = 0;  i < NUMOF(channels);  i++) {
466         init_serial_channel(&channels[i]);
467         CYGACC_CALL_IF_SET_CONSOLE_COMM(i+2);
468         comm = CYGACC_CALL_IF_CONSOLE_PROCS();
469         CYGACC_COMM_IF_CH_DATA_SET(*comm, &channels[i]);
470         CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_serial_write);
471         CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_serial_read);
472         CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_serial_putc);
473         CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_serial_getc);
474         CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_serial_control);
475         CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_serial_isr);
476         CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_serial_getc_timeout);
477         if (jjj == 0) {
478             cyg_hal_plf_serial_putc(&channels[i], '+');
479             jjj++;
480         }
481         cyg_hal_plf_serial_putc(&channels[i], '+');
482     }
483
484     // Restore original console
485     CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
486 }
487
488 void cyg_hal_plf_serial_stop(void)
489 {
490         int i;
491
492         // Init channels
493 #define NUMOF(x) (sizeof(x)/sizeof(x[0]))
494         for (i = 0;  i < NUMOF(channels);  i++) {
495                 stop_serial_channel(&channels[i]);
496         }
497 }
498
499 //=============================================================================
500 // Compatibility with older stubs
501 //=============================================================================
502
503 #ifndef CYGSEM_HAL_VIRTUAL_VECTOR_DIAG
504
505 #include <cyg/hal/hal_stub.h>           // cyg_hal_gdb_interrupt
506
507 #if (CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL == 2)
508 #define __BASE ((void*)MX21_UART1_BASE)
509 #define CYGHWR_HAL_GDB_PORT_VECTOR CYGNUM_HAL_INTERRUPT_UART1
510 #elif (CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL == 3)
511 #define __BASE ((void*)MX21_UART2_BASE)
512 #define CYGHWR_HAL_GDB_PORT_VECTOR CYGNUM_HAL_INTERRUPT_UART2
513 #elif (CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL == 4)
514 #define __BASE ((void*)MX21_UART3_BASE)
515 #define CYGHWR_HAL_GDB_PORT_VECTOR CYGNUM_HAL_INTERRUPT_UART3
516 #elif (CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL == 5)
517 #define __BASE ((void*)MX21_UART4_BASE)
518 #define CYGHWR_HAL_GDB_PORT_VECTOR CYGNUM_HAL_INTERRUPT_UART4
519 #endif
520
521 #ifdef __BASE
522
523 #ifdef CYGSEM_HAL_ROM_MONITOR
524 #define CYG_HAL_STARTUP_ROM
525 #define CYG_HAL_STARTUP_ROMRAM
526 #undef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
527 #endif
528
529 #if (defined(CYG_HAL_STARTUP_ROM) || defined(CYG_HAL_STARTUP_ROMRAM)) && !defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS)
530 #define HAL_DIAG_USES_HARDWARE
531 #elif !defined(CYGDBG_HAL_DIAG_TO_DEBUG_CHAN)
532 #define HAL_DIAG_USES_HARDWARE
533 #elif CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL != CYGNUM_HAL_VIRTUAL_VECTOR_DEBUG_CHANNEL
534 #define HAL_DIAG_USES_HARDWARE
535 #endif
536
537 static channel_data_t channel = {
538     (volatile struct mxc_serial*)__BASE, 0, CYGHWR_HAL_GDB_PORT_VECTOR
539 };
540
541 #ifdef HAL_DIAG_USES_HARDWARE
542
543 void hal_diag_init(void)
544 {
545     static int init = 0;
546     char *msg = "\n\rARM eCos\n\r";
547     cyg_uint8 lcr;
548
549     if (init++) return;
550
551     init_serial_channel(&channel);
552
553     while (*msg) hal_diag_write_char(*msg++);
554 }
555
556 #ifdef DEBUG_DIAG
557 #ifndef CYG_HAL_STARTUP_ROM
558 #define DIAG_BUFSIZE 2048
559 static char diag_buffer[DIAG_BUFSIZE];
560 static int diag_bp = 0;
561 #endif
562 #endif
563
564 void hal_diag_write_char(char c)
565 {
566 #ifdef DEBUG_DIAG
567 #ifndef CYG_HAL_STARTUP_ROM
568     diag_buffer[diag_bp++] = c;
569     if (diag_bp == sizeof(diag_buffer)) diag_bp = 0;
570 #endif
571 #endif
572     cyg_hal_plf_serial_putc(&channel, c);
573 }
574
575 void hal_diag_read_char(char *c)
576 {
577     *c = cyg_hal_plf_serial_getc(&channel);
578 }
579
580 #else // not HAL_DIAG_USES_HARDWARE - it uses GDB protocol
581
582 void hal_diag_read_char(char *c)
583 {
584     *c = cyg_hal_plf_serial_getc(&channel);
585 }
586
587 void hal_diag_write_char(char c)
588 {
589     static char line[100];
590     static int pos = 0;
591
592     // FIXME: Some LED blinking might be nice right here.
593
594     // No need to send CRs
595     if( c == '\r' ) return;
596
597     line[pos++] = c;
598
599         if (c == '\n' || pos == sizeof(line)) {
600         CYG_INTERRUPT_STATE old;
601
602         // Disable interrupts. This prevents GDB trying to interrupt us
603         // while we are in the middle of sending a packet. The serial
604         // receive interrupt will be seen when we re-enable interrupts
605         // later.
606
607 #ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
608         CYG_HAL_GDB_ENTER_CRITICAL_IO_REGION(old);
609 #else
610         HAL_DISABLE_INTERRUPTS(old);
611 #endif
612
613         while (1) {
614             static char hex[] = "0123456789ABCDEF";
615             cyg_uint8 csum = 0;
616             int i;
617 #ifndef CYGDBG_HAL_DEBUG_GDB_CTRLC_SUPPORT
618             char c1;
619 #endif
620             cyg_hal_plf_serial_putc(&channel, '$');
621             cyg_hal_plf_serial_putc(&channel, 'O');
622             csum += 'O';
623             for(i = 0; i < pos; i++) {
624                 char ch = line[i];
625                 char h = hex[(ch>>4)&0xF];
626                 char l = hex[ch&0xF];
627                 cyg_hal_plf_serial_putc(&channel, h);
628                 cyg_hal_plf_serial_putc(&channel, l);
629                 csum += h;
630                 csum += l;
631             }
632             cyg_hal_plf_serial_putc(&channel, '#');
633             cyg_hal_plf_serial_putc(&channel, hex[(csum>>4)&0xF]);
634             cyg_hal_plf_serial_putc(&channel, hex[csum&0xF]);
635
636 #ifdef CYGDBG_HAL_DEBUG_GDB_CTRLC_SUPPORT
637
638             break; // regardless
639
640 #else // not CYGDBG_HAL_DEBUG_GDB_CTRLC_SUPPORT Ie. usually...
641
642             // Wait for the ACK character '+' from GDB here and handle
643             // receiving a ^C instead.  This is the reason for this clause
644             // being a loop.
645             c1 = cyg_hal_plf_serial_getc(&channel);
646
647             if( c1 == '+' )
648                 break;              // a good acknowledge
649
650 #ifdef CYGDBG_HAL_DEBUG_GDB_BREAK_SUPPORT
651             cyg_drv_interrupt_acknowledge(CYGHWR_HAL_GDB_PORT_VECTOR);
652             if( c1 == 3 ) {
653                 // Ctrl-C: breakpoint.
654                 cyg_hal_gdb_interrupt(
655                     (target_register_t)__builtin_return_address(0) );
656                 break;
657             }
658 #endif // CYGDBG_HAL_DEBUG_GDB_BREAK_SUPPORT
659
660 #endif // ! CYGDBG_HAL_DEBUG_GDB_CTRLC_SUPPORT
661             // otherwise, loop round again
662         }
663
664         pos = 0;
665
666         // And re-enable interrupts
667 #ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
668         CYG_HAL_GDB_LEAVE_CRITICAL_IO_REGION(old);
669 #else
670         HAL_RESTORE_INTERRUPTS(old);
671 #endif
672
673     }
674 }
675 #endif
676
677 #endif // __BASE
678
679 #endif // !CYGSEM_HAL_VIRTUAL_VECTOR_DIAG
680
681 /*---------------------------------------------------------------------------*/
682 /* End of hal_diag.c */