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