Home | History | Annotate | Line # | Download | only in dev
com_opb.c revision 1.22
      1 /* $NetBSD: com_opb.c,v 1.22 2018/12/08 17:46:12 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Eduardo Horvath and Simon Burge for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (c) 1998
     40  *	Matthias Drochner.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     52  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     53  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     54  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     55  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     56  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     60  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: com_opb.c,v 1.22 2018/12/08 17:46:12 thorpej Exp $");
     65 
     66 #include <sys/param.h>
     67 #include <sys/device.h>
     68 #include <sys/tty.h>
     69 #include <sys/systm.h>
     70 #include <sys/cpu.h>
     71 
     72 #include <lib/libkern/libkern.h>
     73 
     74 #include <powerpc/ibm4xx/cpu.h>
     75 
     76 #include <powerpc/ibm4xx/dev/opbvar.h>
     77 #include <powerpc/ibm4xx/dev/comopbvar.h>
     78 
     79 #include "com.h"
     80 #if (NCOM > 0)
     81 #include <dev/ic/comreg.h>
     82 #include <dev/ic/comvar.h>
     83 #endif
     84 
     85 struct com_opb_softc {
     86 	struct com_softc sc_com;
     87 	void *sc_ih;
     88 };
     89 
     90 static int	com_opb_probe(device_t, cfdata_t , void *);
     91 static void	com_opb_attach(device_t, device_t, void *);
     92 
     93 CFATTACH_DECL_NEW(com_opb, sizeof(struct com_opb_softc),
     94     com_opb_probe, com_opb_attach, NULL, NULL);
     95 
     96 int
     97 com_opb_probe(device_t parent, cfdata_t cf, void *aux)
     98 {
     99 	struct opb_attach_args *oaa = aux;
    100 
    101 	/* match only com devices */
    102 	if (strcmp(oaa->opb_name, cf->cf_name) != 0)
    103 		return 0;
    104 
    105 	return (1);
    106 }
    107 
    108 void
    109 com_opb_attach(device_t parent, device_t self, void *aux)
    110 {
    111 	struct com_opb_softc *msc = device_private(self);
    112 	struct com_softc *sc = &msc->sc_com;
    113 	struct opb_attach_args *oaa = aux;
    114 	prop_number_t freq;
    115 	bus_space_handle_t ioh;
    116 
    117 	sc->sc_dev = self;
    118 
    119 	/* XXX console check */
    120 
    121 	bus_space_map(oaa->opb_bt, oaa->opb_addr, COM_NPORTS, 0, &ioh);
    122 	com_init_regs(&sc->sc_regs, oaa->opb_bt, ioh, oaa->opb_addr);
    123 
    124 	freq = prop_dictionary_get(device_properties(sc->sc_dev),
    125 	    "clock-frequency");
    126 	if (freq == NULL) {
    127 		aprint_error(": unable to get clock-frequency property\n");
    128 		return;
    129 	}
    130 	KASSERT(prop_object_type(freq) == PROP_TYPE_NUMBER);
    131 	sc->sc_frequency = (int) prop_number_integer_value(freq);
    132 
    133 	com_attach_subr(sc);
    134 
    135 	intr_establish(oaa->opb_irq, IST_LEVEL, IPL_SERIAL, comintr, sc);
    136 }
    137 
    138 /*
    139  * com_opb_cnattach:
    140  * Initialize the system console.
    141  */
    142 void
    143 com_opb_cnattach(int com_freq, int conaddr, int conspeed, int conmode)
    144 {
    145 	static int attached = 0;
    146 #if (NCOM > 0)
    147 	struct com_regs	regs;
    148 #endif
    149 
    150 	if (attached)
    151 		return;
    152 	attached = 1;
    153 
    154 #if (NCOM > 0)
    155 	/* We *know* the com-console attaches to opb */
    156 	regs.cr_iot = opb_get_bus_space_tag();
    157 	regs.cr_iobase = conaddr;
    158 	regs.cr_nports = COM_NPORTS;
    159 	/* regs.ioh is initialized by comcnattach */
    160 
    161 	if (comcnattach1(&regs, conspeed, com_freq, COM_TYPE_NORMAL, conmode))
    162 		panic("can't init serial console @%x", conaddr);
    163 	else
    164 		return;
    165 #endif
    166 	panic("console device missing -- serial console not in kernel");
    167 	/* Of course, this is moot if there is no console... */
    168 }
    169 
    170 /*
    171  * com_opb_device_register:
    172  */
    173 void
    174 com_opb_device_register(device_t dev, int frequency)
    175 {
    176 	/* Set the frequency of the on-chip UART. */
    177 	prop_number_t pn = prop_number_create_integer(frequency);
    178 	KASSERT(pn != NULL);
    179 
    180 	if (prop_dictionary_set(device_properties(dev),
    181 				"clock-frequency", pn) == false) {
    182 		printf("WARNING: unable to set clock-frequency "
    183 			"property for %s\n", device_xname(dev));
    184 	}
    185 	prop_object_release(pn);
    186 }
    187