common: Drop init.h from common header
[pandora-u-boot.git] / arch / x86 / cpu / apollolake / fsp_s.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2019 Google LLC
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6
7 #include <common.h>
8 #include <binman.h>
9 #include <bootstage.h>
10 #include <dm.h>
11 #include <init.h>
12 #include <irq.h>
13 #include <malloc.h>
14 #include <acpi/acpi_s3.h>
15 #include <asm/intel_pinctrl.h>
16 #include <asm/io.h>
17 #include <asm/intel_regs.h>
18 #include <asm/msr.h>
19 #include <asm/msr-index.h>
20 #include <asm/pci.h>
21 #include <asm/arch/cpu.h>
22 #include <asm/arch/systemagent.h>
23 #include <asm/arch/fsp/fsp_configs.h>
24 #include <asm/arch/fsp/fsp_s_upd.h>
25
26 #define PCH_P2SB_E0             0xe0
27 #define HIDE_BIT                BIT(0)
28
29 #define INTEL_GSPI_MAX          3
30 #define MAX_USB2_PORTS          8
31
32 enum {
33         CHIPSET_LOCKDOWN_FSP = 0, /* FSP handles locking per UPDs */
34         CHIPSET_LOCKDOWN_COREBOOT, /* coreboot handles locking */
35 };
36
37 /* Serial IRQ control. SERIRQ_QUIET is the default (0) */
38 enum serirq_mode {
39         SERIRQ_QUIET,
40         SERIRQ_CONTINUOUS,
41         SERIRQ_OFF,
42 };
43
44 struct gspi_cfg {
45         /* Bus speed in MHz */
46         u32 speed_mhz;
47         /* Bus should be enabled prior to ramstage with temporary base */
48         u8 early_init;
49 };
50
51 /*
52  * This structure will hold data required by common blocks.
53  * These are soc specific configurations which will be filled by soc.
54  * We'll fill this structure once during init and use the data in common block.
55  */
56 struct soc_intel_common_config {
57         int chipset_lockdown;
58         struct gspi_cfg gspi[INTEL_GSPI_MAX];
59 };
60
61 enum pnp_settings {
62         PNP_PERF,
63         PNP_POWER,
64         PNP_PERF_POWER,
65 };
66
67 struct usb2_eye_per_port {
68         u8 per_port_tx_pe_half;
69         u8 per_port_pe_txi_set;
70         u8 per_port_txi_set;
71         u8 hs_skew_sel;
72         u8 usb_tx_emphasis_en;
73         u8 per_port_rxi_set;
74         u8 hs_npre_drv_sel;
75         u8 override_en;
76 };
77
78 struct apl_config {
79         /* Common structure containing soc config data required by common code*/
80         struct soc_intel_common_config common_soc_config;
81
82         /*
83          * Mapping from PCIe root port to CLKREQ input on the SOC. The SOC has
84          * four CLKREQ inputs, but six root ports. Root ports without an
85          * associated CLKREQ signal must be marked with "CLKREQ_DISABLED"
86          */
87         u8 pcie_rp_clkreq_pin[MAX_PCIE_PORTS];
88
89         /* Enable/disable hot-plug for root ports (0 = disable, 1 = enable) */
90         u8 pcie_rp_hotplug_enable[MAX_PCIE_PORTS];
91
92         /* De-emphasis enable configuration for each PCIe root port */
93         u8 pcie_rp_deemphasis_enable[MAX_PCIE_PORTS];
94
95         /*
96          * [14:8] DDR mode Number of dealy elements.Each = 125pSec.
97          * [6:0] SDR mode Number of dealy elements.Each = 125pSec.
98          */
99         u32 emmc_tx_cmd_cntl;
100
101         /*
102          * [14:8] HS400 mode Number of dealy elements.Each = 125pSec.
103          * [6:0] SDR104/HS200 mode Number of dealy elements.Each = 125pSec.
104          */
105         u32 emmc_tx_data_cntl1;
106
107         /*
108          * [30:24] SDR50 mode Number of dealy elements.Each = 125pSec.
109          * [22:16] DDR50 mode Number of dealy elements.Each = 125pSec.
110          * [14:8] SDR25/HS50 mode Number of dealy elements.Each = 125pSec.
111          * [6:0] SDR12/Compatibility mode Number of dealy elements.
112          *       Each = 125pSec.
113          */
114         u32 emmc_tx_data_cntl2;
115
116         /*
117          * [30:24] SDR50 mode Number of dealy elements.Each = 125pSec.
118          * [22:16] DDR50 mode Number of dealy elements.Each = 125pSec.
119          * [14:8] SDR25/HS50 mode Number of dealy elements.Each = 125pSec.
120          * [6:0] SDR12/Compatibility mode Number of dealy elements.
121          *       Each = 125pSec.
122          */
123         u32 emmc_rx_cmd_data_cntl1;
124
125         /*
126          * [14:8] HS400 mode 1 Number of dealy elements.Each = 125pSec.
127          * [6:0] HS400 mode 2 Number of dealy elements.Each = 125pSec.
128          */
129         u32 emmc_rx_strobe_cntl;
130
131         /*
132          * [13:8] Auto Tuning mode Number of dealy elements.Each = 125pSec.
133          * [6:0] SDR104/HS200 Number of dealy elements.Each = 125pSec.
134          */
135         u32 emmc_rx_cmd_data_cntl2;
136
137         /* Select the eMMC max speed allowed */
138         u32 emmc_host_max_speed;
139
140         /* Specifies on which IRQ the SCI will internally appear */
141         u32 sci_irq;
142
143         /* Configure serial IRQ (SERIRQ) line */
144         enum serirq_mode serirq_mode;
145
146         /* Configure LPSS S0ix Enable */
147         bool lpss_s0ix_enable;
148
149         /* Enable DPTF support */
150         bool dptf_enable;
151
152         /* TCC activation offset value in degrees Celsius */
153         int tcc_offset;
154
155         /*
156          * Configure Audio clk gate and power gate
157          * IOSF-SB port ID 92 offset 0x530 [5] and [3]
158          */
159         bool hdaudio_clk_gate_enable;
160         bool hdaudio_pwr_gate_enable;
161         bool hdaudio_bios_config_lockdown;
162
163         /* SLP S3 minimum assertion width */
164         int slp_s3_assertion_width_usecs;
165
166         /* GPIO pin for PERST_0 */
167         u32 prt0_gpio;
168
169         /* USB2 eye diagram settings per port */
170         struct usb2_eye_per_port usb2eye[MAX_USB2_PORTS];
171
172         /* GPIO SD card detect pin */
173         unsigned int sdcard_cd_gpio;
174
175         /*
176          * PRMRR size setting with three options
177          *  0x02000000 - 32MiB
178          *  0x04000000 - 64MiB
179          *  0x08000000 - 128MiB
180          */
181         u32 PrmrrSize;
182
183         /*
184          * Enable SGX feature.
185          * Enabling SGX feature is 2 step process,
186          * (1) set sgx_enable = 1
187          * (2) set PrmrrSize to supported size
188          */
189         bool sgx_enable;
190
191         /*
192          * Select PNP Settings.
193          * (0) Performance,
194          * (1) Power
195          * (2) Power & Performance
196          */
197         enum pnp_settings pnp_settings;
198
199         /*
200          * PMIC PCH_PWROK delay configuration - IPC Configuration
201          * Upd for changing PCH_PWROK delay configuration : I2C_Slave_Address
202          * (31:24) + Register_Offset (23:16) + OR Value (15:8) + AND Value (7:0)
203          */
204         u32 pmic_pmc_ipc_ctrl;
205
206         /*
207          * Options to disable XHCI Link Compliance Mode. Default is FALSE to not
208          * disable Compliance Mode. Set TRUE to disable Compliance Mode.
209          * 0:FALSE(Default), 1:True.
210          */
211         bool disable_compliance_mode;
212
213         /*
214          * Options to change USB3 ModPhy setting for the Integrated Filter (IF)
215          * value. Default is 0 to not changing default IF value (0x12). Set
216          * value with the range from 0x01 to 0xff to change IF value.
217          */
218         u32 mod_phy_if_value;
219
220         /*
221          * Options to bump USB3 LDO voltage. Default is FALSE to not increasing
222          * LDO voltage. Set TRUE to increase LDO voltage with 40mV.
223          * 0:FALSE (default), 1:True.
224          */
225         bool mod_phy_voltage_bump;
226
227         /*
228          * Options to adjust PMIC Vdd2 voltage. Default is 0 to not adjusting
229          * the PMIC Vdd2 default voltage 1.20v. Upd for changing Vdd2 Voltage
230          * configuration: I2C_Slave_Address (31:23) + Register_Offset (23:16)
231          * + OR Value (15:8) + AND Value (7:0) through BUCK5_VID[3:2]:
232          * 00=1.10v, 01=1.15v, 10=1.24v, 11=1.20v (default).
233          */
234         u32 pmic_vdd2_voltage;
235
236         /* Option to enable VTD feature */
237         bool enable_vtd;
238 };
239
240 static int get_config(struct udevice *dev, struct apl_config *apl)
241 {
242         const u8 *ptr;
243         ofnode node;
244         u32 emmc[4];
245         int ret;
246
247         memset(apl, '\0', sizeof(*apl));
248
249         node = dev_read_subnode(dev, "fsp-s");
250         if (!ofnode_valid(node))
251                 return log_msg_ret("fsp-s settings", -ENOENT);
252
253         ptr = ofnode_read_u8_array_ptr(node, "pcie-rp-clkreq-pin",
254                                        MAX_PCIE_PORTS);
255         if (!ptr)
256                 return log_msg_ret("pcie-rp-clkreq-pin", -EINVAL);
257         memcpy(apl->pcie_rp_clkreq_pin, ptr, MAX_PCIE_PORTS);
258
259         ret = ofnode_read_u32(node, "prt0-gpio", &apl->prt0_gpio);
260         if (ret)
261                 return log_msg_ret("prt0-gpio", ret);
262         ret = ofnode_read_u32(node, "sdcard-cd-gpio", &apl->sdcard_cd_gpio);
263         if (ret)
264                 return log_msg_ret("sdcard-cd-gpio", ret);
265
266         ret = ofnode_read_u32_array(node, "emmc", emmc, ARRAY_SIZE(emmc));
267         if (ret)
268                 return log_msg_ret("emmc", ret);
269         apl->emmc_tx_data_cntl1 = emmc[0];
270         apl->emmc_tx_data_cntl2 = emmc[1];
271         apl->emmc_rx_cmd_data_cntl1 = emmc[2];
272         apl->emmc_rx_cmd_data_cntl2 = emmc[3];
273
274         apl->dptf_enable = ofnode_read_bool(node, "dptf-enable");
275
276         apl->hdaudio_clk_gate_enable = ofnode_read_bool(node,
277                                                 "hdaudio-clk-gate-enable");
278         apl->hdaudio_pwr_gate_enable = ofnode_read_bool(node,
279                                                 "hdaudio-pwr-gate-enable");
280         apl->hdaudio_bios_config_lockdown = ofnode_read_bool(node,
281                                              "hdaudio-bios-config-lockdown");
282         apl->lpss_s0ix_enable = ofnode_read_bool(node, "lpss-s0ix-enable");
283
284         /* Santa */
285         apl->usb2eye[1].per_port_pe_txi_set = 7;
286         apl->usb2eye[1].per_port_txi_set = 2;
287
288         return 0;
289 }
290
291 static void apl_fsp_silicon_init_params_cb(struct apl_config *apl,
292                                            struct fsp_s_config *cfg)
293 {
294         u8 port;
295
296         for (port = 0; port < MAX_USB2_PORTS; port++) {
297                 if (apl->usb2eye[port].per_port_tx_pe_half)
298                         cfg->port_usb20_per_port_tx_pe_half[port] =
299                                 apl->usb2eye[port].per_port_tx_pe_half;
300
301                 if (apl->usb2eye[port].per_port_pe_txi_set)
302                         cfg->port_usb20_per_port_pe_txi_set[port] =
303                                 apl->usb2eye[port].per_port_pe_txi_set;
304
305                 if (apl->usb2eye[port].per_port_txi_set)
306                         cfg->port_usb20_per_port_txi_set[port] =
307                                 apl->usb2eye[port].per_port_txi_set;
308
309                 if (apl->usb2eye[port].hs_skew_sel)
310                         cfg->port_usb20_hs_skew_sel[port] =
311                                 apl->usb2eye[port].hs_skew_sel;
312
313                 if (apl->usb2eye[port].usb_tx_emphasis_en)
314                         cfg->port_usb20_i_usb_tx_emphasis_en[port] =
315                                 apl->usb2eye[port].usb_tx_emphasis_en;
316
317                 if (apl->usb2eye[port].per_port_rxi_set)
318                         cfg->port_usb20_per_port_rxi_set[port] =
319                                 apl->usb2eye[port].per_port_rxi_set;
320
321                 if (apl->usb2eye[port].hs_npre_drv_sel)
322                         cfg->port_usb20_hs_npre_drv_sel[port] =
323                                 apl->usb2eye[port].hs_npre_drv_sel;
324         }
325 }
326
327 int fsps_update_config(struct udevice *dev, ulong rom_offset,
328                        struct fsps_upd *upd)
329 {
330         struct fsp_s_config *cfg = &upd->config;
331         struct apl_config *apl;
332         struct binman_entry vbt;
333         void *buf;
334         int ret;
335
336         ret = binman_entry_find("intel-vbt", &vbt);
337         if (ret)
338                 return log_msg_ret("Cannot find VBT", ret);
339         vbt.image_pos += rom_offset;
340         buf = malloc(vbt.size);
341         if (!buf)
342                 return log_msg_ret("Alloc VBT", -ENOMEM);
343
344         /*
345          * Load VBT before devicetree-specific config. This only supports
346          * memory-mapped SPI at present.
347          */
348         bootstage_start(BOOTSTAGE_ID_ACCUM_MMAP_SPI, "mmap_spi");
349         memcpy(buf, (void *)vbt.image_pos, vbt.size);
350         bootstage_accum(BOOTSTAGE_ID_ACCUM_MMAP_SPI);
351         if (*(u32 *)buf != VBT_SIGNATURE)
352                 return log_msg_ret("VBT signature", -EINVAL);
353         cfg->graphics_config_ptr = (ulong)buf;
354
355         apl = malloc(sizeof(*apl));
356         if (!apl)
357                 return log_msg_ret("config", -ENOMEM);
358         get_config(dev, apl);
359
360         cfg->ish_enable = 0;
361         cfg->enable_sata = 0;
362         cfg->pcie_root_port_en[2] = 0;
363         cfg->pcie_rp_hot_plug[2] = 0;
364         cfg->pcie_root_port_en[3] = 0;
365         cfg->pcie_rp_hot_plug[3] = 0;
366         cfg->pcie_root_port_en[4] = 0;
367         cfg->pcie_rp_hot_plug[4] = 0;
368         cfg->pcie_root_port_en[5] = 0;
369         cfg->pcie_rp_hot_plug[5] = 0;
370         cfg->pcie_root_port_en[1] = 0;
371         cfg->pcie_rp_hot_plug[1] = 0;
372         cfg->usb_otg = 0;
373         cfg->i2c6_enable = 0;
374         cfg->i2c7_enable = 0;
375         cfg->hsuart3_enable = 0;
376         cfg->spi1_enable = 0;
377         cfg->spi2_enable = 0;
378         cfg->sdio_enabled = 0;
379
380         memcpy(cfg->pcie_rp_clk_req_number, apl->pcie_rp_clkreq_pin,
381                sizeof(cfg->pcie_rp_clk_req_number));
382
383         memcpy(cfg->pcie_rp_hot_plug, apl->pcie_rp_hotplug_enable,
384                sizeof(cfg->pcie_rp_hot_plug));
385
386         switch (apl->serirq_mode) {
387         case SERIRQ_QUIET:
388                 cfg->sirq_enable = 1;
389                 cfg->sirq_mode = 0;
390                 break;
391         case SERIRQ_CONTINUOUS:
392                 cfg->sirq_enable = 1;
393                 cfg->sirq_mode = 1;
394                 break;
395         case SERIRQ_OFF:
396         default:
397                 cfg->sirq_enable = 0;
398                 break;
399         }
400
401         if (apl->emmc_tx_cmd_cntl)
402                 cfg->emmc_tx_cmd_cntl = apl->emmc_tx_cmd_cntl;
403         if (apl->emmc_tx_data_cntl1)
404                 cfg->emmc_tx_data_cntl1 = apl->emmc_tx_data_cntl1;
405         if (apl->emmc_tx_data_cntl2)
406                 cfg->emmc_tx_data_cntl2 = apl->emmc_tx_data_cntl2;
407         if (apl->emmc_rx_cmd_data_cntl1)
408                 cfg->emmc_rx_cmd_data_cntl1 = apl->emmc_rx_cmd_data_cntl1;
409         if (apl->emmc_rx_strobe_cntl)
410                 cfg->emmc_rx_strobe_cntl = apl->emmc_rx_strobe_cntl;
411         if (apl->emmc_rx_cmd_data_cntl2)
412                 cfg->emmc_rx_cmd_data_cntl2 = apl->emmc_rx_cmd_data_cntl2;
413         if (apl->emmc_host_max_speed)
414                 cfg->e_mmc_host_max_speed = apl->emmc_host_max_speed;
415
416         cfg->lpss_s0ix_enable = apl->lpss_s0ix_enable;
417
418         cfg->skip_mp_init = true;
419
420         /* Disable setting of EISS bit in FSP */
421         cfg->spi_eiss = 0;
422
423         /* Disable FSP from locking access to the RTC NVRAM */
424         cfg->rtc_lock = 0;
425
426         /* Enable Audio clk gate and power gate */
427         cfg->hd_audio_clk_gate = apl->hdaudio_clk_gate_enable;
428         cfg->hd_audio_pwr_gate = apl->hdaudio_pwr_gate_enable;
429         /* Bios config lockdown Audio clk and power gate */
430         cfg->bios_cfg_lock_down = apl->hdaudio_bios_config_lockdown;
431         apl_fsp_silicon_init_params_cb(apl, cfg);
432
433         cfg->usb_otg = true;
434         cfg->vtd_enable = apl->enable_vtd;
435
436         return 0;
437 }
438
439 static void p2sb_set_hide_bit(pci_dev_t dev, int hide)
440 {
441         pci_x86_clrset_config(dev, PCH_P2SB_E0 + 1, HIDE_BIT,
442                               hide ? HIDE_BIT : 0, PCI_SIZE_8);
443 }
444
445 /* Configure package power limits */
446 static int set_power_limits(struct udevice *dev)
447 {
448         msr_t rapl_msr_reg, limit;
449         u32 power_unit;
450         u32 tdp, min_power, max_power;
451         u32 pl2_val;
452         u32 override_tdp[2];
453         int ret;
454
455         /* Get units */
456         rapl_msr_reg = msr_read(MSR_PKG_POWER_SKU_UNIT);
457         power_unit = 1 << (rapl_msr_reg.lo & 0xf);
458
459         /* Get power defaults for this SKU */
460         rapl_msr_reg = msr_read(MSR_PKG_POWER_SKU);
461         tdp = rapl_msr_reg.lo & PKG_POWER_LIMIT_MASK;
462         pl2_val = rapl_msr_reg.hi & PKG_POWER_LIMIT_MASK;
463         min_power = (rapl_msr_reg.lo >> 16) & PKG_POWER_LIMIT_MASK;
464         max_power = rapl_msr_reg.hi & PKG_POWER_LIMIT_MASK;
465
466         if (min_power > 0 && tdp < min_power)
467                 tdp = min_power;
468
469         if (max_power > 0 && tdp > max_power)
470                 tdp = max_power;
471
472         ret = dev_read_u32_array(dev, "tdp-pl-override-mw", override_tdp,
473                                  ARRAY_SIZE(override_tdp));
474         if (ret)
475                 return log_msg_ret("tdp-pl-override-mw", ret);
476
477         /* Set PL1 override value */
478         if (override_tdp[0])
479                 tdp = override_tdp[0] * power_unit / 1000;
480
481         /* Set PL2 override value */
482         if (override_tdp[1])
483                 pl2_val = override_tdp[1] * power_unit / 1000;
484
485         /* Set long term power limit to TDP */
486         limit.lo = tdp & PKG_POWER_LIMIT_MASK;
487         /* Set PL1 Pkg Power clamp bit */
488         limit.lo |= PKG_POWER_LIMIT_CLAMP;
489
490         limit.lo |= PKG_POWER_LIMIT_EN;
491         limit.lo |= (MB_POWER_LIMIT1_TIME_DEFAULT &
492                 PKG_POWER_LIMIT_TIME_MASK) << PKG_POWER_LIMIT_TIME_SHIFT;
493
494         /* Set short term power limit PL2 */
495         limit.hi = pl2_val & PKG_POWER_LIMIT_MASK;
496         limit.hi |= PKG_POWER_LIMIT_EN;
497
498         /* Program package power limits in RAPL MSR */
499         msr_write(MSR_PKG_POWER_LIMIT, limit);
500         log_info("RAPL PL1 %d.%dW\n", tdp / power_unit,
501                  100 * (tdp % power_unit) / power_unit);
502         log_info("RAPL PL2 %d.%dW\n", pl2_val / power_unit,
503                  100 * (pl2_val % power_unit) / power_unit);
504
505         /*
506          * Sett RAPL MMIO register for Power limits. RAPL driver is using MSR
507          * instead of MMIO, so disable LIMIT_EN bit for MMIO
508          */
509         writel(limit.lo & ~PKG_POWER_LIMIT_EN, MCHBAR_REG(MCHBAR_RAPL_PPL));
510         writel(limit.hi & ~PKG_POWER_LIMIT_EN, MCHBAR_REG(MCHBAR_RAPL_PPL + 4));
511
512         return 0;
513 }
514
515 int p2sb_unhide(void)
516 {
517         pci_dev_t dev = PCI_BDF(0, 0xd, 0);
518         ulong val;
519
520         p2sb_set_hide_bit(dev, 0);
521
522         pci_x86_read_config(dev, PCI_VENDOR_ID, &val, PCI_SIZE_16);
523
524         if (val != PCI_VENDOR_ID_INTEL)
525                 return log_msg_ret("p2sb unhide", -EIO);
526
527         return 0;
528 }
529
530 /* Overwrites the SCI IRQ if another IRQ number is given by device tree */
531 static void set_sci_irq(void)
532 {
533         /* Skip this for now */
534 }
535
536 int arch_fsps_preinit(void)
537 {
538         struct udevice *itss;
539         int ret;
540
541         ret = irq_first_device_type(X86_IRQT_ITSS, &itss);
542         if (ret)
543                 return log_msg_ret("no itss", ret);
544         /*
545          * Snapshot the current GPIO IRQ polarities. FSP is setting a default
546          * policy that doesn't honour boards' requirements
547          */
548         irq_snapshot_polarities(itss);
549
550         /*
551          * Clear the GPI interrupt status and enable registers. These
552          * registers do not get reset to default state when booting from S5.
553          */
554         ret = pinctrl_gpi_clear_int_cfg();
555         if (ret)
556                 return log_msg_ret("gpi_clear", ret);
557
558         return 0;
559 }
560
561 int arch_fsp_init_r(void)
562 {
563 #ifdef CONFIG_HAVE_ACPI_RESUME
564         bool s3wake = gd->arch.prev_sleep_state == ACPI_S3;
565 #else
566         bool s3wake = false;
567 #endif
568         struct udevice *dev, *itss;
569         int ret;
570
571         if (!ll_boot_init())
572                 return 0;
573         /*
574          * This must be called before any devices are probed. Put any probing
575          * into arch_fsps_preinit() above.
576          *
577          * We don't use CONFIG_APL_BOOT_FROM_FAST_SPI_FLASH here since it will
578          * force PCI to be probed.
579          */
580         ret = fsp_silicon_init(s3wake, false);
581         if (ret)
582                 return ret;
583
584         ret = irq_first_device_type(X86_IRQT_ITSS, &itss);
585         if (ret)
586                 return log_msg_ret("no itss", ret);
587         /* Restore GPIO IRQ polarities back to previous settings */
588         irq_restore_polarities(itss);
589
590         /* soc_init() */
591         ret = p2sb_unhide();
592         if (ret)
593                 return log_msg_ret("unhide p2sb", ret);
594
595         /* Set RAPL MSR for Package power limits*/
596         ret = uclass_first_device_err(UCLASS_NORTHBRIDGE, &dev);
597         if (ret)
598                 return log_msg_ret("Cannot get northbridge", ret);
599         set_power_limits(dev);
600
601         /*
602          * FSP-S routes SCI to IRQ 9. With the help of this function you can
603          * select another IRQ for SCI.
604          */
605         set_sci_irq();
606
607         return 0;
608 }