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