1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2013 Ian Lepore
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 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "opt_platform.h"
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/bus.h>
37#include <sys/conf.h>
38#include <sys/kernel.h>
39#include <sys/sysctl.h>
40#include <machine/bus.h>
41
42#include <arm/ti/ti_prcm.h>
43#include <arm/ti/ti_hwmods.h>
44
45#include <dev/fdt/fdt_common.h>
46#include <dev/ofw/ofw_bus.h>
47#include <dev/ofw/ofw_bus_subr.h>
48
49#include <dev/uart/uart.h>
50#include <dev/uart/uart_cpu.h>
51#include <dev/uart/uart_cpu_fdt.h>
52#include <dev/uart/uart_bus.h>
53#include <dev/uart/uart_dev_ns8250.h>
54
55#include "uart_if.h"
56
57/*
58 * High-level UART interface.
59 */
60struct ti8250_softc {
61	struct ns8250_softc ns8250_base;
62	/*uint32_t	mystuff;*/
63};
64
65#define	MDR1_REG		8
66#define	  MDR1_MODE_UART	0
67#define	  MDR1_MODE_DISABLE	7
68#define	SYSCC_REG		15
69#define	  SYSCC_SOFTRESET	(1 << 1)
70#define	SYSS_REG		16
71#define	  SYSS_STATUS_RESETDONE	(1 << 0)
72
73static int
74ti8250_bus_probe(struct uart_softc *sc)
75{
76	int status;
77	clk_ident_t clkid;
78
79	/* Enable clocks for this device.  We can't continue if that fails.  */
80	clkid = ti_hwmods_get_clock(sc->sc_dev);
81	if (clkid == INVALID_CLK_IDENT) {
82		device_printf(sc->sc_dev,
83		    "failed to get clock based on hwmods\n");
84		clkid = UART1_CLK + device_get_unit(sc->sc_dev);
85	}
86	if ((status = ti_prcm_clk_enable(clkid)) != 0)
87		return (status);
88
89	/*
90	 * Set the hardware to disabled mode, do a full device reset, then set
91	 * it to uart mode.  Most devices will be reset-and-disabled already,
92	 * but you never know what a bootloader might have done.
93	 */
94	uart_setreg(&sc->sc_bas, MDR1_REG, MDR1_MODE_DISABLE);
95	uart_setreg(&sc->sc_bas, SYSCC_REG, SYSCC_SOFTRESET);
96	while (uart_getreg(&sc->sc_bas, SYSS_REG) & SYSS_STATUS_RESETDONE)
97		continue;
98	uart_setreg(&sc->sc_bas, MDR1_REG, MDR1_MODE_UART);
99
100	status = ns8250_bus_probe(sc);
101	if (status == 0)
102		device_set_desc(sc->sc_dev, "TI UART (16550 compatible)");
103
104	return (status);
105}
106
107static kobj_method_t ti8250_methods[] = {
108	KOBJMETHOD(uart_probe,		ti8250_bus_probe),
109
110	KOBJMETHOD(uart_attach,		ns8250_bus_attach),
111	KOBJMETHOD(uart_detach,		ns8250_bus_detach),
112	KOBJMETHOD(uart_flush,		ns8250_bus_flush),
113	KOBJMETHOD(uart_getsig,		ns8250_bus_getsig),
114	KOBJMETHOD(uart_ioctl,		ns8250_bus_ioctl),
115	KOBJMETHOD(uart_ipend,		ns8250_bus_ipend),
116	KOBJMETHOD(uart_param,		ns8250_bus_param),
117	KOBJMETHOD(uart_receive,	ns8250_bus_receive),
118	KOBJMETHOD(uart_setsig,		ns8250_bus_setsig),
119	KOBJMETHOD(uart_transmit,	ns8250_bus_transmit),
120	KOBJMETHOD_END
121};
122
123static struct uart_class uart_ti8250_class = {
124	"ti8250",
125	ti8250_methods,
126	sizeof(struct ti8250_softc),
127	.uc_ops = &uart_ns8250_ops,
128	.uc_range = 0x88,
129	.uc_rclk = 48000000,
130	.uc_rshift = 2
131};
132static struct ofw_compat_data compat_data[] = {
133	{"ti,ns16550",		(uintptr_t)&uart_ti8250_class},
134	{"ti,omap3-uart",	(uintptr_t)&uart_ti8250_class},
135	{"ti,omap4-uart",	(uintptr_t)&uart_ti8250_class},
136	{NULL,			(uintptr_t)NULL},
137};
138UART_FDT_CLASS_AND_DEVICE(compat_data);
139