blob: 4fdd2f56a7504cc96a79648838bc23a2fee497e6 [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"
13
14
15struct misc {
16 uint32_t warmboot;
17 uint32_t gpio;
18 uint32_t e1_led;
19 uint32_t _rsvd;
20 struct {
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020021 uint16_t rx;
Sylvain Munautc1d117b2020-09-15 21:57:52 +020022 uint16_t tx;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020023 } e1_tick[2];
Sylvain Munaut1ac458f2020-09-15 22:06:00 +020024 struct {
25 uint32_t pps;
26 uint32_t now;
27 } time;
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020028 uint32_t pdm[8];
29} __attribute__((packed,aligned(4)));
30
31static volatile struct misc * const misc_regs = (void*)(MISC_BASE);
32
33
34static const int pdm_bits[5] = { 12, 12, 8, 8, 8 };
35
36
37void
38pdm_set(int chan, bool enable, unsigned value, bool normalize)
39{
40 if (normalize)
41 value >>= (16 - pdm_bits[chan]);
42 if (enable)
43 value |= 0x80000000;
44 misc_regs->pdm[chan] = value;
45}
46
47
Sylvain Munaut5e860472020-09-15 22:20:21 +020048void
49e1_led_set(bool enable, uint8_t cfg)
50{
51 misc_regs->e1_led = (enable ? 0x100 : 0x000) | cfg;
52}
53
Sylvain Munautbc9f5c42020-09-14 10:22:29 +020054uint16_t
55e1_tick_read(void)
56{
57 return misc_regs->e1_tick[0].tx;
58}