1/*	$NetBSD: zs_pcctwo.c,v 1.13 2008/03/29 19:15:35 tsutsui Exp $	*/
2
3/*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gordon W. Ross, Jason R. Thorpe and Steve C. Woodford.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Zilog Z8530 Dual UART driver (machine-dependent part)
34 *
35 * Runs two serial lines per chip using slave drivers.
36 * Plain tty/async lines use the zs_async slave.
37 *
38 * Modified for NetBSD/mvme68k by Jason R. Thorpe <thorpej@NetBSD.org>
39 *
40 * Modified to attach to the PCCchip2/MCchip backend by Steve Woodford.
41 */
42
43#include <sys/cdefs.h>
44__KERNEL_RCSID(0, "$NetBSD: zs_pcctwo.c,v 1.13 2008/03/29 19:15:35 tsutsui Exp $");
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/proc.h>
49#include <sys/device.h>
50#include <sys/conf.h>
51#include <sys/file.h>
52#include <sys/ioctl.h>
53#include <sys/tty.h>
54#include <sys/time.h>
55#include <sys/kernel.h>
56#include <sys/syslog.h>
57
58#include <dev/cons.h>
59#include <dev/ic/z8530reg.h>
60#include <machine/z8530var.h>
61
62#include <machine/cpu.h>
63#include <machine/bus.h>
64
65#include <dev/mvme/pcctworeg.h>
66#include <dev/mvme/pcctwovar.h>
67
68#include <mvme68k/dev/mainbus.h>
69#include <mvme68k/dev/zsvar.h>
70
71#include "ioconf.h"
72
73/* Definition of the driver for autoconfig. */
74static int	zsc_pcctwo_match(device_t, cfdata_t, void *);
75static void	zsc_pcctwo_attach(device_t, device_t, void *);
76
77CFATTACH_DECL_NEW(zsc_pcctwo, sizeof(struct zsc_softc),
78    zsc_pcctwo_match, zsc_pcctwo_attach, NULL, NULL);
79
80cons_decl(zsc_pcctwo);
81
82
83/*
84 * Is the zs chip present?
85 */
86static int
87zsc_pcctwo_match(device_t parent, cfdata_t cf, void *aux)
88{
89	struct pcctwo_attach_args *pa = aux;
90
91	if (strcmp(pa->pa_name, zsc_cd.cd_name) ||
92	    (machineid != MVME_162 && machineid != MVME_172))
93		return 0;
94
95	pa->pa_ipl = cf->pcctwocf_ipl;
96	if (pa->pa_ipl == -1)
97		pa->pa_ipl = ZSHARD_PRI;
98	return 1;
99}
100
101/*
102 * Attach a found zs.
103 */
104static void
105zsc_pcctwo_attach(device_t parent, device_t self, void *aux)
106{
107	struct zsc_softc *zsc = device_private(self);
108	struct pcctwo_attach_args *pa = aux;
109	struct zsdevice zs;
110	bus_space_handle_t bush;
111	int zs_level;
112	static int vector = MCCHIPV_ZS0;
113
114	zsc->zsc_dev = self;
115
116	/* Map the device's registers */
117	bus_space_map(pa->pa_bust, pa->pa_offset, 8, 0, &bush);
118
119	zs_level = pa->pa_ipl;
120
121	/* XXX: This is a gross hack. I need to bus-space zs.c ... */
122	zs.zs_chan_b.zc_csr = (volatile uint8_t *)bush + 1;
123	zs.zs_chan_b.zc_data = (volatile uint8_t *)bush + 3;
124	zs.zs_chan_a.zc_csr = (volatile uint8_t *)bush + 5;
125	zs.zs_chan_a.zc_data = (volatile uint8_t *)bush + 7;
126
127	/* Do common parts of SCC configuration. */
128	zs_config(zsc, &zs, vector + PCCTWO_VECBASE, PCLK_162);
129
130	evcnt_attach_dynamic(&zsc->zsc_evcnt, EVCNT_TYPE_INTR,
131	    pcctwointr_evcnt(zs_level), "rs232", device_xname(zsc->zsc_dev));
132
133	/*
134	 * Now safe to install interrupt handlers.
135	 */
136	pcctwointr_establish(vector++, zshard_unshared, zs_level, zsc, NULL);
137
138	/*
139	 * Set master interrupt enable.
140	 */
141	zs_write_reg(zsc->zsc_cs[0], 9, zs_init_reg[9]);
142}
143
144/****************************************************************
145 * Console support functions (MVME PCC specific!)
146 ****************************************************************/
147
148/*
149 * Check for SCC console.  The MVME-1x2 always uses unit 0 chan 0.
150 */
151void
152zsc_pcctwocnprobe(struct consdev *cp)
153{
154	extern const struct cdevsw zstty_cdevsw;
155
156	if (machineid != MVME_162 && machineid != MVME_172) {
157		cp->cn_pri = CN_DEAD;
158		return;
159	}
160
161	/* Initialize required fields. */
162	cp->cn_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), 0);
163	cp->cn_pri = CN_NORMAL;
164}
165
166void
167zsc_pcctwocninit(struct consdev *cp)
168{
169	bus_space_handle_t bush;
170	struct zsdevice zs;
171
172	bus_space_map(&_mainbus_space_tag,
173	    intiobase_phys + MAINBUS_PCCTWO_OFFSET + MCCHIP_ZS0_OFF, 8, 0,
174	    &bush);
175
176	/* XXX: This is a gross hack. I need to bus-space zs.c ... */
177	zs.zs_chan_b.zc_csr = (volatile uint8_t *)bush + 1;
178	zs.zs_chan_b.zc_data = (volatile uint8_t *)bush + 3;
179	zs.zs_chan_a.zc_csr = (volatile uint8_t *)bush + 5;
180	zs.zs_chan_a.zc_data = (volatile uint8_t *)bush + 7;
181
182	/* Do common parts of console init. */
183	zs_cnconfig(0, 0, &zs, PCLK_162);
184}
185