1/*	$NetBSD: epcom.c,v 1.6 2022/09/05 14:14:42 tsutsui Exp $	*/
2
3/*
4 * Copyright (c) 2004 Jesse Off
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/*
30 * This file provides the cons_init() function and console I/O routines
31 * for boards that use the ep93xx ARM SoC UARTs
32 */
33
34#include <sys/types.h>
35#include <arm/ep93xx/epcomreg.h>
36#include <arm/ep93xx/ep93xxreg.h>
37#include <lib/libsa/stand.h>
38
39#include "board.h"
40
41#define SCADDR	(EP93XX_APB_HWBASE + EP93XX_APB_SYSCON)
42#define	EPCOM_READ(x)		*((volatile uint32_t *) (CONADDR + (EPCOM_ ## x)))
43#define	EPCOM_WRITE(x, v)	*((volatile uint32_t *) \
44					(CONADDR + (EPCOM_ ## x))) = (v)
45#define	SYSCON_READ(x)		*((volatile uint32_t *) \
46					(SCADDR + (EP93XX_SYSCON_ ## x)))
47#define	SYSCON_WRITE(x, v)	*((volatile uint32_t *) \
48					(SCADDR + (EP93XX_SYSCON_ ## x))) = (v)
49
50void
51cons_init(void)
52{
53	unsigned long baud, pwrcnt;
54
55	while(!ISSET(EPCOM_READ(Flag), Flag_TXFE));
56
57	/* Make UART base freq 7 MHz */
58	pwrcnt = SYSCON_READ(PwrCnt);
59	pwrcnt &= ~(PwrCnt_UARTBAUD);
60	SYSCON_WRITE(PwrCnt, pwrcnt);
61
62	baud = EPCOMSPEED2BRD(CONSPEED);
63	EPCOM_WRITE(LinCtrlLow, baud & 0xff);
64	EPCOM_WRITE(LinCtrlMid, baud >> 8);
65	EPCOM_WRITE(LinCtrlHigh, LinCtrlHigh_FEN|LinCtrlHigh_WLEN);
66}
67
68int
69getchar(void)
70{
71	while(!ISSET(EPCOM_READ(Flag), Flag_RXFE));
72	return (EPCOM_READ(Data) & 0xff);
73}
74
75void
76putchar(int c)
77{
78	while(ISSET(EPCOM_READ(Flag), Flag_TXFF));
79
80	if (c == '\n') {
81		while(!ISSET(EPCOM_READ(Flag), Flag_TXFE));
82		EPCOM_WRITE(Data, '\r');
83	}
84
85	EPCOM_WRITE(Data, c);
86}
87