123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
-
-
- #include "stddef.h"
- #include <gpxe/init.h>
- #include <gpxe/io.h>
- #include <unistd.h>
- #include <gpxe/serial.h>
- #include "config/serial.h"
-
-
-
- #ifndef COMCONSOLE
- #define COMCONSOLE 0x3f8
- #endif
-
- #ifndef COMSPEED
- #define COMSPEED 9600
- #endif
-
- #ifndef COMDATA
- #define COMDATA 8
- #endif
-
- #ifndef COMPARITY
- #define COMPARITY 0
- #endif
-
- #ifndef COMSTOP
- #define COMSTOP 1
- #endif
-
- #undef UART_BASE
- #define UART_BASE ( COMCONSOLE )
-
- #undef UART_BAUD
- #define UART_BAUD ( COMSPEED )
-
- #if ((115200%UART_BAUD) != 0)
- #error Bad ttys0 baud rate
- #endif
-
- #define COMBRD (115200/UART_BAUD)
-
-
- #define UART_LCS ( ( ( (COMDATA) - 5 ) << 0 ) | \
- ( ( (COMPARITY) ) << 3 ) | \
- ( ( (COMSTOP) - 1 ) << 2 ) )
-
-
- #define UART_RBR 0x00
- #define UART_TBR 0x00
-
-
- #define UART_IER 0x01
- #define UART_IIR 0x02
- #define UART_FCR 0x02
- #define UART_LCR 0x03
- #define UART_MCR 0x04
- #define UART_DLL 0x00
- #define UART_DLM 0x01
-
-
- #define UART_LSR 0x05
- #define UART_LSR_TEMPT 0x40
- #define UART_LSR_THRE 0x20
- #define UART_LSR_BI 0x10
- #define UART_LSR_FE 0x08
- #define UART_LSR_PE 0x04
- #define UART_LSR_OE 0x02
- #define UART_LSR_DR 0x01
-
- #define UART_MSR 0x06
- #define UART_SCR 0x07
-
- #if defined(UART_MEM)
- #define uart_readb(addr) readb((addr))
- #define uart_writeb(val,addr) writeb((val),(addr))
- #else
- #define uart_readb(addr) inb((addr))
- #define uart_writeb(val,addr) outb((val),(addr))
- #endif
-
-
- void serial_putc ( int ch ) {
- int i;
- int status;
- i = 1000;
- while(--i > 0) {
- status = uart_readb(UART_BASE + UART_LSR);
- if (status & UART_LSR_THRE) {
-
- uart_writeb(ch, UART_BASE + UART_TBR);
- break;
- }
- mdelay(2);
- }
- }
-
-
- int serial_getc ( void ) {
- int status;
- int ch;
- do {
- status = uart_readb(UART_BASE + UART_LSR);
- } while((status & 1) == 0);
- ch = uart_readb(UART_BASE + UART_RBR);
- ch &= 0x7f;
- if (ch == 0x7f) {
- ch = 0x08;
- }
- return ch;
- }
-
-
- int serial_ischar ( void ) {
- int status;
- status = uart_readb(UART_BASE + UART_LSR);
- return status & 1;
- }
-
-
- static void serial_init ( void ) {
- int status;
- int divisor, lcs;
-
- DBG ( "Serial port %#x initialising\n", UART_BASE );
-
- divisor = COMBRD;
- lcs = UART_LCS;
-
-
- #ifdef COMPRESERVE
- lcs = uart_readb(UART_BASE + UART_LCR) & 0x7f;
- uart_writeb(0x80 | lcs, UART_BASE + UART_LCR);
- divisor = (uart_readb(UART_BASE + UART_DLM) << 8) | uart_readb(UART_BASE + UART_DLL);
- uart_writeb(lcs, UART_BASE + UART_LCR);
- #endif
-
-
-
- uart_writeb(0x80 | lcs, UART_BASE + UART_LCR);
- uart_writeb(0xaa, UART_BASE + UART_DLL);
- if (uart_readb(UART_BASE + UART_DLL) != 0xaa) {
- DBG ( "Serial port %#x UART_DLL failed\n", UART_BASE );
- goto out;
- }
- uart_writeb(0x55, UART_BASE + UART_DLL);
- if (uart_readb(UART_BASE + UART_DLL) != 0x55) {
- DBG ( "Serial port %#x UART_DLL failed\n", UART_BASE );
- goto out;
- }
- uart_writeb(divisor & 0xff, UART_BASE + UART_DLL);
- if (uart_readb(UART_BASE + UART_DLL) != (divisor & 0xff)) {
- DBG ( "Serial port %#x UART_DLL failed\n", UART_BASE );
- goto out;
- }
- uart_writeb(0xaa, UART_BASE + UART_DLM);
- if (uart_readb(UART_BASE + UART_DLM) != 0xaa) {
- DBG ( "Serial port %#x UART_DLM failed\n", UART_BASE );
- goto out;
- }
- uart_writeb(0x55, UART_BASE + UART_DLM);
- if (uart_readb(UART_BASE + UART_DLM) != 0x55) {
- DBG ( "Serial port %#x UART_DLM failed\n", UART_BASE );
- goto out;
- }
- uart_writeb((divisor >> 8) & 0xff, UART_BASE + UART_DLM);
- if (uart_readb(UART_BASE + UART_DLM) != ((divisor >> 8) & 0xff)) {
- DBG ( "Serial port %#x UART_DLM failed\n", UART_BASE );
- goto out;
- }
- uart_writeb(lcs, UART_BASE + UART_LCR);
-
-
- uart_writeb(0x0, UART_BASE + UART_IER);
-
-
- uart_writeb(0x00, UART_BASE + UART_FCR);
-
-
- uart_writeb((1<<1), UART_BASE + UART_MCR);
-
-
-
- do {
-
-
- (void) uart_readb(UART_BASE + UART_RBR);
-
- status = uart_readb(UART_BASE + UART_LSR);
- } while(status & UART_LSR_DR);
- out:
- return;
- }
-
-
- static void serial_fini ( int flags __unused ) {
- int i, status;
-
-
- i = 10000;
- do {
- status = uart_readb(UART_BASE + UART_LSR);
- } while((--i > 0) && !(status & UART_LSR_TEMPT));
-
- }
-
-
- struct init_fn serial_init_fn __init_fn ( INIT_SERIAL ) = {
- .initialise = serial_init,
- };
-
-
- struct startup_fn serial_startup_fn __startup_fn ( STARTUP_EARLY ) = {
- .shutdown = serial_fini,
- };
|