printk_foo -> printk(BIOS_FOO, ...)
[coreboot.git] / src / southbridge / amd / amd8132 / amd8132_bridge.c
1 /*============================================================================
2 Copyright 2005 ADVANCED MICRO DEVICES, INC. All Rights Reserved.
3 This software and any related documentation (the "Materials") are the
4 confidential proprietary information of AMD. Unless otherwise provided in a
5 software agreement specifically licensing the Materials, the Materials are
6 provided in confidence and may not be distributed, modified, or reproduced in
7 whole or in part by any means.
8 LIMITATION OF LIABILITY: THE MATERIALS ARE PROVIDED "AS IS" WITHOUT ANY
9 EXPRESS OR IMPLIED WARRANTY OF ANY KIND, INCLUDING BUT NOT LIMITED TO
10 WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT, TITLE, FITNESS FOR ANY
11 PARTICULAR PURPOSE, OR WARRANTIES ARISING FROM CONDUCT, COURSE OF DEALING, OR
12 USAGE OF TRADE. IN NO EVENT SHALL AMD OR ITS LICENSORS BE LIABLE FOR ANY
13 DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS,
14 BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OF OR
15 INABILITY TO USE THE MATERIALS, EVEN IF AMD HAS BEEN ADVISED OF THE
16 POSSIBILITY OF SUCH DAMAGES. BECAUSE SOME JURISDICTIONS PROHIBIT THE EXCLUSION
17 OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES, THE ABOVE
18 LIMITATION MAY NOT APPLY TO YOU.
19 AMD does not assume any responsibility for any errors which may appear in the
20 Materials nor any responsibility to support or update the Materials. AMD
21 retains the right to modify the Materials at any time, without notice, and is
22 not obligated to provide such modified Materials to you.
23 NO SUPPORT OBLIGATION: AMD is not obligated to furnish, support, or make any
24 further information, software, technical information, know-how, or show-how
25 available to you.
26 U.S. GOVERNMENT RESTRICTED RIGHTS: The Materials are provided with "RESTRICTED
27 RIGHTS." Use, duplication, or disclosure by the Government is subject to the
28 restrictions as set forth in FAR 52.227-14 and DFAR 252.227-7013, et seq., or
29 its successor. Use of the Materials by the Government constitutes
30 acknowledgement of AMD's proprietary rights in them.
31 ============================================================================*/
32 //@DOC
33 // in amd8132_bridge.c
34 /*
35 $1.0$
36 */
37 // Description: amd 8132 support
38 // by yhlu
39 //
40 //============================================================================
41
42 #include <console/console.h>
43 #include <device/device.h>
44 #include <device/pci.h>
45 #include <device/pci_ids.h>
46 #include <device/pci_ops.h>
47 #include <pc80/mc146818rtc.h>
48 #include <device/pci_def.h>
49 #include <device/pcix.h>
50
51 #define NMI_OFF 0
52
53 #define NPUML 0xD9      /* Non prefetchable upper memory limit */
54 #define NPUMB 0xD8      /* Non prefetchable upper memory base */
55
56 static void amd8132_walk_children(struct bus *bus,
57         void (*visit)(device_t dev, void *ptr), void *ptr)
58 {
59         device_t child;
60         for(child = bus->children; child; child = child->sibling)
61         {
62                 if (child->path.type != DEVICE_PATH_PCI) {
63                         continue;
64                 }
65                 if (child->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
66                         amd8132_walk_children(&child->link[0], visit, ptr);
67                 }
68                 visit(child, ptr);
69         }
70 }
71
72 struct amd8132_bus_info {
73         unsigned sstatus;
74         unsigned rev;
75         int master_devices;
76         int max_func;
77 };
78
79 static void amd8132_count_dev(device_t dev, void *ptr)
80 {
81         struct amd8132_bus_info *info = ptr;
82         /* Don't count pci bridges */
83         if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
84                 info->master_devices++;
85         }
86         if (PCI_FUNC(dev->path.pci.devfn) > info->max_func) {
87                 info->max_func = PCI_FUNC(dev->path.pci.devfn);
88         }
89 }
90
91
92 static void amd8132_pcix_tune_dev(device_t dev, void *ptr)
93 {
94         struct amd8132_bus_info *info = ptr;
95         unsigned cap;
96         unsigned status, cmd, orig_cmd;
97         unsigned max_read, max_tran;
98         int  sibs;
99
100         if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL) {
101                 return;
102         }
103         cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
104         if (!cap) {
105                 return;
106         }
107         /* How many siblings does this device have? */
108         sibs = info->master_devices - 1;
109
110         printk(BIOS_DEBUG, "%s AMD8132 PCI-X tuning\n", dev_path(dev));
111         status = pci_read_config32(dev, cap + PCI_X_STATUS);
112         orig_cmd = cmd = pci_read_config16(dev,cap + PCI_X_CMD);
113
114         max_read = (status & PCI_X_STATUS_MAX_READ) >> 21;
115         max_tran = (status & PCI_X_STATUS_MAX_SPLIT) >> 23;
116
117         if (info->rev == 0x01) { // only a1 need it 
118                 /* Errata #53 Limit the number of split transactions to avoid starvation */
119                 if (sibs >= 2) {
120                         /* At most 2 outstanding split transactions when we have
121                         * 3 or more bus master devices on the bus.
122                         */
123                         if (max_tran > 1) {
124                                 max_tran = 1;
125                         }
126                 }
127                 else if (sibs == 1) {
128                         /* At most 4 outstanding split transactions when we have
129                         * 2 bus master devices on the bus.
130                         */
131                         if (max_tran > 3) {
132                                 max_tran = 3;
133                         }
134                 }
135                 else {
136                         /* At most 8 outstanding split transactions when we have
137                         * only one bus master device on the bus.
138                         */
139                         if (max_tran > 4) {
140                                 max_tran = 4;
141                         }
142                 }
143         }
144
145         if (max_read != ((cmd & PCI_X_CMD_MAX_READ) >> 2)) {
146                 cmd &= ~PCI_X_CMD_MAX_READ;
147                 cmd |= max_read << 2;
148                 }
149         if (max_tran != ((cmd & PCI_X_CMD_MAX_SPLIT) >> 4)) {
150                 cmd &= ~PCI_X_CMD_MAX_SPLIT;
151                 cmd |= max_tran << 4;
152         }
153
154         /* Don't attempt to handle PCI-X errors */
155         cmd &= ~PCI_X_CMD_DPERR_E;
156         if (orig_cmd != cmd) {
157                 pci_write_config16(dev, cap + PCI_X_CMD, cmd);
158         }
159
160
161 }
162 static unsigned int amd8132_scan_bus(struct bus *bus,
163         unsigned min_devfn, unsigned max_devfn, unsigned int max)
164 {
165         struct amd8132_bus_info info;
166         unsigned pos;
167
168
169         /* Find the children on the bus */
170         max = pci_scan_bus(bus, min_devfn, max_devfn, max);
171
172         /* Find the revision of the 8132 */
173         info.rev = pci_read_config8(bus->dev, PCI_CLASS_REVISION);
174
175         /* Find the pcix capability and get the secondary bus status */
176         pos = pci_find_capability(bus->dev, PCI_CAP_ID_PCIX);
177         info.sstatus = pci_read_config16(bus->dev, pos + PCI_X_SEC_STATUS);
178
179         /* Print the PCI-X bus speed */
180         printk(BIOS_DEBUG, "PCI: %02x: %s sstatus=%04x rev=%02x \n", bus->secondary, pcix_speed(info.sstatus), info.sstatus, info.rev);
181
182
183         /* Examine the bus and find out how loaded it is */
184         info.max_func = 0;
185         info.master_devices  = 0;
186         amd8132_walk_children(bus, amd8132_count_dev, &info);
187
188 #if 0
189         /* Disable the bus if there are no devices on it 
190          */
191         if (!bus->children)
192         {
193                 unsigned pcix_misc;
194                 /* Disable all of my children */
195                 disable_children(bus);
196
197                 /* Remember the device is disabled */
198                 bus->dev->enabled = 0;
199
200                 /* Disable the PCI-X clocks */
201                 pcix_misc = pci_read_config32(bus->dev, 0x40);
202                 pcix_misc &= ~(0x1f << 16);
203                 pci_write_config32(bus->dev, 0x40, pcix_misc);
204                 
205                 return max;
206         }
207 #endif
208
209         /* If we are in conventional PCI mode nothing more is necessary.
210          */
211         if (PCI_X_SSTATUS_MFREQ(info.sstatus) == PCI_X_SSTATUS_CONVENTIONAL_PCI) {
212                 return max;
213         }
214
215         /* Tune the devices on the bus */
216         amd8132_walk_children(bus, amd8132_pcix_tune_dev, &info);
217
218         return max;
219 }
220
221 static unsigned int amd8132_scan_bridge(device_t dev, unsigned int max)
222 {
223         return do_pci_scan_bridge(dev, max, amd8132_scan_bus);
224 }
225
226
227 static void amd8132_pcix_init(device_t dev)
228 {
229         uint32_t dword;
230         uint8_t byte;
231         unsigned chip_rev;
232         
233         /* Find the revision of the 8132 */
234         chip_rev = pci_read_config8(dev, PCI_CLASS_REVISION);
235
236         /* Enable memory write and invalidate ??? */
237         dword = pci_read_config32(dev, 0x04);
238         dword |= 0x10;
239         dword &= ~(1<<6); // PERSP Parity Error Response
240         pci_write_config32(dev, 0x04, dword);
241
242         if (chip_rev == 0x01) {
243                 /* Errata #37 */
244                 byte = pci_read_config8(dev, 0x0c);
245                 if(byte == 0x08 )
246                         pci_write_config8(dev, 0x0c, 0x10);
247
248 #if 0
249                 /* Errata #59*/
250                 dword = pci_read_config32(dev, 0x40);
251                 dword &= ~(1<<31);
252                 pci_write_config32(dev, 0x40, dword);
253 #endif
254
255         }
256
257         /* Set up error reporting, enable all */
258         /* system error enable */
259         dword = pci_read_config32(dev, 0x04);
260         dword |= (1<<8);
261         pci_write_config32(dev, 0x04, dword);
262         
263         /* system and error parity enable */
264         dword = pci_read_config32(dev, 0x3c);
265         dword |= (3<<16);
266         pci_write_config32(dev, 0x3c, dword);
267
268         dword = pci_read_config32(dev, 0x40);
269 //        dword &= ~(1<<31); /* WriteChainEnable */
270         dword |= (1<<31); 
271         dword |= (1<<7);// must set to 1
272         dword |= (3<<21); //PCIErrorSerrDisable
273         pci_write_config32(dev, 0x40, dword);
274
275         /* EXTARB = 1, COMPAT = 0 */
276         dword = pci_read_config32(dev, 0x48);
277         dword |= (1<<3);
278         dword &= ~(1<<0);
279         dword |= (1<<15); //CLEARPCILOG_L
280         dword |= (1<<19); //PERR FATAL Enable
281         dword |= (1<<22); // SERR FATAL Enable
282         dword |= (1<<23); // LPMARBENABLE
283         dword |= (0x61<<24); //LPMARBCOUNT
284         pci_write_config32(dev, 0x48, dword);
285
286         dword = pci_read_config32(dev, 0x4c);
287         dword |= (1<<6); //intial prefetch for memory read line request
288         dword |= (1<<9); //continuous prefetch Enable for memory read line request
289         pci_write_config32(dev, 0x4c, dword);
290
291
292        /* Disable Single-Bit-Error Correction [30] = 0 */
293         dword = pci_read_config32(dev, 0x70);
294         dword &= ~(1<<30);
295         pci_write_config32(dev, 0x70, dword);
296
297         //link
298         dword = pci_read_config32(dev, 0xd4);
299         dword |= (0x5c<<16);
300         pci_write_config32(dev, 0xd4, dword);
301
302         /* TxSlack0 [16:17] = 0, RxHwLookahdEn0 [18] = 1, TxSlack1 [24:25] = 0, RxHwLookahdEn1 [26] = 1 */
303         dword = pci_read_config32(dev, 0xdc);
304         dword |= (1<<1) |  (1<<4); // stream disable 1 to 0 , DBLINSRATE
305         dword |= (1<<18)|(1<<26);
306         dword &= ~((3<<16)|(3<<24));
307         pci_write_config32(dev, 0xdc, dword);
308
309         /* Set up CRC flood enable */
310         dword = pci_read_config32(dev, 0xc0);
311         if(dword) {  /* do device A only */
312 #if 0
313                 dword = pci_read_config32(dev, 0xc4);
314                 dword |= (1<<1);
315                 pci_write_config32(dev, 0xc4, dword);
316                 dword = pci_read_config32(dev, 0xc8);
317                 dword |= (1<<1);
318                 pci_write_config32(dev, 0xc8, dword);
319 #endif
320
321                 if (chip_rev == 0x11) {
322                         /* [18] Clock Gate Enable = 1 */
323                         dword = pci_read_config32(dev, 0xf0);
324                         dword |= 0x00040008;
325                         pci_write_config32(dev, 0xf0, dword);
326                 }
327
328         }
329         return;
330 }
331
332 #define BRIDGE_40_BIT_SUPPORT 0
333 #if BRIDGE_40_BIT_SUPPORT
334 static void bridge_read_resources(struct device *dev)
335 {
336         struct resource *res;
337         pci_bus_read_resources(dev);
338         res = find_resource(dev, PCI_MEMORY_BASE);      
339         if (res) {
340                 res->limit = 0xffffffffffULL;
341         }
342 }
343
344 static void bridge_set_resources(struct device *dev)
345 {
346         struct resource *res;
347         res = find_resource(dev, PCI_MEMORY_BASE);
348         if (res) {
349                 resource_t base, end;
350                 /* set the memory range */
351                 dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
352                 res->flags |= IORESOURCE_STORED;
353                 base = res->base;
354                 end  = resource_end(res);
355                 pci_write_config16(dev, PCI_MEMORY_BASE, base >> 16);
356                 pci_write_config8(dev, NPUML, (base >> 32) & 0xff);
357                 pci_write_config16(dev, PCI_MEMORY_LIMIT, end >> 16);
358                 pci_write_config8(dev, NPUMB, (end >> 32) & 0xff);
359
360                 report_resource_stored(dev, res, "");
361         }
362         pci_dev_set_resources(dev);
363 }
364 #endif /* BRIDGE_40_BIT_SUPPORT */
365
366 static struct device_operations pcix_ops  = {
367 #if BRIDGE_40_BIT_SUPPORT
368         .read_resources   = bridge_read_resources,
369         .set_resources    = bridge_set_resources,
370 #else
371         .read_resources   = pci_bus_read_resources,
372         .set_resources    = pci_dev_set_resources,
373 #endif
374         .enable_resources = pci_bus_enable_resources,
375         .init             = amd8132_pcix_init,
376         .scan_bus         = amd8132_scan_bridge,
377         .reset_bus        = pci_bus_reset,
378 };
379
380 static const struct pci_driver pcix_driver __pci_driver = {
381         .ops    = &pcix_ops,
382         .vendor = PCI_VENDOR_ID_AMD,
383         .device = 0x7458,
384 };
385
386 static void ioapic_enable(device_t dev)
387 {
388         uint32_t value;
389
390         value = pci_read_config32(dev, 0x44);
391         if (dev->enabled) {
392                 value |= ((1 << 1) | (1 << 0));
393         } else {
394                 value &= ~((1 << 1) | (1 << 0));
395         }
396         pci_write_config32(dev, 0x44, value);
397 }
398 static void amd8132_ioapic_init(device_t dev)
399 {
400         uint32_t dword;
401         unsigned chip_rev;
402
403         /* Find the revision of the 8132 */
404         chip_rev = pci_read_config8(dev, PCI_CLASS_REVISION);
405
406         if (chip_rev == 0x01) {
407 #if 0
408                 /* Errata #43 */
409                 dword = pci_read_config32(dev, 0xc8);
410                 dword |= (0x3<<23);
411                 pci_write_config32(dev, 0xc8, dword);
412 #endif
413
414         }
415
416
417         if( (chip_rev == 0x11) ||(chip_rev == 0x12) ) {
418                 //for b1 b2
419                 /* Errata #73 */
420                 dword = pci_read_config32(dev, 0x80);
421                 dword |= (0x1f<<5);
422                 pci_write_config32(dev, 0x80, dword);
423                 dword = pci_read_config32(dev, 0x88);
424                 dword |= (0x1f<<5);
425                 pci_write_config32(dev, 0x88, dword);
426
427                 /* Errata #74 */
428                 dword = pci_read_config32(dev, 0x7c);
429                 dword &= ~(0x3<<30);
430                 dword |= (0x01<<30);
431                 pci_write_config32(dev, 0x7c, dword);
432         }
433
434 }
435
436 static struct pci_operations pci_ops_pci_dev = {
437         .set_subsystem    = pci_dev_set_subsystem,
438 };
439 static struct device_operations ioapic_ops = {
440         .read_resources   = pci_dev_read_resources,
441         .set_resources    = pci_dev_set_resources,
442         .enable_resources = pci_dev_enable_resources,
443         .init             = amd8132_ioapic_init,
444         .scan_bus         = 0,
445         .enable           = ioapic_enable,
446         .ops_pci          = &pci_ops_pci_dev,
447 };
448
449 static const struct pci_driver ioapic_driver __pci_driver = {
450         .ops    = &ioapic_ops,
451         .vendor = PCI_VENDOR_ID_AMD,
452         .device = 0x7459,
453         
454 };