Home | History | Annotate | Line # | Download | only in marvell
com_mv.c revision 1.6
      1 /*	$NetBSD: com_mv.c,v 1.5 2011/01/28 16:12:22 jakllsch 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.5 2011/01/28 16:12:22 jakllsch 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/mutex.h>
     38 #include <sys/termios.h>
     39 
     40 #include <dev/marvell/gtvar.h>
     41 #include <dev/marvell/marvellreg.h>
     42 #include <dev/marvell/marvellvar.h>
     43 
     44 #include <dev/ic/comvar.h>
     45 
     46 #include <prop/proplib.h>
     47 
     48 #define MVUART_SIZE		0x80
     49 
     50 #define MVUART_REG_USR		0x7c	/* XXXX: What is this??? */
     51 
     52 
     53 static int mvuart_match(device_t, struct cfdata *, void *);
     54 static void mvuart_attach(device_t, device_t, void *);
     55 
     56 static int mvuart_intr(void *);
     57 
     58 CFATTACH_DECL_NEW(mvuart_gt, sizeof(struct com_softc),
     59     mvuart_match, mvuart_attach, NULL, NULL);
     60 CFATTACH_DECL_NEW(mvuart_mbus, sizeof(struct com_softc),
     61     mvuart_match, mvuart_attach, NULL, NULL);
     62 
     63 #ifdef COM_REGMAP
     64 #define MVUART_INIT_REGS(regs, tag, hdl, addr, size)			\
     65 	do {								\
     66 		int _i;							\
     67 									\
     68 		regs.cr_iot = tag;					\
     69 		regs.cr_ioh = hdl;					\
     70 		regs.cr_iobase = addr;					\
     71 		regs.cr_nports = size;					\
     72 		for (_i = 0; _i < __arraycount(regs.cr_map); _i++)	\
     73 			regs.cr_map[_i] = com_std_map[_i] << 2;		\
     74 	} while (0)
     75 #define CSR_WRITE_1(r, o, v)    \
     76 	bus_space_write_1((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o], v)
     77 #define CSR_READ_1(r, o)        \
     78 	bus_space_read_1((r)->cr_iot, (r)->cr_ioh, (r)->cr_map[o])
     79 #else
     80 #define MVUART_INIT_REGS(regs, tag, hdl, addr, size) \
     81 	COM_INIT_REGS(regs, tag, hdl, addr)
     82 #define CSR_WRITE_1(r, o, v)    \
     83         bus_space_write_1((r)->cr_iot, (r)->cr_ioh, o, v)
     84 #define CSR_READ_1(r, o)        \
     85 	bus_space_read_1((r)->cr_iot, (r)->cr_ioh, o)
     86 #endif
     87 
     88 struct {
     89 	int model;
     90 	int type;
     91 } mvuart_extensions[] = {
     92 	{ MARVELL_ARMADAXP_MV78130,	COM_TYPE_ARMADAXP },
     93 	{ MARVELL_ARMADAXP_MV78160,	COM_TYPE_ARMADAXP },
     94 	{ MARVELL_ARMADAXP_MV78230,	COM_TYPE_ARMADAXP },
     95 	{ MARVELL_ARMADAXP_MV78260,	COM_TYPE_ARMADAXP },
     96 	{ MARVELL_ARMADAXP_MV78460,	COM_TYPE_ARMADAXP },
     97 };
     98 
     99 /* ARGSUSED */
    100 static int
    101 mvuart_match(device_t parent, struct cfdata *match, void *aux)
    102 {
    103 	struct marvell_attach_args *mva = aux;
    104 	struct com_regs regs;
    105 	bus_space_handle_t ioh;
    106 
    107 	if (strcmp(mva->mva_name, match->cf_name) != 0)
    108 		return 0;
    109 	if (mva->mva_offset == MVA_OFFSET_DEFAULT ||
    110 	    mva->mva_irq == MVA_IRQ_DEFAULT)
    111 		return 0;
    112 
    113 	if (com_is_console(mva->mva_iot, mva->mva_addr + mva->mva_offset, NULL))
    114 		goto console;
    115 
    116 	if (bus_space_subregion(mva->mva_iot, mva->mva_ioh, mva->mva_offset,
    117 	    MVUART_SIZE, &ioh))
    118 		return 0;
    119 	MVUART_INIT_REGS(regs, mva->mva_iot, ioh, mva->mva_offset, MVUART_SIZE);
    120 	if (!com_probe_subr(&regs))
    121 		return 0;
    122 
    123 console:
    124 	mva->mva_size = MVUART_SIZE;
    125 	return 1;
    126 }
    127 
    128 /* ARGSUSED */
    129 static void
    130 mvuart_attach(device_t parent, device_t self, void *aux)
    131 {
    132 	struct com_softc *sc = device_private(self);
    133 	struct marvell_attach_args *mva = aux;
    134 	bus_space_tag_t iot;
    135 	bus_space_handle_t ioh;
    136 	prop_dictionary_t dict = device_properties(self);
    137 	int i;
    138 
    139 	sc->sc_dev = self;
    140 
    141 	if (!prop_dictionary_get_uint32(dict, "frequency", &sc->sc_frequency)) {
    142 		aprint_error(": no frequency property\n");
    143 		return;
    144 	}
    145 
    146 	iot = mva->mva_iot;
    147 	if (!com_is_console(iot, mva->mva_addr + mva->mva_offset, &ioh)) {
    148 		if (bus_space_subregion(iot, mva->mva_ioh, mva->mva_offset,
    149 		    mva->mva_size, &ioh)) {
    150 			aprint_error(": can't map registers\n");
    151 			return;
    152 		}
    153 	}
    154 	MVUART_INIT_REGS(sc->sc_regs,
    155 	    iot, ioh, mva->mva_addr + mva->mva_offset, mva->mva_size);
    156 
    157 	for (i = 0; i < __arraycount(mvuart_extensions); i++)
    158 		if (mva->mva_model == mvuart_extensions[i].model) {
    159 			sc->sc_type = mvuart_extensions[i].type;
    160 			break;
    161 		}
    162 
    163 	com_attach_subr(sc);
    164 
    165 	if (sc->sc_type == COM_TYPE_ARMADAXP)
    166 		marvell_intr_establish(mva->mva_irq, IPL_SERIAL,
    167 		    mvuart_intr, sc);
    168 	else
    169 		marvell_intr_establish(mva->mva_irq, IPL_SERIAL, comintr, sc);
    170 }
    171 
    172 static int
    173 mvuart_intr(void *arg)
    174 {
    175 	struct com_softc *sc = arg;
    176 	struct com_regs *regsp = &sc->sc_regs;
    177 	int timeout;
    178 	uint8_t iir, v;
    179 
    180 	if (!device_is_active(sc->sc_dev))
    181 		return 0;
    182 
    183 	KASSERT(regsp != NULL);
    184 
    185 	mutex_spin_enter(&sc->sc_lock);
    186 	iir = CSR_READ_1(regsp, COM_REG_IIR);
    187 	if ((iir & IIR_BUSY) == IIR_BUSY) {
    188 		/*
    189 		 * XXXXX: What is this?  I don't found in Marvell datasheet.
    190 		 *        Maybe workaround for BUG in UART.
    191 		 */
    192 		v = bus_space_read_1(regsp->cr_iot, regsp->cr_ioh,
    193 		    MVUART_REG_USR);
    194 		for (timeout = 10000; (v & 0x1) != 0; timeout--) {
    195 			if (timeout <= 0) {
    196 				aprint_error_dev(sc->sc_dev,
    197 				    "timeout while waiting for BUSY interrupt "
    198 				    "acknowledge\n");
    199 				mutex_spin_exit(&sc->sc_lock);
    200 				return 0;
    201 			}
    202 			v = bus_space_read_1(regsp->cr_iot, regsp->cr_ioh,
    203 			    MVUART_REG_USR);
    204 		}
    205 		CSR_WRITE_1(regsp, COM_REG_LCR, sc->sc_lcr);
    206 	}
    207 	mutex_spin_exit(&sc->sc_lock);
    208 
    209 	return comintr(arg);
    210 }
    211 
    212 #ifdef COM_REGMAP
    213 int mvuart_cnattach(bus_space_tag_t, bus_addr_t, int, uint32_t, int);
    214 
    215 int
    216 mvuart_cnattach(bus_space_tag_t iot, bus_addr_t addr, int baud,
    217 		uint32_t sysfreq, int mode)
    218 {
    219 	struct com_regs regs;
    220 
    221 	MVUART_INIT_REGS(regs, iot, 0x0, addr, MVUART_SIZE);
    222 
    223 	return comcnattach1(&regs, baud, sysfreq, COM_TYPE_16550_NOERS, mode);
    224 }
    225 #endif /* COM_REGMAP */
    226