versatile_i2c.c 3.2 KB

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