aboutsummaryrefslogtreecommitdiffstats
path: root/common/mentor-swupdate/recipes-bsp/grub/amd-wdt/amd_wdt.c
blob: 9d2e83172b19c346d4fc8108dda0111b2af85978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* amd_wdt.c - AMD Watchdog Driver and API for grub cfg & console */
/* 
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2019 Mentor Graphics, a Siemens business
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Author: Arsalan H. Awan <ArsalanHAwan@github>
 *  Email:  Arsalan_Awan@mentor.com
 */

#include <grub/types.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/pci.h>
#include <grub/err.h>
#include <grub/dl.h>
#include <grub/extcmd.h>
#include <grub/i18n.h>
#include "amd_wdt.h"

#define PCI_VENDOR_ID_AMD		0x1022

//#define AMD_WDT_DEBUG

GRUB_MOD_LICENSE ("GPLv3+");

static struct
{
  grub_uint8_t *ptr;
  grub_uint8_t fired;
  int mapped;
  grub_uint32_t base;
  grub_pci_device_t dev;
} watchdog;

static int found = 0;

static void
writel (grub_uint32_t val, grub_uint8_t reg)
{
  watchdog.ptr[reg] = val & 0xff;
  watchdog.ptr[reg + 1] = (val >> 8) & 0xff;
  watchdog.ptr[reg + 2] = (val >> 16) & 0xff;
  watchdog.ptr[reg + 3] = (val >> 24) & 0xff;
}

static grub_uint32_t
readl (grub_uint8_t reg)
{
  return watchdog.ptr[reg] +
         (watchdog.ptr[reg + 1] << 8) +
         (watchdog.ptr[reg + 2] << 16) +
         (watchdog.ptr[reg + 3] << 24);
}

static void
amd_wdt_start (void)
{
  grub_uint32_t val;
  /* Start the watchdog timer */
  val = readl (AMD_WDT_CONTROL(0));
  val |= AMD_WDT_START_STOP_BIT;
  writel (val, AMD_WDT_CONTROL(0));
}

static void
amd_wdt_stop (void)
{
  grub_uint32_t val;
  /* Stop the watchdog timer */
  val = readl (AMD_WDT_CONTROL(0));
  val &= ~AMD_WDT_START_STOP_BIT;
  writel (val, AMD_WDT_CONTROL(0));
}

static void
amd_wdt_ping (void)
{
  grub_uint32_t val;
  /* Trigger/Ping watchdog timer */
  val = readl (AMD_WDT_CONTROL(0));
  val |= AMD_WDT_TRIGGER_BIT;
  writel (val, AMD_WDT_CONTROL(0));
}

static grub_uint16_t
amd_wdt_get_time (void)
{
  /* Read watchdog COUNT register */
  return readl (AMD_WDT_COUNT(0)) & AMD_WDT_COUNT_MASK;
}

static void
amd_wdt_set_time (grub_uint32_t t)
{
  if (t < AMD_WDT_MIN_TIMEOUT)
    t = AMD_WDT_MIN_TIMEOUT;
  else if (t > AMD_WDT_MAX_TIMEOUT)
    t = AMD_WDT_MAX_TIMEOUT;

  /* Write new timeout value to watchdog COUNT register */
  writel (t, AMD_WDT_COUNT(0));
}

static void
amd_wdt_enable (void)
{
  grub_uint8_t val;
  /* Enable watchdog timer */
  grub_outb(AMD_PM_WATCHDOG_EN_REG, AMD_IO_PM_INDEX_REG);
  val = grub_inb(AMD_IO_PM_DATA_REG);
  val |= AMD_PM_WATCHDOG_TIMER_EN;
  grub_outb(val, AMD_IO_PM_DATA_REG);
}

static void
amd_wdt_disable (void)
{
  grub_uint8_t val;
  /* Disable watchdog timer */
  grub_outb(AMD_PM_WATCHDOG_EN_REG, AMD_IO_PM_INDEX_REG);
  val = grub_inb(AMD_IO_PM_DATA_REG);
  val &= ~AMD_PM_WATCHDOG_TIMER_EN;
  grub_outb(val, AMD_IO_PM_DATA_REG);
}

static void
amd_wdt_set_resolution (grub_uint8_t freq)
{
  grub_uint8_t val;
  /* Set the watchdog timer resolution */
  grub_outb(AMD_PM_WATCHDOG_CONFIG_REG, AMD_IO_PM_INDEX_REG);
  val = grub_inb(AMD_IO_PM_DATA_REG);
  /* Clear the previous frequency setting, if any */
  val &= ~AMD_PM_WATCHDOG_CONFIG_MASK;
  /* Set the new frequency value */
  val |= freq;
  grub_outb(val, AMD_IO_PM_DATA_REG);
}

static void
amd_wdt_set_timeout_action (const char * action)
{
  grub_uint32_t val;
  val = readl (AMD_WDT_CONTROL(0));

  /*
  * Set the watchdog timeout action.
  *
  * If action is specified anything other than reboot or shutdown,
  * we default it to reboot.
  */
  if (grub_strncmp(action, "shutdown", 8) == 0)
    val |= AMD_WDT_ACTION_RESET_BIT;
  else
    val &= ~AMD_WDT_ACTION_RESET_BIT;
}

static grub_uint8_t
amd_wdt_check_fired (void)
{
  grub_uint32_t val;
  /* Read watchdog fired bit */
  val = readl (AMD_WDT_CONTROL(0));
  return val & AMD_WDT_FIRED_BIT;
}

static grub_err_t
grub_cmd_amd_wdt (grub_extcmd_context_t ctxt  __attribute__ ((unused)),
                      int argc, char **args)
{
  if (argc < 1)
    return grub_error (GRUB_ERR_BAD_ARGUMENT,
      N_("usage: amd-wdt <command>\ncommands: enable disable start stop ping getstatus gettime settime"));

  if (grub_strcasecmp (args[0], "enable") == 0)
    amd_wdt_enable ();
  else if (grub_strcasecmp (args[0], "disable") == 0)
    amd_wdt_disable ();
  else if (grub_strcasecmp (args[0], "start") == 0)
  {
    amd_wdt_start ();
    amd_wdt_ping ();
  }
  else if (grub_strcasecmp (args[0], "stop") == 0)
    amd_wdt_stop ();
  else if (grub_strcasecmp (args[0], "ping") == 0)
    amd_wdt_ping ();
  else if (grub_strcasecmp (args[0], "getstatus") == 0)
  {
    grub_printf ("%d", watchdog.fired);
    return watchdog.fired;
  }
  else if (grub_strcasecmp (args[0], "gettime") == 0)
    grub_printf ("%d", amd_wdt_get_time ());
  else if (grub_strcasecmp (args[0], "settime") == 0)
  {
    amd_wdt_set_time (grub_strtol(args[1], 0, 0));
    amd_wdt_start ();
    amd_wdt_ping ();
    amd_wdt_stop ();
  }
  else
    return grub_error (GRUB_ERR_BAD_ARGUMENT,
                       N_("error: unknown command"));

  return GRUB_ERR_NONE;
}

/* Helper for finding AMD WDT on the PCI bus */
static int
find_wdt (grub_pci_device_t dev, 
          grub_pci_id_t pciid __attribute__ ((unused)),
          void *data __attribute__ ((unused)))
{
  grub_pci_address_t addr;
  grub_uint32_t device_id;
  grub_uint32_t vendor_id;

#ifdef AMD_WDT_DEBUG
  grub_printf ("bus=%d, device=%d, function=%d\n", dev.bus, dev.device, dev.function);
#endif

  addr = grub_pci_make_address (dev, GRUB_PCI_REG_DEVICE);
  device_id = grub_pci_read_word (addr);

#ifdef AMD_WDT_DEBUG
  grub_printf ("addr GRUB_PCI_REG_DEVICE = 0x%x\n", addr);
#endif

  addr = grub_pci_make_address (dev, GRUB_PCI_REG_VENDOR);
  vendor_id = grub_pci_read_word (addr);

#ifdef AMD_WDT_DEBUG
  grub_printf ("addr GRUB_PCI_REG_VENDOR = 0x%x\n", addr);

  grub_printf ("PCI_DEVICE_ID = 0x%x | 0x%x, PCI_VENDOR_ID = 0x%x | 0x%x\n", device_id, PCI_DEVICE_ID_AMD_CARRIZO_SMBUS, vendor_id, PCI_VENDOR_ID_AMD);
#endif

  if (device_id == PCI_DEVICE_ID_AMD_CARRIZO_SMBUS && vendor_id == PCI_VENDOR_ID_AMD)
  {
    watchdog.base = AMD_ACPI_MMIO_BASE + AMD_WDT_MEM_MAP_OFFSET;

#ifdef AMD_WDT_DEBUG
    grub_printf ("watchdog.base = 0x%x\n", watchdog.base);

    for (grub_uint16_t i=0; i<0x100; i+=4)
    {   
      addr = grub_pci_make_address (dev, i); 
      grub_printf ("0x%x: 0x%x\n", i, grub_pci_read (addr));
    }
#endif

    watchdog.dev = dev;
    found = 1;
    return 1;
  }
  return 0;
}

static grub_err_t
amd_wdt_init (void)
{
  grub_pci_iterate (find_wdt, NULL);
  if (!found)
    return grub_error (GRUB_ERR_IO, "Couldn't find watchdog timer");

  watchdog.ptr = (void *) grub_pci_device_map_range (watchdog.dev,
                                                     watchdog.base,
                                                     AMD_WDT_MEM_MAP_SIZE);

  watchdog.mapped = 1;

#ifdef AMD_WDT_DEBUG
  grub_printf ("watchdog.ptr = %p\n", watchdog.ptr);

  for (grub_uint16_t i=0; i<AMD_WDT_MEM_MAP_SIZE; i++)
  {
    grub_outb(i, AMD_IO_PM_INDEX_REG);
    grub_printf ("watchdog reg[%d] = 0x%x\n", i, grub_inb(AMD_IO_PM_DATA_REG));
  }
#endif

  watchdog.fired = amd_wdt_check_fired ();

  amd_wdt_enable ();
  amd_wdt_set_resolution (AMD_PM_WATCHDOG_1SEC_RES);
  amd_wdt_set_timeout_action (_("reboot"));
  amd_wdt_stop ();
  amd_wdt_set_time (AMD_WDT_DEFAULT_TIMEOUT);

  grub_printf ("AMD Watchdog setup complete!\n");

  return GRUB_ERR_NONE;
}

static grub_err_t
amd_wdt_fini (void)
{
  if (watchdog.mapped)
      grub_pci_device_unmap_range (watchdog.dev,
                                   watchdog.ptr,
                                   AMD_WDT_MEM_MAP_SIZE);

  return GRUB_ERR_NONE;
}

static grub_extcmd_t cmd;

GRUB_MOD_INIT(amd-wdt)
{
  amd_wdt_init ();

  if (watchdog.mapped)
  {
    grub_printf ("watchdog reboot %sdetected\n", watchdog.fired ? "" : "not ");

    cmd = grub_register_extcmd ("amd-wdt", grub_cmd_amd_wdt, 0, 0,
                                N_("AMD Watchdog Timer"), 0);
  }
}

GRUB_MOD_FINI(amd-wdt)
{
  amd_wdt_fini ();

  if (watchdog.mapped)
    grub_unregister_extcmd (cmd);
}