xref: /netbsd-src/sys/arch/sparc/dev/com_ebus.c (revision 6d4870476f69938cdd065da8ea9ad989cef459f4)
1*6d487047Sthorpej /*	$NetBSD: com_ebus.c,v 1.17 2018/12/08 17:46:13 thorpej Exp $ */
278eadd17Suwe 
378eadd17Suwe /*-
478eadd17Suwe  * Copyright (c) 1998 The NetBSD Foundation, Inc.
578eadd17Suwe  * All rights reserved.
678eadd17Suwe  *
778eadd17Suwe  * This code is derived from software contributed to The NetBSD Foundation
878eadd17Suwe  * by Charles M. Hannum.
978eadd17Suwe  *
1078eadd17Suwe  * Redistribution and use in source and binary forms, with or without
1178eadd17Suwe  * modification, are permitted provided that the following conditions
1278eadd17Suwe  * are met:
1378eadd17Suwe  * 1. Redistributions of source code must retain the above copyright
1478eadd17Suwe  *    notice, this list of conditions and the following disclaimer.
1578eadd17Suwe  * 2. Redistributions in binary form must reproduce the above copyright
1678eadd17Suwe  *    notice, this list of conditions and the following disclaimer in the
1778eadd17Suwe  *    documentation and/or other materials provided with the distribution.
1878eadd17Suwe  *
1978eadd17Suwe  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2078eadd17Suwe  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2178eadd17Suwe  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2278eadd17Suwe  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2378eadd17Suwe  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2478eadd17Suwe  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2578eadd17Suwe  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2678eadd17Suwe  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2778eadd17Suwe  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2878eadd17Suwe  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2978eadd17Suwe  * POSSIBILITY OF SUCH DAMAGE.
3078eadd17Suwe  */
3178eadd17Suwe 
32a4183603Slukem #include <sys/cdefs.h>
33*6d487047Sthorpej __KERNEL_RCSID(0, "$NetBSD: com_ebus.c,v 1.17 2018/12/08 17:46:13 thorpej Exp $");
34a4183603Slukem 
3578eadd17Suwe #include <sys/param.h>
3678eadd17Suwe #include <sys/systm.h>
3778eadd17Suwe #include <sys/device.h>
3878eadd17Suwe #include <sys/termios.h>
3978eadd17Suwe 
40b6584574Sdyoung #include <sys/bus.h>
4178eadd17Suwe #include <machine/autoconf.h>
4278eadd17Suwe #include <machine/intr.h>
4378eadd17Suwe 
44e158bed8Suwe #include <dev/ebus/ebusreg.h>
45e158bed8Suwe #include <dev/ebus/ebusvar.h>
4678eadd17Suwe 
4778eadd17Suwe #include <dev/ic/comreg.h>
4878eadd17Suwe #include <dev/ic/comvar.h>
4978eadd17Suwe 
5078eadd17Suwe struct com_ebus_softc {
5178eadd17Suwe 	struct com_softc ebsc_com;	/* real "com" softc */
5278eadd17Suwe 
5378eadd17Suwe 	/* this space for rent */
5478eadd17Suwe };
5578eadd17Suwe 
56607ead0eScube static int com_ebus_match(device_t, cfdata_t , void *);
57607ead0eScube static void com_ebus_attach(device_t, device_t, void *);
5878eadd17Suwe 
59607ead0eScube CFATTACH_DECL_NEW(com_ebus, sizeof(struct com_ebus_softc),
604bf871a7Sthorpej     com_ebus_match, com_ebus_attach, NULL, NULL);
6178eadd17Suwe 
6278eadd17Suwe static int
com_ebus_match(device_t parent,cfdata_t cf,void * aux)63607ead0eScube com_ebus_match(device_t parent, cfdata_t cf, void *aux)
6478eadd17Suwe {
6578eadd17Suwe 	struct ebus_attach_args *ea = aux;
6678eadd17Suwe 	bus_space_handle_t ioh;
6778eadd17Suwe 	int match;
6878eadd17Suwe 
6978eadd17Suwe 	if (strcmp(ea->ea_name, "su") != 0)
7078eadd17Suwe 		return (0);
7178eadd17Suwe 
7278eadd17Suwe 	match = 0;
7340a8bc1dSuwe 	if (bus_space_map(ea->ea_bustag, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
7440a8bc1dSuwe 			  ea->ea_reg[0].size, 0, &ioh) == 0)
7578eadd17Suwe 	{
7678eadd17Suwe 		match = comprobe1(ea->ea_bustag, ioh);
7778eadd17Suwe 		bus_space_unmap(ea->ea_bustag, ioh, ea->ea_reg[0].size);
7878eadd17Suwe 	}
7978eadd17Suwe 
8078eadd17Suwe 	return (match);
8178eadd17Suwe }
8278eadd17Suwe 
8378eadd17Suwe static void
com_ebus_attach(device_t parent,device_t self,void * aux)84607ead0eScube com_ebus_attach(device_t parent, device_t self, void *aux)
8578eadd17Suwe {
86607ead0eScube 	struct com_ebus_softc *ebsc = device_private(self);
8778eadd17Suwe 	struct com_softc *sc = &ebsc->ebsc_com;
8878eadd17Suwe 	struct ebus_attach_args *ea = aux;
8934537908Sgdamore 	bus_space_tag_t iot;
9034537908Sgdamore 	bus_space_handle_t ioh;
9134537908Sgdamore 	bus_addr_t iobase;
9278eadd17Suwe 
93607ead0eScube 	sc->sc_dev = self;
9434537908Sgdamore 	iot = ea->ea_bustag;
9534537908Sgdamore 	iobase = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
9678eadd17Suwe 	sc->sc_frequency = COM_FREQ;
974a0ee23bSthorpej 	sc->sc_hwflags = COM_HW_NO_TXPRELOAD;
9878eadd17Suwe 
9978eadd17Suwe 	/*
10078eadd17Suwe 	 * XXX: It would be nice to be able to split console input and
10178eadd17Suwe 	 * output to different devices.  For now switch to serial
10278eadd17Suwe 	 * console if PROM stdin is on serial (so that we can use DDB).
10378eadd17Suwe 	 */
10478eadd17Suwe 	if (prom_instance_to_package(prom_stdin()) == ea->ea_node)
10534537908Sgdamore 		comcnattach(iot, iobase, B9600, sc->sc_frequency,
10634537908Sgdamore 		    COM_TYPE_NORMAL, (CLOCAL | CREAD | CS8));
10778eadd17Suwe 
10834537908Sgdamore 	if (!com_is_console(iot, iobase, &ioh)
10934537908Sgdamore 	    && bus_space_map(iot, iobase, ea->ea_reg[0].size,
11034537908Sgdamore 			     0, &ioh) != 0)
11178eadd17Suwe 	{
112607ead0eScube 		aprint_error(": unable to map device registers\n");
11378eadd17Suwe 		return;
11478eadd17Suwe 	}
11578eadd17Suwe 
116*6d487047Sthorpej 	com_init_regs(&sc->sc_regs, iot, ioh, iobase);
11734537908Sgdamore 
11878eadd17Suwe 	com_attach_subr(sc);
11978eadd17Suwe 
12078eadd17Suwe 	if (ea->ea_nintr != 0)
12134537908Sgdamore 		(void)bus_intr_establish(iot, ea->ea_intr[0], IPL_SERIAL,
12278eadd17Suwe 					 comintr, sc);
12378eadd17Suwe }
124