ti_com.c revision 1.1 1 /* $NetBSD: ti_com.c,v 1.1 2017/10/26 01:16:32 jakllsch Exp $ */
2
3 /*-
4 * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
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 THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND 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 THE FOUNDATION OR CONTRIBUTORS
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 #include <sys/cdefs.h>
30
31 __KERNEL_RCSID(1, "$NetBSD: ti_com.c,v 1.1 2017/10/26 01:16:32 jakllsch Exp $");
32
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/device.h>
36 #include <sys/intr.h>
37 #include <sys/systm.h>
38 #include <sys/time.h>
39 #include <sys/termios.h>
40
41 #include <dev/ic/comvar.h>
42
43 #include <dev/fdt/fdtvar.h>
44
45 static int ti_com_match(device_t, cfdata_t, void *);
46 static void ti_com_attach(device_t, device_t, void *);
47
48 static const char * const compatible[] = {
49 "ti,am3352-uart",
50 "ti,omap3-uart",
51 NULL
52 };
53
54 struct ti_com_softc {
55 struct com_softc ssc_sc;
56 void *ssc_ih;
57
58 struct clk *ssc_clk;
59 struct fdtbus_reset *ssc_rst;
60 };
61
62 CFATTACH_DECL_NEW(ti_com, sizeof(struct ti_com_softc),
63 ti_com_match, ti_com_attach, NULL, NULL);
64
65 static int
66 ti_com_match(device_t parent, cfdata_t cf, void *aux)
67 {
68 struct fdt_attach_args * const faa = aux;
69
70 return of_match_compatible(faa->faa_phandle, compatible);
71 }
72
73 static void
74 ti_com_attach(device_t parent, device_t self, void *aux)
75 {
76 struct ti_com_softc * const ssc = device_private(self);
77 struct com_softc * const sc = &ssc->ssc_sc;
78 struct fdt_attach_args * const faa = aux;
79 bus_space_handle_t bsh;
80 bus_space_tag_t bst;
81 char intrstr[128];
82 bus_addr_t addr;
83 bus_size_t size;
84 int error;
85
86 if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
87 aprint_error(": couldn't get registers\n");
88 return;
89 }
90
91 bst = faa->faa_a4x_bst;
92
93 sc->sc_dev = self;
94
95 ssc->ssc_clk = fdtbus_clock_get_index(faa->faa_phandle, 0);
96 ssc->ssc_rst = fdtbus_reset_get_index(faa->faa_phandle, 0);
97
98 #if 0
99 if (ssc->ssc_clk == NULL) {
100 aprint_error(": couldn't get frequency\n");
101 return;
102 }
103
104 sc->sc_frequency = clk_get_rate(ssc->ssc_clk);
105 #else
106 sc->sc_frequency = 48000000;
107 #endif
108 sc->sc_type = COM_TYPE_NORMAL;
109
110 error = bus_space_map(bst, addr, size, 0, &bsh);
111 if (error) {
112 aprint_error(": couldn't map %#llx: %d", (uint64_t)addr, error);
113 return;
114 }
115
116 COM_INIT_REGS(sc->sc_regs, bst, bsh, addr);
117
118 com_attach_subr(sc);
119 aprint_naive("\n");
120
121 if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
122 aprint_error_dev(self, "failed to decode interrupt\n");
123 return;
124 }
125
126 ssc->ssc_ih = fdtbus_intr_establish(faa->faa_phandle, 0, IPL_SERIAL,
127 FDT_INTR_MPSAFE, comintr, sc);
128 if (ssc->ssc_ih == NULL) {
129 aprint_error_dev(self, "failed to establish interrupt on %s\n",
130 intrstr);
131 }
132 aprint_normal_dev(self, "interrupting on %s\n", intrstr);
133 }
134
135 /*
136 * Console support
137 */
138
139 static int
140 ti_com_console_match(int phandle)
141 {
142 return of_match_compatible(phandle, compatible);
143 }
144
145 static void
146 ti_com_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
147 {
148 const int phandle = faa->faa_phandle;
149 bus_space_tag_t bst = faa->faa_a4x_bst;
150 bus_addr_t addr;
151 tcflag_t flags;
152 int speed;
153
154 fdtbus_get_reg(phandle, 0, &addr, NULL);
155 speed = fdtbus_get_stdout_speed();
156 if (speed < 0)
157 speed = 115200; /* default */
158 flags = fdtbus_get_stdout_flags();
159
160 if (comcnattach(bst, addr, speed, uart_freq, COM_TYPE_NORMAL, flags))
161 panic("Cannot initialize ti com console");
162 }
163
164 static const struct fdt_console ti_com_console = {
165 .match = ti_com_console_match,
166 .consinit = ti_com_console_consinit,
167 };
168
169 FDT_CONSOLE(ti_com, &ti_com_console);
170