This does the following:
[coreboot.git] / src / southbridge / intel / i82801ex / i82801ex_early_smbus.c
1 #include "i82801ex_smbus.h"
2
3 #define SMBUS_IO_BASE 0x0f00
4
5 static void enable_smbus(void)
6 {
7         device_t dev = PCI_DEV(0x0, 0x1f, 0x3);
8
9         print_spew("SMBus controller enabled\r\n");
10
11         pci_write_config32(dev, 0x20, SMBUS_IO_BASE | 1);
12         print_debug_hex32(pci_read_config32(dev, 0x20));
13         /* Set smbus enable */
14         pci_write_config8(dev, 0x40, 1);
15         /* Set smbus iospace enable */
16         pci_write_config8(dev, 0x4, 1);
17         /* SMBALERT_DIS */
18         pci_write_config8(dev, 0x11, 4);
19
20         /* Disable interrupt generation */
21         outb(0, SMBUS_IO_BASE + SMBHSTCTL);
22
23         /* clear any lingering errors, so the transaction will run */
24         outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT);
25 }
26
27 static int smbus_read_byte(unsigned device, unsigned address)
28 {
29         return do_smbus_read_byte(SMBUS_IO_BASE, device, address);
30 }
31
32 static void smbus_write_byte(unsigned device, unsigned address, unsigned char val)
33 {
34         if (smbus_wait_until_ready(SMBUS_IO_BASE) < 0) {
35                 return;
36         }
37         
38         print_debug("Unimplemented smbus_write_byte() called.\r\n");
39
40 #if 0
41         /* setup transaction */
42         /* disable interrupts */
43         outw(inw(SMBUS_IO_BASE + SMBGCTL) & ~((1<<10)|(1<<9)|(1<<8)|(1<<4)),
44                         SMBUS_IO_BASE + SMBGCTL);
45         /* set the device I'm talking too */
46         outw(((device & 0x7f) << 1) | 1, SMBUS_IO_BASE + SMBHSTADDR);
47         outb(address & 0xFF, SMBUS_IO_BASE + SMBHSTCMD);
48         /* set up for a byte data write */ /* FIXME */
49         outw((inw(SMBUS_IO_BASE + SMBGCTL) & ~7) | (0x1), SMBUS_IO_BASE + SMBGCTL);
50         /* clear any lingering errors, so the transaction will run */
51         /* Do I need to write the bits to a 1 to clear an error? */
52         outw(inw(SMBUS_IO_BASE + SMBGSTATUS), SMBUS_IO_BASE + SMBGSTATUS);
53
54         /* clear the data word...*/
55         outw(val, SMBUS_IO_BASE + SMBHSTDAT);
56
57         /* start the command */
58         outw((inw(SMBUS_IO_BASE + SMBGCTL) | (1 << 3)), SMBUS_IO_BASE + SMBGCTL);
59
60         /* poll for transaction completion */
61         smbus_wait_until_done(SMBUS_IO_BASE);
62 #endif  
63         return;
64 }
65
66 static int smbus_write_block(unsigned device, unsigned length, unsigned cmd, 
67                  unsigned data1, unsigned data2)
68 {
69         unsigned char global_control_register;
70         unsigned char global_status_register;
71         unsigned char byte;
72         unsigned char stat;
73         int i;
74
75         /* chear the PM timeout flags, SECOND_TO_STS */
76         outw(inw(0x0400 + 0x66), 0x0400 + 0x66);
77         
78         if (smbus_wait_until_ready(SMBUS_IO_BASE) < 0) {
79                 return -2;
80         }
81         
82         /* setup transaction */
83         /* Obtain ownership */
84         outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT);
85         for(stat=0;(stat&0x40)==0;) {
86         stat = inb(SMBUS_IO_BASE + SMBHSTSTAT);
87         }
88         /* clear the done bit */
89         outb(0x80, SMBUS_IO_BASE + SMBHSTSTAT);
90         /* disable interrupts */
91         outb(inb(SMBUS_IO_BASE + SMBHSTCTL) & (~1), SMBUS_IO_BASE + SMBHSTCTL);
92         
93         /* set the device I'm talking too */
94         outb(((device & 0x7f) << 1), SMBUS_IO_BASE + SMBXMITADD);
95         
96         /* set the command address */
97         outb(cmd & 0xFF, SMBUS_IO_BASE + SMBHSTCMD);
98         
99         /* set the block length */
100         outb(length & 0xFF, SMBUS_IO_BASE + SMBHSTDAT0);
101         
102         /* try sending out the first byte of data here */
103         byte=(data1>>(0))&0x0ff;
104         outb(byte,SMBUS_IO_BASE + SMBBLKDAT);
105         /* issue a block write command */
106         outb((inb(SMBUS_IO_BASE + SMBHSTCTL) & 0xE3) | (0x5 << 2) | 0x40, 
107                         SMBUS_IO_BASE + SMBHSTCTL);
108
109         for(i=0;i<length;i++) {
110                 
111                 /* poll for transaction completion */
112                 if (smbus_wait_until_blk_done(SMBUS_IO_BASE) < 0) {
113                         return -3;
114                 }
115                 
116                 /* load the next byte */
117                 if(i>3)
118                         byte=(data2>>(i%4))&0x0ff;
119                 else
120                         byte=(data1>>(i))&0x0ff;
121                 outb(byte,SMBUS_IO_BASE + SMBBLKDAT);
122                 
123                 /* clear the done bit */
124                 outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), 
125                                 SMBUS_IO_BASE + SMBHSTSTAT);
126         }
127
128         print_debug("SMBUS Block complete\r\n");
129         return 0;
130 }
131