xref: /freebsd/sys/dev/uart/uart_dev_ti8250.c (revision cc16dea626cf2fc80cde667ac4798065108e596c)
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 
42 #include <dev/fdt/fdt_common.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 
46 #include <dev/uart/uart.h>
47 #include <dev/uart/uart_cpu.h>
48 #include <dev/uart/uart_bus.h>
49 #include <dev/uart/uart_dev_ns8250.h>
50 
51 #include "uart_if.h"
52 
53 /*
54  * High-level UART interface.
55  */
56 struct ti8250_softc {
57 	struct ns8250_softc ns8250_base;
58 	/*uint32_t	mystuff;*/
59 };
60 
61 #define	MDR1_REG		8
62 #define	  MDR1_MODE_UART	0
63 #define	  MDR1_MODE_DISABLE	7
64 #define	SYSCC_REG		15
65 #define	  SYSCC_SOFTRESET	(1 << 1)
66 #define	SYSS_REG		16
67 #define	  SYSS_STATUS_RESETDONE	(1 << 0)
68 
69 static int
70 ti8250_bus_probe(struct uart_softc *sc)
71 {
72 	int status;
73 	int devid;
74 	clk_ident_t clkid;
75 	pcell_t prop;
76 	phandle_t node;
77 
78 	/*
79 	 * Get the device id from FDT.  If it's not there we can't turn on the
80 	 * right clocks, so bail, unless we're doing unit 0.  We assume that's
81 	 * the serial console, whose clock isn't controllable anyway, and we
82 	 * sure don't want to break the console because of a config error.
83 	 */
84 	node = ofw_bus_get_node(sc->sc_dev);
85 	if ((OF_getprop(node, "uart-device-id", &prop, sizeof(prop))) <= 0) {
86 		device_printf(sc->sc_dev,
87 		    "missing uart-device-id attribute in FDT\n");
88 		if (device_get_unit(sc->sc_dev) != 0)
89 			return (ENXIO);
90 		devid = 0;
91 	} else
92 		devid = fdt32_to_cpu(prop);
93 
94 	/* Enable clocks for this device.  We can't continue if that fails.  */
95 	clkid = UART0_CLK + devid;
96 	if ((status = ti_prcm_clk_enable(clkid)) != 0)
97 		return (status);
98 
99 	/*
100 	 * Set the hardware to disabled mode, do a full device reset, then set
101 	 * it to uart mode.  Most devices will be reset-and-disabled already,
102 	 * but you never know what a bootloader might have done.
103 	 */
104 	uart_setreg(&sc->sc_bas, MDR1_REG, MDR1_MODE_DISABLE);
105 	uart_setreg(&sc->sc_bas, SYSCC_REG, SYSCC_SOFTRESET);
106 	while (uart_getreg(&sc->sc_bas, SYSS_REG) & SYSS_STATUS_RESETDONE)
107 		continue;
108 	uart_setreg(&sc->sc_bas, MDR1_REG, MDR1_MODE_UART);
109 
110 	status = ns8250_bus_probe(sc);
111 	if (status == 0)
112 		device_set_desc(sc->sc_dev, "TI UART (16550 compatible)");
113 
114 	return (status);
115 }
116 
117 static kobj_method_t ti8250_methods[] = {
118 	KOBJMETHOD(uart_probe,		ti8250_bus_probe),
119 
120 	KOBJMETHOD(uart_attach,		ns8250_bus_attach),
121 	KOBJMETHOD(uart_detach,		ns8250_bus_detach),
122 	KOBJMETHOD(uart_flush,		ns8250_bus_flush),
123 	KOBJMETHOD(uart_getsig,		ns8250_bus_getsig),
124 	KOBJMETHOD(uart_ioctl,		ns8250_bus_ioctl),
125 	KOBJMETHOD(uart_ipend,		ns8250_bus_ipend),
126 	KOBJMETHOD(uart_param,		ns8250_bus_param),
127 	KOBJMETHOD(uart_receive,	ns8250_bus_receive),
128 	KOBJMETHOD(uart_setsig,		ns8250_bus_setsig),
129 	KOBJMETHOD(uart_transmit,	ns8250_bus_transmit),
130 	KOBJMETHOD_END
131 };
132 
133 struct uart_class uart_ti8250_class = {
134 	"ti8250",
135 	ti8250_methods,
136 	sizeof(struct ti8250_softc),
137 	.uc_ops = &uart_ns8250_ops,
138 	.uc_range = 0x88,
139 	.uc_rclk = 48000000
140 };
141 
142