blob: 892c83e91f7246900ce8a594cd360333f0c4426c [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
81void __weak soc_i2c_misc_init(unsigned int bus, const struct dw_i2c_bus_config *cfg)
82{
83 /* Nothing by default. */
84}
85
86static void dw_i2c_soc_init(bool is_early_init)
87{
88 unsigned int bus;
89 size_t num_buses = 0, num_ctrlrs = 0;
90 const struct dw_i2c_bus_config *cfg = soc_get_i2c_bus_config(&num_buses);
91 const struct soc_i2c_ctrlr_info *ctrlr = soc_get_i2c_ctrlr_info(&num_ctrlrs);
92
93 /* Ensure that the number of controllers in devicetree and SoC match. */
94 assert(num_buses == num_ctrlrs);
95
96 for (bus = 0; bus < num_buses; bus++, cfg++, ctrlr++) {
97 /*
98 * Skip initialization when controller is in peripheral mode or base address
99 * is not configured or is not the expected stage to initialize.
100 */
101 if (ctrlr->mode == I2C_PERIPHERAL_MODE || !ctrlr->bar ||
102 cfg->early_init != is_early_init)
103 continue;
104
Felix Helda16a09f2021-03-25 02:07:23 +0100105 if (dw_i2c_init(bus, cfg)) {
Angel Ponscf72a512021-03-25 11:15:32 +0100106 printk(BIOS_ERR, "Failed to init i2c bus %u\n", bus);
Karthikeyan Ramasubramanian4f87ae12021-03-18 23:16:29 -0600107 continue;
Felix Helda16a09f2021-03-25 02:07:23 +0100108 }
Karthikeyan Ramasubramanian4f87ae12021-03-18 23:16:29 -0600109
110 soc_i2c_misc_init(bus, cfg);
111 }
112}
113
114void i2c_soc_early_init(void)
115{
116 dw_i2c_soc_init(true);
117}
118
119void i2c_soc_init(void)
120{
121 dw_i2c_soc_init(false);
122}
123
124struct device_operations soc_amd_i2c_mmio_ops = {
125 /* TODO(kramasub): Move I2C resource info here. */
126 .read_resources = noop_read_resources,
127 .set_resources = noop_set_resources,
128 .scan_bus = scan_smbus,
129 .acpi_name = i2c_acpi_name,
130 .acpi_fill_ssdt = dw_i2c_acpi_fill_ssdt,
131};
132
Felix Held96c48822021-08-02 19:48:40 +0200133static void drive_scl(const struct soc_i2c_peripheral_reset_info *reset_info, int val)
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700134{
Angel Ponsc57cae82021-03-25 11:29:32 +0100135 size_t j;
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700136
137 for (j = 0; j < reset_info->num_pins; j++) {
138 if (reset_info->i2c_scl_reset_mask & reset_info->i2c_scl[j].pin_mask)
Felix Held96c48822021-08-02 19:48:40 +0200139 gpio_set(reset_info->i2c_scl[j].pin.gpio, val);
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700140 }
141
Felix Held96c48822021-08-02 19:48:40 +0200142 gpio_get(0); /* Flush posted write */
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700143 /*
144 * TODO(b/183010197): 4usec gets 85KHz for 1 pin, 70KHz for 4 pins. Ensure this delay
145 * works fine for all SoCs and make this delay configurable if required.
146 */
147 udelay(4);
148}
149
150void sb_reset_i2c_peripherals(const struct soc_i2c_peripheral_reset_info *reset_info)
151{
Felix Heldf363ad42021-07-31 03:59:28 +0200152 struct soc_amd_gpio_register_save save_table[MAX_PIN_COUNT];
Angel Ponsc57cae82021-03-25 11:29:32 +0100153 size_t i;
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700154
155 if (!reset_info || !reset_info->i2c_scl || !reset_info->num_pins ||
156 !reset_info->i2c_scl_reset_mask)
157 return;
158
159 assert(reset_info->num_pins <= MAX_PIN_COUNT);
160
161 /* Save and reprogram I2C SCL pins */
162 for (i = 0; i < reset_info->num_pins; i++) {
Felix Held35cee382021-08-03 02:55:34 +0200163 /* To program I2C pins without destroying their programming, the registers
164 that will be changed need to be saved first */
165 gpio_save_pin_registers(reset_info->i2c_scl[i].pin.gpio, &save_table[i]);
Felix Held96c48822021-08-02 19:48:40 +0200166 /* Program SCL GPIO as output driven high */
Felix Held7011fa12021-09-22 16:36:12 +0200167 gpio_configure_pads(&reset_info->i2c_scl[i].pin, 1);
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700168 }
169
170 /*
171 * Toggle SCL back and forth 9 times under 100KHz. A single read is
172 * needed after the writes to force the posted write to complete.
173 */
174 for (i = 0; i < 9; i++) {
Felix Held96c48822021-08-02 19:48:40 +0200175 drive_scl(reset_info, 1);
176 drive_scl(reset_info, 0);
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700177 }
178
179 /* Restore I2C pins. */
180 for (i = 0; i < reset_info->num_pins; i++)
Felix Held35cee382021-08-03 02:55:34 +0200181 gpio_restore_pin_registers(reset_info->i2c_scl[i].pin.gpio, &save_table[i]);
Karthikeyan Ramasubramanian0dbea482021-03-08 23:23:50 -0700182}