Merge zizzer:/bk/newmem
[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 #include "sim/builder.hh"
47
48 using namespace std;
49 using namespace TheISA;
50
51 Uart8250::IntrEvent::IntrEvent(Uart8250 *u, int bit)
52 : Event(&mainEventQueue), uart(u)
53 {
54 DPRINTF(Uart, "UART Interrupt Event Initilizing\n");
55 intrBit = bit;
56 }
57
58 const char *
59 Uart8250::IntrEvent::description()
60 {
61 return "uart interrupt delay event";
62 }
63
64 void
65 Uart8250::IntrEvent::process()
66 {
67 if (intrBit & uart->IER) {
68 DPRINTF(Uart, "UART InterEvent, interrupting\n");
69 uart->platform->postConsoleInt();
70 uart->status |= intrBit;
71 uart->lastTxInt = curTick;
72 }
73 else
74 DPRINTF(Uart, "UART InterEvent, not interrupting\n");
75
76 }
77
78 /* The linux serial driver (8250.c about line 1182) loops reading from
79 * the device until the device reports it has no more data to
80 * read. After a maximum of 255 iterations the code prints "serial8250
81 * too much work for irq X," and breaks out of the loop. Since the
82 * simulated system is so much slower than the actual system, if a
83 * user is typing on the keyboard it is very easy for them to provide
84 * input at a fast enough rate to not allow the loop to exit and thus
85 * the error to be printed. This magic number provides a delay between
86 * the time the UART receives a character to send to the simulated
87 * system and the time it actually notifies the system it has a
88 * character to send to alleviate this problem. --Ali
89 */
90 void
91 Uart8250::IntrEvent::scheduleIntr()
92 {
93 static const Tick interval = (Tick)((Clock::Float::s / 2e9) * 450);
94 DPRINTF(Uart, "Scheduling IER interrupt for %#x, at cycle %lld\n", intrBit,
95 curTick + interval);
96 if (!scheduled())
97 schedule(curTick + interval);
98 else
99 reschedule(curTick + interval);
100 }
101
102
103 Uart8250::Uart8250(Params *p)
104 : Uart(p), IER(0), DLAB(0), LCR(0), MCR(0), lastTxInt(0),
105 txIntrEvent(this, TX_INT), rxIntrEvent(this, RX_INT)
106 {
107 pioSize = 8;
108
109 }
110
111 Tick
112 Uart8250::read(PacketPtr pkt)
113 {
114 assert(pkt->result == Packet::Unknown);
115 assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
116 assert(pkt->getSize() == 1);
117
118 Addr daddr = pkt->getAddr() - pioAddr;
119 pkt->allocate();
120
121 DPRINTF(Uart, " read register %#x\n", daddr);
122
123 switch (daddr) {
124 case 0x0:
125 if (!(LCR & 0x80)) { // read byte
126 if (cons->dataAvailable())
127 pkt->set(cons->in());
128 else {
129 pkt->set((uint8_t)0);
130 // A limited amount of these are ok.
131 DPRINTF(Uart, "empty read of RX register\n");
132 }
133 status &= ~RX_INT;
134 platform->clearConsoleInt();
135
136 if (cons->dataAvailable() && (IER & UART_IER_RDI))
137 rxIntrEvent.scheduleIntr();
138 } else { // dll divisor latch
139 ;
140 }
141 break;
142 case 0x1:
143 if (!(LCR & 0x80)) { // Intr Enable Register(IER)
144 pkt->set(IER);
145 } else { // DLM divisor latch MSB
146 ;
147 }
148 break;
149 case 0x2: // Intr Identification Register (IIR)
150 DPRINTF(Uart, "IIR Read, status = %#x\n", (uint32_t)status);
151
152 if (status & RX_INT) /* Rx data interrupt has a higher priority */
153 pkt->set(IIR_RXID);
154 else if (status & TX_INT) {
155 pkt->set(IIR_TXID);
156 //Tx interrupts are cleared on IIR reads
157 status &= ~TX_INT;
158 } else
159 pkt->set(IIR_NOPEND);
160
161 break;
162 case 0x3: // Line Control Register (LCR)
163 pkt->set(LCR);
164 break;
165 case 0x4: // Modem Control Register (MCR)
166 break;
167 case 0x5: // Line Status Register (LSR)
168 uint8_t lsr;
169 lsr = 0;
170 // check if there are any bytes to be read
171 if (cons->dataAvailable())
172 lsr = UART_LSR_DR;
173 lsr |= UART_LSR_TEMT | UART_LSR_THRE;
174 pkt->set(lsr);
175 break;
176 case 0x6: // Modem Status Register (MSR)
177 pkt->set((uint8_t)0);
178 break;
179 case 0x7: // Scratch Register (SCR)
180 pkt->set((uint8_t)0); // doesn't exist with at 8250.
181 break;
182 default:
183 panic("Tried to access a UART port that doesn't exist\n");
184 break;
185 }
186 /* uint32_t d32 = *data;
187 DPRINTF(Uart, "Register read to register %#x returned %#x\n", daddr, d32);
188 */
189 pkt->result = Packet::Success;
190 return pioDelay;
191 }
192
193 Tick
194 Uart8250::write(PacketPtr pkt)
195 {
196
197 assert(pkt->result == Packet::Unknown);
198 assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
199 assert(pkt->getSize() == 1);
200
201 Addr daddr = pkt->getAddr() - pioAddr;
202
203 DPRINTF(Uart, " write register %#x value %#x\n", daddr, pkt->get<uint8_t>());
204
205 switch (daddr) {
206 case 0x0:
207 if (!(LCR & 0x80)) { // write byte
208 cons->out(pkt->get<uint8_t>());
209 platform->clearConsoleInt();
210 status &= ~TX_INT;
211 if (UART_IER_THRI & IER)
212 txIntrEvent.scheduleIntr();
213 } else { // dll divisor latch
214 ;
215 }
216 break;
217 case 0x1:
218 if (!(LCR & 0x80)) { // Intr Enable Register(IER)
219 IER = pkt->get<uint8_t>();
220 if (UART_IER_THRI & IER)
221 {
222 DPRINTF(Uart, "IER: IER_THRI set, scheduling TX intrrupt\n");
223 if (curTick - lastTxInt >
224 (Tick)((Clock::Float::s / 2e9) * 450)) {
225 DPRINTF(Uart, "-- Interrupting Immediately... %d,%d\n",
226 curTick, lastTxInt);
227 txIntrEvent.process();
228 } else {
229 DPRINTF(Uart, "-- Delaying interrupt... %d,%d\n",
230 curTick, lastTxInt);
231 txIntrEvent.scheduleIntr();
232 }
233 }
234 else
235 {
236 DPRINTF(Uart, "IER: IER_THRI cleared, descheduling TX intrrupt\n");
237 if (txIntrEvent.scheduled())
238 txIntrEvent.deschedule();
239 if (status & TX_INT)
240 platform->clearConsoleInt();
241 status &= ~TX_INT;
242 }
243
244 if ((UART_IER_RDI & IER) && cons->dataAvailable()) {
245 DPRINTF(Uart, "IER: IER_RDI set, scheduling RX intrrupt\n");
246 rxIntrEvent.scheduleIntr();
247 } else {
248 DPRINTF(Uart, "IER: IER_RDI cleared, descheduling RX intrrupt\n");
249 if (rxIntrEvent.scheduled())
250 rxIntrEvent.deschedule();
251 if (status & RX_INT)
252 platform->clearConsoleInt();
253 status &= ~RX_INT;
254 }
255 } else { // DLM divisor latch MSB
256 ;
257 }
258 break;
259 case 0x2: // FIFO Control Register (FCR)
260 break;
261 case 0x3: // Line Control Register (LCR)
262 LCR = pkt->get<uint8_t>();
263 break;
264 case 0x4: // Modem Control Register (MCR)
265 if (pkt->get<uint8_t>() == (UART_MCR_LOOP | 0x0A))
266 MCR = 0x9A;
267 break;
268 case 0x7: // Scratch Register (SCR)
269 // We are emulating a 8250 so we don't have a scratch reg
270 break;
271 default:
272 panic("Tried to access a UART port that doesn't exist\n");
273 break;
274 }
275 pkt->result = Packet::Success;
276 return pioDelay;
277 }
278
279 void
280 Uart8250::dataAvailable()
281 {
282 // if the kernel wants an interrupt when we have data
283 if (IER & UART_IER_RDI)
284 {
285 platform->postConsoleInt();
286 status |= RX_INT;
287 }
288
289 }
290
291 void
292 Uart8250::addressRanges(AddrRangeList &range_list)
293 {
294 assert(pioSize != 0);
295 range_list.clear();
296 range_list.push_back(RangeSize(pioAddr, pioSize));
297 }
298
299
300
301 void
302 Uart8250::serialize(ostream &os)
303 {
304 SERIALIZE_SCALAR(status);
305 SERIALIZE_SCALAR(IER);
306 SERIALIZE_SCALAR(DLAB);
307 SERIALIZE_SCALAR(LCR);
308 SERIALIZE_SCALAR(MCR);
309 Tick rxintrwhen;
310 if (rxIntrEvent.scheduled())
311 rxintrwhen = rxIntrEvent.when();
312 else
313 rxintrwhen = 0;
314 Tick txintrwhen;
315 if (txIntrEvent.scheduled())
316 txintrwhen = txIntrEvent.when();
317 else
318 txintrwhen = 0;
319 SERIALIZE_SCALAR(rxintrwhen);
320 SERIALIZE_SCALAR(txintrwhen);
321 }
322
323 void
324 Uart8250::unserialize(Checkpoint *cp, const std::string &section)
325 {
326 UNSERIALIZE_SCALAR(status);
327 UNSERIALIZE_SCALAR(IER);
328 UNSERIALIZE_SCALAR(DLAB);
329 UNSERIALIZE_SCALAR(LCR);
330 UNSERIALIZE_SCALAR(MCR);
331 Tick rxintrwhen;
332 Tick txintrwhen;
333 UNSERIALIZE_SCALAR(rxintrwhen);
334 UNSERIALIZE_SCALAR(txintrwhen);
335 if (rxintrwhen != 0)
336 rxIntrEvent.schedule(rxintrwhen);
337 if (txintrwhen != 0)
338 txIntrEvent.schedule(txintrwhen);
339 }
340
341 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Uart8250)
342
343 Param<Addr> pio_addr;
344 Param<Tick> pio_latency;
345 SimObjectParam<Platform *> platform;
346 SimObjectParam<SimConsole *> sim_console;
347 SimObjectParam<System *> system;
348
349 END_DECLARE_SIM_OBJECT_PARAMS(Uart8250)
350
351 BEGIN_INIT_SIM_OBJECT_PARAMS(Uart8250)
352
353 INIT_PARAM(pio_addr, "Device Address"),
354 INIT_PARAM_DFLT(pio_latency, "Programmed IO latency", 1000),
355 INIT_PARAM(platform, "platform"),
356 INIT_PARAM(sim_console, "The Simulator Console"),
357 INIT_PARAM(system, "system object")
358
359 END_INIT_SIM_OBJECT_PARAMS(Uart8250)
360
361 CREATE_SIM_OBJECT(Uart8250)
362 {
363 Uart8250::Params *p = new Uart8250::Params;
364 p->name = getInstanceName();
365 p->pio_addr = pio_addr;
366 p->pio_delay = pio_latency;
367 p->platform = platform;
368 p->cons = sim_console;
369 p->system = system;
370 return new Uart8250(p);
371 }
372
373 REGISTER_SIM_OBJECT("Uart8250", Uart8250)
374