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