blob: ec05e49204424e75eac33b2b5563b0f21dba47a4 [file] [log] [blame]
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -07001/* SPDX-License-Identifier: GPL-2.0-only */
2
Karthikeyan Ramasubramanian4f87ae12021-03-18 23:16:29 -06003#include <acpi/acpi.h>
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -07004#include <assert.h>
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -07005#include <amdblocks/acpimmio.h>
6#include <amdblocks/gpio_banks.h>
7#include <amdblocks/gpio_defs.h>
8#include <amdblocks/i2c.h>
Karthikeyan Ramasubramanian4f87ae12021-03-18 23:16:29 -06009#include <console/console.h>
10#include <delay.h>
11#include <device/device.h>
12#include <device/i2c.h>
13#include <device/mmio.h>
14#include <drivers/i2c/designware/dw_i2c.h>
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -070015
16#define MAX_PIN_COUNT 4
17
Karthikeyan Ramasubramanian4f87ae12021-03-18 23:16:29 -060018uintptr_t dw_i2c_base_address(unsigned int bus)
19{
20 size_t num_ctrlrs;
21 const struct soc_i2c_ctrlr_info *ctrlr = soc_get_i2c_ctrlr_info(&num_ctrlrs);
22
23 if (bus >= num_ctrlrs) {
24 printk(BIOS_ERR, "Bus ID %d is >= number of I2C controllers %zu\n",
25 bus, num_ctrlrs);
26 return 0;
27 }
28
29 return ctrlr[bus].bar;
30}
31
32const struct dw_i2c_bus_config *dw_i2c_get_soc_cfg(unsigned int bus)
33{
34 size_t num_buses = 0;
35 const struct dw_i2c_bus_config *cfg = soc_get_i2c_bus_config(&num_buses);
36
37 if (bus >= num_buses) {
38 printk(BIOS_ERR, "Bus ID %d is >= number of I2C buses %zu\n", bus, num_buses);
39 return NULL;
40 }
41
42 return &cfg[bus];
43}
44
45static const char *i2c_acpi_name(const struct device *dev)
46{
47 size_t i;
48 size_t num_ctrlrs;
49 const struct soc_i2c_ctrlr_info *ctrlr = soc_get_i2c_ctrlr_info(&num_ctrlrs);
50
51 if (!(uintptr_t)dev->path.mmio.addr)
52 die("NULL MMIO address at %s\n", __func__);
53
54 for (i = 0; i < num_ctrlrs; i++) {
55 if ((uintptr_t)dev->path.mmio.addr == ctrlr[i].bar)
56 return ctrlr[i].acpi_name;
57 }
58 printk(BIOS_ERR, "%s: Could not find %lu\n", __func__, (uintptr_t)dev->path.mmio.addr);
59 return NULL;
60}
61
62int dw_i2c_soc_dev_to_bus(const struct device *dev)
63{
64 size_t i;
65 size_t num_ctrlrs;
66 const struct soc_i2c_ctrlr_info *ctrlr = soc_get_i2c_ctrlr_info(&num_ctrlrs);
67
68 if (!(uintptr_t)dev->path.mmio.addr)
69 die("NULL MMIO address at %s\n", __func__);
70
71 for (i = 0; i < num_ctrlrs; i++) {
72 if ((uintptr_t)dev->path.mmio.addr == ctrlr[i].bar)
73 return i;
74 }
75 printk(BIOS_ERR, "%s: Could not find %lu\n", __func__, (uintptr_t)dev->path.mmio.addr);
76 return -1;
77}
78
79void __weak soc_i2c_misc_init(unsigned int bus, const struct dw_i2c_bus_config *cfg)
80{
81 /* Nothing by default. */
82}
83
84static void dw_i2c_soc_init(bool is_early_init)
85{
86 unsigned int bus;
87 size_t num_buses = 0, num_ctrlrs = 0;
88 const struct dw_i2c_bus_config *cfg = soc_get_i2c_bus_config(&num_buses);
89 const struct soc_i2c_ctrlr_info *ctrlr = soc_get_i2c_ctrlr_info(&num_ctrlrs);
90
91 /* Ensure that the number of controllers in devicetree and SoC match. */
92 assert(num_buses == num_ctrlrs);
93
94 for (bus = 0; bus < num_buses; bus++, cfg++, ctrlr++) {
95 /*
96 * Skip initialization when controller is in peripheral mode or base address
97 * is not configured or is not the expected stage to initialize.
98 */
99 if (ctrlr->mode == I2C_PERIPHERAL_MODE || !ctrlr->bar ||
100 cfg->early_init != is_early_init)
101 continue;
102
Felix Helda16a09f2021-03-25 02:07:23 +0100103 if (dw_i2c_init(bus, cfg)) {
Karthikeyan Ramasubramanian4f87ae12021-03-18 23:16:29 -0600104 printk(BIOS_ERR, "Failed to init i2c bus %d\n", bus);
105 continue;
Felix Helda16a09f2021-03-25 02:07:23 +0100106 }
Karthikeyan Ramasubramanian4f87ae12021-03-18 23:16:29 -0600107
108 soc_i2c_misc_init(bus, cfg);
109 }
110}
111
112void i2c_soc_early_init(void)
113{
114 dw_i2c_soc_init(true);
115}
116
117void i2c_soc_init(void)
118{
119 dw_i2c_soc_init(false);
120}
121
122struct device_operations soc_amd_i2c_mmio_ops = {
123 /* TODO(kramasub): Move I2C resource info here. */
124 .read_resources = noop_read_resources,
125 .set_resources = noop_set_resources,
126 .scan_bus = scan_smbus,
127 .acpi_name = i2c_acpi_name,
128 .acpi_fill_ssdt = dw_i2c_acpi_fill_ssdt,
129};
130
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700131struct common_i2c_save {
132 uint32_t control_value;
133 uint8_t mux_value;
134};
135
136/*
137 * To program I2C pins without destroying their programming, the registers
138 * that will be changed need to be saved first.
139 */
140static void save_i2c_pin_registers(uint8_t gpio, struct common_i2c_save *save_table)
141{
142 save_table->mux_value = iomux_read8(gpio);
143 save_table->control_value = gpio_read32(gpio);
144}
145
146static void restore_i2c_pin_registers(uint8_t gpio, struct common_i2c_save *save_table)
147{
148 /* Write and flush posted writes. */
149 iomux_write8(gpio, save_table->mux_value);
150 iomux_read8(gpio);
151 gpio_write32(gpio, save_table->control_value);
152 gpio_read32(gpio);
153}
154
155static void drive_scl(const struct soc_i2c_peripheral_reset_info *reset_info, uint32_t val)
156{
157 uint8_t j;
158
159 for (j = 0; j < reset_info->num_pins; j++) {
160 if (reset_info->i2c_scl_reset_mask & reset_info->i2c_scl[j].pin_mask)
161 gpio_write32(reset_info->i2c_scl[j].pin.gpio, val);
162 }
163
164 gpio_read32(0); /* Flush posted write */
165 /*
166 * TODO(b/183010197): 4usec gets 85KHz for 1 pin, 70KHz for 4 pins. Ensure this delay
167 * works fine for all SoCs and make this delay configurable if required.
168 */
169 udelay(4);
170}
171
172void sb_reset_i2c_peripherals(const struct soc_i2c_peripheral_reset_info *reset_info)
173{
174 struct common_i2c_save save_table[MAX_PIN_COUNT];
175 uint8_t i;
176
177 if (!reset_info || !reset_info->i2c_scl || !reset_info->num_pins ||
178 !reset_info->i2c_scl_reset_mask)
179 return;
180
181 assert(reset_info->num_pins <= MAX_PIN_COUNT);
182
183 /* Save and reprogram I2C SCL pins */
184 for (i = 0; i < reset_info->num_pins; i++) {
185 save_i2c_pin_registers(reset_info->i2c_scl[i].pin.gpio, &save_table[i]);
186 program_gpios(&reset_info->i2c_scl[i].pin, 1);
187 }
188
189 /*
190 * Toggle SCL back and forth 9 times under 100KHz. A single read is
191 * needed after the writes to force the posted write to complete.
192 */
193 for (i = 0; i < 9; i++) {
194 drive_scl(reset_info, GPIO_OUTPUT_OUT_HIGH);
195 drive_scl(reset_info, GPIO_OUTPUT_OUT_LOW);
196 }
197
198 /* Restore I2C pins. */
199 for (i = 0; i < reset_info->num_pins; i++)
200 restore_i2c_pin_registers(reset_info->i2c_scl[i].pin.gpio, &save_table[i]);
201}