Home | History | Annotate | Line # | Download | only in xilinx
      1 /*	$NetBSD: zynq7000_uart.c,v 1.4 2025/09/06 22:53:48 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2015  Genetec Corporation.  All rights reserved.
      5  * Written by Hashimoto Kenichi for Genetec Corporation.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: zynq7000_uart.c,v 1.4 2025/09/06 22:53:48 thorpej Exp $");
     31 
     32 #include "opt_soc.h"
     33 #include "opt_console.h"
     34 
     35 #include <sys/param.h>
     36 #include <sys/bus.h>
     37 #include <sys/device.h>
     38 
     39 #include <arm/xilinx/zynq_uartreg.h>
     40 #include <arm/xilinx/zynq_uartvar.h>
     41 
     42 #include <dev/fdt/fdtvar.h>
     43 #include <dev/fdt/fdt_console.h>
     44 
     45 static const struct device_compatible_entry compat_data[] = {
     46 	{ .compat = "xlnx,xuartps" },
     47 	{ .compat = "cdns,uart-r1p8" },
     48 	DEVICE_COMPAT_EOL
     49 };
     50 
     51 int
     52 zynquart_match(device_t parent, struct cfdata *cf, void *aux)
     53 {
     54 	struct fdt_attach_args * const faa = aux;
     55 
     56 	return of_compatible_match(faa->faa_phandle, compat_data);
     57 }
     58 
     59 void
     60 zynquart_attach(device_t parent, device_t self, void *aux)
     61 {
     62 	struct fdt_attach_args * faa = aux;
     63 	const int phandle = faa->faa_phandle;
     64 	char intrstr[128];
     65 	bus_addr_t addr;
     66 	bus_size_t size;
     67 
     68 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
     69 		aprint_error(": couldn't get registers\n");
     70 		return;
     71 	}
     72 
     73 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
     74 		aprint_error(": failed to decode interrupt\n");
     75 		return;
     76 	}
     77 
     78 	if (fdtbus_intr_establish(phandle, 0, IPL_SERIAL, IST_LEVEL,
     79 				  zynquartintr, device_private(self)) == NULL) {
     80 		aprint_error(": failed to establish interrupt on %s\n", intrstr);
     81 		return;
     82 	}
     83 
     84 	zynquart_attach_common(parent, self, faa->faa_bst, addr, size, 0);
     85 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
     86 }
     87 
     88 /*
     89  * Console support
     90  */
     91 
     92 static int
     93 zynq_uart_console_match(int phandle)
     94 {
     95 	return of_compatible_match(phandle, compat_data);
     96 }
     97 
     98 static void
     99 zynq_uart_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
    100 {
    101 	const int phandle = faa->faa_phandle;
    102 	bus_space_tag_t bst = faa->faa_bst;
    103 	bus_addr_t addr;
    104 	tcflag_t flags;
    105 	int speed;
    106 
    107 	fdtbus_get_reg(phandle, 0, &addr, NULL);
    108 	speed = fdtbus_get_stdout_speed();
    109 	if (speed < 0)
    110 		speed = 115200;	/* default */
    111 	flags = fdtbus_get_stdout_flags();
    112 
    113 	if (zynquart_cons_attach(bst, addr, speed, flags))
    114 		panic("Cannot initialize zynq uart console");
    115 }
    116 
    117 static const struct fdt_console zynq_uart_console = {
    118 	.match = zynq_uart_console_match,
    119 	.consinit = zynq_uart_console_consinit,
    120 };
    121 
    122 FDT_CONSOLE(zynq_uart, &zynq_uart_console);
    123