sio16.c revision 1.25
11.25Smsaitoh/*	$NetBSD: sio16.c,v 1.25 2019/11/12 13:17:44 msaitoh Exp $	*/
21.1Smrg
31.1Smrg/*
41.1Smrg * Copyright (c) 1998, 2001 Matthew R. Green
51.1Smrg * All rights reserved.
61.1Smrg *
71.1Smrg * Redistribution and use in source and binary forms, with or without
81.1Smrg * modification, are permitted provided that the following conditions
91.1Smrg * are met:
101.1Smrg * 1. Redistributions of source code must retain the above copyright
111.1Smrg *    notice, this list of conditions and the following disclaimer.
121.1Smrg * 2. Redistributions in binary form must reproduce the above copyright
131.1Smrg *    notice, this list of conditions and the following disclaimer in the
141.1Smrg *    documentation and/or other materials provided with the distribution.
151.1Smrg *
161.1Smrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Smrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Smrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Smrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Smrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211.1Smrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221.1Smrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231.1Smrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241.1Smrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Smrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Smrg * SUCH DAMAGE.
271.1Smrg */
281.1Smrg
291.1Smrg/*
301.1Smrg * aurora technologies nova16 driver.  this board is an sbus card with
311.1Smrg * an external 16 port serial box.  there are two cirrus logic cd180
321.1Smrg * 8 port serial chips used in the implementation.
331.1Smrg *
341.1Smrg * thanks go to Oliver Aldulea <oli@morcov.bv.ro> for writing the
351.1Smrg * linux driver of this that helped clear up a couple of issues.
361.1Smrg */
371.2Slukem
381.2Slukem#include <sys/cdefs.h>
391.25Smsaitoh__KERNEL_RCSID(0, "$NetBSD: sio16.c,v 1.25 2019/11/12 13:17:44 msaitoh Exp $");
401.1Smrg
411.1Smrg#include <sys/param.h>
421.1Smrg#include <sys/conf.h>
431.1Smrg#include <sys/device.h>
441.1Smrg#include <sys/systm.h>
451.1Smrg
461.1Smrg#include <machine/autoconf.h>
471.1Smrg
481.1Smrg#include <dev/ic/cd18xxvar.h>
491.1Smrg#include <dev/ic/cd18xxreg.h>
501.1Smrg
511.1Smrg#include "ioconf.h"
521.1Smrg#include "locators.h"
531.1Smrg
541.1Smrg/* 1600se configuration register bits */
551.1Smrg#define SIO16_CONFIGREG_ENABLE_IO    8
561.1Smrg#define SIO16_CONFIGREG_ENABLE_IRQ   4
571.1Smrg
581.1Smrg/* 1600se interrupt ackknowledge register bytes */
591.1Smrg#define	SIO16_MINT_ACK		1	/* modem interrupt acknowledge */
601.1Smrg#define	SIO16_TINT_ACK		2	/* tx interrupt acknowledge */
611.1Smrg#define	SIO16_RINT_ACK		3	/* rx interrupt acknowledge */
621.1Smrg
631.1Smrg/*
641.1Smrg * device cfattach and cfdriver definitions, plus the routine we pass
651.1Smrg * to the cd18xx code or interrupt acknowledgement.
661.1Smrg */
671.21Sceggerstatic int	sio16_match(device_t, cfdata_t, void *);
681.21Sceggerstatic void	sio16_attach(device_t, device_t, void *);
691.1Smrgstatic u_char	sio16_ackfunc(void *, int who);
701.1Smrg
711.1Smrg/*
721.1Smrg * define the sio16 per-device softc.
731.1Smrg */
741.1Smrgstruct sio16_softc {
751.24Smrg	device_t sc_dev;
761.1Smrg
771.1Smrg	/* sbus information */
781.1Smrg	bus_space_tag_t	sc_tag;			/* bus tag for below */
791.1Smrg	bus_space_handle_t sc_configreg;	/* configuration register */
801.1Smrg	bus_space_handle_t sc_reg[2];		/* cd180 register sets */
811.1Smrg	bus_space_handle_t sc_ack;		/* interrupt acknowledgement */
821.1Smrg#define	SIO16_1600SE	0x00000001
831.1Smrg
841.1Smrg	u_int           sc_clk;
851.1Smrg
861.1Smrg	/* cd180 information */
871.1Smrg	int		sc_ncd180;
881.1Smrg
891.1Smrg};
901.1Smrg
911.24SmrgCFATTACH_DECL_NEW(siosixteen, sizeof(struct sio16_softc),
921.10Sthorpej    sio16_match, sio16_attach, NULL, NULL);
931.1Smrg
941.1Smrgstruct sio16_attach_args {
951.1Smrg	bus_space_tag_t		cd_tag;
961.1Smrg	bus_space_handle_t	cd_handle;
971.1Smrg	u_char			(*cd_ackfunc)(void *, int);
981.1Smrg	void			*cd_ackfunc_arg;
991.1Smrg	u_int			cd_osc;
1001.1Smrg};
1011.1Smrg
1021.1Smrg/*
1031.1Smrg * device match routine:  is there an sio16 present?
1041.1Smrg *
1051.1Smrg * note that we can not put "sio16" in the cfdriver, as this is an
1061.1Smrg * illegal name, so we have to hard code it here.
1071.1Smrg */
1081.1Smrg#define	SIO16_ROM_NAME	"sio16"
1091.14Sperryint
1101.21Sceggersio16_match(device_t parent, cfdata_t cf, void *aux)
1111.1Smrg{
1121.1Smrg	struct sbus_attach_args *sa = aux;
1131.1Smrg
1141.1Smrg	/* does the prom think i'm an sio16? */
1151.1Smrg	if (strcmp(SIO16_ROM_NAME, sa->sa_name) != 0)
1161.1Smrg		return (0);
1171.1Smrg
1181.1Smrg	return (1);
1191.1Smrg}
1201.1Smrg
1211.1Smrg/*
1221.1Smrg * device attach routine:  go attach all sub devices.
1231.1Smrg */
1241.14Sperryvoid
1251.21Sceggersio16_attach(device_t parent, device_t self, void *aux)
1261.1Smrg{
1271.1Smrg	struct sbus_attach_args *sa = aux;
1281.23Stsutsui	struct sio16_softc *sc = device_private(self);
1291.1Smrg	bus_space_handle_t h;
1301.1Smrg	char *mode, *model;
1311.1Smrg	int i;
1321.1Smrg
1331.24Smrg	sc->sc_dev = self;
1341.24Smrg
1351.1Smrg	if (sa->sa_nreg != 4)
1361.7Sprovos		panic("sio16_attach: got %d registers intead of 4",
1371.1Smrg		    sa->sa_nreg);
1381.1Smrg
1391.1Smrg	/* copy our bus tag, we will need it */
1401.1Smrg	sc->sc_tag = sa->sa_bustag;
1411.1Smrg
1421.1Smrg	/*
1431.1Smrg	 * sio16 has 4 register mappings.  a single byte configuration
1441.1Smrg	 * register, 2 128 byte regions for the cd180 registers, and
1451.1Smrg	 * a 4 byte region for interrupt acknowledgement.
1461.1Smrg	 */
1471.1Smrg	if (sbus_bus_map(sa->sa_bustag,
1481.5Smartin			 sa->sa_reg[0].oa_space,
1491.5Smartin			 sa->sa_reg[0].oa_base,
1501.5Smartin			 sa->sa_reg[0].oa_size,
1511.4Seeh			 0, &h) != 0) {
1521.1Smrg		printf("%s at sbus: can not map registers 0\n",
1531.16Scegger		    device_xname(self));
1541.1Smrg		return;
1551.1Smrg	}
1561.1Smrg	sc->sc_configreg = h;
1571.1Smrg	if (sbus_bus_map(sa->sa_bustag,
1581.1Smrg			 sa->sa_reg[1].sbr_slot,
1591.1Smrg			 sa->sa_reg[1].sbr_offset,
1601.1Smrg			 sa->sa_reg[1].sbr_size,
1611.4Seeh			 0, &h) != 0) {
1621.1Smrg		printf("%s at sbus: can not map registers 1\n",
1631.16Scegger		    device_xname(self));
1641.1Smrg		return;
1651.1Smrg	}
1661.1Smrg	sc->sc_reg[0] = h;
1671.1Smrg	if (sbus_bus_map(sa->sa_bustag,
1681.1Smrg			 sa->sa_reg[2].sbr_slot,
1691.1Smrg			 sa->sa_reg[2].sbr_offset,
1701.1Smrg			 sa->sa_reg[2].sbr_size,
1711.4Seeh			 0, &h) != 0) {
1721.1Smrg		printf("%s at sbus: can not map registers 2\n",
1731.16Scegger		    device_xname(self));
1741.1Smrg		return;
1751.1Smrg	}
1761.1Smrg	sc->sc_reg[1] = h;
1771.1Smrg	if (sbus_bus_map(sa->sa_bustag,
1781.1Smrg			 sa->sa_reg[3].sbr_slot,
1791.1Smrg			 sa->sa_reg[3].sbr_offset,
1801.1Smrg			 sa->sa_reg[3].sbr_size,
1811.4Seeh			 0, &h) != 0) {
1821.1Smrg		printf("%s at sbus: can not map registers 3\n",
1831.16Scegger		    device_xname(self));
1841.1Smrg		return;
1851.1Smrg	}
1861.1Smrg	sc->sc_ack = h;
1871.1Smrg
1881.12Spk	mode = prom_getpropstring(sa->sa_node, "mode");
1891.1Smrg	if (mode)
1901.1Smrg		printf(", %s mode", mode);
1911.1Smrg
1921.1Smrg	/* get the clock frequency */
1931.12Spk	sc->sc_clk = prom_getpropint(sa->sa_node, "clk", 24000);
1941.1Smrg
1951.12Spk	model = prom_getpropstring(sa->sa_node, "model");
1961.1Smrg	if (model == 0) {
1971.1Smrg		printf(", no model property, bailing\n");
1981.1Smrg		return;
1991.1Smrg	}
2001.14Sperry
2011.1Smrg	/* are we an 1600se? */
2021.1Smrg	if (strcmp(model, "1600se") == 0) {
2031.1Smrg		printf(", 16 channels");
2041.1Smrg		sc->sc_ncd180 = 2;
2051.1Smrg	} else {
2061.1Smrg		printf(", don't know model %s, bailing\n", model);
2071.1Smrg		return;
2081.1Smrg	}
2091.1Smrg
2101.1Smrg	/* establish interrupt channel */
2111.11Spk	(void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_TTY,
2121.1Smrg	    cd18xx_hardintr, sc);
2131.1Smrg
2141.1Smrg	/* reset the board, and turn on interrupts and I/O */
2151.1Smrg	bus_space_write_1(sa->sa_bustag, sc->sc_configreg, 0, 0);
2161.1Smrg	delay(100);
2171.1Smrg	bus_space_write_1(sa->sa_bustag, sc->sc_configreg, 0,
2181.1Smrg	    SIO16_CONFIGREG_ENABLE_IO | SIO16_CONFIGREG_ENABLE_IRQ |
2191.1Smrg	    (((sa->sa_pri) & 0x0f) >> 2));
2201.1Smrg	delay(10000);
2211.1Smrg
2221.1Smrg	printf("\n");
2231.1Smrg
2241.1Smrg	/* finally, configure the clcd's underneath */
2251.1Smrg	for (i = 0; i < sc->sc_ncd180; i++) {
2261.1Smrg		struct sio16_attach_args cd;
2271.1Smrg
2281.1Smrg		cd.cd_tag = sa->sa_bustag;
2291.1Smrg		cd.cd_osc = sc->sc_clk * 100;
2301.1Smrg		cd.cd_handle = (bus_space_handle_t)sc->sc_reg[i];
2311.1Smrg		cd.cd_ackfunc = sio16_ackfunc;
2321.1Smrg		cd.cd_ackfunc_arg = sc;
2331.1Smrg		(void)config_found(self, (void *)&cd, NULL);
2341.1Smrg	}
2351.1Smrg}
2361.1Smrg
2371.1Smrg/*
2381.1Smrg * note that the addresses used in this function match those assigned
2391.1Smrg * in clcd_attach() below, or the various service match routines.
2401.1Smrg */
2411.1Smrgu_char
2421.18Sdslsio16_ackfunc(void *v, int who)
2431.1Smrg{
2441.1Smrg	struct sio16_softc *sc = v;
2451.1Smrg	bus_size_t addr;
2461.1Smrg
2471.1Smrg	switch (who) {
2481.1Smrg	case CD18xx_INTRACK_RxINT:
2491.1Smrg	case CD18xx_INTRACK_REINT:
2501.1Smrg		addr = SIO16_RINT_ACK;
2511.1Smrg		break;
2521.1Smrg	case CD18xx_INTRACK_TxINT:
2531.1Smrg		addr = SIO16_TINT_ACK;
2541.1Smrg		break;
2551.1Smrg	case CD18xx_INTRACK_MxINT:
2561.1Smrg		addr = SIO16_MINT_ACK;
2571.1Smrg		break;
2581.1Smrg	default:
2591.1Smrg		panic("%s: sio16_ackfunc: unknown ackfunc %d",
2601.24Smrg		    device_xname(sc->sc_dev), who);
2611.1Smrg	}
2621.1Smrg	return (bus_space_read_1(sc->sc_tag, sc->sc_ack, addr));
2631.1Smrg}
2641.1Smrg
2651.1Smrg/*
2661.1Smrg * we attach two `clcd' instances per 1600se, that each call the
2671.1Smrg * backend cd18xx driver for help.
2681.1Smrg */
2691.21Sceggerstatic int	clcd_match(device_t, cfdata_t, void *);
2701.21Sceggerstatic void	clcd_attach(device_t, device_t, void *);
2711.1Smrg
2721.24SmrgCFATTACH_DECL_NEW(clcd, sizeof(struct cd18xx_softc),
2731.10Sthorpej    clcd_match, clcd_attach, NULL, NULL);
2741.1Smrg
2751.1Smrgstatic int
2761.21Sceggerclcd_match(device_t parent, cfdata_t cf, void *aux)
2771.1Smrg{
2781.1Smrg
2791.1Smrg	/* XXX */
2801.1Smrg	return 1;
2811.1Smrg}
2821.1Smrg
2831.14Sperrystatic void
2841.21Sceggerclcd_attach(device_t parent, device_t self, void *aux)
2851.1Smrg{
2861.23Stsutsui	struct cd18xx_softc *sc = device_private(self);
2871.1Smrg	struct sio16_attach_args *args = aux;
2881.1Smrg
2891.25Smsaitoh	sc->sc_dev = self;
2901.1Smrg	sc->sc_tag = args->cd_tag;
2911.1Smrg	sc->sc_handle = args->cd_handle;
2921.1Smrg	sc->sc_osc = args->cd_osc;
2931.1Smrg	sc->sc_ackfunc = args->cd_ackfunc;
2941.1Smrg	sc->sc_ackfunc_arg = args->cd_ackfunc_arg;
2951.1Smrg	sc->sc_msmr = SIO16_MINT_ACK;
2961.1Smrg	sc->sc_tsmr = SIO16_TINT_ACK;
2971.1Smrg	sc->sc_rsmr = SIO16_RINT_ACK;
2981.1Smrg
2991.1Smrg	/* call the common code */
3001.1Smrg	cd18xx_attach(sc);
3011.1Smrg}
302