1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <device/mmio.h>
4 #include <device/pci_ops.h>
5 #include <console/console.h>
7 #include <device/device.h>
8 #include <device/pci.h>
10 #include <reg_script.h>
13 #include <cpu/x86/msr.h>
23 #define HAS_IOSF (CONFIG(SOC_INTEL_BAYTRAIL))
26 #include <soc/iosf.h> /* TODO: wrap in <soc/reg_script.h, remove #ifdef? */
29 #define POLL_DELAY 100 /* 100us */
31 #ifdef __SIMPLE_DEVICE__
34 #define EMPTY_DEV NULL
37 #ifdef __SIMPLE_DEVICE__
38 static inline void reg_script_set_dev(struct reg_script_context
*ctx
,
41 static inline void reg_script_set_dev(struct reg_script_context
*ctx
,
49 static inline void reg_script_set_step(struct reg_script_context
*ctx
,
50 const struct reg_script
*step
)
55 static inline const struct reg_script
*
56 reg_script_get_step(struct reg_script_context
*ctx
)
61 static struct resource
*reg_script_get_resource(struct reg_script_context
*ctx
)
63 #ifdef __SIMPLE_DEVICE__
67 const struct reg_script
*step
= reg_script_get_step(ctx
);
71 if (res
!= NULL
&& res
->index
== step
->res_index
)
74 res
= find_resource(ctx
->dev
, step
->res_index
);
80 static uint32_t reg_script_read_pci(struct reg_script_context
*ctx
)
82 const struct reg_script
*step
= reg_script_get_step(ctx
);
85 case REG_SCRIPT_SIZE_8
:
86 return pci_read_config8(ctx
->dev
, step
->reg
);
87 case REG_SCRIPT_SIZE_16
:
88 return pci_read_config16(ctx
->dev
, step
->reg
);
89 case REG_SCRIPT_SIZE_32
:
90 return pci_read_config32(ctx
->dev
, step
->reg
);
95 static void reg_script_write_pci(struct reg_script_context
*ctx
)
97 const struct reg_script
*step
= reg_script_get_step(ctx
);
100 case REG_SCRIPT_SIZE_8
:
101 pci_write_config8(ctx
->dev
, step
->reg
, step
->value
);
103 case REG_SCRIPT_SIZE_16
:
104 pci_write_config16(ctx
->dev
, step
->reg
, step
->value
);
106 case REG_SCRIPT_SIZE_32
:
107 pci_write_config32(ctx
->dev
, step
->reg
, step
->value
);
113 static uint32_t reg_script_read_io(struct reg_script_context
*ctx
)
115 const struct reg_script
*step
= reg_script_get_step(ctx
);
117 switch (step
->size
) {
118 case REG_SCRIPT_SIZE_8
:
119 return inb(step
->reg
);
120 case REG_SCRIPT_SIZE_16
:
121 return inw(step
->reg
);
122 case REG_SCRIPT_SIZE_32
:
123 return inl(step
->reg
);
128 static void reg_script_write_io(struct reg_script_context
*ctx
)
130 const struct reg_script
*step
= reg_script_get_step(ctx
);
132 switch (step
->size
) {
133 case REG_SCRIPT_SIZE_8
:
134 outb(step
->value
, step
->reg
);
136 case REG_SCRIPT_SIZE_16
:
137 outw(step
->value
, step
->reg
);
139 case REG_SCRIPT_SIZE_32
:
140 outl(step
->value
, step
->reg
);
146 static uint32_t reg_script_read_mmio(struct reg_script_context
*ctx
)
148 const struct reg_script
*step
= reg_script_get_step(ctx
);
150 switch (step
->size
) {
151 case REG_SCRIPT_SIZE_8
:
152 return read8((u8
*)(uintptr_t)step
->reg
);
153 case REG_SCRIPT_SIZE_16
:
154 return read16((u16
*)(uintptr_t)step
->reg
);
155 case REG_SCRIPT_SIZE_32
:
156 return read32((u32
*)(uintptr_t)step
->reg
);
161 static void reg_script_write_mmio(struct reg_script_context
*ctx
)
163 const struct reg_script
*step
= reg_script_get_step(ctx
);
165 switch (step
->size
) {
166 case REG_SCRIPT_SIZE_8
:
167 write8((u8
*)(uintptr_t)step
->reg
, step
->value
);
169 case REG_SCRIPT_SIZE_16
:
170 write16((u16
*)(uintptr_t)step
->reg
, step
->value
);
172 case REG_SCRIPT_SIZE_32
:
173 write32((u32
*)(uintptr_t)step
->reg
, step
->value
);
178 static uint32_t reg_script_read_res(struct reg_script_context
*ctx
)
180 struct resource
*res
;
182 const struct reg_script
*step
= reg_script_get_step(ctx
);
184 res
= reg_script_get_resource(ctx
);
189 if (res
->flags
& IORESOURCE_IO
) {
190 const struct reg_script io_step
= {
192 .reg
= res
->base
+ step
->reg
,
194 reg_script_set_step(ctx
, &io_step
);
195 val
= reg_script_read_io(ctx
);
196 } else if (res
->flags
& IORESOURCE_MEM
) {
197 const struct reg_script mmio_step
= {
199 .reg
= res
->base
+ step
->reg
,
201 reg_script_set_step(ctx
, &mmio_step
);
202 val
= reg_script_read_mmio(ctx
);
204 reg_script_set_step(ctx
, step
);
208 static void reg_script_write_res(struct reg_script_context
*ctx
)
210 struct resource
*res
;
211 const struct reg_script
*step
= reg_script_get_step(ctx
);
213 res
= reg_script_get_resource(ctx
);
218 if (res
->flags
& IORESOURCE_IO
) {
219 const struct reg_script io_step
= {
221 .reg
= res
->base
+ step
->reg
,
222 .value
= step
->value
,
224 reg_script_set_step(ctx
, &io_step
);
225 reg_script_write_io(ctx
);
226 } else if (res
->flags
& IORESOURCE_MEM
) {
227 const struct reg_script mmio_step
= {
229 .reg
= res
->base
+ step
->reg
,
230 .value
= step
->value
,
232 reg_script_set_step(ctx
, &mmio_step
);
233 reg_script_write_mmio(ctx
);
235 reg_script_set_step(ctx
, step
);
239 static uint32_t reg_script_read_iosf(struct reg_script_context
*ctx
)
241 const struct reg_script
*step
= reg_script_get_step(ctx
);
244 case IOSF_PORT_AUNIT
:
245 return iosf_aunit_read(step
->reg
);
246 case IOSF_PORT_CPU_BUS
:
247 return iosf_cpu_bus_read(step
->reg
);
248 case IOSF_PORT_BUNIT
:
249 return iosf_bunit_read(step
->reg
);
250 case IOSF_PORT_DUNIT_CH0
:
251 return iosf_dunit_ch0_read(step
->reg
);
253 return iosf_punit_read(step
->reg
);
254 case IOSF_PORT_USBPHY
:
255 return iosf_usbphy_read(step
->reg
);
257 return iosf_sec_read(step
->reg
);
259 return iosf_port45_read(step
->reg
);
261 return iosf_port46_read(step
->reg
);
263 return iosf_port47_read(step
->reg
);
264 case IOSF_PORT_SCORE
:
265 return iosf_score_read(step
->reg
);
267 return iosf_port55_read(step
->reg
);
269 return iosf_port58_read(step
->reg
);
271 return iosf_port59_read(step
->reg
);
273 return iosf_port5a_read(step
->reg
);
274 case IOSF_PORT_USHPHY
:
275 return iosf_ushphy_read(step
->reg
);
277 return iosf_scc_read(step
->reg
);
279 return iosf_lpss_read(step
->reg
);
281 return iosf_porta2_read(step
->reg
);
283 return iosf_ccu_read(step
->reg
);
285 return iosf_ssus_read(step
->reg
);
287 printk(BIOS_DEBUG
, "No read support for IOSF port 0x%x.\n",
294 static void reg_script_write_iosf(struct reg_script_context
*ctx
)
296 const struct reg_script
*step
= reg_script_get_step(ctx
);
299 case IOSF_PORT_AUNIT
:
300 iosf_aunit_write(step
->reg
, step
->value
);
302 case IOSF_PORT_CPU_BUS
:
303 iosf_cpu_bus_write(step
->reg
, step
->value
);
305 case IOSF_PORT_BUNIT
:
306 iosf_bunit_write(step
->reg
, step
->value
);
308 case IOSF_PORT_DUNIT_CH0
:
309 iosf_dunit_write(step
->reg
, step
->value
);
312 iosf_punit_write(step
->reg
, step
->value
);
314 case IOSF_PORT_USBPHY
:
315 iosf_usbphy_write(step
->reg
, step
->value
);
318 iosf_sec_write(step
->reg
, step
->value
);
321 iosf_port45_write(step
->reg
, step
->value
);
324 iosf_port46_write(step
->reg
, step
->value
);
327 iosf_port47_write(step
->reg
, step
->value
);
329 case IOSF_PORT_SCORE
:
330 iosf_score_write(step
->reg
, step
->value
);
333 iosf_port55_write(step
->reg
, step
->value
);
336 iosf_port58_write(step
->reg
, step
->value
);
339 iosf_port59_write(step
->reg
, step
->value
);
342 iosf_port5a_write(step
->reg
, step
->value
);
344 case IOSF_PORT_USHPHY
:
345 iosf_ushphy_write(step
->reg
, step
->value
);
348 iosf_scc_write(step
->reg
, step
->value
);
351 iosf_lpss_write(step
->reg
, step
->value
);
354 iosf_porta2_write(step
->reg
, step
->value
);
357 iosf_ccu_write(step
->reg
, step
->value
);
360 iosf_ssus_write(step
->reg
, step
->value
);
363 printk(BIOS_DEBUG
, "No write support for IOSF port 0x%x.\n",
368 #endif /* HAS_IOSF */
371 static uint64_t reg_script_read_msr(struct reg_script_context
*ctx
)
374 const struct reg_script
*step
= reg_script_get_step(ctx
);
375 msr_t msr
= rdmsr(step
->reg
);
376 uint64_t value
= msr
.hi
;
383 static void reg_script_write_msr(struct reg_script_context
*ctx
)
386 const struct reg_script
*step
= reg_script_get_step(ctx
);
388 msr
.hi
= step
->value
>> 32;
389 msr
.lo
= step
->value
& 0xffffffff;
390 wrmsr(step
->reg
, msr
);
394 /* Locate the structure containing the platform specific bus access routines */
395 static const struct reg_script_bus_entry
396 *find_bus(const struct reg_script
*step
)
398 extern const struct reg_script_bus_entry
*_rsbe_init_begin
[];
399 extern const struct reg_script_bus_entry
*_ersbe_init_begin
[];
400 const struct reg_script_bus_entry
* const *bus
;
401 size_t table_entries
;
404 /* Locate the platform specific bus */
405 bus
= _rsbe_init_begin
;
406 table_entries
= &_ersbe_init_begin
[0] - &_rsbe_init_begin
[0];
407 for (i
= 0; i
< table_entries
; i
++) {
408 if (bus
[i
]->type
== step
->type
)
416 static void reg_script_display(struct reg_script_context
*ctx
,
417 const struct reg_script
*step
, const char *arrow
, uint64_t value
)
419 /* Display the register address and data */
420 if (ctx
->display_prefix
!= NULL
)
421 printk(BIOS_INFO
, "%s: ", ctx
->display_prefix
);
422 if (ctx
->display_features
& REG_SCRIPT_DISPLAY_REGISTER
)
423 printk(BIOS_INFO
, "0x%08x %s ", step
->reg
, arrow
);
424 if (ctx
->display_features
& REG_SCRIPT_DISPLAY_VALUE
)
425 switch (step
->size
) {
426 case REG_SCRIPT_SIZE_8
:
427 printk(BIOS_INFO
, "0x%02x\n", (uint8_t)value
);
429 case REG_SCRIPT_SIZE_16
:
430 printk(BIOS_INFO
, "0x%04x\n", (int16_t)value
);
432 case REG_SCRIPT_SIZE_32
:
433 printk(BIOS_INFO
, "0x%08x\n", (uint32_t)value
);
436 printk(BIOS_INFO
, "0x%016llx\n", value
);
441 static uint64_t reg_script_read(struct reg_script_context
*ctx
)
443 const struct reg_script
*step
= reg_script_get_step(ctx
);
446 switch (step
->type
) {
447 case REG_SCRIPT_TYPE_PCI
:
448 ctx
->display_prefix
= "PCI";
449 value
= reg_script_read_pci(ctx
);
452 case REG_SCRIPT_TYPE_IO
:
453 ctx
->display_prefix
= "IO";
454 value
= reg_script_read_io(ctx
);
457 case REG_SCRIPT_TYPE_MMIO
:
458 ctx
->display_prefix
= "MMIO";
459 value
= reg_script_read_mmio(ctx
);
461 case REG_SCRIPT_TYPE_RES
:
462 ctx
->display_prefix
= "RES";
463 value
= reg_script_read_res(ctx
);
465 case REG_SCRIPT_TYPE_MSR
:
466 ctx
->display_prefix
= "MSR";
467 value
= reg_script_read_msr(ctx
);
470 case REG_SCRIPT_TYPE_IOSF
:
471 ctx
->display_prefix
= "IOSF";
472 value
= reg_script_read_iosf(ctx
);
474 #endif /* HAS_IOSF */
477 const struct reg_script_bus_entry
*bus
;
479 /* Read from the platform specific bus */
480 bus
= find_bus(step
);
482 value
= bus
->reg_script_read(ctx
);
487 "Unsupported read type (0x%x) for this device!\n",
492 /* Display the register address and data */
493 if (ctx
->display_features
)
494 reg_script_display(ctx
, step
, "-->", value
);
498 static void reg_script_write(struct reg_script_context
*ctx
)
500 const struct reg_script
*step
= reg_script_get_step(ctx
);
502 switch (step
->type
) {
503 case REG_SCRIPT_TYPE_PCI
:
504 ctx
->display_prefix
= "PCI";
505 reg_script_write_pci(ctx
);
508 case REG_SCRIPT_TYPE_IO
:
509 ctx
->display_prefix
= "IO";
510 reg_script_write_io(ctx
);
513 case REG_SCRIPT_TYPE_MMIO
:
514 ctx
->display_prefix
= "MMIO";
515 reg_script_write_mmio(ctx
);
517 case REG_SCRIPT_TYPE_RES
:
518 ctx
->display_prefix
= "RES";
519 reg_script_write_res(ctx
);
521 case REG_SCRIPT_TYPE_MSR
:
522 ctx
->display_prefix
= "MSR";
523 reg_script_write_msr(ctx
);
526 case REG_SCRIPT_TYPE_IOSF
:
527 ctx
->display_prefix
= "IOSF";
528 reg_script_write_iosf(ctx
);
530 #endif /* HAS_IOSF */
533 const struct reg_script_bus_entry
*bus
;
535 /* Write to the platform specific bus */
536 bus
= find_bus(step
);
538 bus
->reg_script_write(ctx
);
543 "Unsupported write type (0x%x) for this device!\n",
548 /* Display the register address and data */
549 if (ctx
->display_features
)
550 reg_script_display(ctx
, step
, "<--", step
->value
);
553 static void reg_script_rmw(struct reg_script_context
*ctx
)
556 const struct reg_script
*step
= reg_script_get_step(ctx
);
557 struct reg_script write_step
= *step
;
559 value
= reg_script_read(ctx
);
561 value
|= step
->value
;
562 write_step
.value
= value
;
563 reg_script_set_step(ctx
, &write_step
);
564 reg_script_write(ctx
);
565 reg_script_set_step(ctx
, step
);
568 static void reg_script_rxw(struct reg_script_context
*ctx
)
571 const struct reg_script
*step
= reg_script_get_step(ctx
);
572 struct reg_script write_step
= *step
;
582 * Supported operations
584 * Input Mask Temp XOR Value Operation
585 * 0 0 0 0 0 Clear bit
589 * 0 1 0 0 0 Preserve bit
591 * 0 1 0 1 1 Toggle bit
594 value
= reg_script_read(ctx
);
596 value
^= step
->value
;
597 write_step
.value
= value
;
598 reg_script_set_step(ctx
, &write_step
);
599 reg_script_write(ctx
);
600 reg_script_set_step(ctx
, step
);
603 /* In order to easily chain scripts together handle the REG_SCRIPT_COMMAND_NEXT
604 * as recursive call with a new context that has the same dev and resource
605 * as the previous one. That will run to completion and then move on to the
606 * next step of the previous context. */
607 static void reg_script_run_next(struct reg_script_context
*ctx
,
608 const struct reg_script
*step
);
611 static void reg_script_run_step(struct reg_script_context
*ctx
,
612 const struct reg_script
*step
)
614 uint64_t value
= 0, try;
616 ctx
->display_features
= ctx
->display_state
;
617 ctx
->display_prefix
= NULL
;
618 switch (step
->command
) {
619 case REG_SCRIPT_COMMAND_READ
:
620 (void)reg_script_read(ctx
);
622 case REG_SCRIPT_COMMAND_WRITE
:
623 reg_script_write(ctx
);
625 case REG_SCRIPT_COMMAND_RMW
:
628 case REG_SCRIPT_COMMAND_RXW
:
631 case REG_SCRIPT_COMMAND_POLL
:
632 for (try = 0; try < step
->timeout
; try += POLL_DELAY
) {
633 value
= reg_script_read(ctx
) & step
->mask
;
634 if (value
== step
->value
)
638 if (try >= step
->timeout
)
639 printk(BIOS_WARNING
, "%s: POLL timeout waiting for "
640 "0x%x to be 0x%lx, got 0x%lx\n", __func__
,
641 step
->reg
, (unsigned long)step
->value
,
642 (unsigned long)value
);
644 case REG_SCRIPT_COMMAND_SET_DEV
:
645 reg_script_set_dev(ctx
, step
->dev
);
647 case REG_SCRIPT_COMMAND_NEXT
:
648 reg_script_run_next(ctx
, step
->next
);
650 case REG_SCRIPT_COMMAND_DISPLAY
:
651 ctx
->display_state
= step
->value
;
655 printk(BIOS_WARNING
, "Invalid command: %08x\n",
661 static void reg_script_run_with_context(struct reg_script_context
*ctx
)
664 const struct reg_script
*step
= reg_script_get_step(ctx
);
666 if (step
->command
== REG_SCRIPT_COMMAND_END
)
669 reg_script_run_step(ctx
, step
);
670 reg_script_set_step(ctx
, step
+ 1);
674 static void reg_script_run_next(struct reg_script_context
*prev_ctx
,
675 const struct reg_script
*step
)
677 struct reg_script_context ctx
;
679 /* Use prev context as a basis but start at a new step. */
681 reg_script_set_step(&ctx
, step
);
682 reg_script_run_with_context(&ctx
);
685 #ifdef __SIMPLE_DEVICE__
686 void reg_script_run_on_dev(pci_devfn_t dev
, const struct reg_script
*step
)
688 void reg_script_run_on_dev(struct device
*dev
, const struct reg_script
*step
)
691 struct reg_script_context ctx
;
693 ctx
.display_state
= REG_SCRIPT_DISPLAY_NOTHING
;
694 reg_script_set_dev(&ctx
, dev
);
695 reg_script_set_step(&ctx
, step
);
696 reg_script_run_with_context(&ctx
);
699 void reg_script_run(const struct reg_script
*step
)
701 reg_script_run_on_dev(EMPTY_DEV
, step
);