18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2016 IBM Corporation.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include "ops.h"
78c2ecf20Sopenharmony_ci#include "stdio.h"
88c2ecf20Sopenharmony_ci#include "io.h"
98c2ecf20Sopenharmony_ci#include <libfdt.h>
108c2ecf20Sopenharmony_ci#include "../include/asm/opal-api.h"
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/* Global OPAL struct used by opal-call.S */
138c2ecf20Sopenharmony_cistruct opal {
148c2ecf20Sopenharmony_ci	u64 base;
158c2ecf20Sopenharmony_ci	u64 entry;
168c2ecf20Sopenharmony_ci} opal;
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistatic u32 opal_con_id;
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* see opal-wrappers.S */
218c2ecf20Sopenharmony_ciint64_t opal_console_write(int64_t term_number, u64 *length, const u8 *buffer);
228c2ecf20Sopenharmony_ciint64_t opal_console_read(int64_t term_number, uint64_t *length, u8 *buffer);
238c2ecf20Sopenharmony_ciint64_t opal_console_write_buffer_space(uint64_t term_number, uint64_t *length);
248c2ecf20Sopenharmony_ciint64_t opal_console_flush(uint64_t term_number);
258c2ecf20Sopenharmony_ciint64_t opal_poll_events(uint64_t *outstanding_event_mask);
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_civoid opal_kentry(unsigned long fdt_addr, void *vmlinux_addr);
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic int opal_con_open(void)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	/*
328c2ecf20Sopenharmony_ci	 * When OPAL loads the boot kernel it stashes the OPAL base and entry
338c2ecf20Sopenharmony_ci	 * address in r8 and r9 so the kernel can use the OPAL console
348c2ecf20Sopenharmony_ci	 * before unflattening the devicetree. While executing the wrapper will
358c2ecf20Sopenharmony_ci	 * probably trash r8 and r9 so this kentry hook restores them before
368c2ecf20Sopenharmony_ci	 * entering the decompressed kernel.
378c2ecf20Sopenharmony_ci	 */
388c2ecf20Sopenharmony_ci	platform_ops.kentry = opal_kentry;
398c2ecf20Sopenharmony_ci	return 0;
408c2ecf20Sopenharmony_ci}
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic void opal_con_putc(unsigned char c)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	int64_t rc;
458c2ecf20Sopenharmony_ci	uint64_t olen, len;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	do {
488c2ecf20Sopenharmony_ci		rc = opal_console_write_buffer_space(opal_con_id, &olen);
498c2ecf20Sopenharmony_ci		len = be64_to_cpu(olen);
508c2ecf20Sopenharmony_ci		if (rc)
518c2ecf20Sopenharmony_ci			return;
528c2ecf20Sopenharmony_ci		opal_poll_events(NULL);
538c2ecf20Sopenharmony_ci	} while (len < 1);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	olen = cpu_to_be64(1);
578c2ecf20Sopenharmony_ci	opal_console_write(opal_con_id, &olen, &c);
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic void opal_con_close(void)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	opal_console_flush(opal_con_id);
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic void opal_init(void)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	void *opal_node;
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	opal_node = finddevice("/ibm,opal");
708c2ecf20Sopenharmony_ci	if (!opal_node)
718c2ecf20Sopenharmony_ci		return;
728c2ecf20Sopenharmony_ci	if (getprop(opal_node, "opal-base-address", &opal.base, sizeof(u64)) < 0)
738c2ecf20Sopenharmony_ci		return;
748c2ecf20Sopenharmony_ci	opal.base = be64_to_cpu(opal.base);
758c2ecf20Sopenharmony_ci	if (getprop(opal_node, "opal-entry-address", &opal.entry, sizeof(u64)) < 0)
768c2ecf20Sopenharmony_ci		return;
778c2ecf20Sopenharmony_ci	opal.entry = be64_to_cpu(opal.entry);
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ciint opal_console_init(void *devp, struct serial_console_data *scdp)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	opal_init();
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	if (devp) {
858c2ecf20Sopenharmony_ci		int n = getprop(devp, "reg", &opal_con_id, sizeof(u32));
868c2ecf20Sopenharmony_ci		if (n != sizeof(u32))
878c2ecf20Sopenharmony_ci			return -1;
888c2ecf20Sopenharmony_ci		opal_con_id = be32_to_cpu(opal_con_id);
898c2ecf20Sopenharmony_ci	} else
908c2ecf20Sopenharmony_ci		opal_con_id = 0;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	scdp->open = opal_con_open;
938c2ecf20Sopenharmony_ci	scdp->putc = opal_con_putc;
948c2ecf20Sopenharmony_ci	scdp->close = opal_con_close;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	return 0;
978c2ecf20Sopenharmony_ci}
98