versatile_i2c.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * ARM Versatile I2C controller
  3. *
  4. * Copyright (c) 2006-2007 CodeSourcery.
  5. * Copyright (c) 2012 Oskar Andero <oskar.andero@gmail.com>
  6. *
  7. * This file is derived from hw/realview.c by Paul Brook
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #include "sysbus.h"
  24. #include "bitbang_i2c.h"
  25. typedef struct {
  26. SysBusDevice busdev;
  27. MemoryRegion iomem;
  28. bitbang_i2c_interface *bitbang;
  29. int out;
  30. int in;
  31. } VersatileI2CState;
  32. static uint64_t versatile_i2c_read(void *opaque, hwaddr offset,
  33. unsigned size)
  34. {
  35. VersatileI2CState *s = (VersatileI2CState *)opaque;
  36. if (offset == 0) {
  37. return (s->out & 1) | (s->in << 1);
  38. } else {
  39. qemu_log_mask(LOG_GUEST_ERROR,
  40. "%s: Bad offset 0x%x\n", __func__, (int)offset);
  41. return -1;
  42. }
  43. }
  44. static void versatile_i2c_write(void *opaque, hwaddr offset,
  45. uint64_t value, unsigned size)
  46. {
  47. VersatileI2CState *s = (VersatileI2CState *)opaque;
  48. switch (offset) {
  49. case 0:
  50. s->out |= value & 3;
  51. break;
  52. case 4:
  53. s->out &= ~value;
  54. break;
  55. default:
  56. qemu_log_mask(LOG_GUEST_ERROR,
  57. "%s: Bad offset 0x%x\n", __func__, (int)offset);
  58. }
  59. bitbang_i2c_set(s->bitbang, BITBANG_I2C_SCL, (s->out & 1) != 0);
  60. s->in = bitbang_i2c_set(s->bitbang, BITBANG_I2C_SDA, (s->out & 2) != 0);
  61. }
  62. static const MemoryRegionOps versatile_i2c_ops = {
  63. .read = versatile_i2c_read,
  64. .write = versatile_i2c_write,
  65. .endianness = DEVICE_NATIVE_ENDIAN,
  66. };
  67. static int versatile_i2c_init(SysBusDevice *dev)
  68. {
  69. VersatileI2CState *s = FROM_SYSBUS(VersatileI2CState, dev);
  70. i2c_bus *bus;
  71. bus = i2c_init_bus(&dev->qdev, "i2c");
  72. s->bitbang = bitbang_i2c_init(bus);
  73. memory_region_init_io(&s->iomem, &versatile_i2c_ops, s,
  74. "versatile_i2c", 0x1000);
  75. sysbus_init_mmio(dev, &s->iomem);
  76. return 0;
  77. }
  78. static void versatile_i2c_class_init(ObjectClass *klass, void *data)
  79. {
  80. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  81. k->init = versatile_i2c_init;
  82. }
  83. static const TypeInfo versatile_i2c_info = {
  84. .name = "versatile_i2c",
  85. .parent = TYPE_SYS_BUS_DEVICE,
  86. .instance_size = sizeof(VersatileI2CState),
  87. .class_init = versatile_i2c_class_init,
  88. };
  89. static void versatile_i2c_register_types(void)
  90. {
  91. type_register_static(&versatile_i2c_info);
  92. }
  93. type_init(versatile_i2c_register_types)