blob: 6bbc7a7adc2d53486fa79a90d0e4991b6951bd0a [file] [log] [blame]
Angel Ponsae593872020-04-04 18:50:57 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Martin Roth5c354b92019-04-22 14:55:16 -06003
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 Roth5c354b92019-04-22 14:55:16 -060016/* Global to provide access to chip.c */
17const char *i2c_acpi_name(const struct device *dev);
18
19static const uintptr_t i2c_bus_address[] = {
Marshall Dawsone2c24f72019-06-20 08:47:58 -060020 APU_I2C2_BASE,
21 APU_I2C3_BASE,
22 APU_I2C4_BASE, /* slave device only */
Martin Roth5c354b92019-04-22 14:55:16 -060023};
24
25uintptr_t dw_i2c_base_address(unsigned int bus)
26{
Marshall Dawsone2c24f72019-06-20 08:47:58 -060027 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 Roth5c354b92019-04-22 14:55:16 -060031}
32
Martin Roth5c354b92019-04-22 14:55:16 -060033const struct dw_i2c_bus_config *dw_i2c_get_soc_cfg(unsigned int bus)
34{
Marshall Dawsonbc4c9032019-06-11 12:18:20 -060035 const struct soc_amd_picasso_config *config;
Martin Roth5c354b92019-04-22 14:55:16 -060036
Marshall Dawsone2c24f72019-06-20 08:47:58 -060037 if (bus < APU_I2C_MIN_BUS || bus > APU_I2C_MAX_BUS)
Martin Roth5c354b92019-04-22 14:55:16 -060038 return NULL;
39
40 config = get_soc_config();
41 if (config == NULL)
42 return NULL;
43
44 return &config->i2c[bus];
45}
46
47const char *i2c_acpi_name(const struct device *dev)
48{
49 switch (dev->path.mmio.addr) {
Marshall Dawsone2c24f72019-06-20 08:47:58 -060050 case APU_I2C2_BASE:
Marshall Dawson59e97b62019-08-15 17:49:11 -060051 return "I2C2";
Marshall Dawsone2c24f72019-06-20 08:47:58 -060052 case APU_I2C3_BASE:
Marshall Dawson59e97b62019-08-15 17:49:11 -060053 return "I2C3";
Marshall Dawsone2c24f72019-06-20 08:47:58 -060054 case APU_I2C4_BASE:
Marshall Dawson59e97b62019-08-15 17:49:11 -060055 return "I2C4";
Martin Roth5c354b92019-04-22 14:55:16 -060056 default:
57 return NULL;
58 }
59}
60
61int dw_i2c_soc_dev_to_bus(struct device *dev)
62{
63 switch (dev->path.mmio.addr) {
Marshall Dawsone2c24f72019-06-20 08:47:58 -060064 case APU_I2C2_BASE:
Martin Roth5c354b92019-04-22 14:55:16 -060065 return 2;
Marshall Dawsone2c24f72019-06-20 08:47:58 -060066 case APU_I2C3_BASE:
Martin Roth5c354b92019-04-22 14:55:16 -060067 return 3;
Marshall Dawsone2c24f72019-06-20 08:47:58 -060068 case APU_I2C4_BASE:
69 return 4;
Martin Roth5c354b92019-04-22 14:55:16 -060070 }
71 return -1;
72}
73
Marshall Dawsone2c24f72019-06-20 08:47:58 -060074__weak void mainboard_i2c_override(int bus, uint32_t *pad_settings) { }
75
Martin Roth5c354b92019-04-22 14:55:16 -060076static void dw_i2c_soc_init(bool is_early_init)
77{
78 size_t i;
Marshall Dawsonbc4c9032019-06-11 12:18:20 -060079 const struct soc_amd_picasso_config *config;
Marshall Dawsone2c24f72019-06-20 08:47:58 -060080 uint32_t pad_ctrl;
81 int misc_reg;
Martin Roth5c354b92019-04-22 14:55:16 -060082
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 Dawsone2c24f72019-06-20 08:47:58 -060094 if (dw_i2c_init(i, cfg)) {
Martin Roth5c354b92019-04-22 14:55:16 -060095 printk(BIOS_ERR, "Failed to init i2c bus %zd\n", i);
Marshall Dawsone2c24f72019-06-20 08:47:58 -060096 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 Roth5c354b92019-04-22 14:55:16 -0600116 }
117}
118
119void i2c_soc_early_init(void)
120{
121 dw_i2c_soc_init(true);
122}
123
124void i2c_soc_init(void)
125{
126 dw_i2c_soc_init(false);
127}
128
Marshall Dawsonbc4c9032019-06-11 12:18:20 -0600129struct device_operations picasso_i2c_mmio_ops = {
Martin Roth5c354b92019-04-22 14:55:16 -0600130 /* 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 Huber68680dd2020-03-31 17:34:52 +0200136 .acpi_fill_ssdt = dw_i2c_acpi_fill_ssdt,
Martin Roth5c354b92019-04-22 14:55:16 -0600137};
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 */
145static const struct soc_amd_gpio i2c_2_gpi[] = {
Martin Roth5c354b92019-04-22 14:55:16 -0600146 PAD_GPI(I2C2_SCL_PIN, PULL_NONE),
147 PAD_GPI(I2C3_SCL_PIN, PULL_NONE),
Marshall Dawsone2c24f72019-06-20 08:47:58 -0600148 /* I2C4 is a slave device only */
Martin Roth5c354b92019-04-22 14:55:16 -0600149};
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 */
156static 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
166static 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 */
179void sb_reset_i2c_slaves(void)
180{
Marshall Dawsonbc4c9032019-06-11 12:18:20 -0600181 const struct soc_amd_picasso_config *cfg;
Martin Roth5c354b92019-04-22 14:55:16 -0600182 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 Roth5c354b92019-04-22 14:55:16 -0600203 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 Roth5c354b92019-04-22 14:55:16 -0600211 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}