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