Simon Glass | 36a6cf3 | 2019-12-08 17:40:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright 2019 Google LLC |
| 4 | * |
| 5 | * From coreboot Apollo Lake support lpc.c |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <spl.h> |
| 11 | #include <asm/lpc_common.h> |
| 12 | #include <asm/pci.h> |
| 13 | #include <asm/arch/iomap.h> |
| 14 | #include <asm/arch/lpc.h> |
| 15 | #include <linux/log2.h> |
| 16 | |
| 17 | void lpc_enable_fixed_io_ranges(uint io_enables) |
| 18 | { |
| 19 | pci_x86_clrset_config(PCH_DEV_LPC, LPC_IO_ENABLES, 0, io_enables, |
| 20 | PCI_SIZE_16); |
| 21 | } |
| 22 | |
| 23 | /* |
| 24 | * Find the first unused IO window. |
| 25 | * Returns -1 if not found, 0 for reg 0x84, 1 for reg 0x88 ... |
| 26 | */ |
| 27 | static int find_unused_pmio_window(void) |
| 28 | { |
| 29 | int i; |
| 30 | ulong lgir; |
| 31 | |
| 32 | for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++) { |
| 33 | pci_x86_read_config(PCH_DEV_LPC, LPC_GENERIC_IO_RANGE(i), |
| 34 | &lgir, PCI_SIZE_32); |
| 35 | |
| 36 | if (!(lgir & LPC_LGIR_EN)) |
| 37 | return i; |
| 38 | } |
| 39 | |
| 40 | return -1; |
| 41 | } |
| 42 | |
| 43 | int lpc_open_pmio_window(uint base, uint size) |
| 44 | { |
| 45 | int i, lgir_reg_num; |
| 46 | u32 lgir_reg_offset, lgir, window_size, alignment; |
| 47 | ulong bridged_size, bridge_base; |
| 48 | ulong reg; |
| 49 | |
| 50 | log_debug("LPC: Trying to open IO window from %x size %x\n", base, |
| 51 | size); |
| 52 | |
| 53 | bridged_size = 0; |
| 54 | bridge_base = base; |
| 55 | |
| 56 | while (bridged_size < size) { |
| 57 | /* Each IO range register can only open a 256-byte window */ |
| 58 | window_size = min(size, (uint)LPC_LGIR_MAX_WINDOW_SIZE); |
| 59 | |
| 60 | /* Window size must be a power of two for the AMASK to work */ |
| 61 | alignment = 1UL << (order_base_2(window_size)); |
| 62 | window_size = ALIGN(window_size, alignment); |
| 63 | |
| 64 | /* Address[15:2] in LGIR[15:12] and Mask[7:2] in LGIR[23:18] */ |
| 65 | lgir = (bridge_base & LPC_LGIR_ADDR_MASK) | LPC_LGIR_EN; |
| 66 | lgir |= ((window_size - 1) << 16) & LPC_LGIR_AMASK_MASK; |
| 67 | |
| 68 | /* Skip programming if same range already programmed */ |
| 69 | for (i = 0; i < LPC_NUM_GENERIC_IO_RANGES; i++) { |
| 70 | pci_x86_read_config(PCH_DEV_LPC, |
| 71 | LPC_GENERIC_IO_RANGE(i), ®, |
| 72 | PCI_SIZE_32); |
| 73 | if (lgir == reg) |
| 74 | return -EALREADY; |
| 75 | } |
| 76 | |
| 77 | lgir_reg_num = find_unused_pmio_window(); |
| 78 | if (lgir_reg_num < 0) { |
| 79 | log_err("LPC: Cannot open IO window: %lx size %lx\n", |
| 80 | bridge_base, size - bridged_size); |
| 81 | log_err("No more IO windows\n"); |
| 82 | |
| 83 | return -ENOSPC; |
| 84 | } |
| 85 | lgir_reg_offset = LPC_GENERIC_IO_RANGE(lgir_reg_num); |
| 86 | |
| 87 | pci_x86_write_config(PCH_DEV_LPC, lgir_reg_offset, lgir, |
| 88 | PCI_SIZE_32); |
| 89 | |
| 90 | log_debug("LPC: Opened IO window LGIR%d: base %lx size %x\n", |
| 91 | lgir_reg_num, bridge_base, window_size); |
| 92 | |
| 93 | bridged_size += window_size; |
| 94 | bridge_base += window_size; |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | void lpc_io_setup_comm_a_b(void) |
| 101 | { |
| 102 | /* ComA Range 3F8h-3FFh [2:0] */ |
| 103 | u16 com_ranges = LPC_IOD_COMA_RANGE; |
| 104 | u16 com_enable = LPC_IOE_COMA_EN; |
| 105 | |
| 106 | /* Setup I/O Decode Range Register for LPC */ |
| 107 | pci_write_config16(PCH_DEV_LPC, LPC_IO_DECODE, com_ranges); |
| 108 | /* Enable ComA and ComB Port */ |
| 109 | lpc_enable_fixed_io_ranges(com_enable); |
| 110 | } |
| 111 | |
| 112 | static const struct udevice_id apl_lpc_ids[] = { |
| 113 | { .compatible = "intel,apl-lpc" }, |
| 114 | { } |
| 115 | }; |
| 116 | |
| 117 | /* All pads are LPC already configured by the hostbridge, so no probing here */ |
| 118 | U_BOOT_DRIVER(apl_lpc_drv) = { |
| 119 | .name = "intel_apl_lpc", |
| 120 | .id = UCLASS_LPC, |
| 121 | .of_match = apl_lpc_ids, |
| 122 | }; |