blob: 3f9e5b01da0c3a7582d754c075599c67c2f7012d [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer509f7722012-12-07 17:31:37 -08002
Kyösti Mälkki1d7541f2014-02-17 21:34:42 +02003#ifndef CONSOLE_UART_H
4#define CONSOLE_UART_H
Stefan Reinauer509f7722012-12-07 17:31:37 -08005
Kyösti Mälkki0567c912014-02-14 10:31:38 +02006#include <stdint.h>
7
Kyösti Mälkki3ee16682014-02-17 19:37:52 +02008/* Return the clock frequency UART uses as reference clock for
9 * baudrate generator. */
10unsigned int uart_platform_refclk(void);
11
Julius Wernercd49cce2019-03-05 16:53:33 -080012#if CONFIG(UART_OVERRIDE_BAUDRATE)
Julien Viard de Galbert235daa42018-02-20 11:45:48 +010013/* Return the baudrate, define this in your platform when using the above
14 configuration. */
15unsigned int get_uart_baudrate(void);
16#else
17static inline unsigned int get_uart_baudrate(void)
18{
19 return CONFIG_TTYS0_BAUD;
20}
21#endif
22
Bryant Ou0ee920b2020-09-14 23:41:41 -070023#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 */
27unsigned int get_uart_for_console(void);
28#else
29static inline unsigned int get_uart_for_console(void)
30{
31 return CONFIG_UART_FOR_CONSOLE;
32}
33#endif
34
Kyösti Mälkki3ee16682014-02-17 19:37:52 +020035/* Returns the divisor value for a given baudrate.
36 * The formula to satisfy is:
37 * refclk / divisor = baudrate * oversample
38 */
39unsigned int uart_baudrate_divisor(unsigned int baudrate,
40 unsigned int refclk, unsigned int oversample);
41
Lee Leahy14876212016-05-04 13:13:20 -070042/* Returns the oversample divisor multiplied by any other divisors that act
43 * on the input clock
44 */
45unsigned int uart_input_clock_divider(void);
Kyösti Mälkki3ee16682014-02-17 19:37:52 +020046
Julius Werner1732dcb2018-04-24 16:33:58 -070047/* Bitbang out one byte on an 8n1 UART through the output function set_tx(). */
48void uart_bitbang_tx_byte(unsigned char data, void (*set_tx)(int line_state));
49
Felix Helde3a12472020-09-11 15:47:09 +020050void uart_init(unsigned int idx);
51void uart_tx_byte(unsigned int idx, unsigned char data);
52void uart_tx_flush(unsigned int idx);
53unsigned char uart_rx_byte(unsigned int idx);
Stefan Reinauer509f7722012-12-07 17:31:37 -080054
Felix Helde3a12472020-09-11 15:47:09 +020055uintptr_t uart_platform_base(unsigned int idx);
David Hendricksfdcef1a2013-02-15 19:29:12 -080056
Felix Helde3a12472020-09-11 15:47:09 +020057static inline void *uart_platform_baseptr(unsigned int idx)
Kyösti Mälkkic2610a42014-02-24 20:51:30 +020058{
59 return (void *)uart_platform_base(idx);
60}
Kyösti Mälkkic2610a42014-02-24 20:51:30 +020061
Kyösti Mälkki2b95da02014-02-15 10:19:23 +020062void oxford_remap(unsigned int new_base);
Kyösti Mälkki1d7541f2014-02-17 21:34:42 +020063
Julius Wernercd49cce2019-03-05 16:53:33 -080064#define __CONSOLE_SERIAL_ENABLE__ (CONFIG(CONSOLE_SERIAL) && \
Arthur Heymansa2bc2542021-05-29 08:10:49 +020065 (ENV_BOOTBLOCK || ENV_SEPARATE_ROMSTAGE || ENV_RAMSTAGE || ENV_SEPARATE_VERSTAGE \
Julius Werner21a40532020-04-21 16:03:53 -070066 || ENV_POSTCAR || (ENV_SMM && CONFIG(DEBUG_SMI))))
Kyösti Mälkkif3390862014-02-26 15:19:04 +020067
68#if __CONSOLE_SERIAL_ENABLE__
Lee Leahy6a566d72017-03-07 17:45:12 -080069static inline void __uart_init(void)
70{
Bryant Ou0ee920b2020-09-14 23:41:41 -070071 uart_init(get_uart_for_console());
Lee Leahy6a566d72017-03-07 17:45:12 -080072}
73static inline void __uart_tx_byte(u8 data)
74{
Bryant Ou0ee920b2020-09-14 23:41:41 -070075 uart_tx_byte(get_uart_for_console(), data);
Lee Leahy6a566d72017-03-07 17:45:12 -080076}
77static inline void __uart_tx_flush(void)
78{
Bryant Ou0ee920b2020-09-14 23:41:41 -070079 uart_tx_flush(get_uart_for_console());
Lee Leahy6a566d72017-03-07 17:45:12 -080080}
Kyösti Mälkkif3390862014-02-26 15:19:04 +020081#else
82static inline void __uart_init(void) {}
83static inline void __uart_tx_byte(u8 data) {}
84static inline void __uart_tx_flush(void) {}
85#endif
86
Arthur Heymans2fba4762023-11-06 15:33:23 +010087#if CONFIG(GDB_STUB) && (ENV_ROMSTAGE_OR_BEFORE || ENV_RAMSTAGE)
Elyes HAOUAS251514d2019-01-23 11:36:44 +010088#define CONF_UART_FOR_GDB CONFIG_UART_FOR_CONSOLE
89static inline void __gdb_hw_init(void) { uart_init(CONF_UART_FOR_GDB); }
Lee Leahy6a566d72017-03-07 17:45:12 -080090static inline void __gdb_tx_byte(u8 data)
91{
Elyes HAOUAS251514d2019-01-23 11:36:44 +010092 uart_tx_byte(CONF_UART_FOR_GDB, data);
Lee Leahy6a566d72017-03-07 17:45:12 -080093}
Elyes HAOUAS251514d2019-01-23 11:36:44 +010094static inline void __gdb_tx_flush(void) { uart_tx_flush(CONF_UART_FOR_GDB); }
Lee Leahy6a566d72017-03-07 17:45:12 -080095static inline u8 __gdb_rx_byte(void)
96{
Elyes HAOUAS251514d2019-01-23 11:36:44 +010097 return uart_rx_byte(CONF_UART_FOR_GDB);
Lee Leahy6a566d72017-03-07 17:45:12 -080098}
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +030099#endif
100
Kyösti Mälkki1d7541f2014-02-17 21:34:42 +0200101#endif /* CONSOLE_UART_H */