2 * Copyright (c) 2005 The Regents of The University of Michigan
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.
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.
32 * Implements a 8250 UART
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"
49 using namespace TheISA
;
51 Uart8250::IntrEvent::IntrEvent(Uart8250
*u
, int bit
)
52 : Event(&mainEventQueue
), uart(u
)
54 DPRINTF(Uart
, "UART Interrupt Event Initilizing\n");
59 Uart8250::IntrEvent::description()
61 return "uart interrupt delay event";
65 Uart8250::IntrEvent::process()
67 if (intrBit
& uart
->IER
) {
68 DPRINTF(Uart
, "UART InterEvent, interrupting\n");
69 uart
->platform
->postConsoleInt();
70 uart
->status
|= intrBit
;
73 DPRINTF(Uart
, "UART InterEvent, not interrupting\n");
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
90 Uart8250::IntrEvent::scheduleIntr()
92 static const Tick interval
= (Tick
)((Clock::Float::s
/ 2e9
) * 450);
93 DPRINTF(Uart
, "Scheduling IER interrupt for %#x, at cycle %lld\n", intrBit
,
96 schedule(curTick
+ interval
);
98 reschedule(curTick
+ interval
);
102 Uart8250::Uart8250(Params
*p
)
103 : Uart(p
), txIntrEvent(this, TX_INT
), rxIntrEvent(this, RX_INT
)
114 Uart8250::read(PacketPtr pkt
)
116 assert(pkt
->result
== Packet::Unknown
);
117 assert(pkt
->getAddr() >= pioAddr
&& pkt
->getAddr() < pioAddr
+ pioSize
);
118 assert(pkt
->getSize() == 1);
120 Addr daddr
= pkt
->getAddr() - pioAddr
;
123 DPRINTF(Uart
, " read register %#x\n", daddr
);
127 if (!(LCR
& 0x80)) { // read byte
128 if (cons
->dataAvailable())
129 pkt
->set(cons
->in());
131 pkt
->set((uint8_t)0);
132 // A limited amount of these are ok.
133 DPRINTF(Uart
, "empty read of RX register\n");
136 platform
->clearConsoleInt();
138 if (cons
->dataAvailable() && (IER
& UART_IER_RDI
))
139 rxIntrEvent
.scheduleIntr();
140 } else { // dll divisor latch
145 if (!(LCR
& 0x80)) { // Intr Enable Register(IER)
147 } else { // DLM divisor latch MSB
151 case 0x2: // Intr Identification Register (IIR)
152 DPRINTF(Uart
, "IIR Read, status = %#x\n", (uint32_t)status
);
154 if (status
& RX_INT
) /* Rx data interrupt has a higher priority */
156 else if (status
& TX_INT
)
159 pkt
->set(IIR_NOPEND
);
161 //Tx interrupts are cleared on IIR reads
164 case 0x3: // Line Control Register (LCR)
167 case 0x4: // Modem Control Register (MCR)
169 case 0x5: // Line Status Register (LSR)
172 // check if there are any bytes to be read
173 if (cons
->dataAvailable())
175 lsr
|= UART_LSR_TEMT
| UART_LSR_THRE
;
178 case 0x6: // Modem Status Register (MSR)
179 pkt
->set((uint8_t)0);
181 case 0x7: // Scratch Register (SCR)
182 pkt
->set((uint8_t)0); // doesn't exist with at 8250.
185 panic("Tried to access a UART port that doesn't exist\n");
188 /* uint32_t d32 = *data;
189 DPRINTF(Uart, "Register read to register %#x returned %#x\n", daddr, d32);
191 pkt
->result
= Packet::Success
;
196 Uart8250::write(PacketPtr pkt
)
199 assert(pkt
->result
== Packet::Unknown
);
200 assert(pkt
->getAddr() >= pioAddr
&& pkt
->getAddr() < pioAddr
+ pioSize
);
201 assert(pkt
->getSize() == 1);
203 Addr daddr
= pkt
->getAddr() - pioAddr
;
205 DPRINTF(Uart
, " write register %#x value %#x\n", daddr
, pkt
->get
<uint8_t>());
209 if (!(LCR
& 0x80)) { // write byte
210 cons
->out(pkt
->get
<uint8_t>());
211 platform
->clearConsoleInt();
213 if (UART_IER_THRI
& IER
)
214 txIntrEvent
.scheduleIntr();
215 } else { // dll divisor latch
220 if (!(LCR
& 0x80)) { // Intr Enable Register(IER)
221 IER
= pkt
->get
<uint8_t>();
222 if (UART_IER_THRI
& IER
)
224 DPRINTF(Uart
, "IER: IER_THRI set, scheduling TX intrrupt\n");
225 txIntrEvent
.scheduleIntr();
229 DPRINTF(Uart
, "IER: IER_THRI cleared, descheduling TX intrrupt\n");
230 if (txIntrEvent
.scheduled())
231 txIntrEvent
.deschedule();
233 platform
->clearConsoleInt();
237 if ((UART_IER_RDI
& IER
) && cons
->dataAvailable()) {
238 DPRINTF(Uart
, "IER: IER_RDI set, scheduling RX intrrupt\n");
239 rxIntrEvent
.scheduleIntr();
241 DPRINTF(Uart
, "IER: IER_RDI cleared, descheduling RX intrrupt\n");
242 if (rxIntrEvent
.scheduled())
243 rxIntrEvent
.deschedule();
245 platform
->clearConsoleInt();
248 } else { // DLM divisor latch MSB
252 case 0x2: // FIFO Control Register (FCR)
254 case 0x3: // Line Control Register (LCR)
255 LCR
= pkt
->get
<uint8_t>();
257 case 0x4: // Modem Control Register (MCR)
258 if (pkt
->get
<uint8_t>() == (UART_MCR_LOOP
| 0x0A))
261 case 0x7: // Scratch Register (SCR)
262 // We are emulating a 8250 so we don't have a scratch reg
265 panic("Tried to access a UART port that doesn't exist\n");
268 pkt
->result
= Packet::Success
;
273 Uart8250::dataAvailable()
275 // if the kernel wants an interrupt when we have data
276 if (IER
& UART_IER_RDI
)
278 platform
->postConsoleInt();
285 Uart8250::addressRanges(AddrRangeList
&range_list
)
287 assert(pioSize
!= 0);
289 range_list
.push_back(RangeSize(pioAddr
, pioSize
));
295 Uart8250::serialize(ostream
&os
)
297 SERIALIZE_SCALAR(status
);
298 SERIALIZE_SCALAR(IER
);
299 SERIALIZE_SCALAR(DLAB
);
300 SERIALIZE_SCALAR(LCR
);
301 SERIALIZE_SCALAR(MCR
);
303 if (rxIntrEvent
.scheduled())
304 rxintrwhen
= rxIntrEvent
.when();
308 if (txIntrEvent
.scheduled())
309 txintrwhen
= txIntrEvent
.when();
312 SERIALIZE_SCALAR(rxintrwhen
);
313 SERIALIZE_SCALAR(txintrwhen
);
317 Uart8250::unserialize(Checkpoint
*cp
, const std::string
§ion
)
319 UNSERIALIZE_SCALAR(status
);
320 UNSERIALIZE_SCALAR(IER
);
321 UNSERIALIZE_SCALAR(DLAB
);
322 UNSERIALIZE_SCALAR(LCR
);
323 UNSERIALIZE_SCALAR(MCR
);
326 UNSERIALIZE_SCALAR(rxintrwhen
);
327 UNSERIALIZE_SCALAR(txintrwhen
);
329 rxIntrEvent
.schedule(rxintrwhen
);
331 txIntrEvent
.schedule(txintrwhen
);
334 BEGIN_DECLARE_SIM_OBJECT_PARAMS(Uart8250
)
336 Param
<Addr
> pio_addr
;
337 Param
<Tick
> pio_latency
;
338 SimObjectParam
<Platform
*> platform
;
339 SimObjectParam
<SimConsole
*> sim_console
;
340 SimObjectParam
<System
*> system
;
342 END_DECLARE_SIM_OBJECT_PARAMS(Uart8250
)
344 BEGIN_INIT_SIM_OBJECT_PARAMS(Uart8250
)
346 INIT_PARAM(pio_addr
, "Device Address"),
347 INIT_PARAM_DFLT(pio_latency
, "Programmed IO latency", 1000),
348 INIT_PARAM(platform
, "platform"),
349 INIT_PARAM(sim_console
, "The Simulator Console"),
350 INIT_PARAM(system
, "system object")
352 END_INIT_SIM_OBJECT_PARAMS(Uart8250
)
354 CREATE_SIM_OBJECT(Uart8250
)
356 Uart8250::Params
*p
= new Uart8250::Params
;
357 p
->name
= getInstanceName();
358 p
->pio_addr
= pio_addr
;
359 p
->pio_delay
= pio_latency
;
360 p
->platform
= platform
;
361 p
->cons
= sim_console
;
363 return new Uart8250(p
);
366 REGISTER_SIM_OBJECT("Uart8250", Uart8250
)