Angel Pons | ae59387 | 2020-04-04 18:50:57 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 2 | |
| 3 | #include <device/mmio.h> |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 4 | #include <acpi/acpi.h> |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 5 | #include <console/console.h> |
| 6 | #include <delay.h> |
Felix Held | 00058f5 | 2020-04-06 23:36:24 +0200 | [diff] [blame] | 7 | #include <device/device.h> |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 8 | #include <drivers/i2c/designware/dw_i2c.h> |
| 9 | #include <amdblocks/acpimmio.h> |
Martin Roth | 7e78e56 | 2019-11-03 23:29:02 -0700 | [diff] [blame] | 10 | #include <soc/i2c.h> |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 11 | #include <soc/iomap.h> |
| 12 | #include <soc/pci_devs.h> |
| 13 | #include <soc/southbridge.h> |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 14 | #include "chip.h" |
| 15 | |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame^] | 16 | #if ENV_X86 |
Martin Roth | 7e78e56 | 2019-11-03 23:29:02 -0700 | [diff] [blame] | 17 | static const uintptr_t i2c_bus_address[I2C_MASTER_DEV_COUNT + I2C_SLAVE_DEV_COUNT] = { |
| 18 | 0, |
| 19 | 0, |
Marshall Dawson | e2c24f7 | 2019-06-20 08:47:58 -0600 | [diff] [blame] | 20 | APU_I2C2_BASE, |
| 21 | APU_I2C3_BASE, |
Martin Roth | 7e78e56 | 2019-11-03 23:29:02 -0700 | [diff] [blame] | 22 | APU_I2C4_BASE, /* Can only be used in slave mode */ |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 23 | }; |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame^] | 24 | #else |
| 25 | static uintptr_t i2c_bus_address[I2C_MASTER_DEV_COUNT + I2C_SLAVE_DEV_COUNT]; |
| 26 | #endif |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 27 | |
| 28 | uintptr_t dw_i2c_base_address(unsigned int bus) |
| 29 | { |
Martin Roth | 7e78e56 | 2019-11-03 23:29:02 -0700 | [diff] [blame] | 30 | if (bus >= ARRAY_SIZE(i2c_bus_address)) |
Marshall Dawson | e2c24f7 | 2019-06-20 08:47:58 -0600 | [diff] [blame] | 31 | return 0; |
| 32 | |
Martin Roth | 7e78e56 | 2019-11-03 23:29:02 -0700 | [diff] [blame] | 33 | return i2c_bus_address[bus]; |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 34 | } |
| 35 | |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame^] | 36 | #if !ENV_X86 |
| 37 | void i2c_set_bar(unsigned int bus, uintptr_t bar) |
| 38 | { |
| 39 | if (bus >= ARRAY_SIZE(i2c_bus_address)) { |
| 40 | printk(BIOS_ERR, "Error: i2c index out of bounds: %u.", bus); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | i2c_bus_address[bus] = bar; |
| 45 | } |
| 46 | #endif |
| 47 | |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 48 | const struct dw_i2c_bus_config *dw_i2c_get_soc_cfg(unsigned int bus) |
| 49 | { |
Marshall Dawson | bc4c903 | 2019-06-11 12:18:20 -0600 | [diff] [blame] | 50 | const struct soc_amd_picasso_config *config; |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 51 | |
Martin Roth | 7e78e56 | 2019-11-03 23:29:02 -0700 | [diff] [blame] | 52 | if (bus >= ARRAY_SIZE(config->i2c)) |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 53 | return NULL; |
| 54 | |
Felix Held | 00058f5 | 2020-04-06 23:36:24 +0200 | [diff] [blame] | 55 | /* config is not NULL; if it was, config_of_soc calls die() internally */ |
| 56 | config = config_of_soc(); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 57 | |
| 58 | return &config->i2c[bus]; |
| 59 | } |
| 60 | |
Furquan Shaikh | 4663749 | 2020-06-03 16:22:20 -0700 | [diff] [blame] | 61 | static const char *i2c_acpi_name(const struct device *dev) |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 62 | { |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame^] | 63 | if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[2]) |
Marshall Dawson | 59e97b6 | 2019-08-15 17:49:11 -0600 | [diff] [blame] | 64 | return "I2C2"; |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame^] | 65 | else if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[3]) |
Marshall Dawson | 59e97b6 | 2019-08-15 17:49:11 -0600 | [diff] [blame] | 66 | return "I2C3"; |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame^] | 67 | else if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[4]) |
Marshall Dawson | 59e97b6 | 2019-08-15 17:49:11 -0600 | [diff] [blame] | 68 | return "I2C4"; |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame^] | 69 | return NULL; |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 70 | } |
| 71 | |
Furquan Shaikh | 00296ea | 2020-04-24 21:33:02 -0700 | [diff] [blame] | 72 | int dw_i2c_soc_dev_to_bus(const struct device *dev) |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 73 | { |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame^] | 74 | if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[2]) |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 75 | return 2; |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame^] | 76 | else if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[3]) |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 77 | return 3; |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame^] | 78 | else if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[4]) |
Marshall Dawson | e2c24f7 | 2019-06-20 08:47:58 -0600 | [diff] [blame] | 79 | return 4; |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 80 | return -1; |
| 81 | } |
| 82 | |
Marshall Dawson | e2c24f7 | 2019-06-20 08:47:58 -0600 | [diff] [blame] | 83 | __weak void mainboard_i2c_override(int bus, uint32_t *pad_settings) { } |
| 84 | |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 85 | static void dw_i2c_soc_init(bool is_early_init) |
| 86 | { |
| 87 | size_t i; |
Marshall Dawson | bc4c903 | 2019-06-11 12:18:20 -0600 | [diff] [blame] | 88 | const struct soc_amd_picasso_config *config; |
Marshall Dawson | e2c24f7 | 2019-06-20 08:47:58 -0600 | [diff] [blame] | 89 | uint32_t pad_ctrl; |
| 90 | int misc_reg; |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 91 | |
Felix Held | 00058f5 | 2020-04-06 23:36:24 +0200 | [diff] [blame] | 92 | /* config is not NULL; if it was, config_of_soc calls die() internally */ |
| 93 | config = config_of_soc(); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 94 | |
Martin Roth | 7e78e56 | 2019-11-03 23:29:02 -0700 | [diff] [blame] | 95 | for (i = I2C_MASTER_START_INDEX; i < ARRAY_SIZE(config->i2c); i++) { |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 96 | const struct dw_i2c_bus_config *cfg = &config->i2c[i]; |
| 97 | |
| 98 | if (cfg->early_init != is_early_init) |
| 99 | continue; |
| 100 | |
Marshall Dawson | e2c24f7 | 2019-06-20 08:47:58 -0600 | [diff] [blame] | 101 | if (dw_i2c_init(i, cfg)) { |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 102 | printk(BIOS_ERR, "Failed to init i2c bus %zd\n", i); |
Marshall Dawson | e2c24f7 | 2019-06-20 08:47:58 -0600 | [diff] [blame] | 103 | continue; |
| 104 | } |
| 105 | |
| 106 | misc_reg = MISC_I2C0_PAD_CTRL + sizeof(uint32_t) * i; |
| 107 | pad_ctrl = misc_read32(misc_reg); |
| 108 | |
| 109 | pad_ctrl &= ~I2C_PAD_CTRL_NG_MASK; |
| 110 | pad_ctrl |= I2C_PAD_CTRL_NG_NORMAL; |
| 111 | |
| 112 | pad_ctrl &= ~I2C_PAD_CTRL_RX_SEL_MASK; |
| 113 | pad_ctrl |= I2C_PAD_CTRL_RX_SEL_3_3V; |
| 114 | |
| 115 | pad_ctrl &= ~I2C_PAD_CTRL_FALLSLEW_MASK; |
| 116 | pad_ctrl |= cfg->speed == I2C_SPEED_STANDARD |
| 117 | ? I2C_PAD_CTRL_FALLSLEW_STD |
| 118 | : I2C_PAD_CTRL_FALLSLEW_LOW; |
| 119 | pad_ctrl |= I2C_PAD_CTRL_FALLSLEW_EN; |
| 120 | |
| 121 | mainboard_i2c_override(i, &pad_ctrl); |
| 122 | misc_write32(misc_reg, pad_ctrl); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | void i2c_soc_early_init(void) |
| 127 | { |
| 128 | dw_i2c_soc_init(true); |
| 129 | } |
| 130 | |
| 131 | void i2c_soc_init(void) |
| 132 | { |
| 133 | dw_i2c_soc_init(false); |
| 134 | } |
| 135 | |
Marshall Dawson | bc4c903 | 2019-06-11 12:18:20 -0600 | [diff] [blame] | 136 | struct device_operations picasso_i2c_mmio_ops = { |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 137 | /* TODO(teravest): Move I2C resource info here. */ |
Nico Huber | 2f8ba69 | 2020-04-05 14:05:24 +0200 | [diff] [blame] | 138 | .read_resources = noop_read_resources, |
| 139 | .set_resources = noop_set_resources, |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 140 | .scan_bus = scan_smbus, |
| 141 | .acpi_name = i2c_acpi_name, |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 142 | .acpi_fill_ssdt = dw_i2c_acpi_fill_ssdt, |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | /* |
| 146 | * I2C pins are open drain with external pull up, so in order to bit bang them |
| 147 | * all, SCL pins must become GPIO inputs with no pull, then they need to be |
| 148 | * toggled between input-no-pull and output-low. This table is for the initial |
| 149 | * conversion of all SCL pins to input with no pull. |
| 150 | */ |
| 151 | static const struct soc_amd_gpio i2c_2_gpi[] = { |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 152 | PAD_GPI(I2C2_SCL_PIN, PULL_NONE), |
| 153 | PAD_GPI(I2C3_SCL_PIN, PULL_NONE), |
Marshall Dawson | e2c24f7 | 2019-06-20 08:47:58 -0600 | [diff] [blame] | 154 | /* I2C4 is a slave device only */ |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 155 | }; |
| 156 | #define saved_pins_count ARRAY_SIZE(i2c_2_gpi) |
| 157 | |
| 158 | /* |
| 159 | * To program I2C pins without destroying their programming, the registers |
| 160 | * that will be changed need to be saved first. |
| 161 | */ |
| 162 | static void save_i2c_pin_registers(uint8_t gpio, |
| 163 | struct soc_amd_i2c_save *save_table) |
| 164 | { |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 165 | save_table->mux_value = iomux_read8(gpio); |
Kyösti Mälkki | 39bd46f | 2020-06-18 19:18:21 +0300 | [diff] [blame] | 166 | save_table->control_value = gpio_read32(gpio); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static void restore_i2c_pin_registers(uint8_t gpio, |
| 170 | struct soc_amd_i2c_save *save_table) |
| 171 | { |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 172 | iomux_write8(gpio, save_table->mux_value); |
| 173 | iomux_read8(gpio); |
Kyösti Mälkki | 39bd46f | 2020-06-18 19:18:21 +0300 | [diff] [blame] | 174 | gpio_write32_rb(gpio, save_table->control_value); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* Slaves to be reset are controlled by devicetree register i2c_scl_reset */ |
| 178 | void sb_reset_i2c_slaves(void) |
| 179 | { |
Marshall Dawson | bc4c903 | 2019-06-11 12:18:20 -0600 | [diff] [blame] | 180 | const struct soc_amd_picasso_config *cfg; |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 181 | struct soc_amd_i2c_save save_table[saved_pins_count]; |
| 182 | uint8_t i, j, control; |
| 183 | |
Felix Held | f13b6eb | 2020-06-17 20:09:51 +0200 | [diff] [blame] | 184 | cfg = config_of_soc(); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 185 | control = cfg->i2c_scl_reset & GPIO_I2C_MASK; |
| 186 | if (control == 0) |
| 187 | return; |
| 188 | |
| 189 | /* Save and reprogram I2C SCL pins */ |
| 190 | for (i = 0; i < saved_pins_count; i++) |
| 191 | save_i2c_pin_registers(i2c_2_gpi[i].gpio, &save_table[i]); |
| 192 | program_gpios(i2c_2_gpi, saved_pins_count); |
| 193 | |
| 194 | /* |
| 195 | * Toggle SCL back and forth 9 times under 100KHz. A single read is |
| 196 | * needed after the writes to force the posted write to complete. |
| 197 | */ |
| 198 | for (j = 0; j < 9; j++) { |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 199 | if (control & GPIO_I2C2_SCL) |
Kyösti Mälkki | 39bd46f | 2020-06-18 19:18:21 +0300 | [diff] [blame] | 200 | gpio_write32(I2C2_SCL_PIN, GPIO_OUTPUT_ENABLE); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 201 | if (control & GPIO_I2C3_SCL) |
Kyösti Mälkki | 39bd46f | 2020-06-18 19:18:21 +0300 | [diff] [blame] | 202 | gpio_write32(I2C3_SCL_PIN, GPIO_OUTPUT_ENABLE); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 203 | |
Kyösti Mälkki | 39bd46f | 2020-06-18 19:18:21 +0300 | [diff] [blame] | 204 | gpio_read32(0); /* Flush posted write */ |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 205 | udelay(4); /* 4usec gets 85KHz for 1 pin, 70KHz for 4 pins */ |
| 206 | |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 207 | if (control & GPIO_I2C2_SCL) |
Kyösti Mälkki | 39bd46f | 2020-06-18 19:18:21 +0300 | [diff] [blame] | 208 | gpio_write32(I2C2_SCL_PIN, 0); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 209 | if (control & GPIO_I2C3_SCL) |
Kyösti Mälkki | 39bd46f | 2020-06-18 19:18:21 +0300 | [diff] [blame] | 210 | gpio_write32(I2C3_SCL_PIN, 0); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 211 | |
Kyösti Mälkki | 39bd46f | 2020-06-18 19:18:21 +0300 | [diff] [blame] | 212 | gpio_read32(0); /* Flush posted write */ |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 213 | udelay(4); |
| 214 | } |
| 215 | |
| 216 | /* Restore I2C pins. */ |
| 217 | for (i = 0; i < saved_pins_count; i++) |
| 218 | restore_i2c_pin_registers(i2c_2_gpi[i].gpio, &save_table[i]); |
| 219 | } |