ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1 | /* |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 2 | * Arm PrimeCell PL080/PL081 DMA controller |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2006 CodeSourcery. |
| 5 | * Written by Paul Brook |
| 6 | * |
| 7 | * This code is licenced under the GPL. |
| 8 | */ |
| 9 | |
| 10 | #include "vl.h" |
| 11 | |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 12 | #define PL080_MAX_CHANNELS 8 |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 13 | #define PL080_CONF_E 0x1 |
| 14 | #define PL080_CONF_M1 0x2 |
| 15 | #define PL080_CONF_M2 0x4 |
| 16 | |
| 17 | #define PL080_CCONF_H 0x40000 |
| 18 | #define PL080_CCONF_A 0x20000 |
| 19 | #define PL080_CCONF_L 0x10000 |
| 20 | #define PL080_CCONF_ITC 0x08000 |
| 21 | #define PL080_CCONF_IE 0x04000 |
| 22 | #define PL080_CCONF_E 0x00001 |
| 23 | |
| 24 | #define PL080_CCTRL_I 0x80000000 |
| 25 | #define PL080_CCTRL_DI 0x08000000 |
| 26 | #define PL080_CCTRL_SI 0x04000000 |
| 27 | #define PL080_CCTRL_D 0x02000000 |
| 28 | #define PL080_CCTRL_S 0x01000000 |
| 29 | |
| 30 | typedef struct { |
| 31 | uint32_t src; |
| 32 | uint32_t dest; |
| 33 | uint32_t lli; |
| 34 | uint32_t ctrl; |
| 35 | uint32_t conf; |
| 36 | } pl080_channel; |
| 37 | |
| 38 | typedef struct { |
| 39 | uint32_t base; |
| 40 | uint8_t tc_int; |
| 41 | uint8_t tc_mask; |
| 42 | uint8_t err_int; |
| 43 | uint8_t err_mask; |
| 44 | uint32_t conf; |
| 45 | uint32_t sync; |
| 46 | uint32_t req_single; |
| 47 | uint32_t req_burst; |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 48 | pl080_channel chan[PL080_MAX_CHANNELS]; |
| 49 | int nchannels; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 50 | /* Flag to avoid recursive DMA invocations. */ |
| 51 | int running; |
pbrook | d537cf6 | 2007-04-07 18:14:41 +0000 | [diff] [blame] | 52 | qemu_irq irq; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 53 | } pl080_state; |
| 54 | |
| 55 | static const unsigned char pl080_id[] = |
| 56 | { 0x80, 0x10, 0x04, 0x0a, 0x0d, 0xf0, 0x05, 0xb1 }; |
| 57 | |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 58 | static const unsigned char pl081_id[] = |
| 59 | { 0x81, 0x10, 0x04, 0x0a, 0x0d, 0xf0, 0x05, 0xb1 }; |
| 60 | |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 61 | static void pl080_update(pl080_state *s) |
| 62 | { |
| 63 | if ((s->tc_int & s->tc_mask) |
| 64 | || (s->err_int & s->err_mask)) |
pbrook | d537cf6 | 2007-04-07 18:14:41 +0000 | [diff] [blame] | 65 | qemu_irq_raise(s->irq); |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 66 | else |
pbrook | d537cf6 | 2007-04-07 18:14:41 +0000 | [diff] [blame] | 67 | qemu_irq_lower(s->irq); |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void pl080_run(pl080_state *s) |
| 71 | { |
| 72 | int c; |
| 73 | int flow; |
| 74 | pl080_channel *ch; |
| 75 | int swidth; |
| 76 | int dwidth; |
| 77 | int xsize; |
| 78 | int n; |
| 79 | int src_id; |
| 80 | int dest_id; |
| 81 | int size; |
| 82 | char buff[4]; |
| 83 | uint32_t req; |
| 84 | |
| 85 | s->tc_mask = 0; |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 86 | for (c = 0; c < s->nchannels; c++) { |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 87 | if (s->chan[c].conf & PL080_CCONF_ITC) |
| 88 | s->tc_mask |= 1 << c; |
| 89 | if (s->chan[c].conf & PL080_CCONF_IE) |
| 90 | s->err_mask |= 1 << c; |
| 91 | } |
| 92 | |
| 93 | if ((s->conf & PL080_CONF_E) == 0) |
| 94 | return; |
| 95 | |
| 96 | cpu_abort(cpu_single_env, "DMA active\n"); |
| 97 | /* If we are already in the middle of a DMA operation then indicate that |
| 98 | there may be new DMA requests and return immediately. */ |
| 99 | if (s->running) { |
| 100 | s->running++; |
| 101 | return; |
| 102 | } |
| 103 | s->running = 1; |
| 104 | while (s->running) { |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 105 | for (c = 0; c < s->nchannels; c++) { |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 106 | ch = &s->chan[c]; |
| 107 | again: |
| 108 | /* Test if thiws channel has any pending DMA requests. */ |
| 109 | if ((ch->conf & (PL080_CCONF_H | PL080_CCONF_E)) |
| 110 | != PL080_CCONF_E) |
| 111 | continue; |
| 112 | flow = (ch->conf >> 11) & 7; |
| 113 | if (flow >= 4) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 114 | cpu_abort(cpu_single_env, |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 115 | "pl080_run: Peripheral flow control not implemented\n"); |
| 116 | } |
| 117 | src_id = (ch->conf >> 1) & 0x1f; |
| 118 | dest_id = (ch->conf >> 6) & 0x1f; |
| 119 | size = ch->ctrl & 0xfff; |
| 120 | req = s->req_single | s->req_burst; |
| 121 | switch (flow) { |
| 122 | case 0: |
| 123 | break; |
| 124 | case 1: |
| 125 | if ((req & (1u << dest_id)) == 0) |
| 126 | size = 0; |
| 127 | break; |
| 128 | case 2: |
| 129 | if ((req & (1u << src_id)) == 0) |
| 130 | size = 0; |
| 131 | break; |
| 132 | case 3: |
| 133 | if ((req & (1u << src_id)) == 0 |
| 134 | || (req & (1u << dest_id)) == 0) |
| 135 | size = 0; |
| 136 | break; |
| 137 | } |
| 138 | if (!size) |
| 139 | continue; |
| 140 | |
| 141 | /* Transfer one element. */ |
| 142 | /* ??? Should transfer multiple elements for a burst request. */ |
| 143 | /* ??? Unclear what the proper behavior is when source and |
| 144 | destination widths are different. */ |
| 145 | swidth = 1 << ((ch->ctrl >> 18) & 7); |
| 146 | dwidth = 1 << ((ch->ctrl >> 21) & 7); |
| 147 | for (n = 0; n < dwidth; n+= swidth) { |
| 148 | cpu_physical_memory_read(ch->src, buff + n, swidth); |
| 149 | if (ch->ctrl & PL080_CCTRL_SI) |
| 150 | ch->src += swidth; |
| 151 | } |
| 152 | xsize = (dwidth < swidth) ? swidth : dwidth; |
| 153 | /* ??? This may pad the value incorrectly for dwidth < 32. */ |
| 154 | for (n = 0; n < xsize; n += dwidth) { |
| 155 | cpu_physical_memory_write(ch->dest + n, buff + n, dwidth); |
| 156 | if (ch->ctrl & PL080_CCTRL_DI) |
| 157 | ch->dest += swidth; |
| 158 | } |
| 159 | |
| 160 | size--; |
| 161 | ch->ctrl = (ch->ctrl & 0xfffff000) | size; |
| 162 | if (size == 0) { |
| 163 | /* Transfer complete. */ |
| 164 | if (ch->lli) { |
| 165 | ch->src = ldl_phys(ch->lli); |
| 166 | ch->dest = ldl_phys(ch->lli + 4); |
| 167 | ch->ctrl = ldl_phys(ch->lli + 12); |
| 168 | ch->lli = ldl_phys(ch->lli + 8); |
| 169 | } else { |
| 170 | ch->conf &= ~PL080_CCONF_E; |
| 171 | } |
| 172 | if (ch->ctrl & PL080_CCTRL_I) { |
| 173 | s->tc_int |= 1 << c; |
| 174 | } |
| 175 | } |
| 176 | goto again; |
| 177 | } |
| 178 | if (--s->running) |
| 179 | s->running = 1; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | static uint32_t pl080_read(void *opaque, target_phys_addr_t offset) |
| 184 | { |
| 185 | pl080_state *s = (pl080_state *)opaque; |
| 186 | uint32_t i; |
| 187 | uint32_t mask; |
| 188 | |
| 189 | offset -= s->base; |
| 190 | if (offset >= 0xfe0 && offset < 0x1000) { |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 191 | if (s->nchannels == 8) { |
| 192 | return pl080_id[(offset - 0xfe0) >> 2]; |
| 193 | } else { |
| 194 | return pl081_id[(offset - 0xfe0) >> 2]; |
| 195 | } |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 196 | } |
| 197 | if (offset >= 0x100 && offset < 0x200) { |
| 198 | i = (offset & 0xe0) >> 5; |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 199 | if (i >= s->nchannels) |
| 200 | goto bad_offset; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 201 | switch (offset >> 2) { |
| 202 | case 0: /* SrcAddr */ |
| 203 | return s->chan[i].src; |
| 204 | case 1: /* DestAddr */ |
| 205 | return s->chan[i].dest; |
| 206 | case 2: /* LLI */ |
| 207 | return s->chan[i].lli; |
| 208 | case 3: /* Control */ |
| 209 | return s->chan[i].ctrl; |
| 210 | case 4: /* Configuration */ |
| 211 | return s->chan[i].conf; |
| 212 | default: |
| 213 | goto bad_offset; |
| 214 | } |
| 215 | } |
| 216 | switch (offset >> 2) { |
| 217 | case 0: /* IntStatus */ |
| 218 | return (s->tc_int & s->tc_mask) | (s->err_int & s->err_mask); |
| 219 | case 1: /* IntTCStatus */ |
| 220 | return (s->tc_int & s->tc_mask); |
| 221 | case 3: /* IntErrorStatus */ |
| 222 | return (s->err_int & s->err_mask); |
| 223 | case 5: /* RawIntTCStatus */ |
| 224 | return s->tc_int; |
| 225 | case 6: /* RawIntErrorStatus */ |
| 226 | return s->err_int; |
| 227 | case 7: /* EnbldChns */ |
| 228 | mask = 0; |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 229 | for (i = 0; i < s->nchannels; i++) { |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 230 | if (s->chan[i].conf & PL080_CCONF_E) |
| 231 | mask |= 1 << i; |
| 232 | } |
| 233 | return mask; |
| 234 | case 8: /* SoftBReq */ |
| 235 | case 9: /* SoftSReq */ |
| 236 | case 10: /* SoftLBReq */ |
| 237 | case 11: /* SoftLSReq */ |
| 238 | /* ??? Implement these. */ |
| 239 | return 0; |
| 240 | case 12: /* Configuration */ |
| 241 | return s->conf; |
| 242 | case 13: /* Sync */ |
| 243 | return s->sync; |
| 244 | default: |
| 245 | bad_offset: |
| 246 | cpu_abort(cpu_single_env, "pl080_read: Bad offset %x\n", offset); |
| 247 | return 0; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | static void pl080_write(void *opaque, target_phys_addr_t offset, |
| 252 | uint32_t value) |
| 253 | { |
| 254 | pl080_state *s = (pl080_state *)opaque; |
| 255 | int i; |
| 256 | |
| 257 | offset -= s->base; |
| 258 | if (offset >= 0x100 && offset < 0x200) { |
| 259 | i = (offset & 0xe0) >> 5; |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 260 | if (i >= s->nchannels) |
| 261 | goto bad_offset; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 262 | switch (offset >> 2) { |
| 263 | case 0: /* SrcAddr */ |
| 264 | s->chan[i].src = value; |
| 265 | break; |
| 266 | case 1: /* DestAddr */ |
| 267 | s->chan[i].dest = value; |
| 268 | break; |
| 269 | case 2: /* LLI */ |
| 270 | s->chan[i].lli = value; |
| 271 | break; |
| 272 | case 3: /* Control */ |
| 273 | s->chan[i].ctrl = value; |
| 274 | break; |
| 275 | case 4: /* Configuration */ |
| 276 | s->chan[i].conf = value; |
| 277 | pl080_run(s); |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | switch (offset >> 2) { |
| 282 | case 2: /* IntTCClear */ |
| 283 | s->tc_int &= ~value; |
| 284 | break; |
| 285 | case 4: /* IntErrorClear */ |
| 286 | s->err_int &= ~value; |
| 287 | break; |
| 288 | case 8: /* SoftBReq */ |
| 289 | case 9: /* SoftSReq */ |
| 290 | case 10: /* SoftLBReq */ |
| 291 | case 11: /* SoftLSReq */ |
| 292 | /* ??? Implement these. */ |
| 293 | cpu_abort(cpu_single_env, "pl080_write: Soft DMA not implemented\n"); |
| 294 | break; |
| 295 | case 12: /* Configuration */ |
| 296 | s->conf = value; |
| 297 | if (s->conf & (PL080_CONF_M1 | PL080_CONF_M1)) { |
| 298 | cpu_abort(cpu_single_env, |
| 299 | "pl080_write: Big-endian DMA not implemented\n"); |
| 300 | } |
| 301 | pl080_run(s); |
| 302 | break; |
| 303 | case 13: /* Sync */ |
| 304 | s->sync = value; |
| 305 | break; |
| 306 | default: |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 307 | bad_offset: |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 308 | cpu_abort(cpu_single_env, "pl080_write: Bad offset %x\n", offset); |
| 309 | } |
| 310 | pl080_update(s); |
| 311 | } |
| 312 | |
| 313 | static CPUReadMemoryFunc *pl080_readfn[] = { |
| 314 | pl080_read, |
| 315 | pl080_read, |
| 316 | pl080_read |
| 317 | }; |
| 318 | |
| 319 | static CPUWriteMemoryFunc *pl080_writefn[] = { |
| 320 | pl080_write, |
| 321 | pl080_write, |
| 322 | pl080_write |
| 323 | }; |
| 324 | |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 325 | /* The PL080 and PL081 are the same except for the number of channels |
| 326 | they implement (8 and 2 respectively). */ |
pbrook | d537cf6 | 2007-04-07 18:14:41 +0000 | [diff] [blame] | 327 | void *pl080_init(uint32_t base, qemu_irq irq, int nchannels) |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 328 | { |
| 329 | int iomemtype; |
| 330 | pl080_state *s; |
| 331 | |
| 332 | s = (pl080_state *)qemu_mallocz(sizeof(pl080_state)); |
| 333 | iomemtype = cpu_register_io_memory(0, pl080_readfn, |
| 334 | pl080_writefn, s); |
pbrook | 187337f | 2007-06-03 15:19:33 +0000 | [diff] [blame] | 335 | cpu_register_physical_memory(base, 0x00001000, iomemtype); |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 336 | s->base = base; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 337 | s->irq = irq; |
pbrook | e69954b | 2006-09-23 17:40:58 +0000 | [diff] [blame] | 338 | s->nchannels = nchannels; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 339 | /* ??? Save/restore. */ |
| 340 | return s; |
| 341 | } |
| 342 | |