pl011.cc (9927:9a41f7f07da5) pl011.cc (10565:23593fdaadcd)
1/*
2 * Copyright (c) 2010 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Ali Saidi
41 */
42
43#include "base/trace.hh"
44#include "debug/Checkpoint.hh"
45#include "debug/Uart.hh"
46#include "dev/arm/amba_device.hh"
47#include "dev/arm/base_gic.hh"
48#include "dev/arm/pl011.hh"
49#include "dev/terminal.hh"
50#include "mem/packet.hh"
51#include "mem/packet_access.hh"
52#include "sim/sim_exit.hh"
53
54Pl011::Pl011(const Params *p)
55 : Uart(p, 0xfff), control(0x300), fbrd(0), ibrd(0), lcrh(0), ifls(0x12),
56 imsc(0), rawInt(0), maskInt(0), intNum(p->int_num), gic(p->gic),
57 endOnEOT(p->end_on_eot), intDelay(p->int_delay), intEvent(this)
58{
59}
60
61Tick
62Pl011::read(PacketPtr pkt)
63{
64 assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
65
66 Addr daddr = pkt->getAddr() - pioAddr;
1/*
2 * Copyright (c) 2010 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Ali Saidi
41 */
42
43#include "base/trace.hh"
44#include "debug/Checkpoint.hh"
45#include "debug/Uart.hh"
46#include "dev/arm/amba_device.hh"
47#include "dev/arm/base_gic.hh"
48#include "dev/arm/pl011.hh"
49#include "dev/terminal.hh"
50#include "mem/packet.hh"
51#include "mem/packet_access.hh"
52#include "sim/sim_exit.hh"
53
54Pl011::Pl011(const Params *p)
55 : Uart(p, 0xfff), control(0x300), fbrd(0), ibrd(0), lcrh(0), ifls(0x12),
56 imsc(0), rawInt(0), maskInt(0), intNum(p->int_num), gic(p->gic),
57 endOnEOT(p->end_on_eot), intDelay(p->int_delay), intEvent(this)
58{
59}
60
61Tick
62Pl011::read(PacketPtr pkt)
63{
64 assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
65
66 Addr daddr = pkt->getAddr() - pioAddr;
67 pkt->allocate();
68
69 DPRINTF(Uart, " read register %#x size=%d\n", daddr, pkt->getSize());
70
71 // use a temporary data since the uart registers are read/written with
72 // different size operations
73 //
74 uint32_t data = 0;
75
76 switch(daddr) {
77 case UART_DR:
78 data = 0;
79 if (term->dataAvailable())
80 data = term->in();
81 break;
82 case UART_FR:
83 // For now we're infintely fast, so TX is never full, always empty,
84 // always clear to send
85 data = UART_FR_TXFE | UART_FR_CTS;
86 if (!term->dataAvailable())
87 data |= UART_FR_RXFE;
88 DPRINTF(Uart, "Reading FR register as %#x rawInt=0x%x imsc=0x%x maskInt=0x%x\n",
89 data, rawInt, imsc, maskInt);
90 break;
91 case UART_CR:
92 data = control;
93 break;
94 case UART_IBRD:
95 data = ibrd;
96 break;
97 case UART_FBRD:
98 data = fbrd;
99 break;
100 case UART_LCRH:
101 data = lcrh;
102 break;
103 case UART_IFLS:
104 data = ifls;
105 break;
106 case UART_IMSC:
107 data = imsc;
108 break;
109 case UART_RIS:
110 data = rawInt;
111 DPRINTF(Uart, "Reading Raw Int status as 0x%x\n", rawInt);
112 break;
113 case UART_MIS:
114 DPRINTF(Uart, "Reading Masked Int status as 0x%x\n", rawInt);
115 data = maskInt;
116 break;
117 default:
118 if (readId(pkt, AMBA_ID, pioAddr)) {
119 // Hack for variable size accesses
120 data = pkt->get<uint32_t>();
121 break;
122 }
123
124 panic("Tried to read PL011 at offset %#x that doesn't exist\n", daddr);
125 break;
126 }
127
128 switch(pkt->getSize()) {
129 case 1:
130 pkt->set<uint8_t>(data);
131 break;
132 case 2:
133 pkt->set<uint16_t>(data);
134 break;
135 case 4:
136 pkt->set<uint32_t>(data);
137 break;
138 default:
139 panic("Uart read size too big?\n");
140 break;
141 }
142
143
144 pkt->makeAtomicResponse();
145 return pioDelay;
146}
147
148Tick
149Pl011::write(PacketPtr pkt)
150{
151
152 assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
153
154 Addr daddr = pkt->getAddr() - pioAddr;
155
156 DPRINTF(Uart, " write register %#x value %#x size=%d\n", daddr,
157 pkt->get<uint8_t>(), pkt->getSize());
158
159 // use a temporary data since the uart registers are read/written with
160 // different size operations
161 //
162 uint32_t data = 0;
163
164 switch(pkt->getSize()) {
165 case 1:
166 data = pkt->get<uint8_t>();
167 break;
168 case 2:
169 data = pkt->get<uint16_t>();
170 break;
171 case 4:
172 data = pkt->get<uint32_t>();
173 break;
174 default:
175 panic("Uart write size too big?\n");
176 break;
177 }
178
179
180 switch (daddr) {
181 case UART_DR:
182 if ((data & 0xFF) == 0x04 && endOnEOT)
183 exitSimLoop("UART received EOT", 0);
184
185 term->out(data & 0xFF);
186
187 //raw interrupt is set regardless of imsc.txim
188 rawInt.txim = 1;
189 if (imsc.txim) {
190 DPRINTF(Uart, "TX int enabled, scheduling interruptt\n");
191 if (!intEvent.scheduled())
192 schedule(intEvent, curTick() + intDelay);
193 }
194
195 break;
196 case UART_CR:
197 control = data;
198 break;
199 case UART_IBRD:
200 ibrd = data;
201 break;
202 case UART_FBRD:
203 fbrd = data;
204 break;
205 case UART_LCRH:
206 lcrh = data;
207 break;
208 case UART_IFLS:
209 ifls = data;
210 break;
211 case UART_IMSC:
212 imsc = data;
213
214 if (imsc.feim || imsc.peim || imsc.beim || imsc.oeim || imsc.rsvd)
215 panic("Unknown interrupt enabled\n");
216
217 // rimim, ctsmim, dcdmim, dsrmim can be enabled but are ignored
218 // they are supposed to interrupt on a change of status in the line
219 // which we should never have since our terminal is happy to always
220 // receive bytes.
221
222 if (imsc.txim) {
223 DPRINTF(Uart, "Writing to IMSC: TX int enabled, scheduling interruptt\n");
224 rawInt.txim = 1;
225 if (!intEvent.scheduled())
226 schedule(intEvent, curTick() + intDelay);
227 }
228
229 break;
230
231 case UART_ICR:
232 DPRINTF(Uart, "Clearing interrupts 0x%x\n", data);
233 rawInt = rawInt & ~data;
234 maskInt = rawInt & imsc;
235
236 DPRINTF(Uart, " -- Masked interrupts 0x%x\n", maskInt);
237
238 if (!maskInt)
239 gic->clearInt(intNum);
240
241 break;
242 default:
243 panic("Tried to write PL011 at offset %#x that doesn't exist\n", daddr);
244 break;
245 }
246 pkt->makeAtomicResponse();
247 return pioDelay;
248}
249
250void
251Pl011::dataAvailable()
252{
253 /*@todo ignore the fifo, just say we have data now
254 * We might want to fix this, or we might not care */
255 rawInt.rxim = 1;
256 rawInt.rtim = 1;
257
258 DPRINTF(Uart, "Data available, scheduling interrupt\n");
259
260 if (!intEvent.scheduled())
261 schedule(intEvent, curTick() + intDelay);
262}
263
264void
265Pl011::generateInterrupt()
266{
267 DPRINTF(Uart, "Generate Interrupt: imsc=0x%x rawInt=0x%x maskInt=0x%x\n",
268 imsc, rawInt, maskInt);
269 maskInt = imsc & rawInt;
270
271 if (maskInt.rxim || maskInt.rtim || maskInt.txim) {
272 gic->sendInt(intNum);
273 DPRINTF(Uart, " -- Generated\n");
274 }
275
276}
277
278
279
280void
281Pl011::serialize(std::ostream &os)
282{
283 DPRINTF(Checkpoint, "Serializing Arm PL011\n");
284 SERIALIZE_SCALAR(control);
285 SERIALIZE_SCALAR(fbrd);
286 SERIALIZE_SCALAR(ibrd);
287 SERIALIZE_SCALAR(lcrh);
288 SERIALIZE_SCALAR(ifls);
289
290 uint16_t imsc_serial = imsc;
291 SERIALIZE_SCALAR(imsc_serial);
292
293 uint16_t rawInt_serial = rawInt;
294 SERIALIZE_SCALAR(rawInt_serial);
295
296 uint16_t maskInt_serial = maskInt;
297 SERIALIZE_SCALAR(maskInt_serial);
298
299 SERIALIZE_SCALAR(endOnEOT);
300 SERIALIZE_SCALAR(intDelay);
301}
302
303void
304Pl011::unserialize(Checkpoint *cp, const std::string &section)
305{
306 DPRINTF(Checkpoint, "Unserializing Arm PL011\n");
307
308 UNSERIALIZE_SCALAR(control);
309 UNSERIALIZE_SCALAR(fbrd);
310 UNSERIALIZE_SCALAR(ibrd);
311 UNSERIALIZE_SCALAR(lcrh);
312 UNSERIALIZE_SCALAR(ifls);
313
314 uint16_t imsc_serial;
315 UNSERIALIZE_SCALAR(imsc_serial);
316 imsc = imsc_serial;
317
318 uint16_t rawInt_serial;
319 UNSERIALIZE_SCALAR(rawInt_serial);
320 rawInt = rawInt_serial;
321
322 uint16_t maskInt_serial;
323 UNSERIALIZE_SCALAR(maskInt_serial);
324 maskInt = maskInt_serial;
325
326 UNSERIALIZE_SCALAR(endOnEOT);
327 UNSERIALIZE_SCALAR(intDelay);
328}
329
330Pl011 *
331Pl011Params::create()
332{
333 return new Pl011(this);
334}
67
68 DPRINTF(Uart, " read register %#x size=%d\n", daddr, pkt->getSize());
69
70 // use a temporary data since the uart registers are read/written with
71 // different size operations
72 //
73 uint32_t data = 0;
74
75 switch(daddr) {
76 case UART_DR:
77 data = 0;
78 if (term->dataAvailable())
79 data = term->in();
80 break;
81 case UART_FR:
82 // For now we're infintely fast, so TX is never full, always empty,
83 // always clear to send
84 data = UART_FR_TXFE | UART_FR_CTS;
85 if (!term->dataAvailable())
86 data |= UART_FR_RXFE;
87 DPRINTF(Uart, "Reading FR register as %#x rawInt=0x%x imsc=0x%x maskInt=0x%x\n",
88 data, rawInt, imsc, maskInt);
89 break;
90 case UART_CR:
91 data = control;
92 break;
93 case UART_IBRD:
94 data = ibrd;
95 break;
96 case UART_FBRD:
97 data = fbrd;
98 break;
99 case UART_LCRH:
100 data = lcrh;
101 break;
102 case UART_IFLS:
103 data = ifls;
104 break;
105 case UART_IMSC:
106 data = imsc;
107 break;
108 case UART_RIS:
109 data = rawInt;
110 DPRINTF(Uart, "Reading Raw Int status as 0x%x\n", rawInt);
111 break;
112 case UART_MIS:
113 DPRINTF(Uart, "Reading Masked Int status as 0x%x\n", rawInt);
114 data = maskInt;
115 break;
116 default:
117 if (readId(pkt, AMBA_ID, pioAddr)) {
118 // Hack for variable size accesses
119 data = pkt->get<uint32_t>();
120 break;
121 }
122
123 panic("Tried to read PL011 at offset %#x that doesn't exist\n", daddr);
124 break;
125 }
126
127 switch(pkt->getSize()) {
128 case 1:
129 pkt->set<uint8_t>(data);
130 break;
131 case 2:
132 pkt->set<uint16_t>(data);
133 break;
134 case 4:
135 pkt->set<uint32_t>(data);
136 break;
137 default:
138 panic("Uart read size too big?\n");
139 break;
140 }
141
142
143 pkt->makeAtomicResponse();
144 return pioDelay;
145}
146
147Tick
148Pl011::write(PacketPtr pkt)
149{
150
151 assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
152
153 Addr daddr = pkt->getAddr() - pioAddr;
154
155 DPRINTF(Uart, " write register %#x value %#x size=%d\n", daddr,
156 pkt->get<uint8_t>(), pkt->getSize());
157
158 // use a temporary data since the uart registers are read/written with
159 // different size operations
160 //
161 uint32_t data = 0;
162
163 switch(pkt->getSize()) {
164 case 1:
165 data = pkt->get<uint8_t>();
166 break;
167 case 2:
168 data = pkt->get<uint16_t>();
169 break;
170 case 4:
171 data = pkt->get<uint32_t>();
172 break;
173 default:
174 panic("Uart write size too big?\n");
175 break;
176 }
177
178
179 switch (daddr) {
180 case UART_DR:
181 if ((data & 0xFF) == 0x04 && endOnEOT)
182 exitSimLoop("UART received EOT", 0);
183
184 term->out(data & 0xFF);
185
186 //raw interrupt is set regardless of imsc.txim
187 rawInt.txim = 1;
188 if (imsc.txim) {
189 DPRINTF(Uart, "TX int enabled, scheduling interruptt\n");
190 if (!intEvent.scheduled())
191 schedule(intEvent, curTick() + intDelay);
192 }
193
194 break;
195 case UART_CR:
196 control = data;
197 break;
198 case UART_IBRD:
199 ibrd = data;
200 break;
201 case UART_FBRD:
202 fbrd = data;
203 break;
204 case UART_LCRH:
205 lcrh = data;
206 break;
207 case UART_IFLS:
208 ifls = data;
209 break;
210 case UART_IMSC:
211 imsc = data;
212
213 if (imsc.feim || imsc.peim || imsc.beim || imsc.oeim || imsc.rsvd)
214 panic("Unknown interrupt enabled\n");
215
216 // rimim, ctsmim, dcdmim, dsrmim can be enabled but are ignored
217 // they are supposed to interrupt on a change of status in the line
218 // which we should never have since our terminal is happy to always
219 // receive bytes.
220
221 if (imsc.txim) {
222 DPRINTF(Uart, "Writing to IMSC: TX int enabled, scheduling interruptt\n");
223 rawInt.txim = 1;
224 if (!intEvent.scheduled())
225 schedule(intEvent, curTick() + intDelay);
226 }
227
228 break;
229
230 case UART_ICR:
231 DPRINTF(Uart, "Clearing interrupts 0x%x\n", data);
232 rawInt = rawInt & ~data;
233 maskInt = rawInt & imsc;
234
235 DPRINTF(Uart, " -- Masked interrupts 0x%x\n", maskInt);
236
237 if (!maskInt)
238 gic->clearInt(intNum);
239
240 break;
241 default:
242 panic("Tried to write PL011 at offset %#x that doesn't exist\n", daddr);
243 break;
244 }
245 pkt->makeAtomicResponse();
246 return pioDelay;
247}
248
249void
250Pl011::dataAvailable()
251{
252 /*@todo ignore the fifo, just say we have data now
253 * We might want to fix this, or we might not care */
254 rawInt.rxim = 1;
255 rawInt.rtim = 1;
256
257 DPRINTF(Uart, "Data available, scheduling interrupt\n");
258
259 if (!intEvent.scheduled())
260 schedule(intEvent, curTick() + intDelay);
261}
262
263void
264Pl011::generateInterrupt()
265{
266 DPRINTF(Uart, "Generate Interrupt: imsc=0x%x rawInt=0x%x maskInt=0x%x\n",
267 imsc, rawInt, maskInt);
268 maskInt = imsc & rawInt;
269
270 if (maskInt.rxim || maskInt.rtim || maskInt.txim) {
271 gic->sendInt(intNum);
272 DPRINTF(Uart, " -- Generated\n");
273 }
274
275}
276
277
278
279void
280Pl011::serialize(std::ostream &os)
281{
282 DPRINTF(Checkpoint, "Serializing Arm PL011\n");
283 SERIALIZE_SCALAR(control);
284 SERIALIZE_SCALAR(fbrd);
285 SERIALIZE_SCALAR(ibrd);
286 SERIALIZE_SCALAR(lcrh);
287 SERIALIZE_SCALAR(ifls);
288
289 uint16_t imsc_serial = imsc;
290 SERIALIZE_SCALAR(imsc_serial);
291
292 uint16_t rawInt_serial = rawInt;
293 SERIALIZE_SCALAR(rawInt_serial);
294
295 uint16_t maskInt_serial = maskInt;
296 SERIALIZE_SCALAR(maskInt_serial);
297
298 SERIALIZE_SCALAR(endOnEOT);
299 SERIALIZE_SCALAR(intDelay);
300}
301
302void
303Pl011::unserialize(Checkpoint *cp, const std::string &section)
304{
305 DPRINTF(Checkpoint, "Unserializing Arm PL011\n");
306
307 UNSERIALIZE_SCALAR(control);
308 UNSERIALIZE_SCALAR(fbrd);
309 UNSERIALIZE_SCALAR(ibrd);
310 UNSERIALIZE_SCALAR(lcrh);
311 UNSERIALIZE_SCALAR(ifls);
312
313 uint16_t imsc_serial;
314 UNSERIALIZE_SCALAR(imsc_serial);
315 imsc = imsc_serial;
316
317 uint16_t rawInt_serial;
318 UNSERIALIZE_SCALAR(rawInt_serial);
319 rawInt = rawInt_serial;
320
321 uint16_t maskInt_serial;
322 UNSERIALIZE_SCALAR(maskInt_serial);
323 maskInt = maskInt_serial;
324
325 UNSERIALIZE_SCALAR(endOnEOT);
326 UNSERIALIZE_SCALAR(intDelay);
327}
328
329Pl011 *
330Pl011Params::create()
331{
332 return new Pl011(this);
333}