1*7433666eSthorpej /* $NetBSD: sacc_hpcarm.c,v 1.15 2023/12/20 14:50:02 thorpej Exp $ */
210c7bfc7Sbsh
310c7bfc7Sbsh /*-
410c7bfc7Sbsh * Copyright (c) 2001 The NetBSD Foundation, Inc.
510c7bfc7Sbsh * All rights reserved.
610c7bfc7Sbsh *
710c7bfc7Sbsh * This code is derived from software contributed to The NetBSD Foundation
810c7bfc7Sbsh * by IWAMOTO Toshihiro.
910c7bfc7Sbsh *
1010c7bfc7Sbsh * Redistribution and use in source and binary forms, with or without
1110c7bfc7Sbsh * modification, are permitted provided that the following conditions
1210c7bfc7Sbsh * are met:
1310c7bfc7Sbsh * 1. Redistributions of source code must retain the above copyright
1410c7bfc7Sbsh * notice, this list of conditions and the following disclaimer.
1510c7bfc7Sbsh * 2. Redistributions in binary form must reproduce the above copyright
1610c7bfc7Sbsh * notice, this list of conditions and the following disclaimer in the
1710c7bfc7Sbsh * documentation and/or other materials provided with the distribution.
1810c7bfc7Sbsh *
1910c7bfc7Sbsh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2010c7bfc7Sbsh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2110c7bfc7Sbsh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2210c7bfc7Sbsh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2310c7bfc7Sbsh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2410c7bfc7Sbsh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2510c7bfc7Sbsh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2610c7bfc7Sbsh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2710c7bfc7Sbsh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2810c7bfc7Sbsh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2910c7bfc7Sbsh * POSSIBILITY OF SUCH DAMAGE.
3010c7bfc7Sbsh */
3110c7bfc7Sbsh
3210c7bfc7Sbsh /*
33257aff65Speter * Platform dependent part for SA-11[01]1 companion chip on hpcarm.
3410c7bfc7Sbsh */
3510c7bfc7Sbsh
3610c7bfc7Sbsh #include <sys/cdefs.h>
37*7433666eSthorpej __KERNEL_RCSID(0, "$NetBSD: sacc_hpcarm.c,v 1.15 2023/12/20 14:50:02 thorpej Exp $");
3810c7bfc7Sbsh
3910c7bfc7Sbsh #include <sys/param.h>
4010c7bfc7Sbsh #include <sys/systm.h>
4110c7bfc7Sbsh #include <sys/types.h>
4210c7bfc7Sbsh #include <sys/conf.h>
4310c7bfc7Sbsh #include <sys/device.h>
4410c7bfc7Sbsh #include <sys/kernel.h>
4510c7bfc7Sbsh #include <sys/uio.h>
469edf49b0Sdyoung #include <sys/bus.h>
4710c7bfc7Sbsh
4810c7bfc7Sbsh #include <machine/platid.h>
4910c7bfc7Sbsh #include <machine/platid_mask.h>
5010c7bfc7Sbsh
5110c7bfc7Sbsh #include <arm/sa11x0/sa11x0_reg.h>
5210c7bfc7Sbsh #include <arm/sa11x0/sa11x0_var.h>
5310c7bfc7Sbsh #include <arm/sa11x0/sa11x0_gpioreg.h>
5410c7bfc7Sbsh #include <arm/sa11x0/sa1111_reg.h>
5510c7bfc7Sbsh #include <arm/sa11x0/sa1111_var.h>
5610c7bfc7Sbsh
574c494f76Srjs static void sacc_attach(device_t, device_t, void *);
5810c7bfc7Sbsh static int sacc_intr(void *);
5910c7bfc7Sbsh
6010c7bfc7Sbsh struct platid_data sacc_platid_table[] = {
61a80c4462Speter { &platid_mask_MACH_HP_JORNADA_7XX, (void *)1 },
6210c7bfc7Sbsh { NULL, NULL }
6310c7bfc7Sbsh };
6410c7bfc7Sbsh
654c494f76Srjs CFATTACH_DECL_NEW(sacc, sizeof(struct sacc_softc),
6610c7bfc7Sbsh sacc_probe, sacc_attach, NULL, NULL);
6710c7bfc7Sbsh
6810c7bfc7Sbsh #ifdef INTR_DEBUG
694c494f76Srjs #define DPRINTF(arg) aprint_normal arg
7010c7bfc7Sbsh #else
7110c7bfc7Sbsh #define DPRINTF(arg)
7210c7bfc7Sbsh #endif
7310c7bfc7Sbsh
7410c7bfc7Sbsh static void
sacc_attach(device_t parent,device_t self,void * aux)754c494f76Srjs sacc_attach(device_t parent, device_t self, void *aux)
7610c7bfc7Sbsh {
7710c7bfc7Sbsh int i, gpiopin;
784a5a04e4Speter uint32_t skid;
794c494f76Srjs struct sacc_softc *sc = device_private(self);
804c494f76Srjs struct sa11x0_softc *psc = device_private(parent);
8110c7bfc7Sbsh struct sa11x0_attach_args *sa = aux;
8210c7bfc7Sbsh struct platid_data *p;
8310c7bfc7Sbsh
844c494f76Srjs aprint_normal("\n");
8510c7bfc7Sbsh
864c494f76Srjs sc->sc_dev = self;
8710c7bfc7Sbsh sc->sc_iot = sa->sa_iot;
8810c7bfc7Sbsh sc->sc_piot = psc->sc_iot;
8910c7bfc7Sbsh sc->sc_gpioh = psc->sc_gpioh;
90257aff65Speter
91257aff65Speter p = platid_search_data(&platid, sacc_platid_table);
92257aff65Speter if (p == NULL)
9310c7bfc7Sbsh return;
9410c7bfc7Sbsh
9510c7bfc7Sbsh gpiopin = (int)p->data;
9610c7bfc7Sbsh sc->sc_gpiomask = 1 << gpiopin;
9710c7bfc7Sbsh
9810c7bfc7Sbsh if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
9910c7bfc7Sbsh &sc->sc_ioh)) {
1004c494f76Srjs aprint_normal_dev(self, "unable to map registers\n");
10110c7bfc7Sbsh return;
10210c7bfc7Sbsh }
10310c7bfc7Sbsh
10410c7bfc7Sbsh skid = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACCSBI_SKID);
10510c7bfc7Sbsh
1064c494f76Srjs aprint_normal_dev(self, "SA-1111 rev %d.%d\n",
107a93b4acdSpeter (skid & 0xf0) >> 4, skid & 0xf);
10810c7bfc7Sbsh
10910c7bfc7Sbsh for (i = 0; i < SACCIC_LEN; i++)
11010c7bfc7Sbsh sc->sc_intrhand[i] = NULL;
11110c7bfc7Sbsh
112257aff65Speter /* initialize SA-1111 interrupt controller */
11310c7bfc7Sbsh bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTEN0, 0);
11410c7bfc7Sbsh bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTEN1, 0);
11510c7bfc7Sbsh bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTTSTSEL, 0);
11610c7bfc7Sbsh bus_space_write_4(sc->sc_iot, sc->sc_ioh,
11710c7bfc7Sbsh SACCIC_INTSTATCLR0, 0xffffffff);
11810c7bfc7Sbsh bus_space_write_4(sc->sc_iot, sc->sc_ioh,
11910c7bfc7Sbsh SACCIC_INTSTATCLR1, 0xffffffff);
12010c7bfc7Sbsh
121257aff65Speter /* connect to SA-1110's GPIO intr */
12210c7bfc7Sbsh sa11x0_intr_establish(0, gpiopin, 1, IPL_SERIAL, sacc_intr, sc);
12310c7bfc7Sbsh
124257aff65Speter /* attach each devices */
1252685996bSthorpej config_search(self, NULL,
126c7fb772bSthorpej CFARGS(.search = sa1111_search));
12710c7bfc7Sbsh }
12810c7bfc7Sbsh
12910c7bfc7Sbsh static int
sacc_intr(void * arg)1308a5e924bSpeter sacc_intr(void *arg)
13110c7bfc7Sbsh {
13210c7bfc7Sbsh int i;
1334a5a04e4Speter uint32_t mask;
13410c7bfc7Sbsh struct sacc_intrvec intstat;
13510c7bfc7Sbsh struct sacc_softc *sc = arg;
13610c7bfc7Sbsh struct sacc_intrhand *ih;
13710c7bfc7Sbsh
13810c7bfc7Sbsh intstat.lo =
13910c7bfc7Sbsh bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTSTATCLR0);
14010c7bfc7Sbsh intstat.hi =
14110c7bfc7Sbsh bus_space_read_4(sc->sc_iot, sc->sc_ioh, SACCIC_INTSTATCLR1);
142257aff65Speter DPRINTF(("sacc_intr: %x %x\n", intstat.lo, intstat.hi));
14310c7bfc7Sbsh
144257aff65Speter /* clear SA-1110's GPIO intr status */
14510c7bfc7Sbsh bus_space_write_4(sc->sc_piot, sc->sc_gpioh,
14610c7bfc7Sbsh SAGPIO_EDR, sc->sc_gpiomask);
14710c7bfc7Sbsh
14810c7bfc7Sbsh for (i = 0, mask = 1; i < 32; i++, mask <<= 1)
14910c7bfc7Sbsh if (intstat.lo & mask) {
15010c7bfc7Sbsh /*
15110c7bfc7Sbsh * Clear intr status before calling intr handlers.
15210c7bfc7Sbsh * This cause stray interrupts, but clearing
15310c7bfc7Sbsh * after calling intr handlers cause intr lossage.
15410c7bfc7Sbsh */
15510c7bfc7Sbsh bus_space_write_4(sc->sc_iot, sc->sc_ioh,
15610c7bfc7Sbsh SACCIC_INTSTATCLR0, 1 << i);
15710c7bfc7Sbsh
15810c7bfc7Sbsh for (ih = sc->sc_intrhand[i]; ih; ih = ih->ih_next)
1596bc0c582Smatt softint_schedule(ih->ih_soft);
16010c7bfc7Sbsh }
16110c7bfc7Sbsh for (i = 0, mask = 1; i < SACCIC_LEN - 32; i++, mask <<= 1)
16210c7bfc7Sbsh if (intstat.hi & mask) {
16310c7bfc7Sbsh bus_space_write_4(sc->sc_iot, sc->sc_ioh,
16410c7bfc7Sbsh SACCIC_INTSTATCLR1, 1 << i);
16510c7bfc7Sbsh for (ih = sc->sc_intrhand[i + 32]; ih; ih = ih->ih_next)
1666bc0c582Smatt softint_schedule(ih->ih_soft);
16710c7bfc7Sbsh }
16810c7bfc7Sbsh return 1;
16910c7bfc7Sbsh }
170