1*8367e471Sthorpej /* $NetBSD: iyonix_pci.c,v 1.2 2020/06/17 07:01:02 thorpej Exp $ */
26fe6c35aSmacallan
36fe6c35aSmacallan /*
46fe6c35aSmacallan * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
56fe6c35aSmacallan * All rights reserved.
66fe6c35aSmacallan *
76fe6c35aSmacallan * Based on code written by Jason R. Thorpe for Wasabi Systems, Inc.
86fe6c35aSmacallan *
96fe6c35aSmacallan * Redistribution and use in source and binary forms, with or without
106fe6c35aSmacallan * modification, are permitted provided that the following conditions
116fe6c35aSmacallan * are met:
126fe6c35aSmacallan * 1. Redistributions of source code must retain the above copyright
136fe6c35aSmacallan * notice, this list of conditions and the following disclaimer.
146fe6c35aSmacallan * 2. Redistributions in binary form must reproduce the above copyright
156fe6c35aSmacallan * notice, this list of conditions and the following disclaimer in the
166fe6c35aSmacallan * documentation and/or other materials provided with the distribution.
176fe6c35aSmacallan * 3. All advertising materials mentioning features or use of this software
186fe6c35aSmacallan * must display the following acknowledgement:
196fe6c35aSmacallan * This product includes software developed for the NetBSD Project by
206fe6c35aSmacallan * Wasabi Systems, Inc.
216fe6c35aSmacallan * 4. The name of Wasabi Systems, Inc. may not be used to endorse
226fe6c35aSmacallan * or promote products derived from this software without specific prior
236fe6c35aSmacallan * written permission.
246fe6c35aSmacallan *
256fe6c35aSmacallan * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
266fe6c35aSmacallan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
276fe6c35aSmacallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
286fe6c35aSmacallan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
296fe6c35aSmacallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
306fe6c35aSmacallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
316fe6c35aSmacallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
326fe6c35aSmacallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
336fe6c35aSmacallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
346fe6c35aSmacallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
356fe6c35aSmacallan * POSSIBILITY OF SUCH DAMAGE.
366fe6c35aSmacallan */
376fe6c35aSmacallan
386fe6c35aSmacallan /*
396fe6c35aSmacallan * Iyonix PCI interrupt support.
406fe6c35aSmacallan */
416fe6c35aSmacallan
426fe6c35aSmacallan #include <sys/cdefs.h>
43*8367e471Sthorpej __KERNEL_RCSID(0, "$NetBSD: iyonix_pci.c,v 1.2 2020/06/17 07:01:02 thorpej Exp $");
446fe6c35aSmacallan
456fe6c35aSmacallan #include <sys/param.h>
466fe6c35aSmacallan #include <sys/systm.h>
476fe6c35aSmacallan #include <sys/device.h>
486fe6c35aSmacallan
496fe6c35aSmacallan #include <machine/autoconf.h>
506fe6c35aSmacallan #include <sys/bus.h>
516fe6c35aSmacallan
526fe6c35aSmacallan #include <evbarm/iyonix/iyonixreg.h>
536fe6c35aSmacallan #include <evbarm/iyonix/iyonixvar.h>
546fe6c35aSmacallan
556fe6c35aSmacallan #include <arm/xscale/i80321reg.h>
566fe6c35aSmacallan #include <arm/xscale/i80321var.h>
576fe6c35aSmacallan
586fe6c35aSmacallan #include <dev/pci/pcidevs.h>
596fe6c35aSmacallan #include <dev/pci/ppbreg.h>
606fe6c35aSmacallan
616fe6c35aSmacallan #include <dev/pci/pciconf.h>
626fe6c35aSmacallan
636fe6c35aSmacallan int iyonix_pci_intr_map(const struct pci_attach_args *,
646fe6c35aSmacallan pci_intr_handle_t *);
656fe6c35aSmacallan const char *iyonix_pci_intr_string(void *, pci_intr_handle_t, char *, size_t);
666fe6c35aSmacallan const struct evcnt *iyonix_pci_intr_evcnt(void *, pci_intr_handle_t);
676fe6c35aSmacallan void *iyonix_pci_intr_establish(void *, pci_intr_handle_t,
686fe6c35aSmacallan int, int (*func)(void *), void *, const char *);
696fe6c35aSmacallan void iyonix_pci_intr_disestablish(void *, void *);
706fe6c35aSmacallan void pci_conf_write_byte(pci_chipset_tag_t, pcitag_t, int, int);
716fe6c35aSmacallan int pci_conf_read_byte(pci_chipset_tag_t, pcitag_t, int);
726fe6c35aSmacallan int iyonix_pci_conf_hook(void *, int, int, int, pcireg_t);
736fe6c35aSmacallan
746fe6c35aSmacallan void
iyonix_pci_init(pci_chipset_tag_t pc,void * cookie)756fe6c35aSmacallan iyonix_pci_init(pci_chipset_tag_t pc, void *cookie)
766fe6c35aSmacallan {
776fe6c35aSmacallan
786fe6c35aSmacallan pc->pc_intr_v = cookie; /* the i80321 softc */
796fe6c35aSmacallan pc->pc_intr_map = iyonix_pci_intr_map;
806fe6c35aSmacallan pc->pc_intr_string = iyonix_pci_intr_string;
816fe6c35aSmacallan pc->pc_intr_evcnt = iyonix_pci_intr_evcnt;
826fe6c35aSmacallan pc->pc_intr_establish = iyonix_pci_intr_establish;
836fe6c35aSmacallan pc->pc_intr_disestablish = iyonix_pci_intr_disestablish;
846fe6c35aSmacallan pc->pc_conf_hook = iyonix_pci_conf_hook;
856fe6c35aSmacallan }
866fe6c35aSmacallan
876fe6c35aSmacallan int
iyonix_pci_intr_map(const struct pci_attach_args * pa,pci_intr_handle_t * ihp)886fe6c35aSmacallan iyonix_pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
896fe6c35aSmacallan {
906fe6c35aSmacallan struct i80321_softc *sc = pa->pa_pc->pc_intr_v;
916fe6c35aSmacallan int b, d, f;
926fe6c35aSmacallan uint32_t busno;
936fe6c35aSmacallan
946fe6c35aSmacallan busno = bus_space_read_4(sc->sc_st, sc->sc_atu_sh, ATU_PCIXSR);
956fe6c35aSmacallan busno = PCIXSR_BUSNO(busno);
966fe6c35aSmacallan if (busno == 0xff)
976fe6c35aSmacallan busno = 0;
986fe6c35aSmacallan
996fe6c35aSmacallan pci_decompose_tag(pa->pa_pc, pa->pa_intrtag, &b, &d, &f);
1006fe6c35aSmacallan
1016fe6c35aSmacallan /* No mappings for devices not on our bus. */
1026fe6c35aSmacallan if (b != busno)
1036fe6c35aSmacallan goto no_mapping;
1046fe6c35aSmacallan
1056fe6c35aSmacallan /*
1066fe6c35aSmacallan * XXX We currently deal only with the southbridge and with
1076fe6c35aSmacallan * regular PCI. IOC devices may need further attention.
1086fe6c35aSmacallan */
1096fe6c35aSmacallan
1106fe6c35aSmacallan /* Devices on the southbridge are all routed through xint 1 */
1116fe6c35aSmacallan if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ALI) {
1126fe6c35aSmacallan switch (PCI_PRODUCT(pa->pa_id)) {
1136fe6c35aSmacallan case PCI_PRODUCT_ALI_M1543: /* Southbridge */
1146fe6c35aSmacallan case PCI_PRODUCT_ALI_M5229: /* ATA */
1156fe6c35aSmacallan case PCI_PRODUCT_ALI_M5237: /* ohci */
1166fe6c35aSmacallan case PCI_PRODUCT_ALI_M5257: /* Modem */
1176fe6c35aSmacallan case PCI_PRODUCT_ALI_M5451: /* AC97 */
1186fe6c35aSmacallan case PCI_PRODUCT_ALI_M7101: /* PMC */
1196fe6c35aSmacallan *ihp = ICU_INT_XINT(1);
1206fe6c35aSmacallan return (0);
1216fe6c35aSmacallan }
1226fe6c35aSmacallan }
1236fe6c35aSmacallan
1246fe6c35aSmacallan /* Route other interrupts with default swizzling rule */
1256fe6c35aSmacallan *ihp = ICU_INT_XINT((d + pa->pa_intrpin - 1) % 4);
1266fe6c35aSmacallan return 0;
1276fe6c35aSmacallan
1286fe6c35aSmacallan no_mapping:
1296fe6c35aSmacallan printf("iyonix_pci_intr_map: no mapping for %d/%d/%d\n",
1306fe6c35aSmacallan pa->pa_bus, pa->pa_device, pa->pa_function);
1316fe6c35aSmacallan return (1);
1326fe6c35aSmacallan }
1336fe6c35aSmacallan
1346fe6c35aSmacallan const char *
iyonix_pci_intr_string(void * v,pci_intr_handle_t ih,char * buf,size_t len)1356fe6c35aSmacallan iyonix_pci_intr_string(void *v, pci_intr_handle_t ih, char *buf, size_t len)
1366fe6c35aSmacallan {
1376fe6c35aSmacallan
1386fe6c35aSmacallan strlcpy(buf, i80321_irqnames[ih], len);
1396fe6c35aSmacallan return buf;
1406fe6c35aSmacallan }
1416fe6c35aSmacallan
1426fe6c35aSmacallan const struct evcnt *
iyonix_pci_intr_evcnt(void * v,pci_intr_handle_t ih)1436fe6c35aSmacallan iyonix_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
1446fe6c35aSmacallan {
1456fe6c35aSmacallan
1466fe6c35aSmacallan /* XXX For now. */
1476fe6c35aSmacallan return (NULL);
1486fe6c35aSmacallan }
1496fe6c35aSmacallan
1506fe6c35aSmacallan void *
iyonix_pci_intr_establish(void * v,pci_intr_handle_t ih,int ipl,int (* func)(void *),void * arg,const char * xname)1516fe6c35aSmacallan iyonix_pci_intr_establish(void *v, pci_intr_handle_t ih, int ipl,
1526fe6c35aSmacallan int (*func)(void *), void *arg, const char *xname)
1536fe6c35aSmacallan {
1546fe6c35aSmacallan
1556fe6c35aSmacallan return (i80321_intr_establish(ih, ipl, func, arg));
1566fe6c35aSmacallan }
1576fe6c35aSmacallan
1586fe6c35aSmacallan void
iyonix_pci_intr_disestablish(void * v,void * cookie)1596fe6c35aSmacallan iyonix_pci_intr_disestablish(void *v, void *cookie)
1606fe6c35aSmacallan {
1616fe6c35aSmacallan
1626fe6c35aSmacallan i80321_intr_disestablish(cookie);
1636fe6c35aSmacallan }
1646fe6c35aSmacallan
1656fe6c35aSmacallan void
pci_conf_write_byte(pci_chipset_tag_t pc,pcitag_t tag,int addr,int value)1666fe6c35aSmacallan pci_conf_write_byte(pci_chipset_tag_t pc, pcitag_t tag, int addr, int value)
1676fe6c35aSmacallan {
1686fe6c35aSmacallan int temp;
1696fe6c35aSmacallan temp = pci_conf_read(pc, tag, addr&~3);
1706fe6c35aSmacallan temp = temp & ~(0xff << ((addr%4) * 8));
1716fe6c35aSmacallan temp = temp | (value << ((addr%4) * 8));
1726fe6c35aSmacallan pci_conf_write(pc, tag, addr&~3, temp);
1736fe6c35aSmacallan }
1746fe6c35aSmacallan
1756fe6c35aSmacallan int
pci_conf_read_byte(pci_chipset_tag_t pc,pcitag_t tag,int addr)1766fe6c35aSmacallan pci_conf_read_byte(pci_chipset_tag_t pc, pcitag_t tag, int addr)
1776fe6c35aSmacallan {
1786fe6c35aSmacallan int temp;
1796fe6c35aSmacallan temp = pci_conf_read(pc, tag, addr&~3);
1806fe6c35aSmacallan temp = temp >> ((addr%4) * 8);
1816fe6c35aSmacallan temp = temp & 0xff;
1826fe6c35aSmacallan return temp;
1836fe6c35aSmacallan }
1846fe6c35aSmacallan
1856fe6c35aSmacallan int
iyonix_pci_conf_hook(void * v,int bus,int dev,int func,pcireg_t id)1866fe6c35aSmacallan iyonix_pci_conf_hook(void *v, int bus, int dev, int func, pcireg_t id)
1876fe6c35aSmacallan {
1886fe6c35aSmacallan
1896fe6c35aSmacallan /*
1906fe6c35aSmacallan * We need to disable devices in the Southbridge, and as
1916fe6c35aSmacallan * we have all the tags we need at this point, this is
1926fe6c35aSmacallan * where we do it.
1936fe6c35aSmacallan */
1946fe6c35aSmacallan if (PCI_VENDOR(id) == PCI_VENDOR_ALI &&
1956fe6c35aSmacallan PCI_PRODUCT(id) == PCI_PRODUCT_ALI_M1543)
1966fe6c35aSmacallan {
1976fe6c35aSmacallan pcitag_t tag;
1986fe6c35aSmacallan int status;
1996fe6c35aSmacallan pci_chipset_tag_t pc = (pci_chipset_tag_t) v;
2006fe6c35aSmacallan
2016fe6c35aSmacallan tag = pci_make_tag(pc, bus, dev, func);
2026fe6c35aSmacallan
2036fe6c35aSmacallan /* Undocumented magic */
2046fe6c35aSmacallan
2056fe6c35aSmacallan /* Disable USB */
2066fe6c35aSmacallan pci_conf_write_byte(pc, tag, 0x53, 0x40);
2076fe6c35aSmacallan pci_conf_write_byte(pc, tag, 0x52, 0x00);
2086fe6c35aSmacallan
2096fe6c35aSmacallan status = pci_conf_read_byte(pc, tag, 0x7e);
2106fe6c35aSmacallan pci_conf_write_byte(pc, tag, 0x7e, status & ~0x80);
2116fe6c35aSmacallan
2126fe6c35aSmacallan /* Disable modem */
2136fe6c35aSmacallan pci_conf_write_byte(pc, tag, 0x77, 1 << 6);
2146fe6c35aSmacallan
2156fe6c35aSmacallan /* Disable SCI */
2166fe6c35aSmacallan pci_conf_write_byte(pc, tag, 0x78, 1 << 7);
2176fe6c35aSmacallan }
2186fe6c35aSmacallan
2196fe6c35aSmacallan return (PCI_CONF_DEFAULT);
2206fe6c35aSmacallan }
221