ETRAX: Drop the _t for the internal PIC type.
[qemu] / hw / etraxfs_ser.c
1 /*
2  * QEMU ETRAX System Emulator
3  *
4  * Copyright (c) 2007 Edgar E. Iglesias, Axis Communications AB.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24
25 #include <stdio.h>
26 #include <ctype.h>
27 #include "hw.h"
28 #include "qemu-char.h"
29 #include "etraxfs.h"
30
31 #define D(x)
32
33 #define RW_TR_CTRL     0x00
34 #define RW_TR_DMA_EN   0x04
35 #define RW_REC_CTRL    0x08
36 #define RW_DOUT        0x1c
37 #define RS_STAT_DIN    0x20
38 #define R_STAT_DIN     0x24
39 #define RW_INTR_MASK   0x2c
40 #define RW_ACK_INTR    0x30
41 #define R_INTR         0x34
42 #define R_MASKED_INTR  0x38
43
44 #define STAT_DAV     16
45 #define STAT_TR_IDLE 22
46 #define STAT_TR_RDY  24
47
48 struct etrax_serial_t
49 {
50         CPUState *env;
51         CharDriverState *chr;
52         qemu_irq *irq;
53
54         int pending_tx;
55
56         /* Control registers.  */
57         uint32_t rw_tr_ctrl;
58         uint32_t rw_tr_dma_en;
59         uint32_t rw_rec_ctrl;
60         uint32_t rs_stat_din;
61         uint32_t r_stat_din;
62         uint32_t rw_intr_mask;
63         uint32_t rw_ack_intr;
64         uint32_t r_intr;
65         uint32_t r_masked_intr;
66 };
67
68 static void ser_update_irq(struct etrax_serial_t *s)
69 {
70         s->r_intr &= ~(s->rw_ack_intr);
71         s->r_masked_intr = s->r_intr & s->rw_intr_mask;
72
73         D(printf("irq_mask=%x r_intr=%x rmi=%x airq=%x \n", 
74                  s->rw_intr_mask, s->r_intr, 
75                  s->r_masked_intr, s->rw_ack_intr));
76         qemu_set_irq(s->irq[0], !!s->r_masked_intr);
77         s->rw_ack_intr = 0;
78 }
79
80 static uint32_t ser_readb (void *opaque, target_phys_addr_t addr)
81 {
82         D(CPUState *env = opaque);
83         D(printf ("%s %x\n", __func__, addr));
84         return 0;
85 }
86
87 static uint32_t ser_readl (void *opaque, target_phys_addr_t addr)
88 {
89         struct etrax_serial_t *s = opaque;
90         D(CPUState *env = s->env);
91         uint32_t r = 0;
92
93         switch (addr)
94         {
95                 case RW_TR_CTRL:
96                         r = s->rw_tr_ctrl;
97                         break;
98                 case RW_TR_DMA_EN:
99                         r = s->rw_tr_dma_en;
100                         break;
101                 case RS_STAT_DIN:
102                         r = s->rs_stat_din;
103                         /* clear dav.  */
104                         s->rs_stat_din &= ~(1 << STAT_DAV);
105                         break;
106                 case R_STAT_DIN:
107                         r = s->rs_stat_din;
108                         break;
109                 case RW_ACK_INTR:
110                         D(printf("load rw_ack_intr=%x\n", s->rw_ack_intr));
111                         r = s->rw_ack_intr;
112                         break;
113                 case RW_INTR_MASK:
114                         r = s->rw_intr_mask;
115                         break;
116                 case R_INTR:
117                         D(printf("load r_intr=%x\n", s->r_intr));
118                         r = s->r_intr;
119                         break;
120                 case R_MASKED_INTR:
121                         D(printf("load r_maked_intr=%x\n", s->r_masked_intr));
122                         r = s->r_masked_intr;
123                         break;
124
125                 default:
126                         D(printf ("%s %x\n", __func__, addr));
127                         break;
128         }
129         return r;
130 }
131
132 static void
133 ser_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
134 {
135         D(struct etrax_serial_t *s = opaque);
136         D(CPUState *env = s->env);
137         D(printf ("%s %x %x\n", __func__, addr, value));
138 }
139 static void
140 ser_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
141 {
142         struct etrax_serial_t *s = opaque;
143         unsigned char ch = value;
144         D(CPUState *env = s->env);
145
146         switch (addr)
147         {
148                 case RW_TR_CTRL:
149                         D(printf("rw_tr_ctrl=%x\n", value));
150                         s->rw_tr_ctrl = value;
151                         break;
152                 case RW_TR_DMA_EN:
153                         D(printf("rw_tr_dma_en=%x\n", value));
154                         s->rw_tr_dma_en = value;
155                         break;
156                 case RW_DOUT:
157                         qemu_chr_write(s->chr, &ch, 1);
158                         s->r_intr |= 1;
159                         s->pending_tx = 1;
160                         break;
161                 case RW_ACK_INTR:
162                         D(printf("rw_ack_intr=%x\n", value));
163                         s->rw_ack_intr = value;
164                         if (s->pending_tx && (s->rw_ack_intr & 1)) {
165                                 s->r_intr |= 1;
166                                 s->pending_tx = 0;
167                                 s->rw_ack_intr &= ~1;
168                         }
169                         break;
170                 case RW_INTR_MASK:
171                         D(printf("r_intr_mask=%x\n", value));
172                         s->rw_intr_mask = value;
173                         break;
174                 default:
175                         D(printf ("%s %x %x\n",  __func__, addr, value));
176                         break;
177         }
178         ser_update_irq(s);
179 }
180
181 static CPUReadMemoryFunc *ser_read[] = {
182         &ser_readb,
183         &ser_readb,
184         &ser_readl,
185 };
186
187 static CPUWriteMemoryFunc *ser_write[] = {
188         &ser_writeb,
189         &ser_writeb,
190         &ser_writel,
191 };
192
193 static void serial_receive(void *opaque, const uint8_t *buf, int size)
194 {
195         struct etrax_serial_t *s = opaque;
196
197         s->r_intr |= 8;
198         s->rs_stat_din &= ~0xff;
199         s->rs_stat_din |= (buf[0] & 0xff);
200         s->rs_stat_din |= (1 << STAT_DAV); /* dav.  */
201         ser_update_irq(s);
202 }
203
204 static int serial_can_receive(void *opaque)
205 {
206         struct etrax_serial_t *s = opaque;
207         int r;
208
209         /* Is the receiver enabled?  */
210         r = s->rw_rec_ctrl & 1;
211
212         /* Pending rx data?  */
213         r |= !(s->r_intr & 8);
214         return r;
215 }
216
217 static void serial_event(void *opaque, int event)
218 {
219
220 }
221
222 void etraxfs_ser_init(CPUState *env, qemu_irq *irq, CharDriverState *chr,
223                       target_phys_addr_t base)
224 {
225         struct etrax_serial_t *s;
226         int ser_regs;
227
228         s = qemu_mallocz(sizeof *s);
229
230         s->env = env;
231         s->irq = irq;
232         s->chr = chr;
233
234         /* transmitter begins ready and idle.  */
235         s->rs_stat_din |= (1 << STAT_TR_RDY);
236         s->rs_stat_din |= (1 << STAT_TR_IDLE);
237
238         qemu_chr_add_handlers(chr, serial_can_receive, serial_receive,
239                               serial_event, s);
240
241         ser_regs = cpu_register_io_memory(0, ser_read, ser_write, s);
242         cpu_register_physical_memory (base, 0x3c, ser_regs);
243 }