versatile_i2c.c 3.2 KB

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