blob: c450d9b5555b748eaf4a33864dc2e5bea8e36bd6 [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.
Stefan Reinauer8e073822012-04-04 00:07:22 +020015 */
16
17#include <console/console.h>
18#include <device/device.h>
19#include <device/path.h>
20#include <device/smbus.h>
21#include <device/pci.h>
22#include <device/pci_ids.h>
23#include <device/pci_ops.h>
24#include <arch/io.h>
25#include "pch.h"
26#include "smbus.h"
27
28static void pch_smbus_init(device_t dev)
29{
30 struct resource *res;
31 u16 reg16;
32
33 /* Enable clock gating */
34 reg16 = pci_read_config32(dev, 0x80);
35 reg16 &= ~((1 << 8)|(1 << 10)|(1 << 12)|(1 << 14));
36 pci_write_config32(dev, 0x80, reg16);
37
38 /* Set Receive Slave Address */
39 res = find_resource(dev, PCI_BASE_ADDRESS_4);
40 if (res)
41 outb(SMBUS_SLAVE_ADDR, res->base + SMB_RCV_SLVA);
42}
43
44static int lsmbus_read_byte(device_t dev, u8 address)
45{
46 u16 device;
47 struct resource *res;
48 struct bus *pbus;
49
50 device = dev->path.i2c.device;
51 pbus = get_pbus_smbus(dev);
52 res = find_resource(pbus->dev, 0x20);
53
54 return do_smbus_read_byte(res->base, device, address);
55}
56
Vladimir Serbinenkoe57718f2014-01-27 23:48:27 +010057static int do_smbus_write_byte(unsigned smbus_base, unsigned device, unsigned address, unsigned data)
58{
59 unsigned char global_status_register;
60
61 if (smbus_wait_until_ready(smbus_base) < 0)
62 return SMBUS_WAIT_UNTIL_READY_TIMEOUT;
63
64 /* Setup transaction */
65 /* Disable interrupts */
66 outb(inb(smbus_base + SMBHSTCTL) & (~1), smbus_base + SMBHSTCTL);
67 /* Set the device I'm talking too */
68 outb(((device & 0x7f) << 1) & ~0x01, smbus_base + SMBXMITADD);
69 /* Set the command/address... */
70 outb(address & 0xff, smbus_base + SMBHSTCMD);
71 /* Set up for a byte data read */
72 outb((inb(smbus_base + SMBHSTCTL) & 0xe3) | (0x2 << 2),
73 (smbus_base + SMBHSTCTL));
74 /* Clear any lingering errors, so the transaction will run */
75 outb(inb(smbus_base + SMBHSTSTAT), smbus_base + SMBHSTSTAT);
76
77 /* Clear the data byte... */
78 outb(data, smbus_base + SMBHSTDAT0);
79
80 /* Start the command */
81 outb((inb(smbus_base + SMBHSTCTL) | 0x40),
82 smbus_base + SMBHSTCTL);
83
84 /* Poll for transaction completion */
85 if (smbus_wait_until_done(smbus_base) < 0)
86 return SMBUS_WAIT_UNTIL_DONE_TIMEOUT;
87
88 global_status_register = inb(smbus_base + SMBHSTSTAT);
89
90 /* Ignore the "In Use" status... */
91 global_status_register &= ~(3 << 5);
92
93 /* Read results of transaction */
94 if (global_status_register != (1 << 1))
95 return SMBUS_ERROR;
96
97 return 0;
98}
99
100static int lsmbus_write_byte(device_t dev, u8 address, u8 val)
101{
102 u16 device;
103 struct resource *res;
104 struct bus *pbus;
105
106 device = dev->path.i2c.device;
107 pbus = get_pbus_smbus(dev);
108 res = find_resource(pbus->dev, 0x20);
109
110 return do_smbus_write_byte(res->base, device, address, val);
111}
112
Stefan Reinauer8e073822012-04-04 00:07:22 +0200113static struct smbus_bus_operations lops_smbus_bus = {
114 .read_byte = lsmbus_read_byte,
Vladimir Serbinenkoe57718f2014-01-27 23:48:27 +0100115 .write_byte = lsmbus_write_byte,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200116};
117
118static void smbus_set_subsystem(device_t dev, unsigned vendor, unsigned device)
119{
120 if (!vendor || !device) {
121 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
122 pci_read_config32(dev, PCI_VENDOR_ID));
123 } else {
124 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
125 ((device & 0xffff) << 16) | (vendor & 0xffff));
126 }
127}
128
129static struct pci_operations smbus_pci_ops = {
130 .set_subsystem = smbus_set_subsystem,
131};
132
133static void smbus_read_resources(device_t dev)
134{
135 struct resource *res = new_resource(dev, PCI_BASE_ADDRESS_4);
136 res->base = SMBUS_IO_BASE;
137 res->size = 32;
138 res->limit = res->base + res->size - 1;
139 res->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_RESERVE |
140 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
141
142 /* Also add MMIO resource */
143 res = pci_get_resource(dev, PCI_BASE_ADDRESS_0);
144}
145
Patrick Rudolph604f6982017-06-07 09:46:52 +0200146static const char *smbus_acpi_name(device_t dev)
147{
148 return "SBUS";
149}
150
Stefan Reinauer8e073822012-04-04 00:07:22 +0200151static struct device_operations smbus_ops = {
152 .read_resources = smbus_read_resources,
153 .set_resources = pci_dev_set_resources,
154 .enable_resources = pci_dev_enable_resources,
Kyösti Mälkkid0e212c2015-02-26 20:47:47 +0200155 .scan_bus = scan_smbus,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200156 .init = pch_smbus_init,
157 .ops_smbus_bus = &lops_smbus_bus,
158 .ops_pci = &smbus_pci_ops,
Patrick Rudolph604f6982017-06-07 09:46:52 +0200159 .acpi_name = smbus_acpi_name,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200160};
161
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700162static const unsigned short pci_device_ids[] = { 0x1c22, 0x1e22, 0 };
Stefan Reinauer8e073822012-04-04 00:07:22 +0200163
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700164static const struct pci_driver pch_smbus __pci_driver = {
165 .ops = &smbus_ops,
166 .vendor = PCI_VENDOR_ID_INTEL,
167 .devices = pci_device_ids,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200168};