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