2
0

versatile_i2c.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "qemu/osdep.h"
  24. #include "hw/sysbus.h"
  25. #include "hw/i2c/bitbang_i2c.h"
  26. #include "qemu/log.h"
  27. #include "qemu/module.h"
  28. #define TYPE_VERSATILE_I2C "versatile_i2c"
  29. #define VERSATILE_I2C(obj) \
  30. OBJECT_CHECK(VersatileI2CState, (obj), TYPE_VERSATILE_I2C)
  31. typedef struct VersatileI2CState {
  32. SysBusDevice parent_obj;
  33. MemoryRegion iomem;
  34. bitbang_i2c_interface bitbang;
  35. int out;
  36. int in;
  37. } VersatileI2CState;
  38. static uint64_t versatile_i2c_read(void *opaque, hwaddr offset,
  39. unsigned size)
  40. {
  41. VersatileI2CState *s = (VersatileI2CState *)opaque;
  42. if (offset == 0) {
  43. return (s->out & 1) | (s->in << 1);
  44. } else {
  45. qemu_log_mask(LOG_GUEST_ERROR,
  46. "%s: Bad offset 0x%x\n", __func__, (int)offset);
  47. return -1;
  48. }
  49. }
  50. static void versatile_i2c_write(void *opaque, hwaddr offset,
  51. uint64_t value, unsigned size)
  52. {
  53. VersatileI2CState *s = (VersatileI2CState *)opaque;
  54. switch (offset) {
  55. case 0:
  56. s->out |= value & 3;
  57. break;
  58. case 4:
  59. s->out &= ~value;
  60. break;
  61. default:
  62. qemu_log_mask(LOG_GUEST_ERROR,
  63. "%s: Bad offset 0x%x\n", __func__, (int)offset);
  64. }
  65. bitbang_i2c_set(&s->bitbang, BITBANG_I2C_SCL, (s->out & 1) != 0);
  66. s->in = bitbang_i2c_set(&s->bitbang, BITBANG_I2C_SDA, (s->out & 2) != 0);
  67. }
  68. static const MemoryRegionOps versatile_i2c_ops = {
  69. .read = versatile_i2c_read,
  70. .write = versatile_i2c_write,
  71. .endianness = DEVICE_NATIVE_ENDIAN,
  72. };
  73. static void versatile_i2c_init(Object *obj)
  74. {
  75. DeviceState *dev = DEVICE(obj);
  76. VersatileI2CState *s = VERSATILE_I2C(obj);
  77. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  78. I2CBus *bus;
  79. bus = i2c_init_bus(dev, "i2c");
  80. bitbang_i2c_init(&s->bitbang, bus);
  81. memory_region_init_io(&s->iomem, obj, &versatile_i2c_ops, s,
  82. "versatile_i2c", 0x1000);
  83. sysbus_init_mmio(sbd, &s->iomem);
  84. }
  85. static const TypeInfo versatile_i2c_info = {
  86. .name = TYPE_VERSATILE_I2C,
  87. .parent = TYPE_SYS_BUS_DEVICE,
  88. .instance_size = sizeof(VersatileI2CState),
  89. .instance_init = versatile_i2c_init,
  90. };
  91. static void versatile_i2c_register_types(void)
  92. {
  93. type_register_static(&versatile_i2c_info);
  94. }
  95. type_init(versatile_i2c_register_types)