1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <acpi/acpigen.h>
4 #include <assert.h>
5 #include <amdblocks/acpimmio.h>
6 #include <amdblocks/gpio.h>
7 #include <amdblocks/gpio_defs.h>
8 #include <amdblocks/i2c.h>
9 #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>
15 #include <gpio.h>
16 #include <types.h>
17
18 #define MAX_PIN_COUNT 4
19
dw_i2c_base_address(unsigned int bus)20 uintptr_t dw_i2c_base_address(unsigned int bus)
21 {
22 size_t num_ctrlrs;
23 const struct soc_i2c_ctrlr_info *ctrlr = soc_get_i2c_ctrlr_info(&num_ctrlrs);
24
25 if (bus >= num_ctrlrs) {
26 printk(BIOS_ERR, "Bus ID %u is >= number of I2C controllers %zu\n",
27 bus, num_ctrlrs);
28 return 0;
29 }
30
31 return ctrlr[bus].bar;
32 }
33
dw_i2c_get_soc_cfg(unsigned int bus)34 const struct dw_i2c_bus_config *dw_i2c_get_soc_cfg(unsigned int bus)
35 {
36 size_t num_buses = 0;
37 const struct dw_i2c_bus_config *cfg = soc_get_i2c_bus_config(&num_buses);
38
39 if (bus >= num_buses) {
40 printk(BIOS_ERR, "Bus ID %u is >= number of I2C buses %zu\n", bus, num_buses);
41 return NULL;
42 }
43
44 return &cfg[bus];
45 }
46
47 #if CONFIG(HAVE_ACPI_TABLES)
i2c_acpi_name(const struct device * dev)48 static const char *i2c_acpi_name(const struct device *dev)
49 {
50 size_t i;
51 size_t num_ctrlrs;
52 const struct soc_i2c_ctrlr_info *ctrlr = soc_get_i2c_ctrlr_info(&num_ctrlrs);
53
54 if (!(uintptr_t)dev->path.mmio.addr) {
55 printk(BIOS_ERR, "NULL MMIO address at %s\n", __func__);
56 return NULL;
57 }
58
59 for (i = 0; i < num_ctrlrs; i++) {
60 if ((uintptr_t)dev->path.mmio.addr == ctrlr[i].bar)
61 return ctrlr[i].acpi_name;
62 }
63 printk(BIOS_ERR, "%s: Could not find %lu\n", __func__, (uintptr_t)dev->path.mmio.addr);
64 return NULL;
65 }
66
i2c_acpi_fill_ssdt(const struct device * dev)67 static void i2c_acpi_fill_ssdt(const struct device *dev)
68 {
69 dw_i2c_acpi_fill_ssdt(dev);
70
71 acpigen_write_scope(acpi_device_path(dev));
72 acpigen_write_store_int_to_namestr(acpi_device_status(dev), "STAT");
73 acpigen_pop_len(); /* Scope */
74 }
75 #endif
76
dw_i2c_soc_dev_to_bus(const struct device * dev)77 int dw_i2c_soc_dev_to_bus(const struct device *dev)
78 {
79 size_t i;
80 size_t num_ctrlrs;
81 const struct soc_i2c_ctrlr_info *ctrlr = soc_get_i2c_ctrlr_info(&num_ctrlrs);
82
83 if (!(uintptr_t)dev->path.mmio.addr) {
84 printk(BIOS_ERR, "NULL MMIO address at %s\n", __func__);
85 return -1;
86 }
87
88 for (i = 0; i < num_ctrlrs; i++) {
89 if ((uintptr_t)dev->path.mmio.addr == ctrlr[i].bar)
90 return i;
91 }
92 printk(BIOS_ERR, "%s: Could not find %lu\n", __func__, (uintptr_t)dev->path.mmio.addr);
93 return -1;
94 }
95
dw_i2c_soc_init(bool is_early_init)96 static void dw_i2c_soc_init(bool is_early_init)
97 {
98 unsigned int bus;
99 size_t num_buses = 0, num_ctrlrs = 0;
100 const struct dw_i2c_bus_config *cfg = soc_get_i2c_bus_config(&num_buses);
101 const struct soc_i2c_ctrlr_info *ctrlr = soc_get_i2c_ctrlr_info(&num_ctrlrs);
102
103 /* Ensure that the number of controllers in devicetree and SoC match. */
104 assert(num_buses == num_ctrlrs);
105
106 for (bus = 0; bus < num_buses; bus++, cfg++, ctrlr++) {
107 /*
108 * Skip initialization when controller is in peripheral mode or base address
109 * is not configured or is not the expected stage to initialize.
110 */
111 if (ctrlr->mode == I2C_PERIPHERAL_MODE || !ctrlr->bar ||
112 cfg->early_init != is_early_init)
113 continue;
114
115 if (dw_i2c_init(bus, cfg) != CB_SUCCESS) {
116 printk(BIOS_ERR, "Failed to init i2c bus %u\n", bus);
117 continue;
118 }
119
120 soc_i2c_misc_init(bus, cfg);
121 }
122 }
123
i2c_soc_early_init(void)124 void i2c_soc_early_init(void)
125 {
126 dw_i2c_soc_init(true);
127 }
128
i2c_soc_init(void)129 void i2c_soc_init(void)
130 {
131 dw_i2c_soc_init(false);
132 }
133
i2c_read_resources(struct device * dev)134 static void i2c_read_resources(struct device *dev)
135 {
136 mmio_range(dev, 0, dev->path.mmio.addr, 4 * KiB);
137 }
138
139 struct device_operations soc_amd_i2c_mmio_ops = {
140 .read_resources = i2c_read_resources,
141 .set_resources = noop_set_resources,
142 .scan_bus = scan_smbus,
143 #if CONFIG(HAVE_ACPI_TABLES)
144 .acpi_name = i2c_acpi_name,
145 .acpi_fill_ssdt = i2c_acpi_fill_ssdt,
146 #endif
147 .ops_i2c_bus = &dw_i2c_bus_ops,
148 };
149
drive_scl(const struct soc_i2c_peripheral_reset_info * reset_info,int val)150 static void drive_scl(const struct soc_i2c_peripheral_reset_info *reset_info, int val)
151 {
152 size_t j;
153
154 for (j = 0; j < reset_info->num_pins; j++) {
155 if (reset_info->i2c_scl_reset_mask & reset_info->i2c_scl[j].pin_mask)
156 gpio_set(reset_info->i2c_scl[j].pin.gpio, val);
157 }
158
159 gpio_get(0); /* Flush posted write */
160 /*
161 * TODO(b/183010197): 4usec gets 85KHz for 1 pin, 70KHz for 4 pins. Ensure this delay
162 * works fine for all SoCs and make this delay configurable if required.
163 */
164 udelay(4);
165 }
166
sb_reset_i2c_peripherals(const struct soc_i2c_peripheral_reset_info * reset_info)167 void sb_reset_i2c_peripherals(const struct soc_i2c_peripheral_reset_info *reset_info)
168 {
169 struct soc_amd_gpio_register_save save_table[MAX_PIN_COUNT];
170 size_t i;
171
172 if (!reset_info || !reset_info->i2c_scl || !reset_info->num_pins ||
173 !reset_info->i2c_scl_reset_mask)
174 return;
175
176 assert(reset_info->num_pins <= MAX_PIN_COUNT);
177
178 /* Save and reprogram I2C SCL pins */
179 for (i = 0; i < reset_info->num_pins; i++) {
180 /* To program I2C pins without destroying their programming, the registers
181 that will be changed need to be saved first */
182 gpio_save_pin_registers(reset_info->i2c_scl[i].pin.gpio, &save_table[i]);
183 /* Program SCL GPIO as output driven high */
184 gpio_configure_pads(&reset_info->i2c_scl[i].pin, 1);
185 }
186
187 /*
188 * Toggle SCL back and forth 9 times under 100KHz. A single read is
189 * needed after the writes to force the posted write to complete.
190 */
191 for (i = 0; i < 9; i++) {
192 drive_scl(reset_info, 1);
193 drive_scl(reset_info, 0);
194 }
195
196 /* Restore I2C pins. */
197 for (i = 0; i < reset_info->num_pins; i++)
198 gpio_restore_pin_registers(reset_info->i2c_scl[i].pin.gpio, &save_table[i]);
199 }
200