Factor out a few commonly duplicated functions from northbridge.c.
[coreboot.git] / src / northbridge / via / vt8623 / northbridge.c
1 #include <console/console.h>
2 #include <arch/io.h>
3 #include <stdint.h>
4 #include <device/device.h>
5 #include <device/pci.h>
6 #include <device/hypertransport.h>
7 #include <device/pci_ids.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <bitops.h>
11 #include <cpu/cpu.h>
12 #include <cpu/x86/mtrr.h>
13 #include <cpu/x86/msr.h>
14 #include "chip.h"
15 #include "northbridge.h"
16
17 /*
18  * This fixup is based on capturing values from an Award BIOS.  Without
19  * this fixup the DMA write performance is awful (i.e. hdparm -t /dev/hda is 20x
20  * slower than normal, ethernet drops packets).
21  * Apparently these registers govern some sort of bus master behavior.
22  */
23
24 static void northbridge_init(device_t dev)
25 {
26         device_t fb_dev;
27         unsigned long fb;
28         unsigned char c;
29
30         printk(BIOS_DEBUG, "VT8623 random fixup ...\n");
31         pci_write_config8(dev,  0x0d, 0x08);
32         pci_write_config8(dev,  0x70, 0x82);
33         pci_write_config8(dev,  0x71, 0xc8);
34         pci_write_config8(dev,  0x72, 0x00);
35         pci_write_config8(dev,  0x73, 0x01);
36         pci_write_config8(dev,  0x74, 0x01);
37         pci_write_config8(dev,  0x75, 0x08);
38         pci_write_config8(dev,  0x76, 0x52);
39         pci_write_config8(dev,  0x13, 0xd0);
40         pci_write_config8(dev,  0x84, 0x80);
41         pci_write_config16(dev, 0x80, 0x610f);
42         pci_write_config32(dev, 0x88, 0x00000002);
43
44         fb_dev = dev_find_device(PCI_VENDOR_ID_VIA, 0x3122, 0);
45         if (fb_dev) {
46                 /* Fixup GART and framebuffer addresses properly.
47                  * First setup frame buffer properly.
48                  */
49                 //fb = pci_read_config32(dev, 0x10);       /* Base addres of framebuffer */
50                 fb = 0xd0000000;
51                 printk(BIOS_DEBUG, "Frame buffer at %8lx\n",fb);
52
53                 c = pci_read_config8(dev, 0xe1) & 0xf0;  /* size of vga */
54                 c |= fb>>28;  /* upper nibble of frame buffer address */
55                 c = 0xdd;
56                 pci_write_config8(dev, 0xe1, c);
57                 c = 0x81;                                /* enable framebuffer */
58                 pci_write_config8(dev, 0xe0, c);
59                 pci_write_config8(dev, 0xe2, 0x42);      /* 'cos award does */
60         }
61 }
62
63 static void nullfunc(device_t dev)
64 {
65         /* Nothing to do */
66 }
67
68 static struct device_operations northbridge_operations = {
69         .read_resources   = nullfunc,
70         .set_resources    = pci_dev_set_resources,
71         .enable_resources = pci_dev_enable_resources,
72         .init             = northbridge_init
73 };
74
75 static const struct pci_driver northbridge_driver __pci_driver = {
76         .ops = &northbridge_operations,
77         .vendor = PCI_VENDOR_ID_VIA,
78         .device = PCI_DEVICE_ID_VIA_8623,
79 };
80
81 static void agp_init(device_t dev)
82 {
83         printk(BIOS_DEBUG, "VT8623 AGP random fixup ...\n");
84
85         pci_write_config8(dev, 0x3e, 0x0c);
86         pci_write_config8(dev, 0x40, 0x83);
87         pci_write_config8(dev, 0x41, 0xc5);
88         pci_write_config8(dev, 0x43, 0x44);
89         pci_write_config8(dev, 0x44, 0x34);
90         pci_write_config8(dev, 0x83, 0x02);
91 }
92
93 static struct device_operations agp_operations = {
94         .read_resources   = nullfunc,
95         .set_resources    = pci_dev_set_resources,
96         .enable_resources = pci_bus_enable_resources,
97         .init             = agp_init,
98         .scan_bus         = pci_scan_bridge,
99         .ops_pci          = 0,
100 };
101
102 static const struct pci_driver agp_driver __pci_driver = {
103         .ops = &agp_operations,
104         .vendor = PCI_VENDOR_ID_VIA,
105         .device = PCI_DEVICE_ID_VIA_8633_1,
106 };
107
108 #if CONFIG_WRITE_HIGH_TABLES==1
109 /* maximum size of high tables in KB */
110 #define HIGH_TABLES_SIZE 64
111 extern uint64_t high_tables_base, high_tables_size;
112 #endif
113
114 static void pci_domain_set_resources(device_t dev)
115 {
116         static const uint8_t ramregs[] = {0x5a, 0x5b, 0x5c, 0x5d };
117         device_t mc_dev;
118         uint32_t pci_tolm;
119
120         printk(BIOS_SPEW, "Entering vt8623 pci_domain_set_resources.\n");
121
122         pci_tolm = find_pci_tolm(dev->link_list);
123         mc_dev = dev->link_list->children;
124         if (mc_dev) {
125                 unsigned long tomk, tolmk;
126                 unsigned char rambits;
127                 int i, idx;
128
129                 for(rambits = 0, i = 0; i < ARRAY_SIZE(ramregs); i++) {
130                         unsigned char reg;
131                         reg = pci_read_config8(mc_dev, ramregs[i]);
132                         /* these are ENDING addresses, not sizes.
133                          * if there is memory in this slot, then reg will be > rambits.
134                          * So we just take the max, that gives us total.
135                          * We take the highest one to cover for once and future coreboot
136                          * bugs. We warn about bugs.
137                          */
138                         if (reg > rambits)
139                                 rambits = reg;
140                         if (reg < rambits)
141                                 printk(BIOS_ERR, "ERROR! register 0x%x is not set!\n",
142                                         ramregs[i]);
143                 }
144                 printk(BIOS_DEBUG, "I would set ram size to 0x%x Kbytes\n", (rambits)*16*1024);
145                 tomk = rambits*16*1024 - 32768;
146                 /* Compute the top of Low memory */
147                 tolmk = pci_tolm >> 10;
148                 if (tolmk >= tomk) {
149                         /* The PCI hole does does not overlap the memory.
150                          */
151                         tolmk = tomk;
152                 }
153
154 #if CONFIG_WRITE_HIGH_TABLES == 1
155                 high_tables_base = (tolmk - HIGH_TABLES_SIZE) * 1024;
156                 high_tables_size = HIGH_TABLES_SIZE* 1024;
157                 printk(BIOS_DEBUG, "tom: %lx, high_tables_base: %llx, high_tables_size: %llx\n", tomk*1024, high_tables_base, high_tables_size);
158 #endif
159
160                 /* Report the memory regions */
161                 idx = 10;
162                 ram_resource(dev, idx++, 0, 640);               /* first 640k */
163                 ram_resource(dev, idx++, 768, tolmk - 768);     /* leave a hole for vga */
164         }
165         assign_resources(dev->link_list);
166 }
167
168 static struct device_operations pci_domain_ops = {
169         .read_resources   = pci_domain_read_resources,
170         .set_resources    = pci_domain_set_resources,
171         .enable_resources = NULL,
172         .init             = NULL,
173         .scan_bus         = pci_domain_scan_bus,
174 };
175
176 static void cpu_bus_init(device_t dev)
177 {
178         initialize_cpus(dev->link_list);
179 }
180
181 static void cpu_bus_noop(device_t dev)
182 {
183 }
184
185 static struct device_operations cpu_bus_ops = {
186         .read_resources   = cpu_bus_noop,
187         .set_resources    = cpu_bus_noop,
188         .enable_resources = cpu_bus_noop,
189         .init             = cpu_bus_init,
190         .scan_bus         = 0,
191 };
192
193 static void enable_dev(struct device *dev)
194 {
195         printk(BIOS_SPEW, "In vt8623 enable_dev for device %s.\n", dev_path(dev));
196
197         /* Set the operations if it is a special bus type */
198         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
199                 dev->ops = &pci_domain_ops;
200                 pci_set_method(dev);
201         }
202         else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
203                 dev->ops = &cpu_bus_ops;
204         }
205 }
206
207 struct chip_operations northbridge_via_vt8623_ops = {
208         CHIP_NAME("VIA VT8623 Northbridge")
209         .enable_dev = enable_dev,
210 };