serial.c File Reference

#include "stddef.h"
#include <gpxe/init.h>
#include <gpxe/io.h>
#include <unistd.h>
#include <gpxe/serial.h>
#include "config/serial.h"

Go to the source code of this file.

Defines

#define UART_BASE   ( COMCONSOLE )
#define UART_BAUD   ( COMSPEED )
#define COMBRD   (115200/UART_BAUD)
#define UART_LCS
#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
#define uart_readb(addr)   inb((addr))
#define uart_writeb(val, addr)   outb((val),(addr))

Functions

 FILE_LICENCE (GPL2_OR_LATER)
void serial_putc (int ch)
int serial_getc (void)
int serial_ischar (void)
static void serial_init (void)
static void serial_fini (int flags __unused)
struct init_fn serial_init_fn __init_fn (INIT_SERIAL)
 Serial driver initialisation function.
struct startup_fn serial_startup_fn __startup_fn (STARTUP_EARLY)
 Serial driver startup function.


Define Documentation

#define UART_BASE   ( COMCONSOLE )

Definition at line 46 of file serial.c.

Referenced by serial_fini(), serial_getc(), serial_init(), serial_ischar(), and serial_putc().

#define UART_BAUD   ( COMSPEED )

Definition at line 49 of file serial.c.

#define COMBRD   (115200/UART_BAUD)

Definition at line 55 of file serial.c.

Referenced by serial_init().

#define UART_LCS

Value:

( ( ( (COMDATA) - 5 )   << 0 ) | \
                   ( ( (COMPARITY) )    << 3 ) | \
                   ( ( (COMSTOP) - 1 )  << 2 ) )

Definition at line 58 of file serial.c.

Referenced by serial_init().

#define UART_RBR   0x00

Definition at line 63 of file serial.c.

Referenced by serial_getc(), and serial_init().

#define UART_TBR   0x00

Definition at line 64 of file serial.c.

Referenced by serial_putc().

#define UART_IER   0x01

Definition at line 67 of file serial.c.

Referenced by serial_init().

#define UART_IIR   0x02

Definition at line 68 of file serial.c.

#define UART_FCR   0x02

Definition at line 69 of file serial.c.

Referenced by serial_init().

#define UART_LCR   0x03

Definition at line 70 of file serial.c.

Referenced by serial_init().

#define UART_MCR   0x04

Definition at line 71 of file serial.c.

Referenced by serial_init().

#define UART_DLL   0x00

Definition at line 72 of file serial.c.

Referenced by serial_init().

#define UART_DLM   0x01

Definition at line 73 of file serial.c.

Referenced by serial_init().

#define UART_LSR   0x05

Definition at line 76 of file serial.c.

Referenced by serial_fini(), serial_getc(), serial_init(), serial_ischar(), and serial_putc().

#define UART_LSR_TEMPT   0x40

Definition at line 77 of file serial.c.

Referenced by serial_fini().

#define UART_LSR_THRE   0x20

Definition at line 78 of file serial.c.

Referenced by serial_putc().

#define UART_LSR_BI   0x10

Definition at line 79 of file serial.c.

#define UART_LSR_FE   0x08

Definition at line 80 of file serial.c.

#define UART_LSR_PE   0x04

Definition at line 81 of file serial.c.

#define UART_LSR_OE   0x02

Definition at line 82 of file serial.c.

#define UART_LSR_DR   0x01

Definition at line 83 of file serial.c.

Referenced by serial_init().

#define UART_MSR   0x06

Definition at line 85 of file serial.c.

#define UART_SCR   0x07

Definition at line 86 of file serial.c.

#define uart_readb ( addr   )     inb((addr))

Definition at line 92 of file serial.c.

Referenced by serial_fini(), serial_getc(), serial_init(), serial_ischar(), and serial_putc().

#define uart_writeb ( val,
addr   )     outb((val),(addr))

Definition at line 93 of file serial.c.

Referenced by serial_init(), and serial_putc().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

void serial_putc ( int  ch  ) 

Definition at line 100 of file serial.c.

References mdelay(), UART_BASE, UART_LSR, UART_LSR_THRE, uart_readb, UART_TBR, and uart_writeb.

Referenced by gdbserial_send(), and int21().

00100                             {
00101         int i;
00102         int status;
00103         i = 1000; /* timeout */
00104         while(--i > 0) {
00105                 status = uart_readb(UART_BASE + UART_LSR);
00106                 if (status & UART_LSR_THRE) { 
00107                         /* TX buffer emtpy */
00108                         uart_writeb(ch, UART_BASE + UART_TBR);
00109                         break;
00110                 }
00111                 mdelay(2);
00112         }
00113 }

int serial_getc ( void   ) 

Definition at line 119 of file serial.c.

References UART_BASE, UART_LSR, UART_RBR, and uart_readb.

Referenced by gdbserial_recv().

00119                          {
00120         int status;
00121         int ch;
00122         do {
00123                 status = uart_readb(UART_BASE + UART_LSR);
00124         } while((status & 1) == 0);
00125         ch = uart_readb(UART_BASE + UART_RBR);  /* fetch (first) character */
00126         ch &= 0x7f;                             /* remove any parity bits we get */
00127         if (ch == 0x7f) {                       /* Make DEL... look like BS */
00128                 ch = 0x08;
00129         }
00130         return ch;
00131 }

int serial_ischar ( void   ) 

Definition at line 138 of file serial.c.

References UART_BASE, UART_LSR, and uart_readb.

00138                            {
00139         int status;
00140         status = uart_readb(UART_BASE + UART_LSR);      /* line status reg; */
00141         return status & 1;              /* rx char available */
00142 }

static void serial_init ( void   )  [static]

Definition at line 148 of file serial.c.

References COMBRD, DBG, UART_BASE, UART_DLL, UART_DLM, UART_FCR, UART_IER, UART_LCR, UART_LCS, UART_LSR, UART_LSR_DR, UART_MCR, UART_RBR, uart_readb, and uart_writeb.

00148                                  {
00149         int status;
00150         int divisor, lcs;
00151 
00152         DBG ( "Serial port %#x initialising\n", UART_BASE );
00153 
00154         divisor = COMBRD;
00155         lcs = UART_LCS;
00156 
00157 
00158 #ifdef COMPRESERVE
00159         lcs = uart_readb(UART_BASE + UART_LCR) & 0x7f;
00160         uart_writeb(0x80 | lcs, UART_BASE + UART_LCR);
00161         divisor = (uart_readb(UART_BASE + UART_DLM) << 8) | uart_readb(UART_BASE + UART_DLL);
00162         uart_writeb(lcs, UART_BASE + UART_LCR);
00163 #endif
00164 
00165         /* Set Baud Rate Divisor to COMSPEED, and test to see if the
00166          * serial port appears to be present.
00167          */
00168         uart_writeb(0x80 | lcs, UART_BASE + UART_LCR);
00169         uart_writeb(0xaa, UART_BASE + UART_DLL);
00170         if (uart_readb(UART_BASE + UART_DLL) != 0xaa) {
00171                 DBG ( "Serial port %#x UART_DLL failed\n", UART_BASE );
00172                 goto out;
00173         }
00174         uart_writeb(0x55, UART_BASE + UART_DLL);
00175         if (uart_readb(UART_BASE + UART_DLL) != 0x55) {
00176                 DBG ( "Serial port %#x UART_DLL failed\n", UART_BASE );
00177                 goto out;
00178         }
00179         uart_writeb(divisor & 0xff, UART_BASE + UART_DLL);
00180         if (uart_readb(UART_BASE + UART_DLL) != (divisor & 0xff)) {
00181                 DBG ( "Serial port %#x UART_DLL failed\n", UART_BASE );
00182                 goto out;
00183         }
00184         uart_writeb(0xaa, UART_BASE + UART_DLM);
00185         if (uart_readb(UART_BASE + UART_DLM) != 0xaa) {
00186                 DBG ( "Serial port %#x UART_DLM failed\n", UART_BASE );
00187                 goto out;
00188         }
00189         uart_writeb(0x55, UART_BASE + UART_DLM);
00190         if (uart_readb(UART_BASE + UART_DLM) != 0x55) {
00191                 DBG ( "Serial port %#x UART_DLM failed\n", UART_BASE );
00192                 goto out;
00193         }
00194         uart_writeb((divisor >> 8) & 0xff, UART_BASE + UART_DLM);
00195         if (uart_readb(UART_BASE + UART_DLM) != ((divisor >> 8) & 0xff)) {
00196                 DBG ( "Serial port %#x UART_DLM failed\n", UART_BASE );
00197                 goto out;
00198         }
00199         uart_writeb(lcs, UART_BASE + UART_LCR);
00200         
00201         /* disable interrupts */
00202         uart_writeb(0x0, UART_BASE + UART_IER);
00203 
00204         /* disable fifo's */
00205         uart_writeb(0x00, UART_BASE + UART_FCR);
00206 
00207         /* Set clear to send, so flow control works... */
00208         uart_writeb((1<<1), UART_BASE + UART_MCR);
00209 
00210 
00211         /* Flush the input buffer. */
00212         do {
00213                 /* rx buffer reg
00214                  * throw away (unconditionally the first time)
00215                  */
00216                 (void) uart_readb(UART_BASE + UART_RBR);
00217                 /* line status reg */
00218                 status = uart_readb(UART_BASE + UART_LSR);
00219         } while(status & UART_LSR_DR);
00220  out:
00221         return;
00222 }

static void serial_fini ( int flags  __unused  )  [static]

Definition at line 229 of file serial.c.

References UART_BASE, UART_LSR, UART_LSR_TEMPT, and uart_readb.

00229                                                {
00230         int i, status;
00231         /* Flush the output buffer to avoid dropping characters,
00232          * if we are reinitializing the serial port.
00233          */
00234         i = 10000; /* timeout */
00235         do {
00236                 status = uart_readb(UART_BASE + UART_LSR);
00237         } while((--i > 0) && !(status & UART_LSR_TEMPT));
00238         /* Don't mark it as disabled; it's still usable */
00239 }

struct init_fn serial_init_fn __init_fn ( INIT_SERIAL   )  [read]

Serial driver initialisation function.

Initialise serial port early on so that it is available to capture early debug messages.

struct startup_fn serial_startup_fn __startup_fn ( STARTUP_EARLY   )  [read]

Serial driver startup function.


Generated on Tue Apr 6 20:01:14 2010 for gPXE by  doxygen 1.5.7.1