cfeb8394fd48497486d518a6fddf01dfe5b985fc
[coreboot.git] / src / northbridge / via / vx800 / pci_rawops.h
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2009 One Laptop per Child, Association, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20 #ifndef ARCH_I386_PCI_RAWOPS_H
21 # define ARCH_I386_PCI_RAWOPS_H 1
22 #include <stdint.h>
23
24 #define PCI_RAWDEV(SEGBUS, DEV, FN) ( \
25         (((SEGBUS) & 0xFFF) << 20) | \
26         (((DEV) & 0x1F) << 15) | \
27         (((FN)  & 0x07) << 12))
28 struct  VIA_PCI_REG_INIT_TABLE {
29         u8 ChipRevisionStart;
30         u8 ChipRevisionEnd;
31         u8 Bus;
32         u8 Device;
33         u8 Function;
34         u32 Register;
35         u8 Mask;
36         u8 Value;
37 };
38 typedef unsigned device_t_raw; /* pci and pci_mmio need to have different ways to have dev */
39
40 /* FIXME: We need to make the coreboot to run at 64bit mode, So when read/write memory above 4G,
41  * We don't need to set %fs, and %gs anymore
42  * Before that We need to use %gs, and leave %fs to other RAM access
43  */
44  uint8_t pci_io_rawread_config8(device_t_raw dev, unsigned where)
45 {
46         unsigned addr;
47 #if PCI_IO_CFG_EXT == 0
48         addr = (dev>>4) | where;
49 #else
50         addr = (dev>>4) | (where & 0xff) | ((where & 0xf00)<<16); //seg == 0
51 #endif
52         outl(0x80000000 | (addr & ~3), 0xCF8);
53         return inb(0xCFC + (addr & 3));
54 }
55
56 #if MMCONF_SUPPORT
57  uint8_t pci_mmio_rawread_config8(device_t_raw dev, unsigned where)
58 {
59         unsigned addr;
60         addr = dev | where;
61         return read8x(addr);
62 }
63 #endif
64  uint8_t pci_rawread_config8(device_t_raw dev, unsigned where)
65 {
66 #if MMCONF_SUPPORT
67         return pci_mmio_rawread_config8(dev, where);
68 #else
69         return pci_io_rawread_config8(dev, where);
70 #endif
71 }
72
73  uint16_t pci_io_rawread_config16(device_t_raw dev, unsigned where)
74 {
75         unsigned addr;
76 #if PCI_IO_CFG_EXT == 0
77         addr = (dev>>4) | where;
78 #else
79         addr = (dev>>4) | (where & 0xff) | ((where & 0xf00)<<16);
80 #endif
81         outl(0x80000000 | (addr & ~3), 0xCF8);
82         return inw(0xCFC + (addr & 2));
83 }
84
85 #if MMCONF_SUPPORT
86  uint16_t pci_mmio_rawread_config16(device_t_raw dev, unsigned where)
87 {
88         unsigned addr;
89         addr = dev | where;
90         return read16x(addr);
91 }
92 #endif
93
94  uint16_t pci_rawread_config16(device_t_raw dev, unsigned where)
95 {
96 #if MMCONF_SUPPORT
97         return pci_mmio_rawread_config16(dev, where);
98 #else
99         return pci_io_rawread_config16(dev, where);
100 #endif
101 }
102
103
104  uint32_t pci_io_rawread_config32(device_t_raw dev, unsigned where)
105 {
106         unsigned addr;
107 #if PCI_IO_CFG_EXT == 0
108         addr = (dev>>4) | where;
109 #else
110         addr = (dev>>4) | (where & 0xff) | ((where & 0xf00)<<16);
111 #endif
112         outl(0x80000000 | (addr & ~3), 0xCF8);
113         return inl(0xCFC);
114 }
115
116 #if MMCONF_SUPPORT
117  uint32_t pci_mmio_rawread_config32(device_t_raw dev, unsigned where)
118 {
119         unsigned addr;
120         addr = dev | where;
121         return read32x(addr);
122 }
123 #endif
124
125  uint32_t pci_rawread_config32(device_t_raw dev, unsigned where)
126 {
127 #if MMCONF_SUPPORT
128         return pci_mmio_rawread_config32(dev, where);
129 #else
130         return pci_io_rawread_config32(dev, where);
131 #endif
132 }
133
134  void pci_io_rawwrite_config8(device_t_raw dev, unsigned where, uint8_t value)
135 {
136         unsigned addr;
137 #if PCI_IO_CFG_EXT == 0
138         addr = (dev>>4) | where;
139 #else
140         addr = (dev>>4) | (where & 0xff) | ((where & 0xf00)<<16);
141 #endif
142         outl(0x80000000 | (addr & ~3), 0xCF8);
143         outb(value, 0xCFC + (addr & 3));
144 }
145
146 #if MMCONF_SUPPORT
147  void pci_mmio_rawwrite_config8(device_t_raw dev, unsigned where, uint8_t value)
148 {
149         unsigned addr;
150         addr = dev | where;
151         write8x(addr, value);
152 }
153 #endif
154
155  void pci_rawwrite_config8(device_t_raw dev, unsigned where, uint8_t value)
156 {
157 #if MMCONF_SUPPORT
158         pci_mmio_rawwrite_config8(dev, where, value);
159 #else
160         pci_io_rawwrite_config8(dev, where, value);
161 #endif
162 }
163
164
165  void pci_io_rawwrite_config16(device_t_raw dev, unsigned where, uint16_t value)
166 {
167         unsigned addr;
168 #if PCI_IO_CFG_EXT == 0
169         addr = (dev>>4) | where;
170 #else
171         addr = (dev>>4) | (where & 0xff) | ((where & 0xf00)<<16);
172 #endif
173         outl(0x80000000 | (addr & ~3), 0xCF8);
174         outw(value, 0xCFC + (addr & 2));
175 }
176
177 #if MMCONF_SUPPORT
178  void pci_mmio_rawwrite_config16(device_t_raw dev, unsigned where, uint16_t value)
179 {
180         unsigned addr;
181         addr = dev | where;
182         write16x(addr, value);
183 }
184 #endif
185
186  void pci_rawwrite_config16(device_t_raw dev, unsigned where, uint16_t value)
187 {
188 #if MMCONF_SUPPORT
189         pci_mmio_rawwrite_config16(dev, where, value);
190 #else
191         pci_io_rawwrite_config16(dev, where, value);
192 #endif
193 }
194
195
196  void pci_io_rawwrite_config32(device_t_raw dev, unsigned where, uint32_t value)
197 {
198         unsigned addr;
199 #if PCI_IO_CFG_EXT == 0
200         addr = (dev>>4) | where;
201 #else
202         addr = (dev>>4) | (where & 0xff) | ((where & 0xf00)<<16);
203 #endif
204         outl(0x80000000 | (addr & ~3), 0xCF8);
205         outl(value, 0xCFC);
206 }
207
208 #if MMCONF_SUPPORT
209  void pci_mmio_rawwrite_config32(device_t_raw dev, unsigned where, uint32_t value)
210 {
211         unsigned addr;
212         addr = dev | where;
213         write32x(addr, value);
214 }
215 #endif
216
217  void pci_rawwrite_config32(device_t_raw dev, unsigned where, uint32_t value)
218 {
219 #if MMCONF_SUPPORT
220         pci_mmio_rawwrite_config32(dev, where, value);
221 #else
222         pci_io_rawwrite_config32(dev, where, value);
223 #endif
224 }
225
226
227 void pci_rawmodify_config8(device_t_raw dev, unsigned where, u8 orval,u8 mask)
228 {     u8 data=pci_rawread_config8(dev,where);
229        data&=(~mask);
230        data|=orval;
231        pci_rawwrite_config8(dev,where,data);
232 }
233 void pci_rawmodify_config16(device_t_raw dev, unsigned where, uint16_t orval,uint16_t mask)
234 {     uint16_t data=pci_rawread_config16(dev,where);
235        data&=(~mask);
236        data|=orval;
237        pci_rawwrite_config16(dev,where,data);
238 }
239 void pci_rawmodify_config32(device_t_raw dev, unsigned where, uint32_t orval,uint32_t mask)
240 {     uint32_t data=pci_rawread_config32(dev,where);
241        data&=(~mask);
242        data|=orval;
243        pci_rawwrite_config32(dev,where,data);
244 }
245
246 void io_rawmodify_config8(u16 where, uint8_t orval,uint8_t mask)
247 {
248         u8 data=inb(where);
249        data&=(~mask);
250        data|=orval;
251        outb(data,where);
252 }
253
254 void via_pci_inittable(u8 chipversion,struct VIA_PCI_REG_INIT_TABLE* initdata)
255 {
256         u8 i=0;
257         device_t_raw devbxdxfx;
258         for(i=0;;i++)   {
259                         if((initdata[i].Mask==0)&&(initdata[i].Value==0)&&(initdata[i].Bus==0)&&(initdata[i].ChipRevisionEnd==0xff)&&(initdata[i].ChipRevisionStart==0)&&(initdata[i].Device==0)&&(initdata[i].Function==0)&&(initdata[i].Register==0))
260                                 break;
261                         if((chipversion>=initdata[i].ChipRevisionStart)&&(chipversion<=initdata[i].ChipRevisionEnd)){
262                                 devbxdxfx=PCI_RAWDEV(initdata[i].Bus,initdata[i].Device,initdata[i].Function);
263                                 pci_rawmodify_config8(devbxdxfx, initdata[i].Register,initdata[i].Value,initdata[i].Mask);
264                         }
265         }
266 }
267 #endif