Home | History | Annotate | Line # | Download | only in nxp
imx_com.c revision 1.1
      1 /*	$NetBSD: imx_com.c,v 1.1 2020/12/23 14:42:38 skrll 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.1 2020/12/23 14:42:38 skrll 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 
     40 #include <arm/imx/imxuartreg.h>
     41 #include <arm/imx/imxuartvar.h>
     42 
     43 static int imx_com_match(device_t, struct cfdata *, void *);
     44 static void imx_com_attach(device_t, device_t, void *);
     45 
     46 CFATTACH_DECL_NEW(imx_com, sizeof(struct imxuart_softc),
     47     imx_com_match, imx_com_attach, NULL, NULL);
     48 
     49 static const char * const compatible[] = {
     50 	"fsl,imx6q-uart",
     51 	NULL
     52 };
     53 
     54 static int
     55 imx_com_match(device_t parent, struct cfdata *cf, void *aux)
     56 {
     57 	struct fdt_attach_args * const faa = aux;
     58 
     59 	return of_match_compatible(faa->faa_phandle, compatible);
     60 }
     61 
     62 static void
     63 imx_com_attach(device_t parent, device_t self, void *aux)
     64 {
     65 	struct imxuart_softc *sc = device_private(self);
     66 	struct imxuart_regs *regsp = &sc->sc_regs;
     67 	struct fdt_attach_args *faa = aux;
     68 	const int phandle = faa->faa_phandle;
     69 	bus_space_tag_t bst = faa->faa_bst;
     70 	bus_space_handle_t bsh;
     71 	char intrstr[128];
     72 	struct clk *per;
     73 	bus_addr_t addr;
     74 	bus_size_t size;
     75 
     76 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
     77 		aprint_error(": couldn't get registers\n");
     78 		return;
     79 	}
     80 
     81 	if (bus_space_map(bst, addr, size, 0, &bsh) != 0) {
     82 		aprint_error(": couldn't map registers\n");
     83 		return;
     84 	}
     85 
     86 	if (fdtbus_clock_enable(phandle, "ipg", false) != 0) {
     87 		aprint_error(": couldn't enable ipg clock\n");
     88 		return;
     89 	}
     90 
     91 	per = fdtbus_clock_get(phandle, "per");
     92 	if (per != NULL && clk_enable(per) != 0) {
     93 		aprint_error(": couldn't enable per clock\n");
     94 		return;
     95 	}
     96 
     97 	sc->sc_dev = self;
     98 	regsp->ur_iot = bst;
     99 	regsp->ur_iobase = addr;
    100 	regsp->ur_ioh = bsh;
    101 
    102 	if (per != NULL) {
    103 		aprint_normal(", %u Hz", clk_get_rate(per));
    104 		/* XXX */
    105 		imxuart_set_frequency(clk_get_rate(per), 2);
    106 	}
    107 
    108 	if (imxuart_is_console(regsp->ur_iot, regsp->ur_iobase, &regsp->ur_ioh))
    109 		aprint_normal(" (console)");
    110 
    111 	aprint_normal("\n");
    112 
    113 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    114 		aprint_error_dev(self, "failed to decode interrupt\n");
    115 		return;
    116 	}
    117 
    118 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_SERIAL,
    119 	    0, imxuintr, sc);
    120 	if (sc->sc_ih == NULL) {
    121 		aprint_error_dev(self, "failed to establish interrupt\n");
    122 		return;
    123 	}
    124 
    125 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    126 
    127 	imxuart_attach_subr(sc);
    128 }
    129 
    130 /*
    131  * Console support
    132  */
    133 
    134 static int
    135 imx_com_console_match(int phandle)
    136 {
    137 	return of_match_compatible(phandle, compatible);
    138 }
    139 
    140 static void
    141 imx_com_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
    142 {
    143 	const int phandle = faa->faa_phandle;
    144 	bus_space_tag_t bst = faa->faa_bst;
    145 	bus_addr_t addr;
    146 	bus_size_t size;
    147 	tcflag_t flags;
    148 	int speed;
    149 
    150 	fdtbus_get_reg(phandle, 0, &addr, &size);
    151 	speed = fdtbus_get_stdout_speed();
    152 	if (speed < 0)
    153 		speed = 115200;	/* default */
    154 	flags = fdtbus_get_stdout_flags();
    155 
    156 	imxuart_set_frequency(uart_freq, 2);
    157 	if (imxuart_cnattach(bst, addr, speed, flags) != 0)
    158 		panic("cannot attach console UART");
    159 }
    160 
    161 static const struct fdt_console imx_com_console = {
    162 	.match = imx_com_console_match,
    163 	.consinit = imx_com_console_consinit,
    164 };
    165 
    166 FDT_CONSOLE(imx_com, &imx_com_console);
    167