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