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), |
Shamile Khan | 93c5470 | 2016-09-01 13:36:50 -0700 | [diff] [blame] | 50 | PAD_CFG_NF(LPC_CLKRUNB, UP_20K, DEEP, NF1), |
| 51 | PAD_CFG_NF(LPC_AD0, UP_20K, DEEP, NF1), |
| 52 | PAD_CFG_NF(LPC_AD1, UP_20K, DEEP, NF1), |
| 53 | PAD_CFG_NF(LPC_AD2, UP_20K, DEEP, NF1), |
| 54 | PAD_CFG_NF(LPC_AD3, UP_20K, DEEP, NF1), |
Alexandru Gagniuc | bdd921c | 2016-04-28 10:38:05 -0700 | [diff] [blame] | 55 | PAD_CFG_NF(LPC_FRAMEB, NATIVE, DEEP, NF1), |
| 56 | PAD_CFG_NF(LPC_CLKOUT0, UP_20K, DEEP, NF1), |
| 57 | PAD_CFG_NF(LPC_CLKOUT1, UP_20K, DEEP, NF1) |
| 58 | }; |
| 59 | |
| 60 | void lpc_configure_pads(void) |
| 61 | { |
| 62 | gpio_configure_pads(lpc_gpios, ARRAY_SIZE(lpc_gpios)); |
| 63 | } |
| 64 | |
Alexandru Gagniuc | e237f8b | 2016-03-30 12:09:05 -0700 | [diff] [blame] | 65 | void lpc_enable_fixed_io_ranges(uint16_t io_enables) |
| 66 | { |
| 67 | uint16_t reg_io_enables; |
| 68 | |
| 69 | reg_io_enables = pci_read_config16(LPC_DEV, REG_IO_ENABLES); |
| 70 | io_enables |= reg_io_enables; |
| 71 | pci_write_config16(LPC_DEV, REG_IO_ENABLES, io_enables); |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Find the first unused IO window. |
| 76 | * Returns -1 if not found, 0 for reg 0x84, 1 for reg 0x88 ... |
| 77 | */ |
| 78 | static int find_unused_pmio_window(void) |
| 79 | { |
| 80 | int i; |
| 81 | uint32_t lgir; |
| 82 | |
| 83 | for (i = 0; i < NUM_GENERIC_IO_RANGES; i++) { |
| 84 | lgir = pci_read_config32(LPC_DEV, REG_GENERIC_IO_RANGE(i)); |
| 85 | |
| 86 | if (!(lgir & LGIR_EN)) |
| 87 | return i; |
| 88 | } |
| 89 | |
| 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | void lpc_close_pmio_windows(void) |
| 94 | { |
| 95 | size_t i; |
| 96 | |
| 97 | for (i = 0; i < NUM_GENERIC_IO_RANGES; i++) |
| 98 | pci_write_config32(LPC_DEV, REG_GENERIC_IO_RANGE(i), 0); |
| 99 | } |
| 100 | |
| 101 | void lpc_open_pmio_window(uint16_t base, uint16_t size) |
| 102 | { |
| 103 | int lgir_reg_num; |
| 104 | uint32_t lgir_reg_offset, lgir, window_size, alignment; |
| 105 | resource_t bridged_size, bridge_base; |
| 106 | |
| 107 | printk(BIOS_SPEW, "LPC: Trying to open IO window from %x size %x\n", |
| 108 | base, size); |
| 109 | |
| 110 | bridged_size = 0; |
| 111 | bridge_base = base; |
| 112 | |
| 113 | while (bridged_size < size) { |
| 114 | lgir_reg_num = find_unused_pmio_window(); |
| 115 | if (lgir_reg_num < 0) { |
| 116 | printk(BIOS_ERR, |
| 117 | "LPC: Cannot open IO window: %llx size %llx\n", |
| 118 | bridge_base, size - bridged_size); |
| 119 | printk(BIOS_ERR, "No more IO windows\n"); |
| 120 | return; |
| 121 | } |
| 122 | lgir_reg_offset = REG_GENERIC_IO_RANGE(lgir_reg_num); |
| 123 | |
| 124 | /* Each IO range register can only open a 256-byte window. */ |
| 125 | window_size = MIN(size, LGIR_MAX_WINDOW_SIZE); |
| 126 | |
| 127 | /* Window size must be a power of two for the AMASK to work. */ |
| 128 | alignment = 1 << (log2_ceil(window_size)); |
| 129 | window_size = ALIGN_UP(window_size, alignment); |
| 130 | |
| 131 | /* Address[15:2] in LGIR[15:12] and Mask[7:2] in LGIR[23:18]. */ |
| 132 | lgir = (bridge_base & LGIR_ADDR_MASK) | LGIR_EN; |
| 133 | lgir |= ((window_size - 1) << 16) & LGIR_AMASK_MASK; |
| 134 | |
| 135 | pci_write_config32(LPC_DEV, lgir_reg_offset, lgir); |
| 136 | |
| 137 | printk(BIOS_DEBUG, |
| 138 | "LPC: Opened IO window LGIR%d: base %llx size %x\n", |
| 139 | lgir_reg_num, bridge_base, window_size); |
| 140 | |
| 141 | bridged_size += window_size; |
| 142 | bridge_base += window_size; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void lpc_open_mmio_window(uintptr_t base, size_t size) |
| 147 | { |
| 148 | uint32_t lgmr; |
| 149 | |
| 150 | lgmr = pci_read_config32(LPC_DEV, REG_GENERIC_MEM_RANGE); |
| 151 | |
| 152 | if (lgmr & LGMR_EN) { |
| 153 | printk(BIOS_ERR, |
Zhao, Lijian | 0f78857 | 2016-05-16 11:41:41 -0700 | [diff] [blame] | 154 | "LPC: Cannot open window to resource %lx size %zx\n", |
Alexandru Gagniuc | e237f8b | 2016-03-30 12:09:05 -0700 | [diff] [blame] | 155 | base, size); |
| 156 | printk(BIOS_ERR, "LPC: MMIO window already in use\n"); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | if (size > LGMR_WINDOW_SIZE) { |
| 161 | printk(BIOS_WARNING, |
Zhao, Lijian | 0f78857 | 2016-05-16 11:41:41 -0700 | [diff] [blame] | 162 | "LPC: Resource %lx size %zx larger than window(%x)\n", |
Alexandru Gagniuc | e237f8b | 2016-03-30 12:09:05 -0700 | [diff] [blame] | 163 | base, size, LGMR_WINDOW_SIZE); |
| 164 | } |
| 165 | |
| 166 | lgmr = (base & LGMR_ADDR_MASK) | LGMR_EN; |
| 167 | |
| 168 | pci_write_config32(LPC_DEV, REG_GENERIC_MEM_RANGE, lgmr); |
| 169 | } |
| 170 | |
| 171 | bool lpc_fits_fixed_mmio_window(uintptr_t base, size_t size) |
| 172 | { |
| 173 | resource_t res_end, range_end; |
| 174 | const struct lpc_mmio_range *range; |
| 175 | |
| 176 | for (range = lpc_fixed_mmio_ranges; range->size; range++) { |
| 177 | range_end = range->base + range->size; |
| 178 | res_end = base + size; |
| 179 | |
| 180 | if ((base >= range->base) && (res_end <= range_end)) { |
| 181 | printk(BIOS_DEBUG, |
Zhao, Lijian | 0f78857 | 2016-05-16 11:41:41 -0700 | [diff] [blame] | 182 | "Resource %lx size %zx fits in fixed window" |
| 183 | " %lx size %zx\n", |
Alexandru Gagniuc | e237f8b | 2016-03-30 12:09:05 -0700 | [diff] [blame] | 184 | base, size, range->base, range->size); |
| 185 | return true; |
| 186 | } |
| 187 | } |
| 188 | return false; |
| 189 | } |