versatile_i2c.c 3.1 KB

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