|
| 1 | +/* |
| 2 | + * Copyright (c) 2016, Intel Corporation |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Author: Baofeng, Tian <[email protected]> |
| 6 | + * Author: Xinanx, Luo <[email protected]> |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions |
| 10 | + * are met: |
| 11 | + * |
| 12 | + * * Redistributions of source code must retain the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer. |
| 14 | + * * Redistributions in binary form must reproduce the above copyright |
| 15 | + * notice, this list of conditions and the following disclaimer |
| 16 | + * in the documentation and/or other materials provided with the |
| 17 | + * distribution. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 22 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 23 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 24 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 25 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 26 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 28 | + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 30 | + * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | + |
| 33 | +#include <hwconfig.h> |
| 34 | +#include <interface.h> |
| 35 | +#include <kconfig.h> |
| 36 | +#include <libpayload-config.h> |
| 37 | +#include <libpayload.h> |
| 38 | + |
| 39 | +#include "tco_wdt/tco_protocol.h" |
| 40 | +#include "tco_wdt/tco_wdt.h" |
| 41 | + |
| 42 | +#ifndef BIT |
| 43 | +#define BIT(x) (1 << (x)) |
| 44 | +#endif |
| 45 | + |
| 46 | +/* TCO Registers */ |
| 47 | +#define TCO_RLD (TCOBASE + 0x00) /* TCO Timer Reload/Curr. Value */ |
| 48 | +#define TCO1_STS (TCOBASE + 0x04) /* TCO1 Status Register */ |
| 49 | +#define TCO2_STS (TCOBASE + 0x06) /* TCO2 Status Register */ |
| 50 | +#define TCO1_CNT (TCOBASE + 0x08) /* TCO1 Control Register */ |
| 51 | +#define TCO2_CNT (TCOBASE + 0x0a) /* TCO1 Control Register */ |
| 52 | +#define TCOv2_TMR (TCOBASE + 0x12) /* TCOv2 Timer Initial Value*/ |
| 53 | + |
| 54 | +#define PMC_GCR_PMC_CFG_REG (0xfe043008) /*NO_REBOOT bit config reg*/ |
| 55 | + |
| 56 | +/* TCO Registers' bits */ |
| 57 | +#define TCO_HALT_BIT BIT(11) |
| 58 | +#define TCO_TIMEOUT1_BIT BIT(3) |
| 59 | +#define TCO_TIMEOUT2_BIT BIT(2) |
| 60 | +#define NO_REBOOT_BIT BIT(4) |
| 61 | + |
| 62 | +#define TCO_MIN_TIMEOUT 4 |
| 63 | + |
| 64 | +static EFI_GUID tco_wdt_guid = EFI_TCO_RESET_PROTOCOL_GUID; |
| 65 | +static EFI_HANDLE handle; |
| 66 | + |
| 67 | +static EFIAPI EFI_STATUS tco_wdt_enable (UINT32 *timeout) |
| 68 | +{ |
| 69 | + UINT16 val = 0; |
| 70 | + UINT32 tmp = 0; |
| 71 | + |
| 72 | + if (NULL == timeout) |
| 73 | + return EFI_INVALID_PARAMETER; |
| 74 | + if (*timeout < TCO_MIN_TIMEOUT) |
| 75 | + *timeout = TCO_MIN_TIMEOUT; |
| 76 | + |
| 77 | + /* Halt TCO */ |
| 78 | + val = inw(TCO1_CNT); |
| 79 | + val |= TCO_HALT_BIT; |
| 80 | + outw(val, TCO1_CNT); |
| 81 | + val = inw(TCO1_CNT); |
| 82 | + |
| 83 | + /* Clear STS */ |
| 84 | + outw(TCO_TIMEOUT1_BIT, TCO1_STS); |
| 85 | + outw(TCO_TIMEOUT2_BIT, TCO2_STS); |
| 86 | + |
| 87 | + /* Set timeout */ |
| 88 | + val = inw(TCOv2_TMR); |
| 89 | + val = (val & 0xfc00) | (((*timeout * 10) / 6) & 0x3ff); |
| 90 | + outw((val & 0x3ff), TCOv2_TMR); |
| 91 | + val = inw(TCOv2_TMR); |
| 92 | + |
| 93 | + /* Clear NO_REBOOT bit */ |
| 94 | + tmp = read32((void *)(UINTN)PMC_GCR_PMC_CFG_REG); |
| 95 | + tmp &= ~ NO_REBOOT_BIT; |
| 96 | + write32((void *)(UINTN)PMC_GCR_PMC_CFG_REG, tmp); |
| 97 | + /*Read back and check */ |
| 98 | + tmp = read32((void *)(UINTN)PMC_GCR_PMC_CFG_REG); |
| 99 | + if (tmp & NO_REBOOT_BIT) |
| 100 | + return EFI_DEVICE_ERROR; |
| 101 | + |
| 102 | + /* Reload */ |
| 103 | + outw(0x1, TCO_RLD); |
| 104 | + |
| 105 | + /* Start TCO */ |
| 106 | + val = inw(TCO1_CNT); |
| 107 | + val &= ~ TCO_HALT_BIT; |
| 108 | + outw(val, TCO1_CNT); |
| 109 | + val = inw(TCO1_CNT); |
| 110 | + if (val & TCO_HALT_BIT) |
| 111 | + return EFI_DEVICE_ERROR; |
| 112 | + |
| 113 | + return EFI_SUCCESS; |
| 114 | +} |
| 115 | + |
| 116 | +static EFIAPI EFI_STATUS tco_wdt_disable (UINT32 timeout) |
| 117 | +{ |
| 118 | + UINT16 val = 0; |
| 119 | + UINT32 tmp = timeout; |
| 120 | + |
| 121 | + /* Halt TCO */ |
| 122 | + val = inw(TCO1_CNT); |
| 123 | + val |= TCO_HALT_BIT; |
| 124 | + outw(val, TCO1_CNT); |
| 125 | + val = inw(TCO1_CNT); |
| 126 | + if (!(val & TCO_HALT_BIT)) |
| 127 | + return EFI_DEVICE_ERROR; |
| 128 | + |
| 129 | + /* Set NO_REBOOT bit */ |
| 130 | + tmp = read32((void *)(UINTN)PMC_GCR_PMC_CFG_REG); |
| 131 | + tmp |= NO_REBOOT_BIT; |
| 132 | + write32((void *)(UINTN)PMC_GCR_PMC_CFG_REG, tmp); |
| 133 | + /*Read back and check */ |
| 134 | + tmp = read32((void *)(UINTN)PMC_GCR_PMC_CFG_REG); |
| 135 | + if (!(tmp & NO_REBOOT_BIT)) |
| 136 | + return EFI_DEVICE_ERROR; |
| 137 | + |
| 138 | + return EFI_SUCCESS; |
| 139 | +} |
| 140 | +static EFI_STATUS tco_wdt_init(EFI_SYSTEM_TABLE *st) |
| 141 | +{ |
| 142 | + static EFI_TCO_RESET_PROTOCOL tco_wdt_default = { |
| 143 | + .EnableTcoReset = tco_wdt_enable, |
| 144 | + .DisableTcoReset = tco_wdt_disable |
| 145 | + }; |
| 146 | + EFI_TCO_RESET_PROTOCOL *tco_wdt; |
| 147 | + |
| 148 | + return interface_init(st, &tco_wdt_guid, &handle, |
| 149 | + &tco_wdt_default, sizeof(tco_wdt_default), |
| 150 | + (void **)&tco_wdt); |
| 151 | +} |
| 152 | + |
| 153 | +static EFI_STATUS tco_wdt_exit(EFI_SYSTEM_TABLE *st) |
| 154 | +{ |
| 155 | + return interface_free(st, &tco_wdt_guid, handle); |
| 156 | +} |
| 157 | + |
| 158 | +ewdrv_t tco_wdt_drv = { |
| 159 | + .name = "tco_wdt", |
| 160 | + .description = "TCO watchdog Protocol", |
| 161 | + .init = tco_wdt_init, |
| 162 | + .exit = tco_wdt_exit |
| 163 | +}; |
0 commit comments