Home | History | Annotate | Line # | Download | only in marvell
com_mv.c revision 1.8
      1 /*	$NetBSD: com_mv.c,v 1.8 2018/12/08 17:46:13 thorpej Exp $	*/
      2 /*
      3  * Copyright (c) 2007, 2010 KIYOHARA Takashi
      4  * All rights reserved.
      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
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  * POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: com_mv.c,v 1.8 2018/12/08 17:46:13 thorpej Exp $");
     30 
     31 #include "opt_com.h"
     32 
     33 #include <sys/param.h>
     34 #include <sys/bus.h>
     35 #include <sys/device.h>
     36 #include <sys/errno.h>
     37 #include <sys/termios.h>
     38 
     39 #include <dev/marvell/gtvar.h>
     40 #include <dev/marvell/marvellreg.h>
     41 #include <dev/marvell/marvellvar.h>
     42 
     43 #include <dev/ic/comvar.h>
     44 
     45 #include <prop/proplib.h>
     46 
     47 #define MVUART_SIZE		0x20
     48 
     49 
     50 static int mvuart_match(device_t, struct cfdata *, void *);
     51 static void mvuart_attach(device_t, device_t, void *);
     52 
     53 CFATTACH_DECL_NEW(mvuart_gt, sizeof(struct com_softc),
     54     mvuart_match, mvuart_attach, NULL, NULL);
     55 CFATTACH_DECL_NEW(mvuart_mbus, sizeof(struct com_softc),
     56     mvuart_match, mvuart_attach, NULL, NULL);
     57 
     58 static void
     59 mvuart_init_regs(struct com_regs *regs, bus_space_tag_t tag,
     60 		 bus_space_handle_t hdl, bus_addr_t addr, bus_size_t size)
     61 {
     62 
     63 	com_init_regs(regs, tag, hdl, addr);
     64 #ifdef COM_REGMAP
     65 	for (size_t i = 0; i < __arraycount(regs->cr_map); i++)
     66 		regs->cr_map[i] = regs->cr_map[i] << 2;
     67 	regs->cr_nports = size;
     68 #endif
     69 }
     70 
     71 
     72 /* ARGSUSED */
     73 static int
     74 mvuart_match(device_t parent, struct cfdata *match, void *aux)
     75 {
     76 	struct marvell_attach_args *mva = aux;
     77 	struct com_regs regs;
     78 	bus_space_handle_t ioh;
     79 
     80 	if (strcmp(mva->mva_name, match->cf_name) != 0)
     81 		return 0;
     82 	if (mva->mva_offset == MVA_OFFSET_DEFAULT ||
     83 	    mva->mva_irq == MVA_IRQ_DEFAULT)
     84 		return 0;
     85 
     86 	if (com_is_console(mva->mva_iot, mva->mva_addr + mva->mva_offset, NULL))
     87 		goto console;
     88 
     89 	if (bus_space_subregion(mva->mva_iot, mva->mva_ioh, mva->mva_offset,
     90 	    MVUART_SIZE, &ioh))
     91 		return 0;
     92 	mvuart_init_regs(&regs, mva->mva_iot, ioh, mva->mva_offset,
     93 			 MVUART_SIZE);
     94 	if (!com_probe_subr(&regs))
     95 		return 0;
     96 
     97 console:
     98 	mva->mva_size = MVUART_SIZE;
     99 	return 1;
    100 }
    101 
    102 /* ARGSUSED */
    103 static void
    104 mvuart_attach(device_t parent, device_t self, void *aux)
    105 {
    106 	struct com_softc *sc = device_private(self);
    107 	struct marvell_attach_args *mva = aux;
    108 	bus_space_tag_t iot;
    109 	bus_space_handle_t ioh;
    110 	prop_dictionary_t dict = device_properties(self);
    111 
    112 	sc->sc_dev = self;
    113 
    114 	if (!prop_dictionary_get_uint32(dict, "frequency", &sc->sc_frequency)) {
    115 		aprint_error(": no frequency property\n");
    116 		return;
    117 	}
    118 
    119 	iot = mva->mva_iot;
    120 	if (!com_is_console(iot, mva->mva_addr + mva->mva_offset, &ioh)) {
    121 		if (bus_space_subregion(iot, mva->mva_ioh, mva->mva_offset,
    122 		    mva->mva_size, &ioh)) {
    123 			aprint_error(": can't map registers\n");
    124 			return;
    125 		}
    126 	}
    127 	mvuart_init_regs(&sc->sc_regs,
    128 	    iot, ioh, mva->mva_addr + mva->mva_offset, mva->mva_size);
    129 
    130 	com_attach_subr(sc);
    131 
    132 	marvell_intr_establish(mva->mva_irq, IPL_SERIAL, comintr, sc);
    133 }
    134 
    135 #ifdef COM_REGMAP
    136 int mvuart_cnattach(bus_space_tag_t, bus_addr_t, int, uint32_t, int);
    137 
    138 int
    139 mvuart_cnattach(bus_space_tag_t iot, bus_addr_t addr, int baud,
    140 		uint32_t sysfreq, int mode)
    141 {
    142 	struct com_regs regs;
    143 
    144 	mvuart_init_regs(&regs, iot, 0x0, addr, MVUART_SIZE);
    145 
    146 	return comcnattach1(&regs, baud, sysfreq, COM_TYPE_16550_NOERS, mode);
    147 }
    148 #endif /* COM_REGMAP */
    149