Karthikeyan Ramasubramanian | 0dbea48 | 2021-03-08 23:23:50 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
Karthikeyan Ramasubramanian | 4f87ae1 | 2021-03-18 23:16:29 -0600 | [diff] [blame] | 3 | #include <acpi/acpi.h> |
Karthikeyan Ramasubramanian | 0dbea48 | 2021-03-08 23:23:50 -0700 | [diff] [blame] | 4 | #include <assert.h> |
Karthikeyan Ramasubramanian | 0dbea48 | 2021-03-08 23:23:50 -0700 | [diff] [blame] | 5 | #include <amdblocks/acpimmio.h> |
Felix Held | dea4e0f | 2021-09-22 20:05:53 +0200 | [diff] [blame] | 6 | #include <amdblocks/gpio.h> |
Karthikeyan Ramasubramanian | 0dbea48 | 2021-03-08 23:23:50 -0700 | [diff] [blame] | 7 | #include <amdblocks/gpio_defs.h> |
| 8 | #include <amdblocks/i2c.h> |
Karthikeyan Ramasubramanian | 4f87ae1 | 2021-03-18 23:16:29 -0600 | [diff] [blame] | 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> |
Felix Held | 96c4882 | 2021-08-02 19:48:40 +0200 | [diff] [blame] | 15 | #include <gpio.h> |
Angel Pons | c57cae8 | 2021-03-25 11:29:32 +0100 | [diff] [blame] | 16 | #include <types.h> |
Karthikeyan Ramasubramanian | 0dbea48 | 2021-03-08 23:23:50 -0700 | [diff] [blame] | 17 | |
| 18 | #define MAX_PIN_COUNT 4 |
| 19 | |
Karthikeyan Ramasubramanian | 4f87ae1 | 2021-03-18 23:16:29 -0600 | [diff] [blame] | 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) { |
Angel Pons | cf72a51 | 2021-03-25 11:15:32 +0100 | [diff] [blame] | 26 | printk(BIOS_ERR, "Bus ID %u is >= number of I2C controllers %zu\n", |
Karthikeyan Ramasubramanian | 4f87ae1 | 2021-03-18 23:16:29 -0600 | [diff] [blame] | 27 | bus, num_ctrlrs); |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | return ctrlr[bus].bar; |
| 32 | } |
| 33 | |
| 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) { |
Angel Pons | cf72a51 | 2021-03-25 11:15:32 +0100 | [diff] [blame] | 40 | printk(BIOS_ERR, "Bus ID %u is >= number of I2C buses %zu\n", bus, num_buses); |
Karthikeyan Ramasubramanian | 4f87ae1 | 2021-03-18 23:16:29 -0600 | [diff] [blame] | 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | return &cfg[bus]; |
| 45 | } |
| 46 | |
| 47 | static 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 | |
| 64 | int 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 Ramasubramanian | 4f87ae1 | 2021-03-18 23:16:29 -0600 | [diff] [blame] | 81 | static 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 Held | a16a09f | 2021-03-25 02:07:23 +0100 | [diff] [blame] | 100 | if (dw_i2c_init(bus, cfg)) { |
Angel Pons | cf72a51 | 2021-03-25 11:15:32 +0100 | [diff] [blame] | 101 | printk(BIOS_ERR, "Failed to init i2c bus %u\n", bus); |
Karthikeyan Ramasubramanian | 4f87ae1 | 2021-03-18 23:16:29 -0600 | [diff] [blame] | 102 | continue; |
Felix Held | a16a09f | 2021-03-25 02:07:23 +0100 | [diff] [blame] | 103 | } |
Karthikeyan Ramasubramanian | 4f87ae1 | 2021-03-18 23:16:29 -0600 | [diff] [blame] | 104 | |
| 105 | soc_i2c_misc_init(bus, cfg); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void i2c_soc_early_init(void) |
| 110 | { |
| 111 | dw_i2c_soc_init(true); |
| 112 | } |
| 113 | |
| 114 | void i2c_soc_init(void) |
| 115 | { |
| 116 | dw_i2c_soc_init(false); |
| 117 | } |
| 118 | |
Felix Held | c2cee06 | 2021-10-12 23:35:11 +0200 | [diff] [blame] | 119 | static void i2c_read_resources(struct device *dev) |
| 120 | { |
| 121 | mmio_resource(dev, 0, dev->path.mmio.addr / KiB, 4); |
| 122 | } |
| 123 | |
Karthikeyan Ramasubramanian | 4f87ae1 | 2021-03-18 23:16:29 -0600 | [diff] [blame] | 124 | struct device_operations soc_amd_i2c_mmio_ops = { |
Felix Held | c2cee06 | 2021-10-12 23:35:11 +0200 | [diff] [blame] | 125 | .read_resources = i2c_read_resources, |
Karthikeyan Ramasubramanian | 4f87ae1 | 2021-03-18 23:16:29 -0600 | [diff] [blame] | 126 | .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 Held | 96c4882 | 2021-08-02 19:48:40 +0200 | [diff] [blame] | 132 | static void drive_scl(const struct soc_i2c_peripheral_reset_info *reset_info, int val) |
Karthikeyan Ramasubramanian | 0dbea48 | 2021-03-08 23:23:50 -0700 | [diff] [blame] | 133 | { |
Angel Pons | c57cae8 | 2021-03-25 11:29:32 +0100 | [diff] [blame] | 134 | size_t j; |
Karthikeyan Ramasubramanian | 0dbea48 | 2021-03-08 23:23:50 -0700 | [diff] [blame] | 135 | |
| 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 Held | 96c4882 | 2021-08-02 19:48:40 +0200 | [diff] [blame] | 138 | gpio_set(reset_info->i2c_scl[j].pin.gpio, val); |
Karthikeyan Ramasubramanian | 0dbea48 | 2021-03-08 23:23:50 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Felix Held | 96c4882 | 2021-08-02 19:48:40 +0200 | [diff] [blame] | 141 | gpio_get(0); /* Flush posted write */ |
Karthikeyan Ramasubramanian | 0dbea48 | 2021-03-08 23:23:50 -0700 | [diff] [blame] | 142 | /* |
| 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 | |
| 149 | void sb_reset_i2c_peripherals(const struct soc_i2c_peripheral_reset_info *reset_info) |
| 150 | { |
Felix Held | f363ad4 | 2021-07-31 03:59:28 +0200 | [diff] [blame] | 151 | struct soc_amd_gpio_register_save save_table[MAX_PIN_COUNT]; |
Angel Pons | c57cae8 | 2021-03-25 11:29:32 +0100 | [diff] [blame] | 152 | size_t i; |
Karthikeyan Ramasubramanian | 0dbea48 | 2021-03-08 23:23:50 -0700 | [diff] [blame] | 153 | |
| 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 Held | 35cee38 | 2021-08-03 02:55:34 +0200 | [diff] [blame] | 162 | /* 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 Held | 96c4882 | 2021-08-02 19:48:40 +0200 | [diff] [blame] | 165 | /* Program SCL GPIO as output driven high */ |
Felix Held | 7011fa1 | 2021-09-22 16:36:12 +0200 | [diff] [blame] | 166 | gpio_configure_pads(&reset_info->i2c_scl[i].pin, 1); |
Karthikeyan Ramasubramanian | 0dbea48 | 2021-03-08 23:23:50 -0700 | [diff] [blame] | 167 | } |
| 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 Held | 96c4882 | 2021-08-02 19:48:40 +0200 | [diff] [blame] | 174 | drive_scl(reset_info, 1); |
| 175 | drive_scl(reset_info, 0); |
Karthikeyan Ramasubramanian | 0dbea48 | 2021-03-08 23:23:50 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* Restore I2C pins. */ |
| 179 | for (i = 0; i < reset_info->num_pins; i++) |
Felix Held | 35cee38 | 2021-08-03 02:55:34 +0200 | [diff] [blame] | 180 | gpio_restore_pin_registers(reset_info->i2c_scl[i].pin.gpio, &save_table[i]); |
Karthikeyan Ramasubramanian | 0dbea48 | 2021-03-08 23:23:50 -0700 | [diff] [blame] | 181 | } |