Add support for the VIA EPIA-CN baord, which uses C7 + CN700 + VT8237R.
[coreboot.git] / src / northbridge / via / cn700 / northbridge.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2008 VIA Technologies, Inc.
5  * (Written by Aaron Lwe <aaron.lwe@gmail.com> for VIA)
6  * Copyright (C) 2007 Corey Osgood <corey.osgood@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21  */
22
23 #include <console/console.h>
24 #include <arch/io.h>
25 #include <stdint.h>
26 #include <device/device.h>
27 #include <device/pci.h>
28 #include <device/pci_ids.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <bitops.h>
32 #include <cpu/cpu.h>
33 #include "chip.h"
34 #include "northbridge.h"
35 #include "cn700.h"
36
37 static void memctrl_init(device_t dev)
38 {
39         device_t vlink_dev;
40         u16 reg16;
41         u8 ranks;
42         u8 pagec, paged, pagee, pagef;
43         u8 shadowreg;
44         
45         /* Set up the vga framebuffer size */
46         reg16 = (log2(CONFIG_VIDEO_MB) << 12) | (1 << 15);
47         pci_write_config16(dev, 0xa0, reg16);
48         
49         /* Set up VGA timers */
50         pci_write_config8(dev, 0xa2, 0x44);
51         
52         for (ranks = 0x4b; ranks >= 0x48; ranks--) {
53                 if (pci_read_config8(dev, ranks)) {
54                         ranks -= 0x48;
55                         break;
56                 }
57         }
58         if (ranks == 0x47)
59                 ranks = 0x00;
60         reg16 = 0xaae0;
61         reg16 |= ranks;
62         /* GMINT Misc. FrameBuffer rank */
63         pci_write_config16(dev, 0xb0, reg16);
64         /* AGPCINT Misc. */
65         pci_write_config8(dev, 0xb8, 0x08);
66
67         /* shadown ram */
68         pagec = 0xff, paged = 0xff, pagee = 0xff, pagef = 0x30;
69         /* PAGE C, D, E are all read write enable */
70         pci_write_config8(dev, 0x80, pagec);
71         pci_write_config8(dev, 0x81, paged);
72         pci_write_config8(dev, 0x82, pagee);
73         /* PAGE F are read/writable */
74         shadowreg = pci_read_config8(dev, 0x83);
75         shadowreg |= pagef;
76         pci_write_config8(dev, 0x83, shadowreg);
77         /* vlink mirror */
78         vlink_dev = dev_find_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_CN700_VLINK, 0);
79         if (vlink_dev) {
80                 pci_write_config8(vlink_dev, 0x61, pagec);
81                 pci_write_config8(vlink_dev, 0x62, paged);
82                 pci_write_config8(vlink_dev, 0x64, pagee);
83                 shadowreg = pci_read_config8(vlink_dev, 0x63);
84                 shadowreg |= pagef;
85                 pci_write_config8(vlink_dev, 0x63, shadowreg);
86         }
87 }
88
89 static const struct device_operations memctrl_operations = {
90         .read_resources = cn700_noop,
91         .init = memctrl_init,
92 };
93
94 static const struct pci_driver memctrl_driver __pci_driver = {
95         .ops = &memctrl_operations,
96         .vendor = PCI_VENDOR_ID_VIA,
97         .device = PCI_DEVICE_ID_VIA_CN700_MEMCTRL,
98 };
99
100 static void pci_domain_read_resources(device_t dev)
101 {
102         struct resource *resource;
103
104         printk_spew("Entering cn700 pci_domain_read_resources.\n");
105
106         /* Initialize the system wide io space constraints */
107         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(0,0));
108         resource->limit = 0xffffUL;
109         resource->flags = IORESOURCE_IO | IORESOURCE_SUBTRACTIVE |
110                 IORESOURCE_ASSIGNED;
111
112         /* Initialize the system wide memory resources constraints */
113         resource = new_resource(dev, IOINDEX_SUBTRACTIVE(1,0));
114         resource->limit = 0xffffffffULL;
115         resource->flags = IORESOURCE_MEM | IORESOURCE_SUBTRACTIVE |
116                 IORESOURCE_ASSIGNED;
117
118         printk_spew("Leaving cn700 pci_domain_read_resources.\n");
119 }
120
121 static void ram_resource(device_t dev, unsigned long index,
122         unsigned long basek, unsigned long sizek)
123 {
124         struct resource *resource;
125
126         if (!sizek) {
127                 return;
128         }
129         resource = new_resource(dev, index);
130         resource->base  = ((resource_t)basek) << 10;
131         resource->size  = ((resource_t)sizek) << 10;
132         resource->flags =  IORESOURCE_MEM | IORESOURCE_CACHEABLE | \
133                 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
134 }
135
136 static void tolm_test(void *gp, struct device *dev, struct resource *new)
137 {
138         struct resource **best_p = gp;
139         struct resource *best;
140         best = *best_p;
141         if (!best || (best->base > new->base)) {
142                 best = new;
143         }
144         *best_p = best;
145 }
146
147 static u32 find_pci_tolm(struct bus *bus)
148 {
149         print_debug("Entering find_pci_tolm\n");
150         struct resource *min;
151         u32 tolm;
152         min = 0;
153         search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test, &min);
154         tolm = 0xffffffffUL;
155         if (min && tolm > min->base) {
156                 tolm = min->base;
157         }
158         print_debug("Leaving find_pci_tolm\n");
159         return tolm;
160 }
161
162 static void pci_domain_set_resources(device_t dev)
163 {
164         /* 
165          * the order is important to find the correct ram size.
166          */
167         static const u8 ramregs[] = {0x43, 0x42, 0x41, 0x40};
168         device_t mc_dev;
169         u32 pci_tolm;
170
171         printk_spew("Entering cn700 pci_domain_set_resources.\n");
172
173         pci_tolm = find_pci_tolm(&dev->link[0]);
174         mc_dev = dev_find_device(PCI_VENDOR_ID_VIA, 
175                                 PCI_DEVICE_ID_VIA_CN700_MEMCTRL, 0);
176
177         if (mc_dev) {
178                 unsigned long tomk, tolmk;
179                 unsigned char rambits;
180                 int i, idx;
181
182                 /*
183                  * once the register value is not zero, the ramsize is
184                  * this register's value multiply 64 * 1024 * 1024
185                  */
186                 for(rambits = 0, i = 0; i < ARRAY_SIZE(ramregs); i++) {
187                         unsigned char reg;
188                         rambits = pci_read_config8(mc_dev, ramregs[i]);
189                         if (rambits != 0)
190                                 break;
191                 }
192                 
193                 tomk = rambits * 64 * 1024;
194                 printk_spew("tomk is 0x%x\n", tomk);
195                 /* Compute the Top Of Low Memory, in Kb */
196                 tolmk = pci_tolm >> 10;
197                 if (tolmk >= tomk) {
198                         /* The PCI hole does does not overlap the memory. */
199                         tolmk = tomk;
200                 }
201                 /* Report the memory regions */
202                 idx = 10;
203                 /* TODO: Hole needed? */
204                 ram_resource(dev, idx++, 0, 640); /* first 640k */
205                 /* Leave a hole for vga, 0xa0000 - 0xc0000 */
206                 ram_resource(dev, idx++, 768, (tolmk - 768 - CONFIG_VIDEO_MB * 1024));
207         }
208         assign_resources(&dev->link[0]);
209 }
210
211 static unsigned int pci_domain_scan_bus(device_t dev, unsigned int max)
212 {
213         printk_debug("Entering cn700 pci_domain_scan_bus.\n");
214
215         max = pci_scan_bus(&dev->link[0], PCI_DEVFN(0, 0), 0xff, max);
216         return max;
217 }
218
219 static const struct device_operations pci_domain_ops = {
220         .read_resources   = pci_domain_read_resources,
221         .set_resources    = pci_domain_set_resources,
222         .enable_resources = enable_childrens_resources,
223         .init             = 0,
224         .scan_bus         = pci_domain_scan_bus,
225 };  
226
227 static void cpu_bus_init(device_t dev)
228 {
229         initialize_cpus(&dev->link[0]);
230 }
231
232 static void cpu_bus_noop(device_t dev)
233 {
234 }
235
236 static const struct device_operations cpu_bus_ops = {
237         .read_resources   = cpu_bus_noop,
238         .set_resources    = cpu_bus_noop,
239         .enable_resources = cpu_bus_noop,
240         .init             = cpu_bus_init,
241         .scan_bus         = 0,
242 };
243
244 static void enable_dev(struct device *dev)
245 {
246         printk_spew("In cn700 enable_dev for device %s.\n", dev_path(dev));
247
248         /* Set the operations if it is a special bus type */
249         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
250                 dev->ops = &pci_domain_ops;
251                 pci_set_method(dev);
252         }
253         else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
254                 dev->ops = &cpu_bus_ops;
255         }
256 }
257
258 struct chip_operations northbridge_via_cn700_ops = {
259         CHIP_NAME("VIA CN700 Northbridge")
260         .enable_dev = enable_dev,
261 };