Home | History | Annotate | Line # | Download | only in boot
pci.c revision 1.3
      1  1.3  riastrad /*	$NetBSD: pci.c,v 1.3 2022/02/16 23:49:27 riastradh Exp $	*/
      2  1.1  kiyohara 
      3  1.1  kiyohara /*
      4  1.1  kiyohara  * Copyright (C) 1995-1997 Gary Thomas (gdt (at) linuxppc.org)
      5  1.1  kiyohara  * All rights reserved.
      6  1.1  kiyohara  *
      7  1.1  kiyohara  * Adapted from a program by:
      8  1.1  kiyohara  *                                      Steve Sellgren
      9  1.1  kiyohara  *                                      San Francisco Indigo Company
     10  1.1  kiyohara  *                                      sfindigo!sellgren (at) uunet.uu.net
     11  1.1  kiyohara  * Adapted for Moto boxes by:
     12  1.1  kiyohara  *                                      Pat Kane & Mark Scott, 1996
     13  1.1  kiyohara  * Fixed for IBM/PowerStack II          Pat Kane 1997
     14  1.1  kiyohara  *
     15  1.1  kiyohara  * Redistribution and use in source and binary forms, with or without
     16  1.1  kiyohara  * modification, are permitted provided that the following conditions
     17  1.1  kiyohara  * are met:
     18  1.1  kiyohara  * 1. Redistributions of source code must retain the above copyright
     19  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer.
     20  1.1  kiyohara  * 2. Redistributions in binary form must reproduce the above copyright
     21  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer in the
     22  1.1  kiyohara  *    documentation and/or other materials provided with the distribution.
     23  1.1  kiyohara  * 3. All advertising materials mentioning features or use of this software
     24  1.1  kiyohara  *    must display the following acknowledgement:
     25  1.1  kiyohara  *      This product includes software developed by Gary Thomas.
     26  1.1  kiyohara  * 4. The name of the author may not be used to endorse or promote products
     27  1.1  kiyohara  *    derived from this software without specific prior written permission.
     28  1.1  kiyohara  *
     29  1.1  kiyohara  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     30  1.1  kiyohara  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     31  1.1  kiyohara  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     32  1.1  kiyohara  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     33  1.1  kiyohara  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     34  1.1  kiyohara  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     35  1.1  kiyohara  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     36  1.1  kiyohara  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     37  1.1  kiyohara  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     38  1.1  kiyohara  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     39  1.1  kiyohara  */
     40  1.1  kiyohara 
     41  1.1  kiyohara #include <lib/libsa/stand.h>
     42  1.1  kiyohara #include <sys/bswap.h>
     43  1.1  kiyohara #include <dev/pci/pcireg.h>
     44  1.1  kiyohara 
     45  1.1  kiyohara #include "boot.h"
     46  1.1  kiyohara 
     47  1.1  kiyohara #define PCI_NSLOTS	8
     48  1.1  kiyohara #define PCI_NREGS	10
     49  1.1  kiyohara 
     50  1.1  kiyohara /*
     51  1.1  kiyohara  * should use devfunc number/indirect method to be totally safe on
     52  1.1  kiyohara  * all machines, this works for now on 3 slot Moto boxes
     53  1.1  kiyohara  */
     54  1.1  kiyohara 
     55  1.1  kiyohara #define PCI_CONFIG_SPACE_BASE	0x80800000
     56  1.1  kiyohara #define PCI_CONFIG_SPACE(d, f)	\
     57  1.1  kiyohara 		(u_long *)(PCI_CONFIG_SPACE_BASE | (1 << (d)) | ((f) << 8))
     58  1.1  kiyohara 
     59  1.1  kiyohara #define DEVID		(PCI_ID_REG >> 2)
     60  1.1  kiyohara #define CMD		(PCI_COMMAND_STATUS_REG >> 2)
     61  1.1  kiyohara #define CLASS		(PCI_CLASS_REG >> 2)
     62  1.1  kiyohara #define BAR_BASE	(PCI_MAPREG_START >> 2)
     63  1.1  kiyohara 
     64  1.1  kiyohara struct PCI_cinfo {
     65  1.1  kiyohara 	u_long *config_addr;
     66  1.1  kiyohara 	u_long regs[PCI_NREGS];
     67  1.1  kiyohara } PCI_slots[PCI_NSLOTS] = {
     68  1.1  kiyohara 	{ (u_long *)0x80808000, {0xDEADBEEF,} },
     69  1.1  kiyohara 	{ (u_long *)0x80800800, {0xDEADBEEF,} },
     70  1.1  kiyohara 	{ (u_long *)0x80801000, {0xDEADBEEF,} },
     71  1.1  kiyohara 	{ (u_long *)0x80802000, {0xDEADBEEF,} },
     72  1.1  kiyohara 	{ (u_long *)0x80804000, {0xDEADBEEF,} },
     73  1.1  kiyohara 	{ (u_long *)0x80810000, {0xDEADBEEF,} },
     74  1.1  kiyohara 	{ (u_long *)0x80820000, {0xDEADBEEF,} },
     75  1.1  kiyohara 	{ (u_long *)0x80840000, {0xDEADBEEF,} },
     76  1.1  kiyohara };
     77  1.1  kiyohara 
     78  1.1  kiyohara /*
     79  1.1  kiyohara  * The following code modifies the PCI Command register
     80  1.1  kiyohara  * to enable memory and I/O accesses.
     81  1.1  kiyohara  */
     82  1.1  kiyohara void
     83  1.1  kiyohara enablePCI(int slot, int io, int mem, int master)
     84  1.1  kiyohara {
     85  1.1  kiyohara 	volatile u_char *ppci;
     86  1.1  kiyohara 	u_char enable = 0;
     87  1.1  kiyohara 
     88  1.1  kiyohara 	if (io)
     89  1.1  kiyohara 		enable |= PCI_COMMAND_IO_ENABLE;
     90  1.1  kiyohara 	if (mem)
     91  1.1  kiyohara 		enable |= PCI_COMMAND_MEM_ENABLE;
     92  1.1  kiyohara 	if (master)
     93  1.1  kiyohara 		enable |= PCI_COMMAND_MASTER_ENABLE;
     94  1.1  kiyohara 
     95  1.1  kiyohara 	ppci = (u_char *)&PCI_slots[slot].config_addr[CMD];
     96  1.1  kiyohara 	*ppci = enable;
     97  1.3  riastrad 	__asm volatile("eieio" ::: "memory");
     98  1.1  kiyohara }
     99  1.1  kiyohara 
    100  1.1  kiyohara int
    101  1.1  kiyohara PCISlotnum(u_int bus, u_int dev, u_int func)
    102  1.1  kiyohara {
    103  1.1  kiyohara 	u_long *tag;
    104  1.1  kiyohara 	int i;
    105  1.1  kiyohara 
    106  1.1  kiyohara 	if (bus != 0 ||
    107  1.1  kiyohara 	    dev < 8 || dev > 18 ||
    108  1.1  kiyohara 	    func > 7)
    109  1.1  kiyohara 		return -1;
    110  1.1  kiyohara 
    111  1.1  kiyohara 	tag = PCI_CONFIG_SPACE(dev, func);
    112  1.1  kiyohara 	for (i = 0; i < sizeof(PCI_slots) / sizeof(struct PCI_cinfo); i++)
    113  1.1  kiyohara 		if (tag == PCI_slots[i].config_addr)
    114  1.1  kiyohara 			return i;
    115  1.1  kiyohara 	return -1;
    116  1.1  kiyohara }
    117  1.1  kiyohara 
    118  1.1  kiyohara /* return mapped address for I/O or Memory */
    119  1.1  kiyohara u_long
    120  1.1  kiyohara PCIAddress(int slotnum, u_int bar, int type)
    121  1.1  kiyohara {
    122  1.1  kiyohara 	struct PCI_cinfo *pslot;
    123  1.1  kiyohara 
    124  1.1  kiyohara 	if (bar >= 6)
    125  1.1  kiyohara 		return 0xffffffff;
    126  1.1  kiyohara 
    127  1.1  kiyohara 	pslot = &PCI_slots[slotnum];
    128  1.1  kiyohara 
    129  1.1  kiyohara 	if (pslot->regs[DEVID] == 0xffffffff ||
    130  1.1  kiyohara 	    PCI_MAPREG_TYPE(pslot->regs[BAR_BASE + bar]) != type)
    131  1.1  kiyohara 		return 0xffffffff;
    132  1.1  kiyohara 
    133  1.1  kiyohara 	return PCI_MAPREG_MEM_ADDR(pslot->regs[BAR_BASE + bar]);
    134  1.1  kiyohara }
    135  1.1  kiyohara 
    136  1.1  kiyohara void
    137  1.1  kiyohara unlockVideo(int slot)
    138  1.1  kiyohara {
    139  1.1  kiyohara 	volatile u_int8_t *ppci;
    140  1.1  kiyohara 
    141  1.1  kiyohara 	ppci = (u_int8_t *)PCI_slots[slot].config_addr;
    142  1.1  kiyohara 	ppci[4] = 0x0003;	/* enable memory and IO Access */
    143  1.1  kiyohara #if 0
    144  1.1  kiyohara 	ppci[0x10] = 0x00000;	/* Turn off memory mapping */
    145  1.1  kiyohara 	ppci[0x11] = 0x00000;	/* mem base = 0 */
    146  1.1  kiyohara 	ppci[0x12] = 0x00000;
    147  1.1  kiyohara 	ppci[0x13] = 0x00000;
    148  1.1  kiyohara #endif
    149  1.3  riastrad 	__asm__ volatile("eieio" ::: "memory");
    150  1.1  kiyohara 
    151  1.1  kiyohara 	outb(0x3d4, 0x11);
    152  1.1  kiyohara 	outb(0x3d5, 0x0e);   /* unlock CR0-CR7 */
    153  1.1  kiyohara }
    154  1.1  kiyohara 
    155  1.1  kiyohara int
    156  1.1  kiyohara scan_PCI(int start)
    157  1.1  kiyohara {
    158  1.1  kiyohara 	int slot, r;
    159  1.1  kiyohara 	struct PCI_cinfo *pslot;
    160  1.1  kiyohara 	int VGAslot = -1;
    161  1.1  kiyohara 
    162  1.1  kiyohara 	for (slot = start + 1; slot < PCI_NSLOTS; slot++) {
    163  1.1  kiyohara 		pslot = &PCI_slots[slot];
    164  1.1  kiyohara 		for (r = 0; r < PCI_NREGS; r++)
    165  1.1  kiyohara 			pslot->regs[r] = bswap32(pslot->config_addr[r]);
    166  1.1  kiyohara 		if (pslot->regs[DEVID] != 0xffffffff) {
    167  1.1  kiyohara 			/* we have a card */
    168  1.1  kiyohara 			if (((pslot->regs[CLASS] & 0xffffff00) ==
    169  1.1  kiyohara 				0x03000000) ||
    170  1.1  kiyohara 			    ((pslot->regs[CLASS] & 0xffffff00) ==
    171  1.1  kiyohara 				0x00010000)) {
    172  1.1  kiyohara 				/* it's a VGA card */
    173  1.1  kiyohara 				if ((pslot->regs[CMD] & 0x03)) {
    174  1.1  kiyohara 					/* fW enabled it */
    175  1.1  kiyohara 					VGAslot = slot;
    176  1.1  kiyohara 					break;
    177  1.1  kiyohara 				}
    178  1.1  kiyohara 			}
    179  1.1  kiyohara 		}
    180  1.1  kiyohara 	}
    181  1.1  kiyohara 	return VGAslot;
    182  1.1  kiyohara }
    183  1.1  kiyohara 
    184  1.1  kiyohara int
    185  1.1  kiyohara PCI_vendor(int slotnum)
    186  1.1  kiyohara {
    187  1.1  kiyohara 	struct PCI_cinfo *pslot = &PCI_slots[slotnum];
    188  1.1  kiyohara 
    189  1.1  kiyohara 	return (pslot->regs[DEVID] & 0xffff);
    190  1.1  kiyohara }
    191