xref: /netbsd-src/sys/arch/dreamcast/dev/g2/gapspci_pci.c (revision 3c7a3199b61179961749a6b659cab2fe11746cc5)
1*3c7a3199Sandvar /*	$NetBSD: gapspci_pci.c,v 1.18 2021/08/07 19:41:13 andvar Exp $	*/
226cf6921Sthorpej 
326cf6921Sthorpej /*-
426cf6921Sthorpej  * Copyright (c) 2001 Marcus Comstedt.
526cf6921Sthorpej  * Copyright (c) 2001 Jason R. Thorpe.
626cf6921Sthorpej  * All rights reserved.
726cf6921Sthorpej  *
826cf6921Sthorpej  * Redistribution and use in source and binary forms, with or without
926cf6921Sthorpej  * modification, are permitted provided that the following conditions
1026cf6921Sthorpej  * are met:
1126cf6921Sthorpej  * 1. Redistributions of source code must retain the above copyright
1226cf6921Sthorpej  *    notice, this list of conditions and the following disclaimer.
1326cf6921Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
1426cf6921Sthorpej  *    notice, this list of conditions and the following disclaimer in the
1526cf6921Sthorpej  *    documentation and/or other materials provided with the distribution.
1626cf6921Sthorpej  * 3. All advertising materials mentioning features or use of this software
1726cf6921Sthorpej  *    must display the following acknowledgement:
1826cf6921Sthorpej  *	This product includes software developed by Marcus Comstedt.
1926cf6921Sthorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
2026cf6921Sthorpej  *    contributors may be used to endorse or promote products derived
2126cf6921Sthorpej  *    from this software without specific prior written permission.
2226cf6921Sthorpej  *
2326cf6921Sthorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2426cf6921Sthorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2526cf6921Sthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2626cf6921Sthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2726cf6921Sthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2826cf6921Sthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2926cf6921Sthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3026cf6921Sthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3126cf6921Sthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3226cf6921Sthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3326cf6921Sthorpej  * POSSIBILITY OF SUCH DAMAGE.
3426cf6921Sthorpej  */
3526cf6921Sthorpej 
3626cf6921Sthorpej /*
37*3c7a3199Sandvar  * PCI configuration space implementation for the SEGA GAPS PCI bridge.
3826cf6921Sthorpej  */
3926cf6921Sthorpej 
4026cf6921Sthorpej #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
41*3c7a3199Sandvar __KERNEL_RCSID(0, "$NetBSD: gapspci_pci.c,v 1.18 2021/08/07 19:41:13 andvar Exp $");
4226cf6921Sthorpej 
4326cf6921Sthorpej #include <sys/param.h>
4426cf6921Sthorpej #include <sys/systm.h>
4526cf6921Sthorpej #include <sys/kernel.h>
4626cf6921Sthorpej #include <sys/device.h>
4786b5be6eSdyoung #include <sys/bus.h>
4826cf6921Sthorpej 
4926cf6921Sthorpej #include <machine/cpu.h>
50bc069b93Smarcus #include <machine/sysasicvar.h>
5126cf6921Sthorpej 
5226cf6921Sthorpej #include <dev/pci/pcivar.h>
5326cf6921Sthorpej #include <dev/pci/pcireg.h>
5426cf6921Sthorpej 
5526cf6921Sthorpej #include <dreamcast/dev/g2/gapspcivar.h>
5626cf6921Sthorpej 
57b1361f43Stsutsui void		gaps_attach_hook(device_t, device_t,
5826cf6921Sthorpej 		    struct pcibus_attach_args *);
5926cf6921Sthorpej int		gaps_bus_maxdevs(void *, int);
6026cf6921Sthorpej pcitag_t	gaps_make_tag(void *, int, int, int);
61e19aab37Sthorpej void		gaps_decompose_tag(void *, pcitag_t, int *, int *, int *);
6226cf6921Sthorpej pcireg_t	gaps_conf_read(void *, pcitag_t, int);
6326cf6921Sthorpej void		gaps_conf_write(void *, pcitag_t, int, pcireg_t);
6426cf6921Sthorpej 
65dabc7fa7Sknakahara int		gaps_intr_map(const struct pci_attach_args *, pci_intr_handle_t *);
662997c336Smartin const char	*gaps_intr_string(void *, pci_intr_handle_t,
672997c336Smartin 		    char *buf, size_t len);
6826cf6921Sthorpej void		*gaps_intr_establish(void *, pci_intr_handle_t,
6926cf6921Sthorpej 		    int, int (*)(void *), void *);
7026cf6921Sthorpej void		gaps_intr_disestablish(void *, void *);
7126cf6921Sthorpej 
7226cf6921Sthorpej void
gaps_pci_init(struct gaps_softc * sc)7326cf6921Sthorpej gaps_pci_init(struct gaps_softc *sc)
7426cf6921Sthorpej {
7526cf6921Sthorpej 	pci_chipset_tag_t pc = &sc->sc_pc;
7626cf6921Sthorpej 
7726cf6921Sthorpej 	memset(pc, 0, sizeof(*pc));
7826cf6921Sthorpej 
7926cf6921Sthorpej 	pc->pc_attach_hook = gaps_attach_hook;
8026cf6921Sthorpej 	pc->pc_bus_maxdevs = gaps_bus_maxdevs;
8126cf6921Sthorpej 	pc->pc_make_tag = gaps_make_tag;
82e19aab37Sthorpej 	pc->pc_decompose_tag = gaps_decompose_tag;
8326cf6921Sthorpej 	pc->pc_conf_read = gaps_conf_read;
8426cf6921Sthorpej 	pc->pc_conf_write = gaps_conf_write;
8526cf6921Sthorpej 	pc->pc_conf_v = sc;
8626cf6921Sthorpej 
8726cf6921Sthorpej 	pc->pc_intr_map = gaps_intr_map;
8826cf6921Sthorpej 	pc->pc_intr_string = gaps_intr_string;
8926cf6921Sthorpej 	pc->pc_intr_establish = gaps_intr_establish;
9026cf6921Sthorpej 	pc->pc_intr_disestablish = gaps_intr_disestablish;
9126cf6921Sthorpej 
9226cf6921Sthorpej 	if (bus_space_map(sc->sc_memt, 0x01001600, 0x100,
9326cf6921Sthorpej 	    0, &sc->sc_pci_memh) != 0)
9426cf6921Sthorpej 		panic("gaps_pci_init: can't map PCI configuration space");
9526cf6921Sthorpej }
9626cf6921Sthorpej 
9726cf6921Sthorpej #define	GAPS_PCITAG_MAGIC	0x022473
9826cf6921Sthorpej 
9926cf6921Sthorpej void
gaps_attach_hook(device_t parent,device_t pci,struct pcibus_attach_args * pba)100b1361f43Stsutsui gaps_attach_hook(device_t parent, device_t pci, struct pcibus_attach_args *pba)
10126cf6921Sthorpej {
102b1361f43Stsutsui 	struct gaps_softc *sc = device_private(parent);
10326cf6921Sthorpej 
10426cf6921Sthorpej 	/*
10526cf6921Sthorpej 	 * Now that we know there's a bus configured, go ahead and
10626cf6921Sthorpej 	 * program the BAR on the device.
10726cf6921Sthorpej 	 */
10826cf6921Sthorpej 	pci_conf_write(&sc->sc_pc, GAPS_PCITAG_MAGIC,
10926cf6921Sthorpej 	    PCI_MAPREG_START + 4, 0x01000000);
11026cf6921Sthorpej 	pci_conf_write(&sc->sc_pc, GAPS_PCITAG_MAGIC, PCI_COMMAND_STATUS_REG,
11126cf6921Sthorpej 	    pci_conf_read(&sc->sc_pc, 0, PCI_COMMAND_STATUS_REG) |
11226cf6921Sthorpej 	    PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE);
11326cf6921Sthorpej }
11426cf6921Sthorpej 
11526cf6921Sthorpej int
gaps_bus_maxdevs(void * v,int bus)11626cf6921Sthorpej gaps_bus_maxdevs(void *v, int bus)
11726cf6921Sthorpej {
11826cf6921Sthorpej 
119cb6453dbStsutsui 	return 1;
12026cf6921Sthorpej }
12126cf6921Sthorpej 
12226cf6921Sthorpej pcitag_t
gaps_make_tag(void * v,int bus,int dev,int func)12326cf6921Sthorpej gaps_make_tag(void *v, int bus, int dev, int func)
12426cf6921Sthorpej {
12526cf6921Sthorpej 
12626cf6921Sthorpej 	if (bus == 0 && dev == 0 && func == 0)
127cb6453dbStsutsui 		return GAPS_PCITAG_MAGIC;
12826cf6921Sthorpej 
129cb6453dbStsutsui 	return 0;
13026cf6921Sthorpej }
13126cf6921Sthorpej 
132e19aab37Sthorpej void
gaps_decompose_tag(void * v,pcitag_t tag,int * bp,int * dp,int * fp)133e19aab37Sthorpej gaps_decompose_tag(void *v, pcitag_t tag, int *bp, int *dp, int *fp)
134e19aab37Sthorpej {
135e19aab37Sthorpej 	int b, d, f;
136e19aab37Sthorpej 
137e19aab37Sthorpej 	if (tag == GAPS_PCITAG_MAGIC)
138e19aab37Sthorpej 		b = d = f = 0;
139e19aab37Sthorpej 	else {
140e19aab37Sthorpej 		/*
141e19aab37Sthorpej 		 * Invalid for GAPS.  These values ensure that a valid
142e19aab37Sthorpej 		 * tag cannot be built.
143e19aab37Sthorpej 		 */
144e19aab37Sthorpej 		b = 0xff;
145e19aab37Sthorpej 		d = 0x1f;
146e19aab37Sthorpej 		f = 0x7;
147e19aab37Sthorpej 	}
148e19aab37Sthorpej 
149e19aab37Sthorpej 	if (bp != NULL)
150e19aab37Sthorpej 		*bp = b;
151e19aab37Sthorpej 	if (dp != NULL)
152e19aab37Sthorpej 		*dp = d;
153e19aab37Sthorpej 	if (fp != NULL)
154e19aab37Sthorpej 		*fp = f;
155e19aab37Sthorpej }
156e19aab37Sthorpej 
15726cf6921Sthorpej pcireg_t
gaps_conf_read(void * v,pcitag_t tag,int reg)15826cf6921Sthorpej gaps_conf_read(void *v, pcitag_t tag, int reg)
15926cf6921Sthorpej {
16026cf6921Sthorpej 	struct gaps_softc *sc = v;
16126cf6921Sthorpej 
16226cf6921Sthorpej 	if (tag != GAPS_PCITAG_MAGIC)
163cb6453dbStsutsui 		return -1;
16426cf6921Sthorpej 
165605f564fSmsaitoh 	if ((unsigned int)reg >= PCI_CONF_SIZE)
166605f564fSmsaitoh 		return -1;
167605f564fSmsaitoh 
16826cf6921Sthorpej 	if (reg == (PCI_MAPREG_START + 4)) {
16926cf6921Sthorpej 		/*
17026cf6921Sthorpej 		 * We fake the BAR -- just return the physical address
17126cf6921Sthorpej 		 * to which the device is mapped.
17226cf6921Sthorpej 		 */
173cb6453dbStsutsui 		return 0x01001700;
17426cf6921Sthorpej 	}
17526cf6921Sthorpej 
176cb6453dbStsutsui 	return bus_space_read_4(sc->sc_memt, sc->sc_pci_memh, reg);
17726cf6921Sthorpej }
17826cf6921Sthorpej 
17926cf6921Sthorpej void
gaps_conf_write(void * v,pcitag_t tag,int reg,pcireg_t val)18026cf6921Sthorpej gaps_conf_write(void *v, pcitag_t tag, int reg, pcireg_t val)
18126cf6921Sthorpej {
18226cf6921Sthorpej 	struct gaps_softc *sc = v;
18326cf6921Sthorpej 
18426cf6921Sthorpej 	if (tag != GAPS_PCITAG_MAGIC)
18526cf6921Sthorpej 		return;
18626cf6921Sthorpej 
187605f564fSmsaitoh 	if ((unsigned int)reg >= PCI_CONF_SIZE)
188605f564fSmsaitoh 		return;
189605f564fSmsaitoh 
19026cf6921Sthorpej 	/* Disallow writing to the "BAR" ... it doesn't actually exist. */
19126cf6921Sthorpej 	if (reg == (PCI_MAPREG_START + 4) && val != 0x01000000)
19226cf6921Sthorpej 		return;
19326cf6921Sthorpej 
19426cf6921Sthorpej 	bus_space_write_4(sc->sc_memt, sc->sc_pci_memh, reg, val);
19526cf6921Sthorpej }
19626cf6921Sthorpej 
19726cf6921Sthorpej int
gaps_intr_map(const struct pci_attach_args * pa,pci_intr_handle_t * ihp)198dabc7fa7Sknakahara gaps_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
19926cf6921Sthorpej {
20026cf6921Sthorpej 
201e8300f36Such 	*ihp = SYSASIC_EVENT_EXT;
202cb6453dbStsutsui 	return 0;
20326cf6921Sthorpej }
20426cf6921Sthorpej 
20526cf6921Sthorpej const char *
gaps_intr_string(void * v,pci_intr_handle_t ih,char * buf,size_t len)2062997c336Smartin gaps_intr_string(void *v, pci_intr_handle_t ih,
2072997c336Smartin     char *buf, size_t len)
20826cf6921Sthorpej {
20926cf6921Sthorpej 
21034ebc08aSchristos 	strlcpy(buf, sysasic_intr_string(SYSASIC_IRL11), len);
21134ebc08aSchristos 	return buf;
21226cf6921Sthorpej }
21326cf6921Sthorpej 
21426cf6921Sthorpej void *
gaps_intr_establish(void * v,pci_intr_handle_t ih,int level,int (* func)(void *),void * arg)21526cf6921Sthorpej gaps_intr_establish(void *v, pci_intr_handle_t ih, int level,
21626cf6921Sthorpej     int (*func)(void *), void *arg)
21726cf6921Sthorpej {
218e8300f36Such 
2190b5d7fc8Stsutsui 	return sysasic_intr_establish(ih, level, SYSASIC_IRL11, func, arg);
22026cf6921Sthorpej }
22126cf6921Sthorpej 
22226cf6921Sthorpej void
gaps_intr_disestablish(void * v,void * ih)22326cf6921Sthorpej gaps_intr_disestablish(void *v, void *ih)
22426cf6921Sthorpej {
22526cf6921Sthorpej 
226cb6453dbStsutsui 	return sysasic_intr_disestablish(ih);
22726cf6921Sthorpej }
228