xref: /netbsd-src/sys/arch/hpcmips/vr/vrecu.c (revision 7433666e375b3ac4cc764df5a6726be98bc1cdd5)
1*7433666eSthorpej /* $NetBSD: vrecu.c,v 1.11 2023/12/20 14:50:02 thorpej Exp $ */
2f8bc0014Sigy 
3f8bc0014Sigy /*
4f8bc0014Sigy  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5f8bc0014Sigy  * All rights reserved.
6f8bc0014Sigy  *
7f8bc0014Sigy  * This code is derived from software contributed to The NetBSD Foundation
8f8bc0014Sigy  * by Naoto Shimazaki of YOKOGAWA Electric Corporation.
9f8bc0014Sigy  *
10f8bc0014Sigy  * Redistribution and use in source and binary forms, with or without
11f8bc0014Sigy  * modification, are permitted provided that the following conditions
12f8bc0014Sigy  * are met:
13f8bc0014Sigy  * 1. Redistributions of source code must retain the above copyright
14f8bc0014Sigy  *    notice, this list of conditions and the following disclaimer.
15f8bc0014Sigy  * 2. Redistributions in binary form must reproduce the above copyright
16f8bc0014Sigy  *    notice, this list of conditions and the following disclaimer in the
17f8bc0014Sigy  *    documentation and/or other materials provided with the distribution.
18f8bc0014Sigy  *
19f8bc0014Sigy  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20f8bc0014Sigy  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21f8bc0014Sigy  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f8bc0014Sigy  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23f8bc0014Sigy  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24f8bc0014Sigy  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f8bc0014Sigy  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f8bc0014Sigy  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27f8bc0014Sigy  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f8bc0014Sigy  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f8bc0014Sigy  * POSSIBILITY OF SUCH DAMAGE.
30f8bc0014Sigy  */
31f8bc0014Sigy 
320c82163cSlukem #include <sys/cdefs.h>
33*7433666eSthorpej __KERNEL_RCSID(0, "$NetBSD: vrecu.c,v 1.11 2023/12/20 14:50:02 thorpej Exp $");
340c82163cSlukem 
35f8bc0014Sigy #include <sys/param.h>
36f8bc0014Sigy #include <sys/device.h>
37f8bc0014Sigy #include <sys/queue.h>
38f8bc0014Sigy #include <sys/systm.h>
39f8bc0014Sigy 
40f8bc0014Sigy #include <machine/bus.h>
41f8bc0014Sigy #include <machine/intr.h>
42f8bc0014Sigy 
43f8bc0014Sigy #include <hpcmips/vr/vrcpudef.h>
44f8bc0014Sigy #include <hpcmips/vr/vripif.h>
45f8bc0014Sigy #include <hpcmips/vr/vr4181ecureg.h>
46f8bc0014Sigy 
47f8bc0014Sigy #include <dev/isa/isareg.h>
48f8bc0014Sigy #include <dev/isa/isavar.h>
49f8bc0014Sigy #include <dev/pcmcia/pcmciareg.h>
50f8bc0014Sigy #include <dev/pcmcia/pcmciavar.h>
51f8bc0014Sigy #include <dev/pcmcia/pcmciachip.h>
52f8bc0014Sigy 
53f8bc0014Sigy #include <dev/ic/i82365reg.h>
54f8bc0014Sigy #include <dev/ic/i82365var.h>
55f8bc0014Sigy #include <dev/isa/i82365_isavar.h>
56f8bc0014Sigy 
57cbab9cadSchs static int pcic_vrip_match(device_t, cfdata_t, void *);
58cbab9cadSchs static void pcic_vrip_attach(device_t, device_t, void *);
59f8bc0014Sigy static void *pcic_vrip_chip_intr_establish(pcmcia_chipset_handle_t,
60f8bc0014Sigy 					   struct pcmcia_function *, int,
61f8bc0014Sigy 					   int (*)(void *), void *);
62f8bc0014Sigy static void pcic_vrip_chip_intr_disestablish(pcmcia_chipset_handle_t, void *);
63f8bc0014Sigy static int pcic_vrip_intr(void *);
64f8bc0014Sigy 
65f8bc0014Sigy struct pcic_vrip_softc {
66f8bc0014Sigy 	struct pcic_softc	sc_pcic;	/* real pcic softc */
675770fb4eStsutsui 	uint16_t		sc_intr_mask;
685770fb4eStsutsui 	uint16_t		sc_intr_valid;
69f8bc0014Sigy 	struct intrhand {
70f8bc0014Sigy 		int	(*ih_fun)(void *);
71f8bc0014Sigy 		void	*ih_arg;
72f8bc0014Sigy 	} 			sc_intrhand[ECU_MAX_INTR];
73f8bc0014Sigy };
74f8bc0014Sigy 
75cbab9cadSchs CFATTACH_DECL_NEW(pcic_vrip, sizeof(struct pcic_vrip_softc),
76f8bc0014Sigy 	      pcic_vrip_match, pcic_vrip_attach, NULL, NULL);
77f8bc0014Sigy 
78f8bc0014Sigy static struct pcmcia_chip_functions pcic_vrip_functions = {
79f8bc0014Sigy 	.mem_alloc		= pcic_chip_mem_alloc,
80f8bc0014Sigy 	.mem_free		= pcic_chip_mem_free,
81f8bc0014Sigy 	.mem_map		= pcic_chip_mem_map,
82f8bc0014Sigy 	.mem_unmap		= pcic_chip_mem_unmap,
83f8bc0014Sigy 
84f8bc0014Sigy 	.io_alloc		= pcic_chip_io_alloc,
85f8bc0014Sigy 	.io_free		= pcic_chip_io_free,
86f8bc0014Sigy 	.io_map			= pcic_chip_io_map,
87f8bc0014Sigy 	.io_unmap		= pcic_chip_io_unmap,
88f8bc0014Sigy 
89f8bc0014Sigy 	.intr_establish		= pcic_vrip_chip_intr_establish,
90f8bc0014Sigy 	.intr_disestablish	= pcic_vrip_chip_intr_disestablish,
91f8bc0014Sigy 
92f8bc0014Sigy 	.socket_enable		= pcic_chip_socket_enable,
93f8bc0014Sigy 	.socket_disable		= pcic_chip_socket_disable,
949de9d325Smycroft 	.socket_settype		= pcic_chip_socket_settype,
95f8bc0014Sigy };
96f8bc0014Sigy 
97f8bc0014Sigy 
98f8bc0014Sigy static int
pcic_vrip_match(device_t parent,cfdata_t match,void * aux)99cbab9cadSchs pcic_vrip_match(device_t parent, cfdata_t match, void *aux)
100f8bc0014Sigy {
101f8bc0014Sigy 	return 1;
102f8bc0014Sigy }
103f8bc0014Sigy 
104f8bc0014Sigy static void
pcic_vrip_attach(device_t parent,device_t self,void * aux)105cbab9cadSchs pcic_vrip_attach(device_t parent, device_t self, void *aux)
106f8bc0014Sigy {
10716c0f838Stsutsui 	struct pcic_vrip_softc	*vsc = device_private(self);
10816c0f838Stsutsui 	struct pcic_softc	*sc = &vsc->sc_pcic;
109f8bc0014Sigy 	struct vrip_attach_args	*va = aux;
110f8bc0014Sigy 	bus_space_handle_t	ioh;
111f8bc0014Sigy 	bus_space_handle_t	memh;
112f8bc0014Sigy 	int			i;
113f8bc0014Sigy 
114cbab9cadSchs 	sc->dev = self;
115f8bc0014Sigy 	vsc->sc_intr_valid = PCIC_INTR_IRQ_VALIDMASK;
116f8bc0014Sigy 	vsc->sc_intr_mask = 0xffff;
117f8bc0014Sigy 	for (i = 0; i < ECU_MAX_INTR; i++)
118f8bc0014Sigy 		vsc->sc_intrhand[i].ih_fun = NULL;
119f8bc0014Sigy 
120f8bc0014Sigy 	if ((sc->ih = vrip_intr_establish(va->va_vc, va->va_unit, 0,
12116c0f838Stsutsui 					  IPL_NET, pcic_vrip_intr, vsc))
122f8bc0014Sigy 	    == NULL) {
12316c0f838Stsutsui 		printf(": can't establish interrupt");
124f8bc0014Sigy 	}
125f8bc0014Sigy 
126f8bc0014Sigy         /* Map i/o space. */
127f8bc0014Sigy         if (bus_space_map(va->va_iot, va->va_addr, ECU_SIZE, 0, &ioh)) {
128f8bc0014Sigy                 printf(": can't map pcic register space\n");
129f8bc0014Sigy                 return;
130f8bc0014Sigy         }
131f8bc0014Sigy 
132f8bc0014Sigy 	/* init CFG_REG_1 */
133f8bc0014Sigy 	bus_space_write_2(va->va_iot, ioh, ECU_CFG_REG_1_W, 0x0001);
134f8bc0014Sigy 
135f8bc0014Sigy 	/* mask all interrupt */
136f8bc0014Sigy 	bus_space_write_2(va->va_iot, ioh, ECU_INTMSK_REG_W,
137f8bc0014Sigy 			  vsc->sc_intr_mask);
138f8bc0014Sigy 
139f8bc0014Sigy 	/* Map mem space. */
140f8bc0014Sigy #if 1
141f8bc0014Sigy 	if (bus_space_map(va->va_iot, VR_ISA_MEM_BASE, 0x4000, 0, &memh))
142f8bc0014Sigy 		panic("pcic_pci_attach: can't map mem space");
143f8bc0014Sigy 
144f8bc0014Sigy 	sc->membase = VR_ISA_MEM_BASE;
145f8bc0014Sigy 	sc->subregionmask = (1 << (0x4000 / PCIC_MEM_PAGESIZE)) - 1;
146f8bc0014Sigy 
147f8bc0014Sigy 	sc->iobase = VR_ISA_PORT_BASE + 0x400;
148f8bc0014Sigy 	sc->iosize = 0xbff;
149f8bc0014Sigy #else
150f8bc0014Sigy 	if (bus_space_map(va->va_iot, VR_ISA_MEM_BASE, 0x70000, 0, &memh))
151f8bc0014Sigy 		panic("pcic_pci_attach: can't map mem space");
152f8bc0014Sigy 
153f8bc0014Sigy 	sc->membase = VR_ISA_MEM_BASE;
154f8bc0014Sigy 	sc->subregionmask = (1 << (0x70000 / PCIC_MEM_PAGESIZE)) - 1;
155f8bc0014Sigy 
156f8bc0014Sigy 	sc->iobase = VR_ISA_PORT_BASE;
157f8bc0014Sigy 	sc->iosize = 0x10000;
158f8bc0014Sigy #endif
159f8bc0014Sigy 
160f8bc0014Sigy 	sc->pct = &pcic_vrip_functions;
161f8bc0014Sigy 
162f8bc0014Sigy 	sc->iot = va->va_iot;
163f8bc0014Sigy 	sc->ioh = ioh;
164f8bc0014Sigy 	sc->memt = va->va_iot;
165f8bc0014Sigy 	sc->memh = memh;
166f8bc0014Sigy 
167f8bc0014Sigy 	printf("\n");
168f8bc0014Sigy 
169231121a8Sdrochner 	sc->irq = -1;
170f8bc0014Sigy 
171f8bc0014Sigy 	pcic_attach(sc);
172f8bc0014Sigy 	pcic_attach_sockets(sc);
173f8bc0014Sigy         pcic_attach_sockets_finish(sc);
174f8bc0014Sigy }
175f8bc0014Sigy 
176f8bc0014Sigy static void *
pcic_vrip_chip_intr_establish(pcmcia_chipset_handle_t pch,struct pcmcia_function * pf,int ipl,int (* ih_fun)(void *),void * ih_arg)177f8bc0014Sigy pcic_vrip_chip_intr_establish(pcmcia_chipset_handle_t pch,
178f8bc0014Sigy 			      struct pcmcia_function *pf,
179f8bc0014Sigy 			      int ipl,
180f8bc0014Sigy 			      int (*ih_fun)(void *),
181f8bc0014Sigy 			      void *ih_arg)
182f8bc0014Sigy {
183f8bc0014Sigy 	struct pcic_handle	*h;
184f8bc0014Sigy 	struct pcic_softc	*sc;
185f8bc0014Sigy 	struct pcic_vrip_softc	*vsc;
186f8bc0014Sigy 	struct intrhand		*ih;
187f8bc0014Sigy 
188f8bc0014Sigy 	int	irq;
189f8bc0014Sigy 	int	r;
190f8bc0014Sigy 
191f8bc0014Sigy 
192f8bc0014Sigy 	/*
193f8bc0014Sigy 	 * XXXXXXXXXXXXXXXXXXXXXXXXXXXX
194f8bc0014Sigy 	 * XXXXXXXXXXXXXXXXXXXXXXXXXXXX
195f8bc0014Sigy 	 * XXXXXXXXXXXXXXXXXXXXXXXXXXXX
196f8bc0014Sigy 	 */
197f8bc0014Sigy 	irq = 11;
198f8bc0014Sigy 	/*
199f8bc0014Sigy 	 * XXXXXXXXXXXXXXXXXXXXXXXXXXXX
200f8bc0014Sigy 	 * XXXXXXXXXXXXXXXXXXXXXXXXXXXX
201f8bc0014Sigy 	 * XXXXXXXXXXXXXXXXXXXXXXXXXXXX
202f8bc0014Sigy 	 */
203f8bc0014Sigy 
204f8bc0014Sigy 
205f8bc0014Sigy 	h = (struct pcic_handle *) pch;
20616c0f838Stsutsui 	vsc = device_private(h->ph_parent);
20716c0f838Stsutsui 	sc = &vsc->sc_pcic;
208f8bc0014Sigy 
209f8bc0014Sigy 
210f8bc0014Sigy 	ih = &vsc->sc_intrhand[irq];
211f8bc0014Sigy 	if (ih->ih_fun) /* cannot share ecu interrupt */
212f8bc0014Sigy 		return NULL;
213f8bc0014Sigy 	ih->ih_fun = ih_fun;
214f8bc0014Sigy 	ih->ih_arg = ih_arg;
215f8bc0014Sigy 
216f8bc0014Sigy 	h->ih_irq = irq;
217f8bc0014Sigy 	if (h->flags & PCIC_FLAG_ENABLED) {
218f8bc0014Sigy 		r = pcic_read(h, PCIC_INTR);
219a93d4983Smycroft 		r &= ~PCIC_INTR_IRQ_MASK;
220a93d4983Smycroft 		pcic_write(h, PCIC_INTR, r | irq);
221f8bc0014Sigy         }
222f8bc0014Sigy 
223f8bc0014Sigy 	vsc->sc_intr_mask &= ~(1 << irq);
224f8bc0014Sigy 	bus_space_write_2(sc->iot, sc->ioh, ECU_INTMSK_REG_W,
225f8bc0014Sigy 			  vsc->sc_intr_mask);
226f8bc0014Sigy 
227f8bc0014Sigy 	return ih;
228f8bc0014Sigy }
229f8bc0014Sigy 
230f8bc0014Sigy static void
pcic_vrip_chip_intr_disestablish(pcmcia_chipset_handle_t pch,void * arg)231f8bc0014Sigy pcic_vrip_chip_intr_disestablish(pcmcia_chipset_handle_t pch, void *arg)
232f8bc0014Sigy {
233f8bc0014Sigy 	struct pcic_handle	*h;
234f8bc0014Sigy 	struct pcic_softc	*sc;
235f8bc0014Sigy 	struct pcic_vrip_softc	*vsc;
236f8bc0014Sigy 	struct intrhand		*ih = arg;
237f8bc0014Sigy 
238f8bc0014Sigy 	int	s;
239f8bc0014Sigy 	int	r;
240f8bc0014Sigy 
241f8bc0014Sigy 	h = (struct pcic_handle *) pch;
24216c0f838Stsutsui 	vsc = device_private(h->ph_parent);
24316c0f838Stsutsui 	sc = &vsc->sc_pcic;
244f8bc0014Sigy 
245f8bc0014Sigy 	if (ih != &vsc->sc_intrhand[h->ih_irq])
246f8bc0014Sigy 		panic("pcic_vrip_chip_intr_disestablish: bad handler");
247f8bc0014Sigy 
248f8bc0014Sigy 	s = splhigh();
249f8bc0014Sigy 
250f8bc0014Sigy 	vsc->sc_intr_mask |= 1 << h->ih_irq;
251f8bc0014Sigy 	bus_space_write_2(sc->iot, sc->ioh, ECU_INTMSK_REG_W,
252f8bc0014Sigy 			  vsc->sc_intr_mask);
253f8bc0014Sigy 
254f8bc0014Sigy 	h->ih_irq = 0;
255f8bc0014Sigy 	if (h->flags & PCIC_FLAG_ENABLED) {
256f8bc0014Sigy 		r = pcic_read(h, PCIC_INTR);
257f8bc0014Sigy 		r &= ~(PCIC_INTR_IRQ_MASK | PCIC_INTR_ENABLE);
258f8bc0014Sigy 		pcic_write(h, PCIC_INTR, r);
259f8bc0014Sigy         }
260f8bc0014Sigy 
261f8bc0014Sigy 	ih->ih_fun = NULL;
262f8bc0014Sigy 	ih->ih_arg = NULL;
263f8bc0014Sigy 
264f8bc0014Sigy 	splx(s);
265f8bc0014Sigy }
266f8bc0014Sigy 
267f8bc0014Sigy /*
268f8bc0014Sigy  * interrupt handler
269f8bc0014Sigy  */
270f8bc0014Sigy static int
pcic_vrip_intr(void * arg)271f8bc0014Sigy pcic_vrip_intr(void *arg)
272f8bc0014Sigy {
273f8bc0014Sigy 	struct pcic_vrip_softc	*vsc = arg;
27416c0f838Stsutsui 	struct pcic_softc	*sc = &vsc->sc_pcic;
275f8bc0014Sigy 	int			i;
2765770fb4eStsutsui 	uint16_t		r;
277f8bc0014Sigy 
278f8bc0014Sigy 	r = bus_space_read_2(sc->iot, sc->ioh, ECU_INTSTAT_REG_W)
279f8bc0014Sigy 		& ~vsc->sc_intr_mask;
280f8bc0014Sigy 
281f8bc0014Sigy 	for (i = 0; i < ECU_MAX_INTR; i++) {
282f8bc0014Sigy 		struct intrhand	*ih = &vsc->sc_intrhand[i];
283f8bc0014Sigy 		if (ih->ih_fun && (r & (1 << i)))
284f8bc0014Sigy 			ih->ih_fun(ih->ih_arg);
285f8bc0014Sigy 	}
286f8bc0014Sigy 	return 1;
287f8bc0014Sigy }
288