1168404Spjd/* $NetBSD: pci_up1000.c,v 1.17 2021/06/19 16:59:07 thorpej Exp $ */
2168404Spjd
3168404Spjd/*-
4168404Spjd * Copyright (c) 2000 The NetBSD Foundation, Inc.
5168404Spjd * All rights reserved.
6168792Sru *
7168404Spjd * This code is derived from software contributed to The NetBSD Foundation
8168792Sru * by Jason R. Thorpe.
9168404Spjd *
10168792Sru * Redistribution and use in source and binary forms, with or without
11168404Spjd * modification, are permitted provided that the following conditions
12168792Sru * are met:
13168404Spjd * 1. Redistributions of source code must retain the above copyright
14168792Sru *    notice, this list of conditions and the following disclaimer.
15168404Spjd * 2. Redistributions in binary form must reproduce the above copyright
16168792Sru *    notice, this list of conditions and the following disclaimer in the
17168404Spjd *    documentation and/or other materials provided with the distribution.
18168792Sru *
19168792Sru * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20168792Sru * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21168792Sru * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22168792Sru * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23168404Spjd * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24168792Sru * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25168792Sru * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26168404Spjd * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27168792Sru * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28168792Sru * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29168792Sru * POSSIBILITY OF SUCH DAMAGE.
30168792Sru */
31168792Sru
32168792Sru#include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
33168792Sru
34168792Sru__KERNEL_RCSID(0, "$NetBSD: pci_up1000.c,v 1.17 2021/06/19 16:59:07 thorpej Exp $");
35168792Sru
36168792Sru#include <sys/types.h>
37168792Sru#include <sys/param.h>
38168404Spjd#include <sys/time.h>
39168792Sru#include <sys/systm.h>
40168792Sru#include <sys/errno.h>
41168404Spjd#include <sys/device.h>
42168404Spjd
43168404Spjd#include <machine/autoconf.h>
44168404Spjd#include <machine/rpb.h>
45168404Spjd#include <sys/bus.h>
46#include <machine/intr.h>
47
48#include <dev/isa/isavar.h>
49
50#include <dev/pci/pcireg.h>
51#include <dev/pci/pcivar.h>
52#include <dev/pci/pciidereg.h>
53#include <dev/pci/pciidevar.h>
54
55#include <alpha/pci/irongatevar.h>
56
57#include <alpha/pci/siovar.h>
58#include <alpha/pci/sioreg.h>
59
60#include "sio.h"
61
62static int	api_up1000_intr_map(const struct pci_attach_args *,
63		    pci_intr_handle_t *);
64
65static void
66pci_up1000_pickintr(void *core, bus_space_tag_t iot, bus_space_tag_t memt,
67    pci_chipset_tag_t pc)
68{
69#if NSIO
70	pc->pc_intr_v = core;
71	pc->pc_intr_map = api_up1000_intr_map;
72	pc->pc_intr_string = sio_pci_intr_string;
73	pc->pc_intr_evcnt = sio_pci_intr_evcnt;
74	pc->pc_intr_establish = sio_pci_intr_establish;
75	pc->pc_intr_disestablish = sio_pci_intr_disestablish;
76
77	pc->pc_pciide_compat_intr_establish =
78	    sio_pciide_compat_intr_establish;
79
80	sio_intr_setup(pc, iot);
81#else
82	panic("pci_up1000_pickintr: no I/O interrupt handler (no sio)");
83#endif
84}
85ALPHA_PCI_INTR_INIT(ST_API_NAUTILUS, pci_up1000_pickintr)
86
87static int
88api_up1000_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
89{
90	pci_chipset_tag_t pc = pa->pa_pc;
91	int buspin = pa->pa_intrpin;
92	int bustag = pa->pa_intrtag;
93	int line = pa->pa_intrline;
94	int bus, device, function;
95
96	if (buspin == 0) {
97		/* No IRQ used. */
98		return 1;
99	}
100	if (buspin < 0 || buspin > 4) {
101		printf("%s: bad interrupt pin %d\n", __func__, buspin);
102		return 1;
103	}
104
105	pci_decompose_tag(pc, bustag, &bus, &device, &function);
106
107	/*
108	 * The console places the interrupt mapping in the "line" value.
109	 * A value of (char)-1 indicates there is no mapping.
110	 */
111	if (line == 0xff) {
112		printf("%s: no mapping for %d/%d/%d\n", __func__,
113		    bus, device, function);
114		return (1);
115	}
116
117	if (line < 0 || line > 15) {
118		printf("%s: bad ISA IRQ (%d)\n", __func__, line);
119		return (1);
120	}
121	if (line == 2) {
122		printf("%s: changed IRQ 2 to IRQ 9\n", __func__);
123		line = 9;
124	}
125
126	alpha_pci_intr_handle_init(ihp, line, 0);
127	return (0);
128}
129