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