ns8250.c 8.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009,2010  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/serial.h>
#include <grub/ns8250.h>
#include <grub/types.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/cpu/io.h>
#include <grub/mm.h>
26
#include <grub/time.h>
27
#include <grub/i18n.h>
28 29

#ifdef GRUB_MACHINE_PCBIOS
30
#include <grub/machine/memory.h>
31 32 33 34 35 36 37 38
static const unsigned short *serial_hw_io_addr = (const unsigned short *) GRUB_MEMORY_MACHINE_BIOS_DATA_AREA_ADDR;
#define GRUB_SERIAL_PORT_NUM 4
#else
#include <grub/machine/serial.h>
static const grub_port_t serial_hw_io_addr[] = GRUB_MACHINE_SERIAL_PORTS;
#define GRUB_SERIAL_PORT_NUM (ARRAY_SIZE(serial_hw_io_addr))
#endif

39 40
static int dead_ports = 0;

41 42 43 44 45 46 47
#ifdef GRUB_MACHINE_MIPS_LOONGSON
#define DEFAULT_BASE_CLOCK (2 * 115200)
#else
#define DEFAULT_BASE_CLOCK 115200
#endif


48 49
/* Convert speed to divisor.  */
static unsigned short
50
serial_get_divisor (const struct grub_serial_port *port __attribute__ ((unused)),
51
		    const struct grub_serial_config *config)
52
{
53 54 55
  grub_uint32_t base_clock;
  grub_uint32_t divisor;
  grub_uint32_t actual_speed, error;
56

57
  base_clock = config->base_clock ? (config->base_clock >> 4) : DEFAULT_BASE_CLOCK;
58

59
  divisor = (base_clock + (config->speed / 2)) / config->speed;
60 61
  if (config->speed == 0)
    return 0;
62 63 64 65 66 67 68 69
  if (divisor > 0xffff || divisor == 0)
    return 0;
  actual_speed = base_clock / divisor;
  error = actual_speed > config->speed ? (actual_speed - config->speed)
    : (config->speed - actual_speed);
  if (error > (config->speed / 30 + 1))
    return 0;
  return divisor;
70 71
}

72 73
static void
do_real_config (struct grub_serial_port *port)
74
{
75
  int divisor;
76
  unsigned char status = 0;
77 78
  grub_uint64_t endtime;

79 80 81 82 83 84 85 86 87
  const unsigned char parities[] = {
    [GRUB_SERIAL_PARITY_NONE] = UART_NO_PARITY,
    [GRUB_SERIAL_PARITY_ODD] = UART_ODD_PARITY,
    [GRUB_SERIAL_PARITY_EVEN] = UART_EVEN_PARITY
  };
  const unsigned char stop_bits[] = {
    [GRUB_SERIAL_STOP_BITS_1] = UART_1_STOP_BIT,
    [GRUB_SERIAL_STOP_BITS_2] = UART_2_STOP_BITS,
  };
88

89 90
  if (port->configured)
    return;
91

92 93
  port->broken = 0;

94
  divisor = serial_get_divisor (port, &port->config);
95 96 97 98 99 100 101 102 103 104 105 106

  /* Turn off the interrupt.  */
  grub_outb (0, port->port + UART_IER);

  /* Set DLAB.  */
  grub_outb (UART_DLAB, port->port + UART_LCR);

  /* Set the baud rate.  */
  grub_outb (divisor & 0xFF, port->port + UART_DLL);
  grub_outb (divisor >> 8, port->port + UART_DLH);

  /* Set the line status.  */
107
  status |= (parities[port->config.parity]
108 109
	     | (port->config.word_len - 5)
	     | stop_bits[port->config.stop_bits]);
110 111
  grub_outb (status, port->port + UART_LCR);

112 113 114 115
  if (port->config.rtscts)
    {
      /* Enable the FIFO.  */
      grub_outb (UART_ENABLE_FIFO_TRIGGER1, port->port + UART_FCR);
116

117 118 119 120 121 122 123
      /* Turn on DTR and RTS.  */
      grub_outb (UART_ENABLE_DTRRTS, port->port + UART_MCR);
    }
  else
    {
      /* Enable the FIFO.  */
      grub_outb (UART_ENABLE_FIFO_TRIGGER14, port->port + UART_FCR);
124

125 126 127
      /* Turn on DTR, RTS, and OUT2.  */
      grub_outb (UART_ENABLE_DTRRTS | UART_ENABLE_OUT2, port->port + UART_MCR);
    }
128 129

  /* Drain the input buffer.  */
130
  endtime = grub_get_time_ms () + 1000;
131
  while (grub_inb (port->port + UART_LSR) & UART_DATA_READY)
132 133 134 135 136 137 138 139
    {
      grub_inb (port->port + UART_RX);
      if (grub_get_time_ms () > endtime)
	{
	  port->broken = 1;
	  break;
	}
    }
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

  port->configured = 1;
}

/* Fetch a key.  */
static int
serial_hw_fetch (struct grub_serial_port *port)
{
  do_real_config (port);
  if (grub_inb (port->port + UART_LSR) & UART_DATA_READY)
    return grub_inb (port->port + UART_RX);

  return -1;
}

/* Put a character.  */
static void
serial_hw_put (struct grub_serial_port *port, const int c)
{
159
  grub_uint64_t endtime;
160 161 162

  do_real_config (port);

163 164 165 166 167 168
  if (port->broken > 5)
    endtime = grub_get_time_ms ();
  else if (port->broken > 1)
    endtime = grub_get_time_ms () + 50;
  else
    endtime = grub_get_time_ms () + 200;
169 170 171
  /* Wait until the transmitter holding register is empty.  */
  while ((grub_inb (port->port + UART_LSR) & UART_EMPTY_TRANSMITTER) == 0)
    {
172 173 174 175 176 177
      if (grub_get_time_ms () > endtime)
	{
	  port->broken++;
	  /* There is something wrong. But what can I do?  */
	  return;
	}
178 179
    }

180 181 182
  if (port->broken)
    port->broken--;

183 184 185 186 187 188 189 190 191 192 193 194
  grub_outb (c, port->port + UART_TX);
}

/* Initialize a serial device. PORT is the port number for a serial device.
   SPEED is a DTE-DTE speed which must be one of these: 2400, 4800, 9600,
   19200, 38400, 57600 and 115200. WORD_LEN is the word length to be used
   for the device. Likewise, PARITY is the type of the parity and
   STOP_BIT_LEN is the length of the stop bit. The possible values for
   WORD_LEN, PARITY and STOP_BIT_LEN are defined in the header file as
   macros.  */
static grub_err_t
serial_hw_configure (struct grub_serial_port *port,
195
		     struct grub_serial_config *config)
196 197 198
{
  unsigned short divisor;

199
  divisor = serial_get_divisor (port, config);
200
  if (divisor == 0)
201 202
    return grub_error (GRUB_ERR_BAD_ARGUMENT,
		       N_("unsupported serial port speed"));
203

204 205 206
  if (config->parity != GRUB_SERIAL_PARITY_NONE
      && config->parity != GRUB_SERIAL_PARITY_ODD
      && config->parity != GRUB_SERIAL_PARITY_EVEN)
207 208
    return grub_error (GRUB_ERR_BAD_ARGUMENT,
		       N_("unsupported serial port parity"));
209 210 211

  if (config->stop_bits != GRUB_SERIAL_STOP_BITS_1
      && config->stop_bits != GRUB_SERIAL_STOP_BITS_2)
212 213
    return grub_error (GRUB_ERR_BAD_ARGUMENT,
		       N_("unsupported serial port stop bits number"));
214

215
  if (config->word_len < 5 || config->word_len > 8)
216 217
    return grub_error (GRUB_ERR_BAD_ARGUMENT,
		       N_("unsupported serial port word length"));
218

219
  port->config = *config;
220
  port->configured = 0;
221 222 223 224 225 226

  /*  FIXME: should check if the serial terminal was found.  */

  return GRUB_ERR_NONE;
}

227
struct grub_serial_driver grub_ns8250_driver =
228 229 230 231 232 233 234 235 236 237 238 239
  {
    .configure = serial_hw_configure,
    .fetch = serial_hw_fetch,
    .put = serial_hw_put
  };

static char com_names[GRUB_SERIAL_PORT_NUM][20];
static struct grub_serial_port com_ports[GRUB_SERIAL_PORT_NUM];

void
grub_ns8250_init (void)
{
240
  unsigned i;
241
  for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
242
    if (serial_hw_io_addr[i])
243
      {
244
	grub_err_t err;
245 246 247 248 249 250 251 252 253 254 255 256 257 258

	grub_outb (0x5a, serial_hw_io_addr[i] + UART_SR);
	if (grub_inb (serial_hw_io_addr[i] + UART_SR) != 0x5a)
	  {
	    dead_ports |= (1 << i);
	    continue;
	  }
	grub_outb (0xa5, serial_hw_io_addr[i] + UART_SR);
	if (grub_inb (serial_hw_io_addr[i] + UART_SR) != 0xa5)
	  {
	    dead_ports |= (1 << i);
	    continue;
	  }

259 260 261 262
	grub_snprintf (com_names[i], sizeof (com_names[i]), "com%d", i);
	com_ports[i].name = com_names[i];
	com_ports[i].driver = &grub_ns8250_driver;
	com_ports[i].port = serial_hw_io_addr[i];
263 264 265 266
	err = grub_serial_config_defaults (&com_ports[i]);
	if (err)
	  grub_print_error ();

267 268 269 270
	grub_serial_register (&com_ports[i]);
      }
}

271 272 273 274
/* Return the port number for the UNITth serial device.  */
grub_port_t
grub_ns8250_hw_get_port (const unsigned int unit)
{
275 276
  if (unit < GRUB_SERIAL_PORT_NUM
      && !(dead_ports & (1 << unit)))
277 278 279 280 281
    return serial_hw_io_addr[unit];
  else
    return 0;
}

282 283 284 285
char *
grub_serial_ns8250_add_port (grub_port_t port)
{
  struct grub_serial_port *p;
286
  unsigned i;
287 288
  for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
    if (com_ports[i].port == port)
289 290 291 292 293 294 295 296 297 298 299 300 301 302
      {
	if (dead_ports & (1 << i))
	  return NULL;
	return com_names[i];
      }

  grub_outb (0x5a, port + UART_SR);
  if (grub_inb (port + UART_SR) != 0x5a)
    return NULL;

  grub_outb (0xa5, port + UART_SR);
  if (grub_inb (port + UART_SR) != 0xa5)
    return NULL;

303 304 305 306 307 308 309 310 311 312
  p = grub_malloc (sizeof (*p));
  if (!p)
    return NULL;
  p->name = grub_xasprintf ("port%lx", (unsigned long) port);
  if (!p->name)
    {
      grub_free (p);
      return NULL;
    }
  p->driver = &grub_ns8250_driver;
313
  grub_serial_config_defaults (p);
314 315 316 317 318
  p->port = port;
  grub_serial_register (p);  

  return p->name;
}