blob: d2e047363796cc0d09d78c7fb4963a739281375f [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>
Felix Helddea4e0f2021-09-22 20:05:53 +02006#include <amdblocks/gpio.h>
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -07007#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>
Felix Held96c48822021-08-02 19:48:40 +020015#include <gpio.h>
Angel Ponsc57cae82021-03-25 11:29:32 +010016#include <types.h>
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -070017
18#define MAX_PIN_COUNT 4
19
Karthikeyan Ramasubramanian4f87ae12021-03-18 23:16:29 -060020uintptr_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) {
Angel Ponscf72a512021-03-25 11:15:32 +010026 printk(BIOS_ERR, "Bus ID %u is >= number of I2C controllers %zu\n",
Karthikeyan Ramasubramanian4f87ae12021-03-18 23:16:29 -060027 bus, num_ctrlrs);
28 return 0;
29 }
30
31 return ctrlr[bus].bar;
32}
33
34const 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) {
Angel Ponscf72a512021-03-25 11:15:32 +010040 printk(BIOS_ERR, "Bus ID %u is >= number of I2C buses %zu\n", bus, num_buses);
Karthikeyan Ramasubramanian4f87ae12021-03-18 23:16:29 -060041 return NULL;
42 }
43
44 return &cfg[bus];
45}
46
47static const char *i2c_acpi_name(const struct device *dev)
48{
49 size_t i;
50 size_t num_ctrlrs;
51 const struct soc_i2c_ctrlr_info *ctrlr = soc_get_i2c_ctrlr_info(&num_ctrlrs);
52
53 if (!(uintptr_t)dev->path.mmio.addr)
54 die("NULL MMIO address at %s\n", __func__);
55
56 for (i = 0; i < num_ctrlrs; i++) {
57 if ((uintptr_t)dev->path.mmio.addr == ctrlr[i].bar)
58 return ctrlr[i].acpi_name;
59 }
60 printk(BIOS_ERR, "%s: Could not find %lu\n", __func__, (uintptr_t)dev->path.mmio.addr);
61 return NULL;
62}
63
64int dw_i2c_soc_dev_to_bus(const struct device *dev)
65{
66 size_t i;
67 size_t num_ctrlrs;
68 const struct soc_i2c_ctrlr_info *ctrlr = soc_get_i2c_ctrlr_info(&num_ctrlrs);
69
70 if (!(uintptr_t)dev->path.mmio.addr)
71 die("NULL MMIO address at %s\n", __func__);
72
73 for (i = 0; i < num_ctrlrs; i++) {
74 if ((uintptr_t)dev->path.mmio.addr == ctrlr[i].bar)
75 return i;
76 }
77 printk(BIOS_ERR, "%s: Could not find %lu\n", __func__, (uintptr_t)dev->path.mmio.addr);
78 return -1;
79}
80
Karthikeyan Ramasubramanian4f87ae12021-03-18 23:16:29 -060081static void dw_i2c_soc_init(bool is_early_init)
82{
83 unsigned int bus;
84 size_t num_buses = 0, num_ctrlrs = 0;
85 const struct dw_i2c_bus_config *cfg = soc_get_i2c_bus_config(&num_buses);
86 const struct soc_i2c_ctrlr_info *ctrlr = soc_get_i2c_ctrlr_info(&num_ctrlrs);
87
88 /* Ensure that the number of controllers in devicetree and SoC match. */
89 assert(num_buses == num_ctrlrs);
90
91 for (bus = 0; bus < num_buses; bus++, cfg++, ctrlr++) {
92 /*
93 * Skip initialization when controller is in peripheral mode or base address
94 * is not configured or is not the expected stage to initialize.
95 */
96 if (ctrlr->mode == I2C_PERIPHERAL_MODE || !ctrlr->bar ||
97 cfg->early_init != is_early_init)
98 continue;
99
Felix Helda16a09f2021-03-25 02:07:23 +0100100 if (dw_i2c_init(bus, cfg)) {
Angel Ponscf72a512021-03-25 11:15:32 +0100101 printk(BIOS_ERR, "Failed to init i2c bus %u\n", bus);
Karthikeyan Ramasubramanian4f87ae12021-03-18 23:16:29 -0600102 continue;
Felix Helda16a09f2021-03-25 02:07:23 +0100103 }
Karthikeyan Ramasubramanian4f87ae12021-03-18 23:16:29 -0600104
105 soc_i2c_misc_init(bus, cfg);
106 }
107}
108
109void i2c_soc_early_init(void)
110{
111 dw_i2c_soc_init(true);
112}
113
114void i2c_soc_init(void)
115{
116 dw_i2c_soc_init(false);
117}
118
Felix Heldc2cee062021-10-12 23:35:11 +0200119static void i2c_read_resources(struct device *dev)
120{
121 mmio_resource(dev, 0, dev->path.mmio.addr / KiB, 4);
122}
123
Karthikeyan Ramasubramanian4f87ae12021-03-18 23:16:29 -0600124struct device_operations soc_amd_i2c_mmio_ops = {
Felix Heldc2cee062021-10-12 23:35:11 +0200125 .read_resources = i2c_read_resources,
Karthikeyan Ramasubramanian4f87ae12021-03-18 23:16:29 -0600126 .set_resources = noop_set_resources,
127 .scan_bus = scan_smbus,
128 .acpi_name = i2c_acpi_name,
129 .acpi_fill_ssdt = dw_i2c_acpi_fill_ssdt,
130};
131
Felix Held96c48822021-08-02 19:48:40 +0200132static void drive_scl(const struct soc_i2c_peripheral_reset_info *reset_info, int val)
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700133{
Angel Ponsc57cae82021-03-25 11:29:32 +0100134 size_t j;
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700135
136 for (j = 0; j < reset_info->num_pins; j++) {
137 if (reset_info->i2c_scl_reset_mask & reset_info->i2c_scl[j].pin_mask)
Felix Held96c48822021-08-02 19:48:40 +0200138 gpio_set(reset_info->i2c_scl[j].pin.gpio, val);
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700139 }
140
Felix Held96c48822021-08-02 19:48:40 +0200141 gpio_get(0); /* Flush posted write */
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700142 /*
143 * TODO(b/183010197): 4usec gets 85KHz for 1 pin, 70KHz for 4 pins. Ensure this delay
144 * works fine for all SoCs and make this delay configurable if required.
145 */
146 udelay(4);
147}
148
149void sb_reset_i2c_peripherals(const struct soc_i2c_peripheral_reset_info *reset_info)
150{
Felix Heldf363ad42021-07-31 03:59:28 +0200151 struct soc_amd_gpio_register_save save_table[MAX_PIN_COUNT];
Angel Ponsc57cae82021-03-25 11:29:32 +0100152 size_t i;
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700153
154 if (!reset_info || !reset_info->i2c_scl || !reset_info->num_pins ||
155 !reset_info->i2c_scl_reset_mask)
156 return;
157
158 assert(reset_info->num_pins <= MAX_PIN_COUNT);
159
160 /* Save and reprogram I2C SCL pins */
161 for (i = 0; i < reset_info->num_pins; i++) {
Felix Held35cee382021-08-03 02:55:34 +0200162 /* To program I2C pins without destroying their programming, the registers
163 that will be changed need to be saved first */
164 gpio_save_pin_registers(reset_info->i2c_scl[i].pin.gpio, &save_table[i]);
Felix Held96c48822021-08-02 19:48:40 +0200165 /* Program SCL GPIO as output driven high */
Felix Held7011fa12021-09-22 16:36:12 +0200166 gpio_configure_pads(&reset_info->i2c_scl[i].pin, 1);
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700167 }
168
169 /*
170 * Toggle SCL back and forth 9 times under 100KHz. A single read is
171 * needed after the writes to force the posted write to complete.
172 */
173 for (i = 0; i < 9; i++) {
Felix Held96c48822021-08-02 19:48:40 +0200174 drive_scl(reset_info, 1);
175 drive_scl(reset_info, 0);
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700176 }
177
178 /* Restore I2C pins. */
179 for (i = 0; i < reset_info->num_pins; i++)
Felix Held35cee382021-08-03 02:55:34 +0200180 gpio_restore_pin_registers(reset_info->i2c_scl[i].pin.gpio, &save_table[i]);
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700181}