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