xref: /netbsd-src/sys/arch/arc/jazz/jazzio.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /*	$NetBSD: jazzio.c,v 1.24 2021/08/07 16:18:42 thorpej Exp $	*/
2459f2585Sur /*	$OpenBSD: picabus.c,v 1.11 1999/01/11 05:11:10 millert Exp $	*/
3459f2585Sur /*	NetBSD: tc.c,v 1.2 1995/03/08 00:39:05 cgd Exp 	*/
4459f2585Sur 
5459f2585Sur /*
6459f2585Sur  * Copyright (c) 1994, 1995 Carnegie-Mellon University.
7459f2585Sur  * All rights reserved.
8459f2585Sur  *
9459f2585Sur  * Author: Chris G. Demetriou
10459f2585Sur  * Author: Per Fogelstrom. (Mips R4x00)
11459f2585Sur  *
12459f2585Sur  * Permission to use, copy, modify and distribute this software and
13459f2585Sur  * its documentation is hereby granted, provided that both the copyright
14459f2585Sur  * notice and this permission notice appear in all copies of the
15459f2585Sur  * software, derivative works or modified versions, and any portions
16459f2585Sur  * thereof, and that both notices appear in supporting documentation.
17459f2585Sur  *
18459f2585Sur  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
19459f2585Sur  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
20459f2585Sur  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
21459f2585Sur  *
22459f2585Sur  * Carnegie Mellon requests users of this software to return to
23459f2585Sur  *
24459f2585Sur  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
25459f2585Sur  *  School of Computer Science
26459f2585Sur  *  Carnegie Mellon University
27459f2585Sur  *  Pittsburgh PA 15213-3890
28459f2585Sur  *
29459f2585Sur  * any improvements or extensions that they make and grant Carnegie the
30459f2585Sur  * rights to redistribute these changes.
31459f2585Sur  */
32459f2585Sur 
33a4183603Slukem #include <sys/cdefs.h>
34*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: jazzio.c,v 1.24 2021/08/07 16:18:42 thorpej Exp $");
35a4183603Slukem 
36459f2585Sur #include <sys/param.h>
37459f2585Sur #include <sys/systm.h>
38459f2585Sur #include <sys/device.h>
39459f2585Sur 
40cf10107dSdyoung #include <sys/bus.h>
41459f2585Sur #include <machine/pio.h>
42459f2585Sur #include <machine/autoconf.h>
435738ad78Ssoda #include <machine/platform.h>
44459f2585Sur 
45459f2585Sur #include <arc/jazz/jazziovar.h>
46459f2585Sur #include <arc/jazz/pica.h>
47459f2585Sur #include <arc/jazz/jazzdmatlbreg.h>
48f80f1b22Stsutsui #include <arc/jazz/jazzdmatlbvar.h>
49459f2585Sur #include <arc/jazz/dma.h>
505738ad78Ssoda #include <arc/jazz/pckbc_jazzioreg.h>
515738ad78Ssoda 
52c234c43aStsutsui #include "ioconf.h"
53c234c43aStsutsui 
547fe2a5a0Stsutsui void arc_sysreset(bus_addr_t, bus_size_t);
55459f2585Sur 
56459f2585Sur struct jazzio_softc {
570f31d9deStsutsui 	device_t sc_dev;
58459f2585Sur 	struct	arc_bus_dma_tag sc_dmat;
59459f2585Sur 	struct	pica_dev *sc_devs;
60459f2585Sur };
61459f2585Sur 
62459f2585Sur /* Definition of the driver for autoconfig. */
630f31d9deStsutsui static int	jazziomatch(device_t, cfdata_t, void *);
640f31d9deStsutsui static void	jazzioattach(device_t, device_t, void *);
650f31d9deStsutsui static int	jazzioprint(void *, const char *);
66459f2585Sur 
670f31d9deStsutsui CFATTACH_DECL_NEW(jazzio, sizeof(struct jazzio_softc),
68c5e91d44Sthorpej     jazziomatch, jazzioattach, NULL, NULL);
69459f2585Sur 
700f31d9deStsutsui static uint32_t jazzio_intr(uint32_t, struct clockframe *);
710f31d9deStsutsui static int	jazzio_no_handler(void *);
72459f2585Sur 
73459f2585Sur /*
745738ad78Ssoda  *  Interrupt dispatch table for jazz i/o bus.
75459f2585Sur  */
76cdb8e31dStsutsui struct jazzio_intrhand {
77cdb8e31dStsutsui 	intr_handler_t	ih_func;	/* Interrupt handler */
78cdb8e31dStsutsui 	void		*ih_arg;	/* Parameter to send to handler */
79cdb8e31dStsutsui 	struct evcnt	ih_evcnt;	/* interrupt counter */
80cdb8e31dStsutsui 	char		ih_evname[32];	/* event counter name */
81459f2585Sur };
82459f2585Sur 
830f31d9deStsutsui static struct jazzio_intrhand jazzio_intrtab[16];
84459f2585Sur 
85459f2585Sur 
865738ad78Ssoda struct jazzio_config *jazzio_conf = NULL;
875738ad78Ssoda struct pica_dev *jazzio_devconfig = NULL;
885738ad78Ssoda int jazzio_int_mask = 0;	/* jazz i/o interrupt enable mask */
895738ad78Ssoda struct arc_bus_space jazzio_bus;
90459f2585Sur 
910f31d9deStsutsui static int jazzio_found = 0;
920f31d9deStsutsui 
930f31d9deStsutsui static int
jazziomatch(device_t parent,cfdata_t cf,void * aux)940f31d9deStsutsui jazziomatch(device_t parent, cfdata_t cf, void *aux)
95459f2585Sur {
96459f2585Sur 	struct confargs *ca = aux;
97459f2585Sur 
985738ad78Ssoda         /* Make sure that we're looking for a jazzio bus. */
99459f2585Sur         if (strcmp(ca->ca_name, jazzio_cd.cd_name) != 0)
1007fe2a5a0Stsutsui                 return 0;
101459f2585Sur 
1027fe2a5a0Stsutsui 	return 1;
103459f2585Sur }
104459f2585Sur 
1050f31d9deStsutsui static void
jazzioattach(device_t parent,device_t self,void * aux)1060f31d9deStsutsui jazzioattach(device_t parent, device_t self, void *aux)
107459f2585Sur {
1080f31d9deStsutsui 	struct jazzio_softc *sc = device_private(self);
109459f2585Sur 	struct jazzio_attach_args ja;
110459f2585Sur 	int i;
111459f2585Sur 
1120f31d9deStsutsui 	sc->sc_dev = self;
1130f31d9deStsutsui 
1145738ad78Ssoda 	if (jazzio_conf == NULL)
1155738ad78Ssoda 		panic("jazzio_conf isn't initialized");
1165738ad78Ssoda 	if (jazzio_devconfig == NULL)
1175738ad78Ssoda 		panic("jazzio_devconfig isn't initialized");
118459f2585Sur 
119459f2585Sur 	jazzio_found = 1;
120459f2585Sur 
1210f31d9deStsutsui 	aprint_normal("\n");
1225738ad78Ssoda 
123459f2585Sur 	/* keep our CPU device description handy */
1245738ad78Ssoda 	sc->sc_devs = jazzio_devconfig;
125459f2585Sur 
126cdb8e31dStsutsui 	/* initialize interrupt handler table */
1270f31d9deStsutsui 	for (i = 0; i < __arraycount(jazzio_intrtab); i++) {
128cdb8e31dStsutsui 		jazzio_intrtab[i].ih_func = jazzio_no_handler;
129cdb8e31dStsutsui 		jazzio_intrtab[i].ih_arg = NULL;
130cdb8e31dStsutsui 	}
131cdb8e31dStsutsui 
132459f2585Sur 	/* set up interrupt handlers */
1332a8a21a0Stsutsui 	(*platform->set_intr)(MIPS_INT_MASK_1, jazzio_intr, ARC_INTPRI_JAZZ);
134459f2585Sur 
1351ffa7b76Swiz 	/* Initialize jazzio DMA mapping register area and pool */
136f80f1b22Stsutsui 	jazz_dmatlb_init(&jazzio_bus, jazzio_conf->jc_dmatlbreg);
137459f2585Sur 
138459f2585Sur 	/* Create bus_dma_tag */
139459f2585Sur 	jazz_bus_dma_tag_init(&sc->sc_dmat);
140459f2585Sur 
1415738ad78Ssoda 	/* Try to configure each jazzio attached device */
1425738ad78Ssoda 	for (i = 0; sc->sc_devs[i].ps_ca.ca_name != NULL; i++) {
143459f2585Sur 
144459f2585Sur 		ja.ja_name = sc->sc_devs[i].ps_ca.ca_name;
1455738ad78Ssoda 		ja.ja_bust = &jazzio_bus;
146459f2585Sur 		ja.ja_dmat = &sc->sc_dmat;
147459f2585Sur 		ja.ja_addr = (bus_addr_t)sc->sc_devs[i].ps_base;
148459f2585Sur 		ja.ja_intr = sc->sc_devs[i].ps_ca.ca_slot;
149459f2585Sur 		ja.ja_dma = 0;
150459f2585Sur 
151459f2585Sur 		/* Tell the autoconfig machinery we've found the hardware. */
152*c7fb772bSthorpej 		config_found(self, &ja, jazzioprint, CFARGS_NONE);
153459f2585Sur 	}
154459f2585Sur }
155459f2585Sur 
1560f31d9deStsutsui static int
jazzioprint(void * aux,const char * pnp)1577fe2a5a0Stsutsui jazzioprint(void *aux, const char *pnp)
158459f2585Sur {
159459f2585Sur 	struct jazzio_attach_args *ja = aux;
160459f2585Sur 
161459f2585Sur 	if (pnp)
162aec1389bSthorpej 		aprint_normal("%s at %s", ja->ja_name, pnp);
163aec1389bSthorpej 	aprint_normal(" addr 0x%lx", ja->ja_addr);
1645738ad78Ssoda 	if (ja->ja_intr != -1)
165aec1389bSthorpej 		aprint_normal(" intr %d", ja->ja_intr);
1667fe2a5a0Stsutsui 	return UNCONF;
167459f2585Sur }
168459f2585Sur 
169459f2585Sur void
jazzio_intr_establish(int intr,intr_handler_t handler,void * val)1707fe2a5a0Stsutsui jazzio_intr_establish(int intr, intr_handler_t handler, void *val)
171459f2585Sur {
172cdb8e31dStsutsui 	struct jazzio_intrhand *jirp;
173cdb8e31dStsutsui 
1740f31d9deStsutsui 	if (intr < 0 || intr >= __arraycount(jazzio_intrtab))
1755738ad78Ssoda 		panic("jazzio intr %d out of range", intr);
176cdb8e31dStsutsui 	jirp = &jazzio_intrtab[intr];
177cdb8e31dStsutsui 	if (jirp->ih_func != jazzio_no_handler)
178cdb8e31dStsutsui 		panic("jazzio intr %d already set to %p", intr, jirp->ih_func);
179cdb8e31dStsutsui 
1805738ad78Ssoda 	jazzio_int_mask |= 1 << intr;
181cdb8e31dStsutsui 	jirp->ih_func = handler;
182cdb8e31dStsutsui 	jirp->ih_arg = val;
183cdb8e31dStsutsui 	snprintf(jirp->ih_evname, sizeof(jirp->ih_evname), "intr %d", intr);
184cdb8e31dStsutsui 	evcnt_attach_dynamic(&jirp->ih_evcnt, EVCNT_TYPE_INTR,
185cdb8e31dStsutsui 	    NULL, "jazzio", jirp->ih_evname);
186459f2585Sur 
1875738ad78Ssoda 	(*jazzio_conf->jc_set_iointr_mask)(jazzio_int_mask);
188459f2585Sur }
189459f2585Sur 
190459f2585Sur void
jazzio_intr_disestablish(int intr)1917fe2a5a0Stsutsui jazzio_intr_disestablish(int intr)
192459f2585Sur {
193cdb8e31dStsutsui 	struct jazzio_intrhand *jirp;
194cdb8e31dStsutsui 
1955738ad78Ssoda 	jazzio_int_mask &= ~(1 << intr);
196cdb8e31dStsutsui 	jirp = &jazzio_intrtab[intr];
197cdb8e31dStsutsui 	jirp->ih_func = jazzio_no_handler;
198cdb8e31dStsutsui 	jirp->ih_arg = NULL;
199cdb8e31dStsutsui 	evcnt_detach(&jirp->ih_evcnt);
2005738ad78Ssoda 
2015738ad78Ssoda 	(*jazzio_conf->jc_set_iointr_mask)(jazzio_int_mask);
202459f2585Sur }
203459f2585Sur 
2040f31d9deStsutsui static int
jazzio_no_handler(void * arg)2057fe2a5a0Stsutsui jazzio_no_handler(void *arg)
206459f2585Sur {
207cdb8e31dStsutsui 
2085738ad78Ssoda 	panic("uncaught jazzio interrupt with arg %p", arg);
209459f2585Sur }
210459f2585Sur 
211459f2585Sur /*
2125738ad78Ssoda  *   Handle jazz i/o interrupt.
213459f2585Sur  */
2147310abdeStsutsui uint32_t
jazzio_intr(uint32_t mask,struct clockframe * cf)2157310abdeStsutsui jazzio_intr(uint32_t mask, struct clockframe *cf)
216459f2585Sur {
2175738ad78Ssoda 	unsigned int vector;
218cdb8e31dStsutsui 	struct jazzio_intrhand *jirp;
219459f2585Sur 
2205738ad78Ssoda 	while ((vector = inb(jazzio_conf->jc_iointr_status_reg)) != 0) {
2215738ad78Ssoda 		jirp = &jazzio_intrtab[(vector >> 2) - 1];
222cdb8e31dStsutsui 		(*jirp->ih_func)(jirp->ih_arg);
223cdb8e31dStsutsui 		jirp->ih_evcnt.ev_count++;
224459f2585Sur 	}
2254f59bf75Stsutsui 	return MIPS_INT_MASK_1;
226459f2585Sur }
227459f2585Sur 
2285738ad78Ssoda void
jazzio_reset(void)2297fe2a5a0Stsutsui jazzio_reset(void)
230459f2585Sur {
231cdb8e31dStsutsui 
2325738ad78Ssoda 	arc_sysreset(PICA_SYS_KBD, JAZZIO_KBCMDP);
233459f2585Sur }
234