1*863a28deStsutsui /* $NetBSD: pci_hades.c,v 1.16 2019/05/04 09:03:08 tsutsui Exp $ */
268f5fe6eSleo
368f5fe6eSleo /*
468f5fe6eSleo * Copyright (c) 1996 Leo Weppelman. All rights reserved.
568f5fe6eSleo * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
668f5fe6eSleo * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
768f5fe6eSleo *
868f5fe6eSleo * Redistribution and use in source and binary forms, with or without
968f5fe6eSleo * modification, are permitted provided that the following conditions
1068f5fe6eSleo * are met:
1168f5fe6eSleo * 1. Redistributions of source code must retain the above copyright
1268f5fe6eSleo * notice, this list of conditions and the following disclaimer.
1368f5fe6eSleo * 2. Redistributions in binary form must reproduce the above copyright
1468f5fe6eSleo * notice, this list of conditions and the following disclaimer in the
1568f5fe6eSleo * documentation and/or other materials provided with the distribution.
1668f5fe6eSleo * 3. All advertising materials mentioning features or use of this software
1768f5fe6eSleo * must display the following acknowledgement:
1868f5fe6eSleo * This product includes software developed by Charles M. Hannum.
1968f5fe6eSleo * 4. The name of the author may not be used to endorse or promote products
2068f5fe6eSleo * derived from this software without specific prior written permission.
2168f5fe6eSleo *
2268f5fe6eSleo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2368f5fe6eSleo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2468f5fe6eSleo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2568f5fe6eSleo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2668f5fe6eSleo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2768f5fe6eSleo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2868f5fe6eSleo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2968f5fe6eSleo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3068f5fe6eSleo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3168f5fe6eSleo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3268f5fe6eSleo */
3368f5fe6eSleo
34e803bea7Slukem #include <sys/cdefs.h>
35*863a28deStsutsui __KERNEL_RCSID(0, "$NetBSD: pci_hades.c,v 1.16 2019/05/04 09:03:08 tsutsui Exp $");
36e803bea7Slukem
3768f5fe6eSleo #include <sys/types.h>
3868f5fe6eSleo #include <sys/param.h>
3968f5fe6eSleo #include <sys/systm.h>
4068f5fe6eSleo #include <sys/device.h>
4168f5fe6eSleo
42720768a1Sthorpej #include <uvm/uvm_extern.h>
43720768a1Sthorpej
44ed9977b1Sdyoung #include <sys/bus.h>
4568f5fe6eSleo
4668f5fe6eSleo #include <dev/pci/pcivar.h>
4768f5fe6eSleo #include <dev/pci/pcireg.h>
4868f5fe6eSleo
4968f5fe6eSleo #include <machine/cpu.h>
5068f5fe6eSleo #include <machine/iomap.h>
5168f5fe6eSleo #include <machine/mfp.h>
526f0f9f87Sdsl #include <sys/bswap.h>
5368f5fe6eSleo
5468f5fe6eSleo #include <atari/atari/device.h>
5568f5fe6eSleo #include <atari/pci/pci_vga.h>
56bbad0a5dSleo #include <atari/dev/grf_etreg.h>
5768f5fe6eSleo
5868f5fe6eSleo int
pci_bus_maxdevs(pci_chipset_tag_t pc,int busno)59454af1c0Sdsl pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
6068f5fe6eSleo {
619b30bd93Stsutsui
629b30bd93Stsutsui return 4;
6368f5fe6eSleo }
6468f5fe6eSleo
6502cdf4d2Sdsl static int pci_config_offset(pcitag_t);
6668f5fe6eSleo
6768f5fe6eSleo /*
68720768a1Sthorpej * Atari_init.c maps the config areas PAGE_SIZE bytes apart....
6968f5fe6eSleo */
709b30bd93Stsutsui static int
pci_config_offset(pcitag_t tag)719b30bd93Stsutsui pci_config_offset(pcitag_t tag)
7268f5fe6eSleo {
7368f5fe6eSleo int device;
7468f5fe6eSleo
7568f5fe6eSleo device = (tag >> 11) & 0x1f;
769b30bd93Stsutsui
779b30bd93Stsutsui return device * PAGE_SIZE;
7868f5fe6eSleo }
7968f5fe6eSleo
8068f5fe6eSleo pcireg_t
pci_conf_read(pci_chipset_tag_t pc,pcitag_t tag,int reg)81454af1c0Sdsl pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
8268f5fe6eSleo {
839b30bd93Stsutsui uint32_t data;
8468f5fe6eSleo
859b30bd93Stsutsui if ((uint32_t)reg >= PCI_CONF_SIZE)
869b30bd93Stsutsui return 0xffffffff;
87605f564fSmsaitoh
889b30bd93Stsutsui data = *(uint32_t *)(pci_conf_addr + pci_config_offset(tag) + reg);
899b30bd93Stsutsui return bswap32(data);
9068f5fe6eSleo }
9168f5fe6eSleo
9268f5fe6eSleo void
pci_conf_write(pci_chipset_tag_t pc,pcitag_t tag,int reg,pcireg_t data)93454af1c0Sdsl pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
9468f5fe6eSleo {
95605f564fSmsaitoh
969b30bd93Stsutsui if ((uint32_t)reg >= PCI_CONF_SIZE)
97605f564fSmsaitoh return;
98605f564fSmsaitoh
999b30bd93Stsutsui *((uint32_t *)(pci_conf_addr + pci_config_offset(tag) + reg))
10068f5fe6eSleo = bswap32(data);
10168f5fe6eSleo }
10268f5fe6eSleo
10368f5fe6eSleo /*
10468f5fe6eSleo * The interrupt stuff is rather ugly. On the Hades, all interrupt lines
10568f5fe6eSleo * for a slot are wired together and connected to IO 0,1,2 or 5 (slots:
10668f5fe6eSleo * (0-3) on the TT-MFP. The Pci-config code initializes the irq. number
10768f5fe6eSleo * to the slot position.
10868f5fe6eSleo */
10968f5fe6eSleo static pci_intr_info_t iinfo[4] = { { -1 }, { -1 }, { -1 }, { -1 } };
11068f5fe6eSleo
11102cdf4d2Sdsl static int iifun(int, int);
11268f5fe6eSleo
11368f5fe6eSleo static int
iifun(int slot,int sr)114454af1c0Sdsl iifun(int slot, int sr)
11568f5fe6eSleo {
11668f5fe6eSleo pci_intr_info_t *iinfo_p;
11768f5fe6eSleo int s;
11868f5fe6eSleo
11968f5fe6eSleo iinfo_p = &iinfo[slot];
12068f5fe6eSleo
12168f5fe6eSleo /*
12268f5fe6eSleo * Disable the interrupts
12368f5fe6eSleo */
12468f5fe6eSleo MFP2->mf_imrb &= ~iinfo_p->imask;
12568f5fe6eSleo
12668f5fe6eSleo if ((sr & PSL_IPL) >= (iinfo_p->ipl & PSL_IPL)) {
12768f5fe6eSleo /*
12868f5fe6eSleo * We're running at a too high priority now.
12968f5fe6eSleo */
13068f5fe6eSleo add_sicallback((si_farg)iifun, (void*)slot, 0);
1319b30bd93Stsutsui } else {
13268f5fe6eSleo s = splx(iinfo_p->ipl);
13368f5fe6eSleo (void)(iinfo_p->ifunc)(iinfo_p->iarg);
13468f5fe6eSleo splx(s);
13568f5fe6eSleo
13668f5fe6eSleo /*
13768f5fe6eSleo * Re-enable interrupts after handling
13868f5fe6eSleo */
13968f5fe6eSleo MFP2->mf_imrb |= iinfo_p->imask;
14068f5fe6eSleo }
14168f5fe6eSleo return 1;
14268f5fe6eSleo }
14368f5fe6eSleo
14448ced055Sad int
pci_intr_setattr(pci_chipset_tag_t pc,pci_intr_handle_t * ih,int attr,uint64_t data)14548ced055Sad pci_intr_setattr(pci_chipset_tag_t pc, pci_intr_handle_t *ih,
14648ced055Sad int attr, uint64_t data)
14748ced055Sad {
14848ced055Sad
14948ced055Sad switch (attr) {
15048ced055Sad case PCI_INTR_MPSAFE:
15148ced055Sad return 0;
15248ced055Sad default:
15348ced055Sad return ENODEV;
15448ced055Sad }
15548ced055Sad }
15648ced055Sad
15768f5fe6eSleo void *
pci_intr_establish(pci_chipset_tag_t pc,pci_intr_handle_t ih,int level,int (* ih_fun)(void *),void * ih_arg)1589b30bd93Stsutsui pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
1599b30bd93Stsutsui int (*ih_fun)(void *), void *ih_arg)
16068f5fe6eSleo {
16168f5fe6eSleo pci_intr_info_t *iinfo_p;
16268f5fe6eSleo struct intrhand *ihand;
16368f5fe6eSleo int slot;
16468f5fe6eSleo
16568f5fe6eSleo slot = ih;
16668f5fe6eSleo iinfo_p = &iinfo[slot];
16768f5fe6eSleo
16868f5fe6eSleo if (iinfo_p->ipl > 0)
1690f09ed48Sprovos panic("pci_intr_establish: interrupt was already established");
17068f5fe6eSleo
17168f5fe6eSleo ihand = intr_establish((slot == 3) ? 23 : 16 + slot, USER_VEC, 0,
17268f5fe6eSleo (hw_ifun_t)iifun, (void *)slot);
17368f5fe6eSleo if (ihand != NULL) {
17468f5fe6eSleo iinfo_p->ipl = level;
17568f5fe6eSleo iinfo_p->imask = (slot == 3) ? 0x80 : (0x01 << slot);
17668f5fe6eSleo iinfo_p->ifunc = ih_fun;
17768f5fe6eSleo iinfo_p->iarg = ih_arg;
17868f5fe6eSleo iinfo_p->ihand = ihand;
17968f5fe6eSleo
18068f5fe6eSleo /*
18168f5fe6eSleo * Enable (unmask) the interrupt
18268f5fe6eSleo */
18368f5fe6eSleo MFP2->mf_imrb |= iinfo_p->imask;
18468f5fe6eSleo MFP2->mf_ierb |= iinfo_p->imask;
1859b30bd93Stsutsui return iinfo_p;
18668f5fe6eSleo }
18768f5fe6eSleo return NULL;
18868f5fe6eSleo }
18968f5fe6eSleo
19068f5fe6eSleo void
pci_intr_disestablish(pci_chipset_tag_t pc,void * cookie)191454af1c0Sdsl pci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
19268f5fe6eSleo {
19368f5fe6eSleo pci_intr_info_t *iinfo_p = (pci_intr_info_t *)cookie;
19468f5fe6eSleo
19568f5fe6eSleo if (iinfo->ipl < 0)
1960f09ed48Sprovos panic("pci_intr_disestablish: interrupt was not established");
19768f5fe6eSleo
19868f5fe6eSleo MFP2->mf_imrb &= ~iinfo->imask;
19968f5fe6eSleo MFP2->mf_ierb &= ~iinfo->imask;
20068f5fe6eSleo (void)intr_disestablish(iinfo_p->ihand);
20168f5fe6eSleo iinfo_p->ipl = -1;
20268f5fe6eSleo }
203bbad0a5dSleo
204bbad0a5dSleo /*
205bbad0a5dSleo * XXX: Why are we repeating this everywhere! (Leo)
206bbad0a5dSleo */
207bbad0a5dSleo #define PCI_LINMEMBASE 0x0e000000
208bbad0a5dSleo
209*863a28deStsutsui static const uint8_t crt_tab[] = {
210bbad0a5dSleo 0x5f, 0x4f, 0x50, 0x82, 0x55, 0x81, 0xbf, 0x1f,
211bbad0a5dSleo 0x00, 0x4f, 0x0d, 0x0e, 0x00, 0x00, 0x00, 0x00,
212bbad0a5dSleo 0x9c, 0x8e, 0x8f, 0x28, 0x1f, 0x96, 0xb9, 0xa3,
213bbad0a5dSleo 0xff };
214bbad0a5dSleo
215*863a28deStsutsui static const uint8_t seq_tab[] = {
2169b30bd93Stsutsui 0x03, 0x00, 0x03, 0x00, 0x02, 0x00, 0x00, 0x00
2179b30bd93Stsutsui };
218bbad0a5dSleo
219*863a28deStsutsui static const uint8_t attr_tab[] = {
2209b30bd93Stsutsui 0x0c, 0x00, 0x0f, 0x08, 0x00, 0x00, 0x00, 0x00
2219b30bd93Stsutsui };
222bbad0a5dSleo
223*863a28deStsutsui static const uint8_t gdc_tab[] = {
2249b30bd93Stsutsui 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0e, 0x00, 0xff
2259b30bd93Stsutsui };
226bbad0a5dSleo
227bbad0a5dSleo void
ati_vga_init(pci_chipset_tag_t pc,pcitag_t tag,int id,volatile uint8_t * ba,uint8_t * fb)2289b30bd93Stsutsui ati_vga_init(pci_chipset_tag_t pc, pcitag_t tag, int id, volatile uint8_t *ba,
2299b30bd93Stsutsui uint8_t *fb)
230bbad0a5dSleo {
2319b30bd93Stsutsui uint32_t csr;
2329b30bd93Stsutsui int i;
233bbad0a5dSleo
234bbad0a5dSleo /* Turn on the card */
235bbad0a5dSleo pci_conf_write(pc, tag, PCI_MAPREG_START, PCI_LINMEMBASE);
236bbad0a5dSleo csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
237bbad0a5dSleo csr |= (PCI_COMMAND_MEM_ENABLE|PCI_COMMAND_IO_ENABLE);
238bbad0a5dSleo csr |= PCI_COMMAND_MASTER_ENABLE;
239bbad0a5dSleo pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
240bbad0a5dSleo
241bbad0a5dSleo /*
242bbad0a5dSleo * Make sure we're allowed to write all crt-registers and reload them.
243bbad0a5dSleo */
244bbad0a5dSleo WCrt(ba, CRT_ID_END_VER_RETR, (RCrt(ba, CRT_ID_END_VER_RETR) & 0x7f));
245bbad0a5dSleo
246bbad0a5dSleo for (i = 0; i < 0x18; i++)
247bbad0a5dSleo WCrt(ba, i, crt_tab[i]);
248bbad0a5dSleo for (i = 0; i < 8; i++)
249bbad0a5dSleo WSeq(ba, i, seq_tab[i]);
250bbad0a5dSleo for (i = 0; i < 9; i++)
251bbad0a5dSleo WGfx(ba, i, gdc_tab[i]);
252bbad0a5dSleo for (i = 0x10; i < 0x18; i++)
253bbad0a5dSleo WAttr(ba, i, attr_tab[i - 0x10]);
254bbad0a5dSleo WAttr(ba, 0x20, 0);
255bbad0a5dSleo }
256