xref: /netbsd-src/sys/arch/i386/pci/amd756.c (revision f5b064ee385650bff4906d7abf18d635e842b8ee)
1*f5b064eeSdyoung /*	$NetBSD: amd756.c,v 1.9 2011/07/01 17:37:26 dyoung Exp $	*/
29b730bc3Such 
39b730bc3Such /*-
49b730bc3Such  * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
59b730bc3Such  * All rights reserved.
69b730bc3Such  *
79b730bc3Such  * This code is derived from software contributed to The NetBSD Foundation
89b730bc3Such  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
99b730bc3Such  * NASA Ames Research Center.
109b730bc3Such  *
119b730bc3Such  * Redistribution and use in source and binary forms, with or without
129b730bc3Such  * modification, are permitted provided that the following conditions
139b730bc3Such  * are met:
149b730bc3Such  * 1. Redistributions of source code must retain the above copyright
159b730bc3Such  *    notice, this list of conditions and the following disclaimer.
169b730bc3Such  * 2. Redistributions in binary form must reproduce the above copyright
179b730bc3Such  *    notice, this list of conditions and the following disclaimer in the
189b730bc3Such  *    documentation and/or other materials provided with the distribution.
199b730bc3Such  *
209b730bc3Such  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
219b730bc3Such  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
229b730bc3Such  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
239b730bc3Such  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
249b730bc3Such  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
259b730bc3Such  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
269b730bc3Such  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
279b730bc3Such  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
289b730bc3Such  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
299b730bc3Such  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
309b730bc3Such  * POSSIBILITY OF SUCH DAMAGE.
319b730bc3Such  */
329b730bc3Such 
339b730bc3Such /*
349b730bc3Such  * Copyright (c) 1999, by UCHIYAMA Yasushi
359b730bc3Such  * All rights reserved.
369b730bc3Such  *
379b730bc3Such  * Redistribution and use in source and binary forms, with or without
389b730bc3Such  * modification, are permitted provided that the following conditions
399b730bc3Such  * are met:
409b730bc3Such  * 1. Redistributions of source code must retain the above copyright
419b730bc3Such  *    notice, this list of conditions and the following disclaimer.
429b730bc3Such  * 2. The name of the developer may NOT be used to endorse or promote products
439b730bc3Such  *    derived from this software without specific prior written permission.
449b730bc3Such  *
459b730bc3Such  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
469b730bc3Such  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
479b730bc3Such  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
489b730bc3Such  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
499b730bc3Such  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
509b730bc3Such  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
519b730bc3Such  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
529b730bc3Such  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
539b730bc3Such  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
549b730bc3Such  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
559b730bc3Such  * SUCH DAMAGE.
569b730bc3Such  */
579b730bc3Such 
589b730bc3Such /*
599b730bc3Such  * Support for the Advanced Micro Devices AMD756 Peripheral Bus Controller.
609b730bc3Such  */
619b730bc3Such 
6295c969f2Slukem #include <sys/cdefs.h>
63*f5b064eeSdyoung __KERNEL_RCSID(0, "$NetBSD: amd756.c,v 1.9 2011/07/01 17:37:26 dyoung Exp $");
6495c969f2Slukem 
659b730bc3Such #include <sys/param.h>
669b730bc3Such #include <sys/systm.h>
679b730bc3Such #include <sys/device.h>
689b730bc3Such #include <sys/malloc.h>
699b730bc3Such 
709b730bc3Such #include <machine/intr.h>
71*f5b064eeSdyoung #include <sys/bus.h>
729b730bc3Such 
739b730bc3Such #include <dev/pci/pcivar.h>
749b730bc3Such #include <dev/pci/pcireg.h>
759b730bc3Such #include <dev/pci/pcidevs.h>
769b730bc3Such 
779b730bc3Such #include <i386/pci/pci_intr_fixup.h>
789b730bc3Such #include <i386/pci/amd756reg.h>
799b730bc3Such 
809b730bc3Such struct amd756_handle {
819b730bc3Such 	bus_space_tag_t ph_iot;
829b730bc3Such 	bus_space_handle_t ph_regs_ioh;
839b730bc3Such 	pci_chipset_tag_t ph_pc;
849b730bc3Such 	pcitag_t ph_tag;
859b730bc3Such };
869b730bc3Such 
87e8085086Skochi int	amd756_getclink(pciintr_icu_handle_t, int, int *);
88e8085086Skochi int	amd756_get_intr(pciintr_icu_handle_t, int, int *);
89e8085086Skochi int	amd756_set_intr(pciintr_icu_handle_t, int, int);
90e8085086Skochi int	amd756_get_trigger(pciintr_icu_handle_t, int, int *);
91e8085086Skochi int	amd756_set_trigger(pciintr_icu_handle_t, int, int);
929b730bc3Such #ifdef AMD756_DEBUG
93e8085086Skochi static void	amd756_pir_dump(struct amd756_handle *);
949b730bc3Such #endif
959b730bc3Such 
969b730bc3Such const struct pciintr_icu amd756_pci_icu = {
979b730bc3Such 	amd756_getclink,
989b730bc3Such 	amd756_get_intr,
999b730bc3Such 	amd756_set_intr,
1009b730bc3Such 	amd756_get_trigger,
1019b730bc3Such 	amd756_set_trigger,
1029b730bc3Such };
1039b730bc3Such 
1049b730bc3Such 
1059b730bc3Such int
amd756_init(pci_chipset_tag_t pc,bus_space_tag_t iot,pcitag_t tag,pciintr_icu_tag_t * ptagp,pciintr_icu_handle_t * phandp)106e8085086Skochi amd756_init(pci_chipset_tag_t pc, bus_space_tag_t iot, pcitag_t tag,
107e8085086Skochi     pciintr_icu_tag_t *ptagp, pciintr_icu_handle_t *phandp)
1089b730bc3Such {
1099b730bc3Such 	struct amd756_handle *ph;
1109b730bc3Such 
1119b730bc3Such 	ph = malloc(sizeof(*ph), M_DEVBUF, M_NOWAIT);
1129b730bc3Such 	if (ph == NULL)
1139b730bc3Such 		return (1);
1149b730bc3Such 
1159b730bc3Such 	ph->ph_iot = iot;
1169b730bc3Such 	ph->ph_pc = pc;
1179b730bc3Such 	ph->ph_tag = tag;
1189b730bc3Such 
1199b730bc3Such 	*ptagp = &amd756_pci_icu;
1209b730bc3Such 	*phandp = ph;
1219b730bc3Such 
1229b730bc3Such #ifdef AMD756_DEBUG
1239b730bc3Such 	amd756_pir_dump(ph);
1249b730bc3Such #endif
1259b730bc3Such 
1269b730bc3Such 	return (0);
1279b730bc3Such }
1289b730bc3Such 
1299b730bc3Such int
amd756_getclink(pciintr_icu_handle_t v,int link,int * clinkp)130168cd830Schristos amd756_getclink(pciintr_icu_handle_t v, int link, int *clinkp)
1319b730bc3Such {
1329b730bc3Such 	if (AMD756_LEGAL_LINK(link - 1) == 0)
1339b730bc3Such 		return (1);
1349b730bc3Such 
1359b730bc3Such 	*clinkp = link - 1;
1369b730bc3Such 
1379b730bc3Such 	return (0);
1389b730bc3Such }
1399b730bc3Such 
1409b730bc3Such int
amd756_get_intr(pciintr_icu_handle_t v,int clink,int * irqp)141e8085086Skochi amd756_get_intr(pciintr_icu_handle_t v, int clink, int *irqp)
1429b730bc3Such {
1439b730bc3Such 	struct amd756_handle *ph = v;
1449b730bc3Such 	pcireg_t reg;
1459b730bc3Such 	int val;
1469b730bc3Such 
1479b730bc3Such 	if (AMD756_LEGAL_LINK(clink) == 0)
1489b730bc3Such 		return (1);
1499b730bc3Such 
1509b730bc3Such 	reg = AMD756_GET_PIIRQSEL(ph);
1519b730bc3Such 	val = (reg >> (4 * clink)) & 0x0f;
1529b730bc3Such 	*irqp = (val == 0) ?
153ab4edb55Sfvdl 	    X86_PCI_INTERRUPT_LINE_NO_CONNECTION : val;
1549b730bc3Such 
1559b730bc3Such 	return (0);
1569b730bc3Such }
1579b730bc3Such 
1589b730bc3Such int
amd756_set_intr(pciintr_icu_handle_t v,int clink,int irq)159e8085086Skochi amd756_set_intr(pciintr_icu_handle_t v, int clink, int irq)
1609b730bc3Such {
1619b730bc3Such 	struct amd756_handle *ph = v;
1629b730bc3Such 	int val;
1639b730bc3Such 	pcireg_t reg;
1649b730bc3Such 
1659b730bc3Such 	if (AMD756_LEGAL_LINK(clink) == 0 || AMD756_LEGAL_IRQ(irq) == 0)
1669b730bc3Such 		return (1);
1679b730bc3Such 
1689b730bc3Such 	reg = AMD756_GET_PIIRQSEL(ph);
1699b730bc3Such 	amd756_get_intr(v, clink, &val);
1709b730bc3Such 	reg &= ~(0x000f << (4 * clink));
1719b730bc3Such 	reg |= irq << (4 * clink);
1729b730bc3Such 	AMD756_SET_PIIRQSEL(ph, reg);
1739b730bc3Such 
1749b730bc3Such 	return (0);
1759b730bc3Such }
1769b730bc3Such 
1779b730bc3Such int
amd756_get_trigger(pciintr_icu_handle_t v,int irq,int * triggerp)178e8085086Skochi amd756_get_trigger(pciintr_icu_handle_t v, int irq, int *triggerp)
1799b730bc3Such {
1809b730bc3Such 	struct amd756_handle *ph = v;
1819b730bc3Such 	int i, pciirq;
1829b730bc3Such 	pcireg_t reg;
1839b730bc3Such 
1849b730bc3Such 	if (AMD756_LEGAL_IRQ(irq) == 0)
1859b730bc3Such 		return (1);
1869b730bc3Such 
1879b730bc3Such 	for (i = 0; i <= 3; i++) {
1889b730bc3Such 		amd756_get_intr(v, i, &pciirq);
1899b730bc3Such 		if (pciirq == irq) {
1909b730bc3Such 			reg = AMD756_GET_EDGESEL(ph);
1919b730bc3Such 			if (reg & (1 << i))
1929b730bc3Such 				*triggerp = IST_EDGE;
1939b730bc3Such 			else
1949b730bc3Such 				*triggerp = IST_LEVEL;
1959b730bc3Such 			break;
1969b730bc3Such 		}
1979b730bc3Such 	}
1989b730bc3Such 
1999b730bc3Such 	return (0);
2009b730bc3Such }
2019b730bc3Such 
2029b730bc3Such int
amd756_set_trigger(pciintr_icu_handle_t v,int irq,int trigger)203e8085086Skochi amd756_set_trigger(pciintr_icu_handle_t v, int irq, int trigger)
2049b730bc3Such {
2059b730bc3Such 	struct amd756_handle *ph = v;
2069b730bc3Such 	int i, pciirq;
2079b730bc3Such 	pcireg_t reg;
2089b730bc3Such 
2099b730bc3Such 	if (AMD756_LEGAL_IRQ(irq) == 0)
2109b730bc3Such 		return (1);
2119b730bc3Such 
2129b730bc3Such 	for (i = 0; i <= 3; i++) {
2139b730bc3Such 		amd756_get_intr(v, i, &pciirq);
2149b730bc3Such 		if (pciirq == irq) {
2159b730bc3Such 			reg = AMD756_GET_PIIRQSEL(ph);
2169b730bc3Such 			if (trigger == IST_LEVEL)
2179b730bc3Such 				reg &= ~(1 << (4 * i));
2189b730bc3Such 			else
2199b730bc3Such 				reg |= 1 << (4 * i);
2209b730bc3Such 			AMD756_SET_PIIRQSEL(ph, reg);
2219b730bc3Such 			break;
2229b730bc3Such 		}
2239b730bc3Such 	}
2249b730bc3Such 
2259b730bc3Such 	return (0);
2269b730bc3Such }
2279b730bc3Such 
2289b730bc3Such #ifdef AMD756_DEBUG
2299b730bc3Such static void
amd756_pir_dump(struct amd756_handle * ph)230e8085086Skochi amd756_pir_dump(struct amd756_handle *ph)
2319b730bc3Such {
2329b730bc3Such 	int a, b;
2339b730bc3Such 
2349b730bc3Such 	printf ("AMD756 PCI INTERRUPT ROUTING REGISTERS:\n");
2359b730bc3Such 
2369b730bc3Such 	a = AMD756_GET_EDGESEL(ph);
2379b730bc3Such 	b = AMD756_GET_PIIRQSEL(ph);
2389b730bc3Such 
2399b730bc3Such 	printf ("TRIGGER: %02x, ROUTING: %04x\n", a, b);
2409b730bc3Such }
2419b730bc3Such #endif
242