1 /*=============================================================================
5 // HAL diagnostic output code
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.
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.
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
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.
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.
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.
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 //===========================================================================*/
42 #include <pkgconf/hal.h>
44 #include <cyg/infra/cyg_type.h> // base types
45 #include <cyg/infra/cyg_trac.h> // tracing macros
46 #include <cyg/infra/cyg_ass.h> // assertion macros
48 #include <cyg/hal/hal_arch.h> // basic machine info
49 #include <cyg/hal/hal_intr.h> // interrupt macros
50 #include <cyg/hal/hal_io.h> // IO macros
51 #include <cyg/hal/hal_diag.h>
52 #include <cyg/hal/hal_if.h> // Calling-if API
53 #include <cyg/hal/drv_api.h> // driver API
54 #include <cyg/hal/hal_misc.h> // Helper functions
55 #include <cyg/hal/hal_soc.h> // Hardware definitions
56 #include <cyg/hal/fsl_board.h> // Platform specifics
58 static void cyg_hal_plf_duart_init(void);
59 extern void cyg_hal_plf_serial_init(void);
61 #define CHECK_BOARD_VERSION(ret)
62 #define CHECK_BOARD_VERSION_RET_VOID
65 #define CHECK_BOARD_VERSION(ret) \
67 if (((*(volatile unsigned short*)0xB4000000) >> 8) < 0x15) \
71 #define CHECK_BOARD_VERSION_RET_VOID \
73 if (((*(volatile unsigned short*)0xB4000000) >> 8) < 0x15) \
78 void cyg_hal_plf_comms_init(void)
80 static int initialized = 0;
87 /* Setup GPIO and enable transceiver for UARTs */
88 cyg_hal_plf_duart_init();
89 cyg_hal_plf_serial_init();
92 //=============================================================================
93 // ST16552 DUART driver
94 //=============================================================================
96 //-----------------------------------------------------------------------------
97 // There are two serial ports.
98 #define CYG_DEV_SERIAL_BASE_A (BOARD_CS_UART_BASE + 0x0000) // port A
99 #define CYG_DEV_SERIAL_BASE_B (BOARD_CS_UART_BASE + 0x0010) // port B
101 //-----------------------------------------------------------------------------
102 // Based on 14.7456 MHz xtal
103 #if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==9600
104 #define CYG_DEV_SERIAL_BAUD_MSB 0x00
105 #define CYG_DEV_SERIAL_BAUD_LSB 0x60
107 #if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==19200
108 #define CYG_DEV_SERIAL_BAUD_MSB 0x00
109 #define CYG_DEV_SERIAL_BAUD_LSB 0x30
111 #if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==38400
112 #define CYG_DEV_SERIAL_BAUD_MSB 0x00
113 #define CYG_DEV_SERIAL_BAUD_LSB 0x18
115 #if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==57600
116 #define CYG_DEV_SERIAL_BAUD_MSB 0x00
117 #define CYG_DEV_SERIAL_BAUD_LSB 0x10
119 #if CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL_BAUD==115200
120 #define CYG_DEV_SERIAL_BAUD_MSB 0x00
121 #define CYG_DEV_SERIAL_BAUD_LSB 0x08
124 #ifndef CYG_DEV_SERIAL_BAUD_MSB
125 #error Missing/incorrect serial baud rate defined - CDL error?
128 //-----------------------------------------------------------------------------
129 // Define the serial registers. The board is equipped with a 16552
133 #define HAL_WRITE_UINT_UART HAL_WRITE_UINT16
134 #define HAL_READ_UINT_UART HAL_READ_UINT16
135 typedef cyg_uint16 uart_width;
137 typedef cyg_uint8 uart_width;
138 #define HAL_WRITE_UINT_UART HAL_WRITE_UINT8
139 #define HAL_READ_UINT_UART HAL_READ_UINT8
142 #define CYG_DEV_SERIAL_RHR 0x00 // receiver buffer register, read, dlab = 0
143 #define CYG_DEV_SERIAL_THR 0x00 // transmitter holding register, write, dlab = 0
144 #define CYG_DEV_SERIAL_DLL 0x00 // divisor latch (LS), read/write, dlab = 1
145 #define CYG_DEV_SERIAL_IER 0x01 // interrupt enable register, read/write, dlab = 0
146 #define CYG_DEV_SERIAL_DLM 0x01 // divisor latch (MS), read/write, dlab = 1
147 #define CYG_DEV_SERIAL_IIR 0x02 // interrupt identification register, read, dlab = 0
148 #define CYG_DEV_SERIAL_FCR 0x02 // fifo control register, write, dlab = 0
149 #define CYG_DEV_SERIAL_AFR 0x02 // alternate function register, read/write, dlab = 1
150 #define CYG_DEV_SERIAL_LCR 0x03 // line control register, read/write
151 #define CYG_DEV_SERIAL_MCR 0x04
152 #define CYG_DEV_SERIAL_MCR_A 0x04
153 #define CYG_DEV_SERIAL_MCR_B 0x04
154 #define CYG_DEV_SERIAL_LSR 0x05 // line status register, read
155 #define CYG_DEV_SERIAL_MSR 0x06 // modem status register, read
156 #define CYG_DEV_SERIAL_SCR 0x07 // scratch pad register
158 // The interrupt enable register bits.
159 #define SIO_IER_ERDAI 0x01 // enable received data available irq
160 #define SIO_IER_ETHREI 0x02 // enable THR empty interrupt
161 #define SIO_IER_ELSI 0x04 // enable receiver line status irq
162 #define SIO_IER_EMSI 0x08 // enable modem status interrupt
164 // The interrupt identification register bits.
165 #define SIO_IIR_IP 0x01 // 0 if interrupt pending
166 #define SIO_IIR_ID_MASK 0x0e // mask for interrupt ID bits
170 // The line status register bits.
171 #define SIO_LSR_DR 0x01 // data ready
172 #define SIO_LSR_OE 0x02 // overrun error
173 #define SIO_LSR_PE 0x04 // parity error
174 #define SIO_LSR_FE 0x08 // framing error
175 #define SIO_LSR_BI 0x10 // break interrupt
176 #define SIO_LSR_THRE 0x20 // transmitter holding register empty
177 #define SIO_LSR_TEMT 0x40 // transmitter register empty
178 #define SIO_LSR_ERR 0x80 // any error condition
180 // The modem status register bits.
181 #define SIO_MSR_DCTS 0x01 // delta clear to send
182 #define SIO_MSR_DDSR 0x02 // delta data set ready
183 #define SIO_MSR_TERI 0x04 // trailing edge ring indicator
184 #define SIO_MSR_DDCD 0x08 // delta data carrier detect
185 #define SIO_MSR_CTS 0x10 // clear to send
186 #define SIO_MSR_DSR 0x20 // data set ready
187 #define SIO_MSR_RI 0x40 // ring indicator
188 #define SIO_MSR_DCD 0x80 // data carrier detect
190 // The line control register bits.
191 #define SIO_LCR_WLS0 0x01 // word length select bit 0
192 #define SIO_LCR_WLS1 0x02 // word length select bit 1
193 #define SIO_LCR_STB 0x04 // number of stop bits
194 #define SIO_LCR_PEN 0x08 // parity enable
195 #define SIO_LCR_EPS 0x10 // even parity select
196 #define SIO_LCR_SP 0x20 // stick parity
197 #define SIO_LCR_SB 0x40 // set break
198 #define SIO_LCR_DLAB 0x80 // divisor latch access bit
200 // The FIFO control register
201 #define SIO_FCR_FCR0 0x01 // enable xmit and rcvr fifos
202 #define SIO_FCR_FCR1 0x02 // clear RCVR FIFO
203 #define SIO_FCR_FCR2 0x04 // clear XMIT FIFO
205 //-----------------------------------------------------------------------------
209 cyg_int32 msec_timeout;
213 static channel_data_t channels[] = {
214 #ifdef CYGHWR_HAL_ARM_DUART_UARTA
215 #if CYGHWR_HAL_ARM_DUART_UARTA != 0
216 {(uart_width*)CYG_DEV_SERIAL_BASE_A, 1000, 0},
219 #ifdef CYGHWR_HAL_ARM_DUART_UARTB
220 #if CYGHWR_HAL_ARM_DUART_UARTB != 0
221 {(uart_width*)CYG_DEV_SERIAL_BASE_B, 1000, 0}
226 //-----------------------------------------------------------------------------
228 static void init_duart_channel(channel_data_t* __ch_data)
230 uart_width* base = __ch_data->base;
233 CHECK_BOARD_VERSION_RET_VOID;
236 HAL_WRITE_UINT_UART(base+CYG_DEV_SERIAL_LCR,
237 SIO_LCR_WLS0 | SIO_LCR_WLS1);
239 HAL_READ_UINT_UART(base+CYG_DEV_SERIAL_LCR, lcr);
241 HAL_WRITE_UINT_UART(base+CYG_DEV_SERIAL_LCR, lcr);
242 HAL_WRITE_UINT_UART(base+CYG_DEV_SERIAL_DLL, CYG_DEV_SERIAL_BAUD_LSB);
243 HAL_WRITE_UINT_UART(base+CYG_DEV_SERIAL_DLM, CYG_DEV_SERIAL_BAUD_MSB);
244 lcr &= ~SIO_LCR_DLAB;
245 HAL_WRITE_UINT_UART(base+CYG_DEV_SERIAL_LCR, lcr);
246 HAL_WRITE_UINT_UART(base+CYG_DEV_SERIAL_FCR, 0x07); // Enable & clear FIFO
249 void cyg_hal_plf_duart_putc(void* __ch_data, cyg_uint8 c)
251 uart_width* base = ((channel_data_t*)__ch_data)->base;
254 CHECK_BOARD_VERSION_RET_VOID;
256 CYGARC_HAL_SAVE_GP();
259 HAL_READ_UINT_UART(base+CYG_DEV_SERIAL_LSR, lsr);
260 } while ((lsr & SIO_LSR_THRE) == 0);
262 HAL_WRITE_UINT_UART(base+CYG_DEV_SERIAL_THR, c);
264 // Hang around until the character has been safely sent.
266 HAL_READ_UINT_UART(base+CYG_DEV_SERIAL_LSR, lsr);
267 } while ((lsr & SIO_LSR_THRE) == 0);
269 CYGARC_HAL_RESTORE_GP();
272 static cyg_bool cyg_hal_plf_duart_getc_nonblock(void* __ch_data, cyg_uint8* ch)
274 uart_width* base = ((channel_data_t*)__ch_data)->base;
275 uart_width lsr, ch16;
277 CHECK_BOARD_VERSION(false);
279 HAL_READ_UINT_UART(base+CYG_DEV_SERIAL_LSR, lsr);
280 if ((lsr & SIO_LSR_DR) == 0)
283 HAL_READ_UINT_UART(base+CYG_DEV_SERIAL_RHR, ch16);
285 *ch = (cyg_uint8) (ch16 & 0x00FF);
290 cyg_uint8 cyg_hal_plf_duart_getc(void* __ch_data)
294 CYGARC_HAL_SAVE_GP();
296 while (!cyg_hal_plf_duart_getc_nonblock(__ch_data, &ch));
298 CYGARC_HAL_RESTORE_GP();
302 static void cyg_hal_plf_duart_write(void* __ch_data, const cyg_uint8* __buf,
305 CHECK_BOARD_VERSION_RET_VOID;
307 CYGARC_HAL_SAVE_GP();
310 cyg_hal_plf_duart_putc(__ch_data, *__buf++);
312 CYGARC_HAL_RESTORE_GP();
315 static void cyg_hal_plf_duart_read(void* __ch_data, cyg_uint8* __buf,
318 CHECK_BOARD_VERSION_RET_VOID;
320 CYGARC_HAL_SAVE_GP();
323 *__buf++ = cyg_hal_plf_duart_getc(__ch_data);
325 CYGARC_HAL_RESTORE_GP();
328 cyg_bool cyg_hal_plf_duart_getc_timeout(void* __ch_data, cyg_uint8* ch)
331 channel_data_t* chan = (channel_data_t*)__ch_data;
334 CHECK_BOARD_VERSION(false);
336 CYGARC_HAL_SAVE_GP();
338 delay_count = chan->msec_timeout * 10; // delay in .1 ms steps
340 res = cyg_hal_plf_duart_getc_nonblock(__ch_data, ch);
341 if (res || 0 == delay_count--)
344 CYGACC_CALL_IF_DELAY_US(100);
347 CYGARC_HAL_RESTORE_GP();
351 static int cyg_hal_plf_duart_control(void *__ch_data,
352 __comm_control_cmd_t __func, ...)
354 static int irq_state = 0;
355 channel_data_t* chan = (channel_data_t*)__ch_data;
359 CHECK_BOARD_VERSION(-1);
361 CYGARC_HAL_SAVE_GP();
364 case __COMMCTL_IRQ_ENABLE:
365 HAL_INTERRUPT_UNMASK(chan->isr_vector);
366 HAL_INTERRUPT_SET_LEVEL(chan->isr_vector, 1);
367 HAL_READ_UINT_UART(chan->base+CYG_DEV_SERIAL_IER, ier);
368 ier |= SIO_IER_ERDAI;
369 HAL_WRITE_UINT_UART(chan->base+CYG_DEV_SERIAL_IER, ier);
372 case __COMMCTL_IRQ_DISABLE:
375 HAL_INTERRUPT_MASK(chan->isr_vector);
376 HAL_READ_UINT_UART(chan->base+CYG_DEV_SERIAL_IER, ier);
377 ier &= ~SIO_IER_ERDAI;
378 HAL_WRITE_UINT_UART(chan->base+CYG_DEV_SERIAL_IER, ier);
380 case __COMMCTL_DBG_ISR_VECTOR:
381 ret = chan->isr_vector;
383 case __COMMCTL_SET_TIMEOUT:
387 va_start(ap, __func);
389 ret = chan->msec_timeout;
390 chan->msec_timeout = va_arg(ap, cyg_uint32);
398 CYGARC_HAL_RESTORE_GP();
402 static int cyg_hal_plf_duart_isr(void *__ch_data, int* __ctrlc,
403 CYG_ADDRWORD __vector, CYG_ADDRWORD __data)
405 channel_data_t* chan = (channel_data_t*)__ch_data;
408 CYGARC_HAL_SAVE_GP();
410 HAL_READ_UINT_UART(chan->base+CYG_DEV_SERIAL_IIR, _iir);
411 _iir &= SIO_IIR_ID_MASK;
414 if ( ISR_Rx == _iir ) {
417 HAL_READ_UINT_UART(chan->base+CYG_DEV_SERIAL_LSR, lsr);
418 if (lsr & SIO_LSR_DR) {
420 HAL_READ_UINT_UART(chan->base+CYG_DEV_SERIAL_RHR, c);
422 c8 = (cyg_uint8) (c & 0x00FF);
424 if (cyg_hal_is_break( &c8 , 1 ))
428 // Acknowledge the interrupt
429 HAL_INTERRUPT_ACKNOWLEDGE(chan->isr_vector);
430 res = CYG_ISR_HANDLED;
433 CYGARC_HAL_RESTORE_GP();
437 static void cyg_hal_plf_duart_init(void)
439 hal_virtual_comm_table_t* comm;
440 int cur = CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);
443 CHECK_BOARD_VERSION_RET_VOID;
446 #define NUMOF(x) (sizeof(x)/sizeof(x[0]))
447 for (i = 0; i < NUMOF(channels); i++) {
448 HAL_INTERRUPT_MASK(channels[i].isr_vector);
449 init_duart_channel(&channels[i]);
450 CYGACC_CALL_IF_SET_CONSOLE_COMM(i);
451 comm = CYGACC_CALL_IF_CONSOLE_PROCS();
452 CYGACC_COMM_IF_CH_DATA_SET(*comm, &channels[i]);
453 CYGACC_COMM_IF_WRITE_SET(*comm, cyg_hal_plf_duart_write);
454 CYGACC_COMM_IF_READ_SET(*comm, cyg_hal_plf_duart_read);
455 CYGACC_COMM_IF_PUTC_SET(*comm, cyg_hal_plf_duart_putc);
456 CYGACC_COMM_IF_GETC_SET(*comm, cyg_hal_plf_duart_getc);
457 CYGACC_COMM_IF_CONTROL_SET(*comm, cyg_hal_plf_duart_control);
458 CYGACC_COMM_IF_DBG_ISR_SET(*comm, cyg_hal_plf_duart_isr);
459 CYGACC_COMM_IF_GETC_TIMEOUT_SET(*comm, cyg_hal_plf_duart_getc_timeout);
462 // Restore original console
463 CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);
466 //=============================================================================
467 // Compatibility with older stubs
468 //=============================================================================
470 //=============================================================================
471 // Compatibility with older stubs
472 //=============================================================================
474 #ifndef CYGSEM_HAL_VIRTUAL_VECTOR_DIAG
476 #include <cyg/hal/hal_stub.h> // cyg_hal_gdb_interrupt
478 #if (CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL == 0)
479 #define __BASE CMA101_DUARTA
480 #define _INT CYGNUM_HAL_INTERRUPT_SERIAL_A
481 #elif (CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL == 1)
482 #define __BASE CMA101_DUARTB
483 #define _INT CYGNUM_HAL_INTERRUPT_SERIAL_B
488 #ifdef CYGSEM_HAL_ROM_MONITOR
489 #define CYG_HAL_STARTUP_ROM
490 #define CYG_HAL_STARTUP_ROMRAM
491 #undef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
494 #if (defined(CYG_HAL_STARTUP_ROM) || defined(CYG_HAL_STARTUP_ROMRAM)) && !defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS)
495 #define HAL_DIAG_USES_HARDWARE
496 #elif !defined(CYGDBG_HAL_DIAG_TO_DEBUG_CHAN)
497 #define HAL_DIAG_USES_HARDWARE
498 #elif CYGNUM_HAL_VIRTUAL_VECTOR_CONSOLE_CHANNEL != CYGNUM_HAL_VIRTUAL_VECTOR_DEBUG_CHANNEL
499 #define HAL_DIAG_USES_HARDWARE
502 static channel_data_t channel = {
503 (uart_width*) _BASE, 0, 0
506 #ifdef HAL_DIAG_USES_HARDWARE
508 void hal_diag_init(void)
511 char *msg = "\n\rARM eCos\n\r";
516 init_duart_channel(&channel);
518 while (*msg) hal_diag_write_char(*msg++);
522 #if defined(CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS)
523 #define DIAG_BUFSIZE 32
525 #define DIAG_BUFSIZE 2048
527 static char diag_buffer[DIAG_BUFSIZE];
528 static int diag_bp = 0;
531 void hal_diag_write_char(char c)
537 cyg_hal_plf_duart_putc(&channel, c)
540 diag_buffer[diag_bp++] = c;
541 if (diag_bp == DIAG_BUFSIZE) {
548 void hal_diag_read_char(char *c)
550 *c = cyg_hal_plf_duart_getc(&channel);
553 #else // HAL_DIAG relies on GDB
555 // Initialize diag port - assume GDB channel is already set up
556 void hal_diag_init(void)
558 if (0) init_duart_channel(&channel); // avoid warning
561 // Actually send character down the wire
562 static void hal_diag_write_char_serial(char c)
564 cyg_hal_plf_duart_putc(&channel, c);
567 static bool hal_diag_read_serial(char *c)
569 long timeout = 1000000000; // A long time...
571 while (!cyg_hal_plf_duart_getc_nonblock(&channel, c))
572 if (0 == --timeout) return false;
577 void hal_diag_read_char(char *c)
579 while (!hal_diag_read_serial(c)) ;
582 void hal_diag_write_char(char c)
584 static char line[100];
587 // No need to send CRs
588 if (c == '\r') return;
592 if (c == '\n' || pos == sizeof(line)) {
593 CYG_INTERRUPT_STATE old;
595 // Disable interrupts. This prevents GDB trying to interrupt us
596 // while we are in the middle of sending a packet. The serial
597 // receive interrupt will be seen when we re-enable interrupts
600 #ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
601 CYG_HAL_GDB_ENTER_CRITICAL_IO_REGION(old);
603 HAL_DISABLE_INTERRUPTS(old);
607 static char hex[] = "0123456789ABCDEF";
612 hal_diag_write_char_serial('$');
613 hal_diag_write_char_serial('O');
615 for (i = 0; i < pos; i++) {
617 char h = hex[(ch>>4)&0xF];
618 char l = hex[ch&0xF];
619 hal_diag_write_char_serial(h);
620 hal_diag_write_char_serial(l);
624 hal_diag_write_char_serial('#');
625 hal_diag_write_char_serial(hex[(csum>>4)&0xF]);
626 hal_diag_write_char_serial(hex[csum&0xF]);
628 // Wait for the ACK character '+' from GDB here and handle
629 // receiving a ^C instead. This is the reason for this clause
631 if (!hal_diag_read_serial(&c1))
632 continue; // No response - try sending packet again
635 break; // a good acknowledge
637 #ifdef CYGDBG_HAL_DEBUG_GDB_BREAK_SUPPORT
638 cyg_drv_interrupt_acknowledge(CYG_DEV_SERIAL_INT);
640 // Ctrl-C: breakpoint.
641 cyg_hal_gdb_interrupt (__builtin_return_address(0));
645 // otherwise, loop round again
650 // And re-enable interrupts
651 #ifdef CYGDBG_HAL_DEBUG_GDB_INCLUDE_STUBS
652 CYG_HAL_GDB_LEAVE_CRITICAL_IO_REGION(old);
654 HAL_RESTORE_INTERRUPTS(old);
663 #endif // CYGSEM_HAL_VIRTUAL_VECTOR_DIAG
665 /*---------------------------------------------------------------------------*/