blob: 960d0bb4808a7de7e2aa299f48c1d09440a24019 [file] [log] [blame]
Macpaul Lin5d16f8d2022-08-11 16:27:10 +08001/* SPDX-License-Identifier: GPL-2.0-only OR MIT */
Jianjun Wangac1410d2021-07-14 15:39:40 +08002
Jianjun Wangac1410d2021-07-14 15:39:40 +08003#include <device/mmio.h>
4#include <device/resource.h>
Elyes Haouas5316abe2023-01-11 09:46:57 +01005#include <gpio.h>
Jianjun Wangac1410d2021-07-14 15:39:40 +08006#include <soc/addressmap.h>
Jianjun Wangaa751cc2022-03-23 15:38:56 +08007#include <soc/early_init.h>
Jianjun Wangac1410d2021-07-14 15:39:40 +08008#include <soc/pcie.h>
9#include <soc/pcie_common.h>
10#include <stdlib.h>
Jianjun Wangac1410d2021-07-14 15:39:40 +080011
12#define PCIE_REG_BASE_PORT0 0x112f0000
13#define PCIE_RST_CTRL_REG (PCIE_REG_BASE_PORT0 + 0x148)
14#define PCIE_MAC_RSTB BIT(0)
15#define PCIE_PHY_RSTB BIT(1)
16#define PCIE_BRG_RSTB BIT(2)
17#define PCIE_PE_RSTB BIT(3)
18
Jianjun Wangac1410d2021-07-14 15:39:40 +080019struct pad_func {
20 gpio_t gpio;
21 u8 func;
22};
23
24#define PAD_FUNC(name, func) {GPIO(name), PAD_##name##_FUNC_##func}
25
26static const struct pad_func pcie_pins[2][3] = {
27 {
28 PAD_FUNC(PCIE_WAKE_N, WAKEN),
29 PAD_FUNC(PCIE_PERESET_N, PERSTN),
30 PAD_FUNC(PCIE_CLKREQ_N, CLKREQN),
31 },
32 {
33 PAD_FUNC(CMMCLK0, PERSTN_1),
34 PAD_FUNC(CMMCLK1, CLKREQN_1),
35 PAD_FUNC(CMMCLK2, WAKEN_1),
36 },
37};
38
39static void mtk_pcie_set_pinmux(uint8_t port)
40{
41 const struct pad_func *pins = pcie_pins[port];
42 size_t i;
43
44 for (i = 0; i < ARRAY_SIZE(pcie_pins[port]); i++) {
45 gpio_set_mode(pins[i].gpio, pins[i].func);
46 gpio_set_pull(pins[i].gpio, GPIO_PULL_ENABLE, GPIO_PULL_UP);
47 }
48}
49
Jianjun Wangc0808b62022-03-14 20:38:18 +080050void mtk_pcie_reset(uintptr_t reg, bool enable)
Jianjun Wangac1410d2021-07-14 15:39:40 +080051{
52 uint32_t val;
53
54 val = read32p(reg);
55
56 if (enable)
57 val |= PCIE_MAC_RSTB | PCIE_PHY_RSTB | PCIE_BRG_RSTB |
58 PCIE_PE_RSTB;
59 else
60 val &= ~(PCIE_MAC_RSTB | PCIE_PHY_RSTB | PCIE_BRG_RSTB |
61 PCIE_PE_RSTB);
62
63 write32p(reg, val);
64}
65
66void mtk_pcie_pre_init(void)
67{
68 mtk_pcie_set_pinmux(0);
69
70 /* Assert all reset signals at early stage */
71 mtk_pcie_reset(PCIE_RST_CTRL_REG, true);
Jianjun Wangaa751cc2022-03-23 15:38:56 +080072
73 early_init_save_time(EARLY_INIT_PCIE);
Jianjun Wangac1410d2021-07-14 15:39:40 +080074}