8b21545d4899e8a734fbd32fdb86ae2b6249806a
[coreboot.git] / src / cpu / amd / sc520 / sc520.c
1 /*
2  * This file needs a major cleanup. Too much #if 0 code
3  */
4
5 #include <console/console.h>
6 #include <arch/io.h>
7 #include <stdint.h>
8 #include <device/device.h>
9 #include <device/pci.h>
10 #include <device/pci_ids.h>
11 #include <device/hypertransport.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <bitops.h>
15 #include "chip.h"
16
17
18 /* hack for now */
19 void sc520_udelay(int microseconds) {
20         volatile int x;
21         for(x = 0; x < 1000; x++) 
22                 ;  
23 }
24
25 /* looks like we define this now */
26 void
27 udelay(int microseconds) {
28         sc520_udelay(microseconds); 
29 }
30 /*
31  * set up basic things ... 
32  * PAR should NOT go here, as it might change with the mainboard. 
33  */
34 static void cpu_init(device_t dev) 
35 {
36   unsigned long *l = (unsigned long *) 0xfffef088;
37   int i;
38   for(i = 0; i < 16; i++, l++)
39     printk_err("Par%d: 0x%lx\n", i, *l);
40
41   printk_spew("SC520 random fixup ...\n");
42 }
43
44
45 /* Ollie says: make a northbridge/amd/sc520. Ron sez: 
46  * there is no real northbridge, keep it here in cpu. 
47  * Ron wins, he's writing the code. 
48  */
49 void sc520_enable_resources(struct device *dev) {
50         unsigned char command;
51
52         printk_spew("%s\n", __func__);
53         command = pci_read_config8(dev, PCI_COMMAND);
54         printk_spew("========>%s, command 0x%x\n", __func__, command);
55         command |= PCI_COMMAND_MEMORY | PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
56         printk_spew("========>%s, command 0x%x\n", __func__, command);
57         pci_write_config8(dev, PCI_COMMAND, command);
58         command = pci_read_config8(dev, PCI_COMMAND);
59         printk_spew("========>%s, command 0x%x\n", __func__, command);
60 /*
61  */
62
63 }
64
65 static void sc520_read_resources(device_t dev)
66 {
67         struct resource* res;
68
69         pci_dev_read_resources(dev);
70
71         res = new_resource(dev, 1);
72         res->base = 0x0UL;
73         res->size = 0x400UL;
74         res->limit = 0xffffUL;
75         res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
76
77         res = new_resource(dev, 3); /* IOAPIC */
78         res->base = 0xfec00000;
79         res->size = 0x00001000;
80         res->flags = IORESOURCE_MEM | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
81 }
82
83
84 static struct device_operations cpu_operations = {
85         .read_resources   = sc520_read_resources,
86         .set_resources    = pci_dev_set_resources,
87         .enable_resources = sc520_enable_resources,
88         .init             = cpu_init,
89         .enable           = 0,
90         .ops_pci          = 0,
91 };
92
93 static const struct pci_driver cpu_driver __pci_driver = {
94         .ops = &cpu_operations,
95         .vendor = PCI_VENDOR_ID_AMD,
96         .device = 0x3000
97 };
98
99 static void ram_resource(device_t dev, unsigned long index,
100         unsigned long basek, unsigned long sizek)
101 {
102         struct resource *resource;
103   printk_spew("%s sizek 0x%x\n", __func__, sizek);
104         if (!sizek) {
105                 return;
106         }
107         resource = new_resource(dev, index);
108         resource->base  = ((resource_t)basek) << 10;
109         resource->size  = ((resource_t)sizek) << 10;
110         resource->flags =  IORESOURCE_MEM | IORESOURCE_CACHEABLE | \
111                 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
112 }
113
114 static void tolm_test(void *gp, struct device *dev, struct resource *new)
115 {
116         struct resource **best_p = gp;
117         struct resource *best;
118         best = *best_p;
119         if (!best || (best->base > new->base)) {
120                 best = new;
121         }
122         *best_p = best;
123 }
124
125 static uint32_t find_pci_tolm(struct bus *bus)
126 {
127         struct resource *min;
128         uint32_t tolm;
129   printk_spew("%s\n", __func__);
130         min = 0;
131         search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, tolm_test, &min);
132         tolm = 0xffffffffUL;
133         if (min && tolm > min->base) {
134                 tolm = min->base;
135         }
136   printk_spew("%s returns 0x%x\n", __func__, tolm);
137         return tolm;
138 }
139
140 static void pci_domain_set_resources(device_t dev)
141 {
142         device_t mc_dev;
143         uint32_t pci_tolm;
144   printk_spew("%s\n", __func__);
145         pci_tolm = find_pci_tolm(&dev->link[0]);
146         mc_dev = dev->link[0].children;
147         if (mc_dev) {
148                 unsigned long tomk, tolmk;
149                 //              unsigned char rambits;
150                 // int i;
151                 int idx;
152 #if 0
153                 for(rambits = 0, i = 0; i < ARRAY_SIZE(ramregs); i++) {
154                         unsigned char reg;
155                         reg = pci_read_config8(mc_dev, ramregs[i]);
156                         /* these are ENDING addresses, not sizes. 
157                          * if there is memory in this slot, then reg will be > rambits.
158                          * So we just take the max, that gives us total. 
159                          * We take the highest one to cover for once and future coreboot
160                          * bugs. We warn about bugs.
161                          */
162                         if (reg > rambits)
163                                 rambits = reg;
164                         if (reg < rambits)
165                                 printk_err("ERROR! register 0x%x is not set!\n", 
166                                         ramregs[i]);
167                 }
168                 printk_debug("I would set ram size to 0x%x Kbytes\n", (rambits)*8*1024);
169                 tomk = rambits*8*1024;
170 #endif
171                 tomk = 32 * 1024;
172                 /* Compute the top of Low memory */
173                 tolmk = pci_tolm >> 10;
174                 if (tolmk >= tomk) {
175                         /* The PCI hole does does not overlap the memory.
176                          */
177                         tolmk = tomk;
178                 }
179                 /* Report the memory regions */
180                 idx = 10;
181                 ram_resource(dev, idx++, 0, tolmk);
182         }
183         assign_resources(&dev->link[0]);
184 }
185
186 #if 0
187 void sc520_enable_resources(device_t dev) {
188
189         printk_spew("%s\n", __func__);
190         printk_spew("THIS IS FOR THE SC520 =============================\n");
191
192 /*
193         command = pci_read_config8(dev, PCI_COMMAND);
194         printk_spew("%s, command 0x%x\n", __func__, command);
195         command |= PCI_COMMAND_MEMORY;
196         printk_spew("%s, command 0x%x\n", __func__, command);
197         pci_write_config8(dev, PCI_COMMAND, command);
198         command = pci_read_config8(dev, PCI_COMMAND);
199         printk_spew("%s, command 0x%x\n", __func__, command);
200  */
201         enable_childrens_resources(dev);
202         printk_spew("%s\n", __func__);
203 }
204 #endif
205
206 static struct device_operations pci_domain_ops = {
207         .read_resources   = pci_domain_read_resources,
208         .set_resources    = pci_domain_set_resources,
209         /*
210          * If enable_resources is set to the generic enable_resources
211          * function the whole thing will hang in an endless loop on
212          * the ts5300. If this is really needed on another platform,
213          * something is conceptually wrong.
214          */
215         .enable_resources = 0, //enable_resources,
216         .init             = 0,
217         .scan_bus         = pci_domain_scan_bus,
218 };
219
220 #if 0
221 static void cpu_bus_init(device_t dev)
222 {
223   printk_spew("cpu_bus_init\n");
224 }
225
226 static void cpu_bus_noop(device_t dev)
227 {
228 }
229
230 static struct device_operations cpu_bus_ops = {
231         .read_resources   = cpu_bus_noop,
232         .set_resources    = cpu_bus_noop,
233         .enable_resources = cpu_bus_noop,
234         .init             = cpu_bus_init,
235         .scan_bus         = 0,
236 };
237 #endif
238
239 static void enable_dev(struct device *dev)
240 {
241   printk_spew("%s\n", __func__);
242         /* Set the operations if it is a special bus type */
243         if (dev->path.type == DEVICE_PATH_PCI_DOMAIN) {
244                 dev->ops = &pci_domain_ops;
245                 pci_set_method(dev);
246         }
247 #if 0
248         /* This is never hit as none of the sc520 boards have
249          * an APIC cluster defined
250          */
251         else if (dev->path.type == DEVICE_PATH_APIC_CLUSTER) {
252                 dev->ops = &cpu_bus_ops;
253         }
254 #endif
255 }
256
257
258 struct chip_operations cpu_amd_sc520_ops = {
259         CHIP_NAME("AMD Elan SC520 CPU")
260         .enable_dev = enable_dev, 
261 };