blob: 01988419efd15eeaaf44af0ac79bf48103b8a5ff [file] [log] [blame]
Stefan Reinauer8e073822012-04-04 00:07:22 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of
9 * the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010018 * Foundation, Inc.
Stefan Reinauer8e073822012-04-04 00:07:22 +020019 */
20
21#include <console/console.h>
22#include <device/device.h>
23#include <device/path.h>
24#include <device/smbus.h>
25#include <device/pci.h>
26#include <device/pci_ids.h>
27#include <device/pci_ops.h>
28#include <arch/io.h>
29#include "pch.h"
30#include "smbus.h"
31
32static void pch_smbus_init(device_t dev)
33{
34 struct resource *res;
35 u16 reg16;
36
37 /* Enable clock gating */
38 reg16 = pci_read_config32(dev, 0x80);
39 reg16 &= ~((1 << 8)|(1 << 10)|(1 << 12)|(1 << 14));
40 pci_write_config32(dev, 0x80, reg16);
41
42 /* Set Receive Slave Address */
43 res = find_resource(dev, PCI_BASE_ADDRESS_4);
44 if (res)
45 outb(SMBUS_SLAVE_ADDR, res->base + SMB_RCV_SLVA);
46}
47
48static int lsmbus_read_byte(device_t dev, u8 address)
49{
50 u16 device;
51 struct resource *res;
52 struct bus *pbus;
53
54 device = dev->path.i2c.device;
55 pbus = get_pbus_smbus(dev);
56 res = find_resource(pbus->dev, 0x20);
57
58 return do_smbus_read_byte(res->base, device, address);
59}
60
Vladimir Serbinenkoe57718f2014-01-27 23:48:27 +010061static int do_smbus_write_byte(unsigned smbus_base, unsigned device, unsigned address, unsigned data)
62{
63 unsigned char global_status_register;
64
65 if (smbus_wait_until_ready(smbus_base) < 0)
66 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
67
68 /* Setup transaction */
69 /* Disable interrupts */
70 outb(inb(smbus_base + SMBHSTCTL) & (~1), smbus_base + SMBHSTCTL);
71 /* Set the device I'm talking too */
72 outb(((device & 0x7f) << 1) & ~0x01, smbus_base + SMBXMITADD);
73 /* Set the command/address... */
74 outb(address & 0xff, smbus_base + SMBHSTCMD);
75 /* Set up for a byte data read */
76 outb((inb(smbus_base + SMBHSTCTL) & 0xe3) | (0x2 << 2),
77 (smbus_base + SMBHSTCTL));
78 /* Clear any lingering errors, so the transaction will run */
79 outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT);
80
81 /* Clear the data byte... */
82 outb(data, smbus_base + SMBHSTDAT0);
83
84 /* Start the command */
85 outb((inb(smbus_base + SMBHSTCTL) | 0x40),
86 smbus_base + SMBHSTCTL);
87
88 /* Poll for transaction completion */
89 if (smbus_wait_until_done(smbus_base) < 0)
90 return SMBUS_WAIT_UNTIL_DONE_TIMEOUT;
91
92 global_status_register = inb(smbus_base + SMBHSTSTAT);
93
94 /* Ignore the "In Use" status... */
95 global_status_register &= ~(3 << 5);
96
97 /* Read results of transaction */
98 if (global_status_register != (1 << 1))
99 return SMBUS_ERROR;
100
101 return 0;
102}
103
104static int lsmbus_write_byte(device_t dev, u8 address, u8 val)
105{
106 u16 device;
107 struct resource *res;
108 struct bus *pbus;
109
110 device = dev->path.i2c.device;
111 pbus = get_pbus_smbus(dev);
112 res = find_resource(pbus->dev, 0x20);
113
114 return do_smbus_write_byte(res->base, device, address, val);
115}
116
Stefan Reinauer8e073822012-04-04 00:07:22 +0200117static struct smbus_bus_operations lops_smbus_bus = {
118 .read_byte = lsmbus_read_byte,
Vladimir Serbinenkoe57718f2014-01-27 23:48:27 +0100119 .write_byte = lsmbus_write_byte,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200120};
121
122static void smbus_set_subsystem(device_t dev, unsigned vendor, unsigned device)
123{
124 if (!vendor || !device) {
125 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
126 pci_read_config32(dev, PCI_VENDOR_ID));
127 } else {
128 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
129 ((device & 0xffff) << 16) | (vendor & 0xffff));
130 }
131}
132
133static struct pci_operations smbus_pci_ops = {
134 .set_subsystem = smbus_set_subsystem,
135};
136
137static void smbus_read_resources(device_t dev)
138{
139 struct resource *res = new_resource(dev, PCI_BASE_ADDRESS_4);
140 res->base = SMBUS_IO_BASE;
141 res->size = 32;
142 res->limit = res->base + res->size - 1;
143 res->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_RESERVE |
144 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
145
146 /* Also add MMIO resource */
147 res = pci_get_resource(dev, PCI_BASE_ADDRESS_0);
148}
149
150static struct device_operations smbus_ops = {
151 .read_resources = smbus_read_resources,
152 .set_resources = pci_dev_set_resources,
153 .enable_resources = pci_dev_enable_resources,
Kyösti Mälkkid0e212c2015-02-26 20:47:47 +0200154 .scan_bus = scan_smbus,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200155 .init = pch_smbus_init,
156 .ops_smbus_bus = &lops_smbus_bus,
157 .ops_pci = &smbus_pci_ops,
158};
159
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700160static const unsigned short pci_device_ids[] = { 0x1c22, 0x1e22, 0 };
Stefan Reinauer8e073822012-04-04 00:07:22 +0200161
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700162static const struct pci_driver pch_smbus __pci_driver = {
163 .ops = &smbus_ops,
164 .vendor = PCI_VENDOR_ID_INTEL,
165 .devices = pci_device_ids,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200166};