1 //
2 // Copyright (c) 2010-2018 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System;
9 using Antmicro.Renode.Peripherals.Bus;
10 using Antmicro.Renode.Peripherals.Bus.Wrappers;
11 using Antmicro.Renode.Logging;
12 using Antmicro.Renode.Core;
13 
14 namespace Antmicro.Renode.Peripherals.UART
15 {
16     [AllowedTranslations(AllowedTranslation.ByteToDoubleWord)]
17     public sealed class MPC5567_UART : UARTBase, IDoubleWordPeripheral, IWordPeripheral, IKnownSize
18     {
MPC5567_UART(IMachine machine)19         public MPC5567_UART(IMachine machine) : base(machine)
20         {
21             Reset();
22         }
23 
ReadDoubleWord(long offset)24         public uint ReadDoubleWord(long offset)
25         {
26             switch((LongRegister)offset)
27             {
28             case LongRegister.Control1:
29                 return controlRegister1;
30             case LongRegister.Status:
31                 var status = (uint)Status.TransmitDataRegisterEmpty | (uint)Status.TransmitComplete;
32                 if(Count > 0)
33                 {
34                     status |= (uint)Status.ReceiveDataRegisterFull;
35                 }
36                 return status;
37             case LongRegister.LINControl:
38                 break;
39             case LongRegister.LINTransmit:
40                 break;
41             case LongRegister.LINReceive:
42                 break;
43             case LongRegister.LINCRCPolynomial:
44                 break;
45             default:
46                 this.LogUnhandledRead(offset);
47                 break;
48             }
49             return 0;
50         }
51 
WriteDoubleWord(long offset, uint value)52         public void WriteDoubleWord(long offset, uint value)
53         {
54             switch((LongRegister)offset)
55             {
56             case LongRegister.Control1:
57                 controlRegister1 = value;
58                 break;
59             case LongRegister.Status:
60                 break;
61             case LongRegister.LINControl:
62                 break;
63             case LongRegister.LINTransmit:
64                 break;
65             case LongRegister.LINReceive:
66                 break;
67             case LongRegister.LINCRCPolynomial:
68                 break;
69             default:
70                 this.LogUnhandledWrite(offset, value);
71                 break;
72             }
73         }
74 
ReadWord(long offset)75         public ushort ReadWord(long offset)
76         {
77             switch((ShortRegister)offset)
78             {
79             case ShortRegister.Control2:
80                 return controlRegister2;
81             case ShortRegister.Data:
82                 byte result;
83                 TryGetCharacter(out result);
84                 return result;
85             default:
86                 this.LogUnhandledRead(offset);
87                 break;
88             }
89             return 0;
90         }
91 
WriteWord(long offset, ushort value)92         public void WriteWord(long offset, ushort value)
93         {
94             switch((ShortRegister)offset)
95             {
96             case ShortRegister.Control2:
97                 controlRegister2 = value;
98                 break;
99             case ShortRegister.Data:
100                 TransmitCharacter((byte)value);
101                 break;
102             default:
103                 this.LogUnhandledWrite(offset, value);
104                 break;
105             }
106         }
107 
Reset()108         public new void Reset()
109         {
110             base.Reset();
111             controlRegister1 = 1 << 18; //default baud rate divider
112             controlRegister2 = 1 << StopDMAOnError;
113         }
114 
115         public override Bits StopBits
116         {
117             get
118             {
119                 throw new NotImplementedException();
120             }
121         }
122 
123         public override Parity ParityBit
124         {
125             get
126             {
127                 throw new NotImplementedException();
128             }
129         }
130 
131         public override uint BaudRate
132         {
133             get
134             {
135                 throw new NotImplementedException();
136             }
137         }
138 
139         public long Size
140         {
141             get
142             {
143                 return 0x4000;
144             }
145         }
146 
CharWritten()147         protected override void CharWritten()
148         {
149 
150         }
151 
QueueEmptied()152         protected override void QueueEmptied()
153         {
154 
155         }
156 
157         private uint controlRegister1;
158         private ushort controlRegister2;
159 
160         private const int StopDMAOnError = 13;
161 
162         [RegisterMapper.RegistersDescription]
163         private enum LongRegister
164         {
165             Control1 = 0x0,
166             Status = 0x8,
167             LINControl = 0xC,
168             LINTransmit = 0x10,
169             LINReceive = 0x14,
170             LINCRCPolynomial = 0x18
171         }
172 
173         [RegisterMapper.RegistersDescription]
174         private enum ShortRegister
175         {
176             Control2 = 0x4,
177             Data = 0x6
178         }
179 
180         [Flags]
181         private enum Status : uint
182         {
183             LINFrameComplete = 1u << 8,
184             ChecksumError = 1u << 9,
185             CRCError = 1u << 10,
186             ReceiveDataRegisterFull = 1u << 29,
187             TransmitComplete = 1u << 30,
188             TransmitDataRegisterEmpty = 1u << 31
189         }
190 
191     }
192 }
193 
194