Alexandru Gagniuc | e237f8b | 2016-03-30 12:09:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2016 Intel Corp. |
| 5 | * (Written by Alexandru Gagniuc <alexandrux.gagniuc@intel.com> for Intel Corp.) |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #define __SIMPLE_DEVICE__ |
| 19 | |
| 20 | #include <console/console.h> |
| 21 | #include <device/pci.h> |
| 22 | #include <lib.h> |
Alexandru Gagniuc | bdd921c | 2016-04-28 10:38:05 -0700 | [diff] [blame] | 23 | #include <soc/gpio.h> |
Alexandru Gagniuc | e237f8b | 2016-03-30 12:09:05 -0700 | [diff] [blame] | 24 | #include <soc/lpc.h> |
| 25 | #include <soc/pci_devs.h> |
| 26 | |
| 27 | /* |
| 28 | * These are MMIO ranges that the silicon designers decided are always going to |
| 29 | * be decoded to LPC. |
| 30 | */ |
| 31 | static const struct lpc_mmio_range { |
| 32 | uintptr_t base; |
| 33 | size_t size; |
| 34 | } lpc_fixed_mmio_ranges[] = { |
| 35 | { 0xfed40000, 0x8000 }, |
| 36 | { 0xfedc0000, 0x4000 }, |
| 37 | { 0xfed20800, 16 }, |
| 38 | { 0xfed20880, 8 }, |
| 39 | { 0xfed208e0, 16 }, |
| 40 | { 0xfed208f0, 8 }, |
| 41 | { 0xfed30800, 16 }, |
| 42 | { 0xfed30880, 8 }, |
| 43 | { 0xfed308e0, 16 }, |
| 44 | { 0xfed308f0, 8 }, |
| 45 | { 0, 0 } |
| 46 | }; |
| 47 | |
Alexandru Gagniuc | bdd921c | 2016-04-28 10:38:05 -0700 | [diff] [blame] | 48 | static const struct pad_config lpc_gpios[] = { |
Kane Chen | 5976143 | 2016-07-28 19:41:15 +0800 | [diff] [blame] | 49 | PAD_CFG_NF(LPC_ILB_SERIRQ, UP_20K, DEEP, NF1), |
Alexandru Gagniuc | bdd921c | 2016-04-28 10:38:05 -0700 | [diff] [blame] | 50 | PAD_CFG_NF(LPC_AD0, NATIVE, DEEP, NF1), |
| 51 | PAD_CFG_NF(LPC_AD1, NATIVE, DEEP, NF1), |
| 52 | PAD_CFG_NF(LPC_AD2, NATIVE, DEEP, NF1), |
| 53 | PAD_CFG_NF(LPC_AD3, NATIVE, DEEP, NF1), |
| 54 | PAD_CFG_NF(LPC_FRAMEB, NATIVE, DEEP, NF1), |
| 55 | PAD_CFG_NF(LPC_CLKOUT0, UP_20K, DEEP, NF1), |
| 56 | PAD_CFG_NF(LPC_CLKOUT1, UP_20K, DEEP, NF1) |
| 57 | }; |
| 58 | |
| 59 | void lpc_configure_pads(void) |
| 60 | { |
| 61 | gpio_configure_pads(lpc_gpios, ARRAY_SIZE(lpc_gpios)); |
| 62 | } |
| 63 | |
Alexandru Gagniuc | e237f8b | 2016-03-30 12:09:05 -0700 | [diff] [blame] | 64 | void lpc_enable_fixed_io_ranges(uint16_t io_enables) |
| 65 | { |
| 66 | uint16_t reg_io_enables; |
| 67 | |
| 68 | reg_io_enables = pci_read_config16(LPC_DEV, REG_IO_ENABLES); |
| 69 | io_enables |= reg_io_enables; |
| 70 | pci_write_config16(LPC_DEV, REG_IO_ENABLES, io_enables); |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Find the first unused IO window. |
| 75 | * Returns -1 if not found, 0 for reg 0x84, 1 for reg 0x88 ... |
| 76 | */ |
| 77 | static int find_unused_pmio_window(void) |
| 78 | { |
| 79 | int i; |
| 80 | uint32_t lgir; |
| 81 | |
| 82 | for (i = 0; i < NUM_GENERIC_IO_RANGES; i++) { |
| 83 | lgir = pci_read_config32(LPC_DEV, REG_GENERIC_IO_RANGE(i)); |
| 84 | |
| 85 | if (!(lgir & LGIR_EN)) |
| 86 | return i; |
| 87 | } |
| 88 | |
| 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | void lpc_close_pmio_windows(void) |
| 93 | { |
| 94 | size_t i; |
| 95 | |
| 96 | for (i = 0; i < NUM_GENERIC_IO_RANGES; i++) |
| 97 | pci_write_config32(LPC_DEV, REG_GENERIC_IO_RANGE(i), 0); |
| 98 | } |
| 99 | |
| 100 | void lpc_open_pmio_window(uint16_t base, uint16_t size) |
| 101 | { |
| 102 | int lgir_reg_num; |
| 103 | uint32_t lgir_reg_offset, lgir, window_size, alignment; |
| 104 | resource_t bridged_size, bridge_base; |
| 105 | |
| 106 | printk(BIOS_SPEW, "LPC: Trying to open IO window from %x size %x\n", |
| 107 | base, size); |
| 108 | |
| 109 | bridged_size = 0; |
| 110 | bridge_base = base; |
| 111 | |
| 112 | while (bridged_size < size) { |
| 113 | lgir_reg_num = find_unused_pmio_window(); |
| 114 | if (lgir_reg_num < 0) { |
| 115 | printk(BIOS_ERR, |
| 116 | "LPC: Cannot open IO window: %llx size %llx\n", |
| 117 | bridge_base, size - bridged_size); |
| 118 | printk(BIOS_ERR, "No more IO windows\n"); |
| 119 | return; |
| 120 | } |
| 121 | lgir_reg_offset = REG_GENERIC_IO_RANGE(lgir_reg_num); |
| 122 | |
| 123 | /* Each IO range register can only open a 256-byte window. */ |
| 124 | window_size = MIN(size, LGIR_MAX_WINDOW_SIZE); |
| 125 | |
| 126 | /* Window size must be a power of two for the AMASK to work. */ |
| 127 | alignment = 1 << (log2_ceil(window_size)); |
| 128 | window_size = ALIGN_UP(window_size, alignment); |
| 129 | |
| 130 | /* Address[15:2] in LGIR[15:12] and Mask[7:2] in LGIR[23:18]. */ |
| 131 | lgir = (bridge_base & LGIR_ADDR_MASK) | LGIR_EN; |
| 132 | lgir |= ((window_size - 1) << 16) & LGIR_AMASK_MASK; |
| 133 | |
| 134 | pci_write_config32(LPC_DEV, lgir_reg_offset, lgir); |
| 135 | |
| 136 | printk(BIOS_DEBUG, |
| 137 | "LPC: Opened IO window LGIR%d: base %llx size %x\n", |
| 138 | lgir_reg_num, bridge_base, window_size); |
| 139 | |
| 140 | bridged_size += window_size; |
| 141 | bridge_base += window_size; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void lpc_open_mmio_window(uintptr_t base, size_t size) |
| 146 | { |
| 147 | uint32_t lgmr; |
| 148 | |
| 149 | lgmr = pci_read_config32(LPC_DEV, REG_GENERIC_MEM_RANGE); |
| 150 | |
| 151 | if (lgmr & LGMR_EN) { |
| 152 | printk(BIOS_ERR, |
Zhao, Lijian | 0f78857 | 2016-05-16 11:41:41 -0700 | [diff] [blame] | 153 | "LPC: Cannot open window to resource %lx size %zx\n", |
Alexandru Gagniuc | e237f8b | 2016-03-30 12:09:05 -0700 | [diff] [blame] | 154 | base, size); |
| 155 | printk(BIOS_ERR, "LPC: MMIO window already in use\n"); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | if (size > LGMR_WINDOW_SIZE) { |
| 160 | printk(BIOS_WARNING, |
Zhao, Lijian | 0f78857 | 2016-05-16 11:41:41 -0700 | [diff] [blame] | 161 | "LPC: Resource %lx size %zx larger than window(%x)\n", |
Alexandru Gagniuc | e237f8b | 2016-03-30 12:09:05 -0700 | [diff] [blame] | 162 | base, size, LGMR_WINDOW_SIZE); |
| 163 | } |
| 164 | |
| 165 | lgmr = (base & LGMR_ADDR_MASK) | LGMR_EN; |
| 166 | |
| 167 | pci_write_config32(LPC_DEV, REG_GENERIC_MEM_RANGE, lgmr); |
| 168 | } |
| 169 | |
| 170 | bool lpc_fits_fixed_mmio_window(uintptr_t base, size_t size) |
| 171 | { |
| 172 | resource_t res_end, range_end; |
| 173 | const struct lpc_mmio_range *range; |
| 174 | |
| 175 | for (range = lpc_fixed_mmio_ranges; range->size; range++) { |
| 176 | range_end = range->base + range->size; |
| 177 | res_end = base + size; |
| 178 | |
| 179 | if ((base >= range->base) && (res_end <= range_end)) { |
| 180 | printk(BIOS_DEBUG, |
Zhao, Lijian | 0f78857 | 2016-05-16 11:41:41 -0700 | [diff] [blame] | 181 | "Resource %lx size %zx fits in fixed window" |
| 182 | " %lx size %zx\n", |
Alexandru Gagniuc | e237f8b | 2016-03-30 12:09:05 -0700 | [diff] [blame] | 183 | base, size, range->base, range->size); |
| 184 | return true; |
| 185 | } |
| 186 | } |
| 187 | return false; |
| 188 | } |