Home | History | Annotate | Line # | Download | only in fdt
dw_apb_uart.c revision 1.12
      1  1.12    andvar /* $NetBSD: dw_apb_uart.c,v 1.12 2022/04/08 10:17:54 andvar Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17   1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20   1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.1  jmcneill #include <sys/cdefs.h>
     30   1.1  jmcneill 
     31  1.12    andvar __KERNEL_RCSID(1, "$NetBSD: dw_apb_uart.c,v 1.12 2022/04/08 10:17:54 andvar Exp $");
     32   1.1  jmcneill 
     33   1.1  jmcneill #include <sys/param.h>
     34   1.1  jmcneill #include <sys/bus.h>
     35   1.1  jmcneill #include <sys/device.h>
     36   1.1  jmcneill #include <sys/intr.h>
     37   1.1  jmcneill #include <sys/systm.h>
     38   1.1  jmcneill #include <sys/time.h>
     39   1.1  jmcneill #include <sys/termios.h>
     40   1.1  jmcneill 
     41   1.1  jmcneill #include <dev/ic/comvar.h>
     42   1.1  jmcneill 
     43   1.1  jmcneill #include <dev/fdt/fdtvar.h>
     44   1.1  jmcneill 
     45   1.1  jmcneill static int dw_apb_uart_match(device_t, cfdata_t, void *);
     46   1.1  jmcneill static void dw_apb_uart_attach(device_t, device_t, void *);
     47   1.1  jmcneill 
     48  1.10   thorpej static const struct device_compatible_entry compat_data[] = {
     49  1.10   thorpej 	{ .compat = "snps,dw-apb-uart" },
     50  1.10   thorpej 	DEVICE_COMPAT_EOL
     51   1.1  jmcneill };
     52   1.1  jmcneill 
     53   1.1  jmcneill struct dw_apb_uart_softc {
     54   1.1  jmcneill 	struct com_softc ssc_sc;
     55   1.1  jmcneill 	void *ssc_ih;
     56   1.1  jmcneill 
     57   1.1  jmcneill 	struct clk *ssc_clk;
     58   1.2  jmcneill 	struct clk *ssc_pclk;
     59   1.1  jmcneill 	struct fdtbus_reset *ssc_rst;
     60   1.1  jmcneill };
     61   1.1  jmcneill 
     62   1.1  jmcneill CFATTACH_DECL_NEW(dw_apb_uart, sizeof(struct dw_apb_uart_softc),
     63   1.1  jmcneill 	dw_apb_uart_match, dw_apb_uart_attach, NULL, NULL);
     64   1.1  jmcneill 
     65   1.1  jmcneill static int
     66   1.1  jmcneill dw_apb_uart_match(device_t parent, cfdata_t cf, void *aux)
     67   1.1  jmcneill {
     68   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     69   1.1  jmcneill 
     70  1.10   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
     71   1.1  jmcneill }
     72   1.1  jmcneill 
     73   1.1  jmcneill static void
     74   1.1  jmcneill dw_apb_uart_attach(device_t parent, device_t self, void *aux)
     75   1.1  jmcneill {
     76   1.1  jmcneill 	struct dw_apb_uart_softc * const ssc = device_private(self);
     77   1.1  jmcneill 	struct com_softc * const sc = &ssc->ssc_sc;
     78   1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     79  1.11  jmcneill 	const int phandle = faa->faa_phandle;
     80   1.6  jmcneill 	bus_space_tag_t bst = faa->faa_bst;
     81   1.1  jmcneill 	bus_space_handle_t bsh;
     82   1.1  jmcneill 	char intrstr[128];
     83   1.1  jmcneill 	bus_addr_t addr;
     84   1.1  jmcneill 	bus_size_t size;
     85  1.11  jmcneill 	u_int reg_shift, reg_iowidth;
     86   1.1  jmcneill 	int error;
     87   1.1  jmcneill 
     88  1.11  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
     89   1.1  jmcneill 		aprint_error(": couldn't get registers\n");
     90   1.1  jmcneill 		return;
     91   1.1  jmcneill 	}
     92   1.1  jmcneill 
     93  1.11  jmcneill 	if (of_getprop_uint32(phandle, "reg-shift", &reg_shift)) {
     94   1.1  jmcneill 		/* missing or bad reg-shift property, assume 2 */
     95   1.6  jmcneill 		reg_shift = 2;
     96   1.1  jmcneill 	}
     97  1.11  jmcneill 	if (of_getprop_uint32(phandle, "reg-io-width", &reg_iowidth)) {
     98  1.12    andvar 		/* missing or bad reg-io-width property, assume 1 */
     99  1.11  jmcneill 		reg_iowidth = 1;
    100  1.11  jmcneill 	}
    101   1.1  jmcneill 
    102   1.1  jmcneill 	sc->sc_dev = self;
    103   1.1  jmcneill 
    104  1.11  jmcneill 	ssc->ssc_clk = fdtbus_clock_get_index(phandle, 0);
    105   1.1  jmcneill 	if (ssc->ssc_clk == NULL) {
    106   1.1  jmcneill 		aprint_error(": couldn't get clock\n");
    107   1.1  jmcneill 		return;
    108   1.1  jmcneill 	}
    109   1.1  jmcneill 	if (clk_enable(ssc->ssc_clk) != 0) {
    110   1.1  jmcneill 		aprint_error(": couldn't enable clock\n");
    111   1.1  jmcneill 		return;
    112   1.1  jmcneill 	}
    113   1.1  jmcneill 
    114  1.11  jmcneill 	ssc->ssc_pclk = fdtbus_clock_get(phandle, "apb_pclk");
    115   1.2  jmcneill 	if (ssc->ssc_pclk != NULL && clk_enable(ssc->ssc_pclk) != 0) {
    116   1.2  jmcneill 		aprint_error(": couldn't enable peripheral clock\n");
    117   1.2  jmcneill 		return;
    118   1.2  jmcneill 	}
    119   1.2  jmcneill 
    120  1.11  jmcneill 	ssc->ssc_rst = fdtbus_reset_get_index(phandle, 0);
    121   1.1  jmcneill 	if (ssc->ssc_rst && fdtbus_reset_deassert(ssc->ssc_rst) != 0) {
    122   1.1  jmcneill 		aprint_error(": couldn't de-assert reset\n");
    123   1.1  jmcneill 		return;
    124   1.1  jmcneill 	}
    125   1.1  jmcneill 
    126   1.1  jmcneill 	sc->sc_frequency = clk_get_rate(ssc->ssc_clk);
    127   1.1  jmcneill 	sc->sc_type = COM_TYPE_DW_APB;
    128   1.1  jmcneill 
    129   1.1  jmcneill 	error = bus_space_map(bst, addr, size, 0, &bsh);
    130   1.1  jmcneill 	if (error) {
    131   1.3  christos 		aprint_error(": couldn't map %#" PRIx64 ": %d", (uint64_t)addr, error);
    132   1.1  jmcneill 		return;
    133   1.1  jmcneill 	}
    134   1.1  jmcneill 
    135  1.11  jmcneill 	com_init_regs_stride_width(&sc->sc_regs, bst, bsh, addr, reg_shift, reg_iowidth);
    136   1.1  jmcneill 
    137   1.1  jmcneill 	com_attach_subr(sc);
    138   1.1  jmcneill 
    139  1.11  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    140   1.1  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    141   1.1  jmcneill 		return;
    142   1.1  jmcneill 	}
    143   1.1  jmcneill 
    144  1.11  jmcneill 	ssc->ssc_ih = fdtbus_intr_establish_xname(phandle, 0,
    145   1.9       ryo 	    IPL_SERIAL, FDT_INTR_MPSAFE, comintr, sc, device_xname(self));
    146   1.1  jmcneill 	if (ssc->ssc_ih == NULL) {
    147   1.1  jmcneill 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
    148   1.1  jmcneill 		    intrstr);
    149   1.1  jmcneill 	}
    150   1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    151   1.1  jmcneill }
    152   1.1  jmcneill 
    153   1.1  jmcneill /*
    154   1.1  jmcneill  * Console support
    155   1.1  jmcneill  */
    156   1.1  jmcneill 
    157   1.1  jmcneill static int
    158   1.1  jmcneill dw_apb_uart_console_match(int phandle)
    159   1.1  jmcneill {
    160  1.10   thorpej 	return of_compatible_match(phandle, compat_data);
    161   1.1  jmcneill }
    162   1.1  jmcneill 
    163   1.1  jmcneill static void
    164   1.1  jmcneill dw_apb_uart_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
    165   1.1  jmcneill {
    166   1.1  jmcneill 	const int phandle = faa->faa_phandle;
    167   1.6  jmcneill 	bus_space_tag_t bst = faa->faa_bst;
    168   1.6  jmcneill 	bus_space_handle_t dummy_bsh;
    169   1.6  jmcneill 	struct com_regs regs;
    170   1.1  jmcneill 	bus_addr_t addr;
    171   1.1  jmcneill 	tcflag_t flags;
    172  1.11  jmcneill 	u_int reg_shift, reg_iowidth;
    173   1.1  jmcneill 	int speed;
    174   1.1  jmcneill 
    175   1.1  jmcneill 	fdtbus_get_reg(phandle, 0, &addr, NULL);
    176   1.1  jmcneill 	speed = fdtbus_get_stdout_speed();
    177   1.1  jmcneill 	if (speed < 0)
    178   1.1  jmcneill 		speed = 115200;	/* default */
    179   1.1  jmcneill 	flags = fdtbus_get_stdout_flags();
    180   1.1  jmcneill 
    181   1.6  jmcneill 	if (of_getprop_uint32(phandle, "reg-shift", &reg_shift)) {
    182   1.7  jmcneill 		/* missing or bad reg-shift property, assume 2 */
    183   1.7  jmcneill 		reg_shift = 2;
    184   1.6  jmcneill 	}
    185  1.11  jmcneill 	if (of_getprop_uint32(phandle, "reg-io-width", &reg_iowidth)) {
    186  1.12    andvar 		/* missing or bad reg-io-width property, assume 1 */
    187  1.11  jmcneill 		reg_iowidth = 1;
    188  1.11  jmcneill 	}
    189   1.6  jmcneill 
    190   1.6  jmcneill 	memset(&dummy_bsh, 0, sizeof(dummy_bsh));
    191  1.11  jmcneill 	com_init_regs_stride_width(&regs, bst, dummy_bsh, addr, reg_shift, reg_iowidth);
    192   1.6  jmcneill 
    193   1.6  jmcneill 	if (comcnattach1(&regs, speed, uart_freq, COM_TYPE_DW_APB, flags))
    194   1.1  jmcneill 		panic("Cannot initialize dw-apb-uart console");
    195   1.1  jmcneill }
    196   1.1  jmcneill 
    197   1.1  jmcneill static const struct fdt_console dw_apb_uart_console = {
    198   1.1  jmcneill 	.match = dw_apb_uart_console_match,
    199   1.1  jmcneill 	.consinit = dw_apb_uart_console_consinit,
    200   1.1  jmcneill };
    201   1.1  jmcneill 
    202   1.1  jmcneill FDT_CONSOLE(dw_apb_uart, &dw_apb_uart_console);
    203