1*cde8f271Sriastradh /* $NetBSD: pci.c,v 1.6 2022/02/16 23:49:26 riastradh Exp $ */
22fb624f2Skiyohara
32fb624f2Skiyohara /*
42fb624f2Skiyohara * Copyright (C) 1995-1997 Gary Thomas (gdt@linuxppc.org)
52fb624f2Skiyohara * All rights reserved.
62fb624f2Skiyohara *
72fb624f2Skiyohara * Adapted from a program by:
82fb624f2Skiyohara * Steve Sellgren
92fb624f2Skiyohara * San Francisco Indigo Company
102fb624f2Skiyohara * sfindigo!sellgren@uunet.uu.net
112fb624f2Skiyohara * Adapted for Moto boxes by:
122fb624f2Skiyohara * Pat Kane & Mark Scott, 1996
132fb624f2Skiyohara * Fixed for IBM/PowerStack II Pat Kane 1997
142fb624f2Skiyohara *
152fb624f2Skiyohara * Redistribution and use in source and binary forms, with or without
162fb624f2Skiyohara * modification, are permitted provided that the following conditions
172fb624f2Skiyohara * are met:
182fb624f2Skiyohara * 1. Redistributions of source code must retain the above copyright
192fb624f2Skiyohara * notice, this list of conditions and the following disclaimer.
202fb624f2Skiyohara * 2. Redistributions in binary form must reproduce the above copyright
212fb624f2Skiyohara * notice, this list of conditions and the following disclaimer in the
222fb624f2Skiyohara * documentation and/or other materials provided with the distribution.
232fb624f2Skiyohara * 3. All advertising materials mentioning features or use of this software
242fb624f2Skiyohara * must display the following acknowledgement:
252fb624f2Skiyohara * This product includes software developed by Gary Thomas.
262fb624f2Skiyohara * 4. The name of the author may not be used to endorse or promote products
272fb624f2Skiyohara * derived from this software without specific prior written permission.
282fb624f2Skiyohara *
292fb624f2Skiyohara * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
302fb624f2Skiyohara * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
312fb624f2Skiyohara * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
322fb624f2Skiyohara * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
332fb624f2Skiyohara * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
342fb624f2Skiyohara * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352fb624f2Skiyohara * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362fb624f2Skiyohara * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372fb624f2Skiyohara * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
382fb624f2Skiyohara * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392fb624f2Skiyohara */
402fb624f2Skiyohara
412fb624f2Skiyohara #include <lib/libsa/stand.h>
422fb624f2Skiyohara #include <sys/bswap.h>
432fb624f2Skiyohara #include <dev/pci/pcireg.h>
442fb624f2Skiyohara #include "boot.h"
452fb624f2Skiyohara
462fb624f2Skiyohara #define NSLOTS 5
472fb624f2Skiyohara #define NPCIREGS 10
482fb624f2Skiyohara
492fb624f2Skiyohara
502fb624f2Skiyohara /*
512fb624f2Skiyohara * should use devfunc number/indirect method to be totally safe on
522fb624f2Skiyohara * all machines, this works for now on 3 slot Moto boxes
532fb624f2Skiyohara */
542fb624f2Skiyohara
552fb624f2Skiyohara #define PCI_CONFIG_SPACE_BASE 0x80800000
562fb624f2Skiyohara #define PCI_CONFIG_SPACE(d, f) \
572fb624f2Skiyohara (u_long *)(PCI_CONFIG_SPACE_BASE | (1 << (d)) | ((f) << 8))
582fb624f2Skiyohara
592fb624f2Skiyohara struct PCI_ConfigInfo {
602fb624f2Skiyohara u_long *config_addr;
612fb624f2Skiyohara u_long regs[NPCIREGS];
622fb624f2Skiyohara } PCI_slots [NSLOTS] = {
632fb624f2Skiyohara { PCI_CONFIG_SPACE(11, 0), { 0xDE, 0xAD, 0xBE, 0xEF } },
642fb624f2Skiyohara { PCI_CONFIG_SPACE(12, 0), { 0xDE, 0xAD, 0xBE, 0xEF } },
652fb624f2Skiyohara { PCI_CONFIG_SPACE(13, 0), { 0xDE, 0xAD, 0xBE, 0xEF } },
662fb624f2Skiyohara { PCI_CONFIG_SPACE(14, 0), { 0xDE, 0xAD, 0xBE, 0xEF } },
672fb624f2Skiyohara { PCI_CONFIG_SPACE(15, 0), { 0xDE, 0xAD, 0xBE, 0xEF } },
682fb624f2Skiyohara };
692fb624f2Skiyohara
702fb624f2Skiyohara
712fb624f2Skiyohara #define DEVID (PCI_ID_REG >> 2)
722fb624f2Skiyohara #define CMD (PCI_COMMAND_STATUS_REG >> 2)
732fb624f2Skiyohara #define CLASS (PCI_CLASS_REG >> 2)
742fb624f2Skiyohara #define BAR_BASE (PCI_MAPREG_START >> 2)
752fb624f2Skiyohara
762fb624f2Skiyohara /*
772fb624f2Skiyohara * The following code modifies the PCI Command register
782fb624f2Skiyohara * to enable memory and I/O accesses.
792fb624f2Skiyohara */
802fb624f2Skiyohara void
enablePCI(int slot,int io,int mem,int master)812fb624f2Skiyohara enablePCI(int slot, int io, int mem, int master)
822fb624f2Skiyohara {
832fb624f2Skiyohara volatile u_char *ppci;
842fb624f2Skiyohara u_char enable = 0;
852fb624f2Skiyohara
862fb624f2Skiyohara if (io)
872fb624f2Skiyohara enable |= PCI_COMMAND_IO_ENABLE;
882fb624f2Skiyohara if (mem)
892fb624f2Skiyohara enable |= PCI_COMMAND_MEM_ENABLE;
902fb624f2Skiyohara if (master)
912fb624f2Skiyohara enable |= PCI_COMMAND_MASTER_ENABLE;
922fb624f2Skiyohara
932fb624f2Skiyohara ppci = (u_char *)&PCI_slots[slot].config_addr[CMD];
942fb624f2Skiyohara *ppci = enable;
95*cde8f271Sriastradh __asm volatile("eieio" ::: "memory");
962fb624f2Skiyohara }
972fb624f2Skiyohara
982fb624f2Skiyohara void
scanPCI(void)992fb624f2Skiyohara scanPCI(void)
1002fb624f2Skiyohara {
1012fb624f2Skiyohara struct PCI_ConfigInfo *pslot;
1022fb624f2Skiyohara int slt, r;
1032fb624f2Skiyohara
1042fb624f2Skiyohara for (slt = 0; slt < NSLOTS; slt++) {
1052fb624f2Skiyohara pslot = &PCI_slots[slt];
1062fb624f2Skiyohara for (r = 0; r < NPCIREGS; r++)
1072fb624f2Skiyohara pslot->regs[r] = bswap32(pslot->config_addr[r]);
1082fb624f2Skiyohara }
1092fb624f2Skiyohara }
1102fb624f2Skiyohara
1112fb624f2Skiyohara int
findPCIVga(void)1122fb624f2Skiyohara findPCIVga(void)
1132fb624f2Skiyohara {
1142fb624f2Skiyohara struct PCI_ConfigInfo *pslot;
1152fb624f2Skiyohara int theSlot = -1;
1162fb624f2Skiyohara int highVgaSlot = -1;
1172fb624f2Skiyohara int slt;
1182fb624f2Skiyohara
1192fb624f2Skiyohara for (slt = 0; slt < NSLOTS; slt++) {
1202fb624f2Skiyohara pslot = &PCI_slots[slt];
1212fb624f2Skiyohara if (pslot->regs[DEVID] != 0xffffffff) { /* card in slot ? */
1222fb624f2Skiyohara if (PCI_CLASS(pslot->regs[CLASS]) ==
1232fb624f2Skiyohara PCI_CLASS_DISPLAY) {
1242fb624f2Skiyohara highVgaSlot = slt;
1252fb624f2Skiyohara if ((pslot->regs[CMD] & 0x03)) { /* did firmware enable it ? */
1262fb624f2Skiyohara theSlot = slt;
1272fb624f2Skiyohara }
1282fb624f2Skiyohara }
1292fb624f2Skiyohara }
1302fb624f2Skiyohara }
1312fb624f2Skiyohara if (theSlot == -1)
1322fb624f2Skiyohara theSlot = highVgaSlot;
1332fb624f2Skiyohara
1342fb624f2Skiyohara return theSlot;
1352fb624f2Skiyohara }
1362fb624f2Skiyohara
1372fb624f2Skiyohara int
PCISlotnum(u_int bus,u_int dev,u_int func)1382fb624f2Skiyohara PCISlotnum(u_int bus, u_int dev, u_int func)
1392fb624f2Skiyohara {
1402fb624f2Skiyohara u_long *tag;
1412fb624f2Skiyohara int i;
1422fb624f2Skiyohara
1432fb624f2Skiyohara if (bus != 0 ||
1442fb624f2Skiyohara dev < 11 || dev > 15 ||
1452fb624f2Skiyohara func > 7)
1462fb624f2Skiyohara return -1;
1472fb624f2Skiyohara
1482fb624f2Skiyohara tag = PCI_CONFIG_SPACE(dev, func);
1492fb624f2Skiyohara for (i = 0; i < sizeof(PCI_slots) / sizeof(struct PCI_ConfigInfo); i++)
1502fb624f2Skiyohara if (tag == PCI_slots[i].config_addr)
1512fb624f2Skiyohara return i;
1522fb624f2Skiyohara return -1;
1532fb624f2Skiyohara }
1542fb624f2Skiyohara
1552fb624f2Skiyohara /* return Vendor ID of card in the slot */
1562fb624f2Skiyohara int
PCIVendor(int slotnum)1572fb624f2Skiyohara PCIVendor(int slotnum)
1582fb624f2Skiyohara {
1592fb624f2Skiyohara struct PCI_ConfigInfo *pslot;
1602fb624f2Skiyohara
1612fb624f2Skiyohara pslot = &PCI_slots[slotnum];
1622fb624f2Skiyohara
1632fb624f2Skiyohara return pslot->regs[DEVID] & 0xffff;
1642fb624f2Skiyohara }
1652fb624f2Skiyohara
1662fb624f2Skiyohara /* return mapped address for I/O or Memory */
1672fb624f2Skiyohara u_long
PCIAddress(int slotnum,u_int bar,int type)1682fb624f2Skiyohara PCIAddress(int slotnum, u_int bar, int type)
1692fb624f2Skiyohara {
1702fb624f2Skiyohara struct PCI_ConfigInfo *pslot;
1712fb624f2Skiyohara
1722fb624f2Skiyohara if (bar >= 6)
1732fb624f2Skiyohara return 0xffffffff;
1742fb624f2Skiyohara
1752fb624f2Skiyohara pslot = &PCI_slots[slotnum];
1762fb624f2Skiyohara
1772fb624f2Skiyohara if (pslot->regs[DEVID] == 0xffffffff ||
1782fb624f2Skiyohara PCI_MAPREG_TYPE(pslot->regs[BAR_BASE + bar]) != type)
1792fb624f2Skiyohara return 0xffffffff;
1802fb624f2Skiyohara
1812fb624f2Skiyohara return PCI_MAPREG_MEM_ADDR(pslot->regs[BAR_BASE + bar]);
1822fb624f2Skiyohara }
1832fb624f2Skiyohara
1842fb624f2Skiyohara #ifdef DEBUG
1852fb624f2Skiyohara void
printPCIslots(void)1862fb624f2Skiyohara printPCIslots(void)
1872fb624f2Skiyohara {
1882fb624f2Skiyohara int i;
1892fb624f2Skiyohara for (i = 0; i < NSLOTS; i++) {
1902fb624f2Skiyohara printf("PCI Slot number: %d", i);
1912fb624f2Skiyohara printf(" Vendor ID: 0x%x\n", PCIVendor(i));
1922fb624f2Skiyohara }
1932fb624f2Skiyohara }
1942fb624f2Skiyohara #endif /* DEBUG */
195