blob: 9ec2309d086c11c056796513133e5d6bfbff2110 [file] [log] [blame]
Simon Glasse5fb75c2019-12-08 17:40:07 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2019 Google LLC
Simon Glassabb4e422020-09-22 12:45:20 -06004 * Copyright (C) 2015 - 2017 Intel Corp.
5 * Copyright (C) 2017 - 2019 Siemens AG
6 * (Written by Alexandru Gagniuc <alexandrux.gagniuc@intel.com> for Intel Corp.)
7 * (Written by Andrey Petrov <andrey.petrov@intel.com> for Intel Corp.)
8 *
9 * Portions from coreboot soc/intel/apollolake/chip.c
Simon Glasse5fb75c2019-12-08 17:40:07 -070010 */
11
Simon Glassabb4e422020-09-22 12:45:20 -060012#define LOG_CATEGORY UCLASS_NORTHBRIDGE
13
Simon Glasse5fb75c2019-12-08 17:40:07 -070014#include <common.h>
15#include <dm.h>
16#include <dt-structs.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060017#include <log.h>
Simon Glasse5fb75c2019-12-08 17:40:07 -070018#include <spl.h>
Simon Glassabb4e422020-09-22 12:45:20 -060019#include <tables_csum.h>
20#include <acpi/acpi_table.h>
21#include <asm/acpi_nhlt.h>
Simon Glasse5fb75c2019-12-08 17:40:07 -070022#include <asm/intel_pinctrl.h>
23#include <asm/intel_regs.h>
Simon Glassabb4e422020-09-22 12:45:20 -060024#include <asm/io.h>
Simon Glasse5fb75c2019-12-08 17:40:07 -070025#include <asm/pci.h>
Simon Glassabb4e422020-09-22 12:45:20 -060026#include <asm/arch/acpi.h>
Simon Glasse5fb75c2019-12-08 17:40:07 -070027#include <asm/arch/systemagent.h>
Simon Glassabb4e422020-09-22 12:45:20 -060028#include <dt-bindings/sound/nhlt.h>
29#include <dm/acpi.h>
30
31enum {
32 PCIEXBAR = 0x60,
33 PCIEXBAR_LENGTH_256MB = 0,
34 PCIEXBAR_LENGTH_128MB,
35 PCIEXBAR_LENGTH_64MB,
36
37 PCIEXBAR_PCIEXBAREN = 1 << 0,
38
39 BGSM = 0xb4, /* Base GTT Stolen Memory */
40 TSEG = 0xb8, /* TSEG base */
41 TOLUD = 0xbc,
42};
Simon Glasse5fb75c2019-12-08 17:40:07 -070043
44/**
Simon Glass8a8d24b2020-12-03 16:55:23 -070045 * struct apl_hostbridge_plat - platform data for hostbridge
Simon Glasse5fb75c2019-12-08 17:40:07 -070046 *
47 * @dtplat: Platform data for of-platdata
48 * @early_pads: Early pad data to set up, each (pad, cfg0, cfg1)
49 * @early_pads_count: Number of pads to process
50 * @pciex_region_size: BAR length in bytes
51 * @bdf: Bus/device/function of hostbridge
52 */
Simon Glass8a8d24b2020-12-03 16:55:23 -070053struct apl_hostbridge_plat {
Simon Glasse5fb75c2019-12-08 17:40:07 -070054#if CONFIG_IS_ENABLED(OF_PLATDATA)
55 struct dtd_intel_apl_hostbridge dtplat;
56#endif
57 u32 *early_pads;
58 int early_pads_count;
59 uint pciex_region_size;
60 pci_dev_t bdf;
61};
62
Simon Glass5e89be12020-12-23 08:11:29 -070063#if CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
Simon Glassabb4e422020-09-22 12:45:20 -060064static const struct nhlt_format_config dmic_1ch_formats[] = {
65 /* 48 KHz 16-bits per sample. */
66 {
67 .num_channels = 1,
68 .sample_freq_khz = 48,
69 .container_bits_per_sample = 16,
70 .valid_bits_per_sample = 16,
71 .settings_file = "dmic-1ch-48khz-16b.dat",
72 },
73};
Simon Glasse5fb75c2019-12-08 17:40:07 -070074
Simon Glassabb4e422020-09-22 12:45:20 -060075static const struct nhlt_dmic_array_config dmic_1ch_mic_config = {
76 .tdm_config = {
77 .config_type = NHLT_TDM_MIC_ARRAY,
78 },
79 .array_type = NHLT_MIC_ARRAY_VENDOR_DEFINED,
80};
Simon Glasse5fb75c2019-12-08 17:40:07 -070081
Simon Glassabb4e422020-09-22 12:45:20 -060082static const struct nhlt_endp_descriptor dmic_1ch_descriptors[] = {
83 {
84 .link = NHLT_LINK_PDM,
85 .device = NHLT_PDM_DEV,
86 .direction = NHLT_DIR_CAPTURE,
87 .vid = NHLT_VID,
88 .did = NHLT_DID_DMIC,
89 .cfg = &dmic_1ch_mic_config,
90 .cfg_size = sizeof(dmic_1ch_mic_config),
91 .formats = dmic_1ch_formats,
92 .num_formats = ARRAY_SIZE(dmic_1ch_formats),
93 },
94};
95
96static const struct nhlt_format_config dmic_2ch_formats[] = {
97 /* 48 KHz 16-bits per sample. */
98 {
99 .num_channels = 2,
100 .sample_freq_khz = 48,
101 .container_bits_per_sample = 16,
102 .valid_bits_per_sample = 16,
103 .settings_file = "dmic-2ch-48khz-16b.dat",
104 },
105};
106
107static const struct nhlt_dmic_array_config dmic_2ch_mic_config = {
108 .tdm_config = {
109 .config_type = NHLT_TDM_MIC_ARRAY,
110 },
111 .array_type = NHLT_MIC_ARRAY_2CH_SMALL,
112};
113
114static const struct nhlt_endp_descriptor dmic_2ch_descriptors[] = {
115 {
116 .link = NHLT_LINK_PDM,
117 .device = NHLT_PDM_DEV,
118 .direction = NHLT_DIR_CAPTURE,
119 .vid = NHLT_VID,
120 .did = NHLT_DID_DMIC,
121 .cfg = &dmic_2ch_mic_config,
122 .cfg_size = sizeof(dmic_2ch_mic_config),
123 .formats = dmic_2ch_formats,
124 .num_formats = ARRAY_SIZE(dmic_2ch_formats),
125 },
126};
127
128static const struct nhlt_format_config dmic_4ch_formats[] = {
129 /* 48 KHz 16-bits per sample. */
130 {
131 .num_channels = 4,
132 .sample_freq_khz = 48,
133 .container_bits_per_sample = 16,
134 .valid_bits_per_sample = 16,
135 .settings_file = "dmic-4ch-48khz-16b.dat",
136 },
137};
138
139static const struct nhlt_dmic_array_config dmic_4ch_mic_config = {
140 .tdm_config = {
141 .config_type = NHLT_TDM_MIC_ARRAY,
142 },
143 .array_type = NHLT_MIC_ARRAY_4CH_L_SHAPED,
144};
145
146static const struct nhlt_endp_descriptor dmic_4ch_descriptors[] = {
147 {
148 .link = NHLT_LINK_PDM,
149 .device = NHLT_PDM_DEV,
150 .direction = NHLT_DIR_CAPTURE,
151 .vid = NHLT_VID,
152 .did = NHLT_DID_DMIC,
153 .cfg = &dmic_4ch_mic_config,
154 .cfg_size = sizeof(dmic_4ch_mic_config),
155 .formats = dmic_4ch_formats,
156 .num_formats = ARRAY_SIZE(dmic_4ch_formats),
157 },
Simon Glasse5fb75c2019-12-08 17:40:07 -0700158};
Simon Glass5e89be12020-12-23 08:11:29 -0700159#endif
Simon Glasse5fb75c2019-12-08 17:40:07 -0700160
161static int apl_hostbridge_early_init_pinctrl(struct udevice *dev)
162{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700163 struct apl_hostbridge_plat *plat = dev_get_plat(dev);
Simon Glasse5fb75c2019-12-08 17:40:07 -0700164 struct udevice *pinctrl;
165 int ret;
166
167 ret = uclass_first_device_err(UCLASS_PINCTRL, &pinctrl);
168 if (ret)
169 return log_msg_ret("no hostbridge pinctrl", ret);
170
171 return pinctrl_config_pads(pinctrl, plat->early_pads,
172 plat->early_pads_count);
173}
174
175static int apl_hostbridge_early_init(struct udevice *dev)
176{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700177 struct apl_hostbridge_plat *plat = dev_get_plat(dev);
Simon Glasse5fb75c2019-12-08 17:40:07 -0700178 u32 region_size;
179 ulong base;
180 u32 reg;
181 int ret;
182
183 /* Set up the MCHBAR */
184 pci_x86_read_config(plat->bdf, MCHBAR, &base, PCI_SIZE_32);
185 base = MCH_BASE_ADDRESS;
186 pci_x86_write_config(plat->bdf, MCHBAR, base | 1, PCI_SIZE_32);
187
188 /*
189 * The PCIEXBAR is assumed to live in the memory mapped IO space under
190 * 4GiB
191 */
192 pci_x86_write_config(plat->bdf, PCIEXBAR + 4, 0, PCI_SIZE_32);
193
194 switch (plat->pciex_region_size >> 20) {
195 default:
196 case 256:
197 region_size = PCIEXBAR_LENGTH_256MB;
198 break;
199 case 128:
200 region_size = PCIEXBAR_LENGTH_128MB;
201 break;
202 case 64:
203 region_size = PCIEXBAR_LENGTH_64MB;
204 break;
205 }
206
207 reg = CONFIG_MMCONF_BASE_ADDRESS | (region_size << 1)
208 | PCIEXBAR_PCIEXBAREN;
209 pci_x86_write_config(plat->bdf, PCIEXBAR, reg, PCI_SIZE_32);
210
211 /*
212 * TSEG defines the base of SMM range. BIOS determines the base
213 * of TSEG memory which must be at or below Graphics base of GTT
214 * Stolen memory, hence its better to clear TSEG register early
215 * to avoid power on default non-zero value (if any).
216 */
217 pci_x86_write_config(plat->bdf, TSEG, 0, PCI_SIZE_32);
218
219 ret = apl_hostbridge_early_init_pinctrl(dev);
220 if (ret)
221 return log_msg_ret("pinctrl", ret);
222
223 return 0;
224}
225
Simon Glassd1998a92020-12-03 16:55:21 -0700226static int apl_hostbridge_of_to_plat(struct udevice *dev)
Simon Glasse5fb75c2019-12-08 17:40:07 -0700227{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700228 struct apl_hostbridge_plat *plat = dev_get_plat(dev);
Simon Glasse5fb75c2019-12-08 17:40:07 -0700229 struct udevice *pinctrl;
230 int ret;
231
232 /*
233 * The host bridge holds the early pad data needed to get through TPL.
234 * This is a small amount of data, enough to fit in TPL, so we keep it
235 * separate from the full pad data, stored in the fsp-s subnode. That
236 * subnode is not present in TPL, to save space.
237 */
238 ret = uclass_first_device_err(UCLASS_PINCTRL, &pinctrl);
239 if (ret)
240 return log_msg_ret("no hostbridge PINCTRL", ret);
241#if !CONFIG_IS_ENABLED(OF_PLATDATA)
242 int root;
243
244 /* Get length of PCI Express Region */
245 plat->pciex_region_size = dev_read_u32_default(dev, "pciex-region-size",
246 256 << 20);
247
248 root = pci_get_devfn(dev);
249 if (root < 0)
250 return log_msg_ret("Cannot get host-bridge PCI address", root);
251 plat->bdf = root;
252
253 ret = pinctrl_read_pads(pinctrl, dev_ofnode(dev), "early-pads",
254 &plat->early_pads, &plat->early_pads_count);
255 if (ret)
256 return log_msg_ret("early-pads", ret);
257#else
258 struct dtd_intel_apl_hostbridge *dtplat = &plat->dtplat;
259 int size;
260
261 plat->pciex_region_size = dtplat->pciex_region_size;
262 plat->bdf = pci_ofplat_get_devfn(dtplat->reg[0]);
263
264 /* Assume that if everything is 0, it is empty */
265 plat->early_pads = dtplat->early_pads;
266 size = ARRAY_SIZE(dtplat->early_pads);
267 plat->early_pads_count = pinctrl_count_pads(pinctrl, plat->early_pads,
268 size);
269
270#endif
271
272 return 0;
273}
274
275static int apl_hostbridge_probe(struct udevice *dev)
276{
277 if (spl_phase() == PHASE_TPL)
278 return apl_hostbridge_early_init(dev);
279
280 return 0;
281}
282
Simon Glassabb4e422020-09-22 12:45:20 -0600283static int apl_acpi_hb_get_name(const struct udevice *dev, char *out_name)
284{
285 return acpi_copy_name(out_name, "RHUB");
286}
287
Simon Glass5e89be12020-12-23 08:11:29 -0700288#if CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
Simon Glassabb4e422020-09-22 12:45:20 -0600289static int apl_acpi_hb_write_tables(const struct udevice *dev,
290 struct acpi_ctx *ctx)
291{
292 struct acpi_table_header *header;
293 struct acpi_dmar *dmar;
294 u32 val;
295
296 /*
297 * Create DMAR table only if virtualization is enabled. Due to some
298 * constraints on Apollo Lake SoC (some stepping affected), VTD could
299 * not be enabled together with IPU. Doing so will override and disable
300 * VTD while leaving CAPID0_A still reporting that VTD is available.
301 * As in this case FSP will lock VTD to disabled state, we need to make
302 * sure that DMAR table generation only happens when at least DEFVTBAR
303 * is enabled. Otherwise the DMAR header will be generated while the
304 * content of the table will be missing.
305 */
306 dm_pci_read_config32(dev, CAPID0_A, &val);
307 if ((val & VTD_DISABLE) ||
308 !(readl(MCHBAR_REG(DEFVTBAR)) & VTBAR_ENABLED))
309 return 0;
310
311 log_debug("ACPI: * DMAR\n");
312 dmar = (struct acpi_dmar *)ctx->current;
313 header = &dmar->header;
314 acpi_create_dmar(dmar, DMAR_INTR_REMAP);
315 ctx->current += sizeof(struct acpi_dmar);
316 apl_acpi_fill_dmar(ctx);
317
318 /* (Re)calculate length and checksum */
319 header->length = ctx->current - (void *)dmar;
320 header->checksum = table_compute_checksum((void *)dmar, header->length);
321
322 acpi_align(ctx);
323 acpi_add_table(ctx, dmar);
324
325 return 0;
326}
Simon Glassabb4e422020-09-22 12:45:20 -0600327
328static int apl_acpi_setup_nhlt(const struct udevice *dev, struct acpi_ctx *ctx)
329{
330 struct nhlt *nhlt = ctx->nhlt;
331 u32 channels;
332 ofnode node;
333
334 node = ofnode_find_subnode(dev_ofnode(dev), "nhlt");
335 if (ofnode_read_u32(node, "intel,dmic-channels", &channels))
336 return log_msg_ret("channels", -EINVAL);
337 switch (channels) {
338 case 1:
339 return nhlt_add_endpoints(nhlt, dmic_1ch_descriptors,
340 ARRAY_SIZE(dmic_1ch_descriptors));
341 case 2:
342 return nhlt_add_endpoints(nhlt, dmic_2ch_descriptors,
343 ARRAY_SIZE(dmic_2ch_descriptors));
344 case 4:
345 return nhlt_add_endpoints(nhlt, dmic_4ch_descriptors,
346 ARRAY_SIZE(dmic_4ch_descriptors));
347 }
348
349 return log_msg_ret("channels", -EINVAL);
350}
Simon Glass5e89be12020-12-23 08:11:29 -0700351#endif
Simon Glassabb4e422020-09-22 12:45:20 -0600352
353static int apl_hostbridge_remove(struct udevice *dev)
354{
355 /*
356 * TODO(sjg@chromium.org): Consider adding code from coreboot's
357 * platform_fsp_notify_status()
358 */
359
360 return 0;
361}
362
Simon Glass94c5ad22020-09-22 12:45:17 -0600363static ulong sa_read_reg(struct udevice *dev, int reg)
364{
365 u32 val;
366
367 /* All regions concerned for have 1 MiB alignment */
368 dm_pci_read_config32(dev, BGSM, &val);
369
370 return ALIGN_DOWN(val, 1 << 20);
371}
372
373ulong sa_get_tolud_base(struct udevice *dev)
374{
375 return sa_read_reg(dev, TOLUD);
376}
377
378ulong sa_get_gsm_base(struct udevice *dev)
379{
380 return sa_read_reg(dev, BGSM);
381}
382
383ulong sa_get_tseg_base(struct udevice *dev)
384{
385 return sa_read_reg(dev, TSEG);
386}
387
Simon Glassabb4e422020-09-22 12:45:20 -0600388struct acpi_ops apl_hostbridge_acpi_ops = {
389 .get_name = apl_acpi_hb_get_name,
Simon Glass5e89be12020-12-23 08:11:29 -0700390#if CONFIG_IS_ENABLED(GENERATE_ACPI_TABLE)
Simon Glassabb4e422020-09-22 12:45:20 -0600391 .write_tables = apl_acpi_hb_write_tables,
Simon Glassabb4e422020-09-22 12:45:20 -0600392 .setup_nhlt = apl_acpi_setup_nhlt,
Simon Glass5e89be12020-12-23 08:11:29 -0700393#endif
Simon Glassabb4e422020-09-22 12:45:20 -0600394};
395
Simon Glass5e89be12020-12-23 08:11:29 -0700396#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glasse5fb75c2019-12-08 17:40:07 -0700397static const struct udevice_id apl_hostbridge_ids[] = {
398 { .compatible = "intel,apl-hostbridge" },
399 { }
400};
Simon Glass5e89be12020-12-23 08:11:29 -0700401#endif
Simon Glasse5fb75c2019-12-08 17:40:07 -0700402
Simon Glass9d20db02020-10-05 05:27:01 -0600403U_BOOT_DRIVER(intel_apl_hostbridge) = {
Simon Glasse5fb75c2019-12-08 17:40:07 -0700404 .name = "intel_apl_hostbridge",
405 .id = UCLASS_NORTHBRIDGE,
Simon Glass5e89be12020-12-23 08:11:29 -0700406 .of_match = of_match_ptr(apl_hostbridge_ids),
Simon Glassd1998a92020-12-03 16:55:21 -0700407 .of_to_plat = apl_hostbridge_of_to_plat,
Simon Glasse5fb75c2019-12-08 17:40:07 -0700408 .probe = apl_hostbridge_probe,
Simon Glassabb4e422020-09-22 12:45:20 -0600409 .remove = apl_hostbridge_remove,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700410 .plat_auto = sizeof(struct apl_hostbridge_plat),
Simon Glassabb4e422020-09-22 12:45:20 -0600411 ACPI_OPS_PTR(&apl_hostbridge_acpi_ops)
412 .flags = DM_FLAG_OS_PREPARE,
Simon Glasse5fb75c2019-12-08 17:40:07 -0700413};