1*a6ce3504Sthorpej /* $NetBSD: ali1543.c,v 1.13 2023/12/20 15:00:08 thorpej Exp $ */
231d98218Shaya
331d98218Shaya /*
431d98218Shaya * Copyright (c) 2001
531d98218Shaya * HAYAKAWA Koichi. All rights reserved.
631d98218Shaya *
731d98218Shaya * Redistribution and use in source and binary forms, with or without
831d98218Shaya * modification, are permitted provided that the following conditions
931d98218Shaya * are met:
1031d98218Shaya * 1. Redistributions of source code must retain the above copyright
1131d98218Shaya * notice, this list of conditions and the following disclaimer.
1231d98218Shaya * 2. Redistributions in binary form must reproduce the above copyright
1331d98218Shaya * notice, this list of conditions and the following disclaimer in the
1431d98218Shaya * documentation and/or other materials provided with the distribution.
1531d98218Shaya *
1631d98218Shaya * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1731d98218Shaya * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1831d98218Shaya * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1931d98218Shaya * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2031d98218Shaya * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2131d98218Shaya * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2231d98218Shaya * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2331d98218Shaya * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2431d98218Shaya * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
2531d98218Shaya * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2631d98218Shaya * POSSIBILITY OF SUCH DAMAGE.
2731d98218Shaya */
2831d98218Shaya
2931d98218Shaya /*-
3031d98218Shaya * Copyright (c) 1999 The NetBSD Foundation, Inc.
3131d98218Shaya * All rights reserved.
3231d98218Shaya *
3331d98218Shaya * This code is derived from software contributed to The NetBSD Foundation
3431d98218Shaya * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
3531d98218Shaya * NASA Ames Research Center.
3631d98218Shaya *
3731d98218Shaya * Redistribution and use in source and binary forms, with or without
3831d98218Shaya * modification, are permitted provided that the following conditions
3931d98218Shaya * are met:
4031d98218Shaya * 1. Redistributions of source code must retain the above copyright
4131d98218Shaya * notice, this list of conditions and the following disclaimer.
4231d98218Shaya * 2. Redistributions in binary form must reproduce the above copyright
4331d98218Shaya * notice, this list of conditions and the following disclaimer in the
4431d98218Shaya * documentation and/or other materials provided with the distribution.
4531d98218Shaya *
4631d98218Shaya * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
4731d98218Shaya * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
4831d98218Shaya * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4931d98218Shaya * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5031d98218Shaya * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5131d98218Shaya * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5231d98218Shaya * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5331d98218Shaya * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
5431d98218Shaya * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5531d98218Shaya * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
5631d98218Shaya * POSSIBILITY OF SUCH DAMAGE.
5731d98218Shaya */
5831d98218Shaya
5931d98218Shaya /*
6031d98218Shaya * Copyright (c) 1999, by UCHIYAMA Yasushi
6131d98218Shaya * All rights reserved.
6231d98218Shaya *
6331d98218Shaya * Redistribution and use in source and binary forms, with or without
6431d98218Shaya * modification, are permitted provided that the following conditions
6531d98218Shaya * are met:
6631d98218Shaya * 1. Redistributions of source code must retain the above copyright
6731d98218Shaya * notice, this list of conditions and the following disclaimer.
6831d98218Shaya * 2. The name of the developer may NOT be used to endorse or promote products
6931d98218Shaya * derived from this software without specific prior written permission.
7031d98218Shaya *
7131d98218Shaya * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
7231d98218Shaya * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7331d98218Shaya * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7431d98218Shaya * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
7531d98218Shaya * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
7631d98218Shaya * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
7731d98218Shaya * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
7831d98218Shaya * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
7931d98218Shaya * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
8031d98218Shaya * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
8131d98218Shaya * SUCH DAMAGE.
8231d98218Shaya */
8331d98218Shaya
8431d98218Shaya /* HAYAKAWA Koichi wrote ALi 1543 PCI ICU code basing on VIA82C586 driver */
8531d98218Shaya
8695c969f2Slukem #include <sys/cdefs.h>
87*a6ce3504Sthorpej __KERNEL_RCSID(0, "$NetBSD: ali1543.c,v 1.13 2023/12/20 15:00:08 thorpej Exp $");
8895c969f2Slukem
8931d98218Shaya #include <sys/param.h>
9031d98218Shaya #include <sys/systm.h>
9131d98218Shaya #include <sys/errno.h>
9231d98218Shaya #include <sys/device.h>
9331d98218Shaya #include <sys/proc.h>
9431d98218Shaya
9531d98218Shaya #include <machine/intr.h>
96f5b064eeSdyoung #include <sys/bus.h>
9731d98218Shaya
9831d98218Shaya #include <dev/pci/pcivar.h>
9931d98218Shaya #include <dev/pci/pcireg.h>
10031d98218Shaya #include <dev/pci/pcidevs.h>
10131d98218Shaya
10231d98218Shaya #include <i386/pci/pci_intr_fixup.h>
10331d98218Shaya #include <i386/pci/piixvar.h>
10431d98218Shaya
10531d98218Shaya
10631d98218Shaya int ali1543_getclink(pciintr_icu_handle_t, int, int *);
10731d98218Shaya int ali1543_get_intr(pciintr_icu_handle_t, int, int *);
10831d98218Shaya int ali1543_set_intr(pciintr_icu_handle_t, int, int);
10931d98218Shaya
11031d98218Shaya
11131d98218Shaya const struct pciintr_icu ali1543_icu = {
11231d98218Shaya ali1543_getclink,
11331d98218Shaya ali1543_get_intr,
11431d98218Shaya ali1543_set_intr,
1155341bac8Stshiozak piix_get_trigger,
1165341bac8Stshiozak piix_set_trigger,
11731d98218Shaya };
11831d98218Shaya
11931d98218Shaya
12031d98218Shaya /*
12131d98218Shaya * Linux source code (linux/arch/i386/kernel/pci-irq.c) says that the
12231d98218Shaya * irq order of ALi PCI ICU is shuffled.
12331d98218Shaya */
124b4509098Ssimonb static const int ali1543_intr_shuffle_get[16] = {
12531d98218Shaya 0, 9, 3, 10, 4, 5, 7, 6, 1, 11, 0, 12, 0, 14, 0, 15
12631d98218Shaya };
127b4509098Ssimonb static const int ali1543_intr_shuffle_set[16] = {
12831d98218Shaya 0, 8, 0, 2, 4, 5, 7, 6, 0, 1, 3, 9, 11, 0, 13, 15
12931d98218Shaya };
13031d98218Shaya
13131d98218Shaya #define ALI1543_IRQ_MASK 0xdefa
13231d98218Shaya
13331d98218Shaya #define ALI1543_LEGAL_LINK(link) (((link) >= 0) && ((link) <= 7))
13431d98218Shaya #define ALI1543_LEGAL_IRQ(irq) ((1 << (irq)) & ALI1543_IRQ_MASK)
13531d98218Shaya
13631d98218Shaya #define ALI1543_INTR_CFG_REG 0x48
13731d98218Shaya
13831d98218Shaya #define ALI1543_INTR_PIRQA_SHIFT 0
13931d98218Shaya #define ALI1543_INTR_PIRQA_MASK 0x0000000f
14031d98218Shaya #define ALI1543_INTR_PIRQB_SHIFT 4
14131d98218Shaya #define ALI1543_INTR_PIRQB_MASK 0x000000f0
14231d98218Shaya #define ALI1543_INTR_PIRQC_SHIFT 8
14331d98218Shaya #define ALI1543_INTR_PIRQC_MASK 0x00000f00
14431d98218Shaya #define ALI1543_INTR_PIRQD_SHIFT 12
14531d98218Shaya #define ALI1543_INTR_PIRQD_MASK 0x0000f000
14631d98218Shaya
14731d98218Shaya #define ALI1543_INTR_PIRQ_SHIFT(clink) ((clink)*4)
14831d98218Shaya #define ALI1543_INTR_PIRQ_IRQ(reg, clink) \
14931d98218Shaya (((reg) >> ((clink)*4)) & 0x0f)
15031d98218Shaya #define ALI1543_PIRQ(reg, clink) \
15131d98218Shaya ali1543_intr_shuffle_get[ALI1543_INTR_PIRQ_IRQ((reg), (clink))]
15231d98218Shaya
15331d98218Shaya
15431d98218Shaya int
ali1543_init(pci_chipset_tag_t pc,bus_space_tag_t iot,pcitag_t tag,pciintr_icu_tag_t * ptagp,pciintr_icu_handle_t * phandp)15531d98218Shaya ali1543_init(pci_chipset_tag_t pc, bus_space_tag_t iot, pcitag_t tag,
15631d98218Shaya pciintr_icu_tag_t *ptagp, pciintr_icu_handle_t *phandp)
15731d98218Shaya {
15831d98218Shaya
15931d98218Shaya if (piix_init(pc, iot, tag, ptagp, phandp) == 0) {
16031d98218Shaya *ptagp = &ali1543_icu;
16131d98218Shaya
16231d98218Shaya return (0);
16331d98218Shaya }
16431d98218Shaya
16531d98218Shaya return (1);
16631d98218Shaya }
16731d98218Shaya
16831d98218Shaya int
ali1543_getclink(pciintr_icu_handle_t v,int link,int * clinkp)169168cd830Schristos ali1543_getclink(pciintr_icu_handle_t v, int link, int *clinkp)
17031d98218Shaya {
17131d98218Shaya
17231d98218Shaya if (ALI1543_LEGAL_LINK(link - 1)) {
17331d98218Shaya *clinkp = link - 1;
17431d98218Shaya return (0);
17531d98218Shaya }
17631d98218Shaya
17731d98218Shaya return (1);
17831d98218Shaya }
17931d98218Shaya
18031d98218Shaya int
ali1543_get_intr(pciintr_icu_handle_t v,int clink,int * irqp)18131d98218Shaya ali1543_get_intr(pciintr_icu_handle_t v, int clink, int *irqp)
18231d98218Shaya {
18331d98218Shaya struct piix_handle *ph = v;
18431d98218Shaya pcireg_t reg;
18531d98218Shaya int val;
18631d98218Shaya
18731d98218Shaya if (ALI1543_LEGAL_LINK(clink) == 0)
18831d98218Shaya return (1);
18931d98218Shaya
19031d98218Shaya reg = pci_conf_read(ph->ph_pc, ph->ph_tag, ALI1543_INTR_CFG_REG);
19131d98218Shaya #ifdef DEBUG_1543
19231d98218Shaya printf("ali1543: PIRQ reg 0x%08x\n", reg); /* XXX debug */
19331d98218Shaya #endif /* DEBUG_1543 */
19431d98218Shaya val = ALI1543_PIRQ(reg, clink);
19531d98218Shaya *irqp = (val == 0) ?
196ab4edb55Sfvdl X86_PCI_INTERRUPT_LINE_NO_CONNECTION : val;
19731d98218Shaya
19831d98218Shaya return (0);
19931d98218Shaya }
20031d98218Shaya
20131d98218Shaya int
ali1543_set_intr(pciintr_icu_handle_t v,int clink,int irq)20231d98218Shaya ali1543_set_intr(pciintr_icu_handle_t v, int clink, int irq)
20331d98218Shaya {
20431d98218Shaya struct piix_handle *ph = v;
20531d98218Shaya int shift, val;
20631d98218Shaya pcireg_t reg;
20731d98218Shaya
20831d98218Shaya if (ALI1543_LEGAL_LINK(clink) == 0 || ALI1543_LEGAL_IRQ(irq) == 0)
20931d98218Shaya return (1);
21031d98218Shaya
21131d98218Shaya reg = pci_conf_read(ph->ph_pc, ph->ph_tag, ALI1543_INTR_CFG_REG);
21231d98218Shaya ali1543_get_intr(v, clink, &val);
21331d98218Shaya shift = ALI1543_INTR_PIRQ_SHIFT(clink);
21431d98218Shaya reg &= ~(0x0f << shift);
21531d98218Shaya reg |= (ali1543_intr_shuffle_set[irq] << shift);
21631d98218Shaya pci_conf_write(ph->ph_pc, ph->ph_tag, ALI1543_INTR_CFG_REG, reg);
21731d98218Shaya if (ali1543_get_intr(v, clink, &val) != 0 || val != irq)
21831d98218Shaya return (1);
21931d98218Shaya
22031d98218Shaya return (0);
22131d98218Shaya }
222