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