1*6d487047Sthorpej /* $NetBSD: com_intio.c,v 1.2 2018/12/08 17:46:13 thorpej Exp $ */
2231b9114Stsutsui
3231b9114Stsutsui /*
4231b9114Stsutsui * Copyright (c) 2009 Tetsuya Isaki. All rights reserved.
5231b9114Stsutsui *
6231b9114Stsutsui * Redistribution and use in source and binary forms, with or without
7231b9114Stsutsui * modification, are permitted provided that the following conditions
8231b9114Stsutsui * are met:
9231b9114Stsutsui * 1. Redistributions of source code must retain the above copyright
10231b9114Stsutsui * notice, this list of conditions and the following disclaimer.
11231b9114Stsutsui * 2. Redistributions in binary form must reproduce the above copyright
12231b9114Stsutsui * notice, this list of conditions and the following disclaimer in the
13231b9114Stsutsui * documentation and/or other materials provided with the distribution.
14231b9114Stsutsui *
15231b9114Stsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16231b9114Stsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17231b9114Stsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18231b9114Stsutsui * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19231b9114Stsutsui * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20231b9114Stsutsui * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21231b9114Stsutsui * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22231b9114Stsutsui * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23231b9114Stsutsui * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24231b9114Stsutsui * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25231b9114Stsutsui * SUCH DAMAGE.
26231b9114Stsutsui */
27231b9114Stsutsui
28231b9114Stsutsui /*
29231b9114Stsutsui * PSX16550, High-speed RS-232C board
30231b9114Stsutsui */
31231b9114Stsutsui
32231b9114Stsutsui #include <sys/cdefs.h>
33*6d487047Sthorpej __KERNEL_RCSID(0, "$NetBSD: com_intio.c,v 1.2 2018/12/08 17:46:13 thorpej Exp $");
34231b9114Stsutsui
35231b9114Stsutsui #include <sys/param.h>
36231b9114Stsutsui #include <sys/systm.h>
37231b9114Stsutsui #include <sys/tty.h>
38231b9114Stsutsui #include <sys/device.h>
39231b9114Stsutsui #include <sys/bus.h>
40231b9114Stsutsui
41231b9114Stsutsui #include <dev/ic/comreg.h>
42231b9114Stsutsui #include <dev/ic/comvar.h>
43231b9114Stsutsui
44231b9114Stsutsui #include <machine/cpu.h>
45231b9114Stsutsui
46231b9114Stsutsui #include <arch/x68k/dev/intiovar.h>
47231b9114Stsutsui
48231b9114Stsutsui #define COM_PSX16550_FREQ (22118400 / 2)
49231b9114Stsutsui #define COM_PSX16550_SIZE (COM_NPORTS << 1)
50231b9114Stsutsui #define COM_PSX16550_REG_VECT com_scratch
51231b9114Stsutsui
52231b9114Stsutsui static int com_intio_match(device_t, cfdata_t, void *);
53231b9114Stsutsui static void com_intio_attach(device_t, device_t, void *);
54231b9114Stsutsui
55231b9114Stsutsui CFATTACH_DECL_NEW(com_intio, sizeof(struct com_softc),
56231b9114Stsutsui com_intio_match, com_intio_attach, NULL, NULL);
57231b9114Stsutsui
58231b9114Stsutsui static int
com_intio_match(device_t parent,cfdata_t cf,void * aux)59231b9114Stsutsui com_intio_match(device_t parent, cfdata_t cf, void *aux)
60231b9114Stsutsui {
61231b9114Stsutsui struct intio_attach_args *ia = aux;
62231b9114Stsutsui bus_space_tag_t iot;
63231b9114Stsutsui bus_space_handle_t ioh;
64231b9114Stsutsui int iobase;
65231b9114Stsutsui int rv;
66231b9114Stsutsui
67231b9114Stsutsui /* Check whether the board is inserted or not */
68231b9114Stsutsui if (badaddr((void *)IIOV(ia->ia_addr)))
69231b9114Stsutsui return 0;
70231b9114Stsutsui
71231b9114Stsutsui ia->ia_size = COM_PSX16550_SIZE;
72231b9114Stsutsui if (intio_map_allocate_region(parent, ia, INTIO_MAP_TESTONLY) < 0)
73231b9114Stsutsui return 0;
74231b9114Stsutsui
75231b9114Stsutsui iot = ia->ia_bst;
76231b9114Stsutsui iobase = ia->ia_addr;
77231b9114Stsutsui
78231b9114Stsutsui /* if it's in use as console, it's there. */
79231b9114Stsutsui if (com_is_console(iot, iobase, NULL))
80231b9114Stsutsui return 1;
81231b9114Stsutsui
82231b9114Stsutsui if (bus_space_map(iot, iobase, ia->ia_size,
83231b9114Stsutsui BUS_SPACE_MAP_SHIFTED_ODD, &ioh))
84231b9114Stsutsui return 0;
85231b9114Stsutsui
86231b9114Stsutsui rv = comprobe1(iot, ioh);
87231b9114Stsutsui bus_space_unmap(iot, ioh, ia->ia_size);
88231b9114Stsutsui
89231b9114Stsutsui return rv;
90231b9114Stsutsui }
91231b9114Stsutsui
92231b9114Stsutsui static void
com_intio_attach(device_t parent,device_t self,void * aux)93231b9114Stsutsui com_intio_attach(device_t parent, device_t self, void *aux)
94231b9114Stsutsui {
95231b9114Stsutsui struct com_softc *sc = device_private(self);
96231b9114Stsutsui struct intio_attach_args *ia = aux;
97231b9114Stsutsui bus_space_tag_t iot;
98231b9114Stsutsui bus_space_handle_t ioh;
99231b9114Stsutsui int iobase;
100231b9114Stsutsui
101231b9114Stsutsui intio_map_allocate_region(parent, ia, INTIO_MAP_ALLOCATE);
102231b9114Stsutsui
103231b9114Stsutsui sc->sc_dev = self;
104231b9114Stsutsui iot = ia->ia_bst;
105231b9114Stsutsui iobase = ia->ia_addr;
106231b9114Stsutsui if (bus_space_map(iot, iobase, ia->ia_size,
107231b9114Stsutsui BUS_SPACE_MAP_SHIFTED_ODD, &ioh)) {
108231b9114Stsutsui aprint_error(": can't map I/O space\n");
109231b9114Stsutsui return;
110231b9114Stsutsui }
111231b9114Stsutsui
112231b9114Stsutsui /* check and set interrupt vector */
113231b9114Stsutsui if (ia->ia_intr < 16 || ia->ia_intr > 255) {
114231b9114Stsutsui aprint_error(": invalid intr vector (0x%02x)\n", ia->ia_intr);
115231b9114Stsutsui return;
116231b9114Stsutsui }
117231b9114Stsutsui bus_space_write_1(iot, ioh, COM_PSX16550_REG_VECT, ia->ia_intr);
118231b9114Stsutsui
119231b9114Stsutsui sc->sc_frequency = COM_PSX16550_FREQ;
120*6d487047Sthorpej com_init_regs(&sc->sc_regs, iot, ioh, iobase);
121231b9114Stsutsui
122231b9114Stsutsui /* PSX16550 uses MCR_DRS to switch interrupt priority level */
123231b9114Stsutsui SET(sc->sc_mcr, MCR_DRS); /* 0: ipl2 / 1: ipl4 */
124231b9114Stsutsui
125231b9114Stsutsui com_attach_subr(sc);
126231b9114Stsutsui
127231b9114Stsutsui if (intio_intr_establish(ia->ia_intr, device_xname(self),
128231b9114Stsutsui comintr, sc))
129231b9114Stsutsui aprint_error_dev(self, "can't establish interrupt handler\n");
130231b9114Stsutsui }
131