18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  8250/16550-type serial ports prom_putchar()
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2010  Yoichi Yuasa <yuasa@linux-mips.org>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <linux/io.h>
88c2ecf20Sopenharmony_ci#include <linux/serial_core.h>
98c2ecf20Sopenharmony_ci#include <linux/serial_reg.h>
108c2ecf20Sopenharmony_ci#include <asm/setup.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistatic void __iomem *serial8250_base;
138c2ecf20Sopenharmony_cistatic unsigned int serial8250_reg_shift;
148c2ecf20Sopenharmony_cistatic unsigned int serial8250_tx_timeout;
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_civoid setup_8250_early_printk_port(unsigned long base, unsigned int reg_shift,
178c2ecf20Sopenharmony_ci				  unsigned int timeout)
188c2ecf20Sopenharmony_ci{
198c2ecf20Sopenharmony_ci	serial8250_base = (void __iomem *)base;
208c2ecf20Sopenharmony_ci	serial8250_reg_shift = reg_shift;
218c2ecf20Sopenharmony_ci	serial8250_tx_timeout = timeout;
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic inline u8 serial_in(int offset)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	return readb(serial8250_base + (offset << serial8250_reg_shift));
278c2ecf20Sopenharmony_ci}
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic inline void serial_out(int offset, char value)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	writeb(value, serial8250_base + (offset << serial8250_reg_shift));
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_civoid prom_putchar(char c)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	unsigned int timeout;
378c2ecf20Sopenharmony_ci	int status, bits;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	if (!serial8250_base)
408c2ecf20Sopenharmony_ci		return;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	timeout = serial8250_tx_timeout;
438c2ecf20Sopenharmony_ci	bits = UART_LSR_TEMT | UART_LSR_THRE;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	do {
468c2ecf20Sopenharmony_ci		status = serial_in(UART_LSR);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci		if (--timeout == 0)
498c2ecf20Sopenharmony_ci			break;
508c2ecf20Sopenharmony_ci	} while ((status & bits) != bits);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	if (timeout)
538c2ecf20Sopenharmony_ci		serial_out(UART_TX, c);
548c2ecf20Sopenharmony_ci}
55