Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

uart.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * 16550-compatible UART
  27. *
  28. */
  29. #include <unistd.h>
  30. #include <errno.h>
  31. #include <ipxe/uart.h>
  32. /** Timeout for transmit holding register to become empty */
  33. #define UART_THRE_TIMEOUT_MS 100
  34. /** Timeout for transmitter to become empty */
  35. #define UART_TEMT_TIMEOUT_MS 1000
  36. /**
  37. * Transmit data
  38. *
  39. * @v uart UART
  40. * @v data Data
  41. */
  42. void uart_transmit ( struct uart *uart, uint8_t data ) {
  43. unsigned int i;
  44. uint8_t lsr;
  45. /* Wait for transmitter holding register to become empty */
  46. for ( i = 0 ; i < UART_THRE_TIMEOUT_MS ; i++ ) {
  47. lsr = uart_read ( uart, UART_LSR );
  48. if ( lsr & UART_LSR_THRE )
  49. break;
  50. mdelay ( 1 );
  51. }
  52. /* Transmit data (even if we timed out) */
  53. uart_write ( uart, UART_THR, data );
  54. }
  55. /**
  56. * Flush data
  57. *
  58. * @v uart UART
  59. */
  60. void uart_flush ( struct uart *uart ) {
  61. unsigned int i;
  62. uint8_t lsr;
  63. /* Wait for transmitter and receiver to become empty */
  64. for ( i = 0 ; i < UART_TEMT_TIMEOUT_MS ; i++ ) {
  65. uart_read ( uart, UART_RBR );
  66. lsr = uart_read ( uart, UART_LSR );
  67. if ( ( lsr & UART_LSR_TEMT ) && ! ( lsr & UART_LSR_DR ) )
  68. break;
  69. }
  70. }
  71. /**
  72. * Check for existence of UART
  73. *
  74. * @v uart UART
  75. * @ret rc Return status code
  76. */
  77. int uart_exists ( struct uart *uart ) {
  78. /* Fail if no UART port is defined */
  79. if ( ! uart->base )
  80. return -ENODEV;
  81. /* Fail if UART scratch register seems not to be present */
  82. uart_write ( uart, UART_SCR, 0x18 );
  83. if ( uart_read ( uart, UART_SCR ) != 0x18 )
  84. return -ENODEV;
  85. uart_write ( uart, UART_SCR, 0xae );
  86. if ( uart_read ( uart, UART_SCR ) != 0xae )
  87. return -ENODEV;
  88. return 0;
  89. }
  90. /**
  91. * Initialise UART
  92. *
  93. * @v uart UART
  94. * @v baud Baud rate, or zero to leave unchanged
  95. * @v lcr Line control register value, or zero to leave unchanged
  96. * @ret rc Return status code
  97. */
  98. int uart_init ( struct uart *uart, unsigned int baud, uint8_t lcr ) {
  99. uint8_t dlm;
  100. uint8_t dll;
  101. int rc;
  102. /* Check for existence of UART */
  103. if ( ( rc = uart_exists ( uart ) ) != 0 )
  104. return rc;
  105. /* Configure divisor and line control register, if applicable */
  106. if ( ! lcr )
  107. lcr = uart_read ( uart, UART_LCR );
  108. uart->lcr = lcr;
  109. uart_write ( uart, UART_LCR, ( lcr | UART_LCR_DLAB ) );
  110. if ( baud ) {
  111. uart->divisor = ( UART_MAX_BAUD / baud );
  112. dlm = ( ( uart->divisor >> 8 ) & 0xff );
  113. dll = ( ( uart->divisor >> 0 ) & 0xff );
  114. uart_write ( uart, UART_DLM, dlm );
  115. uart_write ( uart, UART_DLL, dll );
  116. } else {
  117. dlm = uart_read ( uart, UART_DLM );
  118. dll = uart_read ( uart, UART_DLL );
  119. uart->divisor = ( ( dlm << 8 ) | dll );
  120. }
  121. uart_write ( uart, UART_LCR, ( lcr & ~UART_LCR_DLAB ) );
  122. /* Disable interrupts */
  123. uart_write ( uart, UART_IER, 0 );
  124. /* Enable FIFOs */
  125. uart_write ( uart, UART_FCR, UART_FCR_FE );
  126. /* Assert DTR and RTS */
  127. uart_write ( uart, UART_MCR, ( UART_MCR_DTR | UART_MCR_RTS ) );
  128. /* Flush any stale data */
  129. uart_flush ( uart );
  130. return 0;
  131. }