imx_com.c revision 1.1.2.2 1 1.1.2.2 thorpej /* $NetBSD: imx_com.c,v 1.1.2.2 2021/01/03 16:34:52 thorpej Exp $ */
2 1.1.2.2 thorpej /*-
3 1.1.2.2 thorpej * Copyright (c) 2019 Genetec Corporation. All rights reserved.
4 1.1.2.2 thorpej * Written by Hashimoto Kenichi for Genetec Corporation.
5 1.1.2.2 thorpej *
6 1.1.2.2 thorpej * Redistribution and use in source and binary forms, with or without
7 1.1.2.2 thorpej * modification, are permitted provided that the following conditions
8 1.1.2.2 thorpej * are met:
9 1.1.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
10 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer.
11 1.1.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
13 1.1.2.2 thorpej * documentation and/or other materials provided with the distribution.
14 1.1.2.2 thorpej *
15 1.1.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1.2.2 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1.2.2 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1.2.2 thorpej * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1.2.2 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 1.1.2.2 thorpej * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 1.1.2.2 thorpej * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 1.1.2.2 thorpej * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 1.1.2.2 thorpej * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1.2.2 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1.2.2 thorpej * SUCH DAMAGE.
26 1.1.2.2 thorpej */
27 1.1.2.2 thorpej
28 1.1.2.2 thorpej #include <sys/cdefs.h>
29 1.1.2.2 thorpej __KERNEL_RCSID(0, "$NetBSD: imx_com.c,v 1.1.2.2 2021/01/03 16:34:52 thorpej Exp $");
30 1.1.2.2 thorpej
31 1.1.2.2 thorpej #include "opt_fdt.h"
32 1.1.2.2 thorpej #include "opt_imxuart.h"
33 1.1.2.2 thorpej
34 1.1.2.2 thorpej #include <sys/param.h>
35 1.1.2.2 thorpej #include <sys/bus.h>
36 1.1.2.2 thorpej #include <sys/device.h>
37 1.1.2.2 thorpej
38 1.1.2.2 thorpej #include <dev/fdt/fdtvar.h>
39 1.1.2.2 thorpej
40 1.1.2.2 thorpej #include <arm/imx/imxuartreg.h>
41 1.1.2.2 thorpej #include <arm/imx/imxuartvar.h>
42 1.1.2.2 thorpej
43 1.1.2.2 thorpej static int imx_com_match(device_t, struct cfdata *, void *);
44 1.1.2.2 thorpej static void imx_com_attach(device_t, device_t, void *);
45 1.1.2.2 thorpej
46 1.1.2.2 thorpej CFATTACH_DECL_NEW(imx_com, sizeof(struct imxuart_softc),
47 1.1.2.2 thorpej imx_com_match, imx_com_attach, NULL, NULL);
48 1.1.2.2 thorpej
49 1.1.2.2 thorpej static const char * const compatible[] = {
50 1.1.2.2 thorpej "fsl,imx6q-uart",
51 1.1.2.2 thorpej NULL
52 1.1.2.2 thorpej };
53 1.1.2.2 thorpej
54 1.1.2.2 thorpej static int
55 1.1.2.2 thorpej imx_com_match(device_t parent, struct cfdata *cf, void *aux)
56 1.1.2.2 thorpej {
57 1.1.2.2 thorpej struct fdt_attach_args * const faa = aux;
58 1.1.2.2 thorpej
59 1.1.2.2 thorpej return of_match_compatible(faa->faa_phandle, compatible);
60 1.1.2.2 thorpej }
61 1.1.2.2 thorpej
62 1.1.2.2 thorpej static void
63 1.1.2.2 thorpej imx_com_attach(device_t parent, device_t self, void *aux)
64 1.1.2.2 thorpej {
65 1.1.2.2 thorpej struct imxuart_softc *sc = device_private(self);
66 1.1.2.2 thorpej struct imxuart_regs *regsp = &sc->sc_regs;
67 1.1.2.2 thorpej struct fdt_attach_args *faa = aux;
68 1.1.2.2 thorpej const int phandle = faa->faa_phandle;
69 1.1.2.2 thorpej bus_space_tag_t bst = faa->faa_bst;
70 1.1.2.2 thorpej bus_space_handle_t bsh;
71 1.1.2.2 thorpej char intrstr[128];
72 1.1.2.2 thorpej struct clk *per;
73 1.1.2.2 thorpej bus_addr_t addr;
74 1.1.2.2 thorpej bus_size_t size;
75 1.1.2.2 thorpej
76 1.1.2.2 thorpej if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
77 1.1.2.2 thorpej aprint_error(": couldn't get registers\n");
78 1.1.2.2 thorpej return;
79 1.1.2.2 thorpej }
80 1.1.2.2 thorpej
81 1.1.2.2 thorpej if (bus_space_map(bst, addr, size, 0, &bsh) != 0) {
82 1.1.2.2 thorpej aprint_error(": couldn't map registers\n");
83 1.1.2.2 thorpej return;
84 1.1.2.2 thorpej }
85 1.1.2.2 thorpej
86 1.1.2.2 thorpej if (fdtbus_clock_enable(phandle, "ipg", false) != 0) {
87 1.1.2.2 thorpej aprint_error(": couldn't enable ipg clock\n");
88 1.1.2.2 thorpej return;
89 1.1.2.2 thorpej }
90 1.1.2.2 thorpej
91 1.1.2.2 thorpej per = fdtbus_clock_get(phandle, "per");
92 1.1.2.2 thorpej if (per != NULL && clk_enable(per) != 0) {
93 1.1.2.2 thorpej aprint_error(": couldn't enable per clock\n");
94 1.1.2.2 thorpej return;
95 1.1.2.2 thorpej }
96 1.1.2.2 thorpej
97 1.1.2.2 thorpej sc->sc_dev = self;
98 1.1.2.2 thorpej regsp->ur_iot = bst;
99 1.1.2.2 thorpej regsp->ur_iobase = addr;
100 1.1.2.2 thorpej regsp->ur_ioh = bsh;
101 1.1.2.2 thorpej
102 1.1.2.2 thorpej if (per != NULL) {
103 1.1.2.2 thorpej aprint_normal(", %u Hz", clk_get_rate(per));
104 1.1.2.2 thorpej /* XXX */
105 1.1.2.2 thorpej imxuart_set_frequency(clk_get_rate(per), 2);
106 1.1.2.2 thorpej }
107 1.1.2.2 thorpej
108 1.1.2.2 thorpej if (imxuart_is_console(regsp->ur_iot, regsp->ur_iobase, ®sp->ur_ioh))
109 1.1.2.2 thorpej aprint_normal(" (console)");
110 1.1.2.2 thorpej
111 1.1.2.2 thorpej aprint_normal("\n");
112 1.1.2.2 thorpej
113 1.1.2.2 thorpej if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
114 1.1.2.2 thorpej aprint_error_dev(self, "failed to decode interrupt\n");
115 1.1.2.2 thorpej return;
116 1.1.2.2 thorpej }
117 1.1.2.2 thorpej
118 1.1.2.2 thorpej sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_SERIAL,
119 1.1.2.2 thorpej 0, imxuintr, sc);
120 1.1.2.2 thorpej if (sc->sc_ih == NULL) {
121 1.1.2.2 thorpej aprint_error_dev(self, "failed to establish interrupt\n");
122 1.1.2.2 thorpej return;
123 1.1.2.2 thorpej }
124 1.1.2.2 thorpej
125 1.1.2.2 thorpej aprint_normal_dev(self, "interrupting on %s\n", intrstr);
126 1.1.2.2 thorpej
127 1.1.2.2 thorpej imxuart_attach_subr(sc);
128 1.1.2.2 thorpej }
129 1.1.2.2 thorpej
130 1.1.2.2 thorpej /*
131 1.1.2.2 thorpej * Console support
132 1.1.2.2 thorpej */
133 1.1.2.2 thorpej
134 1.1.2.2 thorpej static int
135 1.1.2.2 thorpej imx_com_console_match(int phandle)
136 1.1.2.2 thorpej {
137 1.1.2.2 thorpej return of_match_compatible(phandle, compatible);
138 1.1.2.2 thorpej }
139 1.1.2.2 thorpej
140 1.1.2.2 thorpej static void
141 1.1.2.2 thorpej imx_com_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
142 1.1.2.2 thorpej {
143 1.1.2.2 thorpej const int phandle = faa->faa_phandle;
144 1.1.2.2 thorpej bus_space_tag_t bst = faa->faa_bst;
145 1.1.2.2 thorpej bus_addr_t addr;
146 1.1.2.2 thorpej bus_size_t size;
147 1.1.2.2 thorpej tcflag_t flags;
148 1.1.2.2 thorpej int speed;
149 1.1.2.2 thorpej
150 1.1.2.2 thorpej fdtbus_get_reg(phandle, 0, &addr, &size);
151 1.1.2.2 thorpej speed = fdtbus_get_stdout_speed();
152 1.1.2.2 thorpej if (speed < 0)
153 1.1.2.2 thorpej speed = 115200; /* default */
154 1.1.2.2 thorpej flags = fdtbus_get_stdout_flags();
155 1.1.2.2 thorpej
156 1.1.2.2 thorpej imxuart_set_frequency(uart_freq, 2);
157 1.1.2.2 thorpej if (imxuart_cnattach(bst, addr, speed, flags) != 0)
158 1.1.2.2 thorpej panic("cannot attach console UART");
159 1.1.2.2 thorpej }
160 1.1.2.2 thorpej
161 1.1.2.2 thorpej static const struct fdt_console imx_com_console = {
162 1.1.2.2 thorpej .match = imx_com_console_match,
163 1.1.2.2 thorpej .consinit = imx_com_console_consinit,
164 1.1.2.2 thorpej };
165 1.1.2.2 thorpej
166 1.1.2.2 thorpej FDT_CONSOLE(imx_com, &imx_com_console);
167