Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2009 |
Patrick Delaunay | eae488b | 2022-05-20 18:38:10 +0200 | [diff] [blame] | 4 | * Vipin Kumar, STMicroelectronics, vipin.kumar@st.com. |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 5 | * Copyright 2019 Google Inc |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | fa11fe1 | 2019-12-06 21:41:42 -0700 | [diff] [blame] | 11 | #include <spl.h> |
Simon Glass | 4b0ec52 | 2020-07-07 21:32:29 -0600 | [diff] [blame] | 12 | #include <acpi/acpigen.h> |
| 13 | #include <acpi/acpi_device.h> |
Simon Glass | 070a946 | 2019-12-10 21:28:20 -0700 | [diff] [blame] | 14 | #include <asm/lpss.h> |
Simon Glass | 4b0ec52 | 2020-07-07 21:32:29 -0600 | [diff] [blame] | 15 | #include <dm/acpi.h> |
| 16 | #include <dm/device-internal.h> |
| 17 | #include <dm/uclass-internal.h> |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 18 | #include "designware_i2c.h" |
| 19 | |
Simon Glass | 070a946 | 2019-12-10 21:28:20 -0700 | [diff] [blame] | 20 | enum { |
| 21 | VANILLA = 0, /* standard I2C with no tweaks */ |
| 22 | INTEL_APL, /* Apollo Lake I2C */ |
| 23 | }; |
| 24 | |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 25 | /* BayTrail HCNT/LCNT/SDA hold time */ |
| 26 | static struct dw_scl_sda_cfg byt_config = { |
| 27 | .ss_hcnt = 0x200, |
| 28 | .fs_hcnt = 0x55, |
| 29 | .ss_lcnt = 0x200, |
| 30 | .fs_lcnt = 0x99, |
| 31 | .sda_hold = 0x6, |
| 32 | }; |
| 33 | |
Simon Glass | 070a946 | 2019-12-10 21:28:20 -0700 | [diff] [blame] | 34 | /* Have a weak function for now - possibly should be a new uclass */ |
| 35 | __weak void lpss_reset_release(void *regs); |
| 36 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 37 | static int designware_i2c_pci_of_to_plat(struct udevice *dev) |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 38 | { |
| 39 | struct dw_i2c *priv = dev_get_priv(dev); |
| 40 | |
Simon Glass | fa11fe1 | 2019-12-06 21:41:42 -0700 | [diff] [blame] | 41 | if (spl_phase() < PHASE_SPL) { |
| 42 | u32 base; |
| 43 | int ret; |
| 44 | |
| 45 | ret = dev_read_u32(dev, "early-regs", &base); |
| 46 | if (ret) |
| 47 | return log_msg_ret("early-regs", ret); |
| 48 | |
| 49 | /* Set i2c base address */ |
| 50 | dm_pci_write_config32(dev, PCI_BASE_ADDRESS_0, base); |
| 51 | |
| 52 | /* Enable memory access and bus master */ |
| 53 | dm_pci_write_config32(dev, PCI_COMMAND, PCI_COMMAND_MEMORY | |
| 54 | PCI_COMMAND_MASTER); |
| 55 | } |
| 56 | |
| 57 | if (spl_phase() < PHASE_BOARD_F) { |
| 58 | /* Handle early, fixed mapping into a different address space */ |
| 59 | priv->regs = (struct i2c_regs *)dm_pci_read_bar32(dev, 0); |
| 60 | } else { |
| 61 | priv->regs = (struct i2c_regs *) |
Andrew Scull | 2635e3b | 2022-04-21 16:11:13 +0000 | [diff] [blame] | 62 | dm_pci_map_bar(dev, PCI_BASE_ADDRESS_0, 0, 0, |
| 63 | PCI_REGION_TYPE, PCI_REGION_MEM); |
Simon Glass | fa11fe1 | 2019-12-06 21:41:42 -0700 | [diff] [blame] | 64 | } |
| 65 | if (!priv->regs) |
| 66 | return -EINVAL; |
| 67 | |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 68 | /* Save base address from PCI BAR */ |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 69 | if (IS_ENABLED(CONFIG_INTEL_BAYTRAIL)) |
| 70 | /* Use BayTrail specific timing values */ |
| 71 | priv->scl_sda_cfg = &byt_config; |
Simon Glass | 96fe11c | 2020-01-23 11:48:15 -0700 | [diff] [blame] | 72 | if (dev_get_driver_data(dev) == INTEL_APL) |
| 73 | priv->has_spk_cnt = true; |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 74 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 75 | return designware_i2c_of_to_plat(dev); |
Simon Glass | fa11fe1 | 2019-12-06 21:41:42 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static int designware_i2c_pci_probe(struct udevice *dev) |
| 79 | { |
Simon Glass | 070a946 | 2019-12-10 21:28:20 -0700 | [diff] [blame] | 80 | struct dw_i2c *priv = dev_get_priv(dev); |
| 81 | |
| 82 | if (dev_get_driver_data(dev) == INTEL_APL) { |
| 83 | /* Ensure controller is in D0 state */ |
| 84 | lpss_set_power_state(dev, STATE_D0); |
| 85 | |
| 86 | lpss_reset_release(priv->regs); |
| 87 | } |
| 88 | |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 89 | return designware_i2c_probe(dev); |
| 90 | } |
| 91 | |
| 92 | static int designware_i2c_pci_bind(struct udevice *dev) |
| 93 | { |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 94 | char name[20]; |
| 95 | |
Simon Glass | 7d14ee4 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 96 | if (dev_has_ofnode(dev)) |
Simon Glass | 4b0ec52 | 2020-07-07 21:32:29 -0600 | [diff] [blame] | 97 | return 0; |
| 98 | |
Simon Glass | 16df993 | 2020-12-16 21:20:15 -0700 | [diff] [blame] | 99 | sprintf(name, "i2c_designware#%u", dev_seq(dev)); |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 100 | device_set_name(dev, name); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
Simon Glass | 4b0ec52 | 2020-07-07 21:32:29 -0600 | [diff] [blame] | 105 | /* |
| 106 | * Write ACPI object to describe speed configuration. |
| 107 | * |
| 108 | * ACPI Object: Name ("xxxx", Package () { scl_lcnt, scl_hcnt, sda_hold } |
| 109 | * |
| 110 | * SSCN: I2C_SPEED_STANDARD |
| 111 | * FMCN: I2C_SPEED_FAST |
| 112 | * FPCN: I2C_SPEED_FAST_PLUS |
| 113 | * HSCN: I2C_SPEED_HIGH |
| 114 | */ |
| 115 | static void dw_i2c_acpi_write_speed_config(struct acpi_ctx *ctx, |
| 116 | struct dw_i2c_speed_config *config) |
| 117 | { |
| 118 | switch (config->speed_mode) { |
| 119 | case IC_SPEED_MODE_HIGH: |
| 120 | acpigen_write_name(ctx, "HSCN"); |
| 121 | break; |
| 122 | case IC_SPEED_MODE_FAST_PLUS: |
| 123 | acpigen_write_name(ctx, "FPCN"); |
| 124 | break; |
| 125 | case IC_SPEED_MODE_FAST: |
| 126 | acpigen_write_name(ctx, "FMCN"); |
| 127 | break; |
| 128 | case IC_SPEED_MODE_STANDARD: |
| 129 | default: |
| 130 | acpigen_write_name(ctx, "SSCN"); |
| 131 | } |
| 132 | |
| 133 | /* Package () { scl_lcnt, scl_hcnt, sda_hold } */ |
| 134 | acpigen_write_package(ctx, 3); |
| 135 | acpigen_write_word(ctx, config->scl_hcnt); |
| 136 | acpigen_write_word(ctx, config->scl_lcnt); |
| 137 | acpigen_write_dword(ctx, config->sda_hold); |
| 138 | acpigen_pop_len(ctx); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Generate I2C timing information into the SSDT for the OS driver to consume, |
| 143 | * optionally applying override values provided by the caller. |
| 144 | */ |
| 145 | static int dw_i2c_acpi_fill_ssdt(const struct udevice *dev, |
| 146 | struct acpi_ctx *ctx) |
| 147 | { |
| 148 | struct dw_i2c_speed_config config; |
| 149 | char path[ACPI_PATH_MAX]; |
| 150 | u32 speeds[4]; |
| 151 | uint speed; |
| 152 | int size; |
| 153 | int ret; |
| 154 | |
| 155 | /* If no device-tree node, ignore this since we assume it isn't used */ |
Simon Glass | 7d14ee4 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 156 | if (!dev_has_ofnode(dev)) |
Simon Glass | 4b0ec52 | 2020-07-07 21:32:29 -0600 | [diff] [blame] | 157 | return 0; |
| 158 | |
| 159 | ret = acpi_device_path(dev, path, sizeof(path)); |
| 160 | if (ret) |
| 161 | return log_msg_ret("path", ret); |
| 162 | |
| 163 | size = dev_read_size(dev, "i2c,speeds"); |
| 164 | if (size < 0) |
| 165 | return log_msg_ret("i2c,speeds", -EINVAL); |
| 166 | |
| 167 | size /= sizeof(u32); |
| 168 | if (size > ARRAY_SIZE(speeds)) |
| 169 | return log_msg_ret("array", -E2BIG); |
| 170 | |
| 171 | ret = dev_read_u32_array(dev, "i2c,speeds", speeds, size); |
| 172 | if (ret) |
| 173 | return log_msg_ret("read", -E2BIG); |
| 174 | |
| 175 | speed = dev_read_u32_default(dev, "clock-frequency", 100000); |
| 176 | acpigen_write_scope(ctx, path); |
| 177 | ret = dw_i2c_gen_speed_config(dev, speed, &config); |
| 178 | if (ret) |
| 179 | return log_msg_ret("config", ret); |
| 180 | dw_i2c_acpi_write_speed_config(ctx, &config); |
| 181 | acpigen_pop_len(ctx); |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | struct acpi_ops dw_i2c_acpi_ops = { |
| 187 | .fill_ssdt = dw_i2c_acpi_fill_ssdt, |
| 188 | }; |
| 189 | |
Simon Glass | fa11fe1 | 2019-12-06 21:41:42 -0700 | [diff] [blame] | 190 | static const struct udevice_id designware_i2c_pci_ids[] = { |
| 191 | { .compatible = "snps,designware-i2c-pci" }, |
Simon Glass | 070a946 | 2019-12-10 21:28:20 -0700 | [diff] [blame] | 192 | { .compatible = "intel,apl-i2c", .data = INTEL_APL }, |
Simon Glass | fa11fe1 | 2019-12-06 21:41:42 -0700 | [diff] [blame] | 193 | { } |
| 194 | }; |
| 195 | |
Simon Glass | dfb5bfb | 2021-01-21 13:57:08 -0700 | [diff] [blame] | 196 | DM_DRIVER_ALIAS(i2c_designware_pci, intel_apl_i2c) |
| 197 | |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 198 | U_BOOT_DRIVER(i2c_designware_pci) = { |
| 199 | .name = "i2c_designware_pci", |
| 200 | .id = UCLASS_I2C, |
Simon Glass | fa11fe1 | 2019-12-06 21:41:42 -0700 | [diff] [blame] | 201 | .of_match = designware_i2c_pci_ids, |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 202 | .bind = designware_i2c_pci_bind, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 203 | .of_to_plat = designware_i2c_pci_of_to_plat, |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 204 | .probe = designware_i2c_pci_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 205 | .priv_auto = sizeof(struct dw_i2c), |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 206 | .remove = designware_i2c_remove, |
| 207 | .flags = DM_FLAG_OS_PREPARE, |
| 208 | .ops = &designware_i2c_ops, |
Simon Glass | 4b0ec52 | 2020-07-07 21:32:29 -0600 | [diff] [blame] | 209 | ACPI_OPS_PTR(&dw_i2c_acpi_ops) |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | static struct pci_device_id designware_pci_supported[] = { |
| 213 | /* Intel BayTrail has 7 I2C controller located on the PCI bus */ |
| 214 | { PCI_VDEVICE(INTEL, 0x0f41) }, |
| 215 | { PCI_VDEVICE(INTEL, 0x0f42) }, |
| 216 | { PCI_VDEVICE(INTEL, 0x0f43) }, |
| 217 | { PCI_VDEVICE(INTEL, 0x0f44) }, |
| 218 | { PCI_VDEVICE(INTEL, 0x0f45) }, |
| 219 | { PCI_VDEVICE(INTEL, 0x0f46) }, |
| 220 | { PCI_VDEVICE(INTEL, 0x0f47) }, |
Simon Glass | 070a946 | 2019-12-10 21:28:20 -0700 | [diff] [blame] | 221 | { PCI_VDEVICE(INTEL, 0x5aac), .driver_data = INTEL_APL }, |
| 222 | { PCI_VDEVICE(INTEL, 0x5aae), .driver_data = INTEL_APL }, |
| 223 | { PCI_VDEVICE(INTEL, 0x5ab0), .driver_data = INTEL_APL }, |
| 224 | { PCI_VDEVICE(INTEL, 0x5ab2), .driver_data = INTEL_APL }, |
| 225 | { PCI_VDEVICE(INTEL, 0x5ab4), .driver_data = INTEL_APL }, |
| 226 | { PCI_VDEVICE(INTEL, 0x5ab6), .driver_data = INTEL_APL }, |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 227 | {}, |
| 228 | }; |
| 229 | |
| 230 | U_BOOT_PCI_DEVICE(i2c_designware_pci, designware_pci_supported); |