8c71e43a2d895c2224e6b6847c64b29f09a3f836
[oweals/u-boot.git] / arch / x86 / cpu / broadwell / pch.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016 Google, Inc
4  */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <log.h>
9 #include <pch.h>
10 #include <asm/cpu.h>
11 #include <asm/gpio.h>
12 #include <asm/i8259.h>
13 #include <asm/intel_regs.h>
14 #include <asm/io.h>
15 #include <asm/ioapic.h>
16 #include <asm/lpc_common.h>
17 #include <asm/pch_common.h>
18 #include <asm/arch/cpu.h>
19 #include <asm/arch/gpio.h>
20 #include <asm/arch/iomap.h>
21 #include <asm/arch/pch.h>
22 #include <asm/arch/pm.h>
23 #include <asm/arch/rcb.h>
24 #include <asm/arch/serialio.h>
25 #include <asm/arch/spi.h>
26 #include <dm/uclass-internal.h>
27
28 #define BIOS_CTRL       0xdc
29
30 bool cpu_is_ult(void)
31 {
32         u32 fm = cpu_get_family_model();
33
34         return fm == BROADWELL_FAMILY_ULT || fm == HASWELL_FAMILY_ULT;
35 }
36
37 static int broadwell_pch_early_init(struct udevice *dev)
38 {
39         struct gpio_desc desc;
40         struct udevice *bus;
41         pci_dev_t bdf;
42         int ret;
43
44         dm_pci_write_config32(dev, PCH_RCBA, RCB_BASE_ADDRESS | 1);
45
46         dm_pci_write_config32(dev, PMBASE, ACPI_BASE_ADDRESS | 1);
47         dm_pci_write_config8(dev, ACPI_CNTL, ACPI_EN);
48         dm_pci_write_config32(dev, GPIO_BASE, GPIO_BASE_ADDRESS | 1);
49         dm_pci_write_config8(dev, GPIO_CNTL, GPIO_EN);
50
51         /* Enable IOAPIC */
52         writew(0x1000, RCB_REG(OIC));
53         /* Read back for posted write */
54         readw(RCB_REG(OIC));
55
56         /* Set HPET address and enable it */
57         clrsetbits_le32(RCB_REG(HPTC), 3, 1 << 7);
58         /* Read back for posted write */
59         readl(RCB_REG(HPTC));
60         /* Enable HPET to start counter */
61         setbits_le32(HPET_BASE_ADDRESS + 0x10, 1 << 0);
62
63         setbits_le32(RCB_REG(GCS), 1 << 5);
64
65         /*
66          * Enable PP3300_AUTOBAHN_EN after initial GPIO setup
67          * to prevent possible brownout. This will cause the GPIOs to be set
68          * up if it has not been done already.
69          */
70         ret = gpio_request_by_name(dev, "power-enable-gpio", 0, &desc,
71                                    GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
72         if (ret)
73                 return ret;
74
75         /* 8.14 Additional PCI Express Programming Steps, step #1 */
76         bdf = PCI_BDF(0, 0x1c, 0);
77         bus = pci_get_controller(dev);
78         pci_bus_clrset_config32(bus, bdf, 0xf4, 0x60, 0);
79         pci_bus_clrset_config32(bus, bdf, 0xf4, 0x80, 0x80);
80         pci_bus_clrset_config32(bus, bdf, 0xe2, 0x30, 0x30);
81
82         return 0;
83 }
84
85 static void pch_misc_init(struct udevice *dev)
86 {
87         /* Setup SLP signal assertion, SLP_S4=4s, SLP_S3=50ms */
88         dm_pci_clrset_config8(dev, GEN_PMCON_3, 3 << 4 | 1 << 10,
89                               1 << 3 | 1 << 11 | 1 << 12);
90         /* Prepare sleep mode */
91         clrsetio_32(ACPI_BASE_ADDRESS + PM1_CNT, SLP_TYP, SCI_EN);
92
93         /* Setup NMI on errors, disable SERR */
94         clrsetio_8(0x61, 0xf0, 1 << 2);
95         /* Disable NMI sources */
96         setio_8(0x70, 1 << 7);
97         /* Indicate DRAM init done for MRC */
98         dm_pci_clrset_config8(dev, GEN_PMCON_2, 0, 1 << 7);
99
100         /* Clear status bits to prevent unexpected wake */
101         setbits_le32(RCB_REG(0x3310), 0x0000002f);
102         clrsetbits_le32(RCB_REG(0x3f02), 0x0000000f, 0);
103         /* Enable PCIe Relaxed Order */
104         setbits_le32(RCB_REG(0x2314), 1 << 31 | 1 << 7);
105         setbits_le32(RCB_REG(0x1114), 1 << 15 | 1 << 14);
106         /* Setup SERIRQ, enable continuous mode */
107         dm_pci_clrset_config8(dev, SERIRQ_CNTL, 0, 1 << 7 | 1 << 6);
108 };
109
110 static void pch_enable_ioapic(void)
111 {
112         u32 reg32;
113
114         /* Make sure this is a unique ID within system */
115         io_apic_set_id(0x04);
116
117         /* affirm full set of redirection table entries ("write once") */
118         reg32 = io_apic_read(0x01);
119
120         /* PCH-LP has 39 redirection entries */
121         reg32 &= ~0x00ff0000;
122         reg32 |= 0x00270000;
123
124         io_apic_write(0x01, reg32);
125
126         /*
127          * Select Boot Configuration register (0x03) and
128          * use Processor System Bus (0x01) to deliver interrupts.
129          */
130         io_apic_write(0x03, 0x01);
131 }
132
133 /* Enable all requested GPE */
134 void enable_all_gpe(u32 set1, u32 set2, u32 set3, u32 set4)
135 {
136         outl(set1, ACPI_BASE_ADDRESS + GPE0_EN(GPE_31_0));
137         outl(set2, ACPI_BASE_ADDRESS + GPE0_EN(GPE_63_32));
138         outl(set3, ACPI_BASE_ADDRESS + GPE0_EN(GPE_94_64));
139         outl(set4, ACPI_BASE_ADDRESS + GPE0_EN(GPE_STD));
140 }
141
142 /*
143  * Enable GPIO SMI events - it would be good to put this in the GPIO driver
144  * but it would need a new driver operation.
145  */
146 int enable_alt_smi(struct udevice *pch, u32 mask)
147 {
148         struct pch_lp_gpio_regs *regs;
149         u32 gpiobase;
150         int ret;
151
152         ret = pch_get_gpio_base(pch, &gpiobase);
153         if (ret) {
154                 debug("%s: invalid GPIOBASE address (%08x)\n", __func__,
155                       gpiobase);
156                 return -EINVAL;
157         }
158
159         regs = (struct pch_lp_gpio_regs *)gpiobase;
160         setio_32(regs->alt_gpi_smi_en, mask);
161
162         return 0;
163 }
164
165 static int pch_power_options(struct udevice *dev)
166 {
167         int pwr_on_after_power_fail = MAINBOARD_POWER_OFF;
168         const char *state;
169         u32 enable[4];
170         u16 reg16;
171         int ret;
172
173         dm_pci_read_config16(dev, GEN_PMCON_3, &reg16);
174         reg16 &= 0xfffe;
175         switch (pwr_on_after_power_fail) {
176         case MAINBOARD_POWER_OFF:
177                 reg16 |= 1;
178                 state = "off";
179                 break;
180         case MAINBOARD_POWER_ON:
181                 reg16 &= ~1;
182                 state = "on";
183                 break;
184         case MAINBOARD_POWER_KEEP:
185                 reg16 &= ~1;
186                 state = "state keep";
187                 break;
188         default:
189                 state = "undefined";
190         }
191         dm_pci_write_config16(dev, GEN_PMCON_3, reg16);
192         debug("Set power %s after power failure.\n", state);
193
194         /* GPE setup based on device tree configuration */
195         ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
196                                    "intel,gpe0-en", enable, ARRAY_SIZE(enable));
197         if (ret)
198                 return -EINVAL;
199         enable_all_gpe(enable[0], enable[1], enable[2], enable[3]);
200
201         /* SMI setup based on device tree configuration */
202         enable_alt_smi(dev, fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
203                                            "intel,alt-gp-smi-enable", 0));
204
205         return 0;
206 }
207
208 /* Magic register settings for power management */
209 static void pch_pm_init_magic(struct udevice *dev)
210 {
211         dm_pci_write_config8(dev, 0xa9, 0x46);
212         clrbits_le32(RCB_REG(0x232c), 1),
213         setbits_le32(RCB_REG(0x1100), 0x0000c13f);
214         clrsetbits_le32(RCB_REG(0x2320), 0x60, 0x10);
215         writel(0x00012fff, RCB_REG(0x3314));
216         clrsetbits_le32(RCB_REG(0x3318), 0x000f0330, 0x0dcf0400);
217         writel(0x04000000, RCB_REG(0x3324));
218         writel(0x00041400, RCB_REG(0x3368));
219         writel(0x3f8ddbff, RCB_REG(0x3388));
220         writel(0x00007001, RCB_REG(0x33ac));
221         writel(0x00181900, RCB_REG(0x33b0));
222         writel(0x00060A00, RCB_REG(0x33c0));
223         writel(0x06200840, RCB_REG(0x33d0));
224         writel(0x01010101, RCB_REG(0x3a28));
225         writel(0x040c0404, RCB_REG(0x3a2c));
226         writel(0x9000000a, RCB_REG(0x3a9c));
227         writel(0x03808033, RCB_REG(0x2b1c));
228         writel(0x80000009, RCB_REG(0x2b34));
229         writel(0x022ddfff, RCB_REG(0x3348));
230         writel(0x00000001, RCB_REG(0x334c));
231         writel(0x0001c000, RCB_REG(0x3358));
232         writel(0x3f8ddbff, RCB_REG(0x3380));
233         writel(0x0001c7e1, RCB_REG(0x3384));
234         writel(0x0001c7e1, RCB_REG(0x338c));
235         writel(0x0001c000, RCB_REG(0x3398));
236         writel(0x00181900, RCB_REG(0x33a8));
237         writel(0x00080000, RCB_REG(0x33dc));
238         writel(0x00000001, RCB_REG(0x33e0));
239         writel(0x0000040c, RCB_REG(0x3a20));
240         writel(0x01010101, RCB_REG(0x3a24));
241         writel(0x01010101, RCB_REG(0x3a30));
242         dm_pci_clrset_config32(dev, 0xac, 0x00200000, 0);
243         setbits_le32(RCB_REG(0x0410), 0x00000003);
244         setbits_le32(RCB_REG(0x2618), 0x08000000);
245         setbits_le32(RCB_REG(0x2300), 0x00000002);
246         setbits_le32(RCB_REG(0x2600), 0x00000008);
247         writel(0x00007001, RCB_REG(0x33b4));
248         writel(0x022ddfff, RCB_REG(0x3350));
249         writel(0x00000001, RCB_REG(0x3354));
250         /* Power Optimizer */
251         setbits_le32(RCB_REG(0x33d4), 0x08000000);
252         /*
253          * This stops the LCD from turning on:
254          * setbits_le32(RCB_REG(0x33c8), 0x08000080);
255          */
256         writel(0x0000883c, RCB_REG(0x2b10));
257         writel(0x1e0a4616, RCB_REG(0x2b14));
258         writel(0x40000005, RCB_REG(0x2b24));
259         writel(0x0005db01, RCB_REG(0x2b20));
260         writel(0x05145005, RCB_REG(0x3a80));
261         writel(0x00001005, RCB_REG(0x3a84));
262         setbits_le32(RCB_REG(0x33d4), 0x2fff2fb1);
263         setbits_le32(RCB_REG(0x33c8), 0x00008000);
264 };
265
266 static int pch_type(struct udevice *dev)
267 {
268         u16 type;
269
270         dm_pci_read_config16(dev, PCI_DEVICE_ID, &type);
271
272         return type;
273 }
274
275 /* Return 1 if PCH type is WildcatPoint */
276 static int pch_is_wpt(struct udevice *dev)
277 {
278         return ((pch_type(dev) & 0xfff0) == 0x9cc0) ? 1 : 0;
279 }
280
281 /* Return 1 if PCH type is WildcatPoint ULX */
282 static int pch_is_wpt_ulx(struct udevice *dev)
283 {
284         u16 lpcid = pch_type(dev);
285
286         switch (lpcid) {
287         case PCH_WPT_BDW_Y_SAMPLE:
288         case PCH_WPT_BDW_Y_PREMIUM:
289         case PCH_WPT_BDW_Y_BASE:
290                 return 1;
291         }
292
293         return 0;
294 }
295
296 static u32 pch_read_soft_strap(int id)
297 {
298         clrbits_le32(SPI_REG(SPIBAR_FDOC), 0x00007ffc);
299         setbits_le32(SPI_REG(SPIBAR_FDOC), 0x00004000 | id * 4);
300
301         return readl(SPI_REG(SPIBAR_FDOD));
302 }
303
304 static void pch_enable_mphy(struct udevice *dev)
305 {
306         u32 data_and = 0xffffffff;
307         u32 data_or = (1 << 14) | (1 << 13) | (1 << 12);
308
309         data_or |= (1 << 0);
310         if (pch_is_wpt(dev)) {
311                 data_and &= ~((1 << 7) | (1 << 6) | (1 << 3));
312                 data_or |= (1 << 5) | (1 << 4);
313
314                 if (pch_is_wpt_ulx(dev)) {
315                         /* Check if SATA and USB3 MPHY are enabled */
316                         u32 strap19 = pch_read_soft_strap(19);
317                         strap19 &= ((1 << 31) | (1 << 30));
318                         strap19 >>= 30;
319                         if (strap19 == 3) {
320                                 data_or |= (1 << 3);
321                                 debug("Enable ULX MPHY PG control in single domain\n");
322                         } else if (strap19 == 0) {
323                                 debug("Enable ULX MPHY PG control in split domains\n");
324                         } else {
325                                 debug("Invalid PCH Soft Strap 19 configuration\n");
326                         }
327                 } else {
328                         data_or |= (1 << 3);
329                 }
330         }
331
332         pch_iobp_update(0xCF000000, data_and, data_or);
333 }
334
335 static void pch_init_deep_sx(bool deep_sx_enable_ac, bool deep_sx_enable_dc)
336 {
337         if (deep_sx_enable_ac) {
338                 setbits_le32(RCB_REG(DEEP_S3_POL), DEEP_S3_EN_AC);
339                 setbits_le32(RCB_REG(DEEP_S5_POL), DEEP_S5_EN_AC);
340         }
341
342         if (deep_sx_enable_dc) {
343                 setbits_le32(RCB_REG(DEEP_S3_POL), DEEP_S3_EN_DC);
344                 setbits_le32(RCB_REG(DEEP_S5_POL), DEEP_S5_EN_DC);
345         }
346
347         if (deep_sx_enable_ac || deep_sx_enable_dc) {
348                 setbits_le32(RCB_REG(DEEP_SX_CONFIG),
349                              DEEP_SX_WAKE_PIN_EN | DEEP_SX_GP27_PIN_EN);
350         }
351 }
352
353 /* Power Management init */
354 static void pch_pm_init(struct udevice *dev)
355 {
356         debug("PCH PM init\n");
357
358         pch_init_deep_sx(false, false);
359         pch_enable_mphy(dev);
360         pch_pm_init_magic(dev);
361
362         if (pch_is_wpt(dev)) {
363                 setbits_le32(RCB_REG(0x33e0), 1 << 4 | 1 << 1);
364                 setbits_le32(RCB_REG(0x2b1c), 1 << 22 | 1 << 14 | 1 << 13);
365                 writel(0x16bf0002, RCB_REG(0x33e4));
366                 setbits_le32(RCB_REG(0x33e4), 0x1);
367         }
368
369         pch_iobp_update(0xCA000000, ~0UL, 0x00000009);
370
371         /* Set RCBA 0x2b1c[29]=1 if DSP disabled */
372         if (readl(RCB_REG(FD)) & PCH_DISABLE_ADSPD)
373                 setbits_le32(RCB_REG(0x2b1c), 1 << 29);
374 }
375
376 static void pch_cg_init(struct udevice *dev)
377 {
378         struct udevice *bus = pci_get_controller(dev);
379         u32 reg32;
380         u16 reg16;
381         ulong val;
382
383         /* DMI */
384         setbits_le32(RCB_REG(0x2234), 0xf);
385
386         dm_pci_read_config16(dev, GEN_PMCON_1, &reg16);
387         reg16 &= ~(1 << 10); /* Disable BIOS_PCI_EXP_EN for native PME */
388         if (pch_is_wpt(dev))
389                 reg16 &= ~(1 << 11);
390         else
391                 reg16 |= 1 << 11;
392         reg16 |= 1 << 5 | 1 << 6 | 1 << 7 | 1 << 12;
393         reg16 |= 1 << 2; /* PCI CLKRUN# Enable */
394         dm_pci_write_config16(dev, GEN_PMCON_1, reg16);
395
396         /*
397          * RCBA + 0x2614[27:25,14:13,10,8] = 101,11,1,1
398          * RCBA + 0x2614[23:16] = 0x20
399          * RCBA + 0x2614[30:28] = 0x0
400          * RCBA + 0x2614[26] = 1 (IF 0:2.0@0x08 >= 0x0b)
401          */
402         clrsetbits_le32(RCB_REG(0x2614), 0x64ff0000, 0x0a206500);
403
404         /* Check for 0:2.0@0x08 >= 0x0b */
405         pci_bus_read_config(bus, PCI_BDF(0, 0x2, 0), 0x8, &val, PCI_SIZE_8);
406         if (pch_is_wpt(dev) || val >= 0x0b)
407                 setbits_le32(RCB_REG(0x2614), 1 << 26);
408
409         setbits_le32(RCB_REG(0x900), 0x0000031f);
410
411         reg32 = readl(RCB_REG(CG));
412         if (readl(RCB_REG(0x3454)) & (1 << 4))
413                 reg32 &= ~(1 << 29); /* LPC Dynamic */
414         else
415                 reg32 |= (1 << 29); /* LPC Dynamic */
416         reg32 |= 1 << 31; /* LP LPC */
417         reg32 |= 1 << 30; /* LP BLA */
418         if (readl(RCB_REG(0x3454)) & (1 << 4))
419                 reg32 &= ~(1 << 29);
420         else
421                 reg32 |= 1 << 29;
422         reg32 |= 1 << 28; /* GPIO Dynamic */
423         reg32 |= 1 << 27; /* HPET Dynamic */
424         reg32 |= 1 << 26; /* Generic Platform Event Clock */
425         if (readl(RCB_REG(BUC)) & PCH_DISABLE_GBE)
426                 reg32 |= 1 << 23; /* GbE Static */
427         if (readl(RCB_REG(FD)) & PCH_DISABLE_HD_AUDIO)
428                 reg32 |= 1 << 21; /* HDA Static */
429         reg32 |= 1 << 22; /* HDA Dynamic */
430         writel(reg32, RCB_REG(CG));
431
432         /* PCH-LP LPC */
433         if (pch_is_wpt(dev))
434                 clrsetbits_le32(RCB_REG(0x3434), 0x1f, 0x17);
435         else
436                 setbits_le32(RCB_REG(0x3434), 0x7);
437
438         /* SPI */
439         setbits_le32(RCB_REG(0x38c0), 0x3c07);
440
441         pch_iobp_update(0xCE00C000, ~1UL, 0x00000000);
442 }
443
444 static void systemagent_init(void)
445 {
446         /* Enable Power Aware Interrupt Routing */
447         clrsetbits_8(MCHBAR_REG(MCH_PAIR), 0x7, 0x4); /* Fixed Priority */
448
449         /*
450          * Set bits 0+1 of BIOS_RESET_CPL to indicate to the CPU
451          * that BIOS has initialized memory and power management
452          */
453         setbits_8(MCHBAR_REG(BIOS_RESET_CPL), 3);
454         debug("Set BIOS_RESET_CPL\n");
455
456         /* Configure turbo power limits 1ms after reset complete bit */
457         mdelay(1);
458
459         cpu_set_power_limits(28);
460 }
461
462 /* Enable LTR Auto Mode for D21:F1-F6 */
463 static void serialio_d21_ltr(u32 bar0)
464 {
465         /* 1. Program BAR0 + 808h[2] = 0b */
466         clrbits_le32(bar0 + SIO_REG_PPR_GEN, SIO_REG_PPR_GEN_LTR_MODE_MASK);
467
468         /* 2. Program BAR0 + 804h[1:0] = 00b */
469         clrbits_le32(bar0 + SIO_REG_PPR_RST, SIO_REG_PPR_RST_ASSERT);
470
471         /* 3. Program BAR0 + 804h[1:0] = 11b */
472         setbits_le32(bar0 + SIO_REG_PPR_RST, SIO_REG_PPR_RST_ASSERT);
473
474         /* 4. Program BAR0 + 814h[31:0] = 00000000h */
475         writel(0, bar0 + SIO_REG_AUTO_LTR);
476 }
477
478 /* Select I2C voltage of 1.8V or 3.3V */
479 static void serialio_i2c_voltage_sel(u32 bar0, uint voltage)
480 {
481         clrsetbits_le32(bar0 + SIO_REG_PPR_GEN, SIO_REG_PPR_GEN_VOLTAGE_MASK,
482                         SIO_REG_PPR_GEN_VOLTAGE(voltage));
483 }
484
485 /* Put Serial IO D21:F0-F6 device into desired mode */
486 static void serialio_d21_mode(int sio_index, int int_pin, bool acpi_mode)
487 {
488         u32 portctrl = SIO_IOBP_PORTCTRL_PM_CAP_PRSNT;
489
490         /* Snoop select 1 */
491         portctrl |= SIO_IOBP_PORTCTRL_SNOOP_SELECT(1);
492
493         /* Set interrupt pin */
494         portctrl |= SIO_IOBP_PORTCTRL_INT_PIN(int_pin);
495
496         if (acpi_mode) {
497                 /* Enable ACPI interrupt mode */
498                 portctrl |= SIO_IOBP_PORTCTRL_ACPI_IRQ_EN;
499         }
500
501         pch_iobp_update(SIO_IOBP_PORTCTRLX(sio_index), 0, portctrl);
502 }
503
504 /* Init sequence to be run once, done as part of D21:F0 (SDMA) init */
505 static void serialio_init_once(bool acpi_mode)
506 {
507         if (acpi_mode) {
508                 /* Enable ACPI IRQ for IRQ13, IRQ7, IRQ6, IRQ5 in RCBA */
509                 setbits_le32(RCB_REG(ACPIIRQEN),
510                              1 << 13 | 1 << 7 | 1 << 6 | 1 << 5);
511         }
512
513         /* Program IOBP CB000154h[12,9:8,4:0] = 1001100011111b */
514         pch_iobp_update(SIO_IOBP_GPIODF, ~0x0000131f, 0x0000131f);
515
516         /* Program IOBP CB000180h[5:0] = 111111b (undefined register) */
517         pch_iobp_update(0xcb000180, ~0x0000003f, 0x0000003f);
518 }
519
520 /**
521  * pch_serialio_init() - set up serial I/O devices
522  *
523  * @return 0 if OK, -ve on error
524  */
525 static int pch_serialio_init(void)
526 {
527         struct udevice *dev, *hda;
528         bool acpi_mode = true;
529         u32 bar0, bar1;
530         int ret;
531
532         ret = uclass_find_first_device(UCLASS_I2C, &dev);
533         if (ret)
534                 return ret;
535         bar0 = dm_pci_read_bar32(dev, 0);
536         if (!bar0)
537                 return -EINVAL;
538         bar1 = dm_pci_read_bar32(dev, 1);
539         if (!bar1)
540                 return -EINVAL;
541
542         serialio_init_once(acpi_mode);
543         serialio_d21_mode(SIO_ID_SDMA, SIO_PIN_INTB, acpi_mode);
544
545         serialio_d21_ltr(bar0);
546         serialio_i2c_voltage_sel(bar0, 1); /* Select 1.8V always */
547         serialio_d21_mode(SIO_ID_I2C0, SIO_PIN_INTC, acpi_mode);
548         setbits_le32(bar1 + PCH_PCS, PCH_PCS_PS_D3HOT);
549
550         clrbits_le32(bar1 + PCH_PCS, PCH_PCS_PS_D3HOT);
551
552         setbits_le32(bar0 + SIO_REG_PPR_CLOCK, SIO_REG_PPR_CLOCK_EN);
553
554         /* Manually find the High-definition audio, to turn it off */
555         ret = dm_pci_bus_find_bdf(PCI_BDF(0, 0x1b, 0), &hda);
556         if (ret)
557                 return -ENOENT;
558         dm_pci_clrset_config8(hda, 0x43, 0, 0x6f);
559
560         /* Route I/O buffers to ADSP function */
561         dm_pci_clrset_config8(hda, 0x42, 0, 1 << 7 | 1 << 6);
562         log_debug("HDA disabled, I/O buffers routed to ADSP\n");
563
564         return 0;
565 }
566
567 static int broadwell_pch_init(struct udevice *dev)
568 {
569         int ret;
570
571         /* Enable upper 128 bytes of CMOS */
572         setbits_le32(RCB_REG(RC), 1 << 2);
573
574         /*
575          * TODO: TCO timer halt - this hangs
576          * setio_16(ACPI_BASE_ADDRESS + TCO1_CNT, TCO_TMR_HLT);
577          */
578
579         /* Disable unused device (always) */
580         setbits_le32(RCB_REG(FD), PCH_DISABLE_ALWAYS);
581
582         pch_misc_init(dev);
583
584         /* Interrupt configuration */
585         pch_enable_ioapic();
586
587         /* Initialize power management */
588         ret = pch_power_options(dev);
589         if (ret)
590                 return ret;
591         pch_pm_init(dev);
592         pch_cg_init(dev);
593         ret = pch_serialio_init();
594         if (ret)
595                 return ret;
596         systemagent_init();
597
598         return 0;
599 }
600
601 static int broadwell_pch_probe(struct udevice *dev)
602 {
603         if (CONFIG_IS_ENABLED(X86_32BIT_INIT)) {
604                 if (!(gd->flags & GD_FLG_RELOC))
605                         return broadwell_pch_early_init(dev);
606                 else
607                         return broadwell_pch_init(dev);
608         } else if (IS_ENABLED(CONFIG_SPL) && !IS_ENABLED(CONFIG_SPL_BUILD)) {
609                 return broadwell_pch_init(dev);
610         } else {
611                 return 0;
612         }
613 }
614
615 static int broadwell_pch_get_spi_base(struct udevice *dev, ulong *sbasep)
616 {
617         u32 rcba;
618
619         dm_pci_read_config32(dev, PCH_RCBA, &rcba);
620         /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable */
621         rcba = rcba & 0xffffc000;
622         *sbasep = rcba + 0x3800;
623
624         return 0;
625 }
626
627 static int broadwell_set_spi_protect(struct udevice *dev, bool protect)
628 {
629         return lpc_set_spi_protect(dev, BIOS_CTRL, protect);
630 }
631
632 static int broadwell_get_gpio_base(struct udevice *dev, u32 *gbasep)
633 {
634         dm_pci_read_config32(dev, GPIO_BASE, gbasep);
635         *gbasep &= PCI_BASE_ADDRESS_IO_MASK;
636
637         return 0;
638 }
639
640 static int broadwell_ioctl(struct udevice *dev, enum pch_req_t req, void *data,
641                            int size)
642 {
643         switch (req) {
644         case PCH_REQ_PMBASE_INFO: {
645                 struct pch_pmbase_info *pm = data;
646                 int ret;
647
648                 /* Find the base address of the powermanagement registers */
649                 ret = dm_pci_read_config16(dev, 0x40, &pm->base);
650                 if (ret)
651                         return ret;
652                 pm->base &= 0xfffe;
653                 pm->gpio0_en_ofs = GPE0_EN(0);
654                 pm->pm1_sts_ofs = PM1_STS;
655                 pm->pm1_cnt_ofs = PM1_CNT;
656
657                 return 0;
658         }
659         default:
660                 return -ENOSYS;
661         }
662 }
663
664 static const struct pch_ops broadwell_pch_ops = {
665         .get_spi_base   = broadwell_pch_get_spi_base,
666         .set_spi_protect = broadwell_set_spi_protect,
667         .get_gpio_base  = broadwell_get_gpio_base,
668         .ioctl          = broadwell_ioctl,
669 };
670
671 static const struct udevice_id broadwell_pch_ids[] = {
672         { .compatible = "intel,broadwell-pch" },
673         { }
674 };
675
676 U_BOOT_DRIVER(broadwell_pch) = {
677         .name           = "broadwell_pch",
678         .id             = UCLASS_PCH,
679         .of_match       = broadwell_pch_ids,
680         .probe          = broadwell_pch_probe,
681         .ops            = &broadwell_pch_ops,
682 };