tegra_com.c revision 1.8.8.2 1 1.8.8.2 jdolecek /* $NetBSD: tegra_com.c,v 1.8.8.2 2017/12/03 11:35:54 jdolecek Exp $ */
2 1.8.8.2 jdolecek
3 1.8.8.2 jdolecek /*-
4 1.8.8.2 jdolecek * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.8.8.2 jdolecek * All rights reserved.
6 1.8.8.2 jdolecek *
7 1.8.8.2 jdolecek * This code is derived from software contributed to The NetBSD Foundation
8 1.8.8.2 jdolecek * by Matt Thomas of 3am Software Foundry.
9 1.8.8.2 jdolecek *
10 1.8.8.2 jdolecek * Redistribution and use in source and binary forms, with or without
11 1.8.8.2 jdolecek * modification, are permitted provided that the following conditions
12 1.8.8.2 jdolecek * are met:
13 1.8.8.2 jdolecek * 1. Redistributions of source code must retain the above copyright
14 1.8.8.2 jdolecek * notice, this list of conditions and the following disclaimer.
15 1.8.8.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
16 1.8.8.2 jdolecek * notice, this list of conditions and the following disclaimer in the
17 1.8.8.2 jdolecek * documentation and/or other materials provided with the distribution.
18 1.8.8.2 jdolecek *
19 1.8.8.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.8.8.2 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.8.8.2 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.8.8.2 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.8.8.2 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.8.8.2 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.8.8.2 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.8.8.2 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.8.8.2 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.8.8.2 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.8.8.2 jdolecek * POSSIBILITY OF SUCH DAMAGE.
30 1.8.8.2 jdolecek */
31 1.8.8.2 jdolecek
32 1.8.8.2 jdolecek #include <sys/cdefs.h>
33 1.8.8.2 jdolecek
34 1.8.8.2 jdolecek __KERNEL_RCSID(1, "$NetBSD: tegra_com.c,v 1.8.8.2 2017/12/03 11:35:54 jdolecek Exp $");
35 1.8.8.2 jdolecek
36 1.8.8.2 jdolecek #include <sys/param.h>
37 1.8.8.2 jdolecek #include <sys/bus.h>
38 1.8.8.2 jdolecek #include <sys/device.h>
39 1.8.8.2 jdolecek #include <sys/intr.h>
40 1.8.8.2 jdolecek #include <sys/systm.h>
41 1.8.8.2 jdolecek #include <sys/time.h>
42 1.8.8.2 jdolecek #include <sys/termios.h>
43 1.8.8.2 jdolecek
44 1.8.8.2 jdolecek #include <arm/nvidia/tegra_reg.h>
45 1.8.8.2 jdolecek #include <arm/nvidia/tegra_var.h>
46 1.8.8.2 jdolecek
47 1.8.8.2 jdolecek #include <dev/ic/comvar.h>
48 1.8.8.2 jdolecek
49 1.8.8.2 jdolecek #include <dev/fdt/fdtvar.h>
50 1.8.8.2 jdolecek
51 1.8.8.2 jdolecek static int tegra_com_match(device_t, cfdata_t, void *);
52 1.8.8.2 jdolecek static void tegra_com_attach(device_t, device_t, void *);
53 1.8.8.2 jdolecek
54 1.8.8.2 jdolecek static const char * const compatible[] = {
55 1.8.8.2 jdolecek "nvidia,tegra210-uart",
56 1.8.8.2 jdolecek "nvidia,tegra124-uart",
57 1.8.8.2 jdolecek "nvidia,tegra20-uart",
58 1.8.8.2 jdolecek NULL
59 1.8.8.2 jdolecek };
60 1.8.8.2 jdolecek
61 1.8.8.2 jdolecek struct tegra_com_softc {
62 1.8.8.2 jdolecek struct com_softc tsc_sc;
63 1.8.8.2 jdolecek void *tsc_ih;
64 1.8.8.2 jdolecek
65 1.8.8.2 jdolecek struct clk *tsc_clk;
66 1.8.8.2 jdolecek struct fdtbus_reset *tsc_rst;
67 1.8.8.2 jdolecek };
68 1.8.8.2 jdolecek
69 1.8.8.2 jdolecek CFATTACH_DECL_NEW(tegra_com, sizeof(struct tegra_com_softc),
70 1.8.8.2 jdolecek tegra_com_match, tegra_com_attach, NULL, NULL);
71 1.8.8.2 jdolecek
72 1.8.8.2 jdolecek static int
73 1.8.8.2 jdolecek tegra_com_match(device_t parent, cfdata_t cf, void *aux)
74 1.8.8.2 jdolecek {
75 1.8.8.2 jdolecek struct fdt_attach_args * const faa = aux;
76 1.8.8.2 jdolecek
77 1.8.8.2 jdolecek return of_match_compatible(faa->faa_phandle, compatible);
78 1.8.8.2 jdolecek }
79 1.8.8.2 jdolecek
80 1.8.8.2 jdolecek static void
81 1.8.8.2 jdolecek tegra_com_attach(device_t parent, device_t self, void *aux)
82 1.8.8.2 jdolecek {
83 1.8.8.2 jdolecek struct tegra_com_softc * const tsc = device_private(self);
84 1.8.8.2 jdolecek struct com_softc * const sc = &tsc->tsc_sc;
85 1.8.8.2 jdolecek struct fdt_attach_args * const faa = aux;
86 1.8.8.2 jdolecek bus_space_handle_t bsh;
87 1.8.8.2 jdolecek bus_space_tag_t bst;
88 1.8.8.2 jdolecek char intrstr[128];
89 1.8.8.2 jdolecek bus_addr_t addr;
90 1.8.8.2 jdolecek bus_size_t size;
91 1.8.8.2 jdolecek u_int reg_shift;
92 1.8.8.2 jdolecek int error;
93 1.8.8.2 jdolecek
94 1.8.8.2 jdolecek if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
95 1.8.8.2 jdolecek aprint_error(": couldn't get registers\n");
96 1.8.8.2 jdolecek return;
97 1.8.8.2 jdolecek }
98 1.8.8.2 jdolecek
99 1.8.8.2 jdolecek if (of_getprop_uint32(faa->faa_phandle, "reg-shift", ®_shift)) {
100 1.8.8.2 jdolecek /* missing or bad reg-shift property, assume 2 */
101 1.8.8.2 jdolecek bst = faa->faa_a4x_bst;
102 1.8.8.2 jdolecek } else {
103 1.8.8.2 jdolecek if (reg_shift == 2) {
104 1.8.8.2 jdolecek bst = faa->faa_a4x_bst;
105 1.8.8.2 jdolecek } else if (reg_shift == 0) {
106 1.8.8.2 jdolecek bst = faa->faa_bst;
107 1.8.8.2 jdolecek } else {
108 1.8.8.2 jdolecek aprint_error(": unsupported reg-shift value %d\n",
109 1.8.8.2 jdolecek reg_shift);
110 1.8.8.2 jdolecek return;
111 1.8.8.2 jdolecek }
112 1.8.8.2 jdolecek }
113 1.8.8.2 jdolecek
114 1.8.8.2 jdolecek sc->sc_dev = self;
115 1.8.8.2 jdolecek
116 1.8.8.2 jdolecek tsc->tsc_clk = fdtbus_clock_get_index(faa->faa_phandle, 0);
117 1.8.8.2 jdolecek tsc->tsc_rst = fdtbus_reset_get(faa->faa_phandle, "serial");
118 1.8.8.2 jdolecek
119 1.8.8.2 jdolecek if (tsc->tsc_clk == NULL) {
120 1.8.8.2 jdolecek aprint_error(": couldn't get frequency\n");
121 1.8.8.2 jdolecek return;
122 1.8.8.2 jdolecek }
123 1.8.8.2 jdolecek
124 1.8.8.2 jdolecek sc->sc_frequency = clk_get_rate(tsc->tsc_clk);
125 1.8.8.2 jdolecek sc->sc_type = COM_TYPE_TEGRA;
126 1.8.8.2 jdolecek
127 1.8.8.2 jdolecek error = bus_space_map(bst, addr, size, 0, &bsh);
128 1.8.8.2 jdolecek if (error) {
129 1.8.8.2 jdolecek aprint_error(": couldn't map %#llx: %d", (uint64_t)addr, error);
130 1.8.8.2 jdolecek return;
131 1.8.8.2 jdolecek }
132 1.8.8.2 jdolecek
133 1.8.8.2 jdolecek COM_INIT_REGS(sc->sc_regs, bst, bsh, addr);
134 1.8.8.2 jdolecek
135 1.8.8.2 jdolecek com_attach_subr(sc);
136 1.8.8.2 jdolecek aprint_naive("\n");
137 1.8.8.2 jdolecek
138 1.8.8.2 jdolecek if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
139 1.8.8.2 jdolecek aprint_error_dev(self, "failed to decode interrupt\n");
140 1.8.8.2 jdolecek return;
141 1.8.8.2 jdolecek }
142 1.8.8.2 jdolecek
143 1.8.8.2 jdolecek tsc->tsc_ih = fdtbus_intr_establish(faa->faa_phandle, 0, IPL_SERIAL,
144 1.8.8.2 jdolecek FDT_INTR_MPSAFE, comintr, sc);
145 1.8.8.2 jdolecek if (tsc->tsc_ih == NULL) {
146 1.8.8.2 jdolecek aprint_error_dev(self, "failed to establish interrupt on %s\n",
147 1.8.8.2 jdolecek intrstr);
148 1.8.8.2 jdolecek }
149 1.8.8.2 jdolecek aprint_normal_dev(self, "interrupting on %s\n", intrstr);
150 1.8.8.2 jdolecek }
151 1.8.8.2 jdolecek
152 1.8.8.2 jdolecek /*
153 1.8.8.2 jdolecek * Console support
154 1.8.8.2 jdolecek */
155 1.8.8.2 jdolecek
156 1.8.8.2 jdolecek static int
157 1.8.8.2 jdolecek tegra_com_console_match(int phandle)
158 1.8.8.2 jdolecek {
159 1.8.8.2 jdolecek return of_match_compatible(phandle, compatible);
160 1.8.8.2 jdolecek }
161 1.8.8.2 jdolecek
162 1.8.8.2 jdolecek static void
163 1.8.8.2 jdolecek tegra_com_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
164 1.8.8.2 jdolecek {
165 1.8.8.2 jdolecek const int phandle = faa->faa_phandle;
166 1.8.8.2 jdolecek bus_space_tag_t bst = faa->faa_a4x_bst;
167 1.8.8.2 jdolecek bus_addr_t addr;
168 1.8.8.2 jdolecek tcflag_t flags;
169 1.8.8.2 jdolecek int speed;
170 1.8.8.2 jdolecek
171 1.8.8.2 jdolecek fdtbus_get_reg(phandle, 0, &addr, NULL);
172 1.8.8.2 jdolecek speed = fdtbus_get_stdout_speed();
173 1.8.8.2 jdolecek if (speed < 0)
174 1.8.8.2 jdolecek speed = 115200; /* default */
175 1.8.8.2 jdolecek flags = fdtbus_get_stdout_flags();
176 1.8.8.2 jdolecek
177 1.8.8.2 jdolecek if (comcnattach(bst, addr, speed, uart_freq, COM_TYPE_TEGRA, flags))
178 1.8.8.2 jdolecek panic("Cannot initialize tegra com console");
179 1.8.8.2 jdolecek }
180 1.8.8.2 jdolecek
181 1.8.8.2 jdolecek static const struct fdt_console tegra_com_console = {
182 1.8.8.2 jdolecek .match = tegra_com_console_match,
183 1.8.8.2 jdolecek .consinit = tegra_com_console_consinit,
184 1.8.8.2 jdolecek };
185 1.8.8.2 jdolecek
186 1.8.8.2 jdolecek FDT_CONSOLE(tegra_com, &tegra_com_console);
187