versatile_i2c.c 3.0 KB

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