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