ns8250.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/serial.h>
  19. #include <grub/ns8250.h>
  20. #include <grub/types.h>
  21. #include <grub/dl.h>
  22. #include <grub/misc.h>
  23. #include <grub/cpu/io.h>
  24. #include <grub/mm.h>
  25. #include <grub/time.h>
  26. #include <grub/i18n.h>
  27. #ifdef GRUB_MACHINE_PCBIOS
  28. #include <grub/machine/memory.h>
  29. static const unsigned short *serial_hw_io_addr = (const unsigned short *) GRUB_MEMORY_MACHINE_BIOS_DATA_AREA_ADDR;
  30. #define GRUB_SERIAL_PORT_NUM 4
  31. #else
  32. #include <grub/machine/serial.h>
  33. static const grub_port_t serial_hw_io_addr[] = GRUB_MACHINE_SERIAL_PORTS;
  34. #define GRUB_SERIAL_PORT_NUM (ARRAY_SIZE(serial_hw_io_addr))
  35. #endif
  36. static int dead_ports = 0;
  37. #ifdef GRUB_MACHINE_MIPS_LOONGSON
  38. #define DEFAULT_BASE_CLOCK (2 * 115200)
  39. #else
  40. #define DEFAULT_BASE_CLOCK 115200
  41. #endif
  42. /* Convert speed to divisor. */
  43. static unsigned short
  44. serial_get_divisor (const struct grub_serial_port *port __attribute__ ((unused)),
  45. const struct grub_serial_config *config)
  46. {
  47. grub_uint32_t base_clock;
  48. grub_uint32_t divisor;
  49. grub_uint32_t actual_speed, error;
  50. base_clock = config->base_clock ? (config->base_clock >> 4) : DEFAULT_BASE_CLOCK;
  51. divisor = (base_clock + (config->speed / 2)) / config->speed;
  52. if (config->speed == 0)
  53. return 0;
  54. if (divisor > 0xffff || divisor == 0)
  55. return 0;
  56. actual_speed = base_clock / divisor;
  57. error = actual_speed > config->speed ? (actual_speed - config->speed)
  58. : (config->speed - actual_speed);
  59. if (error > (config->speed / 30 + 1))
  60. return 0;
  61. return divisor;
  62. }
  63. static void
  64. do_real_config (struct grub_serial_port *port)
  65. {
  66. int divisor;
  67. unsigned char status = 0;
  68. grub_uint64_t endtime;
  69. const unsigned char parities[] = {
  70. [GRUB_SERIAL_PARITY_NONE] = UART_NO_PARITY,
  71. [GRUB_SERIAL_PARITY_ODD] = UART_ODD_PARITY,
  72. [GRUB_SERIAL_PARITY_EVEN] = UART_EVEN_PARITY
  73. };
  74. const unsigned char stop_bits[] = {
  75. [GRUB_SERIAL_STOP_BITS_1] = UART_1_STOP_BIT,
  76. [GRUB_SERIAL_STOP_BITS_2] = UART_2_STOP_BITS,
  77. };
  78. if (port->configured)
  79. return;
  80. port->broken = 0;
  81. divisor = serial_get_divisor (port, &port->config);
  82. /* Turn off the interrupt. */
  83. grub_outb (0, port->port + UART_IER);
  84. /* Set DLAB. */
  85. grub_outb (UART_DLAB, port->port + UART_LCR);
  86. /* Set the baud rate. */
  87. grub_outb (divisor & 0xFF, port->port + UART_DLL);
  88. grub_outb (divisor >> 8, port->port + UART_DLH);
  89. /* Set the line status. */
  90. status |= (parities[port->config.parity]
  91. | (port->config.word_len - 5)
  92. | stop_bits[port->config.stop_bits]);
  93. grub_outb (status, port->port + UART_LCR);
  94. if (port->config.rtscts)
  95. {
  96. /* Enable the FIFO. */
  97. grub_outb (UART_ENABLE_FIFO_TRIGGER1, port->port + UART_FCR);
  98. /* Turn on DTR and RTS. */
  99. grub_outb (UART_ENABLE_DTRRTS, port->port + UART_MCR);
  100. }
  101. else
  102. {
  103. /* Enable the FIFO. */
  104. grub_outb (UART_ENABLE_FIFO_TRIGGER14, port->port + UART_FCR);
  105. /* Turn on DTR, RTS, and OUT2. */
  106. grub_outb (UART_ENABLE_DTRRTS | UART_ENABLE_OUT2, port->port + UART_MCR);
  107. }
  108. /* Drain the input buffer. */
  109. endtime = grub_get_time_ms () + 1000;
  110. while (grub_inb (port->port + UART_LSR) & UART_DATA_READY)
  111. {
  112. grub_inb (port->port + UART_RX);
  113. if (grub_get_time_ms () > endtime)
  114. {
  115. port->broken = 1;
  116. break;
  117. }
  118. }
  119. port->configured = 1;
  120. }
  121. /* Fetch a key. */
  122. static int
  123. serial_hw_fetch (struct grub_serial_port *port)
  124. {
  125. do_real_config (port);
  126. if (grub_inb (port->port + UART_LSR) & UART_DATA_READY)
  127. return grub_inb (port->port + UART_RX);
  128. return -1;
  129. }
  130. /* Put a character. */
  131. static void
  132. serial_hw_put (struct grub_serial_port *port, const int c)
  133. {
  134. grub_uint64_t endtime;
  135. do_real_config (port);
  136. if (port->broken > 5)
  137. endtime = grub_get_time_ms ();
  138. else if (port->broken > 1)
  139. endtime = grub_get_time_ms () + 50;
  140. else
  141. endtime = grub_get_time_ms () + 200;
  142. /* Wait until the transmitter holding register is empty. */
  143. while ((grub_inb (port->port + UART_LSR) & UART_EMPTY_TRANSMITTER) == 0)
  144. {
  145. if (grub_get_time_ms () > endtime)
  146. {
  147. port->broken++;
  148. /* There is something wrong. But what can I do? */
  149. return;
  150. }
  151. }
  152. if (port->broken)
  153. port->broken--;
  154. grub_outb (c, port->port + UART_TX);
  155. }
  156. /* Initialize a serial device. PORT is the port number for a serial device.
  157. SPEED is a DTE-DTE speed which must be one of these: 2400, 4800, 9600,
  158. 19200, 38400, 57600 and 115200. WORD_LEN is the word length to be used
  159. for the device. Likewise, PARITY is the type of the parity and
  160. STOP_BIT_LEN is the length of the stop bit. The possible values for
  161. WORD_LEN, PARITY and STOP_BIT_LEN are defined in the header file as
  162. macros. */
  163. static grub_err_t
  164. serial_hw_configure (struct grub_serial_port *port,
  165. struct grub_serial_config *config)
  166. {
  167. unsigned short divisor;
  168. divisor = serial_get_divisor (port, config);
  169. if (divisor == 0)
  170. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  171. N_("unsupported serial port speed"));
  172. if (config->parity != GRUB_SERIAL_PARITY_NONE
  173. && config->parity != GRUB_SERIAL_PARITY_ODD
  174. && config->parity != GRUB_SERIAL_PARITY_EVEN)
  175. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  176. N_("unsupported serial port parity"));
  177. if (config->stop_bits != GRUB_SERIAL_STOP_BITS_1
  178. && config->stop_bits != GRUB_SERIAL_STOP_BITS_2)
  179. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  180. N_("unsupported serial port stop bits number"));
  181. if (config->word_len < 5 || config->word_len > 8)
  182. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  183. N_("unsupported serial port word length"));
  184. port->config = *config;
  185. port->configured = 0;
  186. /* FIXME: should check if the serial terminal was found. */
  187. return GRUB_ERR_NONE;
  188. }
  189. struct grub_serial_driver grub_ns8250_driver =
  190. {
  191. .configure = serial_hw_configure,
  192. .fetch = serial_hw_fetch,
  193. .put = serial_hw_put
  194. };
  195. static char com_names[GRUB_SERIAL_PORT_NUM][20];
  196. static struct grub_serial_port com_ports[GRUB_SERIAL_PORT_NUM];
  197. void
  198. grub_ns8250_init (void)
  199. {
  200. unsigned i;
  201. for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
  202. if (serial_hw_io_addr[i])
  203. {
  204. grub_err_t err;
  205. grub_outb (0x5a, serial_hw_io_addr[i] + UART_SR);
  206. if (grub_inb (serial_hw_io_addr[i] + UART_SR) != 0x5a)
  207. {
  208. dead_ports |= (1 << i);
  209. continue;
  210. }
  211. grub_outb (0xa5, serial_hw_io_addr[i] + UART_SR);
  212. if (grub_inb (serial_hw_io_addr[i] + UART_SR) != 0xa5)
  213. {
  214. dead_ports |= (1 << i);
  215. continue;
  216. }
  217. grub_snprintf (com_names[i], sizeof (com_names[i]), "com%d", i);
  218. com_ports[i].name = com_names[i];
  219. com_ports[i].driver = &grub_ns8250_driver;
  220. com_ports[i].port = serial_hw_io_addr[i];
  221. err = grub_serial_config_defaults (&com_ports[i]);
  222. if (err)
  223. grub_print_error ();
  224. grub_serial_register (&com_ports[i]);
  225. }
  226. }
  227. /* Return the port number for the UNITth serial device. */
  228. grub_port_t
  229. grub_ns8250_hw_get_port (const unsigned int unit)
  230. {
  231. if (unit < GRUB_SERIAL_PORT_NUM
  232. && !(dead_ports & (1 << unit)))
  233. return serial_hw_io_addr[unit];
  234. else
  235. return 0;
  236. }
  237. char *
  238. grub_serial_ns8250_add_port (grub_port_t port)
  239. {
  240. struct grub_serial_port *p;
  241. unsigned i;
  242. for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
  243. if (com_ports[i].port == port)
  244. {
  245. if (dead_ports & (1 << i))
  246. return NULL;
  247. return com_names[i];
  248. }
  249. grub_outb (0x5a, port + UART_SR);
  250. if (grub_inb (port + UART_SR) != 0x5a)
  251. return NULL;
  252. grub_outb (0xa5, port + UART_SR);
  253. if (grub_inb (port + UART_SR) != 0xa5)
  254. return NULL;
  255. p = grub_malloc (sizeof (*p));
  256. if (!p)
  257. return NULL;
  258. p->name = grub_xasprintf ("port%lx", (unsigned long) port);
  259. if (!p->name)
  260. {
  261. grub_free (p);
  262. return NULL;
  263. }
  264. p->driver = &grub_ns8250_driver;
  265. grub_serial_config_defaults (p);
  266. p->port = port;
  267. grub_serial_register (p);
  268. return p->name;
  269. }