Jianjun Wang | ac1410d | 2021-07-14 15:39:40 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <commonlib/stdlib.h> |
| 4 | #include <console/console.h> |
| 5 | #include <device/mmio.h> |
| 6 | #include <device/resource.h> |
| 7 | #include <delay.h> |
| 8 | #include <soc/addressmap.h> |
Jianjun Wang | aa751cc | 2022-03-23 15:38:56 +0800 | [diff] [blame] | 9 | #include <soc/early_init.h> |
Jianjun Wang | ac1410d | 2021-07-14 15:39:40 +0800 | [diff] [blame] | 10 | #include <soc/gpio.h> |
| 11 | #include <soc/pcie.h> |
| 12 | #include <soc/pcie_common.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | #define PCIE_REG_BASE_PORT0 0x112f0000 |
| 17 | #define PCIE_RST_CTRL_REG (PCIE_REG_BASE_PORT0 + 0x148) |
| 18 | #define PCIE_MAC_RSTB BIT(0) |
| 19 | #define PCIE_PHY_RSTB BIT(1) |
| 20 | #define PCIE_BRG_RSTB BIT(2) |
| 21 | #define PCIE_PE_RSTB BIT(3) |
| 22 | |
Jianjun Wang | ac1410d | 2021-07-14 15:39:40 +0800 | [diff] [blame] | 23 | struct pad_func { |
| 24 | gpio_t gpio; |
| 25 | u8 func; |
| 26 | }; |
| 27 | |
| 28 | #define PAD_FUNC(name, func) {GPIO(name), PAD_##name##_FUNC_##func} |
| 29 | |
| 30 | static const struct pad_func pcie_pins[2][3] = { |
| 31 | { |
| 32 | PAD_FUNC(PCIE_WAKE_N, WAKEN), |
| 33 | PAD_FUNC(PCIE_PERESET_N, PERSTN), |
| 34 | PAD_FUNC(PCIE_CLKREQ_N, CLKREQN), |
| 35 | }, |
| 36 | { |
| 37 | PAD_FUNC(CMMCLK0, PERSTN_1), |
| 38 | PAD_FUNC(CMMCLK1, CLKREQN_1), |
| 39 | PAD_FUNC(CMMCLK2, WAKEN_1), |
| 40 | }, |
| 41 | }; |
| 42 | |
| 43 | static void mtk_pcie_set_pinmux(uint8_t port) |
| 44 | { |
| 45 | const struct pad_func *pins = pcie_pins[port]; |
| 46 | size_t i; |
| 47 | |
| 48 | for (i = 0; i < ARRAY_SIZE(pcie_pins[port]); i++) { |
| 49 | gpio_set_mode(pins[i].gpio, pins[i].func); |
| 50 | gpio_set_pull(pins[i].gpio, GPIO_PULL_ENABLE, GPIO_PULL_UP); |
| 51 | } |
| 52 | } |
| 53 | |
Jianjun Wang | c0808b6 | 2022-03-14 20:38:18 +0800 | [diff] [blame] | 54 | void mtk_pcie_reset(uintptr_t reg, bool enable) |
Jianjun Wang | ac1410d | 2021-07-14 15:39:40 +0800 | [diff] [blame] | 55 | { |
| 56 | uint32_t val; |
| 57 | |
| 58 | val = read32p(reg); |
| 59 | |
| 60 | if (enable) |
| 61 | val |= PCIE_MAC_RSTB | PCIE_PHY_RSTB | PCIE_BRG_RSTB | |
| 62 | PCIE_PE_RSTB; |
| 63 | else |
| 64 | val &= ~(PCIE_MAC_RSTB | PCIE_PHY_RSTB | PCIE_BRG_RSTB | |
| 65 | PCIE_PE_RSTB); |
| 66 | |
| 67 | write32p(reg, val); |
| 68 | } |
| 69 | |
| 70 | void mtk_pcie_pre_init(void) |
| 71 | { |
| 72 | mtk_pcie_set_pinmux(0); |
| 73 | |
| 74 | /* Assert all reset signals at early stage */ |
| 75 | mtk_pcie_reset(PCIE_RST_CTRL_REG, true); |
Jianjun Wang | aa751cc | 2022-03-23 15:38:56 +0800 | [diff] [blame] | 76 | |
| 77 | early_init_save_time(EARLY_INIT_PCIE); |
Jianjun Wang | ac1410d | 2021-07-14 15:39:40 +0800 | [diff] [blame] | 78 | } |