f3cfa418aa56642e305df56073b3cff06294cdf1
[coreboot.git] / src / northbridge / via / vx800 / northbridge.c
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; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  */
19
20 /*
21     part of this file is from cx700 port, part of is from cn700 port,
22    */
23
24 #include <console/console.h>
25 #include <arch/io.h>
26 #include <stdint.h>
27 #include <device/device.h>
28 #include <device/pci.h>
29 #include <device/pci_ids.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <bitops.h>
33 #include <cpu/cpu.h>
34 #include "chip.h"
35 #include "northbridge.h"
36 #include "vx800.h"
37
38 /* !!FIXME!!  This was meant to be a CONFIG option */
39 #define VIACONFIG_TOP_SM_SIZE_MB 32     // Set frame buffer 32M for default
40 /* !!FIXME!!  I declared this to fix the build. */
41 u8 acpi_sleep_type = 0;
42
43 static void memctrl_init(device_t dev)
44 {
45 /*
46   set VGA in uma_ram_setting.c, not in this function.
47 */
48 #if 0
49         pci_write_config8(dev, 0x85, 0x20);
50         pci_write_config8(dev, 0x86, 0x2d);
51
52         /* Set up VGA timers */
53         pci_write_config8(dev, 0xa2, 0x44);
54
55         /* Enable VGA with a 32mb framebuffer */
56         pci_write_config16(dev, 0xa0, 0xd000);
57
58         pci_write_config16(dev, 0xa4, 0x0010);
59
60         //b0: 60 aa aa 5a 0f 00 00 00 08
61         pci_write_config16(dev, 0xb0, 0xaa00);
62         pci_write_config8(dev, 0xb8, 0x08);
63 #endif
64 }
65
66 static const struct device_operations memctrl_operations = {
67         .read_resources = vx800_noop,
68         .init = memctrl_init,
69 };
70
71 static const struct pci_driver memctrl_driver __pci_driver = {
72         .ops = &memctrl_operations,
73         .vendor = PCI_VENDOR_ID_VIA,
74         .device = PCI_DEVICE_ID_VIA_VX855_MEMCTRL,
75 };
76
77 static void pci_domain_set_resources(device_t dev)
78 {
79         /*
80          * the order is important to find the correct ram size.
81          */
82         u8 ramregs[] = { 0x43, 0x42, 0x41, 0x40 };
83         device_t mc_dev;
84         u32 pci_tolm;
85         u8 reg;
86
87         printk(BIOS_SPEW, "Entering vx800 pci_domain_set_resources.\n");
88
89         pci_tolm = find_pci_tolm(dev->link_list);
90         mc_dev = dev_find_device(PCI_VENDOR_ID_VIA,
91                                  PCI_DEVICE_ID_VIA_VX855_MEMCTRL, 0);
92
93         if (mc_dev) {
94                 unsigned long tomk, tolmk;
95                 unsigned char rambits;
96                 u8 i, idx;
97
98                 /*
99                  * once the register value is not zero, the ramsize is
100                  * this register's value multiply 64 * 1024 * 1024
101                  */
102                 for (rambits = 0, i = 0; i < ARRAY_SIZE(ramregs); i++) {
103                         rambits = pci_read_config8(mc_dev, ramregs[i]);
104                         if (rambits != 0)
105                                 break;
106                 }
107 /*
108 Get memory size and frame buffer from northbridge's registers.
109 if register with invalid value we set frame buffer size to 32M for default, but it won't happen.
110 */
111                 reg = pci_read_config8(mc_dev, 0xa1);
112                 reg &= 0x70;
113                 reg = reg >> 4;
114                 /* TOP 1M SM Memory */
115                 if (reg == 0x0)
116                         tomk = (((rambits << 6) - 32 - VIACONFIG_TOP_SM_SIZE_MB) * 1024);       // Set frame buffer 32M for default
117                 else
118                         tomk =
119                             (((rambits << 6) - (4 << reg) -
120                               VIACONFIG_TOP_SM_SIZE_MB) * 1024);
121
122                 printk(BIOS_SPEW, "tomk is 0x%lx\n", tomk);
123                 /* Compute the Top Of Low Memory, in Kb */
124                 tolmk = pci_tolm >> 10;
125                 if (tolmk >= tomk) {
126                         /* The PCI hole does does not overlap the memory. */
127                         tolmk = tomk;
128                 }
129                 /* Report the memory regions */
130                 idx = 10;
131                 /* TODO: Hole needed? */
132                 ram_resource(dev, idx++, 0, 640);       /* first 640k */
133                 /* Leave a hole for vga, 0xa0000 - 0xc0000 */
134                 ram_resource(dev, idx++, 768, (tolmk - 768));
135         }
136         assign_resources(dev->link_list);
137 }
138
139 static struct device_operations pci_domain_ops = {
140         .read_resources = pci_domain_read_resources,
141         .set_resources = pci_domain_set_resources,
142         .enable_resources = NULL,
143         .init = NULL,
144         .scan_bus = pci_domain_scan_bus,
145 };
146
147 static void cpu_bus_init(device_t dev)
148 {
149         initialize_cpus(dev->link_list);
150 }
151
152 static void cpu_bus_noop(device_t dev)
153 {
154 }
155
156 static struct device_operations cpu_bus_ops = {
157         .read_resources = cpu_bus_noop,
158         .set_resources = cpu_bus_noop,
159         .enable_resources = cpu_bus_noop,
160         .init = cpu_bus_init,
161         .scan_bus = 0,
162 };
163
164 static void enable_dev(struct device *dev)
165 {
166         printk(BIOS_SPEW, "In VX800 enable_dev for device %s.\n", dev_path(dev));
167
168         /* Set the operations if it is a special bus type */
169         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
170                 dev->ops = &pci_domain_ops;
171                 pci_set_method(dev);
172         } else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
173                 dev->ops = &cpu_bus_ops;
174         }
175 }
176
177 struct chip_operations northbridge_via_vx800_ops = {
178         CHIP_NAME("VIA VX800 Chipset")
179             .enable_dev = enable_dev,
180 };