Home | History | Annotate | Line # | Download | only in sbus
      1 /*	$NetBSD: sio16.c,v 1.28 2021/12/07 22:13:56 andvar Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2001 Matthew R. Green
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * aurora technologies nova16 driver.  this board is an sbus card with
     31  * an external 16 port serial box.  there are two cirrus logic cd180
     32  * 8 port serial chips used in the implementation.
     33  *
     34  * thanks go to Oliver Aldulea <oli (at) morcov.bv.ro> for writing the
     35  * linux driver of this that helped clear up a couple of issues.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: sio16.c,v 1.28 2021/12/07 22:13:56 andvar Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/conf.h>
     43 #include <sys/device.h>
     44 #include <sys/systm.h>
     45 
     46 #include <machine/autoconf.h>
     47 
     48 #include <dev/ic/cd18xxvar.h>
     49 #include <dev/ic/cd18xxreg.h>
     50 
     51 #include "ioconf.h"
     52 #include "locators.h"
     53 
     54 /* 1600se configuration register bits */
     55 #define SIO16_CONFIGREG_ENABLE_IO    8
     56 #define SIO16_CONFIGREG_ENABLE_IRQ   4
     57 
     58 /* 1600se interrupt ackknowledge register bytes */
     59 #define	SIO16_MINT_ACK		1	/* modem interrupt acknowledge */
     60 #define	SIO16_TINT_ACK		2	/* tx interrupt acknowledge */
     61 #define	SIO16_RINT_ACK		3	/* rx interrupt acknowledge */
     62 
     63 /*
     64  * device cfattach and cfdriver definitions, plus the routine we pass
     65  * to the cd18xx code or interrupt acknowledgement.
     66  */
     67 static int	sio16_match(device_t, cfdata_t, void *);
     68 static void	sio16_attach(device_t, device_t, void *);
     69 static u_char	sio16_ackfunc(void *, int who);
     70 
     71 /*
     72  * define the sio16 per-device softc.
     73  */
     74 struct sio16_softc {
     75 	device_t sc_dev;
     76 
     77 	/* sbus information */
     78 	bus_space_tag_t	sc_tag;			/* bus tag for below */
     79 	bus_space_handle_t sc_configreg;	/* configuration register */
     80 	bus_space_handle_t sc_reg[2];		/* cd180 register sets */
     81 	bus_space_handle_t sc_ack;		/* interrupt acknowledgement */
     82 #define	SIO16_1600SE	0x00000001
     83 
     84 	u_int           sc_clk;
     85 
     86 	/* cd180 information */
     87 	int		sc_ncd180;
     88 
     89 };
     90 
     91 CFATTACH_DECL_NEW(siosixteen, sizeof(struct sio16_softc),
     92     sio16_match, sio16_attach, NULL, NULL);
     93 
     94 struct sio16_attach_args {
     95 	bus_space_tag_t		cd_tag;
     96 	bus_space_handle_t	cd_handle;
     97 	u_char			(*cd_ackfunc)(void *, int);
     98 	void			*cd_ackfunc_arg;
     99 	u_int			cd_osc;
    100 };
    101 
    102 /*
    103  * device match routine:  is there an sio16 present?
    104  *
    105  * note that we can not put "sio16" in the cfdriver, as this is an
    106  * illegal name, so we have to hard code it here.
    107  */
    108 #define	SIO16_ROM_NAME	"sio16"
    109 int
    110 sio16_match(device_t parent, cfdata_t cf, void *aux)
    111 {
    112 	struct sbus_attach_args *sa = aux;
    113 
    114 	/* does the prom think i'm an sio16? */
    115 	if (strcmp(SIO16_ROM_NAME, sa->sa_name) != 0)
    116 		return (0);
    117 
    118 	return (1);
    119 }
    120 
    121 /*
    122  * device attach routine:  go attach all sub devices.
    123  */
    124 void
    125 sio16_attach(device_t parent, device_t self, void *aux)
    126 {
    127 	struct sbus_attach_args *sa = aux;
    128 	struct sio16_softc *sc = device_private(self);
    129 	bus_space_handle_t h;
    130 	char *mode, *model;
    131 	int i;
    132 
    133 	sc->sc_dev = self;
    134 
    135 	if (sa->sa_nreg != 4)
    136 		panic("sio16_attach: got %d registers instead of 4",
    137 		    sa->sa_nreg);
    138 
    139 	/* copy our bus tag, we will need it */
    140 	sc->sc_tag = sa->sa_bustag;
    141 
    142 	/*
    143 	 * sio16 has 4 register mappings.  a single byte configuration
    144 	 * register, 2 128 byte regions for the cd180 registers, and
    145 	 * a 4 byte region for interrupt acknowledgement.
    146 	 */
    147 	if (sbus_bus_map(sa->sa_bustag,
    148 			 sa->sa_reg[0].oa_space,
    149 			 sa->sa_reg[0].oa_base,
    150 			 sa->sa_reg[0].oa_size,
    151 			 0, &h) != 0) {
    152 		printf("%s at sbus: can not map registers 0\n",
    153 		    device_xname(self));
    154 		return;
    155 	}
    156 	sc->sc_configreg = h;
    157 	if (sbus_bus_map(sa->sa_bustag,
    158 			 sa->sa_reg[1].sbr_slot,
    159 			 sa->sa_reg[1].sbr_offset,
    160 			 sa->sa_reg[1].sbr_size,
    161 			 0, &h) != 0) {
    162 		printf("%s at sbus: can not map registers 1\n",
    163 		    device_xname(self));
    164 		return;
    165 	}
    166 	sc->sc_reg[0] = h;
    167 	if (sbus_bus_map(sa->sa_bustag,
    168 			 sa->sa_reg[2].sbr_slot,
    169 			 sa->sa_reg[2].sbr_offset,
    170 			 sa->sa_reg[2].sbr_size,
    171 			 0, &h) != 0) {
    172 		printf("%s at sbus: can not map registers 2\n",
    173 		    device_xname(self));
    174 		return;
    175 	}
    176 	sc->sc_reg[1] = h;
    177 	if (sbus_bus_map(sa->sa_bustag,
    178 			 sa->sa_reg[3].sbr_slot,
    179 			 sa->sa_reg[3].sbr_offset,
    180 			 sa->sa_reg[3].sbr_size,
    181 			 0, &h) != 0) {
    182 		printf("%s at sbus: can not map registers 3\n",
    183 		    device_xname(self));
    184 		return;
    185 	}
    186 	sc->sc_ack = h;
    187 
    188 	mode = prom_getpropstring(sa->sa_node, "mode");
    189 	if (mode)
    190 		printf(", %s mode", mode);
    191 
    192 	/* get the clock frequency */
    193 	sc->sc_clk = prom_getpropint(sa->sa_node, "clk", 24000);
    194 
    195 	model = prom_getpropstring(sa->sa_node, "model");
    196 	if (model == 0) {
    197 		printf(", no model property, bailing\n");
    198 		return;
    199 	}
    200 
    201 	/* are we an 1600se? */
    202 	if (strcmp(model, "1600se") == 0) {
    203 		printf(", 16 channels");
    204 		sc->sc_ncd180 = 2;
    205 	} else {
    206 		printf(", don't know model %s, bailing\n", model);
    207 		return;
    208 	}
    209 
    210 	/* establish interrupt channel */
    211 	(void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_TTY,
    212 	    cd18xx_hardintr, sc);
    213 
    214 	/* reset the board, and turn on interrupts and I/O */
    215 	bus_space_write_1(sa->sa_bustag, sc->sc_configreg, 0, 0);
    216 	delay(100);
    217 	bus_space_write_1(sa->sa_bustag, sc->sc_configreg, 0,
    218 	    SIO16_CONFIGREG_ENABLE_IO | SIO16_CONFIGREG_ENABLE_IRQ |
    219 	    (((sa->sa_pri) & 0x0f) >> 2));
    220 	delay(10000);
    221 
    222 	printf("\n");
    223 
    224 	/* finally, configure the clcd's underneath */
    225 	for (i = 0; i < sc->sc_ncd180; i++) {
    226 		struct sio16_attach_args cd;
    227 
    228 		cd.cd_tag = sa->sa_bustag;
    229 		cd.cd_osc = sc->sc_clk * 100;
    230 		cd.cd_handle = (bus_space_handle_t)sc->sc_reg[i];
    231 		cd.cd_ackfunc = sio16_ackfunc;
    232 		cd.cd_ackfunc_arg = sc;
    233 		(void)config_found(self, (void *)&cd, NULL, CFARGS_NONE);
    234 	}
    235 }
    236 
    237 /*
    238  * note that the addresses used in this function match those assigned
    239  * in clcd_attach() below, or the various service match routines.
    240  */
    241 u_char
    242 sio16_ackfunc(void *v, int who)
    243 {
    244 	struct sio16_softc *sc = v;
    245 	bus_size_t addr;
    246 
    247 	switch (who) {
    248 	case CD18xx_INTRACK_RxINT:
    249 	case CD18xx_INTRACK_REINT:
    250 		addr = SIO16_RINT_ACK;
    251 		break;
    252 	case CD18xx_INTRACK_TxINT:
    253 		addr = SIO16_TINT_ACK;
    254 		break;
    255 	case CD18xx_INTRACK_MxINT:
    256 		addr = SIO16_MINT_ACK;
    257 		break;
    258 	default:
    259 		panic("%s: sio16_ackfunc: unknown ackfunc %d",
    260 		    device_xname(sc->sc_dev), who);
    261 	}
    262 	return (bus_space_read_1(sc->sc_tag, sc->sc_ack, addr));
    263 }
    264 
    265 /*
    266  * we attach two `clcd' instances per 1600se, that each call the
    267  * backend cd18xx driver for help.
    268  */
    269 static int	clcd_match(device_t, cfdata_t, void *);
    270 static void	clcd_attach(device_t, device_t, void *);
    271 
    272 CFATTACH_DECL_NEW(clcd, sizeof(struct cd18xx_softc),
    273     clcd_match, clcd_attach, NULL, NULL);
    274 
    275 static int
    276 clcd_match(device_t parent, cfdata_t cf, void *aux)
    277 {
    278 
    279 	/* XXX */
    280 	return 1;
    281 }
    282 
    283 static void
    284 clcd_attach(device_t parent, device_t self, void *aux)
    285 {
    286 	struct cd18xx_softc *sc = device_private(self);
    287 	struct sio16_attach_args *args = aux;
    288 
    289 	sc->sc_dev = self;
    290 	sc->sc_tag = args->cd_tag;
    291 	sc->sc_handle = args->cd_handle;
    292 	sc->sc_osc = args->cd_osc;
    293 	sc->sc_ackfunc = args->cd_ackfunc;
    294 	sc->sc_ackfunc_arg = args->cd_ackfunc_arg;
    295 	sc->sc_msmr = SIO16_MINT_ACK;
    296 	sc->sc_tsmr = SIO16_TINT_ACK;
    297 	sc->sc_rsmr = SIO16_RINT_ACK;
    298 
    299 	/* call the common code */
    300 	cd18xx_attach(sc);
    301 }
    302