xref: /netbsd-src/sys/arch/powerpc/ibm4xx/dev/com_opb.c (revision 0ece553e21bb21e22506ec4b07fc6913593f1fa5)
1*0ece553eSrin /* $NetBSD: com_opb.c,v 1.25 2021/02/27 20:43:58 rin Exp $ */
26bf1aaf8Ssimonb 
36bf1aaf8Ssimonb /*
46bf1aaf8Ssimonb  * Copyright 2001 Wasabi Systems, Inc.
56bf1aaf8Ssimonb  * All rights reserved.
66bf1aaf8Ssimonb  *
76bf1aaf8Ssimonb  * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
86bf1aaf8Ssimonb  *
96bf1aaf8Ssimonb  * Redistribution and use in source and binary forms, with or without
106bf1aaf8Ssimonb  * modification, are permitted provided that the following conditions
116bf1aaf8Ssimonb  * are met:
126bf1aaf8Ssimonb  * 1. Redistributions of source code must retain the above copyright
136bf1aaf8Ssimonb  *    notice, this list of conditions and the following disclaimer.
146bf1aaf8Ssimonb  * 2. Redistributions in binary form must reproduce the above copyright
156bf1aaf8Ssimonb  *    notice, this list of conditions and the following disclaimer in the
166bf1aaf8Ssimonb  *    documentation and/or other materials provided with the distribution.
176bf1aaf8Ssimonb  * 3. All advertising materials mentioning features or use of this software
186bf1aaf8Ssimonb  *    must display the following acknowledgement:
196bf1aaf8Ssimonb  *      This product includes software developed for the NetBSD Project by
206bf1aaf8Ssimonb  *      Wasabi Systems, Inc.
216bf1aaf8Ssimonb  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
226bf1aaf8Ssimonb  *    or promote products derived from this software without specific prior
236bf1aaf8Ssimonb  *    written permission.
246bf1aaf8Ssimonb  *
256bf1aaf8Ssimonb  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
266bf1aaf8Ssimonb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
276bf1aaf8Ssimonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
286bf1aaf8Ssimonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
296bf1aaf8Ssimonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
306bf1aaf8Ssimonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
316bf1aaf8Ssimonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
326bf1aaf8Ssimonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
336bf1aaf8Ssimonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
346bf1aaf8Ssimonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
356bf1aaf8Ssimonb  * POSSIBILITY OF SUCH DAMAGE.
366bf1aaf8Ssimonb  */
376bf1aaf8Ssimonb 
38d2306c8cSshige /*
39d2306c8cSshige  * Copyright (c) 1998
40d2306c8cSshige  *	Matthias Drochner.  All rights reserved.
41d2306c8cSshige  *
42d2306c8cSshige  * Redistribution and use in source and binary forms, with or without
43d2306c8cSshige  * modification, are permitted provided that the following conditions
44d2306c8cSshige  * are met:
45d2306c8cSshige  * 1. Redistributions of source code must retain the above copyright
46d2306c8cSshige  *    notice, this list of conditions and the following disclaimer.
47d2306c8cSshige  * 2. Redistributions in binary form must reproduce the above copyright
48d2306c8cSshige  *    notice, this list of conditions and the following disclaimer in the
49d2306c8cSshige  *    documentation and/or other materials provided with the distribution.
50d2306c8cSshige  *
51d2306c8cSshige  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52d2306c8cSshige  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53d2306c8cSshige  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54d2306c8cSshige  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55d2306c8cSshige  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56d2306c8cSshige  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57d2306c8cSshige  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58d2306c8cSshige  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59d2306c8cSshige  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60d2306c8cSshige  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61d2306c8cSshige  */
62d2306c8cSshige 
63ed517291Slukem #include <sys/cdefs.h>
64*0ece553eSrin __KERNEL_RCSID(0, "$NetBSD: com_opb.c,v 1.25 2021/02/27 20:43:58 rin Exp $");
6516031f7dSrin 
6616031f7dSrin #include "com.h"
67ed517291Slukem 
686bf1aaf8Ssimonb #include <sys/param.h>
696bf1aaf8Ssimonb #include <sys/device.h>
706bf1aaf8Ssimonb #include <sys/tty.h>
716bf1aaf8Ssimonb #include <sys/systm.h>
721fd2c684Smatt #include <sys/cpu.h>
736bf1aaf8Ssimonb 
746bf1aaf8Ssimonb #include <lib/libkern/libkern.h>
756bf1aaf8Ssimonb 
761fd2c684Smatt #include <powerpc/ibm4xx/cpu.h>
77f384fe8bSscw 
786bf1aaf8Ssimonb #include <powerpc/ibm4xx/dev/opbvar.h>
79d2306c8cSshige #include <powerpc/ibm4xx/dev/comopbvar.h>
806bf1aaf8Ssimonb 
81d2306c8cSshige #if (NCOM > 0)
826bf1aaf8Ssimonb #include <dev/ic/comreg.h>
836bf1aaf8Ssimonb #include <dev/ic/comvar.h>
84d2306c8cSshige #endif
856bf1aaf8Ssimonb 
866bf1aaf8Ssimonb struct com_opb_softc {
876bf1aaf8Ssimonb 	struct com_softc sc_com;
886bf1aaf8Ssimonb 	void *sc_ih;
896bf1aaf8Ssimonb };
906bf1aaf8Ssimonb 
91607ead0eScube static int	com_opb_probe(device_t, cfdata_t , void *);
92607ead0eScube static void	com_opb_attach(device_t, device_t, void *);
936bf1aaf8Ssimonb 
94607ead0eScube CFATTACH_DECL_NEW(com_opb, sizeof(struct com_opb_softc),
9589bf5a8fSthorpej     com_opb_probe, com_opb_attach, NULL, NULL);
966bf1aaf8Ssimonb 
976bf1aaf8Ssimonb int
com_opb_probe(device_t parent,cfdata_t cf,void * aux)98607ead0eScube com_opb_probe(device_t parent, cfdata_t cf, void *aux)
996bf1aaf8Ssimonb {
1006bf1aaf8Ssimonb 	struct opb_attach_args *oaa = aux;
1016bf1aaf8Ssimonb 
1026bf1aaf8Ssimonb 	/* match only com devices */
103d1ad2ac4Sthorpej 	if (strcmp(oaa->opb_name, cf->cf_name) != 0)
1046bf1aaf8Ssimonb 		return 0;
1056bf1aaf8Ssimonb 
106bff11b16Ssimonb 	return (1);
1076bf1aaf8Ssimonb }
1086bf1aaf8Ssimonb 
1096bf1aaf8Ssimonb void
com_opb_attach(device_t parent,device_t self,void * aux)110607ead0eScube com_opb_attach(device_t parent, device_t self, void *aux)
1116bf1aaf8Ssimonb {
112607ead0eScube 	struct com_opb_softc *msc = device_private(self);
1136bf1aaf8Ssimonb 	struct com_softc *sc = &msc->sc_com;
1146bf1aaf8Ssimonb 	struct opb_attach_args *oaa = aux;
115fb44a857Sthorpej 	prop_number_t freq;
11634537908Sgdamore 	bus_space_handle_t ioh;
117f384fe8bSscw 
118607ead0eScube 	sc->sc_dev = self;
119607ead0eScube 
1205b7c5eadSthorpej 	/* XXX console check */
1215b7c5eadSthorpej 
12234537908Sgdamore 	bus_space_map(oaa->opb_bt, oaa->opb_addr, COM_NPORTS, 0, &ioh);
1236d487047Sthorpej 	com_init_regs(&sc->sc_regs, oaa->opb_bt, ioh, oaa->opb_addr);
124f384fe8bSscw 
125607ead0eScube 	freq = prop_dictionary_get(device_properties(sc->sc_dev),
12655e8bbebSthorpej 	    "clock-frequency");
127fb44a857Sthorpej 	if (freq == NULL) {
128607ead0eScube 		aprint_error(": unable to get clock-frequency property\n");
1295b7c5eadSthorpej 		return;
1305b7c5eadSthorpej 	}
131fb44a857Sthorpej 	KASSERT(prop_object_type(freq) == PROP_TYPE_NUMBER);
132fb44a857Sthorpej 	sc->sc_frequency = (int) prop_number_integer_value(freq);
1336bf1aaf8Ssimonb 
1346bf1aaf8Ssimonb 	com_attach_subr(sc);
1356bf1aaf8Ssimonb 
136*0ece553eSrin 	intr_establish_xname(oaa->opb_irq, IST_LEVEL, IPL_SERIAL, comintr, sc,
137*0ece553eSrin 	    device_xname(self));
1386bf1aaf8Ssimonb }
139d2306c8cSshige 
140d2306c8cSshige /*
141d2306c8cSshige  * com_opb_cnattach:
142d2306c8cSshige  * Initialize the system console.
143d2306c8cSshige  */
144d2306c8cSshige void
com_opb_cnattach(int com_freq,int conaddr,int conspeed,int conmode)145d2306c8cSshige com_opb_cnattach(int com_freq, int conaddr, int conspeed, int conmode)
146d2306c8cSshige {
147d2306c8cSshige 	static int attached = 0;
148d2306c8cSshige 
149d2306c8cSshige 	if (attached)
150d2306c8cSshige 		return;
151d2306c8cSshige 	attached = 1;
152d2306c8cSshige 
153d2306c8cSshige #if (NCOM > 0)
154d2306c8cSshige 	/* We *know* the com-console attaches to opb */
155d83f8a8eSrin 	if (comcnattach(opb_get_bus_space_tag(), conaddr, conspeed, com_freq,
156d83f8a8eSrin 	    COM_TYPE_NORMAL, conmode))
157d2306c8cSshige 		panic("can't init serial console @%x", conaddr);
158d2306c8cSshige 	else
159d2306c8cSshige 		return;
160d2306c8cSshige #endif
161d2306c8cSshige 	panic("console device missing -- serial console not in kernel");
162d2306c8cSshige 	/* Of course, this is moot if there is no console... */
163d2306c8cSshige }
164d2306c8cSshige 
165d2306c8cSshige /*
166d2306c8cSshige  * com_opb_device_register:
167d2306c8cSshige  */
168d2306c8cSshige void
com_opb_device_register(device_t dev,int frequency)169607ead0eScube com_opb_device_register(device_t dev, int frequency)
170d2306c8cSshige {
171d2306c8cSshige 	/* Set the frequency of the on-chip UART. */
172fb44a857Sthorpej 	prop_number_t pn = prop_number_create_integer(frequency);
173fb44a857Sthorpej 	KASSERT(pn != NULL);
174fb44a857Sthorpej 
175fb44a857Sthorpej 	if (prop_dictionary_set(device_properties(dev),
176c8daa94cSthorpej 				"clock-frequency", pn) == false) {
17755e8bbebSthorpej 		printf("WARNING: unable to set clock-frequency "
178607ead0eScube 			"property for %s\n", device_xname(dev));
179fb44a857Sthorpej 	}
180fb44a857Sthorpej 	prop_object_release(pn);
181d2306c8cSshige }
182