merge: style.py fix
[gem5.git] / src / dev / uart8250.cc
1 /*
2 * Copyright (c) 2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Ali Saidi
29 */
30
31 /** @file
32 * Implements a 8250 UART
33 */
34
35 #include <string>
36 #include <vector>
37
38 #include "base/inifile.hh"
39 #include "base/str.hh" // for to_number
40 #include "base/trace.hh"
41 #include "dev/simconsole.hh"
42 #include "dev/uart8250.hh"
43 #include "dev/platform.hh"
44 #include "mem/packet.hh"
45 #include "mem/packet_access.hh"
46
47 using namespace std;
48 using namespace TheISA;
49
50 Uart8250::IntrEvent::IntrEvent(Uart8250 *u, int bit)
51 : Event(&mainEventQueue), uart(u)
52 {
53 DPRINTF(Uart, "UART Interrupt Event Initilizing\n");
54 intrBit = bit;
55 }
56
57 const char *
58 Uart8250::IntrEvent::description()
59 {
60 return "uart interrupt delay";
61 }
62
63 void
64 Uart8250::IntrEvent::process()
65 {
66 if (intrBit & uart->IER) {
67 DPRINTF(Uart, "UART InterEvent, interrupting\n");
68 uart->platform->postConsoleInt();
69 uart->status |= intrBit;
70 uart->lastTxInt = curTick;
71 }
72 else
73 DPRINTF(Uart, "UART InterEvent, not interrupting\n");
74
75 }
76
77 /* The linux serial driver (8250.c about line 1182) loops reading from
78 * the device until the device reports it has no more data to
79 * read. After a maximum of 255 iterations the code prints "serial8250
80 * too much work for irq X," and breaks out of the loop. Since the
81 * simulated system is so much slower than the actual system, if a
82 * user is typing on the keyboard it is very easy for them to provide
83 * input at a fast enough rate to not allow the loop to exit and thus
84 * the error to be printed. This magic number provides a delay between
85 * the time the UART receives a character to send to the simulated
86 * system and the time it actually notifies the system it has a
87 * character to send to alleviate this problem. --Ali
88 */
89 void
90 Uart8250::IntrEvent::scheduleIntr()
91 {
92 static const Tick interval = (Tick)((Clock::Float::s / 2e9) * 450);
93 DPRINTF(Uart, "Scheduling IER interrupt for %#x, at cycle %lld\n", intrBit,
94 curTick + interval);
95 if (!scheduled())
96 schedule(curTick + interval);
97 else
98 reschedule(curTick + interval);
99 }
100
101
102 Uart8250::Uart8250(const Params *p)
103 : Uart(p), IER(0), DLAB(0), LCR(0), MCR(0), lastTxInt(0),
104 txIntrEvent(this, TX_INT), rxIntrEvent(this, RX_INT)
105 {
106 pioSize = 8;
107 }
108
109 Tick
110 Uart8250::read(PacketPtr pkt)
111 {
112 assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
113 assert(pkt->getSize() == 1);
114
115 Addr daddr = pkt->getAddr() - pioAddr;
116 pkt->allocate();
117
118 DPRINTF(Uart, " read register %#x\n", daddr);
119
120 switch (daddr) {
121 case 0x0:
122 if (!(LCR & 0x80)) { // read byte
123 if (cons->dataAvailable())
124 pkt->set(cons->in());
125 else {
126 pkt->set((uint8_t)0);
127 // A limited amount of these are ok.
128 DPRINTF(Uart, "empty read of RX register\n");
129 }
130 status &= ~RX_INT;
131 platform->clearConsoleInt();
132
133 if (cons->dataAvailable() && (IER & UART_IER_RDI))
134 rxIntrEvent.scheduleIntr();
135 } else { // dll divisor latch
136 ;
137 }
138 break;
139 case 0x1:
140 if (!(LCR & 0x80)) { // Intr Enable Register(IER)
141 pkt->set(IER);
142 } else { // DLM divisor latch MSB
143 ;
144 }
145 break;
146 case 0x2: // Intr Identification Register (IIR)
147 DPRINTF(Uart, "IIR Read, status = %#x\n", (uint32_t)status);
148
149 if (status & RX_INT) /* Rx data interrupt has a higher priority */
150 pkt->set(IIR_RXID);
151 else if (status & TX_INT) {
152 pkt->set(IIR_TXID);
153 //Tx interrupts are cleared on IIR reads
154 status &= ~TX_INT;
155 } else
156 pkt->set(IIR_NOPEND);
157
158 break;
159 case 0x3: // Line Control Register (LCR)
160 pkt->set(LCR);
161 break;
162 case 0x4: // Modem Control Register (MCR)
163 break;
164 case 0x5: // Line Status Register (LSR)
165 uint8_t lsr;
166 lsr = 0;
167 // check if there are any bytes to be read
168 if (cons->dataAvailable())
169 lsr = UART_LSR_DR;
170 lsr |= UART_LSR_TEMT | UART_LSR_THRE;
171 pkt->set(lsr);
172 break;
173 case 0x6: // Modem Status Register (MSR)
174 pkt->set((uint8_t)0);
175 break;
176 case 0x7: // Scratch Register (SCR)
177 pkt->set((uint8_t)0); // doesn't exist with at 8250.
178 break;
179 default:
180 panic("Tried to access a UART port that doesn't exist\n");
181 break;
182 }
183 /* uint32_t d32 = *data;
184 DPRINTF(Uart, "Register read to register %#x returned %#x\n", daddr, d32);
185 */
186 pkt->makeAtomicResponse();
187 return pioDelay;
188 }
189
190 Tick
191 Uart8250::write(PacketPtr pkt)
192 {
193
194 assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
195 assert(pkt->getSize() == 1);
196
197 Addr daddr = pkt->getAddr() - pioAddr;
198
199 DPRINTF(Uart, " write register %#x value %#x\n", daddr, pkt->get<uint8_t>());
200
201 switch (daddr) {
202 case 0x0:
203 if (!(LCR & 0x80)) { // write byte
204 cons->out(pkt->get<uint8_t>());
205 platform->clearConsoleInt();
206 status &= ~TX_INT;
207 if (UART_IER_THRI & IER)
208 txIntrEvent.scheduleIntr();
209 } else { // dll divisor latch
210 ;
211 }
212 break;
213 case 0x1:
214 if (!(LCR & 0x80)) { // Intr Enable Register(IER)
215 IER = pkt->get<uint8_t>();
216 if (UART_IER_THRI & IER)
217 {
218 DPRINTF(Uart, "IER: IER_THRI set, scheduling TX intrrupt\n");
219 if (curTick - lastTxInt >
220 (Tick)((Clock::Float::s / 2e9) * 450)) {
221 DPRINTF(Uart, "-- Interrupting Immediately... %d,%d\n",
222 curTick, lastTxInt);
223 txIntrEvent.process();
224 } else {
225 DPRINTF(Uart, "-- Delaying interrupt... %d,%d\n",
226 curTick, lastTxInt);
227 txIntrEvent.scheduleIntr();
228 }
229 }
230 else
231 {
232 DPRINTF(Uart, "IER: IER_THRI cleared, descheduling TX intrrupt\n");
233 if (txIntrEvent.scheduled())
234 txIntrEvent.deschedule();
235 if (status & TX_INT)
236 platform->clearConsoleInt();
237 status &= ~TX_INT;
238 }
239
240 if ((UART_IER_RDI & IER) && cons->dataAvailable()) {
241 DPRINTF(Uart, "IER: IER_RDI set, scheduling RX intrrupt\n");
242 rxIntrEvent.scheduleIntr();
243 } else {
244 DPRINTF(Uart, "IER: IER_RDI cleared, descheduling RX intrrupt\n");
245 if (rxIntrEvent.scheduled())
246 rxIntrEvent.deschedule();
247 if (status & RX_INT)
248 platform->clearConsoleInt();
249 status &= ~RX_INT;
250 }
251 } else { // DLM divisor latch MSB
252 ;
253 }
254 break;
255 case 0x2: // FIFO Control Register (FCR)
256 break;
257 case 0x3: // Line Control Register (LCR)
258 LCR = pkt->get<uint8_t>();
259 break;
260 case 0x4: // Modem Control Register (MCR)
261 if (pkt->get<uint8_t>() == (UART_MCR_LOOP | 0x0A))
262 MCR = 0x9A;
263 break;
264 case 0x7: // Scratch Register (SCR)
265 // We are emulating a 8250 so we don't have a scratch reg
266 break;
267 default:
268 panic("Tried to access a UART port that doesn't exist\n");
269 break;
270 }
271 pkt->makeAtomicResponse();
272 return pioDelay;
273 }
274
275 void
276 Uart8250::dataAvailable()
277 {
278 // if the kernel wants an interrupt when we have data
279 if (IER & UART_IER_RDI)
280 {
281 platform->postConsoleInt();
282 status |= RX_INT;
283 }
284
285 }
286
287 void
288 Uart8250::addressRanges(AddrRangeList &range_list)
289 {
290 assert(pioSize != 0);
291 range_list.clear();
292 range_list.push_back(RangeSize(pioAddr, pioSize));
293 }
294
295
296
297 void
298 Uart8250::serialize(ostream &os)
299 {
300 SERIALIZE_SCALAR(status);
301 SERIALIZE_SCALAR(IER);
302 SERIALIZE_SCALAR(DLAB);
303 SERIALIZE_SCALAR(LCR);
304 SERIALIZE_SCALAR(MCR);
305 Tick rxintrwhen;
306 if (rxIntrEvent.scheduled())
307 rxintrwhen = rxIntrEvent.when();
308 else
309 rxintrwhen = 0;
310 Tick txintrwhen;
311 if (txIntrEvent.scheduled())
312 txintrwhen = txIntrEvent.when();
313 else
314 txintrwhen = 0;
315 SERIALIZE_SCALAR(rxintrwhen);
316 SERIALIZE_SCALAR(txintrwhen);
317 }
318
319 void
320 Uart8250::unserialize(Checkpoint *cp, const std::string &section)
321 {
322 UNSERIALIZE_SCALAR(status);
323 UNSERIALIZE_SCALAR(IER);
324 UNSERIALIZE_SCALAR(DLAB);
325 UNSERIALIZE_SCALAR(LCR);
326 UNSERIALIZE_SCALAR(MCR);
327 Tick rxintrwhen;
328 Tick txintrwhen;
329 UNSERIALIZE_SCALAR(rxintrwhen);
330 UNSERIALIZE_SCALAR(txintrwhen);
331 if (rxintrwhen != 0)
332 rxIntrEvent.schedule(rxintrwhen);
333 if (txintrwhen != 0)
334 txIntrEvent.schedule(txintrwhen);
335 }
336
337 Uart8250 *
338 Uart8250Params::create()
339 {
340 return new Uart8250(this);
341 }