1 /* 2 * StrongARM SA-1100/SA-1110 emulation 3 * 4 * Copyright (C) 2011 Dmitry Eremin-Solenikov 5 * 6 * Largely based on StrongARM emulation: 7 * Copyright (c) 2006 Openedhand Ltd. 8 * Written by Andrzej Zaborowski <balrog@zabor.org> 9 * 10 * UART code based on QEMU 16550A UART emulation 11 * Copyright (c) 2003-2004 Fabrice Bellard 12 * Copyright (c) 2008 Citrix Systems, Inc. 13 * 14 * This program is free software; you can redistribute it and/or modify 15 * it under the terms of the GNU General Public License version 2 as 16 * published by the Free Software Foundation. 17 * 18 * This program is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU General Public License for more details. 22 * 23 * You should have received a copy of the GNU General Public License along 24 * with this program; if not, see <http://www.gnu.org/licenses/>. 25 * 26 * Contributions after 2012-01-13 are licensed under the terms of the 27 * GNU GPL, version 2 or (at your option) any later version. 28 */ 29 #include "hw/sysbus.h" 30 #include "strongarm.h" 31 #include "qemu/error-report.h" 32 #include "hw/arm/arm.h" 33 #include "sysemu/char.h" 34 #include "sysemu/sysemu.h" 35 #include "hw/ssi.h" 36 37 //#define DEBUG 38 39 /* 40 TODO 41 - Implement cp15, c14 ? 42 - Implement cp15, c15 !!! (idle used in L) 43 - Implement idle mode handling/DIM 44 - Implement sleep mode/Wake sources 45 - Implement reset control 46 - Implement memory control regs 47 - PCMCIA handling 48 - Maybe support MBGNT/MBREQ 49 - DMA channels 50 - GPCLK 51 - IrDA 52 - MCP 53 - Enhance UART with modem signals 54 */ 55 56 #ifdef DEBUG 57 # define DPRINTF(format, ...) printf(format , ## __VA_ARGS__) 58 #else 59 # define DPRINTF(format, ...) do { } while (0) 60 #endif 61 62 static struct { 63 hwaddr io_base; 64 int irq; 65 } sa_serial[] = { 66 { 0x80010000, SA_PIC_UART1 }, 67 { 0x80030000, SA_PIC_UART2 }, 68 { 0x80050000, SA_PIC_UART3 }, 69 { 0, 0 } 70 }; 71 72 /* Interrupt Controller */ 73 74 #define TYPE_STRONGARM_PIC "strongarm_pic" 75 #define STRONGARM_PIC(obj) \ 76 OBJECT_CHECK(StrongARMPICState, (obj), TYPE_STRONGARM_PIC) 77 78 typedef struct StrongARMPICState { 79 SysBusDevice parent_obj; 80 81 MemoryRegion iomem; 82 qemu_irq irq; 83 qemu_irq fiq; 84 85 uint32_t pending; 86 uint32_t enabled; 87 uint32_t is_fiq; 88 uint32_t int_idle; 89 } StrongARMPICState; 90 91 #define ICIP 0x00 92 #define ICMR 0x04 93 #define ICLR 0x08 94 #define ICFP 0x10 95 #define ICPR 0x20 96 #define ICCR 0x0c 97 98 #define SA_PIC_SRCS 32 99 100 101 static void strongarm_pic_update(void *opaque) 102 { 103 StrongARMPICState *s = opaque; 104 105 /* FIXME: reflect DIM */ 106 qemu_set_irq(s->fiq, s->pending & s->enabled & s->is_fiq); 107 qemu_set_irq(s->irq, s->pending & s->enabled & ~s->is_fiq); 108 } 109 110 static void strongarm_pic_set_irq(void *opaque, int irq, int level) 111 { 112 StrongARMPICState *s = opaque; 113 114 if (level) { 115 s->pending |= 1 << irq; 116 } else { 117 s->pending &= ~(1 << irq); 118 } 119 120 strongarm_pic_update(s); 121 } 122 123 static uint64_t strongarm_pic_mem_read(void *opaque, hwaddr offset, 124 unsigned size) 125 { 126 StrongARMPICState *s = opaque; 127 128 switch (offset) { 129 case ICIP: 130 return s->pending & ~s->is_fiq & s->enabled; 131 case ICMR: 132 return s->enabled; 133 case ICLR: 134 return s->is_fiq; 135 case ICCR: 136 return s->int_idle == 0; 137 case ICFP: 138 return s->pending & s->is_fiq & s->enabled; 139 case ICPR: 140 return s->pending; 141 default: 142 printf("%s: Bad register offset 0x" TARGET_FMT_plx "\n", 143 __func__, offset); 144 return 0; 145 } 146 } 147 148 static void strongarm_pic_mem_write(void *opaque, hwaddr offset, 149 uint64_t value, unsigned size) 150 { 151 StrongARMPICState *s = opaque; 152 153 switch (offset) { 154 case ICMR: 155 s->enabled = value; 156 break; 157 case ICLR: 158 s->is_fiq = value; 159 break; 160 case ICCR: 161 s->int_idle = (value & 1) ? 0 : ~0; 162 break; 163 default: 164 printf("%s: Bad register offset 0x" TARGET_FMT_plx "\n", 165 __func__, offset); 166 break; 167 } 168 strongarm_pic_update(s); 169 } 170 171 static const MemoryRegionOps strongarm_pic_ops = { 172 .read = strongarm_pic_mem_read, 173 .write = strongarm_pic_mem_write, 174 .endianness = DEVICE_NATIVE_ENDIAN, 175 }; 176 177 static int strongarm_pic_initfn(SysBusDevice *sbd) 178 { 179 DeviceState *dev = DEVICE(sbd); 180 StrongARMPICState *s = STRONGARM_PIC(dev); 181 182 qdev_init_gpio_in(dev, strongarm_pic_set_irq, SA_PIC_SRCS); 183 memory_region_init_io(&s->iomem, OBJECT(s), &strongarm_pic_ops, s, 184 "pic", 0x1000); 185 sysbus_init_mmio(sbd, &s->iomem); 186 sysbus_init_irq(sbd, &s->irq); 187 sysbus_init_irq(sbd, &s->fiq); 188 189 return 0; 190 } 191 192 static int strongarm_pic_post_load(void *opaque, int version_id) 193 { 194 strongarm_pic_update(opaque); 195 return 0; 196 } 197 198 static VMStateDescription vmstate_strongarm_pic_regs = { 199 .name = "strongarm_pic", 200 .version_id = 0, 201 .minimum_version_id = 0, 202 .post_load = strongarm_pic_post_load, 203 .fields = (VMStateField[]) { 204 VMSTATE_UINT32(pending, StrongARMPICState), 205 VMSTATE_UINT32(enabled, StrongARMPICState), 206 VMSTATE_UINT32(is_fiq, StrongARMPICState), 207 VMSTATE_UINT32(int_idle, StrongARMPICState), 208 VMSTATE_END_OF_LIST(), 209 }, 210 }; 211 212 static void strongarm_pic_class_init(ObjectClass *klass, void *data) 213 { 214 DeviceClass *dc = DEVICE_CLASS(klass); 215 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); 216 217 k->init = strongarm_pic_initfn; 218 dc->desc = "StrongARM PIC"; 219 dc->vmsd = &vmstate_strongarm_pic_regs; 220 } 221 222 static const TypeInfo strongarm_pic_info = { 223 .name = TYPE_STRONGARM_PIC, 224 .parent = TYPE_SYS_BUS_DEVICE, 225 .instance_size = sizeof(StrongARMPICState), 226 .class_init = strongarm_pic_class_init, 227 }; 228 229 /* Real-Time Clock */ 230 #define RTAR 0x00 /* RTC Alarm register */ 231 #define RCNR 0x04 /* RTC Counter register */ 232 #define RTTR 0x08 /* RTC Timer Trim register */ 233 #define RTSR 0x10 /* RTC Status register */ 234 235 #define RTSR_AL (1 << 0) /* RTC Alarm detected */ 236 #define RTSR_HZ (1 << 1) /* RTC 1Hz detected */ 237 #define RTSR_ALE (1 << 2) /* RTC Alarm enable */ 238 #define RTSR_HZE (1 << 3) /* RTC 1Hz enable */ 239 240 /* 16 LSB of RTTR are clockdiv for internal trim logic, 241 * trim delete isn't emulated, so 242 * f = 32 768 / (RTTR_trim + 1) */ 243 244 #define TYPE_STRONGARM_RTC "strongarm-rtc" 245 #define STRONGARM_RTC(obj) \ 246 OBJECT_CHECK(StrongARMRTCState, (obj), TYPE_STRONGARM_RTC) 247 248 typedef struct StrongARMRTCState { 249 SysBusDevice parent_obj; 250 251 MemoryRegion iomem; 252 uint32_t rttr; 253 uint32_t rtsr; 254 uint32_t rtar; 255 uint32_t last_rcnr; 256 int64_t last_hz; 257 QEMUTimer *rtc_alarm; 258 QEMUTimer *rtc_hz; 259 qemu_irq rtc_irq; 260 qemu_irq rtc_hz_irq; 261 } StrongARMRTCState; 262 263 static inline void strongarm_rtc_int_update(StrongARMRTCState *s) 264 { 265 qemu_set_irq(s->rtc_irq, s->rtsr & RTSR_AL); 266 qemu_set_irq(s->rtc_hz_irq, s->rtsr & RTSR_HZ); 267 } 268 269 static void strongarm_rtc_hzupdate(StrongARMRTCState *s) 270 { 271 int64_t rt = qemu_clock_get_ms(rtc_clock); 272 s->last_rcnr += ((rt - s->last_hz) << 15) / 273 (1000 * ((s->rttr & 0xffff) + 1)); 274 s->last_hz = rt; 275 } 276 277 static inline void strongarm_rtc_timer_update(StrongARMRTCState *s) 278 { 279 if ((s->rtsr & RTSR_HZE) && !(s->rtsr & RTSR_HZ)) { 280 timer_mod(s->rtc_hz, s->last_hz + 1000); 281 } else { 282 timer_del(s->rtc_hz); 283 } 284 285 if ((s->rtsr & RTSR_ALE) && !(s->rtsr & RTSR_AL)) { 286 timer_mod(s->rtc_alarm, s->last_hz + 287 (((s->rtar - s->last_rcnr) * 1000 * 288 ((s->rttr & 0xffff) + 1)) >> 15)); 289 } else { 290 timer_del(s->rtc_alarm); 291 } 292 } 293 294 static inline void strongarm_rtc_alarm_tick(void *opaque) 295 { 296 StrongARMRTCState *s = opaque; 297 s->rtsr |= RTSR_AL; 298 strongarm_rtc_timer_update(s); 299 strongarm_rtc_int_update(s); 300 } 301 302 static inline void strongarm_rtc_hz_tick(void *opaque) 303 { 304 StrongARMRTCState *s = opaque; 305 s->rtsr |= RTSR_HZ; 306 strongarm_rtc_timer_update(s); 307 strongarm_rtc_int_update(s); 308 } 309 310 static uint64_t strongarm_rtc_read(void *opaque, hwaddr addr, 311 unsigned size) 312 { 313 StrongARMRTCState *s = opaque; 314 315 switch (addr) { 316 case RTTR: 317 return s->rttr; 318 case RTSR: 319 return s->rtsr; 320 case RTAR: 321 return s->rtar; 322 case RCNR: 323 return s->last_rcnr + 324 ((qemu_clock_get_ms(rtc_clock) - s->last_hz) << 15) / 325 (1000 * ((s->rttr & 0xffff) + 1)); 326 default: 327 printf("%s: Bad register 0x" TARGET_FMT_plx "\n", __func__, addr); 328 return 0; 329 } 330 } 331 332 static void strongarm_rtc_write(void *opaque, hwaddr addr, 333 uint64_t value, unsigned size) 334 { 335 StrongARMRTCState *s = opaque; 336 uint32_t old_rtsr; 337 338 switch (addr) { 339 case RTTR: 340 strongarm_rtc_hzupdate(s); 341 s->rttr = value; 342 strongarm_rtc_timer_update(s); 343 break; 344 345 case RTSR: 346 old_rtsr = s->rtsr; 347 s->rtsr = (value & (RTSR_ALE | RTSR_HZE)) | 348 (s->rtsr & ~(value & (RTSR_AL | RTSR_HZ))); 349 350 if (s->rtsr != old_rtsr) { 351 strongarm_rtc_timer_update(s); 352 } 353 354 strongarm_rtc_int_update(s); 355 break; 356 357 case RTAR: 358 s->rtar = value; 359 strongarm_rtc_timer_update(s); 360 break; 361 362 case RCNR: 363 strongarm_rtc_hzupdate(s); 364 s->last_rcnr = value; 365 strongarm_rtc_timer_update(s); 366 break; 367 368 default: 369 printf("%s: Bad register 0x" TARGET_FMT_plx "\n", __func__, addr); 370 } 371 } 372 373 static const MemoryRegionOps strongarm_rtc_ops = { 374 .read = strongarm_rtc_read, 375 .write = strongarm_rtc_write, 376 .endianness = DEVICE_NATIVE_ENDIAN, 377 }; 378 379 static int strongarm_rtc_init(SysBusDevice *dev) 380 { 381 StrongARMRTCState *s = STRONGARM_RTC(dev); 382 struct tm tm; 383 384 s->rttr = 0x0; 385 s->rtsr = 0; 386 387 qemu_get_timedate(&tm, 0); 388 389 s->last_rcnr = (uint32_t) mktimegm(&tm); 390 s->last_hz = qemu_clock_get_ms(rtc_clock); 391 392 s->rtc_alarm = timer_new_ms(rtc_clock, strongarm_rtc_alarm_tick, s); 393 s->rtc_hz = timer_new_ms(rtc_clock, strongarm_rtc_hz_tick, s); 394 395 sysbus_init_irq(dev, &s->rtc_irq); 396 sysbus_init_irq(dev, &s->rtc_hz_irq); 397 398 memory_region_init_io(&s->iomem, OBJECT(s), &strongarm_rtc_ops, s, 399 "rtc", 0x10000); 400 sysbus_init_mmio(dev, &s->iomem); 401 402 return 0; 403 } 404 405 static void strongarm_rtc_pre_save(void *opaque) 406 { 407 StrongARMRTCState *s = opaque; 408 409 strongarm_rtc_hzupdate(s); 410 } 411 412 static int strongarm_rtc_post_load(void *opaque, int version_id) 413 { 414 StrongARMRTCState *s = opaque; 415 416 strongarm_rtc_timer_update(s); 417 strongarm_rtc_int_update(s); 418 419 return 0; 420 } 421 422 static const VMStateDescription vmstate_strongarm_rtc_regs = { 423 .name = "strongarm-rtc", 424 .version_id = 0, 425 .minimum_version_id = 0, 426 .pre_save = strongarm_rtc_pre_save, 427 .post_load = strongarm_rtc_post_load, 428 .fields = (VMStateField[]) { 429 VMSTATE_UINT32(rttr, StrongARMRTCState), 430 VMSTATE_UINT32(rtsr, StrongARMRTCState), 431 VMSTATE_UINT32(rtar, StrongARMRTCState), 432 VMSTATE_UINT32(last_rcnr, StrongARMRTCState), 433 VMSTATE_INT64(last_hz, StrongARMRTCState), 434 VMSTATE_END_OF_LIST(), 435 }, 436 }; 437 438 static void strongarm_rtc_sysbus_class_init(ObjectClass *klass, void *data) 439 { 440 DeviceClass *dc = DEVICE_CLASS(klass); 441 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); 442 443 k->init = strongarm_rtc_init; 444 dc->desc = "StrongARM RTC Controller"; 445 dc->vmsd = &vmstate_strongarm_rtc_regs; 446 } 447 448 static const TypeInfo strongarm_rtc_sysbus_info = { 449 .name = TYPE_STRONGARM_RTC, 450 .parent = TYPE_SYS_BUS_DEVICE, 451 .instance_size = sizeof(StrongARMRTCState), 452 .class_init = strongarm_rtc_sysbus_class_init, 453 }; 454 455 /* GPIO */ 456 #define GPLR 0x00 457 #define GPDR 0x04 458 #define GPSR 0x08 459 #define GPCR 0x0c 460 #define GRER 0x10 461 #define GFER 0x14 462 #define GEDR 0x18 463 #define GAFR 0x1c 464 465 #define TYPE_STRONGARM_GPIO "strongarm-gpio" 466 #define STRONGARM_GPIO(obj) \ 467 OBJECT_CHECK(StrongARMGPIOInfo, (obj), TYPE_STRONGARM_GPIO) 468 469 typedef struct StrongARMGPIOInfo StrongARMGPIOInfo; 470 struct StrongARMGPIOInfo { 471 SysBusDevice busdev; 472 MemoryRegion iomem; 473 qemu_irq handler[28]; 474 qemu_irq irqs[11]; 475 qemu_irq irqX; 476 477 uint32_t ilevel; 478 uint32_t olevel; 479 uint32_t dir; 480 uint32_t rising; 481 uint32_t falling; 482 uint32_t status; 483 uint32_t gafr; 484 485 uint32_t prev_level; 486 }; 487 488 489 static void strongarm_gpio_irq_update(StrongARMGPIOInfo *s) 490 { 491 int i; 492 for (i = 0; i < 11; i++) { 493 qemu_set_irq(s->irqs[i], s->status & (1 << i)); 494 } 495 496 qemu_set_irq(s->irqX, (s->status & ~0x7ff)); 497 } 498 499 static void strongarm_gpio_set(void *opaque, int line, int level) 500 { 501 StrongARMGPIOInfo *s = opaque; 502 uint32_t mask; 503 504 mask = 1 << line; 505 506 if (level) { 507 s->status |= s->rising & mask & 508 ~s->ilevel & ~s->dir; 509 s->ilevel |= mask; 510 } else { 511 s->status |= s->falling & mask & 512 s->ilevel & ~s->dir; 513 s->ilevel &= ~mask; 514 } 515 516 if (s->status & mask) { 517 strongarm_gpio_irq_update(s); 518 } 519 } 520 521 static void strongarm_gpio_handler_update(StrongARMGPIOInfo *s) 522 { 523 uint32_t level, diff; 524 int bit; 525 526 level = s->olevel & s->dir; 527 528 for (diff = s->prev_level ^ level; diff; diff ^= 1 << bit) { 529 bit = ffs(diff) - 1; 530 qemu_set_irq(s->handler[bit], (level >> bit) & 1); 531 } 532 533 s->prev_level = level; 534 } 535 536 static uint64_t strongarm_gpio_read(void *opaque, hwaddr offset, 537 unsigned size) 538 { 539 StrongARMGPIOInfo *s = opaque; 540 541 switch (offset) { 542 case GPDR: /* GPIO Pin-Direction registers */ 543 return s->dir; 544 545 case GPSR: /* GPIO Pin-Output Set registers */ 546 qemu_log_mask(LOG_GUEST_ERROR, 547 "strongarm GPIO: read from write only register GPSR\n"); 548 return 0; 549 550 case GPCR: /* GPIO Pin-Output Clear registers */ 551 qemu_log_mask(LOG_GUEST_ERROR, 552 "strongarm GPIO: read from write only register GPCR\n"); 553 return 0; 554 555 case GRER: /* GPIO Rising-Edge Detect Enable registers */ 556 return s->rising; 557 558 case GFER: /* GPIO Falling-Edge Detect Enable registers */ 559 return s->falling; 560 561 case GAFR: /* GPIO Alternate Function registers */ 562 return s->gafr; 563 564 case GPLR: /* GPIO Pin-Level registers */ 565 return (s->olevel & s->dir) | 566 (s->ilevel & ~s->dir); 567 568 case GEDR: /* GPIO Edge Detect Status registers */ 569 return s->status; 570 571 default: 572 printf("%s: Bad offset 0x" TARGET_FMT_plx "\n", __func__, offset); 573 } 574 575 return 0; 576 } 577 578 static void strongarm_gpio_write(void *opaque, hwaddr offset, 579 uint64_t value, unsigned size) 580 { 581 StrongARMGPIOInfo *s = opaque; 582 583 switch (offset) { 584 case GPDR: /* GPIO Pin-Direction registers */ 585 s->dir = value; 586 strongarm_gpio_handler_update(s); 587 break; 588 589 case GPSR: /* GPIO Pin-Output Set registers */ 590 s->olevel |= value; 591 strongarm_gpio_handler_update(s); 592 break; 593 594 case GPCR: /* GPIO Pin-Output Clear registers */ 595 s->olevel &= ~value; 596 strongarm_gpio_handler_update(s); 597 break; 598 599 case GRER: /* GPIO Rising-Edge Detect Enable registers */ 600 s->rising = value; 601 break; 602 603 case GFER: /* GPIO Falling-Edge Detect Enable registers */ 604 s->falling = value; 605 break; 606 607 case GAFR: /* GPIO Alternate Function registers */ 608 s->gafr = value; 609 break; 610 611 case GEDR: /* GPIO Edge Detect Status registers */ 612 s->status &= ~value; 613 strongarm_gpio_irq_update(s); 614 break; 615 616 default: 617 printf("%s: Bad offset 0x" TARGET_FMT_plx "\n", __func__, offset); 618 } 619 } 620 621 static const MemoryRegionOps strongarm_gpio_ops = { 622 .read = strongarm_gpio_read, 623 .write = strongarm_gpio_write, 624 .endianness = DEVICE_NATIVE_ENDIAN, 625 }; 626 627 static DeviceState *strongarm_gpio_init(hwaddr base, 628 DeviceState *pic) 629 { 630 DeviceState *dev; 631 int i; 632 633 dev = qdev_create(NULL, TYPE_STRONGARM_GPIO); 634 qdev_init_nofail(dev); 635 636 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base); 637 for (i = 0; i < 12; i++) 638 sysbus_connect_irq(SYS_BUS_DEVICE(dev), i, 639 qdev_get_gpio_in(pic, SA_PIC_GPIO0_EDGE + i)); 640 641 return dev; 642 } 643 644 static int strongarm_gpio_initfn(SysBusDevice *sbd) 645 { 646 DeviceState *dev = DEVICE(sbd); 647 StrongARMGPIOInfo *s = STRONGARM_GPIO(dev); 648 int i; 649 650 qdev_init_gpio_in(dev, strongarm_gpio_set, 28); 651 qdev_init_gpio_out(dev, s->handler, 28); 652 653 memory_region_init_io(&s->iomem, OBJECT(s), &strongarm_gpio_ops, s, 654 "gpio", 0x1000); 655 656 sysbus_init_mmio(sbd, &s->iomem); 657 for (i = 0; i < 11; i++) { 658 sysbus_init_irq(sbd, &s->irqs[i]); 659 } 660 sysbus_init_irq(sbd, &s->irqX); 661 662 return 0; 663 } 664 665 static const VMStateDescription vmstate_strongarm_gpio_regs = { 666 .name = "strongarm-gpio", 667 .version_id = 0, 668 .minimum_version_id = 0, 669 .fields = (VMStateField[]) { 670 VMSTATE_UINT32(ilevel, StrongARMGPIOInfo), 671 VMSTATE_UINT32(olevel, StrongARMGPIOInfo), 672 VMSTATE_UINT32(dir, StrongARMGPIOInfo), 673 VMSTATE_UINT32(rising, StrongARMGPIOInfo), 674 VMSTATE_UINT32(falling, StrongARMGPIOInfo), 675 VMSTATE_UINT32(status, StrongARMGPIOInfo), 676 VMSTATE_UINT32(gafr, StrongARMGPIOInfo), 677 VMSTATE_END_OF_LIST(), 678 }, 679 }; 680 681 static void strongarm_gpio_class_init(ObjectClass *klass, void *data) 682 { 683 DeviceClass *dc = DEVICE_CLASS(klass); 684 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); 685 686 k->init = strongarm_gpio_initfn; 687 dc->desc = "StrongARM GPIO controller"; 688 } 689 690 static const TypeInfo strongarm_gpio_info = { 691 .name = TYPE_STRONGARM_GPIO, 692 .parent = TYPE_SYS_BUS_DEVICE, 693 .instance_size = sizeof(StrongARMGPIOInfo), 694 .class_init = strongarm_gpio_class_init, 695 }; 696 697 /* Peripheral Pin Controller */ 698 #define PPDR 0x00 699 #define PPSR 0x04 700 #define PPAR 0x08 701 #define PSDR 0x0c 702 #define PPFR 0x10 703 704 #define TYPE_STRONGARM_PPC "strongarm-ppc" 705 #define STRONGARM_PPC(obj) \ 706 OBJECT_CHECK(StrongARMPPCInfo, (obj), TYPE_STRONGARM_PPC) 707 708 typedef struct StrongARMPPCInfo StrongARMPPCInfo; 709 struct StrongARMPPCInfo { 710 SysBusDevice parent_obj; 711 712 MemoryRegion iomem; 713 qemu_irq handler[28]; 714 715 uint32_t ilevel; 716 uint32_t olevel; 717 uint32_t dir; 718 uint32_t ppar; 719 uint32_t psdr; 720 uint32_t ppfr; 721 722 uint32_t prev_level; 723 }; 724 725 static void strongarm_ppc_set(void *opaque, int line, int level) 726 { 727 StrongARMPPCInfo *s = opaque; 728 729 if (level) { 730 s->ilevel |= 1 << line; 731 } else { 732 s->ilevel &= ~(1 << line); 733 } 734 } 735 736 static void strongarm_ppc_handler_update(StrongARMPPCInfo *s) 737 { 738 uint32_t level, diff; 739 int bit; 740 741 level = s->olevel & s->dir; 742 743 for (diff = s->prev_level ^ level; diff; diff ^= 1 << bit) { 744 bit = ffs(diff) - 1; 745 qemu_set_irq(s->handler[bit], (level >> bit) & 1); 746 } 747 748 s->prev_level = level; 749 } 750 751 static uint64_t strongarm_ppc_read(void *opaque, hwaddr offset, 752 unsigned size) 753 { 754 StrongARMPPCInfo *s = opaque; 755 756 switch (offset) { 757 case PPDR: /* PPC Pin Direction registers */ 758 return s->dir | ~0x3fffff; 759 760 case PPSR: /* PPC Pin State registers */ 761 return (s->olevel & s->dir) | 762 (s->ilevel & ~s->dir) | 763 ~0x3fffff; 764 765 case PPAR: 766 return s->ppar | ~0x41000; 767 768 case PSDR: 769 return s->psdr; 770 771 case PPFR: 772 return s->ppfr | ~0x7f001; 773 774 default: 775 printf("%s: Bad offset 0x" TARGET_FMT_plx "\n", __func__, offset); 776 } 777 778 return 0; 779 } 780 781 static void strongarm_ppc_write(void *opaque, hwaddr offset, 782 uint64_t value, unsigned size) 783 { 784 StrongARMPPCInfo *s = opaque; 785 786 switch (offset) { 787 case PPDR: /* PPC Pin Direction registers */ 788 s->dir = value & 0x3fffff; 789 strongarm_ppc_handler_update(s); 790 break; 791 792 case PPSR: /* PPC Pin State registers */ 793 s->olevel = value & s->dir & 0x3fffff; 794 strongarm_ppc_handler_update(s); 795 break; 796 797 case PPAR: 798 s->ppar = value & 0x41000; 799 break; 800 801 case PSDR: 802 s->psdr = value & 0x3fffff; 803 break; 804 805 case PPFR: 806 s->ppfr = value & 0x7f001; 807 break; 808 809 default: 810 printf("%s: Bad offset 0x" TARGET_FMT_plx "\n", __func__, offset); 811 } 812 } 813 814 static const MemoryRegionOps strongarm_ppc_ops = { 815 .read = strongarm_ppc_read, 816 .write = strongarm_ppc_write, 817 .endianness = DEVICE_NATIVE_ENDIAN, 818 }; 819 820 static int strongarm_ppc_init(SysBusDevice *sbd) 821 { 822 DeviceState *dev = DEVICE(sbd); 823 StrongARMPPCInfo *s = STRONGARM_PPC(dev); 824 825 qdev_init_gpio_in(dev, strongarm_ppc_set, 22); 826 qdev_init_gpio_out(dev, s->handler, 22); 827 828 memory_region_init_io(&s->iomem, OBJECT(s), &strongarm_ppc_ops, s, 829 "ppc", 0x1000); 830 831 sysbus_init_mmio(sbd, &s->iomem); 832 833 return 0; 834 } 835 836 static const VMStateDescription vmstate_strongarm_ppc_regs = { 837 .name = "strongarm-ppc", 838 .version_id = 0, 839 .minimum_version_id = 0, 840 .fields = (VMStateField[]) { 841 VMSTATE_UINT32(ilevel, StrongARMPPCInfo), 842 VMSTATE_UINT32(olevel, StrongARMPPCInfo), 843 VMSTATE_UINT32(dir, StrongARMPPCInfo), 844 VMSTATE_UINT32(ppar, StrongARMPPCInfo), 845 VMSTATE_UINT32(psdr, StrongARMPPCInfo), 846 VMSTATE_UINT32(ppfr, StrongARMPPCInfo), 847 VMSTATE_END_OF_LIST(), 848 }, 849 }; 850 851 static void strongarm_ppc_class_init(ObjectClass *klass, void *data) 852 { 853 DeviceClass *dc = DEVICE_CLASS(klass); 854 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); 855 856 k->init = strongarm_ppc_init; 857 dc->desc = "StrongARM PPC controller"; 858 } 859 860 static const TypeInfo strongarm_ppc_info = { 861 .name = TYPE_STRONGARM_PPC, 862 .parent = TYPE_SYS_BUS_DEVICE, 863 .instance_size = sizeof(StrongARMPPCInfo), 864 .class_init = strongarm_ppc_class_init, 865 }; 866 867 /* UART Ports */ 868 #define UTCR0 0x00 869 #define UTCR1 0x04 870 #define UTCR2 0x08 871 #define UTCR3 0x0c 872 #define UTDR 0x14 873 #define UTSR0 0x1c 874 #define UTSR1 0x20 875 876 #define UTCR0_PE (1 << 0) /* Parity enable */ 877 #define UTCR0_OES (1 << 1) /* Even parity */ 878 #define UTCR0_SBS (1 << 2) /* 2 stop bits */ 879 #define UTCR0_DSS (1 << 3) /* 8-bit data */ 880 881 #define UTCR3_RXE (1 << 0) /* Rx enable */ 882 #define UTCR3_TXE (1 << 1) /* Tx enable */ 883 #define UTCR3_BRK (1 << 2) /* Force Break */ 884 #define UTCR3_RIE (1 << 3) /* Rx int enable */ 885 #define UTCR3_TIE (1 << 4) /* Tx int enable */ 886 #define UTCR3_LBM (1 << 5) /* Loopback */ 887 888 #define UTSR0_TFS (1 << 0) /* Tx FIFO nearly empty */ 889 #define UTSR0_RFS (1 << 1) /* Rx FIFO nearly full */ 890 #define UTSR0_RID (1 << 2) /* Receiver Idle */ 891 #define UTSR0_RBB (1 << 3) /* Receiver begin break */ 892 #define UTSR0_REB (1 << 4) /* Receiver end break */ 893 #define UTSR0_EIF (1 << 5) /* Error in FIFO */ 894 895 #define UTSR1_RNE (1 << 1) /* Receive FIFO not empty */ 896 #define UTSR1_TNF (1 << 2) /* Transmit FIFO not full */ 897 #define UTSR1_PRE (1 << 3) /* Parity error */ 898 #define UTSR1_FRE (1 << 4) /* Frame error */ 899 #define UTSR1_ROR (1 << 5) /* Receive Over Run */ 900 901 #define RX_FIFO_PRE (1 << 8) 902 #define RX_FIFO_FRE (1 << 9) 903 #define RX_FIFO_ROR (1 << 10) 904 905 #define TYPE_STRONGARM_UART "strongarm-uart" 906 #define STRONGARM_UART(obj) \ 907 OBJECT_CHECK(StrongARMUARTState, (obj), TYPE_STRONGARM_UART) 908 909 typedef struct StrongARMUARTState { 910 SysBusDevice parent_obj; 911 912 MemoryRegion iomem; 913 CharDriverState *chr; 914 qemu_irq irq; 915 916 uint8_t utcr0; 917 uint16_t brd; 918 uint8_t utcr3; 919 uint8_t utsr0; 920 uint8_t utsr1; 921 922 uint8_t tx_fifo[8]; 923 uint8_t tx_start; 924 uint8_t tx_len; 925 uint16_t rx_fifo[12]; /* value + error flags in high bits */ 926 uint8_t rx_start; 927 uint8_t rx_len; 928 929 uint64_t char_transmit_time; /* time to transmit a char in ticks*/ 930 bool wait_break_end; 931 QEMUTimer *rx_timeout_timer; 932 QEMUTimer *tx_timer; 933 } StrongARMUARTState; 934 935 static void strongarm_uart_update_status(StrongARMUARTState *s) 936 { 937 uint16_t utsr1 = 0; 938 939 if (s->tx_len != 8) { 940 utsr1 |= UTSR1_TNF; 941 } 942 943 if (s->rx_len != 0) { 944 uint16_t ent = s->rx_fifo[s->rx_start]; 945 946 utsr1 |= UTSR1_RNE; 947 if (ent & RX_FIFO_PRE) { 948 s->utsr1 |= UTSR1_PRE; 949 } 950 if (ent & RX_FIFO_FRE) { 951 s->utsr1 |= UTSR1_FRE; 952 } 953 if (ent & RX_FIFO_ROR) { 954 s->utsr1 |= UTSR1_ROR; 955 } 956 } 957 958 s->utsr1 = utsr1; 959 } 960 961 static void strongarm_uart_update_int_status(StrongARMUARTState *s) 962 { 963 uint16_t utsr0 = s->utsr0 & 964 (UTSR0_REB | UTSR0_RBB | UTSR0_RID); 965 int i; 966 967 if ((s->utcr3 & UTCR3_TXE) && 968 (s->utcr3 & UTCR3_TIE) && 969 s->tx_len <= 4) { 970 utsr0 |= UTSR0_TFS; 971 } 972 973 if ((s->utcr3 & UTCR3_RXE) && 974 (s->utcr3 & UTCR3_RIE) && 975 s->rx_len > 4) { 976 utsr0 |= UTSR0_RFS; 977 } 978 979 for (i = 0; i < s->rx_len && i < 4; i++) 980 if (s->rx_fifo[(s->rx_start + i) % 12] & ~0xff) { 981 utsr0 |= UTSR0_EIF; 982 break; 983 } 984 985 s->utsr0 = utsr0; 986 qemu_set_irq(s->irq, utsr0); 987 } 988 989 static void strongarm_uart_update_parameters(StrongARMUARTState *s) 990 { 991 int speed, parity, data_bits, stop_bits, frame_size; 992 QEMUSerialSetParams ssp; 993 994 /* Start bit. */ 995 frame_size = 1; 996 if (s->utcr0 & UTCR0_PE) { 997 /* Parity bit. */ 998 frame_size++; 999 if (s->utcr0 & UTCR0_OES) { 1000 parity = 'E'; 1001 } else { 1002 parity = 'O'; 1003 } 1004 } else { 1005 parity = 'N'; 1006 } 1007 if (s->utcr0 & UTCR0_SBS) { 1008 stop_bits = 2; 1009 } else { 1010 stop_bits = 1; 1011 } 1012 1013 data_bits = (s->utcr0 & UTCR0_DSS) ? 8 : 7; 1014 frame_size += data_bits + stop_bits; 1015 speed = 3686400 / 16 / (s->brd + 1); 1016 ssp.speed = speed; 1017 ssp.parity = parity; 1018 ssp.data_bits = data_bits; 1019 ssp.stop_bits = stop_bits; 1020 s->char_transmit_time = (get_ticks_per_sec() / speed) * frame_size; 1021 if (s->chr) { 1022 qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp); 1023 } 1024 1025 DPRINTF(stderr, "%s speed=%d parity=%c data=%d stop=%d\n", s->chr->label, 1026 speed, parity, data_bits, stop_bits); 1027 } 1028 1029 static void strongarm_uart_rx_to(void *opaque) 1030 { 1031 StrongARMUARTState *s = opaque; 1032 1033 if (s->rx_len) { 1034 s->utsr0 |= UTSR0_RID; 1035 strongarm_uart_update_int_status(s); 1036 } 1037 } 1038 1039 static void strongarm_uart_rx_push(StrongARMUARTState *s, uint16_t c) 1040 { 1041 if ((s->utcr3 & UTCR3_RXE) == 0) { 1042 /* rx disabled */ 1043 return; 1044 } 1045 1046 if (s->wait_break_end) { 1047 s->utsr0 |= UTSR0_REB; 1048 s->wait_break_end = false; 1049 } 1050 1051 if (s->rx_len < 12) { 1052 s->rx_fifo[(s->rx_start + s->rx_len) % 12] = c; 1053 s->rx_len++; 1054 } else 1055 s->rx_fifo[(s->rx_start + 11) % 12] |= RX_FIFO_ROR; 1056 } 1057 1058 static int strongarm_uart_can_receive(void *opaque) 1059 { 1060 StrongARMUARTState *s = opaque; 1061 1062 if (s->rx_len == 12) { 1063 return 0; 1064 } 1065 /* It's best not to get more than 2/3 of RX FIFO, so advertise that much */ 1066 if (s->rx_len < 8) { 1067 return 8 - s->rx_len; 1068 } 1069 return 1; 1070 } 1071 1072 static void strongarm_uart_receive(void *opaque, const uint8_t *buf, int size) 1073 { 1074 StrongARMUARTState *s = opaque; 1075 int i; 1076 1077 for (i = 0; i < size; i++) { 1078 strongarm_uart_rx_push(s, buf[i]); 1079 } 1080 1081 /* call the timeout receive callback in 3 char transmit time */ 1082 timer_mod(s->rx_timeout_timer, 1083 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->char_transmit_time * 3); 1084 1085 strongarm_uart_update_status(s); 1086 strongarm_uart_update_int_status(s); 1087 } 1088 1089 static void strongarm_uart_event(void *opaque, int event) 1090 { 1091 StrongARMUARTState *s = opaque; 1092 if (event == CHR_EVENT_BREAK) { 1093 s->utsr0 |= UTSR0_RBB; 1094 strongarm_uart_rx_push(s, RX_FIFO_FRE); 1095 s->wait_break_end = true; 1096 strongarm_uart_update_status(s); 1097 strongarm_uart_update_int_status(s); 1098 } 1099 } 1100 1101 static void strongarm_uart_tx(void *opaque) 1102 { 1103 StrongARMUARTState *s = opaque; 1104 uint64_t new_xmit_ts = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1105 1106 if (s->utcr3 & UTCR3_LBM) /* loopback */ { 1107 strongarm_uart_receive(s, &s->tx_fifo[s->tx_start], 1); 1108 } else if (s->chr) { 1109 qemu_chr_fe_write(s->chr, &s->tx_fifo[s->tx_start], 1); 1110 } 1111 1112 s->tx_start = (s->tx_start + 1) % 8; 1113 s->tx_len--; 1114 if (s->tx_len) { 1115 timer_mod(s->tx_timer, new_xmit_ts + s->char_transmit_time); 1116 } 1117 strongarm_uart_update_status(s); 1118 strongarm_uart_update_int_status(s); 1119 } 1120 1121 static uint64_t strongarm_uart_read(void *opaque, hwaddr addr, 1122 unsigned size) 1123 { 1124 StrongARMUARTState *s = opaque; 1125 uint16_t ret; 1126 1127 switch (addr) { 1128 case UTCR0: 1129 return s->utcr0; 1130 1131 case UTCR1: 1132 return s->brd >> 8; 1133 1134 case UTCR2: 1135 return s->brd & 0xff; 1136 1137 case UTCR3: 1138 return s->utcr3; 1139 1140 case UTDR: 1141 if (s->rx_len != 0) { 1142 ret = s->rx_fifo[s->rx_start]; 1143 s->rx_start = (s->rx_start + 1) % 12; 1144 s->rx_len--; 1145 strongarm_uart_update_status(s); 1146 strongarm_uart_update_int_status(s); 1147 return ret; 1148 } 1149 return 0; 1150 1151 case UTSR0: 1152 return s->utsr0; 1153 1154 case UTSR1: 1155 return s->utsr1; 1156 1157 default: 1158 printf("%s: Bad register 0x" TARGET_FMT_plx "\n", __func__, addr); 1159 return 0; 1160 } 1161 } 1162 1163 static void strongarm_uart_write(void *opaque, hwaddr addr, 1164 uint64_t value, unsigned size) 1165 { 1166 StrongARMUARTState *s = opaque; 1167 1168 switch (addr) { 1169 case UTCR0: 1170 s->utcr0 = value & 0x7f; 1171 strongarm_uart_update_parameters(s); 1172 break; 1173 1174 case UTCR1: 1175 s->brd = (s->brd & 0xff) | ((value & 0xf) << 8); 1176 strongarm_uart_update_parameters(s); 1177 break; 1178 1179 case UTCR2: 1180 s->brd = (s->brd & 0xf00) | (value & 0xff); 1181 strongarm_uart_update_parameters(s); 1182 break; 1183 1184 case UTCR3: 1185 s->utcr3 = value & 0x3f; 1186 if ((s->utcr3 & UTCR3_RXE) == 0) { 1187 s->rx_len = 0; 1188 } 1189 if ((s->utcr3 & UTCR3_TXE) == 0) { 1190 s->tx_len = 0; 1191 } 1192 strongarm_uart_update_status(s); 1193 strongarm_uart_update_int_status(s); 1194 break; 1195 1196 case UTDR: 1197 if ((s->utcr3 & UTCR3_TXE) && s->tx_len != 8) { 1198 s->tx_fifo[(s->tx_start + s->tx_len) % 8] = value; 1199 s->tx_len++; 1200 strongarm_uart_update_status(s); 1201 strongarm_uart_update_int_status(s); 1202 if (s->tx_len == 1) { 1203 strongarm_uart_tx(s); 1204 } 1205 } 1206 break; 1207 1208 case UTSR0: 1209 s->utsr0 = s->utsr0 & ~(value & 1210 (UTSR0_REB | UTSR0_RBB | UTSR0_RID)); 1211 strongarm_uart_update_int_status(s); 1212 break; 1213 1214 default: 1215 printf("%s: Bad register 0x" TARGET_FMT_plx "\n", __func__, addr); 1216 } 1217 } 1218 1219 static const MemoryRegionOps strongarm_uart_ops = { 1220 .read = strongarm_uart_read, 1221 .write = strongarm_uart_write, 1222 .endianness = DEVICE_NATIVE_ENDIAN, 1223 }; 1224 1225 static int strongarm_uart_init(SysBusDevice *dev) 1226 { 1227 StrongARMUARTState *s = STRONGARM_UART(dev); 1228 1229 memory_region_init_io(&s->iomem, OBJECT(s), &strongarm_uart_ops, s, 1230 "uart", 0x10000); 1231 sysbus_init_mmio(dev, &s->iomem); 1232 sysbus_init_irq(dev, &s->irq); 1233 1234 s->rx_timeout_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, strongarm_uart_rx_to, s); 1235 s->tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, strongarm_uart_tx, s); 1236 1237 if (s->chr) { 1238 qemu_chr_add_handlers(s->chr, 1239 strongarm_uart_can_receive, 1240 strongarm_uart_receive, 1241 strongarm_uart_event, 1242 s); 1243 } 1244 1245 return 0; 1246 } 1247 1248 static void strongarm_uart_reset(DeviceState *dev) 1249 { 1250 StrongARMUARTState *s = STRONGARM_UART(dev); 1251 1252 s->utcr0 = UTCR0_DSS; /* 8 data, no parity */ 1253 s->brd = 23; /* 9600 */ 1254 /* enable send & recv - this actually violates spec */ 1255 s->utcr3 = UTCR3_TXE | UTCR3_RXE; 1256 1257 s->rx_len = s->tx_len = 0; 1258 1259 strongarm_uart_update_parameters(s); 1260 strongarm_uart_update_status(s); 1261 strongarm_uart_update_int_status(s); 1262 } 1263 1264 static int strongarm_uart_post_load(void *opaque, int version_id) 1265 { 1266 StrongARMUARTState *s = opaque; 1267 1268 strongarm_uart_update_parameters(s); 1269 strongarm_uart_update_status(s); 1270 strongarm_uart_update_int_status(s); 1271 1272 /* tx and restart timer */ 1273 if (s->tx_len) { 1274 strongarm_uart_tx(s); 1275 } 1276 1277 /* restart rx timeout timer */ 1278 if (s->rx_len) { 1279 timer_mod(s->rx_timeout_timer, 1280 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->char_transmit_time * 3); 1281 } 1282 1283 return 0; 1284 } 1285 1286 static const VMStateDescription vmstate_strongarm_uart_regs = { 1287 .name = "strongarm-uart", 1288 .version_id = 0, 1289 .minimum_version_id = 0, 1290 .post_load = strongarm_uart_post_load, 1291 .fields = (VMStateField[]) { 1292 VMSTATE_UINT8(utcr0, StrongARMUARTState), 1293 VMSTATE_UINT16(brd, StrongARMUARTState), 1294 VMSTATE_UINT8(utcr3, StrongARMUARTState), 1295 VMSTATE_UINT8(utsr0, StrongARMUARTState), 1296 VMSTATE_UINT8_ARRAY(tx_fifo, StrongARMUARTState, 8), 1297 VMSTATE_UINT8(tx_start, StrongARMUARTState), 1298 VMSTATE_UINT8(tx_len, StrongARMUARTState), 1299 VMSTATE_UINT16_ARRAY(rx_fifo, StrongARMUARTState, 12), 1300 VMSTATE_UINT8(rx_start, StrongARMUARTState), 1301 VMSTATE_UINT8(rx_len, StrongARMUARTState), 1302 VMSTATE_BOOL(wait_break_end, StrongARMUARTState), 1303 VMSTATE_END_OF_LIST(), 1304 }, 1305 }; 1306 1307 static Property strongarm_uart_properties[] = { 1308 DEFINE_PROP_CHR("chardev", StrongARMUARTState, chr), 1309 DEFINE_PROP_END_OF_LIST(), 1310 }; 1311 1312 static void strongarm_uart_class_init(ObjectClass *klass, void *data) 1313 { 1314 DeviceClass *dc = DEVICE_CLASS(klass); 1315 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); 1316 1317 k->init = strongarm_uart_init; 1318 dc->desc = "StrongARM UART controller"; 1319 dc->reset = strongarm_uart_reset; 1320 dc->vmsd = &vmstate_strongarm_uart_regs; 1321 dc->props = strongarm_uart_properties; 1322 } 1323 1324 static const TypeInfo strongarm_uart_info = { 1325 .name = TYPE_STRONGARM_UART, 1326 .parent = TYPE_SYS_BUS_DEVICE, 1327 .instance_size = sizeof(StrongARMUARTState), 1328 .class_init = strongarm_uart_class_init, 1329 }; 1330 1331 /* Synchronous Serial Ports */ 1332 1333 #define TYPE_STRONGARM_SSP "strongarm-ssp" 1334 #define STRONGARM_SSP(obj) \ 1335 OBJECT_CHECK(StrongARMSSPState, (obj), TYPE_STRONGARM_SSP) 1336 1337 typedef struct StrongARMSSPState { 1338 SysBusDevice parent_obj; 1339 1340 MemoryRegion iomem; 1341 qemu_irq irq; 1342 SSIBus *bus; 1343 1344 uint16_t sscr[2]; 1345 uint16_t sssr; 1346 1347 uint16_t rx_fifo[8]; 1348 uint8_t rx_level; 1349 uint8_t rx_start; 1350 } StrongARMSSPState; 1351 1352 #define SSCR0 0x60 /* SSP Control register 0 */ 1353 #define SSCR1 0x64 /* SSP Control register 1 */ 1354 #define SSDR 0x6c /* SSP Data register */ 1355 #define SSSR 0x74 /* SSP Status register */ 1356 1357 /* Bitfields for above registers */ 1358 #define SSCR0_SPI(x) (((x) & 0x30) == 0x00) 1359 #define SSCR0_SSP(x) (((x) & 0x30) == 0x10) 1360 #define SSCR0_UWIRE(x) (((x) & 0x30) == 0x20) 1361 #define SSCR0_PSP(x) (((x) & 0x30) == 0x30) 1362 #define SSCR0_SSE (1 << 7) 1363 #define SSCR0_DSS(x) (((x) & 0xf) + 1) 1364 #define SSCR1_RIE (1 << 0) 1365 #define SSCR1_TIE (1 << 1) 1366 #define SSCR1_LBM (1 << 2) 1367 #define SSSR_TNF (1 << 2) 1368 #define SSSR_RNE (1 << 3) 1369 #define SSSR_TFS (1 << 5) 1370 #define SSSR_RFS (1 << 6) 1371 #define SSSR_ROR (1 << 7) 1372 #define SSSR_RW 0x0080 1373 1374 static void strongarm_ssp_int_update(StrongARMSSPState *s) 1375 { 1376 int level = 0; 1377 1378 level |= (s->sssr & SSSR_ROR); 1379 level |= (s->sssr & SSSR_RFS) && (s->sscr[1] & SSCR1_RIE); 1380 level |= (s->sssr & SSSR_TFS) && (s->sscr[1] & SSCR1_TIE); 1381 qemu_set_irq(s->irq, level); 1382 } 1383 1384 static void strongarm_ssp_fifo_update(StrongARMSSPState *s) 1385 { 1386 s->sssr &= ~SSSR_TFS; 1387 s->sssr &= ~SSSR_TNF; 1388 if (s->sscr[0] & SSCR0_SSE) { 1389 if (s->rx_level >= 4) { 1390 s->sssr |= SSSR_RFS; 1391 } else { 1392 s->sssr &= ~SSSR_RFS; 1393 } 1394 if (s->rx_level) { 1395 s->sssr |= SSSR_RNE; 1396 } else { 1397 s->sssr &= ~SSSR_RNE; 1398 } 1399 /* TX FIFO is never filled, so it is always in underrun 1400 condition if SSP is enabled */ 1401 s->sssr |= SSSR_TFS; 1402 s->sssr |= SSSR_TNF; 1403 } 1404 1405 strongarm_ssp_int_update(s); 1406 } 1407 1408 static uint64_t strongarm_ssp_read(void *opaque, hwaddr addr, 1409 unsigned size) 1410 { 1411 StrongARMSSPState *s = opaque; 1412 uint32_t retval; 1413 1414 switch (addr) { 1415 case SSCR0: 1416 return s->sscr[0]; 1417 case SSCR1: 1418 return s->sscr[1]; 1419 case SSSR: 1420 return s->sssr; 1421 case SSDR: 1422 if (~s->sscr[0] & SSCR0_SSE) { 1423 return 0xffffffff; 1424 } 1425 if (s->rx_level < 1) { 1426 printf("%s: SSP Rx Underrun\n", __func__); 1427 return 0xffffffff; 1428 } 1429 s->rx_level--; 1430 retval = s->rx_fifo[s->rx_start++]; 1431 s->rx_start &= 0x7; 1432 strongarm_ssp_fifo_update(s); 1433 return retval; 1434 default: 1435 printf("%s: Bad register 0x" TARGET_FMT_plx "\n", __func__, addr); 1436 break; 1437 } 1438 return 0; 1439 } 1440 1441 static void strongarm_ssp_write(void *opaque, hwaddr addr, 1442 uint64_t value, unsigned size) 1443 { 1444 StrongARMSSPState *s = opaque; 1445 1446 switch (addr) { 1447 case SSCR0: 1448 s->sscr[0] = value & 0xffbf; 1449 if ((s->sscr[0] & SSCR0_SSE) && SSCR0_DSS(value) < 4) { 1450 printf("%s: Wrong data size: %i bits\n", __func__, 1451 (int)SSCR0_DSS(value)); 1452 } 1453 if (!(value & SSCR0_SSE)) { 1454 s->sssr = 0; 1455 s->rx_level = 0; 1456 } 1457 strongarm_ssp_fifo_update(s); 1458 break; 1459 1460 case SSCR1: 1461 s->sscr[1] = value & 0x2f; 1462 if (value & SSCR1_LBM) { 1463 printf("%s: Attempt to use SSP LBM mode\n", __func__); 1464 } 1465 strongarm_ssp_fifo_update(s); 1466 break; 1467 1468 case SSSR: 1469 s->sssr &= ~(value & SSSR_RW); 1470 strongarm_ssp_int_update(s); 1471 break; 1472 1473 case SSDR: 1474 if (SSCR0_UWIRE(s->sscr[0])) { 1475 value &= 0xff; 1476 } else 1477 /* Note how 32bits overflow does no harm here */ 1478 value &= (1 << SSCR0_DSS(s->sscr[0])) - 1; 1479 1480 /* Data goes from here to the Tx FIFO and is shifted out from 1481 * there directly to the slave, no need to buffer it. 1482 */ 1483 if (s->sscr[0] & SSCR0_SSE) { 1484 uint32_t readval; 1485 if (s->sscr[1] & SSCR1_LBM) { 1486 readval = value; 1487 } else { 1488 readval = ssi_transfer(s->bus, value); 1489 } 1490 1491 if (s->rx_level < 0x08) { 1492 s->rx_fifo[(s->rx_start + s->rx_level++) & 0x7] = readval; 1493 } else { 1494 s->sssr |= SSSR_ROR; 1495 } 1496 } 1497 strongarm_ssp_fifo_update(s); 1498 break; 1499 1500 default: 1501 printf("%s: Bad register 0x" TARGET_FMT_plx "\n", __func__, addr); 1502 break; 1503 } 1504 } 1505 1506 static const MemoryRegionOps strongarm_ssp_ops = { 1507 .read = strongarm_ssp_read, 1508 .write = strongarm_ssp_write, 1509 .endianness = DEVICE_NATIVE_ENDIAN, 1510 }; 1511 1512 static int strongarm_ssp_post_load(void *opaque, int version_id) 1513 { 1514 StrongARMSSPState *s = opaque; 1515 1516 strongarm_ssp_fifo_update(s); 1517 1518 return 0; 1519 } 1520 1521 static int strongarm_ssp_init(SysBusDevice *sbd) 1522 { 1523 DeviceState *dev = DEVICE(sbd); 1524 StrongARMSSPState *s = STRONGARM_SSP(dev); 1525 1526 sysbus_init_irq(sbd, &s->irq); 1527 1528 memory_region_init_io(&s->iomem, OBJECT(s), &strongarm_ssp_ops, s, 1529 "ssp", 0x1000); 1530 sysbus_init_mmio(sbd, &s->iomem); 1531 1532 s->bus = ssi_create_bus(dev, "ssi"); 1533 return 0; 1534 } 1535 1536 static void strongarm_ssp_reset(DeviceState *dev) 1537 { 1538 StrongARMSSPState *s = STRONGARM_SSP(dev); 1539 1540 s->sssr = 0x03; /* 3 bit data, SPI, disabled */ 1541 s->rx_start = 0; 1542 s->rx_level = 0; 1543 } 1544 1545 static const VMStateDescription vmstate_strongarm_ssp_regs = { 1546 .name = "strongarm-ssp", 1547 .version_id = 0, 1548 .minimum_version_id = 0, 1549 .post_load = strongarm_ssp_post_load, 1550 .fields = (VMStateField[]) { 1551 VMSTATE_UINT16_ARRAY(sscr, StrongARMSSPState, 2), 1552 VMSTATE_UINT16(sssr, StrongARMSSPState), 1553 VMSTATE_UINT16_ARRAY(rx_fifo, StrongARMSSPState, 8), 1554 VMSTATE_UINT8(rx_start, StrongARMSSPState), 1555 VMSTATE_UINT8(rx_level, StrongARMSSPState), 1556 VMSTATE_END_OF_LIST(), 1557 }, 1558 }; 1559 1560 static void strongarm_ssp_class_init(ObjectClass *klass, void *data) 1561 { 1562 DeviceClass *dc = DEVICE_CLASS(klass); 1563 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); 1564 1565 k->init = strongarm_ssp_init; 1566 dc->desc = "StrongARM SSP controller"; 1567 dc->reset = strongarm_ssp_reset; 1568 dc->vmsd = &vmstate_strongarm_ssp_regs; 1569 } 1570 1571 static const TypeInfo strongarm_ssp_info = { 1572 .name = TYPE_STRONGARM_SSP, 1573 .parent = TYPE_SYS_BUS_DEVICE, 1574 .instance_size = sizeof(StrongARMSSPState), 1575 .class_init = strongarm_ssp_class_init, 1576 }; 1577 1578 /* Main CPU functions */ 1579 StrongARMState *sa1110_init(MemoryRegion *sysmem, 1580 unsigned int sdram_size, const char *rev) 1581 { 1582 StrongARMState *s; 1583 int i; 1584 1585 s = g_malloc0(sizeof(StrongARMState)); 1586 1587 if (!rev) { 1588 rev = "sa1110-b5"; 1589 } 1590 1591 if (strncmp(rev, "sa1110", 6)) { 1592 error_report("Machine requires a SA1110 processor."); 1593 exit(1); 1594 } 1595 1596 s->cpu = cpu_arm_init(rev); 1597 1598 if (!s->cpu) { 1599 error_report("Unable to find CPU definition"); 1600 exit(1); 1601 } 1602 1603 memory_region_init_ram(&s->sdram, NULL, "strongarm.sdram", sdram_size); 1604 vmstate_register_ram_global(&s->sdram); 1605 memory_region_add_subregion(sysmem, SA_SDCS0, &s->sdram); 1606 1607 s->pic = sysbus_create_varargs("strongarm_pic", 0x90050000, 1608 qdev_get_gpio_in(DEVICE(s->cpu), ARM_CPU_IRQ), 1609 qdev_get_gpio_in(DEVICE(s->cpu), ARM_CPU_FIQ), 1610 NULL); 1611 1612 sysbus_create_varargs("pxa25x-timer", 0x90000000, 1613 qdev_get_gpio_in(s->pic, SA_PIC_OSTC0), 1614 qdev_get_gpio_in(s->pic, SA_PIC_OSTC1), 1615 qdev_get_gpio_in(s->pic, SA_PIC_OSTC2), 1616 qdev_get_gpio_in(s->pic, SA_PIC_OSTC3), 1617 NULL); 1618 1619 sysbus_create_simple(TYPE_STRONGARM_RTC, 0x90010000, 1620 qdev_get_gpio_in(s->pic, SA_PIC_RTC_ALARM)); 1621 1622 s->gpio = strongarm_gpio_init(0x90040000, s->pic); 1623 1624 s->ppc = sysbus_create_varargs(TYPE_STRONGARM_PPC, 0x90060000, NULL); 1625 1626 for (i = 0; sa_serial[i].io_base; i++) { 1627 DeviceState *dev = qdev_create(NULL, TYPE_STRONGARM_UART); 1628 qdev_prop_set_chr(dev, "chardev", serial_hds[i]); 1629 qdev_init_nofail(dev); 1630 sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, 1631 sa_serial[i].io_base); 1632 sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, 1633 qdev_get_gpio_in(s->pic, sa_serial[i].irq)); 1634 } 1635 1636 s->ssp = sysbus_create_varargs(TYPE_STRONGARM_SSP, 0x80070000, 1637 qdev_get_gpio_in(s->pic, SA_PIC_SSP), NULL); 1638 s->ssp_bus = (SSIBus *)qdev_get_child_bus(s->ssp, "ssi"); 1639 1640 return s; 1641 } 1642 1643 static void strongarm_register_types(void) 1644 { 1645 type_register_static(&strongarm_pic_info); 1646 type_register_static(&strongarm_rtc_sysbus_info); 1647 type_register_static(&strongarm_gpio_info); 1648 type_register_static(&strongarm_ppc_info); 1649 type_register_static(&strongarm_uart_info); 1650 type_register_static(&strongarm_ssp_info); 1651 } 1652 1653 type_init(strongarm_register_types) 1654