blob: 8ba05aa9dea8edbad35994c18bc40bba4d817f0d [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>
Felix Held00058f52020-04-06 23:36:24 +02008#include <device/device.h>
Martin Roth5c354b92019-04-22 14:55:16 -06009#include <drivers/i2c/designware/dw_i2c.h>
10#include <amdblocks/acpimmio.h>
Martin Roth7e78e562019-11-03 23:29:02 -070011#include <soc/i2c.h>
Martin Roth5c354b92019-04-22 14:55:16 -060012#include <soc/iomap.h>
13#include <soc/pci_devs.h>
14#include <soc/southbridge.h>
Martin Roth5c354b92019-04-22 14:55:16 -060015#include "chip.h"
16
Martin Roth5c354b92019-04-22 14:55:16 -060017/* Global to provide access to chip.c */
18const char *i2c_acpi_name(const struct device *dev);
19
Martin Roth7e78e562019-11-03 23:29:02 -070020/*
21 * We don't have addresses for I2C0-1.
22 */
23static const uintptr_t i2c_bus_address[I2C_MASTER_DEV_COUNT + I2C_SLAVE_DEV_COUNT] = {
24 0,
25 0,
Marshall Dawsone2c24f72019-06-20 08:47:58 -060026 APU_I2C2_BASE,
27 APU_I2C3_BASE,
Martin Roth7e78e562019-11-03 23:29:02 -070028 APU_I2C4_BASE, /* Can only be used in slave mode */
Martin Roth5c354b92019-04-22 14:55:16 -060029};
30
31uintptr_t dw_i2c_base_address(unsigned int bus)
32{
Martin Roth7e78e562019-11-03 23:29:02 -070033 if (bus >= ARRAY_SIZE(i2c_bus_address))
Marshall Dawsone2c24f72019-06-20 08:47:58 -060034 return 0;
35
Martin Roth7e78e562019-11-03 23:29:02 -070036 return i2c_bus_address[bus];
Martin Roth5c354b92019-04-22 14:55:16 -060037}
38
Martin Roth5c354b92019-04-22 14:55:16 -060039const struct dw_i2c_bus_config *dw_i2c_get_soc_cfg(unsigned int bus)
40{
Marshall Dawsonbc4c9032019-06-11 12:18:20 -060041 const struct soc_amd_picasso_config *config;
Martin Roth5c354b92019-04-22 14:55:16 -060042
Martin Roth7e78e562019-11-03 23:29:02 -070043 if (bus >= ARRAY_SIZE(config->i2c))
Martin Roth5c354b92019-04-22 14:55:16 -060044 return NULL;
45
Felix Held00058f52020-04-06 23:36:24 +020046 /* config is not NULL; if it was, config_of_soc calls die() internally */
47 config = config_of_soc();
Martin Roth5c354b92019-04-22 14:55:16 -060048
49 return &config->i2c[bus];
50}
51
52const char *i2c_acpi_name(const struct device *dev)
53{
54 switch (dev->path.mmio.addr) {
Marshall Dawsone2c24f72019-06-20 08:47:58 -060055 case APU_I2C2_BASE:
Marshall Dawson59e97b62019-08-15 17:49:11 -060056 return "I2C2";
Marshall Dawsone2c24f72019-06-20 08:47:58 -060057 case APU_I2C3_BASE:
Marshall Dawson59e97b62019-08-15 17:49:11 -060058 return "I2C3";
Marshall Dawsone2c24f72019-06-20 08:47:58 -060059 case APU_I2C4_BASE:
Marshall Dawson59e97b62019-08-15 17:49:11 -060060 return "I2C4";
Martin Roth5c354b92019-04-22 14:55:16 -060061 default:
62 return NULL;
63 }
64}
65
66int dw_i2c_soc_dev_to_bus(struct device *dev)
67{
68 switch (dev->path.mmio.addr) {
Marshall Dawsone2c24f72019-06-20 08:47:58 -060069 case APU_I2C2_BASE:
Martin Roth5c354b92019-04-22 14:55:16 -060070 return 2;
Marshall Dawsone2c24f72019-06-20 08:47:58 -060071 case APU_I2C3_BASE:
Martin Roth5c354b92019-04-22 14:55:16 -060072 return 3;
Marshall Dawsone2c24f72019-06-20 08:47:58 -060073 case APU_I2C4_BASE:
74 return 4;
Martin Roth5c354b92019-04-22 14:55:16 -060075 }
76 return -1;
77}
78
Marshall Dawsone2c24f72019-06-20 08:47:58 -060079__weak void mainboard_i2c_override(int bus, uint32_t *pad_settings) { }
80
Martin Roth5c354b92019-04-22 14:55:16 -060081static void dw_i2c_soc_init(bool is_early_init)
82{
83 size_t i;
Marshall Dawsonbc4c9032019-06-11 12:18:20 -060084 const struct soc_amd_picasso_config *config;
Marshall Dawsone2c24f72019-06-20 08:47:58 -060085 uint32_t pad_ctrl;
86 int misc_reg;
Martin Roth5c354b92019-04-22 14:55:16 -060087
Felix Held00058f52020-04-06 23:36:24 +020088 /* config is not NULL; if it was, config_of_soc calls die() internally */
89 config = config_of_soc();
Martin Roth5c354b92019-04-22 14:55:16 -060090
Martin Roth7e78e562019-11-03 23:29:02 -070091 for (i = I2C_MASTER_START_INDEX; i < ARRAY_SIZE(config->i2c); i++) {
Martin Roth5c354b92019-04-22 14:55:16 -060092 const struct dw_i2c_bus_config *cfg = &config->i2c[i];
93
94 if (cfg->early_init != is_early_init)
95 continue;
96
Marshall Dawsone2c24f72019-06-20 08:47:58 -060097 if (dw_i2c_init(i, cfg)) {
Martin Roth5c354b92019-04-22 14:55:16 -060098 printk(BIOS_ERR, "Failed to init i2c bus %zd\n", i);
Marshall Dawsone2c24f72019-06-20 08:47:58 -060099 continue;
100 }
101
102 misc_reg = MISC_I2C0_PAD_CTRL + sizeof(uint32_t) * i;
103 pad_ctrl = misc_read32(misc_reg);
104
105 pad_ctrl &= ~I2C_PAD_CTRL_NG_MASK;
106 pad_ctrl |= I2C_PAD_CTRL_NG_NORMAL;
107
108 pad_ctrl &= ~I2C_PAD_CTRL_RX_SEL_MASK;
109 pad_ctrl |= I2C_PAD_CTRL_RX_SEL_3_3V;
110
111 pad_ctrl &= ~I2C_PAD_CTRL_FALLSLEW_MASK;
112 pad_ctrl |= cfg->speed == I2C_SPEED_STANDARD
113 ? I2C_PAD_CTRL_FALLSLEW_STD
114 : I2C_PAD_CTRL_FALLSLEW_LOW;
115 pad_ctrl |= I2C_PAD_CTRL_FALLSLEW_EN;
116
117 mainboard_i2c_override(i, &pad_ctrl);
118 misc_write32(misc_reg, pad_ctrl);
Martin Roth5c354b92019-04-22 14:55:16 -0600119 }
120}
121
122void i2c_soc_early_init(void)
123{
124 dw_i2c_soc_init(true);
125}
126
127void i2c_soc_init(void)
128{
129 dw_i2c_soc_init(false);
130}
131
Marshall Dawsonbc4c9032019-06-11 12:18:20 -0600132struct device_operations picasso_i2c_mmio_ops = {
Martin Roth5c354b92019-04-22 14:55:16 -0600133 /* TODO(teravest): Move I2C resource info here. */
Nico Huber2f8ba692020-04-05 14:05:24 +0200134 .read_resources = noop_read_resources,
135 .set_resources = noop_set_resources,
Martin Roth5c354b92019-04-22 14:55:16 -0600136 .scan_bus = scan_smbus,
137 .acpi_name = i2c_acpi_name,
Nico Huber68680dd2020-03-31 17:34:52 +0200138 .acpi_fill_ssdt = dw_i2c_acpi_fill_ssdt,
Martin Roth5c354b92019-04-22 14:55:16 -0600139};
140
141/*
142 * I2C pins are open drain with external pull up, so in order to bit bang them
143 * all, SCL pins must become GPIO inputs with no pull, then they need to be
144 * toggled between input-no-pull and output-low. This table is for the initial
145 * conversion of all SCL pins to input with no pull.
146 */
147static const struct soc_amd_gpio i2c_2_gpi[] = {
Martin Roth5c354b92019-04-22 14:55:16 -0600148 PAD_GPI(I2C2_SCL_PIN, PULL_NONE),
149 PAD_GPI(I2C3_SCL_PIN, PULL_NONE),
Marshall Dawsone2c24f72019-06-20 08:47:58 -0600150 /* I2C4 is a slave device only */
Martin Roth5c354b92019-04-22 14:55:16 -0600151};
152#define saved_pins_count ARRAY_SIZE(i2c_2_gpi)
153
154/*
155 * To program I2C pins without destroying their programming, the registers
156 * that will be changed need to be saved first.
157 */
158static void save_i2c_pin_registers(uint8_t gpio,
159 struct soc_amd_i2c_save *save_table)
160{
161 uint32_t *gpio_ptr;
162
163 gpio_ptr = (uint32_t *)gpio_get_address(gpio);
164 save_table->mux_value = iomux_read8(gpio);
165 save_table->control_value = read32(gpio_ptr);
166}
167
168static void restore_i2c_pin_registers(uint8_t gpio,
169 struct soc_amd_i2c_save *save_table)
170{
171 uint32_t *gpio_ptr;
172
173 gpio_ptr = (uint32_t *)gpio_get_address(gpio);
174 iomux_write8(gpio, save_table->mux_value);
175 iomux_read8(gpio);
176 write32(gpio_ptr, save_table->control_value);
177 read32(gpio_ptr);
178}
179
180/* Slaves to be reset are controlled by devicetree register i2c_scl_reset */
181void sb_reset_i2c_slaves(void)
182{
Marshall Dawsonbc4c9032019-06-11 12:18:20 -0600183 const struct soc_amd_picasso_config *cfg;
Martin Roth5c354b92019-04-22 14:55:16 -0600184 const struct device *dev = pcidev_path_on_root(GNB_DEVFN);
185 struct soc_amd_i2c_save save_table[saved_pins_count];
186 uint8_t i, j, control;
187
188 if (!dev || !dev->chip_info)
189 return;
190 cfg = dev->chip_info;
191 control = cfg->i2c_scl_reset & GPIO_I2C_MASK;
192 if (control == 0)
193 return;
194
195 /* Save and reprogram I2C SCL pins */
196 for (i = 0; i < saved_pins_count; i++)
197 save_i2c_pin_registers(i2c_2_gpi[i].gpio, &save_table[i]);
198 program_gpios(i2c_2_gpi, saved_pins_count);
199
200 /*
201 * Toggle SCL back and forth 9 times under 100KHz. A single read is
202 * needed after the writes to force the posted write to complete.
203 */
204 for (j = 0; j < 9; j++) {
Martin Roth5c354b92019-04-22 14:55:16 -0600205 if (control & GPIO_I2C2_SCL)
206 write32((uint32_t *)GPIO_I2C2_ADDRESS, GPIO_SCL_LOW);
207 if (control & GPIO_I2C3_SCL)
208 write32((uint32_t *)GPIO_I2C3_ADDRESS, GPIO_SCL_LOW);
209
210 read32((uint32_t *)GPIO_I2C3_ADDRESS); /* Flush posted write */
211 udelay(4); /* 4usec gets 85KHz for 1 pin, 70KHz for 4 pins */
212
Martin Roth5c354b92019-04-22 14:55:16 -0600213 if (control & GPIO_I2C2_SCL)
214 write32((uint32_t *)GPIO_I2C2_ADDRESS, GPIO_SCL_HIGH);
215 if (control & GPIO_I2C3_SCL)
216 write32((uint32_t *)GPIO_I2C3_ADDRESS, GPIO_SCL_HIGH);
217
218 read32((uint32_t *)GPIO_I2C3_ADDRESS); /* Flush posted write */
219 udelay(4);
220 }
221
222 /* Restore I2C pins. */
223 for (i = 0; i < saved_pins_count; i++)
224 restore_i2c_pin_registers(i2c_2_gpi[i].gpio, &save_table[i]);
225}