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