Merge tag 'efi-2020-07-rc6' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / arch / x86 / cpu / apollolake / hostbridge.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2019 Google LLC
4  */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <dt-structs.h>
9 #include <log.h>
10 #include <spl.h>
11 #include <asm/intel_pinctrl.h>
12 #include <asm/intel_regs.h>
13 #include <asm/pci.h>
14 #include <asm/arch/systemagent.h>
15
16 /**
17  * struct apl_hostbridge_platdata - platform data for hostbridge
18  *
19  * @dtplat: Platform data for of-platdata
20  * @early_pads: Early pad data to set up, each (pad, cfg0, cfg1)
21  * @early_pads_count: Number of pads to process
22  * @pciex_region_size: BAR length in bytes
23  * @bdf: Bus/device/function of hostbridge
24  */
25 struct apl_hostbridge_platdata {
26 #if CONFIG_IS_ENABLED(OF_PLATDATA)
27         struct dtd_intel_apl_hostbridge dtplat;
28 #endif
29         u32 *early_pads;
30         int early_pads_count;
31         uint pciex_region_size;
32         pci_dev_t bdf;
33 };
34
35 enum {
36         PCIEXBAR                = 0x60,
37         PCIEXBAR_LENGTH_256MB   = 0,
38         PCIEXBAR_LENGTH_128MB,
39         PCIEXBAR_LENGTH_64MB,
40
41         PCIEXBAR_PCIEXBAREN     = 1 << 0,
42
43         TSEG                    = 0xb8,  /* TSEG base */
44 };
45
46 static int apl_hostbridge_early_init_pinctrl(struct udevice *dev)
47 {
48         struct apl_hostbridge_platdata *plat = dev_get_platdata(dev);
49         struct udevice *pinctrl;
50         int ret;
51
52         ret = uclass_first_device_err(UCLASS_PINCTRL, &pinctrl);
53         if (ret)
54                 return log_msg_ret("no hostbridge pinctrl", ret);
55
56         return pinctrl_config_pads(pinctrl, plat->early_pads,
57                                    plat->early_pads_count);
58 }
59
60 static int apl_hostbridge_early_init(struct udevice *dev)
61 {
62         struct apl_hostbridge_platdata *plat = dev_get_platdata(dev);
63         u32 region_size;
64         ulong base;
65         u32 reg;
66         int ret;
67
68         /* Set up the MCHBAR */
69         pci_x86_read_config(plat->bdf, MCHBAR, &base, PCI_SIZE_32);
70         base = MCH_BASE_ADDRESS;
71         pci_x86_write_config(plat->bdf, MCHBAR, base | 1, PCI_SIZE_32);
72
73         /*
74          * The PCIEXBAR is assumed to live in the memory mapped IO space under
75          * 4GiB
76          */
77         pci_x86_write_config(plat->bdf, PCIEXBAR + 4, 0, PCI_SIZE_32);
78
79         switch (plat->pciex_region_size >> 20) {
80         default:
81         case 256:
82                 region_size = PCIEXBAR_LENGTH_256MB;
83                 break;
84         case 128:
85                 region_size = PCIEXBAR_LENGTH_128MB;
86                 break;
87         case 64:
88                 region_size = PCIEXBAR_LENGTH_64MB;
89                 break;
90         }
91
92         reg = CONFIG_MMCONF_BASE_ADDRESS | (region_size << 1)
93                                 | PCIEXBAR_PCIEXBAREN;
94         pci_x86_write_config(plat->bdf, PCIEXBAR, reg, PCI_SIZE_32);
95
96         /*
97          * TSEG defines the base of SMM range. BIOS determines the base
98          * of TSEG memory which must be at or below Graphics base of GTT
99          * Stolen memory, hence its better to clear TSEG register early
100          * to avoid power on default non-zero value (if any).
101          */
102         pci_x86_write_config(plat->bdf, TSEG, 0, PCI_SIZE_32);
103
104         ret = apl_hostbridge_early_init_pinctrl(dev);
105         if (ret)
106                 return log_msg_ret("pinctrl", ret);
107
108         return 0;
109 }
110
111 static int apl_hostbridge_ofdata_to_platdata(struct udevice *dev)
112 {
113         struct apl_hostbridge_platdata *plat = dev_get_platdata(dev);
114         struct udevice *pinctrl;
115         int ret;
116
117         /*
118          * The host bridge holds the early pad data needed to get through TPL.
119          * This is a small amount of data, enough to fit in TPL, so we keep it
120          * separate from the full pad data, stored in the fsp-s subnode. That
121          * subnode is not present in TPL, to save space.
122          */
123         ret = uclass_first_device_err(UCLASS_PINCTRL, &pinctrl);
124         if (ret)
125                 return log_msg_ret("no hostbridge PINCTRL", ret);
126 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
127         int root;
128
129         /* Get length of PCI Express Region */
130         plat->pciex_region_size = dev_read_u32_default(dev, "pciex-region-size",
131                                                        256 << 20);
132
133         root = pci_get_devfn(dev);
134         if (root < 0)
135                 return log_msg_ret("Cannot get host-bridge PCI address", root);
136         plat->bdf = root;
137
138         ret = pinctrl_read_pads(pinctrl, dev_ofnode(dev), "early-pads",
139                                 &plat->early_pads, &plat->early_pads_count);
140         if (ret)
141                 return log_msg_ret("early-pads", ret);
142 #else
143         struct dtd_intel_apl_hostbridge *dtplat = &plat->dtplat;
144         int size;
145
146         plat->pciex_region_size = dtplat->pciex_region_size;
147         plat->bdf = pci_ofplat_get_devfn(dtplat->reg[0]);
148
149         /* Assume that if everything is 0, it is empty */
150         plat->early_pads = dtplat->early_pads;
151         size = ARRAY_SIZE(dtplat->early_pads);
152         plat->early_pads_count = pinctrl_count_pads(pinctrl, plat->early_pads,
153                                                     size);
154
155 #endif
156
157         return 0;
158 }
159
160 static int apl_hostbridge_probe(struct udevice *dev)
161 {
162         if (spl_phase() == PHASE_TPL)
163                 return apl_hostbridge_early_init(dev);
164
165         return 0;
166 }
167
168 static const struct udevice_id apl_hostbridge_ids[] = {
169         { .compatible = "intel,apl-hostbridge" },
170         { }
171 };
172
173 U_BOOT_DRIVER(apl_hostbridge_drv) = {
174         .name           = "intel_apl_hostbridge",
175         .id             = UCLASS_NORTHBRIDGE,
176         .of_match       = apl_hostbridge_ids,
177         .ofdata_to_platdata = apl_hostbridge_ofdata_to_platdata,
178         .probe          = apl_hostbridge_probe,
179         .platdata_auto_alloc_size = sizeof(struct apl_hostbridge_platdata),
180 };