/ src / include / console / uart.h
uart.h
  1  /* SPDX-License-Identifier: GPL-2.0-only */
  2  
  3  #ifndef CONSOLE_UART_H
  4  #define CONSOLE_UART_H
  5  
  6  #include <stdint.h>
  7  
  8  /* Return the clock frequency UART uses as reference clock for
  9   * baudrate generator. */
 10  unsigned int uart_platform_refclk(void);
 11  
 12  #if CONFIG(UART_OVERRIDE_BAUDRATE)
 13  /* Return the baudrate, define this in your platform when using the above
 14     configuration. */
 15  unsigned int get_uart_baudrate(void);
 16  #else
 17  static inline unsigned int get_uart_baudrate(void)
 18  {
 19  	return CONFIG_TTYS0_BAUD;
 20  }
 21  #endif
 22  
 23  #if CONFIG(OVERRIDE_UART_FOR_CONSOLE)
 24  /* Return the index of uart port, define this in your platform
 25   * when need to use variables to override the index.
 26   */
 27  unsigned int get_uart_for_console(void);
 28  #else
 29  static inline unsigned int get_uart_for_console(void)
 30  {
 31  	return CONFIG_UART_FOR_CONSOLE;
 32  }
 33  #endif
 34  
 35  /* Returns the divisor value for a given baudrate.
 36   * The formula to satisfy is:
 37   *    refclk / divisor = baudrate * oversample
 38   */
 39  unsigned int uart_baudrate_divisor(unsigned int baudrate,
 40  	unsigned int refclk, unsigned int oversample);
 41  
 42  /* Returns the oversample divisor multiplied by any other divisors that act
 43   * on the input clock
 44   */
 45  unsigned int uart_input_clock_divider(void);
 46  
 47  /* Bitbang out one byte on an 8n1 UART through the output function set_tx(). */
 48  void uart_bitbang_tx_byte(unsigned char data, void (*set_tx)(int line_state));
 49  
 50  void uart_init(unsigned int idx);
 51  void uart_tx_byte(unsigned int idx, unsigned char data);
 52  void uart_tx_flush(unsigned int idx);
 53  unsigned char uart_rx_byte(unsigned int idx);
 54  
 55  uintptr_t uart_platform_base(unsigned int idx);
 56  
 57  static inline void *uart_platform_baseptr(unsigned int idx)
 58  {
 59  	return (void *)uart_platform_base(idx);
 60  }
 61  
 62  void oxford_remap(unsigned int new_base);
 63  
 64  #define __CONSOLE_SERIAL_ENABLE__	(CONFIG(CONSOLE_SERIAL) && \
 65  	(ENV_BOOTBLOCK || ENV_SEPARATE_ROMSTAGE || ENV_RAMSTAGE || ENV_SEPARATE_VERSTAGE \
 66  	 || ENV_POSTCAR || (ENV_SMM && CONFIG(DEBUG_SMI))))
 67  
 68  #if __CONSOLE_SERIAL_ENABLE__
 69  static inline void __uart_init(void)
 70  {
 71  	uart_init(get_uart_for_console());
 72  }
 73  static inline void __uart_tx_byte(u8 data)
 74  {
 75  	uart_tx_byte(get_uart_for_console(), data);
 76  }
 77  static inline void __uart_tx_flush(void)
 78  {
 79  	uart_tx_flush(get_uart_for_console());
 80  }
 81  #else
 82  static inline void __uart_init(void)		{}
 83  static inline void __uart_tx_byte(u8 data)	{}
 84  static inline void __uart_tx_flush(void)	{}
 85  #endif
 86  
 87  #if CONFIG(GDB_STUB) && (ENV_ROMSTAGE_OR_BEFORE || ENV_RAMSTAGE)
 88  #define CONF_UART_FOR_GDB	CONFIG_UART_FOR_CONSOLE
 89  static inline void __gdb_hw_init(void)	{ uart_init(CONF_UART_FOR_GDB); }
 90  static inline void __gdb_tx_byte(u8 data)
 91  {
 92  	uart_tx_byte(CONF_UART_FOR_GDB, data);
 93  }
 94  static inline void __gdb_tx_flush(void)	{ uart_tx_flush(CONF_UART_FOR_GDB); }
 95  static inline u8 __gdb_rx_byte(void)
 96  {
 97  	return uart_rx_byte(CONF_UART_FOR_GDB);
 98  }
 99  #endif
100  
101  #endif /* CONSOLE_UART_H */