ns8250.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. #define GRUB_SERIAL_PORT_NUM 4
  30. #else
  31. #include <grub/machine/serial.h>
  32. static const grub_port_t serial_hw_io_addr[] = GRUB_MACHINE_SERIAL_PORTS;
  33. #define GRUB_SERIAL_PORT_NUM (ARRAY_SIZE(serial_hw_io_addr))
  34. #endif
  35. static int dead_ports = 0;
  36. static grub_uint8_t
  37. ns8250_reg_read (struct grub_serial_port *port, grub_addr_t reg)
  38. {
  39. asm volatile("" : : : "memory");
  40. if (port->use_mmio == true)
  41. {
  42. /*
  43. * Note: we assume MMIO UARTs are little endian. This is not true of all
  44. * embedded platforms but will do for now.
  45. */
  46. switch(port->mmio.access_size)
  47. {
  48. default:
  49. /* ACPI tables occasionally uses "0" (legacy) as equivalent to "1" (byte). */
  50. case 1:
  51. return *((volatile grub_uint8_t *) (port->mmio.base + reg));
  52. case 2:
  53. return grub_le_to_cpu16 (*((volatile grub_uint16_t *) (port->mmio.base + (reg << 1))));
  54. case 3:
  55. return grub_le_to_cpu32 (*((volatile grub_uint32_t *) (port->mmio.base + (reg << 2))));
  56. case 4:
  57. /*
  58. * This will only work properly on 64-bit systems since 64-bit
  59. * accessors aren't atomic on 32-bit hardware. Thankfully the
  60. * case of a UART with a 64-bit register spacing on 32-bit
  61. * also probably doesn't exist.
  62. */
  63. return grub_le_to_cpu64 (*((volatile grub_uint64_t *) (port->mmio.base + (reg << 3))));
  64. }
  65. }
  66. return grub_inb (port->port + reg);
  67. }
  68. static void
  69. ns8250_reg_write (struct grub_serial_port *port, grub_uint8_t value, grub_addr_t reg)
  70. {
  71. asm volatile("" : : : "memory");
  72. if (port->use_mmio == true)
  73. {
  74. switch(port->mmio.access_size)
  75. {
  76. default:
  77. /* ACPI tables occasionally uses "0" (legacy) as equivalent to "1" (byte). */
  78. case 1:
  79. *((volatile grub_uint8_t *) (port->mmio.base + reg)) = value;
  80. break;
  81. case 2:
  82. *((volatile grub_uint16_t *) (port->mmio.base + (reg << 1))) = grub_cpu_to_le16 (value);
  83. break;
  84. case 3:
  85. *((volatile grub_uint32_t *) (port->mmio.base + (reg << 2))) = grub_cpu_to_le32 (value);
  86. break;
  87. case 4:
  88. /* See commment in ns8250_reg_read(). */
  89. *((volatile grub_uint64_t *) (port->mmio.base + (reg << 3))) = grub_cpu_to_le64 (value);
  90. break;
  91. }
  92. }
  93. else
  94. grub_outb (value, port->port + reg);
  95. }
  96. /* Convert speed to divisor. */
  97. static unsigned short
  98. serial_get_divisor (const struct grub_serial_port *port __attribute__ ((unused)),
  99. const struct grub_serial_config *config)
  100. {
  101. grub_uint32_t base_clock;
  102. grub_uint32_t divisor;
  103. grub_uint32_t actual_speed, error;
  104. /* Get the UART input clock frequency. */
  105. base_clock = config->base_clock ? config->base_clock : UART_DEFAULT_BASE_CLOCK;
  106. /*
  107. * The UART uses 16 times oversampling for the BRG, so adjust the value
  108. * accordingly to calculate the divisor.
  109. */
  110. base_clock >>= 4;
  111. divisor = (base_clock + (config->speed / 2)) / config->speed;
  112. if (config->speed == 0)
  113. return 0;
  114. if (divisor > 0xffff || divisor == 0)
  115. return 0;
  116. actual_speed = base_clock / divisor;
  117. error = actual_speed > config->speed ? (actual_speed - config->speed)
  118. : (config->speed - actual_speed);
  119. if (error > (config->speed / 30 + 1))
  120. return 0;
  121. return divisor;
  122. }
  123. static void
  124. do_real_config (struct grub_serial_port *port)
  125. {
  126. int divisor;
  127. unsigned char status = 0;
  128. grub_uint64_t endtime;
  129. const unsigned char parities[] = {
  130. [GRUB_SERIAL_PARITY_NONE] = UART_NO_PARITY,
  131. [GRUB_SERIAL_PARITY_ODD] = UART_ODD_PARITY,
  132. [GRUB_SERIAL_PARITY_EVEN] = UART_EVEN_PARITY
  133. };
  134. const unsigned char stop_bits[] = {
  135. [GRUB_SERIAL_STOP_BITS_1] = UART_1_STOP_BIT,
  136. [GRUB_SERIAL_STOP_BITS_2] = UART_2_STOP_BITS,
  137. };
  138. if (port->configured)
  139. return;
  140. port->broken = 0;
  141. divisor = serial_get_divisor (port, &port->config);
  142. /* Turn off the interrupt. */
  143. ns8250_reg_write (port, 0, UART_IER);
  144. /* Set DLAB. */
  145. ns8250_reg_write (port, UART_DLAB, UART_LCR);
  146. ns8250_reg_write (port, divisor & 0xFF, UART_DLL);
  147. ns8250_reg_write (port, divisor >> 8, UART_DLH);
  148. /* Set the line status. */
  149. status |= (parities[port->config.parity]
  150. | (port->config.word_len - 5)
  151. | stop_bits[port->config.stop_bits]);
  152. ns8250_reg_write (port, status, UART_LCR);
  153. if (port->config.rtscts)
  154. {
  155. /* Enable the FIFO. */
  156. ns8250_reg_write (port, UART_ENABLE_FIFO_TRIGGER1, UART_FCR);
  157. /* Turn on DTR and RTS. */
  158. ns8250_reg_write (port, UART_ENABLE_DTRRTS, UART_MCR);
  159. }
  160. else
  161. {
  162. /* Enable the FIFO. */
  163. ns8250_reg_write (port, UART_ENABLE_FIFO_TRIGGER14, UART_FCR);
  164. /* Turn on DTR, RTS, and OUT2. */
  165. ns8250_reg_write (port, UART_ENABLE_DTRRTS | UART_ENABLE_OUT2, UART_MCR);
  166. }
  167. /* Drain the input buffer. */
  168. endtime = grub_get_time_ms () + 1000;
  169. while (ns8250_reg_read (port, UART_LSR) & UART_DATA_READY)
  170. {
  171. ns8250_reg_read (port, UART_RX);
  172. if (grub_get_time_ms () > endtime)
  173. {
  174. port->broken = 1;
  175. break;
  176. }
  177. }
  178. port->configured = 1;
  179. }
  180. /* Fetch a key. */
  181. static int
  182. serial_hw_fetch (struct grub_serial_port *port)
  183. {
  184. do_real_config (port);
  185. if (ns8250_reg_read (port, UART_LSR) & UART_DATA_READY)
  186. return ns8250_reg_read (port, UART_RX);
  187. return -1;
  188. }
  189. /* Put a character. */
  190. static void
  191. serial_hw_put (struct grub_serial_port *port, const int c)
  192. {
  193. grub_uint64_t endtime;
  194. do_real_config (port);
  195. if (port->broken > 5)
  196. endtime = grub_get_time_ms ();
  197. else if (port->broken > 1)
  198. endtime = grub_get_time_ms () + 50;
  199. else
  200. endtime = grub_get_time_ms () + 200;
  201. /* Wait until the transmitter holding register is empty. */
  202. while ((ns8250_reg_read (port, UART_LSR) & UART_EMPTY_TRANSMITTER) == 0)
  203. {
  204. if (grub_get_time_ms () > endtime)
  205. {
  206. port->broken++;
  207. /* There is something wrong. But what can I do? */
  208. return;
  209. }
  210. }
  211. if (port->broken)
  212. port->broken--;
  213. ns8250_reg_write (port, c, UART_TX);
  214. }
  215. /* Initialize a serial device. PORT is the port number for a serial device.
  216. SPEED is a DTE-DTE speed which must be one of these: 2400, 4800, 9600,
  217. 19200, 38400, 57600 and 115200. WORD_LEN is the word length to be used
  218. for the device. Likewise, PARITY is the type of the parity and
  219. STOP_BIT_LEN is the length of the stop bit. The possible values for
  220. WORD_LEN, PARITY and STOP_BIT_LEN are defined in the header file as
  221. macros. */
  222. static grub_err_t
  223. serial_hw_configure (struct grub_serial_port *port,
  224. struct grub_serial_config *config)
  225. {
  226. unsigned short divisor;
  227. divisor = serial_get_divisor (port, config);
  228. if (divisor == 0)
  229. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  230. N_("unsupported serial port speed"));
  231. if (config->parity != GRUB_SERIAL_PARITY_NONE
  232. && config->parity != GRUB_SERIAL_PARITY_ODD
  233. && config->parity != GRUB_SERIAL_PARITY_EVEN)
  234. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  235. N_("unsupported serial port parity"));
  236. if (config->stop_bits != GRUB_SERIAL_STOP_BITS_1
  237. && config->stop_bits != GRUB_SERIAL_STOP_BITS_2)
  238. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  239. N_("unsupported serial port stop bits number"));
  240. if (config->word_len < 5 || config->word_len > 8)
  241. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  242. N_("unsupported serial port word length"));
  243. port->config = *config;
  244. port->configured = 0;
  245. /* FIXME: should check if the serial terminal was found. */
  246. return GRUB_ERR_NONE;
  247. }
  248. struct grub_serial_driver grub_ns8250_driver =
  249. {
  250. .configure = serial_hw_configure,
  251. .fetch = serial_hw_fetch,
  252. .put = serial_hw_put
  253. };
  254. static char com_names[GRUB_SERIAL_PORT_NUM][20];
  255. static struct grub_serial_port com_ports[GRUB_SERIAL_PORT_NUM];
  256. void
  257. grub_ns8250_init (void)
  258. {
  259. #ifdef GRUB_MACHINE_PCBIOS
  260. const unsigned short *serial_hw_io_addr = (const unsigned short *) grub_absolute_pointer (GRUB_MEMORY_MACHINE_BIOS_DATA_AREA_ADDR);
  261. #endif
  262. unsigned i;
  263. for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
  264. if (serial_hw_io_addr[i])
  265. {
  266. grub_err_t err;
  267. grub_outb (0x5a, serial_hw_io_addr[i] + UART_SR);
  268. if (grub_inb (serial_hw_io_addr[i] + UART_SR) != 0x5a)
  269. {
  270. dead_ports |= (1 << i);
  271. continue;
  272. }
  273. grub_outb (0xa5, serial_hw_io_addr[i] + UART_SR);
  274. if (grub_inb (serial_hw_io_addr[i] + UART_SR) != 0xa5)
  275. {
  276. dead_ports |= (1 << i);
  277. continue;
  278. }
  279. grub_snprintf (com_names[i], sizeof (com_names[i]), "com%d", i);
  280. com_ports[i].name = com_names[i];
  281. com_ports[i].driver = &grub_ns8250_driver;
  282. com_ports[i].port = serial_hw_io_addr[i];
  283. com_ports[i].use_mmio = false;
  284. err = grub_serial_config_defaults (&com_ports[i]);
  285. if (err)
  286. grub_print_error ();
  287. grub_serial_register (&com_ports[i]);
  288. }
  289. }
  290. /* Return the port number for the UNITth serial device. */
  291. grub_port_t
  292. grub_ns8250_hw_get_port (const unsigned int unit)
  293. {
  294. #ifdef GRUB_MACHINE_PCBIOS
  295. const unsigned short *serial_hw_io_addr = (const unsigned short *) grub_absolute_pointer (GRUB_MEMORY_MACHINE_BIOS_DATA_AREA_ADDR);
  296. #endif
  297. if (unit < GRUB_SERIAL_PORT_NUM
  298. && !(dead_ports & (1 << unit)))
  299. return serial_hw_io_addr[unit];
  300. else
  301. return 0;
  302. }
  303. struct grub_serial_port *
  304. grub_serial_ns8250_add_port (grub_port_t port, struct grub_serial_config *config)
  305. {
  306. struct grub_serial_port *p;
  307. unsigned i;
  308. for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
  309. if (com_ports[i].port == port)
  310. {
  311. if (dead_ports & (1 << i))
  312. return NULL;
  313. /* Give the opportunity for SPCR to configure a default com port. */
  314. if (config != NULL)
  315. grub_serial_port_configure (&com_ports[i], config);
  316. return &com_ports[i];
  317. }
  318. FOR_SERIAL_PORTS (p)
  319. if (p->use_mmio == false && p->port == port)
  320. {
  321. if (config != NULL)
  322. grub_serial_port_configure (p, config);
  323. return p;
  324. }
  325. grub_outb (0x5a, port + UART_SR);
  326. if (grub_inb (port + UART_SR) != 0x5a)
  327. return NULL;
  328. grub_outb (0xa5, port + UART_SR);
  329. if (grub_inb (port + UART_SR) != 0xa5)
  330. return NULL;
  331. p = grub_malloc (sizeof (*p));
  332. if (p == NULL)
  333. return NULL;
  334. p->name = grub_xasprintf ("port%lx", (unsigned long) port);
  335. if (p->name == NULL)
  336. {
  337. grub_free (p);
  338. return NULL;
  339. }
  340. p->driver = &grub_ns8250_driver;
  341. p->use_mmio = false;
  342. p->port = port;
  343. if (config != NULL)
  344. grub_serial_port_configure (p, config);
  345. else
  346. grub_serial_config_defaults (p);
  347. grub_serial_register (p);
  348. return p;
  349. }
  350. struct grub_serial_port *
  351. grub_serial_ns8250_add_mmio (grub_addr_t addr, unsigned int acc_size,
  352. struct grub_serial_config *config)
  353. {
  354. struct grub_serial_port *p;
  355. unsigned i;
  356. for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
  357. if (com_ports[i].use_mmio == true && com_ports[i].mmio.base == addr)
  358. {
  359. if (config != NULL)
  360. grub_serial_port_configure (&com_ports[i], config);
  361. return &com_ports[i];
  362. }
  363. FOR_SERIAL_PORTS (p)
  364. if (p->use_mmio == true && p->mmio.base == addr)
  365. {
  366. if (config != NULL)
  367. grub_serial_port_configure (p, config);
  368. return p;
  369. }
  370. p = grub_malloc (sizeof (*p));
  371. if (p == NULL)
  372. return NULL;
  373. p->name = grub_xasprintf ("mmio,%llx", (unsigned long long) addr);
  374. if (p->name == NULL)
  375. {
  376. grub_free (p);
  377. return NULL;
  378. }
  379. p->driver = &grub_ns8250_driver;
  380. p->use_mmio = true;
  381. p->mmio.base = addr;
  382. p->mmio.access_size = acc_size;
  383. if (config != NULL)
  384. grub_serial_port_configure (p, config);
  385. else
  386. grub_serial_config_defaults (p);
  387. grub_serial_register (p);
  388. return p;
  389. }