blob: 1eb0720dc9749f07ba7cd3755ef458e1547f502f [file] [log] [blame]
Angel Ponsae593872020-04-04 18:50:57 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Martin Roth5c354b92019-04-22 14:55:16 -06002
3#include <device/mmio.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07004#include <acpi/acpi.h>
Martin Roth5c354b92019-04-22 14:55:16 -06005#include <console/console.h>
6#include <delay.h>
Felix Held00058f52020-04-06 23:36:24 +02007#include <device/device.h>
Martin Roth5c354b92019-04-22 14:55:16 -06008#include <drivers/i2c/designware/dw_i2c.h>
9#include <amdblocks/acpimmio.h>
Martin Roth7e78e562019-11-03 23:29:02 -070010#include <soc/i2c.h>
Martin Roth5c354b92019-04-22 14:55:16 -060011#include <soc/iomap.h>
12#include <soc/pci_devs.h>
13#include <soc/southbridge.h>
Martin Roth5c354b92019-04-22 14:55:16 -060014#include "chip.h"
15
Martin Rothac41f582020-06-14 17:24:12 -060016#if ENV_X86
Martin Roth7e78e562019-11-03 23:29:02 -070017static const uintptr_t i2c_bus_address[I2C_MASTER_DEV_COUNT + I2C_SLAVE_DEV_COUNT] = {
18 0,
19 0,
Marshall Dawsone2c24f72019-06-20 08:47:58 -060020 APU_I2C2_BASE,
21 APU_I2C3_BASE,
Martin Roth7e78e562019-11-03 23:29:02 -070022 APU_I2C4_BASE, /* Can only be used in slave mode */
Martin Roth5c354b92019-04-22 14:55:16 -060023};
Martin Rothac41f582020-06-14 17:24:12 -060024#else
25static uintptr_t i2c_bus_address[I2C_MASTER_DEV_COUNT + I2C_SLAVE_DEV_COUNT];
26#endif
Martin Roth5c354b92019-04-22 14:55:16 -060027
28uintptr_t dw_i2c_base_address(unsigned int bus)
29{
Martin Roth7e78e562019-11-03 23:29:02 -070030 if (bus >= ARRAY_SIZE(i2c_bus_address))
Marshall Dawsone2c24f72019-06-20 08:47:58 -060031 return 0;
32
Martin Roth7e78e562019-11-03 23:29:02 -070033 return i2c_bus_address[bus];
Martin Roth5c354b92019-04-22 14:55:16 -060034}
35
Martin Rothac41f582020-06-14 17:24:12 -060036#if !ENV_X86
37void 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 Roth5c354b92019-04-22 14:55:16 -060048const struct dw_i2c_bus_config *dw_i2c_get_soc_cfg(unsigned int bus)
49{
Marshall Dawsonbc4c9032019-06-11 12:18:20 -060050 const struct soc_amd_picasso_config *config;
Martin Roth5c354b92019-04-22 14:55:16 -060051
Martin Roth7e78e562019-11-03 23:29:02 -070052 if (bus >= ARRAY_SIZE(config->i2c))
Martin Roth5c354b92019-04-22 14:55:16 -060053 return NULL;
54
Felix Held00058f52020-04-06 23:36:24 +020055 /* config is not NULL; if it was, config_of_soc calls die() internally */
56 config = config_of_soc();
Martin Roth5c354b92019-04-22 14:55:16 -060057
58 return &config->i2c[bus];
59}
60
Furquan Shaikh46637492020-06-03 16:22:20 -070061static const char *i2c_acpi_name(const struct device *dev)
Martin Roth5c354b92019-04-22 14:55:16 -060062{
Martin Rothac41f582020-06-14 17:24:12 -060063 if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[2])
Marshall Dawson59e97b62019-08-15 17:49:11 -060064 return "I2C2";
Martin Rothac41f582020-06-14 17:24:12 -060065 else if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[3])
Marshall Dawson59e97b62019-08-15 17:49:11 -060066 return "I2C3";
Martin Rothac41f582020-06-14 17:24:12 -060067 else if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[4])
Marshall Dawson59e97b62019-08-15 17:49:11 -060068 return "I2C4";
Martin Rothac41f582020-06-14 17:24:12 -060069 return NULL;
Martin Roth5c354b92019-04-22 14:55:16 -060070}
71
Furquan Shaikh00296ea2020-04-24 21:33:02 -070072int dw_i2c_soc_dev_to_bus(const struct device *dev)
Martin Roth5c354b92019-04-22 14:55:16 -060073{
Martin Rothac41f582020-06-14 17:24:12 -060074 if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[2])
Martin Roth5c354b92019-04-22 14:55:16 -060075 return 2;
Martin Rothac41f582020-06-14 17:24:12 -060076 else if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[3])
Martin Roth5c354b92019-04-22 14:55:16 -060077 return 3;
Martin Rothac41f582020-06-14 17:24:12 -060078 else if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[4])
Marshall Dawsone2c24f72019-06-20 08:47:58 -060079 return 4;
Martin Roth5c354b92019-04-22 14:55:16 -060080 return -1;
81}
82
Marshall Dawsone2c24f72019-06-20 08:47:58 -060083__weak void mainboard_i2c_override(int bus, uint32_t *pad_settings) { }
84
Martin Roth5c354b92019-04-22 14:55:16 -060085static void dw_i2c_soc_init(bool is_early_init)
86{
87 size_t i;
Marshall Dawsonbc4c9032019-06-11 12:18:20 -060088 const struct soc_amd_picasso_config *config;
Marshall Dawsone2c24f72019-06-20 08:47:58 -060089 uint32_t pad_ctrl;
90 int misc_reg;
Martin Roth5c354b92019-04-22 14:55:16 -060091
Felix Held00058f52020-04-06 23:36:24 +020092 /* config is not NULL; if it was, config_of_soc calls die() internally */
93 config = config_of_soc();
Martin Roth5c354b92019-04-22 14:55:16 -060094
Martin Roth7e78e562019-11-03 23:29:02 -070095 for (i = I2C_MASTER_START_INDEX; i < ARRAY_SIZE(config->i2c); i++) {
Martin Roth5c354b92019-04-22 14:55:16 -060096 const struct dw_i2c_bus_config *cfg = &config->i2c[i];
97
98 if (cfg->early_init != is_early_init)
99 continue;
100
Marshall Dawsone2c24f72019-06-20 08:47:58 -0600101 if (dw_i2c_init(i, cfg)) {
Martin Roth5c354b92019-04-22 14:55:16 -0600102 printk(BIOS_ERR, "Failed to init i2c bus %zd\n", i);
Marshall Dawsone2c24f72019-06-20 08:47:58 -0600103 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 Roth5c354b92019-04-22 14:55:16 -0600123 }
124}
125
126void i2c_soc_early_init(void)
127{
128 dw_i2c_soc_init(true);
129}
130
131void i2c_soc_init(void)
132{
133 dw_i2c_soc_init(false);
134}
135
Marshall Dawsonbc4c9032019-06-11 12:18:20 -0600136struct device_operations picasso_i2c_mmio_ops = {
Martin Roth5c354b92019-04-22 14:55:16 -0600137 /* TODO(teravest): Move I2C resource info here. */
Nico Huber2f8ba692020-04-05 14:05:24 +0200138 .read_resources = noop_read_resources,
139 .set_resources = noop_set_resources,
Martin Roth5c354b92019-04-22 14:55:16 -0600140 .scan_bus = scan_smbus,
141 .acpi_name = i2c_acpi_name,
Nico Huber68680dd2020-03-31 17:34:52 +0200142 .acpi_fill_ssdt = dw_i2c_acpi_fill_ssdt,
Martin Roth5c354b92019-04-22 14:55:16 -0600143};
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 */
151static const struct soc_amd_gpio i2c_2_gpi[] = {
Martin Roth5c354b92019-04-22 14:55:16 -0600152 PAD_GPI(I2C2_SCL_PIN, PULL_NONE),
153 PAD_GPI(I2C3_SCL_PIN, PULL_NONE),
Marshall Dawsone2c24f72019-06-20 08:47:58 -0600154 /* I2C4 is a slave device only */
Martin Roth5c354b92019-04-22 14:55:16 -0600155};
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 */
162static void save_i2c_pin_registers(uint8_t gpio,
163 struct soc_amd_i2c_save *save_table)
164{
Martin Roth5c354b92019-04-22 14:55:16 -0600165 save_table->mux_value = iomux_read8(gpio);
Kyösti Mälkki39bd46f2020-06-18 19:18:21 +0300166 save_table->control_value = gpio_read32(gpio);
Martin Roth5c354b92019-04-22 14:55:16 -0600167}
168
169static void restore_i2c_pin_registers(uint8_t gpio,
170 struct soc_amd_i2c_save *save_table)
171{
Martin Roth5c354b92019-04-22 14:55:16 -0600172 iomux_write8(gpio, save_table->mux_value);
173 iomux_read8(gpio);
Kyösti Mälkki39bd46f2020-06-18 19:18:21 +0300174 gpio_write32_rb(gpio, save_table->control_value);
Martin Roth5c354b92019-04-22 14:55:16 -0600175}
176
177/* Slaves to be reset are controlled by devicetree register i2c_scl_reset */
178void sb_reset_i2c_slaves(void)
179{
Marshall Dawsonbc4c9032019-06-11 12:18:20 -0600180 const struct soc_amd_picasso_config *cfg;
Martin Roth5c354b92019-04-22 14:55:16 -0600181 struct soc_amd_i2c_save save_table[saved_pins_count];
182 uint8_t i, j, control;
183
Felix Heldf13b6eb2020-06-17 20:09:51 +0200184 cfg = config_of_soc();
Martin Roth5c354b92019-04-22 14:55:16 -0600185 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 Roth5c354b92019-04-22 14:55:16 -0600199 if (control & GPIO_I2C2_SCL)
Kyösti Mälkki39bd46f2020-06-18 19:18:21 +0300200 gpio_write32(I2C2_SCL_PIN, GPIO_OUTPUT_ENABLE);
Martin Roth5c354b92019-04-22 14:55:16 -0600201 if (control & GPIO_I2C3_SCL)
Kyösti Mälkki39bd46f2020-06-18 19:18:21 +0300202 gpio_write32(I2C3_SCL_PIN, GPIO_OUTPUT_ENABLE);
Martin Roth5c354b92019-04-22 14:55:16 -0600203
Kyösti Mälkki39bd46f2020-06-18 19:18:21 +0300204 gpio_read32(0); /* Flush posted write */
Martin Roth5c354b92019-04-22 14:55:16 -0600205 udelay(4); /* 4usec gets 85KHz for 1 pin, 70KHz for 4 pins */
206
Martin Roth5c354b92019-04-22 14:55:16 -0600207 if (control & GPIO_I2C2_SCL)
Kyösti Mälkki39bd46f2020-06-18 19:18:21 +0300208 gpio_write32(I2C2_SCL_PIN, 0);
Martin Roth5c354b92019-04-22 14:55:16 -0600209 if (control & GPIO_I2C3_SCL)
Kyösti Mälkki39bd46f2020-06-18 19:18:21 +0300210 gpio_write32(I2C3_SCL_PIN, 0);
Martin Roth5c354b92019-04-22 14:55:16 -0600211
Kyösti Mälkki39bd46f2020-06-18 19:18:21 +0300212 gpio_read32(0); /* Flush posted write */
Martin Roth5c354b92019-04-22 14:55:16 -0600213 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}