814d17751e36953d0bcc7f347ff2b6d4740304d8
[coreboot.git] / src / mainboard / olpc / btest / mainboard.c
1 #include <console/console.h>
2 #include <device/device.h>
3 #include <device/pci.h>
4 #include <device/pci_ids.h>
5 #include <device/pci_ops.h>
6 #include <arch/io.h>
7 #include <pc80/mc146818rtc.h>
8 #include "chip.h"
9 #include "../southbridge/amd/cs5536/cs5536_smbus2.h"
10
11 /* Borrowed from mc146818rtc.c */
12
13 #define CMOS_READ(addr) ({ \
14                 outb((addr),RTC_PORT(0)); \
15                 inb(RTC_PORT(1)); \
16                 })
17
18 #define CMOS_WRITE(val, addr) ({ \
19                 outb((addr),RTC_PORT(0)); \
20                 outb((val),RTC_PORT(1)); \
21                 })
22
23 static void write_bit(unsigned char val) {
24
25         unsigned char byte = CMOS_READ(440 / 8);
26
27         /* Don't change it if its already set */
28
29         if ((byte & 1) == (val & 1))
30                 return;
31
32         byte &= ~1;
33         byte |= val & 1;
34         CMOS_WRITE(val, 440/8);
35 }
36
37 static unsigned short _getsmbusbase(void) {
38         unsigned devfn = PCI_DEVFN(0xf, 0);
39         device_t dev = dev_find_slot(0x0, devfn);
40         unsigned long addr = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
41
42         return (unsigned short) (addr & ~1);
43 }
44
45 static void init_dcon(void) {
46
47   int ret = 1;
48   unsigned short rev = 0;
49   unsigned short iobase = _getsmbusbase();
50
51   printk_debug("CHECKING FOR DCON (%x)\n", iobase);
52
53   /* Get the IO base for the SMBUS */
54
55   rev = do_smbus_read_word(iobase, 0x0D << 1, 0x00);
56
57   if (rev & 0xDC00) {
58         printk_debug("DCON FOUND - REV %x\n", rev);
59
60         /* Enable the DCON */
61         ret = do_smbus_write_word(iobase, 0x0D << 1, 0x01, 0x0069);
62         if (ret != 0)
63                 printk_debug("DCON ENABLE FAILED\n", ret);
64   }
65   else
66           printk_debug("DCON NOT FOUND (%x)\n", rev);
67
68   write_bit(rev > 0 ? 1 : 0);
69 }
70
71 void
72 init_cafe_irq(void){
73         const unsigned char slots_cafe[4] = {11, 0, 0, 0};
74
75  
76         /* CAFE PCI slots */ 
77         pci_assign_irqs(0, 0x0C, slots_cafe); 
78
79         /* Make the pin assignments - NOTENOTENOTE:  This should be 
80           * configurable! 
81           */ 
82
83         /* Configure the GPIO pins to use - class 0, index 9 to configure 
84           * AB.  Write 0xFF to disable 
85           */ 
86         
87         vrWrite(0x9, 0XFF00); 
88         
89         /* Configure the GPIO pins to use - class 0, index A to configure 
90           * CD.  Write 0xFF to disable 
91           */ 
92         
93        vrWrite(0xA, 0xFFFF); 
94          
95 }
96
97
98 static void init(struct device *dev) {
99 /*
100         unsigned bus = 0;
101         unsigned devfn = PCI_DEVFN(0xf, 4);
102         device_t usb = NULL;
103         unsigned char usbirq = 0xa;
104 */
105
106         printk_debug("OLPC BTEST ENTER %s\n", __FUNCTION__);
107
108 #if 0
109         /* I can't think of any reason NOT to just set this. If it turns out we want this to be
110           * conditional we can make it a config variable later.
111           */
112
113         printk_debug("%s (%x,%x)SET USB PCI interrupt line to %d\n", 
114                 __FUNCTION__, bus, devfn, usbirq);
115         usb = dev_find_slot(bus, devfn);
116         if (! usb){
117                 printk_err("Could not find USB\n");
118         } else {
119                 pci_write_config8(usb, PCI_INTERRUPT_LINE, usbirq);
120         }
121 #endif
122
123         init_dcon();
124         init_cafe_irq();
125         printk_debug("OLPC BTEST EXIT %s\n", __FUNCTION__);
126 }
127
128 static void enable_dev(struct device *dev)
129 {
130         dev->ops->init = init;
131 }
132
133 struct chip_operations mainboard_olpc_btest_ops = {
134         CHIP_NAME("OLPC btest Mainboard")
135         .enable_dev = enable_dev,
136 };