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