Angel Pons | 182dbde | 2020-04-02 23:49:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 2 | |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 3 | #include <device/device.h> |
| 4 | #include <device/path.h> |
| 5 | #include <device/smbus.h> |
| 6 | #include <device/pci.h> |
Elyes HAOUAS | a4dd33c | 2020-08-11 09:39:43 +0200 | [diff] [blame] | 7 | #include <device/pci_def.h> |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 8 | #include <device/pci_ids.h> |
| 9 | #include <device/pci_ops.h> |
Kyösti Mälkki | 1cae454 | 2020-01-06 12:31:34 +0200 | [diff] [blame] | 10 | #include <device/smbus_host.h> |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 11 | #include "pch.h" |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 12 | |
Elyes HAOUAS | be84140 | 2018-05-13 13:40:39 +0200 | [diff] [blame] | 13 | static void pch_smbus_init(struct device *dev) |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 14 | { |
| 15 | struct resource *res; |
| 16 | u16 reg16; |
| 17 | |
| 18 | /* Enable clock gating */ |
| 19 | reg16 = pci_read_config32(dev, 0x80); |
Angel Pons | 77f340a | 2020-10-17 18:39:04 +0200 | [diff] [blame] | 20 | reg16 &= ~((1 << 8) | (1 << 10) | (1 << 12) | (1 << 14)); |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 21 | pci_write_config32(dev, 0x80, reg16); |
| 22 | |
| 23 | /* Set Receive Slave Address */ |
| 24 | res = find_resource(dev, PCI_BASE_ADDRESS_4); |
| 25 | if (res) |
Kyösti Mälkki | 73451fd | 2020-01-06 19:00:31 +0200 | [diff] [blame] | 26 | smbus_set_slave_addr(res->base, SMBUS_SLAVE_ADDR); |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 27 | } |
| 28 | |
Elyes HAOUAS | be84140 | 2018-05-13 13:40:39 +0200 | [diff] [blame] | 29 | static int lsmbus_read_byte(struct device *dev, u8 address) |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 30 | { |
| 31 | u16 device; |
| 32 | struct resource *res; |
| 33 | struct bus *pbus; |
| 34 | |
| 35 | device = dev->path.i2c.device; |
| 36 | pbus = get_pbus_smbus(dev); |
Elyes HAOUAS | a4dd33c | 2020-08-11 09:39:43 +0200 | [diff] [blame] | 37 | res = find_resource(pbus->dev, PCI_BASE_ADDRESS_4); |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 38 | |
| 39 | return do_smbus_read_byte(res->base, device, address); |
| 40 | } |
| 41 | |
Angel Pons | bb19d39 | 2020-12-05 20:23:40 +0100 | [diff] [blame] | 42 | static int lsmbus_write_byte(struct device *dev, u8 address, u8 data) |
Vladimir Serbinenko | 5f20dbf | 2014-01-27 23:57:44 +0100 | [diff] [blame] | 43 | { |
| 44 | u16 device; |
| 45 | struct resource *res; |
| 46 | struct bus *pbus; |
| 47 | |
| 48 | device = dev->path.i2c.device; |
| 49 | pbus = get_pbus_smbus(dev); |
Elyes HAOUAS | a4dd33c | 2020-08-11 09:39:43 +0200 | [diff] [blame] | 50 | res = find_resource(pbus->dev, PCI_BASE_ADDRESS_4); |
Angel Pons | bb19d39 | 2020-12-05 20:23:40 +0100 | [diff] [blame] | 51 | return do_smbus_write_byte(res->base, device, address, data); |
Vladimir Serbinenko | 5f20dbf | 2014-01-27 23:57:44 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Angel Pons | 244cf7d | 2020-12-05 20:25:27 +0100 | [diff] [blame] | 54 | static int lsmbus_block_write(struct device *dev, u8 cmd, u8 bytes, const u8 *buf) |
| 55 | { |
| 56 | u16 device; |
| 57 | struct resource *res; |
| 58 | struct bus *pbus; |
| 59 | |
| 60 | device = dev->path.i2c.device; |
| 61 | pbus = get_pbus_smbus(dev); |
| 62 | res = find_resource(pbus->dev, PCI_BASE_ADDRESS_4); |
| 63 | return do_smbus_block_write(res->base, device, cmd, bytes, buf); |
| 64 | } |
| 65 | |
| 66 | static int lsmbus_block_read(struct device *dev, u8 cmd, u8 bytes, u8 *buf) |
| 67 | { |
| 68 | u16 device; |
| 69 | struct resource *res; |
| 70 | struct bus *pbus; |
| 71 | |
| 72 | device = dev->path.i2c.device; |
| 73 | pbus = get_pbus_smbus(dev); |
| 74 | res = find_resource(pbus->dev, PCI_BASE_ADDRESS_4); |
| 75 | return do_smbus_block_read(res->base, device, cmd, bytes, buf); |
| 76 | } |
| 77 | |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 78 | static struct smbus_bus_operations lops_smbus_bus = { |
| 79 | .read_byte = lsmbus_read_byte, |
Vladimir Serbinenko | 5f20dbf | 2014-01-27 23:57:44 +0100 | [diff] [blame] | 80 | .write_byte = lsmbus_write_byte, |
Angel Pons | 244cf7d | 2020-12-05 20:25:27 +0100 | [diff] [blame] | 81 | .block_read = lsmbus_block_read, |
| 82 | .block_write = lsmbus_block_write, |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 83 | }; |
| 84 | |
Elyes HAOUAS | be84140 | 2018-05-13 13:40:39 +0200 | [diff] [blame] | 85 | static void smbus_read_resources(struct device *dev) |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 86 | { |
| 87 | struct resource *res = new_resource(dev, PCI_BASE_ADDRESS_4); |
Angel Pons | b21bffa | 2020-07-03 01:02:28 +0200 | [diff] [blame] | 88 | res->base = CONFIG_FIXED_SMBUS_IO_BASE; |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 89 | res->size = 32; |
| 90 | res->limit = res->base + res->size - 1; |
| 91 | res->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_RESERVE | |
| 92 | IORESOURCE_STORED | IORESOURCE_ASSIGNED; |
| 93 | |
| 94 | /* Also add MMIO resource */ |
| 95 | res = pci_get_resource(dev, PCI_BASE_ADDRESS_0); |
| 96 | } |
| 97 | |
| 98 | static struct device_operations smbus_ops = { |
| 99 | .read_resources = smbus_read_resources, |
| 100 | .set_resources = pci_dev_set_resources, |
| 101 | .enable_resources = pci_dev_enable_resources, |
Kyösti Mälkki | d0e212c | 2015-02-26 20:47:47 +0200 | [diff] [blame] | 102 | .scan_bus = scan_smbus, |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 103 | .init = pch_smbus_init, |
| 104 | .ops_smbus_bus = &lops_smbus_bus, |
Angel Pons | 1fc0edd | 2020-05-31 00:03:28 +0200 | [diff] [blame] | 105 | .ops_pci = &pci_dev_ops_pci, |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 106 | }; |
| 107 | |
Felix Singer | 838fbc7 | 2019-11-21 21:23:32 +0100 | [diff] [blame] | 108 | static const unsigned short pci_device_ids[] = { |
| 109 | 0x1c22, |
| 110 | 0x1e22, |
| 111 | PCI_DID_INTEL_IBEXPEAK_SMBUS, |
| 112 | 0 |
| 113 | }; |
Vladimir Serbinenko | 888d559 | 2013-11-13 17:53:38 +0100 | [diff] [blame] | 114 | |
| 115 | static const struct pci_driver pch_smbus __pci_driver = { |
| 116 | .ops = &smbus_ops, |
| 117 | .vendor = PCI_VENDOR_ID_INTEL, |
| 118 | .devices = pci_device_ids, |
| 119 | }; |