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