summaryrefslogtreecommitdiff
path: root/sys/arch/x68k/dev/com_intio.c
blob: d3c24d1851cff05c2c1bbdc35a3d351e984e63bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*	$NetBSD: com_intio.c,v 1.2 2018/12/08 17:46:13 thorpej Exp $	*/

/*
 * Copyright (c) 2009 Tetsuya Isaki. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * PSX16550, High-speed RS-232C board
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: com_intio.c,v 1.2 2018/12/08 17:46:13 thorpej Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/tty.h>
#include <sys/device.h>
#include <sys/bus.h>

#include <dev/ic/comreg.h>
#include <dev/ic/comvar.h>

#include <machine/cpu.h>

#include <arch/x68k/dev/intiovar.h>

#define COM_PSX16550_FREQ	(22118400 / 2)
#define COM_PSX16550_SIZE	(COM_NPORTS << 1)
#define COM_PSX16550_REG_VECT	com_scratch

static int  com_intio_match(device_t, cfdata_t, void *);
static void com_intio_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(com_intio, sizeof(struct com_softc),
    com_intio_match, com_intio_attach, NULL, NULL);

static int
com_intio_match(device_t parent, cfdata_t cf, void *aux)
{
	struct intio_attach_args *ia = aux;
	bus_space_tag_t iot;
	bus_space_handle_t ioh;
	int iobase;
	int rv;

	/* Check whether the board is inserted or not */
	if (badaddr((void *)IIOV(ia->ia_addr)))
		return 0;

	ia->ia_size = COM_PSX16550_SIZE;
	if (intio_map_allocate_region(parent, ia, INTIO_MAP_TESTONLY) < 0)
		return 0;

	iot = ia->ia_bst;
	iobase = ia->ia_addr;

	/* if it's in use as console, it's there. */
	if (com_is_console(iot, iobase, NULL))
		return 1;

	if (bus_space_map(iot, iobase, ia->ia_size,
	    BUS_SPACE_MAP_SHIFTED_ODD, &ioh))
		return 0;

	rv = comprobe1(iot, ioh);
	bus_space_unmap(iot, ioh, ia->ia_size);

	return rv;
}

static void
com_intio_attach(device_t parent, device_t self, void *aux)
{
	struct com_softc *sc = device_private(self);
	struct intio_attach_args *ia = aux;
	bus_space_tag_t iot;
	bus_space_handle_t ioh;
	int iobase;

	intio_map_allocate_region(parent, ia, INTIO_MAP_ALLOCATE);

	sc->sc_dev = self;
	iot = ia->ia_bst;
	iobase = ia->ia_addr;
	if (bus_space_map(iot, iobase, ia->ia_size,
	    BUS_SPACE_MAP_SHIFTED_ODD, &ioh)) {
		aprint_error(": can't map I/O space\n");
		return;
	}

	/* check and set interrupt vector */
	if (ia->ia_intr < 16 || ia->ia_intr > 255) {
		aprint_error(": invalid intr vector (0x%02x)\n", ia->ia_intr);
		return;
	}
	bus_space_write_1(iot, ioh, COM_PSX16550_REG_VECT, ia->ia_intr);

	sc->sc_frequency = COM_PSX16550_FREQ;
	com_init_regs(&sc->sc_regs, iot, ioh, iobase);

	/* PSX16550 uses MCR_DRS to switch interrupt priority level */
	SET(sc->sc_mcr, MCR_DRS);	/* 0: ipl2 / 1: ipl4 */

	com_attach_subr(sc);

	if (intio_intr_establish(ia->ia_intr, device_xname(self),
	    comintr, sc))
		aprint_error_dev(self, "can't establish interrupt handler\n");
}