xref: /freebsd/sys/dev/uart/uart_dev_ti8250.c (revision 353e4c5a068d06b0d6dcfa9eb736ecb16e9eae45)
1aef60d8cSIan Lepore /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4aef60d8cSIan Lepore  * Copyright (c) 2013 Ian Lepore
5aef60d8cSIan Lepore  * All rights reserved.
6aef60d8cSIan Lepore  *
7aef60d8cSIan Lepore  * Redistribution and use in source and binary forms, with or without
8aef60d8cSIan Lepore  * modification, are permitted provided that the following conditions
9aef60d8cSIan Lepore  * are met:
10aef60d8cSIan Lepore  *
11aef60d8cSIan Lepore  * 1. Redistributions of source code must retain the above copyright
12aef60d8cSIan Lepore  *    notice, this list of conditions and the following disclaimer.
13aef60d8cSIan Lepore  * 2. Redistributions in binary form must reproduce the above copyright
14aef60d8cSIan Lepore  *    notice, this list of conditions and the following disclaimer in the
15aef60d8cSIan Lepore  *    documentation and/or other materials provided with the distribution.
16aef60d8cSIan Lepore  *
17aef60d8cSIan Lepore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18aef60d8cSIan Lepore  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19aef60d8cSIan Lepore  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20aef60d8cSIan Lepore  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21aef60d8cSIan Lepore  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22aef60d8cSIan Lepore  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23aef60d8cSIan Lepore  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24aef60d8cSIan Lepore  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25aef60d8cSIan Lepore  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26aef60d8cSIan Lepore  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27aef60d8cSIan Lepore  */
28aef60d8cSIan Lepore 
29aef60d8cSIan Lepore #include "opt_platform.h"
30aef60d8cSIan Lepore 
31aef60d8cSIan Lepore #include <sys/param.h>
32aef60d8cSIan Lepore #include <sys/systm.h>
33aef60d8cSIan Lepore #include <sys/bus.h>
34aef60d8cSIan Lepore #include <sys/conf.h>
35aef60d8cSIan Lepore #include <sys/kernel.h>
36aef60d8cSIan Lepore #include <sys/sysctl.h>
37aef60d8cSIan Lepore #include <machine/bus.h>
38aef60d8cSIan Lepore 
39aef60d8cSIan Lepore #include <dev/fdt/fdt_common.h>
40aef60d8cSIan Lepore #include <dev/ofw/ofw_bus.h>
41aef60d8cSIan Lepore #include <dev/ofw/ofw_bus_subr.h>
42aef60d8cSIan Lepore 
43aef60d8cSIan Lepore #include <dev/uart/uart.h>
44aef60d8cSIan Lepore #include <dev/uart/uart_cpu.h>
453bb693afSIan Lepore #include <dev/uart/uart_cpu_fdt.h>
46aef60d8cSIan Lepore #include <dev/uart/uart_bus.h>
47aef60d8cSIan Lepore #include <dev/uart/uart_dev_ns8250.h>
48aef60d8cSIan Lepore 
490050ea24SMichal Meloun #include <arm/ti/ti_sysc.h>
500050ea24SMichal Meloun 
51aef60d8cSIan Lepore #include "uart_if.h"
52aef60d8cSIan Lepore 
53aef60d8cSIan Lepore /*
54aef60d8cSIan Lepore  * High-level UART interface.
55aef60d8cSIan Lepore  */
56aef60d8cSIan Lepore struct ti8250_softc {
57aef60d8cSIan Lepore 	struct ns8250_softc ns8250_base;
58aef60d8cSIan Lepore 	/*uint32_t	mystuff;*/
59aef60d8cSIan Lepore };
60aef60d8cSIan Lepore 
61aef60d8cSIan Lepore #define	MDR1_REG		8
62aef60d8cSIan Lepore #define	  MDR1_MODE_UART	0
63aef60d8cSIan Lepore #define	  MDR1_MODE_DISABLE	7
64aef60d8cSIan Lepore #define	SYSCC_REG		15
65aef60d8cSIan Lepore #define	  SYSCC_SOFTRESET	(1 << 1)
66aef60d8cSIan Lepore #define	SYSS_REG		16
67aef60d8cSIan Lepore #define	  SYSS_STATUS_RESETDONE	(1 << 0)
68aef60d8cSIan Lepore 
69aef60d8cSIan Lepore static int
ti8250_bus_probe(struct uart_softc * sc)70aef60d8cSIan Lepore ti8250_bus_probe(struct uart_softc *sc)
71aef60d8cSIan Lepore {
72aef60d8cSIan Lepore 	int status;
73aef60d8cSIan Lepore 
740050ea24SMichal Meloun 	if ((status = ti_sysc_clock_enable(device_get_parent(sc->sc_dev))) != 0)
75aef60d8cSIan Lepore 		return (status);
76aef60d8cSIan Lepore 
77aef60d8cSIan Lepore 	/*
78aef60d8cSIan Lepore 	 * Set the hardware to disabled mode, do a full device reset, then set
79aef60d8cSIan Lepore 	 * it to uart mode.  Most devices will be reset-and-disabled already,
80aef60d8cSIan Lepore 	 * but you never know what a bootloader might have done.
81aef60d8cSIan Lepore 	 */
82aef60d8cSIan Lepore 	uart_setreg(&sc->sc_bas, MDR1_REG, MDR1_MODE_DISABLE);
83aef60d8cSIan Lepore 	uart_setreg(&sc->sc_bas, SYSCC_REG, SYSCC_SOFTRESET);
84aef60d8cSIan Lepore 	while (uart_getreg(&sc->sc_bas, SYSS_REG) & SYSS_STATUS_RESETDONE)
85aef60d8cSIan Lepore 		continue;
86aef60d8cSIan Lepore 	uart_setreg(&sc->sc_bas, MDR1_REG, MDR1_MODE_UART);
87aef60d8cSIan Lepore 
88aef60d8cSIan Lepore 	status = ns8250_bus_probe(sc);
89aef60d8cSIan Lepore 	if (status == 0)
90aef60d8cSIan Lepore 		device_set_desc(sc->sc_dev, "TI UART (16550 compatible)");
91aef60d8cSIan Lepore 
92aef60d8cSIan Lepore 	return (status);
93aef60d8cSIan Lepore }
94aef60d8cSIan Lepore 
95aef60d8cSIan Lepore static kobj_method_t ti8250_methods[] = {
96aef60d8cSIan Lepore 	KOBJMETHOD(uart_probe,		ti8250_bus_probe),
97aef60d8cSIan Lepore 
98aef60d8cSIan Lepore 	KOBJMETHOD(uart_attach,		ns8250_bus_attach),
99aef60d8cSIan Lepore 	KOBJMETHOD(uart_detach,		ns8250_bus_detach),
100aef60d8cSIan Lepore 	KOBJMETHOD(uart_flush,		ns8250_bus_flush),
101aef60d8cSIan Lepore 	KOBJMETHOD(uart_getsig,		ns8250_bus_getsig),
102aef60d8cSIan Lepore 	KOBJMETHOD(uart_ioctl,		ns8250_bus_ioctl),
103aef60d8cSIan Lepore 	KOBJMETHOD(uart_ipend,		ns8250_bus_ipend),
104aef60d8cSIan Lepore 	KOBJMETHOD(uart_param,		ns8250_bus_param),
105aef60d8cSIan Lepore 	KOBJMETHOD(uart_receive,	ns8250_bus_receive),
106aef60d8cSIan Lepore 	KOBJMETHOD(uart_setsig,		ns8250_bus_setsig),
107aef60d8cSIan Lepore 	KOBJMETHOD(uart_transmit,	ns8250_bus_transmit),
108*353e4c5aSMarius Strobl 	KOBJMETHOD(uart_txbusy,		ns8250_bus_txbusy),
109aef60d8cSIan Lepore 	KOBJMETHOD_END
110aef60d8cSIan Lepore };
111aef60d8cSIan Lepore 
1123bb693afSIan Lepore static struct uart_class uart_ti8250_class = {
113aef60d8cSIan Lepore 	"ti8250",
114aef60d8cSIan Lepore 	ti8250_methods,
115aef60d8cSIan Lepore 	sizeof(struct ti8250_softc),
116aef60d8cSIan Lepore 	.uc_ops = &uart_ns8250_ops,
117aef60d8cSIan Lepore 	.uc_range = 0x88,
118405ada37SAndrew Turner 	.uc_rclk = 48000000,
1195b03aba6SOleksandr Tymoshenko 	.uc_rshift = 2
120aef60d8cSIan Lepore };
1213bb693afSIan Lepore static struct ofw_compat_data compat_data[] = {
1223bb693afSIan Lepore 	{"ti,ns16550",		(uintptr_t)&uart_ti8250_class},
1235b03aba6SOleksandr Tymoshenko 	{"ti,omap3-uart",	(uintptr_t)&uart_ti8250_class},
1245b03aba6SOleksandr Tymoshenko 	{"ti,omap4-uart",	(uintptr_t)&uart_ti8250_class},
1253bb693afSIan Lepore 	{NULL,			(uintptr_t)NULL},
1263bb693afSIan Lepore };
1273bb693afSIan Lepore UART_FDT_CLASS_AND_DEVICE(compat_data);
128