blob: 3117be9fb489940272fae173759c306a638f8706 [file] [log] [blame]
Sylvain Munautbc9f5c42020-09-14 10:22:29 +02001/*
2 * misc.c
3 *
4 * Copyright (C) 2019-2020 Sylvain Munaut <tnt@246tNt.com>
5 * SPDX-License-Identifier: GPL-3.0-or-later
6 */
7
8#include <stdbool.h>
9#include <stdint.h>
10
11#include "config.h"
12#include "misc.h"
Harald Welte52765672020-12-15 18:35:42 +010013#include "e1.h"
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020014
15
16struct misc {
17 uint32_t warmboot;
18 uint32_t gpio;
19 uint32_t e1_led;
20 uint32_t _rsvd;
21 struct {
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020022 uint16_t rx;
Sylvain Munautc1d117b2020-09-15 21:57:52 +020023 uint16_t tx;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020024 } e1_tick[2];
Sylvain Munaut1ac458f2020-09-15 22:06:00 +020025 struct {
26 uint32_t pps;
27 uint32_t now;
28 } time;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020029 uint32_t pdm[8];
30} __attribute__((packed,aligned(4)));
31
32static volatile struct misc * const misc_regs = (void*)(MISC_BASE);
33
34
35static const int pdm_bits[5] = { 12, 12, 8, 8, 8 };
36
37
38void
39pdm_set(int chan, bool enable, unsigned value, bool normalize)
40{
41 if (normalize)
42 value >>= (16 - pdm_bits[chan]);
43 if (enable)
44 value |= 0x80000000;
45 misc_regs->pdm[chan] = value;
46}
47
48
Sylvain Munaut5e860472020-09-15 22:20:21 +020049void
50e1_led_set(bool enable, uint8_t cfg)
51{
52 misc_regs->e1_led = (enable ? 0x100 : 0x000) | cfg;
53}
54
Harald Welte52765672020-12-15 18:35:42 +010055void
56e1_platform_led_set(uint8_t port, enum e1_platform_led led,
57 enum e1_platform_led_state state)
58{
59 uint32_t tmp;
60 unsigned int shift;
61
62 if (port >= 2)
63 return;
64
65 shift = 4*port + 2*led;
66
67 tmp = misc_regs->e1_led;
68 tmp &= ~(3 << shift);
69 tmp |= 0x100 | ((state & 3) << shift);
70 misc_regs->e1_led = tmp;
71}
72
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020073uint16_t
74e1_tick_read(void)
75{
76 return misc_regs->e1_tick[0].tx;
77}
Sylvain Munaut46d6b412020-10-29 13:19:05 +010078
79void
80reboot(int fw)
81{
82 misc_regs->warmboot = (1 << 2) | (fw << 0);
83}