Angel Pons | 0612b27 | 2020-04-05 15:46:56 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 2 | |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 3 | #include <device/pci_ops.h> |
Subrata Banik | e62836b | 2018-05-07 16:27:51 +0530 | [diff] [blame] | 4 | #include <console/console.h> |
| 5 | #include <device/device.h> |
| 6 | #include <device/i2c_simple.h> |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 7 | #include <device/pci.h> |
| 8 | #include <device/pci_def.h> |
| 9 | #include <device/pci_ids.h> |
Chris Ching | b8dc63b | 2017-12-06 14:26:15 -0700 | [diff] [blame] | 10 | #include <drivers/i2c/designware/dw_i2c.h> |
Kyösti Mälkki | 32d47eb | 2019-09-28 00:00:30 +0300 | [diff] [blame] | 11 | #include <intelblocks/cfg.h> |
Subrata Banik | e62836b | 2018-05-07 16:27:51 +0530 | [diff] [blame] | 12 | #include <intelblocks/lpss.h> |
| 13 | #include <soc/iomap.h> |
| 14 | #include <soc/pci_devs.h> |
| 15 | |
Furquan Shaikh | 00296ea | 2020-04-24 21:33:02 -0700 | [diff] [blame] | 16 | int dw_i2c_soc_dev_to_bus(const struct device *dev) |
Subrata Banik | e62836b | 2018-05-07 16:27:51 +0530 | [diff] [blame] | 17 | { |
| 18 | pci_devfn_t devfn = dev->path.pci.devfn; |
| 19 | return dw_i2c_soc_devfn_to_bus(devfn); |
| 20 | } |
| 21 | |
| 22 | /* Getting I2C bus configuration from devicetree config */ |
| 23 | const struct dw_i2c_bus_config *dw_i2c_get_soc_cfg(unsigned int bus) |
| 24 | { |
| 25 | const struct soc_intel_common_config *common_config; |
| 26 | common_config = chip_get_common_soc_structure(); |
| 27 | |
| 28 | return &common_config->i2c[bus]; |
| 29 | } |
| 30 | |
| 31 | /* Get base address for early init of I2C controllers. */ |
| 32 | uintptr_t dw_i2c_get_soc_early_base(unsigned int bus) |
| 33 | { |
| 34 | return EARLY_I2C_BASE(bus); |
| 35 | } |
| 36 | |
Subrata Banik | 42c44c2 | 2019-05-15 20:27:04 +0530 | [diff] [blame] | 37 | #if !ENV_PAYLOAD_LOADER |
Subrata Banik | e62836b | 2018-05-07 16:27:51 +0530 | [diff] [blame] | 38 | static int lpss_i2c_early_init_bus(unsigned int bus) |
| 39 | { |
| 40 | const struct dw_i2c_bus_config *config; |
Subrata Banik | e62836b | 2018-05-07 16:27:51 +0530 | [diff] [blame] | 41 | pci_devfn_t dev; |
| 42 | int devfn; |
| 43 | uintptr_t base; |
| 44 | |
| 45 | /* Find the PCI device for this bus controller */ |
| 46 | devfn = dw_i2c_soc_bus_to_devfn(bus); |
| 47 | if (devfn < 0) { |
| 48 | printk(BIOS_ERR, "I2C%u device not found\n", bus); |
| 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | /* Look up the controller device in the devicetree */ |
| 53 | dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn)); |
Subrata Banik | e62836b | 2018-05-07 16:27:51 +0530 | [diff] [blame] | 54 | |
| 55 | /* Skip if not enabled for early init */ |
| 56 | config = dw_i2c_get_soc_cfg(bus); |
| 57 | if (!config || !config->early_init) { |
| 58 | printk(BIOS_DEBUG, "I2C%u not enabled for early init\n", bus); |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | /* Prepare early base address for access before memory */ |
| 63 | base = dw_i2c_get_soc_early_base(bus); |
| 64 | pci_write_config32(dev, PCI_BASE_ADDRESS_0, base); |
Elyes HAOUAS | 2ec1c13 | 2020-04-29 09:57:05 +0200 | [diff] [blame] | 65 | pci_write_config16(dev, PCI_COMMAND, |
Subrata Banik | e62836b | 2018-05-07 16:27:51 +0530 | [diff] [blame] | 66 | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); |
| 67 | |
| 68 | /* Take device out of reset */ |
| 69 | lpss_reset_release(base); |
| 70 | |
Aamir Bohra | f5202a6 | 2019-07-12 14:37:55 +0530 | [diff] [blame] | 71 | /* Ensure controller is in D0 state */ |
Furquan Shaikh | fb29ca0 | 2021-01-02 00:03:00 -0800 | [diff] [blame] | 72 | lpss_set_power_state(dev, STATE_D0); |
Aamir Bohra | f5202a6 | 2019-07-12 14:37:55 +0530 | [diff] [blame] | 73 | |
Subrata Banik | e62836b | 2018-05-07 16:27:51 +0530 | [diff] [blame] | 74 | /* Initialize the controller */ |
| 75 | if (dw_i2c_init(bus, config) < 0) { |
| 76 | printk(BIOS_ERR, "I2C%u failed to initialize\n", bus); |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | uintptr_t dw_i2c_base_address(unsigned int bus) |
| 84 | { |
| 85 | int devfn; |
| 86 | pci_devfn_t dev; |
| 87 | uintptr_t base; |
| 88 | |
| 89 | /* Find device+function for this controller */ |
| 90 | devfn = dw_i2c_soc_bus_to_devfn(bus); |
| 91 | if (devfn < 0) |
| 92 | return (uintptr_t)NULL; |
| 93 | |
| 94 | /* Form a PCI address for this device */ |
| 95 | dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn)); |
| 96 | |
| 97 | /* Read the first base address for this device */ |
| 98 | base = ALIGN_DOWN(pci_read_config32(dev, PCI_BASE_ADDRESS_0), 16); |
| 99 | |
| 100 | /* Attempt to initialize bus if base is not set yet */ |
| 101 | if (!base && !lpss_i2c_early_init_bus(bus)) |
| 102 | base = ALIGN_DOWN(pci_read_config32(dev, PCI_BASE_ADDRESS_0), |
| 103 | 16); |
| 104 | return base; |
| 105 | } |
| 106 | #else |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 107 | |
Chris Ching | b8dc63b | 2017-12-06 14:26:15 -0700 | [diff] [blame] | 108 | uintptr_t dw_i2c_base_address(unsigned int bus) |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 109 | { |
| 110 | int devfn; |
| 111 | struct device *dev; |
| 112 | struct resource *res; |
| 113 | |
| 114 | /* bus -> devfn */ |
Aaron Durbin | 9aee819 | 2018-01-22 20:29:25 -0700 | [diff] [blame] | 115 | devfn = dw_i2c_soc_bus_to_devfn(bus); |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 116 | |
| 117 | if (devfn < 0) |
| 118 | return (uintptr_t)NULL; |
| 119 | |
| 120 | /* devfn -> dev */ |
Kyösti Mälkki | e737755 | 2018-06-21 16:20:55 +0300 | [diff] [blame] | 121 | dev = pcidev_path_on_root(devfn); |
Furquan Shaikh | d629e433 | 2017-06-09 17:54:00 -0700 | [diff] [blame] | 122 | if (!dev || !dev->enabled) |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 123 | return (uintptr_t)NULL; |
| 124 | |
| 125 | /* dev -> bar0 */ |
| 126 | res = find_resource(dev, PCI_BASE_ADDRESS_0); |
| 127 | if (res) |
| 128 | return res->base; |
| 129 | |
| 130 | return (uintptr_t)NULL; |
| 131 | } |
| 132 | |
Subrata Banik | 7d8c0c2 | 2018-09-27 19:27:39 +0530 | [diff] [blame] | 133 | /* |
| 134 | * This function ensures that the device is actually out of reset and |
| 135 | * its ready for initialization sequence. |
| 136 | */ |
| 137 | static void dw_i2c_device_init(struct device *dev) |
| 138 | { |
| 139 | uintptr_t base_address; |
| 140 | int bus = dw_i2c_soc_dev_to_bus(dev); |
| 141 | |
| 142 | if (bus < 0) |
| 143 | return; |
| 144 | |
| 145 | base_address = dw_i2c_base_address(bus); |
| 146 | if (!base_address) |
| 147 | return; |
| 148 | |
Aamir Bohra | f5202a6 | 2019-07-12 14:37:55 +0530 | [diff] [blame] | 149 | /* Ensure controller is in D0 state */ |
Furquan Shaikh | fb29ca0 | 2021-01-02 00:03:00 -0800 | [diff] [blame] | 150 | lpss_set_power_state(PCI_BDF(dev), STATE_D0); |
Aamir Bohra | f5202a6 | 2019-07-12 14:37:55 +0530 | [diff] [blame] | 151 | |
Subrata Banik | 7d8c0c2 | 2018-09-27 19:27:39 +0530 | [diff] [blame] | 152 | /* Take device out of reset if its not done before */ |
| 153 | if (lpss_is_controller_in_reset(base_address)) |
| 154 | lpss_reset_release(base_address); |
| 155 | |
| 156 | dw_i2c_dev_init(dev); |
| 157 | } |
| 158 | |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 159 | static struct device_operations i2c_dev_ops = { |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 160 | .read_resources = pci_dev_read_resources, |
| 161 | .set_resources = pci_dev_set_resources, |
| 162 | .enable_resources = pci_dev_enable_resources, |
Karthikeyan Ramasubramanian | 7524b5e | 2020-10-02 00:35:14 -0600 | [diff] [blame] | 163 | .scan_bus = scan_static_bus, |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 164 | .ops_i2c_bus = &dw_i2c_bus_ops, |
| 165 | .ops_pci = &pci_dev_ops_pci, |
| 166 | .init = dw_i2c_device_init, |
Karthikeyan Ramasubramanian | 0e971e1 | 2020-01-09 11:32:16 -0700 | [diff] [blame] | 167 | #if CONFIG(HAVE_ACPI_TABLES) |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 168 | .acpi_fill_ssdt = dw_i2c_acpi_fill_ssdt, |
Karthikeyan Ramasubramanian | 0e971e1 | 2020-01-09 11:32:16 -0700 | [diff] [blame] | 169 | #endif |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | static const unsigned short pci_device_ids[] = { |
| 173 | PCI_DEVICE_ID_INTEL_SPT_I2C0, |
| 174 | PCI_DEVICE_ID_INTEL_SPT_I2C1, |
| 175 | PCI_DEVICE_ID_INTEL_SPT_I2C2, |
| 176 | PCI_DEVICE_ID_INTEL_SPT_I2C3, |
| 177 | PCI_DEVICE_ID_INTEL_SPT_I2C4, |
| 178 | PCI_DEVICE_ID_INTEL_SPT_I2C5, |
Angel Pons | f530e36 | 2021-04-27 10:20:04 +0200 | [diff] [blame^] | 179 | PCI_DEVICE_ID_INTEL_UPT_H_I2C0, |
| 180 | PCI_DEVICE_ID_INTEL_UPT_H_I2C1, |
| 181 | PCI_DEVICE_ID_INTEL_UPT_H_I2C2, |
| 182 | PCI_DEVICE_ID_INTEL_UPT_H_I2C3, |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 183 | PCI_DEVICE_ID_INTEL_APL_I2C0, |
| 184 | PCI_DEVICE_ID_INTEL_APL_I2C1, |
| 185 | PCI_DEVICE_ID_INTEL_APL_I2C2, |
| 186 | PCI_DEVICE_ID_INTEL_APL_I2C3, |
| 187 | PCI_DEVICE_ID_INTEL_APL_I2C4, |
| 188 | PCI_DEVICE_ID_INTEL_APL_I2C5, |
| 189 | PCI_DEVICE_ID_INTEL_APL_I2C6, |
| 190 | PCI_DEVICE_ID_INTEL_APL_I2C7, |
Lijian Zhao | bbedef9 | 2017-07-29 16:38:38 -0700 | [diff] [blame] | 191 | PCI_DEVICE_ID_INTEL_CNL_I2C0, |
| 192 | PCI_DEVICE_ID_INTEL_CNL_I2C1, |
| 193 | PCI_DEVICE_ID_INTEL_CNL_I2C2, |
| 194 | PCI_DEVICE_ID_INTEL_CNL_I2C3, |
| 195 | PCI_DEVICE_ID_INTEL_CNL_I2C4, |
| 196 | PCI_DEVICE_ID_INTEL_CNL_I2C5, |
Ravi Sarawadi | 3038e9b | 2017-05-18 16:00:35 -0700 | [diff] [blame] | 197 | PCI_DEVICE_ID_INTEL_GLK_I2C0, |
| 198 | PCI_DEVICE_ID_INTEL_GLK_I2C1, |
| 199 | PCI_DEVICE_ID_INTEL_GLK_I2C2, |
| 200 | PCI_DEVICE_ID_INTEL_GLK_I2C3, |
| 201 | PCI_DEVICE_ID_INTEL_GLK_I2C4, |
| 202 | PCI_DEVICE_ID_INTEL_GLK_I2C5, |
| 203 | PCI_DEVICE_ID_INTEL_GLK_I2C6, |
| 204 | PCI_DEVICE_ID_INTEL_GLK_I2C7, |
praveen hodagatta pranesh | e26c4a4 | 2018-09-20 03:49:45 +0800 | [diff] [blame] | 205 | PCI_DEVICE_ID_INTEL_CNP_H_I2C0, |
| 206 | PCI_DEVICE_ID_INTEL_CNP_H_I2C1, |
| 207 | PCI_DEVICE_ID_INTEL_CNP_H_I2C2, |
| 208 | PCI_DEVICE_ID_INTEL_CNP_H_I2C3, |
Aamir Bohra | 9eac039 | 2018-06-30 12:07:04 +0530 | [diff] [blame] | 209 | PCI_DEVICE_ID_INTEL_ICP_I2C0, |
| 210 | PCI_DEVICE_ID_INTEL_ICP_I2C1, |
| 211 | PCI_DEVICE_ID_INTEL_ICP_I2C2, |
| 212 | PCI_DEVICE_ID_INTEL_ICP_I2C3, |
| 213 | PCI_DEVICE_ID_INTEL_ICP_I2C4, |
| 214 | PCI_DEVICE_ID_INTEL_ICP_I2C5, |
Ronak Kanabar | da7ffb48 | 2019-02-05 01:51:13 +0530 | [diff] [blame] | 215 | PCI_DEVICE_ID_INTEL_CMP_I2C0, |
| 216 | PCI_DEVICE_ID_INTEL_CMP_I2C1, |
| 217 | PCI_DEVICE_ID_INTEL_CMP_I2C2, |
| 218 | PCI_DEVICE_ID_INTEL_CMP_I2C3, |
| 219 | PCI_DEVICE_ID_INTEL_CMP_I2C4, |
| 220 | PCI_DEVICE_ID_INTEL_CMP_I2C5, |
Gaggery Tsai | 12a651c | 2019-12-05 11:23:20 -0800 | [diff] [blame] | 221 | PCI_DEVICE_ID_INTEL_CMP_H_I2C0, |
| 222 | PCI_DEVICE_ID_INTEL_CMP_H_I2C1, |
| 223 | PCI_DEVICE_ID_INTEL_CMP_H_I2C2, |
| 224 | PCI_DEVICE_ID_INTEL_CMP_H_I2C3, |
Ravi Sarawadi | 6b5bf40 | 2019-10-21 22:25:04 -0700 | [diff] [blame] | 225 | PCI_DEVICE_ID_INTEL_TGP_I2C0, |
| 226 | PCI_DEVICE_ID_INTEL_TGP_I2C1, |
| 227 | PCI_DEVICE_ID_INTEL_TGP_I2C2, |
| 228 | PCI_DEVICE_ID_INTEL_TGP_I2C3, |
| 229 | PCI_DEVICE_ID_INTEL_TGP_I2C4, |
| 230 | PCI_DEVICE_ID_INTEL_TGP_I2C5, |
| 231 | PCI_DEVICE_ID_INTEL_TGP_I2C6, |
| 232 | PCI_DEVICE_ID_INTEL_TGP_I2C7, |
Tan, Lean Sheng | 2613609 | 2020-01-20 19:13:56 -0800 | [diff] [blame] | 233 | PCI_DEVICE_ID_INTEL_MCC_I2C0, |
| 234 | PCI_DEVICE_ID_INTEL_MCC_I2C1, |
| 235 | PCI_DEVICE_ID_INTEL_MCC_I2C2, |
| 236 | PCI_DEVICE_ID_INTEL_MCC_I2C3, |
| 237 | PCI_DEVICE_ID_INTEL_MCC_I2C4, |
| 238 | PCI_DEVICE_ID_INTEL_MCC_I2C5, |
| 239 | PCI_DEVICE_ID_INTEL_MCC_I2C6, |
| 240 | PCI_DEVICE_ID_INTEL_MCC_I2C7, |
Meera Ravindranath | 3f4af0d | 2020-02-12 16:01:22 +0530 | [diff] [blame] | 241 | PCI_DEVICE_ID_INTEL_JSP_I2C0, |
| 242 | PCI_DEVICE_ID_INTEL_JSP_I2C1, |
| 243 | PCI_DEVICE_ID_INTEL_JSP_I2C2, |
| 244 | PCI_DEVICE_ID_INTEL_JSP_I2C3, |
| 245 | PCI_DEVICE_ID_INTEL_JSP_I2C4, |
| 246 | PCI_DEVICE_ID_INTEL_JSP_I2C5, |
Subrata Banik | f672f7f | 2020-08-03 14:29:25 +0530 | [diff] [blame] | 247 | PCI_DEVICE_ID_INTEL_ADP_P_I2C0, |
| 248 | PCI_DEVICE_ID_INTEL_ADP_P_I2C1, |
| 249 | PCI_DEVICE_ID_INTEL_ADP_P_I2C2, |
| 250 | PCI_DEVICE_ID_INTEL_ADP_P_I2C3, |
| 251 | PCI_DEVICE_ID_INTEL_ADP_P_I2C4, |
| 252 | PCI_DEVICE_ID_INTEL_ADP_P_I2C5, |
| 253 | PCI_DEVICE_ID_INTEL_ADP_S_I2C0, |
| 254 | PCI_DEVICE_ID_INTEL_ADP_S_I2C1, |
| 255 | PCI_DEVICE_ID_INTEL_ADP_S_I2C2, |
| 256 | PCI_DEVICE_ID_INTEL_ADP_S_I2C3, |
| 257 | PCI_DEVICE_ID_INTEL_ADP_S_I2C4, |
| 258 | PCI_DEVICE_ID_INTEL_ADP_S_I2C5, |
Varshit Pandya | f4d98fdd2 | 2021-01-17 18:39:29 +0530 | [diff] [blame] | 259 | PCI_DEVICE_ID_INTEL_ADP_M_I2C0, |
| 260 | PCI_DEVICE_ID_INTEL_ADP_M_I2C1, |
| 261 | PCI_DEVICE_ID_INTEL_ADP_M_I2C2, |
| 262 | PCI_DEVICE_ID_INTEL_ADP_M_I2C3, |
| 263 | PCI_DEVICE_ID_INTEL_ADP_M_I2C4, |
| 264 | PCI_DEVICE_ID_INTEL_ADP_M_I2C5, |
Ravi Sarawadi | 3038e9b | 2017-05-18 16:00:35 -0700 | [diff] [blame] | 265 | 0, |
Rizwan Qureshi | ae6a4b6 | 2017-04-26 21:06:35 +0530 | [diff] [blame] | 266 | }; |
| 267 | |
| 268 | static const struct pci_driver pch_i2c __pci_driver = { |
| 269 | .ops = &i2c_dev_ops, |
| 270 | .vendor = PCI_VENDOR_ID_INTEL, |
| 271 | .devices = pci_device_ids, |
| 272 | }; |
Subrata Banik | e62836b | 2018-05-07 16:27:51 +0530 | [diff] [blame] | 273 | #endif |