Angel Pons | 32859fc | 2020-04-02 23:48:27 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 509f772 | 2012-12-07 17:31:37 -0800 | [diff] [blame] | 2 | |
Kyösti Mälkki | 1d7541f | 2014-02-17 21:34:42 +0200 | [diff] [blame] | 3 | #ifndef CONSOLE_UART_H |
| 4 | #define CONSOLE_UART_H |
Stefan Reinauer | 509f772 | 2012-12-07 17:31:37 -0800 | [diff] [blame] | 5 | |
Kyösti Mälkki | 0567c91 | 2014-02-14 10:31:38 +0200 | [diff] [blame] | 6 | #include <stdint.h> |
| 7 | |
Kyösti Mälkki | 3ee1668 | 2014-02-17 19:37:52 +0200 | [diff] [blame] | 8 | /* Return the clock frequency UART uses as reference clock for |
| 9 | * baudrate generator. */ |
| 10 | unsigned int uart_platform_refclk(void); |
| 11 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 12 | #if CONFIG(UART_OVERRIDE_BAUDRATE) |
Julien Viard de Galbert | 235daa4 | 2018-02-20 11:45:48 +0100 | [diff] [blame] | 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 | |
Bryant Ou | 0ee920b | 2020-09-14 23:41:41 -0700 | [diff] [blame] | 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 | |
Kyösti Mälkki | 3ee1668 | 2014-02-17 19:37:52 +0200 | [diff] [blame] | 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 | |
Lee Leahy | 1487621 | 2016-05-04 13:13:20 -0700 | [diff] [blame] | 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); |
Kyösti Mälkki | 3ee1668 | 2014-02-17 19:37:52 +0200 | [diff] [blame] | 46 | |
Julius Werner | 1732dcb | 2018-04-24 16:33:58 -0700 | [diff] [blame] | 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 | |
Felix Held | e3a1247 | 2020-09-11 15:47:09 +0200 | [diff] [blame] | 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); |
Stefan Reinauer | 509f772 | 2012-12-07 17:31:37 -0800 | [diff] [blame] | 54 | |
Felix Held | e3a1247 | 2020-09-11 15:47:09 +0200 | [diff] [blame] | 55 | uintptr_t uart_platform_base(unsigned int idx); |
David Hendricks | fdcef1a | 2013-02-15 19:29:12 -0800 | [diff] [blame] | 56 | |
Felix Held | e3a1247 | 2020-09-11 15:47:09 +0200 | [diff] [blame] | 57 | static inline void *uart_platform_baseptr(unsigned int idx) |
Kyösti Mälkki | c2610a4 | 2014-02-24 20:51:30 +0200 | [diff] [blame] | 58 | { |
| 59 | return (void *)uart_platform_base(idx); |
| 60 | } |
Kyösti Mälkki | c2610a4 | 2014-02-24 20:51:30 +0200 | [diff] [blame] | 61 | |
Kyösti Mälkki | 2b95da0 | 2014-02-15 10:19:23 +0200 | [diff] [blame] | 62 | void oxford_remap(unsigned int new_base); |
Kyösti Mälkki | 1d7541f | 2014-02-17 21:34:42 +0200 | [diff] [blame] | 63 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 64 | #define __CONSOLE_SERIAL_ENABLE__ (CONFIG(CONSOLE_SERIAL) && \ |
Arthur Heymans | a2bc254 | 2021-05-29 08:10:49 +0200 | [diff] [blame^] | 65 | (ENV_BOOTBLOCK || ENV_SEPARATE_ROMSTAGE || ENV_RAMSTAGE || ENV_SEPARATE_VERSTAGE \ |
Julius Werner | 21a4053 | 2020-04-21 16:03:53 -0700 | [diff] [blame] | 66 | || ENV_POSTCAR || (ENV_SMM && CONFIG(DEBUG_SMI)))) |
Kyösti Mälkki | f339086 | 2014-02-26 15:19:04 +0200 | [diff] [blame] | 67 | |
| 68 | #if __CONSOLE_SERIAL_ENABLE__ |
Lee Leahy | 6a566d7 | 2017-03-07 17:45:12 -0800 | [diff] [blame] | 69 | static inline void __uart_init(void) |
| 70 | { |
Bryant Ou | 0ee920b | 2020-09-14 23:41:41 -0700 | [diff] [blame] | 71 | uart_init(get_uart_for_console()); |
Lee Leahy | 6a566d7 | 2017-03-07 17:45:12 -0800 | [diff] [blame] | 72 | } |
| 73 | static inline void __uart_tx_byte(u8 data) |
| 74 | { |
Bryant Ou | 0ee920b | 2020-09-14 23:41:41 -0700 | [diff] [blame] | 75 | uart_tx_byte(get_uart_for_console(), data); |
Lee Leahy | 6a566d7 | 2017-03-07 17:45:12 -0800 | [diff] [blame] | 76 | } |
| 77 | static inline void __uart_tx_flush(void) |
| 78 | { |
Bryant Ou | 0ee920b | 2020-09-14 23:41:41 -0700 | [diff] [blame] | 79 | uart_tx_flush(get_uart_for_console()); |
Lee Leahy | 6a566d7 | 2017-03-07 17:45:12 -0800 | [diff] [blame] | 80 | } |
Kyösti Mälkki | f339086 | 2014-02-26 15:19:04 +0200 | [diff] [blame] | 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 | |
Arthur Heymans | 2fba476 | 2023-11-06 15:33:23 +0100 | [diff] [blame] | 87 | #if CONFIG(GDB_STUB) && (ENV_ROMSTAGE_OR_BEFORE || ENV_RAMSTAGE) |
Elyes HAOUAS | 251514d | 2019-01-23 11:36:44 +0100 | [diff] [blame] | 88 | #define CONF_UART_FOR_GDB CONFIG_UART_FOR_CONSOLE |
| 89 | static inline void __gdb_hw_init(void) { uart_init(CONF_UART_FOR_GDB); } |
Lee Leahy | 6a566d7 | 2017-03-07 17:45:12 -0800 | [diff] [blame] | 90 | static inline void __gdb_tx_byte(u8 data) |
| 91 | { |
Elyes HAOUAS | 251514d | 2019-01-23 11:36:44 +0100 | [diff] [blame] | 92 | uart_tx_byte(CONF_UART_FOR_GDB, data); |
Lee Leahy | 6a566d7 | 2017-03-07 17:45:12 -0800 | [diff] [blame] | 93 | } |
Elyes HAOUAS | 251514d | 2019-01-23 11:36:44 +0100 | [diff] [blame] | 94 | static inline void __gdb_tx_flush(void) { uart_tx_flush(CONF_UART_FOR_GDB); } |
Lee Leahy | 6a566d7 | 2017-03-07 17:45:12 -0800 | [diff] [blame] | 95 | static inline u8 __gdb_rx_byte(void) |
| 96 | { |
Elyes HAOUAS | 251514d | 2019-01-23 11:36:44 +0100 | [diff] [blame] | 97 | return uart_rx_byte(CONF_UART_FOR_GDB); |
Lee Leahy | 6a566d7 | 2017-03-07 17:45:12 -0800 | [diff] [blame] | 98 | } |
Kyösti Mälkki | f2f7f03 | 2014-04-04 15:05:28 +0300 | [diff] [blame] | 99 | #endif |
| 100 | |
Kyösti Mälkki | 1d7541f | 2014-02-17 21:34:42 +0200 | [diff] [blame] | 101 | #endif /* CONSOLE_UART_H */ |