Home | History | Annotate | Line # | Download | only in dev
      1  1.8   thorpej /*	$NetBSD: pq3duart.c,v 1.8 2021/08/07 16:19:02 thorpej Exp $	*/
      2  1.2      matt /*-
      3  1.2      matt  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
      4  1.2      matt  * All rights reserved.
      5  1.2      matt  *
      6  1.2      matt  * This code is derived from software contributed to The NetBSD Foundation
      7  1.2      matt  * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
      8  1.2      matt  * Agency and which was developed by Matt Thomas of 3am Software Foundry.
      9  1.2      matt  *
     10  1.2      matt  * This material is based upon work supported by the Defense Advanced Research
     11  1.2      matt  * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
     12  1.2      matt  * Contract No. N66001-09-C-2073.
     13  1.2      matt  * Approved for Public Release, Distribution Unlimited
     14  1.2      matt  *
     15  1.2      matt  * Redistribution and use in source and binary forms, with or without
     16  1.2      matt  * modification, are permitted provided that the following conditions
     17  1.2      matt  * are met:
     18  1.2      matt  * 1. Redistributions of source code must retain the above copyright
     19  1.2      matt  *    notice, this list of conditions and the following disclaimer.
     20  1.2      matt  * 2. Redistributions in binary form must reproduce the above copyright
     21  1.2      matt  *    notice, this list of conditions and the following disclaimer in the
     22  1.2      matt  *    documentation and/or other materials provided with the distribution.
     23  1.2      matt  *
     24  1.2      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  1.2      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  1.2      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  1.2      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  1.2      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  1.2      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  1.2      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  1.2      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  1.2      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  1.2      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  1.2      matt  * POSSIBILITY OF SUCH DAMAGE.
     35  1.2      matt  */
     36  1.2      matt 
     37  1.2      matt #include <sys/cdefs.h>
     38  1.8   thorpej __KERNEL_RCSID(0, "$NetBSD: pq3duart.c,v 1.8 2021/08/07 16:19:02 thorpej Exp $");
     39  1.2      matt 
     40  1.6       rin #include "ioconf.h"
     41  1.3      matt 
     42  1.2      matt #include <sys/param.h>
     43  1.2      matt #include <sys/cpu.h>
     44  1.2      matt #include <sys/device.h>
     45  1.2      matt #include <sys/tty.h>
     46  1.2      matt 
     47  1.2      matt #include <sys/intr.h>
     48  1.2      matt #include <sys/bus.h>
     49  1.2      matt 
     50  1.2      matt #include <dev/ic/comreg.h>
     51  1.2      matt #include <dev/ic/comvar.h>
     52  1.2      matt 
     53  1.2      matt #include <powerpc/booke/cpuvar.h>
     54  1.2      matt #include <powerpc/booke/e500var.h>
     55  1.2      matt #include <powerpc/booke/e500reg.h>
     56  1.2      matt 
     57  1.2      matt struct pq3duart_softc {
     58  1.2      matt 	device_t dsc_dev;
     59  1.2      matt 	struct com_softc *dsc_sc[2];
     60  1.2      matt 	bus_space_tag_t dsc_memt;
     61  1.2      matt 	bus_addr_t dsc_base;
     62  1.2      matt 	void *dsc_ih;
     63  1.2      matt };
     64  1.2      matt 
     65  1.2      matt struct pq3duart_attach_args {
     66  1.2      matt 	const char *da_busname;
     67  1.2      matt 	bus_space_tag_t da_memt;
     68  1.2      matt 	bus_addr_t da_addr;
     69  1.2      matt 	bus_addr_t da_size;
     70  1.2      matt 	u_int da_port;
     71  1.2      matt };
     72  1.2      matt 
     73  1.2      matt static int pq3duart_match(device_t, cfdata_t, void *);
     74  1.2      matt static void pq3duart_attach(device_t, device_t, void *);
     75  1.2      matt static int com_pq3duart_match(device_t, cfdata_t, void *);
     76  1.2      matt static void com_pq3duart_attach(device_t, device_t, void *);
     77  1.2      matt 
     78  1.2      matt CFATTACH_DECL_NEW(pq3duart, sizeof(struct pq3duart_softc),
     79  1.2      matt     pq3duart_match, pq3duart_attach, NULL, NULL);
     80  1.2      matt 
     81  1.2      matt CFATTACH_DECL_NEW(com_pq3duart, sizeof(struct com_softc),
     82  1.2      matt     com_pq3duart_match, com_pq3duart_attach, NULL, NULL);
     83  1.2      matt 
     84  1.2      matt static int
     85  1.2      matt pq3duart_match(device_t parent, cfdata_t cf, void *aux)
     86  1.2      matt {
     87  1.2      matt 
     88  1.2      matt 	if (!e500_cpunode_submatch(parent, cf, cf->cf_name, aux))
     89  1.2      matt 		return 0;
     90  1.2      matt 
     91  1.2      matt 	return 1;
     92  1.2      matt }
     93  1.2      matt 
     94  1.2      matt static int
     95  1.2      matt com_pq3duart_match(device_t parent, cfdata_t cfdata, void *aux)
     96  1.2      matt {
     97  1.2      matt 	struct pq3duart_softc * const dsc = device_private(parent);
     98  1.2      matt 	struct pq3duart_attach_args * const da = aux;
     99  1.2      matt 	struct com_regs regs;
    100  1.2      matt 
    101  1.2      matt 	if ((da->da_port != 1 && da->da_port != 2)
    102  1.2      matt 	    || dsc->dsc_sc[da->da_port-1] != NULL)
    103  1.2      matt 		return 0;
    104  1.2      matt 
    105  1.2      matt 	bus_space_tag_t memt = da->da_memt;
    106  1.2      matt 	bus_addr_t addr = da->da_addr;
    107  1.2      matt 	bus_addr_t size = da->da_size;
    108  1.2      matt 	bus_space_handle_t memh;
    109  1.2      matt 
    110  1.2      matt 	if (com_is_console(memt, addr, &memh))
    111  1.2      matt 		return 1;
    112  1.2      matt 
    113  1.2      matt 	if (bus_space_map(memt, addr, size, 0, &memh))
    114  1.2      matt 		return 0;
    115  1.2      matt 
    116  1.5   thorpej 	com_init_regs(&regs, memt, memh, addr);
    117  1.2      matt 
    118  1.2      matt 	int rv = com_probe_subr(&regs);
    119  1.2      matt 
    120  1.2      matt 	bus_space_unmap(memt, memh, size);
    121  1.2      matt 
    122  1.2      matt 	return rv;
    123  1.2      matt }
    124  1.2      matt 
    125  1.2      matt static int
    126  1.2      matt pq3duart_intr(void *arg)
    127  1.2      matt {
    128  1.2      matt 	struct pq3duart_softc * const dsc = arg;
    129  1.2      matt 	int rv = 0;
    130  1.2      matt 
    131  1.2      matt 	if (dsc->dsc_sc[0] != NULL)
    132  1.2      matt 		rv += comintr(dsc->dsc_sc[0]);
    133  1.2      matt 	if (dsc->dsc_sc[1] != NULL)
    134  1.2      matt 		rv += comintr(dsc->dsc_sc[1]);
    135  1.2      matt 
    136  1.2      matt 	return rv;
    137  1.2      matt }
    138  1.2      matt 
    139  1.2      matt static int
    140  1.2      matt pq3duart_print(void *aux, const char *pnp)
    141  1.2      matt {
    142  1.2      matt 	struct pq3duart_attach_args * const da = aux;
    143  1.2      matt 
    144  1.2      matt 	if (pnp != NULL)
    145  1.2      matt 		return QUIET;
    146  1.2      matt 
    147  1.2      matt 	aprint_normal(" port %d", da->da_port);
    148  1.2      matt 
    149  1.2      matt 	return UNCONF;
    150  1.2      matt }
    151  1.2      matt 
    152  1.2      matt static void
    153  1.2      matt pq3duart_attach(device_t parent, device_t self, void *aux)
    154  1.2      matt {
    155  1.2      matt 	struct cpunode_softc * const psc = device_private(parent);
    156  1.2      matt 	struct pq3duart_softc * const dsc = device_private(self);
    157  1.2      matt 	struct cpunode_attach_args * const cna = aux;
    158  1.2      matt 	struct cpunode_locators * const cnl = &cna->cna_locs;
    159  1.2      matt 	struct pq3duart_attach_args da;
    160  1.2      matt 	u_int nports = cnl->cnl_size / DUART_SIZE;
    161  1.2      matt 
    162  1.2      matt 	psc->sc_children |= cna->cna_childmask;
    163  1.2      matt 	dsc->dsc_dev = self;
    164  1.2      matt 
    165  1.2      matt 	aprint_normal(": %u ports\n", nports);
    166  1.2      matt 
    167  1.4  riastrad 	for (u_int port = 1; port <= uimin(2, nports); port++) {
    168  1.2      matt 		da.da_memt = cna->cna_memt;
    169  1.2      matt 		da.da_port = port;
    170  1.2      matt 		da.da_addr = cnl->cnl_addr + (port - 1) * DUART_SIZE;
    171  1.2      matt 		da.da_size = COM_NPORTS;
    172  1.8   thorpej 		config_found(self, &da, pq3duart_print, CFARGS_NONE);
    173  1.2      matt 	}
    174  1.2      matt 
    175  1.2      matt 	if (dsc->dsc_sc[0] != NULL || dsc->dsc_sc[1] != NULL) {
    176  1.2      matt 		dsc->dsc_ih = intr_establish(cnl->cnl_intrs[0], IPL_SERIAL,
    177  1.2      matt 		    IST_ONCHIP, pq3duart_intr, dsc);
    178  1.2      matt 		if (dsc->dsc_ih == NULL)
    179  1.2      matt 			aprint_error_dev(self,
    180  1.2      matt 			    "failed to establish interrupt %d\n",
    181  1.2      matt 			     cnl->cnl_intrs[0]);
    182  1.2      matt 		else
    183  1.2      matt 			aprint_normal_dev(self,
    184  1.2      matt 			    "interrupting on irq %d\n",
    185  1.2      matt 			     cnl->cnl_intrs[0]);
    186  1.2      matt 	}
    187  1.2      matt }
    188  1.2      matt 
    189  1.2      matt static void
    190  1.2      matt com_pq3duart_attach(device_t parent, device_t self, void *aux)
    191  1.2      matt {
    192  1.2      matt 	struct pq3duart_softc * const dsc = device_private(parent);
    193  1.2      matt 	struct com_softc * const sc = device_private(self);
    194  1.2      matt 	struct pq3duart_attach_args * const da = aux;
    195  1.2      matt 
    196  1.2      matt 	dsc->dsc_sc[da->da_port-1] = sc;
    197  1.2      matt 	sc->sc_dev = self;
    198  1.2      matt 	sc->sc_frequency = (int) board_info_get_number("bus-frequency");
    199  1.2      matt 
    200  1.2      matt 	bus_space_tag_t memt = da->da_memt;
    201  1.2      matt 	bus_addr_t addr = da->da_addr;
    202  1.2      matt 	bus_addr_t size = da->da_size;
    203  1.2      matt 	bus_space_handle_t memh;
    204  1.2      matt 
    205  1.2      matt 	if (!com_is_console(memt, addr, &memh)) {
    206  1.2      matt 		int error = bus_space_map(memt, addr, size, 0, &memh);
    207  1.2      matt 		if (error) {
    208  1.2      matt 			aprint_error(": failed to map registers: %d\n", error);
    209  1.2      matt 			return;
    210  1.2      matt 		}
    211  1.2      matt 	}
    212  1.2      matt 
    213  1.5   thorpej 	com_init_regs(&sc->sc_regs, memt, memh, addr);
    214  1.2      matt 	sc->sc_regs.cr_nports = size;
    215  1.2      matt 
    216  1.2      matt 	com_attach_subr(sc);
    217  1.2      matt 
    218  1.2      matt }
    219