Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2017 Intel Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <arch/acpigen.h> |
Nico Huber | 0f2dd1e | 2017-08-01 14:02:40 +0200 | [diff] [blame] | 17 | #include <device/i2c_bus.h> |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 18 | #include <device/pci.h> |
| 19 | #include <device/pci_def.h> |
| 20 | #include <device/pci_ids.h> |
Chris Ching | b8dc63b | 2017-12-06 14:26:15 -0700 | [diff] [blame] | 21 | #include <drivers/i2c/designware/dw_i2c.h> |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 22 | |
Chris Ching | b8dc63b | 2017-12-06 14:26:15 -0700 | [diff] [blame] | 23 | uintptr_t dw_i2c_base_address(unsigned int bus) |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 24 | { |
| 25 | int devfn; |
| 26 | struct device *dev; |
| 27 | struct resource *res; |
| 28 | |
| 29 | /* bus -> devfn */ |
Aaron Durbin | 9aee819 | 2018-01-22 20:29:25 -0700 | [diff] [blame^] | 30 | devfn = dw_i2c_soc_bus_to_devfn(bus); |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 31 | |
| 32 | if (devfn < 0) |
| 33 | return (uintptr_t)NULL; |
| 34 | |
| 35 | /* devfn -> dev */ |
| 36 | dev = dev_find_slot(0, devfn); |
Furquan Shaikh | d629e433 | 2017-06-09 17:54:00 -0700 | [diff] [blame] | 37 | if (!dev || !dev->enabled) |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 38 | return (uintptr_t)NULL; |
| 39 | |
| 40 | /* dev -> bar0 */ |
| 41 | res = find_resource(dev, PCI_BASE_ADDRESS_0); |
| 42 | if (res) |
| 43 | return res->base; |
| 44 | |
| 45 | return (uintptr_t)NULL; |
| 46 | } |
| 47 | |
Chris Ching | b8dc63b | 2017-12-06 14:26:15 -0700 | [diff] [blame] | 48 | static int lpss_i2c_dev_to_bus(struct device *dev) |
| 49 | { |
| 50 | pci_devfn_t devfn = dev->path.pci.devfn; |
Aaron Durbin | 9aee819 | 2018-01-22 20:29:25 -0700 | [diff] [blame^] | 51 | return dw_i2c_soc_devfn_to_bus(devfn); |
Chris Ching | b8dc63b | 2017-12-06 14:26:15 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 54 | /* |
| 55 | * Write ACPI object to describe speed configuration. |
| 56 | * |
| 57 | * ACPI Object: Name ("xxxx", Package () { scl_lcnt, scl_hcnt, sda_hold } |
| 58 | * |
| 59 | * SSCN: I2C_SPEED_STANDARD |
| 60 | * FMCN: I2C_SPEED_FAST |
| 61 | * FPCN: I2C_SPEED_FAST_PLUS |
| 62 | * HSCN: I2C_SPEED_HIGH |
| 63 | */ |
| 64 | static void lpss_i2c_acpi_write_speed_config( |
Chris Ching | b8dc63b | 2017-12-06 14:26:15 -0700 | [diff] [blame] | 65 | const struct dw_i2c_speed_config *config) |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 66 | { |
| 67 | if (!config) |
| 68 | return; |
| 69 | if (!config->scl_lcnt && !config->scl_hcnt && !config->sda_hold) |
| 70 | return; |
| 71 | |
| 72 | if (config->speed >= I2C_SPEED_HIGH) |
| 73 | acpigen_write_name("HSCN"); |
| 74 | else if (config->speed >= I2C_SPEED_FAST_PLUS) |
| 75 | acpigen_write_name("FPCN"); |
| 76 | else if (config->speed >= I2C_SPEED_FAST) |
| 77 | acpigen_write_name("FMCN"); |
| 78 | else |
| 79 | acpigen_write_name("SSCN"); |
| 80 | |
| 81 | /* Package () { scl_lcnt, scl_hcnt, sda_hold } */ |
| 82 | acpigen_write_package(3); |
| 83 | acpigen_write_word(config->scl_hcnt); |
| 84 | acpigen_write_word(config->scl_lcnt); |
| 85 | acpigen_write_dword(config->sda_hold); |
| 86 | acpigen_pop_len(); |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * The device should already be enabled and out of reset, |
| 91 | * either from early init in coreboot or SiliconInit in FSP. |
| 92 | */ |
| 93 | static void lpss_i2c_dev_init(struct device *dev) |
| 94 | { |
Chris Ching | b8dc63b | 2017-12-06 14:26:15 -0700 | [diff] [blame] | 95 | const struct dw_i2c_bus_config *config; |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 96 | int bus = lpss_i2c_dev_to_bus(dev); |
| 97 | |
Rizwan Qureshi | 7f72c64 | 2017-10-04 18:53:19 +0530 | [diff] [blame] | 98 | if (bus < 0) |
| 99 | return; |
| 100 | |
Aaron Durbin | 9aee819 | 2018-01-22 20:29:25 -0700 | [diff] [blame^] | 101 | config = dw_i2c_get_soc_cfg(bus, dev); |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 102 | |
Rizwan Qureshi | 7f72c64 | 2017-10-04 18:53:19 +0530 | [diff] [blame] | 103 | if (!config) |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 104 | return; |
| 105 | |
Chris Ching | b8dc63b | 2017-12-06 14:26:15 -0700 | [diff] [blame] | 106 | dw_i2c_init(bus, config); |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Generate I2C timing information into the SSDT for the OS driver to consume, |
| 111 | * optionally applying override values provided by the caller. |
| 112 | */ |
| 113 | static void lpss_i2c_acpi_fill_ssdt(struct device *dev) |
| 114 | { |
Chris Ching | b8dc63b | 2017-12-06 14:26:15 -0700 | [diff] [blame] | 115 | const struct dw_i2c_bus_config *bcfg; |
| 116 | uintptr_t dw_i2c_addr; |
| 117 | struct dw_i2c_speed_config sgen; |
| 118 | enum i2c_speed speeds[DW_I2C_SPEED_CONFIG_COUNT] = { |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 119 | I2C_SPEED_STANDARD, |
| 120 | I2C_SPEED_FAST, |
| 121 | I2C_SPEED_FAST_PLUS, |
| 122 | I2C_SPEED_HIGH, |
| 123 | }; |
Furquan Shaikh | d629e433 | 2017-06-09 17:54:00 -0700 | [diff] [blame] | 124 | int i, bus; |
Shaunak Saha | 5a44176 | 2017-12-12 10:21:50 -0800 | [diff] [blame] | 125 | const char *path; |
Furquan Shaikh | d629e433 | 2017-06-09 17:54:00 -0700 | [diff] [blame] | 126 | |
| 127 | if (!dev->enabled) |
| 128 | return; |
| 129 | |
| 130 | bus = lpss_i2c_dev_to_bus(dev); |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 131 | |
Rizwan Qureshi | 7f72c64 | 2017-10-04 18:53:19 +0530 | [diff] [blame] | 132 | if (bus < 0) |
| 133 | return; |
| 134 | |
Aaron Durbin | 9aee819 | 2018-01-22 20:29:25 -0700 | [diff] [blame^] | 135 | bcfg = dw_i2c_get_soc_cfg(bus, dev); |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 136 | |
| 137 | if (!bcfg) |
| 138 | return; |
| 139 | |
Chris Ching | b8dc63b | 2017-12-06 14:26:15 -0700 | [diff] [blame] | 140 | dw_i2c_addr = dw_i2c_base_address(bus); |
| 141 | if (!dw_i2c_addr) |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 142 | return; |
| 143 | |
Shaunak Saha | 5a44176 | 2017-12-12 10:21:50 -0800 | [diff] [blame] | 144 | path = acpi_device_path(dev); |
| 145 | if (!path) |
| 146 | return; |
| 147 | |
| 148 | acpigen_write_scope(path); |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 149 | |
| 150 | /* Report timing values for the OS driver */ |
Chris Ching | b8dc63b | 2017-12-06 14:26:15 -0700 | [diff] [blame] | 151 | for (i = 0; i < DW_I2C_SPEED_CONFIG_COUNT; i++) { |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 152 | /* Generate speed config. */ |
Chris Ching | b8dc63b | 2017-12-06 14:26:15 -0700 | [diff] [blame] | 153 | if (dw_i2c_gen_speed_config(dw_i2c_addr, speeds[i], bcfg, |
| 154 | &sgen) < 0) |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 155 | continue; |
| 156 | |
| 157 | /* Generate ACPI based on selected speed config */ |
| 158 | lpss_i2c_acpi_write_speed_config(&sgen); |
| 159 | } |
| 160 | |
| 161 | acpigen_pop_len(); |
| 162 | } |
| 163 | |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 164 | static int lpss_i2c_dev_transfer(struct device *dev, |
| 165 | const struct i2c_msg *msg, size_t count) |
| 166 | { |
Chris Ching | b8dc63b | 2017-12-06 14:26:15 -0700 | [diff] [blame] | 167 | return dw_i2c_transfer(lpss_i2c_dev_to_bus(dev), msg, count); |
Nico Huber | 5817386 | 2017-08-01 17:09:35 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static const struct i2c_bus_operations i2c_bus_ops = { |
| 171 | .transfer = lpss_i2c_dev_transfer, |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | static struct device_operations i2c_dev_ops = { |
| 175 | .read_resources = &pci_dev_read_resources, |
| 176 | .set_resources = &pci_dev_set_resources, |
| 177 | .enable_resources = &pci_dev_enable_resources, |
| 178 | .scan_bus = &scan_smbus, |
| 179 | .ops_i2c_bus = &i2c_bus_ops, |
Subrata Banik | 6bbc91a | 2017-12-07 14:55:51 +0530 | [diff] [blame] | 180 | .ops_pci = &pci_dev_ops_pci, |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 181 | .init = &lpss_i2c_dev_init, |
| 182 | .acpi_fill_ssdt_generator = &lpss_i2c_acpi_fill_ssdt, |
| 183 | }; |
| 184 | |
| 185 | static const unsigned short pci_device_ids[] = { |
| 186 | PCI_DEVICE_ID_INTEL_SPT_I2C0, |
| 187 | PCI_DEVICE_ID_INTEL_SPT_I2C1, |
| 188 | PCI_DEVICE_ID_INTEL_SPT_I2C2, |
| 189 | PCI_DEVICE_ID_INTEL_SPT_I2C3, |
| 190 | PCI_DEVICE_ID_INTEL_SPT_I2C4, |
| 191 | PCI_DEVICE_ID_INTEL_SPT_I2C5, |
| 192 | PCI_DEVICE_ID_INTEL_APL_I2C0, |
| 193 | PCI_DEVICE_ID_INTEL_APL_I2C1, |
| 194 | PCI_DEVICE_ID_INTEL_APL_I2C2, |
| 195 | PCI_DEVICE_ID_INTEL_APL_I2C3, |
| 196 | PCI_DEVICE_ID_INTEL_APL_I2C4, |
| 197 | PCI_DEVICE_ID_INTEL_APL_I2C5, |
| 198 | PCI_DEVICE_ID_INTEL_APL_I2C6, |
| 199 | PCI_DEVICE_ID_INTEL_APL_I2C7, |
Lijian Zhao | bbedef9 | 2017-07-29 16:38:38 -0700 | [diff] [blame] | 200 | PCI_DEVICE_ID_INTEL_CNL_I2C0, |
| 201 | PCI_DEVICE_ID_INTEL_CNL_I2C1, |
| 202 | PCI_DEVICE_ID_INTEL_CNL_I2C2, |
| 203 | PCI_DEVICE_ID_INTEL_CNL_I2C3, |
| 204 | PCI_DEVICE_ID_INTEL_CNL_I2C4, |
| 205 | PCI_DEVICE_ID_INTEL_CNL_I2C5, |
Ravi Sarawadi | 3038e9b | 2017-05-18 16:00:35 -0700 | [diff] [blame] | 206 | PCI_DEVICE_ID_INTEL_GLK_I2C0, |
| 207 | PCI_DEVICE_ID_INTEL_GLK_I2C1, |
| 208 | PCI_DEVICE_ID_INTEL_GLK_I2C2, |
| 209 | PCI_DEVICE_ID_INTEL_GLK_I2C3, |
| 210 | PCI_DEVICE_ID_INTEL_GLK_I2C4, |
| 211 | PCI_DEVICE_ID_INTEL_GLK_I2C5, |
| 212 | PCI_DEVICE_ID_INTEL_GLK_I2C6, |
| 213 | PCI_DEVICE_ID_INTEL_GLK_I2C7, |
| 214 | 0, |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | static const struct pci_driver pch_i2c __pci_driver = { |
| 218 | .ops = &i2c_dev_ops, |
| 219 | .vendor = PCI_VENDOR_ID_INTEL, |
| 220 | .devices = pci_device_ids, |
| 221 | }; |