Merge tag 'efi-2020-07-rc6' of https://gitlab.denx.de/u-boot/custodians/u-boot-efi
[oweals/u-boot.git] / arch / x86 / cpu / ivybridge / bd82x6x.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2014 Google, Inc
4  */
5 #include <common.h>
6 #include <dm.h>
7 #include <errno.h>
8 #include <fdtdec.h>
9 #include <log.h>
10 #include <malloc.h>
11 #include <pch.h>
12 #include <asm/cpu.h>
13 #include <asm/intel_regs.h>
14 #include <asm/io.h>
15 #include <asm/lapic.h>
16 #include <asm/lpc_common.h>
17 #include <asm/pci.h>
18 #include <asm/arch/model_206ax.h>
19 #include <asm/arch/pch.h>
20 #include <asm/arch/sandybridge.h>
21 #include <linux/bitops.h>
22 #include <linux/delay.h>
23
24 DECLARE_GLOBAL_DATA_PTR;
25
26 #define GPIO_BASE               0x48
27 #define BIOS_CTRL               0xdc
28
29 #define RCBA_AUDIO_CONFIG       0x2030
30 #define RCBA_AUDIO_CONFIG_HDA   BIT(31)
31 #define RCBA_AUDIO_CONFIG_MASK  0xfe
32
33 #ifndef CONFIG_HAVE_FSP
34 static int pch_revision_id = -1;
35 static int pch_type = -1;
36
37 /**
38  * pch_silicon_revision() - Read silicon revision ID from the PCH
39  *
40  * @dev:        PCH device
41  * @return silicon revision ID
42  */
43 static int pch_silicon_revision(struct udevice *dev)
44 {
45         u8 val;
46
47         if (pch_revision_id < 0) {
48                 dm_pci_read_config8(dev, PCI_REVISION_ID, &val);
49                 pch_revision_id = val;
50         }
51
52         return pch_revision_id;
53 }
54
55 int pch_silicon_type(struct udevice *dev)
56 {
57         u8 val;
58
59         if (pch_type < 0) {
60                 dm_pci_read_config8(dev, PCI_DEVICE_ID + 1, &val);
61                 pch_type = val;
62         }
63
64         return pch_type;
65 }
66
67 /**
68  * pch_silicon_supported() - Check if a certain revision is supported
69  *
70  * @dev:        PCH device
71  * @type:       PCH type
72  * @rev:        Minimum required resion
73  * @return 0 if not supported, 1 if supported
74  */
75 static int pch_silicon_supported(struct udevice *dev, int type, int rev)
76 {
77         int cur_type = pch_silicon_type(dev);
78         int cur_rev = pch_silicon_revision(dev);
79
80         switch (type) {
81         case PCH_TYPE_CPT:
82                 /* CougarPoint minimum revision */
83                 if (cur_type == PCH_TYPE_CPT && cur_rev >= rev)
84                         return 1;
85                 /* PantherPoint any revision */
86                 if (cur_type == PCH_TYPE_PPT)
87                         return 1;
88                 break;
89
90         case PCH_TYPE_PPT:
91                 /* PantherPoint minimum revision */
92                 if (cur_type == PCH_TYPE_PPT && cur_rev >= rev)
93                         return 1;
94                 break;
95         }
96
97         return 0;
98 }
99
100 #define IOBP_RETRY 1000
101 static inline int iobp_poll(void)
102 {
103         unsigned try = IOBP_RETRY;
104         u32 data;
105
106         while (try--) {
107                 data = readl(RCB_REG(IOBPS));
108                 if ((data & 1) == 0)
109                         return 1;
110                 udelay(10);
111         }
112
113         printf("IOBP timeout\n");
114         return 0;
115 }
116
117 void pch_iobp_update(struct udevice *dev, u32 address, u32 andvalue,
118                      u32 orvalue)
119 {
120         u32 data;
121
122         /* Set the address */
123         writel(address, RCB_REG(IOBPIRI));
124
125         /* READ OPCODE */
126         if (pch_silicon_supported(dev, PCH_TYPE_CPT, PCH_STEP_B0))
127                 writel(IOBPS_RW_BX, RCB_REG(IOBPS));
128         else
129                 writel(IOBPS_READ_AX, RCB_REG(IOBPS));
130         if (!iobp_poll())
131                 return;
132
133         /* Read IOBP data */
134         data = readl(RCB_REG(IOBPD));
135         if (!iobp_poll())
136                 return;
137
138         /* Check for successful transaction */
139         if ((readl(RCB_REG(IOBPS)) & 0x6) != 0) {
140                 printf("IOBP read 0x%08x failed\n", address);
141                 return;
142         }
143
144         /* Update the data */
145         data &= andvalue;
146         data |= orvalue;
147
148         /* WRITE OPCODE */
149         if (pch_silicon_supported(dev, PCH_TYPE_CPT, PCH_STEP_B0))
150                 writel(IOBPS_RW_BX, RCB_REG(IOBPS));
151         else
152                 writel(IOBPS_WRITE_AX, RCB_REG(IOBPS));
153         if (!iobp_poll())
154                 return;
155
156         /* Write IOBP data */
157         writel(data, RCB_REG(IOBPD));
158         if (!iobp_poll())
159                 return;
160 }
161
162 static int bd82x6x_probe(struct udevice *dev)
163 {
164         if (!(gd->flags & GD_FLG_RELOC))
165                 return 0;
166
167         /* Cause the SATA device to do its init */
168         uclass_first_device(UCLASS_AHCI, &dev);
169
170         return 0;
171 }
172 #endif /* CONFIG_HAVE_FSP */
173
174 static int bd82x6x_pch_get_spi_base(struct udevice *dev, ulong *sbasep)
175 {
176         u32 rcba;
177
178         dm_pci_read_config32(dev, PCH_RCBA, &rcba);
179         /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable */
180         rcba = rcba & 0xffffc000;
181         *sbasep = rcba + 0x3800;
182
183         return 0;
184 }
185
186 static int bd82x6x_set_spi_protect(struct udevice *dev, bool protect)
187 {
188         return lpc_set_spi_protect(dev, BIOS_CTRL, protect);
189 }
190
191 static int bd82x6x_get_gpio_base(struct udevice *dev, u32 *gbasep)
192 {
193         u32 base;
194
195         /*
196          * GPIO_BASE moved to its current offset with ICH6, but prior to
197          * that it was unused (or undocumented). Check that it looks
198          * okay: not all ones or zeros.
199          *
200          * Note we don't need check bit0 here, because the Tunnel Creek
201          * GPIO base address register bit0 is reserved (read returns 0),
202          * while on the Ivybridge the bit0 is used to indicate it is an
203          * I/O space.
204          */
205         dm_pci_read_config32(dev, GPIO_BASE, &base);
206         if (base == 0x00000000 || base == 0xffffffff) {
207                 debug("%s: unexpected BASE value\n", __func__);
208                 return -ENODEV;
209         }
210
211         /*
212          * Okay, I guess we're looking at the right device. The actual
213          * GPIO registers are in the PCI device's I/O space, starting
214          * at the offset that we just read. Bit 0 indicates that it's
215          * an I/O address, not a memory address, so mask that off.
216          */
217         *gbasep = base & 1 ? base & ~3 : base & ~15;
218
219         return 0;
220 }
221
222 static int bd82x6x_ioctl(struct udevice *dev, enum pch_req_t req, void *data,
223                          int size)
224 {
225         u32 rcba, val;
226
227         switch (req) {
228         case PCH_REQ_HDA_CONFIG:
229                 dm_pci_read_config32(dev, PCH_RCBA, &rcba);
230                 val = readl(rcba + RCBA_AUDIO_CONFIG);
231                 if (!(val & RCBA_AUDIO_CONFIG_HDA))
232                         return -ENOENT;
233
234                 return val & RCBA_AUDIO_CONFIG_MASK;
235         case PCH_REQ_PMBASE_INFO: {
236                 struct pch_pmbase_info *pm = data;
237                 int ret;
238
239                 /* Find the base address of the powermanagement registers */
240                 ret = dm_pci_read_config16(dev, 0x40, &pm->base);
241                 if (ret)
242                         return ret;
243                 pm->base &= 0xfffe;
244                 pm->gpio0_en_ofs = GPE0_EN;
245                 pm->pm1_sts_ofs = PM1_STS;
246                 pm->pm1_cnt_ofs = PM1_CNT;
247
248                 return 0;
249         }
250         default:
251                 return -ENOSYS;
252         }
253 }
254
255 static const struct pch_ops bd82x6x_pch_ops = {
256         .get_spi_base   = bd82x6x_pch_get_spi_base,
257         .set_spi_protect = bd82x6x_set_spi_protect,
258         .get_gpio_base  = bd82x6x_get_gpio_base,
259         .ioctl          = bd82x6x_ioctl,
260 };
261
262 static const struct udevice_id bd82x6x_ids[] = {
263         { .compatible = "intel,bd82x6x" },
264         { }
265 };
266
267 U_BOOT_DRIVER(bd82x6x_drv) = {
268         .name           = "bd82x6x",
269         .id             = UCLASS_PCH,
270         .of_match       = bd82x6x_ids,
271 #ifndef CONFIG_HAVE_FSP
272         .probe          = bd82x6x_probe,
273 #endif
274         .ops            = &bd82x6x_pch_ops,
275 };