1 /*        $NetBSD: jazzio.c,v 1.24 2021/08/07 16:18:42 thorpej Exp $  */
2 /*        $OpenBSD: picabus.c,v 1.11 1999/01/11 05:11:10 millert Exp $          */
3 /*        NetBSD: tc.c,v 1.2 1995/03/08 00:39:05 cgd Exp    */
4 
5 /*
6  * Copyright (c) 1994, 1995 Carnegie-Mellon University.
7  * All rights reserved.
8  *
9  * Author: Chris G. Demetriou
10  * Author: Per Fogelstrom. (Mips R4x00)
11  *
12  * Permission to use, copy, modify and distribute this software and
13  * its documentation is hereby granted, provided that both the copyright
14  * notice and this permission notice appear in all copies of the
15  * software, derivative works or modified versions, and any portions
16  * thereof, and that both notices appear in supporting documentation.
17  *
18  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
19  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
20  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
21  *
22  * Carnegie Mellon requests users of this software to return to
23  *
24  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
25  *  School of Computer Science
26  *  Carnegie Mellon University
27  *  Pittsburgh PA 15213-3890
28  *
29  * any improvements or extensions that they make and grant Carnegie the
30  * rights to redistribute these changes.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: jazzio.c,v 1.24 2021/08/07 16:18:42 thorpej Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 
40 #include <sys/bus.h>
41 #include <machine/pio.h>
42 #include <machine/autoconf.h>
43 #include <machine/platform.h>
44 
45 #include <arc/jazz/jazziovar.h>
46 #include <arc/jazz/pica.h>
47 #include <arc/jazz/jazzdmatlbreg.h>
48 #include <arc/jazz/jazzdmatlbvar.h>
49 #include <arc/jazz/dma.h>
50 #include <arc/jazz/pckbc_jazzioreg.h>
51 
52 #include "ioconf.h"
53 
54 void arc_sysreset(bus_addr_t, bus_size_t);
55 
56 struct jazzio_softc {
57           device_t sc_dev;
58           struct    arc_bus_dma_tag sc_dmat;
59           struct    pica_dev *sc_devs;
60 };
61 
62 /* Definition of the driver for autoconfig. */
63 static int          jazziomatch(device_t, cfdata_t, void *);
64 static void         jazzioattach(device_t, device_t, void *);
65 static int          jazzioprint(void *, const char *);
66 
67 CFATTACH_DECL_NEW(jazzio, sizeof(struct jazzio_softc),
68     jazziomatch, jazzioattach, NULL, NULL);
69 
70 static uint32_t jazzio_intr(uint32_t, struct clockframe *);
71 static int          jazzio_no_handler(void *);
72 
73 /*
74  *  Interrupt dispatch table for jazz i/o bus.
75  */
76 struct jazzio_intrhand {
77           intr_handler_t      ih_func;  /* Interrupt handler */
78           void                *ih_arg;  /* Parameter to send to handler */
79           struct evcnt        ih_evcnt; /* interrupt counter */
80           char                ih_evname[32];      /* event counter name */
81 };
82 
83 static struct jazzio_intrhand jazzio_intrtab[16];
84 
85 
86 struct jazzio_config *jazzio_conf = NULL;
87 struct pica_dev *jazzio_devconfig = NULL;
88 int jazzio_int_mask = 0;      /* jazz i/o interrupt enable mask */
89 struct arc_bus_space jazzio_bus;
90 
91 static int jazzio_found = 0;
92 
93 static int
jazziomatch(device_t parent,cfdata_t cf,void * aux)94 jazziomatch(device_t parent, cfdata_t cf, void *aux)
95 {
96           struct confargs *ca = aux;
97 
98         /* Make sure that we're looking for a jazzio bus. */
99         if (strcmp(ca->ca_name, jazzio_cd.cd_name) != 0)
100                 return 0;
101 
102           return 1;
103 }
104 
105 static void
jazzioattach(device_t parent,device_t self,void * aux)106 jazzioattach(device_t parent, device_t self, void *aux)
107 {
108           struct jazzio_softc *sc = device_private(self);
109           struct jazzio_attach_args ja;
110           int i;
111 
112           sc->sc_dev = self;
113 
114           if (jazzio_conf == NULL)
115                     panic("jazzio_conf isn't initialized");
116           if (jazzio_devconfig == NULL)
117                     panic("jazzio_devconfig isn't initialized");
118 
119           jazzio_found = 1;
120 
121           aprint_normal("\n");
122 
123           /* keep our CPU device description handy */
124           sc->sc_devs = jazzio_devconfig;
125 
126           /* initialize interrupt handler table */
127           for (i = 0; i < __arraycount(jazzio_intrtab); i++) {
128                     jazzio_intrtab[i].ih_func = jazzio_no_handler;
129                     jazzio_intrtab[i].ih_arg = NULL;
130           }
131 
132           /* set up interrupt handlers */
133           (*platform->set_intr)(MIPS_INT_MASK_1, jazzio_intr, ARC_INTPRI_JAZZ);
134 
135           /* Initialize jazzio DMA mapping register area and pool */
136           jazz_dmatlb_init(&jazzio_bus, jazzio_conf->jc_dmatlbreg);
137 
138           /* Create bus_dma_tag */
139           jazz_bus_dma_tag_init(&sc->sc_dmat);
140 
141           /* Try to configure each jazzio attached device */
142           for (i = 0; sc->sc_devs[i].ps_ca.ca_name != NULL; i++) {
143 
144                     ja.ja_name = sc->sc_devs[i].ps_ca.ca_name;
145                     ja.ja_bust = &jazzio_bus;
146                     ja.ja_dmat = &sc->sc_dmat;
147                     ja.ja_addr = (bus_addr_t)sc->sc_devs[i].ps_base;
148                     ja.ja_intr = sc->sc_devs[i].ps_ca.ca_slot;
149                     ja.ja_dma = 0;
150 
151                     /* Tell the autoconfig machinery we've found the hardware. */
152                     config_found(self, &ja, jazzioprint, CFARGS_NONE);
153           }
154 }
155 
156 static int
jazzioprint(void * aux,const char * pnp)157 jazzioprint(void *aux, const char *pnp)
158 {
159           struct jazzio_attach_args *ja = aux;
160 
161           if (pnp)
162                     aprint_normal("%s at %s", ja->ja_name, pnp);
163           aprint_normal(" addr 0x%lx", ja->ja_addr);
164           if (ja->ja_intr != -1)
165                     aprint_normal(" intr %d", ja->ja_intr);
166           return UNCONF;
167 }
168 
169 void
jazzio_intr_establish(int intr,intr_handler_t handler,void * val)170 jazzio_intr_establish(int intr, intr_handler_t handler, void *val)
171 {
172           struct jazzio_intrhand *jirp;
173 
174           if (intr < 0 || intr >= __arraycount(jazzio_intrtab))
175                     panic("jazzio intr %d out of range", intr);
176           jirp = &jazzio_intrtab[intr];
177           if (jirp->ih_func != jazzio_no_handler)
178                     panic("jazzio intr %d already set to %p", intr, jirp->ih_func);
179 
180           jazzio_int_mask |= 1 << intr;
181           jirp->ih_func = handler;
182           jirp->ih_arg = val;
183           snprintf(jirp->ih_evname, sizeof(jirp->ih_evname), "intr %d", intr);
184           evcnt_attach_dynamic(&jirp->ih_evcnt, EVCNT_TYPE_INTR,
185               NULL, "jazzio", jirp->ih_evname);
186 
187           (*jazzio_conf->jc_set_iointr_mask)(jazzio_int_mask);
188 }
189 
190 void
jazzio_intr_disestablish(int intr)191 jazzio_intr_disestablish(int intr)
192 {
193           struct jazzio_intrhand *jirp;
194 
195           jazzio_int_mask &= ~(1 << intr);
196           jirp = &jazzio_intrtab[intr];
197           jirp->ih_func = jazzio_no_handler;
198           jirp->ih_arg = NULL;
199           evcnt_detach(&jirp->ih_evcnt);
200 
201           (*jazzio_conf->jc_set_iointr_mask)(jazzio_int_mask);
202 }
203 
204 static int
jazzio_no_handler(void * arg)205 jazzio_no_handler(void *arg)
206 {
207 
208           panic("uncaught jazzio interrupt with arg %p", arg);
209 }
210 
211 /*
212  *   Handle jazz i/o interrupt.
213  */
214 uint32_t
jazzio_intr(uint32_t mask,struct clockframe * cf)215 jazzio_intr(uint32_t mask, struct clockframe *cf)
216 {
217           unsigned int vector;
218           struct jazzio_intrhand *jirp;
219 
220           while ((vector = inb(jazzio_conf->jc_iointr_status_reg)) != 0) {
221                     jirp = &jazzio_intrtab[(vector >> 2) - 1];
222                     (*jirp->ih_func)(jirp->ih_arg);
223                     jirp->ih_evcnt.ev_count++;
224           }
225           return MIPS_INT_MASK_1;
226 }
227 
228 void
jazzio_reset(void)229 jazzio_reset(void)
230 {
231 
232           arc_sysreset(PICA_SYS_KBD, JAZZIO_KBCMDP);
233 }
234