blob: 0da3b76030a23bcc607ecb49037d30dcec2fbcdd [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer8e073822012-04-04 00:07:22 +02002
Stefan Reinauer8e073822012-04-04 00:07:22 +02003#include <device/device.h>
4#include <device/path.h>
5#include <device/smbus.h>
6#include <device/pci.h>
Elyes HAOUASa4dd33c2020-08-11 09:39:43 +02007#include <device/pci_def.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +02008#include <device/pci_ids.h>
9#include <device/pci_ops.h>
Kyösti Mälkki1cae4542020-01-06 12:31:34 +020010#include <device/smbus_host.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020011#include "pch.h"
Stefan Reinauer8e073822012-04-04 00:07:22 +020012
Elyes HAOUAS4aec3402018-05-25 08:29:27 +020013static void pch_smbus_init(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +020014{
15 struct resource *res;
16 u16 reg16;
17
18 /* Enable clock gating */
Angel Ponsc803f652020-06-07 22:09:01 +020019 /* FIXME: Using 32-bit ops with a 16-bit variable is a bug! These should be 16-bit! */
Stefan Reinauer8e073822012-04-04 00:07:22 +020020 reg16 = pci_read_config32(dev, 0x80);
21 reg16 &= ~((1 << 8)|(1 << 10)|(1 << 12)|(1 << 14));
22 pci_write_config32(dev, 0x80, reg16);
23
24 /* Set Receive Slave Address */
25 res = find_resource(dev, PCI_BASE_ADDRESS_4);
26 if (res)
Kyösti Mälkki73451fd2020-01-06 19:00:31 +020027 smbus_set_slave_addr(res->base, SMBUS_SLAVE_ADDR);
Stefan Reinauer8e073822012-04-04 00:07:22 +020028}
29
Elyes HAOUAS4aec3402018-05-25 08:29:27 +020030static int lsmbus_read_byte(struct device *dev, u8 address)
Stefan Reinauer8e073822012-04-04 00:07:22 +020031{
32 u16 device;
33 struct resource *res;
34 struct bus *pbus;
35
36 device = dev->path.i2c.device;
37 pbus = get_pbus_smbus(dev);
Elyes HAOUASa4dd33c2020-08-11 09:39:43 +020038 res = find_resource(pbus->dev, PCI_BASE_ADDRESS_4);
Stefan Reinauer8e073822012-04-04 00:07:22 +020039
40 return do_smbus_read_byte(res->base, device, address);
41}
42
Angel Ponsbb19d392020-12-05 20:23:40 +010043static int lsmbus_write_byte(struct device *dev, u8 address, u8 data)
Vladimir Serbinenkoe57718f2014-01-27 23:48:27 +010044{
45 u16 device;
46 struct resource *res;
47 struct bus *pbus;
48
49 device = dev->path.i2c.device;
50 pbus = get_pbus_smbus(dev);
Elyes HAOUASa4dd33c2020-08-11 09:39:43 +020051 res = find_resource(pbus->dev, PCI_BASE_ADDRESS_4);
Angel Ponsbb19d392020-12-05 20:23:40 +010052 return do_smbus_write_byte(res->base, device, address, data);
Vladimir Serbinenkoe57718f2014-01-27 23:48:27 +010053}
54
Angel Pons244cf7d2020-12-05 20:25:27 +010055static int lsmbus_block_write(struct device *dev, u8 cmd, u8 bytes, const u8 *buf)
56{
57 u16 device;
58 struct resource *res;
59 struct bus *pbus;
60
61 device = dev->path.i2c.device;
62 pbus = get_pbus_smbus(dev);
63 res = find_resource(pbus->dev, PCI_BASE_ADDRESS_4);
64 return do_smbus_block_write(res->base, device, cmd, bytes, buf);
65}
66
67static int lsmbus_block_read(struct device *dev, u8 cmd, u8 bytes, u8 *buf)
68{
69 u16 device;
70 struct resource *res;
71 struct bus *pbus;
72
73 device = dev->path.i2c.device;
74 pbus = get_pbus_smbus(dev);
75 res = find_resource(pbus->dev, PCI_BASE_ADDRESS_4);
76 return do_smbus_block_read(res->base, device, cmd, bytes, buf);
77}
78
Stefan Reinauer8e073822012-04-04 00:07:22 +020079static struct smbus_bus_operations lops_smbus_bus = {
80 .read_byte = lsmbus_read_byte,
Vladimir Serbinenkoe57718f2014-01-27 23:48:27 +010081 .write_byte = lsmbus_write_byte,
Angel Pons244cf7d2020-12-05 20:25:27 +010082 .block_read = lsmbus_block_read,
83 .block_write = lsmbus_block_write,
Stefan Reinauer8e073822012-04-04 00:07:22 +020084};
85
Elyes HAOUAS4aec3402018-05-25 08:29:27 +020086static void smbus_read_resources(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +020087{
88 struct resource *res = new_resource(dev, PCI_BASE_ADDRESS_4);
Angel Ponsb21bffa2020-07-03 01:02:28 +020089 res->base = CONFIG_FIXED_SMBUS_IO_BASE;
Stefan Reinauer8e073822012-04-04 00:07:22 +020090 res->size = 32;
91 res->limit = res->base + res->size - 1;
92 res->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_RESERVE |
93 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
94
95 /* Also add MMIO resource */
96 res = pci_get_resource(dev, PCI_BASE_ADDRESS_0);
97}
98
Aaron Durbinaa090cb2017-09-13 16:01:52 -060099static const char *smbus_acpi_name(const struct device *dev)
Patrick Rudolph604f6982017-06-07 09:46:52 +0200100{
101 return "SBUS";
102}
103
Stefan Reinauer8e073822012-04-04 00:07:22 +0200104static struct device_operations smbus_ops = {
105 .read_resources = smbus_read_resources,
106 .set_resources = pci_dev_set_resources,
107 .enable_resources = pci_dev_enable_resources,
Kyösti Mälkkid0e212c2015-02-26 20:47:47 +0200108 .scan_bus = scan_smbus,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200109 .init = pch_smbus_init,
110 .ops_smbus_bus = &lops_smbus_bus,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200111 .ops_pci = &pci_dev_ops_pci,
Patrick Rudolph604f6982017-06-07 09:46:52 +0200112 .acpi_name = smbus_acpi_name,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200113};
114
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700115static const unsigned short pci_device_ids[] = { 0x1c22, 0x1e22, 0 };
Stefan Reinauer8e073822012-04-04 00:07:22 +0200116
Stefan Reinauer9a380ab2012-06-22 13:16:11 -0700117static const struct pci_driver pch_smbus __pci_driver = {
118 .ops = &smbus_ops,
119 .vendor = PCI_VENDOR_ID_INTEL,
120 .devices = pci_device_ids,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200121};