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