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