1 //
2 // Copyright (c) 2010-2024 Antmicro
3 //
4 //  This file is licensed under the MIT License.
5 //  Full license text is available in 'licenses/MIT.txt'.
6 //
7 
8 using System;
9 using System.Collections.Generic;
10 using Antmicro.Renode.Core;
11 using Antmicro.Renode.Core.Structure.Registers;
12 using Antmicro.Renode.Logging;
13 using Antmicro.Renode.Peripherals.Bus;
14 using Antmicro.Renode.Peripherals.GPIOPort;
15 using Antmicro.Renode.Peripherals.Timers;
16 
17 namespace Antmicro.Renode.Peripherals.Miscellaneous
18 {
19     public sealed class NRF_USBREG : BasicDoubleWordPeripheral, IKnownSize
20     {
NRF_USBREG(IMachine machine)21         public NRF_USBREG(IMachine machine) : base(machine)
22         {
23             IRQ = new GPIO();
24             DefineRegisters();
25         }
26 
Reset()27         public override void Reset()
28         {
29             base.Reset();
30             IRQ.Set(false);
31         }
32 
33         public long Size => 0x1000;
34 
35         public GPIO IRQ { get; }
36 
SetInterrupt(bool irq)37         private void SetInterrupt(bool irq)
38         {
39             this.Log(LogLevel.Noisy, "Setting IRQ: {0}", irq);
40             IRQ.Set(irq);
41         }
42 
DefineRegisters()43         private void DefineRegisters()
44         {
45             // This is hacky way of setting interrupts, but it's only needed
46             // for usb pullup for nRF5340, which works with this
47             Registers.EventsUsbDetected.Define(this)
48                 .WithFlag(0,
49                     valueProviderCallback: _ => true,
50                     writeCallback: (_, __) => SetInterrupt(false),
51                     name: "USBDETECTED")
52                 .WithReservedBits(1, 31);
53 
54             Registers.EventsUsbPowerReady.Define(this)
55                 .WithFlag(0,
56                     valueProviderCallback: _ => true,
57                     writeCallback: (_, __) => SetInterrupt(false),
58                     name: "USBPWRRDY")
59                 .WithReservedBits(1, 31);
60 
61             Registers.InterruptSet.Define(this)
62                 .WithTaggedFlag(name: "USBDETECTED", 0)
63                 .WithTaggedFlag(name: "USBREMOVED", 1)
64                 .WithFlag(2,
65                     valueProviderCallback: _ => true,
66                     writeCallback: (_, __) => SetInterrupt(true),
67                     name: "USBPWRRDY")
68                 .WithReservedBits(3, 29);
69 
70             Registers.UsbRegisterStatus.Define(this)
71                 .WithFlag(0,
72                     FieldMode.Read,
73                     valueProviderCallback: _ => true,
74                     name: "VBUSDETECT")
75                 .WithFlag(1,
76                     FieldMode.Read,
77                     valueProviderCallback: _ => true,
78                     name: "OUTPUTRDY")
79                 .WithReservedBits(2, 30);
80         }
81 
82         private enum Registers
83         {
84             EventsUsbDetected = 0x100,
85             EventsUsbPowerReady = 0x108,
86             InterruptSet = 0x304,
87             UsbRegisterStatus = 0x400,
88         }
89     }
90 }
91