dw_apb_uart.c revision 1.1
1/* $NetBSD: dw_apb_uart.c,v 1.1 2018/05/27 17:14:23 jmcneill Exp $ */
2
3/*-
4 * Copyright (c) 2017 Jared McNeill <jmcneill@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: dw_apb_uart.c,v 1.1 2018/05/27 17:14:23 jmcneill 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
45static int dw_apb_uart_match(device_t, cfdata_t, void *);
46static void dw_apb_uart_attach(device_t, device_t, void *);
47
48static const char * const compatible[] = {
49	"snps,dw-apb-uart",
50	NULL
51};
52
53struct dw_apb_uart_softc {
54	struct com_softc ssc_sc;
55	void *ssc_ih;
56
57	struct clk *ssc_clk;
58	struct fdtbus_reset *ssc_rst;
59};
60
61CFATTACH_DECL_NEW(dw_apb_uart, sizeof(struct dw_apb_uart_softc),
62	dw_apb_uart_match, dw_apb_uart_attach, NULL, NULL);
63
64static int
65dw_apb_uart_match(device_t parent, cfdata_t cf, void *aux)
66{
67	struct fdt_attach_args * const faa = aux;
68
69	return of_match_compatible(faa->faa_phandle, compatible);
70}
71
72static void
73dw_apb_uart_attach(device_t parent, device_t self, void *aux)
74{
75	struct dw_apb_uart_softc * const ssc = device_private(self);
76	struct com_softc * const sc = &ssc->ssc_sc;
77	struct fdt_attach_args * const faa = aux;
78	bus_space_handle_t bsh;
79	bus_space_tag_t bst;
80	char intrstr[128];
81	bus_addr_t addr;
82	bus_size_t size;
83	u_int reg_shift;
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	if (of_getprop_uint32(faa->faa_phandle, "reg-shift", &reg_shift)) {
92		/* missing or bad reg-shift property, assume 2 */
93		bst = faa->faa_a4x_bst;
94	} else {
95		if (reg_shift == 2) {
96			bst = faa->faa_a4x_bst;
97		} else if (reg_shift == 0) {
98			bst = faa->faa_bst;
99		} else {
100			aprint_error(": unsupported reg-shift value %d\n",
101			    reg_shift);
102			return;
103		}
104	}
105
106	sc->sc_dev = self;
107
108	ssc->ssc_clk = fdtbus_clock_get_index(faa->faa_phandle, 0);
109	if (ssc->ssc_clk == NULL) {
110		aprint_error(": couldn't get clock\n");
111		return;
112	}
113	if (clk_enable(ssc->ssc_clk) != 0) {
114		aprint_error(": couldn't enable clock\n");
115		return;
116	}
117
118	ssc->ssc_rst = fdtbus_reset_get_index(faa->faa_phandle, 0);
119	if (ssc->ssc_rst && fdtbus_reset_deassert(ssc->ssc_rst) != 0) {
120		aprint_error(": couldn't de-assert reset\n");
121		return;
122	}
123
124	sc->sc_frequency = clk_get_rate(ssc->ssc_clk);
125	sc->sc_type = COM_TYPE_DW_APB;
126
127	error = bus_space_map(bst, addr, size, 0, &bsh);
128	if (error) {
129		aprint_error(": couldn't map %#llx: %d", (uint64_t)addr, error);
130		return;
131	}
132
133	COM_INIT_REGS(sc->sc_regs, bst, bsh, addr);
134
135	com_attach_subr(sc);
136
137	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
138		aprint_error_dev(self, "failed to decode interrupt\n");
139		return;
140	}
141
142	ssc->ssc_ih = fdtbus_intr_establish(faa->faa_phandle, 0, IPL_SERIAL,
143	    FDT_INTR_MPSAFE, comintr, sc);
144	if (ssc->ssc_ih == NULL) {
145		aprint_error_dev(self, "failed to establish interrupt on %s\n",
146		    intrstr);
147	}
148	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
149}
150
151/*
152 * Console support
153 */
154
155static int
156dw_apb_uart_console_match(int phandle)
157{
158	return of_match_compatible(phandle, compatible);
159}
160
161static void
162dw_apb_uart_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
163{
164	const int phandle = faa->faa_phandle;
165	bus_space_tag_t bst = faa->faa_a4x_bst;
166	bus_addr_t addr;
167	tcflag_t flags;
168	int speed;
169
170	fdtbus_get_reg(phandle, 0, &addr, NULL);
171	speed = fdtbus_get_stdout_speed();
172	if (speed < 0)
173		speed = 115200;	/* default */
174	flags = fdtbus_get_stdout_flags();
175
176	if (comcnattach(bst, addr, speed, uart_freq, COM_TYPE_DW_APB, flags))
177		panic("Cannot initialize dw-apb-uart console");
178}
179
180static const struct fdt_console dw_apb_uart_console = {
181	.match = dw_apb_uart_console_match,
182	.consinit = dw_apb_uart_console_consinit,
183};
184
185FDT_CONSOLE(dw_apb_uart, &dw_apb_uart_console);
186