Home | History | Annotate | Line # | Download | only in dev
com_intio.c revision 1.1
      1  1.1  tsutsui /*	$NetBSD: com_intio.c,v 1.1 2012/04/29 07:17:12 tsutsui Exp $	*/
      2  1.1  tsutsui 
      3  1.1  tsutsui /*
      4  1.1  tsutsui  * Copyright (c) 2009 Tetsuya Isaki. All rights reserved.
      5  1.1  tsutsui  *
      6  1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
      7  1.1  tsutsui  * modification, are permitted provided that the following conditions
      8  1.1  tsutsui  * are met:
      9  1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     10  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     11  1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     14  1.1  tsutsui  *
     15  1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  tsutsui  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1  tsutsui  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1  tsutsui  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1  tsutsui  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     20  1.1  tsutsui  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21  1.1  tsutsui  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     22  1.1  tsutsui  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     23  1.1  tsutsui  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  tsutsui  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  tsutsui  * SUCH DAMAGE.
     26  1.1  tsutsui  */
     27  1.1  tsutsui 
     28  1.1  tsutsui /*
     29  1.1  tsutsui  * PSX16550, High-speed RS-232C board
     30  1.1  tsutsui  */
     31  1.1  tsutsui 
     32  1.1  tsutsui #include <sys/cdefs.h>
     33  1.1  tsutsui __KERNEL_RCSID(0, "$NetBSD: com_intio.c,v 1.1 2012/04/29 07:17:12 tsutsui Exp $");
     34  1.1  tsutsui 
     35  1.1  tsutsui #include <sys/param.h>
     36  1.1  tsutsui #include <sys/systm.h>
     37  1.1  tsutsui #include <sys/tty.h>
     38  1.1  tsutsui #include <sys/device.h>
     39  1.1  tsutsui #include <sys/bus.h>
     40  1.1  tsutsui 
     41  1.1  tsutsui #include <dev/ic/comreg.h>
     42  1.1  tsutsui #include <dev/ic/comvar.h>
     43  1.1  tsutsui 
     44  1.1  tsutsui #include <machine/cpu.h>
     45  1.1  tsutsui 
     46  1.1  tsutsui #include <arch/x68k/dev/intiovar.h>
     47  1.1  tsutsui 
     48  1.1  tsutsui #define COM_PSX16550_FREQ	(22118400 / 2)
     49  1.1  tsutsui #define COM_PSX16550_SIZE	(COM_NPORTS << 1)
     50  1.1  tsutsui #define COM_PSX16550_REG_VECT	com_scratch
     51  1.1  tsutsui 
     52  1.1  tsutsui static int  com_intio_match(device_t, cfdata_t, void *);
     53  1.1  tsutsui static void com_intio_attach(device_t, device_t, void *);
     54  1.1  tsutsui 
     55  1.1  tsutsui CFATTACH_DECL_NEW(com_intio, sizeof(struct com_softc),
     56  1.1  tsutsui     com_intio_match, com_intio_attach, NULL, NULL);
     57  1.1  tsutsui 
     58  1.1  tsutsui static int
     59  1.1  tsutsui com_intio_match(device_t parent, cfdata_t cf, void *aux)
     60  1.1  tsutsui {
     61  1.1  tsutsui 	struct intio_attach_args *ia = aux;
     62  1.1  tsutsui 	bus_space_tag_t iot;
     63  1.1  tsutsui 	bus_space_handle_t ioh;
     64  1.1  tsutsui 	int iobase;
     65  1.1  tsutsui 	int rv;
     66  1.1  tsutsui 
     67  1.1  tsutsui 	/* Check whether the board is inserted or not */
     68  1.1  tsutsui 	if (badaddr((void *)IIOV(ia->ia_addr)))
     69  1.1  tsutsui 		return 0;
     70  1.1  tsutsui 
     71  1.1  tsutsui 	ia->ia_size = COM_PSX16550_SIZE;
     72  1.1  tsutsui 	if (intio_map_allocate_region(parent, ia, INTIO_MAP_TESTONLY) < 0)
     73  1.1  tsutsui 		return 0;
     74  1.1  tsutsui 
     75  1.1  tsutsui 	iot = ia->ia_bst;
     76  1.1  tsutsui 	iobase = ia->ia_addr;
     77  1.1  tsutsui 
     78  1.1  tsutsui 	/* if it's in use as console, it's there. */
     79  1.1  tsutsui 	if (com_is_console(iot, iobase, NULL))
     80  1.1  tsutsui 		return 1;
     81  1.1  tsutsui 
     82  1.1  tsutsui 	if (bus_space_map(iot, iobase, ia->ia_size,
     83  1.1  tsutsui 	    BUS_SPACE_MAP_SHIFTED_ODD, &ioh))
     84  1.1  tsutsui 		return 0;
     85  1.1  tsutsui 
     86  1.1  tsutsui 	rv = comprobe1(iot, ioh);
     87  1.1  tsutsui 	bus_space_unmap(iot, ioh, ia->ia_size);
     88  1.1  tsutsui 
     89  1.1  tsutsui 	return rv;
     90  1.1  tsutsui }
     91  1.1  tsutsui 
     92  1.1  tsutsui static void
     93  1.1  tsutsui com_intio_attach(device_t parent, device_t self, void *aux)
     94  1.1  tsutsui {
     95  1.1  tsutsui 	struct com_softc *sc = device_private(self);
     96  1.1  tsutsui 	struct intio_attach_args *ia = aux;
     97  1.1  tsutsui 	bus_space_tag_t iot;
     98  1.1  tsutsui 	bus_space_handle_t ioh;
     99  1.1  tsutsui 	int iobase;
    100  1.1  tsutsui 
    101  1.1  tsutsui 	intio_map_allocate_region(parent, ia, INTIO_MAP_ALLOCATE);
    102  1.1  tsutsui 
    103  1.1  tsutsui 	sc->sc_dev = self;
    104  1.1  tsutsui 	iot = ia->ia_bst;
    105  1.1  tsutsui 	iobase = ia->ia_addr;
    106  1.1  tsutsui 	if (bus_space_map(iot, iobase, ia->ia_size,
    107  1.1  tsutsui 	    BUS_SPACE_MAP_SHIFTED_ODD, &ioh)) {
    108  1.1  tsutsui 		aprint_error(": can't map I/O space\n");
    109  1.1  tsutsui 		return;
    110  1.1  tsutsui 	}
    111  1.1  tsutsui 
    112  1.1  tsutsui 	/* check and set interrupt vector */
    113  1.1  tsutsui 	if (ia->ia_intr < 16 || ia->ia_intr > 255) {
    114  1.1  tsutsui 		aprint_error(": invalid intr vector (0x%02x)\n", ia->ia_intr);
    115  1.1  tsutsui 		return;
    116  1.1  tsutsui 	}
    117  1.1  tsutsui 	bus_space_write_1(iot, ioh, COM_PSX16550_REG_VECT, ia->ia_intr);
    118  1.1  tsutsui 
    119  1.1  tsutsui 	sc->sc_frequency = COM_PSX16550_FREQ;
    120  1.1  tsutsui 	COM_INIT_REGS(sc->sc_regs, iot, ioh, iobase);
    121  1.1  tsutsui 
    122  1.1  tsutsui 	/* PSX16550 uses MCR_DRS to switch interrupt priority level */
    123  1.1  tsutsui 	SET(sc->sc_mcr, MCR_DRS);	/* 0: ipl2 / 1: ipl4 */
    124  1.1  tsutsui 
    125  1.1  tsutsui 	com_attach_subr(sc);
    126  1.1  tsutsui 
    127  1.1  tsutsui 	if (intio_intr_establish(ia->ia_intr, device_xname(self),
    128  1.1  tsutsui 	    comintr, sc))
    129  1.1  tsutsui 		aprint_error_dev(self, "can't establish interrupt handler\n");
    130  1.1  tsutsui }
    131