Angel Pons | ae59387 | 2020-04-04 18:50:57 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 2 | |
| 3 | #include <device/mmio.h> |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 4 | #include <acpi/acpi.h> |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 5 | #include <console/console.h> |
| 6 | #include <delay.h> |
Felix Held | 00058f5 | 2020-04-06 23:36:24 +0200 | [diff] [blame] | 7 | #include <device/device.h> |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 8 | #include <drivers/i2c/designware/dw_i2c.h> |
| 9 | #include <amdblocks/acpimmio.h> |
Karthikeyan Ramasubramanian | 0dbea48 | 2021-03-08 23:23:50 -0700 | [diff] [blame^] | 10 | #include <amdblocks/i2c.h> |
Martin Roth | 7e78e56 | 2019-11-03 23:29:02 -0700 | [diff] [blame] | 11 | #include <soc/i2c.h> |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 12 | #include <soc/iomap.h> |
| 13 | #include <soc/pci_devs.h> |
| 14 | #include <soc/southbridge.h> |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 15 | #include "chip.h" |
| 16 | |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame] | 17 | #if ENV_X86 |
Martin Roth | 7e78e56 | 2019-11-03 23:29:02 -0700 | [diff] [blame] | 18 | static const uintptr_t i2c_bus_address[I2C_MASTER_DEV_COUNT + I2C_SLAVE_DEV_COUNT] = { |
| 19 | 0, |
| 20 | 0, |
Marshall Dawson | e2c24f7 | 2019-06-20 08:47:58 -0600 | [diff] [blame] | 21 | APU_I2C2_BASE, |
| 22 | APU_I2C3_BASE, |
Martin Roth | 7e78e56 | 2019-11-03 23:29:02 -0700 | [diff] [blame] | 23 | APU_I2C4_BASE, /* Can only be used in slave mode */ |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 24 | }; |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame] | 25 | #else |
| 26 | static uintptr_t i2c_bus_address[I2C_MASTER_DEV_COUNT + I2C_SLAVE_DEV_COUNT]; |
| 27 | #endif |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 28 | |
| 29 | uintptr_t dw_i2c_base_address(unsigned int bus) |
| 30 | { |
Martin Roth | 7e78e56 | 2019-11-03 23:29:02 -0700 | [diff] [blame] | 31 | if (bus >= ARRAY_SIZE(i2c_bus_address)) |
Marshall Dawson | e2c24f7 | 2019-06-20 08:47:58 -0600 | [diff] [blame] | 32 | return 0; |
| 33 | |
Martin Roth | 7e78e56 | 2019-11-03 23:29:02 -0700 | [diff] [blame] | 34 | return i2c_bus_address[bus]; |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 35 | } |
| 36 | |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame] | 37 | #if !ENV_X86 |
| 38 | void i2c_set_bar(unsigned int bus, uintptr_t bar) |
| 39 | { |
| 40 | if (bus >= ARRAY_SIZE(i2c_bus_address)) { |
| 41 | printk(BIOS_ERR, "Error: i2c index out of bounds: %u.", bus); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | i2c_bus_address[bus] = bar; |
| 46 | } |
| 47 | #endif |
| 48 | |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 49 | const struct dw_i2c_bus_config *dw_i2c_get_soc_cfg(unsigned int bus) |
| 50 | { |
Marshall Dawson | bc4c903 | 2019-06-11 12:18:20 -0600 | [diff] [blame] | 51 | const struct soc_amd_picasso_config *config; |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 52 | |
Martin Roth | 7e78e56 | 2019-11-03 23:29:02 -0700 | [diff] [blame] | 53 | if (bus >= ARRAY_SIZE(config->i2c)) |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 54 | return NULL; |
| 55 | |
Felix Held | 00058f5 | 2020-04-06 23:36:24 +0200 | [diff] [blame] | 56 | /* config is not NULL; if it was, config_of_soc calls die() internally */ |
| 57 | config = config_of_soc(); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 58 | |
| 59 | return &config->i2c[bus]; |
| 60 | } |
| 61 | |
Furquan Shaikh | 4663749 | 2020-06-03 16:22:20 -0700 | [diff] [blame] | 62 | static const char *i2c_acpi_name(const struct device *dev) |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 63 | { |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame] | 64 | if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[2]) |
Marshall Dawson | 59e97b6 | 2019-08-15 17:49:11 -0600 | [diff] [blame] | 65 | return "I2C2"; |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame] | 66 | else if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[3]) |
Marshall Dawson | 59e97b6 | 2019-08-15 17:49:11 -0600 | [diff] [blame] | 67 | return "I2C3"; |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame] | 68 | else if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[4]) |
Marshall Dawson | 59e97b6 | 2019-08-15 17:49:11 -0600 | [diff] [blame] | 69 | return "I2C4"; |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame] | 70 | return NULL; |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 71 | } |
| 72 | |
Furquan Shaikh | 00296ea | 2020-04-24 21:33:02 -0700 | [diff] [blame] | 73 | int dw_i2c_soc_dev_to_bus(const struct device *dev) |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 74 | { |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame] | 75 | if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[2]) |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 76 | return 2; |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame] | 77 | else if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[3]) |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 78 | return 3; |
Martin Roth | ac41f58 | 2020-06-14 17:24:12 -0600 | [diff] [blame] | 79 | else if ((uintptr_t)dev->path.mmio.addr == i2c_bus_address[4]) |
Marshall Dawson | e2c24f7 | 2019-06-20 08:47:58 -0600 | [diff] [blame] | 80 | return 4; |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 81 | return -1; |
| 82 | } |
| 83 | |
Marshall Dawson | e2c24f7 | 2019-06-20 08:47:58 -0600 | [diff] [blame] | 84 | __weak void mainboard_i2c_override(int bus, uint32_t *pad_settings) { } |
| 85 | |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 86 | static void dw_i2c_soc_init(bool is_early_init) |
| 87 | { |
| 88 | size_t i; |
Marshall Dawson | bc4c903 | 2019-06-11 12:18:20 -0600 | [diff] [blame] | 89 | const struct soc_amd_picasso_config *config; |
Marshall Dawson | e2c24f7 | 2019-06-20 08:47:58 -0600 | [diff] [blame] | 90 | uint32_t pad_ctrl; |
| 91 | int misc_reg; |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 92 | |
Felix Held | 00058f5 | 2020-04-06 23:36:24 +0200 | [diff] [blame] | 93 | /* config is not NULL; if it was, config_of_soc calls die() internally */ |
| 94 | config = config_of_soc(); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 95 | |
Martin Roth | 7e78e56 | 2019-11-03 23:29:02 -0700 | [diff] [blame] | 96 | for (i = I2C_MASTER_START_INDEX; i < ARRAY_SIZE(config->i2c); i++) { |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 97 | const struct dw_i2c_bus_config *cfg = &config->i2c[i]; |
| 98 | |
| 99 | if (cfg->early_init != is_early_init) |
| 100 | continue; |
| 101 | |
Marshall Dawson | e2c24f7 | 2019-06-20 08:47:58 -0600 | [diff] [blame] | 102 | if (dw_i2c_init(i, cfg)) { |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 103 | printk(BIOS_ERR, "Failed to init i2c bus %zd\n", i); |
Marshall Dawson | e2c24f7 | 2019-06-20 08:47:58 -0600 | [diff] [blame] | 104 | continue; |
| 105 | } |
| 106 | |
| 107 | misc_reg = MISC_I2C0_PAD_CTRL + sizeof(uint32_t) * i; |
| 108 | pad_ctrl = misc_read32(misc_reg); |
| 109 | |
| 110 | pad_ctrl &= ~I2C_PAD_CTRL_NG_MASK; |
| 111 | pad_ctrl |= I2C_PAD_CTRL_NG_NORMAL; |
| 112 | |
| 113 | pad_ctrl &= ~I2C_PAD_CTRL_RX_SEL_MASK; |
| 114 | pad_ctrl |= I2C_PAD_CTRL_RX_SEL_3_3V; |
| 115 | |
| 116 | pad_ctrl &= ~I2C_PAD_CTRL_FALLSLEW_MASK; |
| 117 | pad_ctrl |= cfg->speed == I2C_SPEED_STANDARD |
| 118 | ? I2C_PAD_CTRL_FALLSLEW_STD |
| 119 | : I2C_PAD_CTRL_FALLSLEW_LOW; |
| 120 | pad_ctrl |= I2C_PAD_CTRL_FALLSLEW_EN; |
| 121 | |
| 122 | mainboard_i2c_override(i, &pad_ctrl); |
| 123 | misc_write32(misc_reg, pad_ctrl); |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | void i2c_soc_early_init(void) |
| 128 | { |
| 129 | dw_i2c_soc_init(true); |
| 130 | } |
| 131 | |
| 132 | void i2c_soc_init(void) |
| 133 | { |
| 134 | dw_i2c_soc_init(false); |
| 135 | } |
| 136 | |
Marshall Dawson | bc4c903 | 2019-06-11 12:18:20 -0600 | [diff] [blame] | 137 | struct device_operations picasso_i2c_mmio_ops = { |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 138 | /* TODO(teravest): Move I2C resource info here. */ |
Nico Huber | 2f8ba69 | 2020-04-05 14:05:24 +0200 | [diff] [blame] | 139 | .read_resources = noop_read_resources, |
| 140 | .set_resources = noop_set_resources, |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 141 | .scan_bus = scan_smbus, |
| 142 | .acpi_name = i2c_acpi_name, |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 143 | .acpi_fill_ssdt = dw_i2c_acpi_fill_ssdt, |
Martin Roth | 5c354b9 | 2019-04-22 14:55:16 -0600 | [diff] [blame] | 144 | }; |