1/*	$NetBSD: com_pioc.c,v 1.15 2011/06/03 07:35:37 matt Exp $	*/
2
3/*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
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 * Copyright (c) 1991 The Regents of the University of California.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 *    may be used to endorse or promote products derived from this software
46 *    without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 *	@(#)com.c	7.5 (Berkeley) 5/16/91
61 */
62
63#include <sys/param.h>
64
65__KERNEL_RCSID(0, "$NetBSD: com_pioc.c,v 1.15 2011/06/03 07:35:37 matt Exp $");
66
67#include <sys/systm.h>
68#include <sys/tty.h>
69#include <sys/proc.h>
70#include <sys/conf.h>
71#include <sys/kernel.h>
72#include <sys/device.h>
73#include <sys/bus.h>
74
75#include <machine/intr.h>
76#include <machine/io.h>
77
78#include <acorn32/mainbus/piocvar.h>
79#include <dev/ic/comreg.h>
80#include <dev/ic/comvar.h>
81
82#include <dev/cons.h>
83
84#include "locators.h"
85
86struct com_pioc_softc {
87	struct	com_softc sc_com;	/* real "com" softc */
88	void	*sc_ih;			/* interrupt handler */
89};
90
91/* Prototypes for functions */
92
93cons_decl(com);
94
95static int  com_pioc_probe   (device_t, cfdata_t , void *);
96static void com_pioc_attach  (device_t, device_t, void *);
97
98/* device attach structure */
99
100CFATTACH_DECL_NEW(com_pioc, sizeof(struct com_pioc_softc),
101    com_pioc_probe, com_pioc_attach, NULL, NULL);
102
103extern bus_space_tag_t comconstag;	/* From pioc.c */
104
105/*
106 * int com_pioc_probe(device_t parent, cfdata_t cf, void *aux)
107 *
108 * Make sure we are trying to attach a com device and then
109 * probe for one.
110 */
111
112static int
113com_pioc_probe(device_t parent, cfdata_t cf, void *aux)
114{
115	bus_space_tag_t iot;
116	bus_space_handle_t ioh;
117	int iobase;
118	int rv = 1;
119	struct pioc_attach_args *pa = aux;
120
121	/* We need an offset */
122	if (pa->pa_offset == PIOCCF_OFFSET_DEFAULT)
123		return(0);
124
125	iot = pa->pa_iot;
126	iobase = pa->pa_iobase + pa->pa_offset;
127
128	/* if it's in use as console, it's there. */
129	if (!com_is_console(iot, iobase, 0)) {
130		if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
131			return 0;
132		}
133		rv = comprobe1(iot, ioh);
134		bus_space_unmap(iot, ioh, COM_NPORTS);
135	}
136
137	if (rv) {
138		pa->pa_iosize = COM_NPORTS;
139	}
140	return (rv);
141}
142
143/*
144 * void com_pioc_attach(device_t parent, device_t self, void *aux)
145 *
146 * attach the com device
147 */
148
149static void
150com_pioc_attach(device_t parent, device_t self, void *aux)
151{
152	struct com_pioc_softc *psc = device_private(self);
153	struct com_softc *sc = &psc->sc_com;
154	u_int iobase;
155	bus_space_tag_t iot;
156	bus_space_handle_t ioh;
157	struct pioc_attach_args *pa = aux;
158	int count;
159
160	sc->sc_dev = self;
161	iot = pa->pa_iot;
162	iobase = pa->pa_iobase + pa->pa_offset;
163
164/*
165	printf(" (iot = %p, iobase = 0x%08x) ", iot, iobase);
166*/
167	if (!com_is_console(iot, iobase, &ioh)
168	    && bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
169		panic("comattach: io mapping failed");
170	COM_INIT_REGS(sc->sc_regs, iot, ioh, iobase);
171
172	sc->sc_frequency = COM_FREQ;
173
174	com_attach_subr(sc);
175
176	if (pa->pa_irq != MAINBUSCF_IRQ_DEFAULT) {
177		psc->sc_ih = intr_claim(pa->pa_irq, IPL_SERIAL, "com",
178		    comintr, sc);
179	}
180
181	if (!pmf_device_register1(self, com_suspend, com_resume, com_cleanup)) {
182		aprint_error_dev(self,
183		    "could not establish shutdown hook");
184	}
185
186	/*
187	 * This is a patch for bugged revision 1-4 SMC FDC37C665
188	 * I/O controllers.
189	 * If there is RX data pending when the FIFO in turned on
190	 * the RX register cannot be emptied.
191	 *
192	 * Solution:
193	 *   Make sure FIFO is off.
194	 *   Read pending data / int status etc.
195	 */
196	bus_space_write_1(iot, ioh, com_fifo, 0);
197	for (count = 0; count < 8; ++count)
198		(void)bus_space_read_1(iot, ioh, count);
199
200}
201
202/*
203 * Console attachment functions
204 */
205
206void
207comcnprobe(struct consdev *cp)
208{
209
210#ifdef  COMCONSOLE
211	cp->cn_pri = CN_REMOTE;	/* Force a serial port console */
212#else
213	cp->cn_pri = CN_NORMAL;
214#endif
215}
216
217void
218comcninit(struct consdev *cp)
219{
220	int result;
221
222#ifndef CONMODE
223#define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
224#endif
225#ifndef CONSPEED
226#define CONSPEED 38400
227#endif
228#ifndef CONADDR
229#define CONADDR	0x3f8
230#endif
231
232	result = comcnattach(comconstag, (IO_CONF_BASE + CONADDR), CONSPEED,
233	    COM_FREQ, COM_TYPE_NORMAL, CONMODE);
234	if (result) {
235		printf("initialising serial; got errornr %d\n", result);
236		panic("can't init serial console @%x", CONADDR);
237	};
238}
239
240
241/* End of com_pioc.c */
242