Since some people disapprove of white space cleanups mixed in regular commits
[coreboot.git] / src / southbridge / intel / esb6300 / esb6300_early_smbus.c
1 #include "esb6300_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\n");
10         pci_write_config32(dev, 0x20, SMBUS_IO_BASE | 1);
11         pci_write_config8(dev, 0x40, 1);
12         pci_write_config8(dev, 0x4, 1);
13         /* SMBALERT_DIS */
14         pci_write_config8(dev, 0x11, 4);
15
16         /* Disable interrupt generation */
17         outb(0, SMBUS_IO_BASE + SMBHSTCTL);
18 }
19
20 static int smbus_read_byte(unsigned device, unsigned address)
21 {
22         return do_smbus_read_byte(SMBUS_IO_BASE, device, address);
23 }
24
25 static void smbus_write_byte(unsigned device, unsigned address, unsigned char val)
26 {
27         if (smbus_wait_until_ready(SMBUS_IO_BASE) < 0) {
28                 return;
29         }
30         return;
31 }
32
33 static int smbus_write_block(unsigned device, unsigned length, unsigned cmd,
34                  unsigned data1, unsigned data2)
35 {
36         unsigned char global_control_register;
37         unsigned char global_status_register;
38         unsigned char byte;
39         unsigned char stat;
40         int i;
41
42         /* chear the PM timeout flags, SECOND_TO_STS */
43         outw(inw(0x0400 + 0x66), 0x0400 + 0x66);
44
45         if (smbus_wait_until_ready(SMBUS_IO_BASE) < 0) {
46                 return -2;
47         }
48
49         /* setup transaction */
50         /* Obtain ownership */
51         outb(inb(SMBUS_IO_BASE + SMBHSTSTAT), SMBUS_IO_BASE + SMBHSTSTAT);
52         for(stat=0;(stat&0x40)==0;) {
53         stat = inb(SMBUS_IO_BASE + SMBHSTSTAT);
54         }
55         /* clear the done bit */
56         outb(0x80, SMBUS_IO_BASE + SMBHSTSTAT);
57         /* disable interrupts */
58         outb(inb(SMBUS_IO_BASE + SMBHSTCTL) & (~1), SMBUS_IO_BASE + SMBHSTCTL);
59
60         /* set the device I'm talking too */
61         outb(((device & 0x7f) << 1), SMBUS_IO_BASE + SMBXMITADD);
62
63         /* set the command address */
64         outb(cmd & 0xFF, SMBUS_IO_BASE + SMBHSTCMD);
65
66         /* set the block length */
67         outb(length & 0xFF, SMBUS_IO_BASE + SMBHSTDAT0);
68
69         /* try sending out the first byte of data here */
70         byte=(data1>>(0))&0x0ff;
71         outb(byte,SMBUS_IO_BASE + SMBBLKDAT);
72         /* issue a block write command */
73         outb((inb(SMBUS_IO_BASE + SMBHSTCTL) & 0xE3) | (0x5 << 2) | 0x40,
74                         SMBUS_IO_BASE + SMBHSTCTL);
75
76         for(i=0;i<length;i++) {
77
78                 /* poll for transaction completion */
79                 if (smbus_wait_until_blk_done(SMBUS_IO_BASE) < 0) {
80                         return -3;
81                 }
82
83                 /* load the next byte */
84                 if(i>3)
85                         byte=(data2>>(i%4))&0x0ff;
86                 else
87                         byte=(data1>>(i))&0x0ff;
88                 outb(byte,SMBUS_IO_BASE + SMBBLKDAT);
89
90                 /* clear the done bit */
91                 outb(inb(SMBUS_IO_BASE + SMBHSTSTAT),
92                                 SMBUS_IO_BASE + SMBHSTSTAT);
93         }
94
95         print_debug("SMBUS Block complete\n");
96         return 0;
97 }
98