arm_sbcon_i2c.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. REG32(CONTROL_GET, 0)
  31. REG32(CONTROL_SET, 0)
  32. REG32(CONTROL_CLR, 4)
  33. #define SCL BIT(0)
  34. #define SDA BIT(1)
  35. static uint64_t arm_sbcon_i2c_read(void *opaque, hwaddr offset,
  36. unsigned size)
  37. {
  38. ArmSbconI2CState *s = opaque;
  39. switch (offset) {
  40. case A_CONTROL_SET:
  41. return (s->out & 1) | (s->in << 1);
  42. default:
  43. qemu_log_mask(LOG_GUEST_ERROR,
  44. "%s: Bad offset 0x%x\n", __func__, (int)offset);
  45. return -1;
  46. }
  47. }
  48. static void arm_sbcon_i2c_write(void *opaque, hwaddr offset,
  49. uint64_t value, unsigned size)
  50. {
  51. ArmSbconI2CState *s = opaque;
  52. switch (offset) {
  53. case A_CONTROL_SET:
  54. s->out |= value & 3;
  55. break;
  56. case A_CONTROL_CLR:
  57. s->out &= ~value;
  58. break;
  59. default:
  60. qemu_log_mask(LOG_GUEST_ERROR,
  61. "%s: Bad offset 0x%x\n", __func__, (int)offset);
  62. }
  63. bitbang_i2c_set(&s->bitbang, BITBANG_I2C_SCL, (s->out & SCL) != 0);
  64. s->in = bitbang_i2c_set(&s->bitbang, BITBANG_I2C_SDA, (s->out & SDA) != 0);
  65. }
  66. static const MemoryRegionOps arm_sbcon_i2c_ops = {
  67. .read = arm_sbcon_i2c_read,
  68. .write = arm_sbcon_i2c_write,
  69. .endianness = DEVICE_NATIVE_ENDIAN,
  70. };
  71. static void arm_sbcon_i2c_init(Object *obj)
  72. {
  73. DeviceState *dev = DEVICE(obj);
  74. ArmSbconI2CState *s = ARM_SBCON_I2C(obj);
  75. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  76. I2CBus *bus;
  77. bus = i2c_init_bus(dev, "i2c");
  78. bitbang_i2c_init(&s->bitbang, bus);
  79. memory_region_init_io(&s->iomem, obj, &arm_sbcon_i2c_ops, s,
  80. "arm_sbcon_i2c", 0x1000);
  81. sysbus_init_mmio(sbd, &s->iomem);
  82. }
  83. static const TypeInfo arm_sbcon_i2c_info = {
  84. .name = TYPE_ARM_SBCON_I2C,
  85. .parent = TYPE_SYS_BUS_DEVICE,
  86. .instance_size = sizeof(ArmSbconI2CState),
  87. .instance_init = arm_sbcon_i2c_init,
  88. };
  89. static void arm_sbcon_i2c_register_types(void)
  90. {
  91. type_register_static(&arm_sbcon_i2c_info);
  92. }
  93. type_init(arm_sbcon_i2c_register_types)