sandbox_store.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2018
  4. * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
  5. */
  6. #include <common.h>
  7. #include <axi.h>
  8. #include <dm.h>
  9. #include <log.h>
  10. #include <malloc.h>
  11. /**
  12. * struct sandbox_store_priv - Private data structure of a AXI store device
  13. * @store: The buffer holding the device's internal memory, which is read from
  14. * and written to using the driver's methods
  15. */
  16. struct sandbox_store_priv {
  17. u8 *store;
  18. };
  19. /**
  20. * copy_axi_data() - Copy data from source to destination with a given AXI
  21. * transfer width
  22. * @src: Pointer to the data source from where data will be read
  23. * @dst: Pointer to the data destination where data will be written to
  24. * @size: Size of the data to be copied given by a axi_size_t enum value
  25. *
  26. * Return: 0 if OK, -ve on error
  27. */
  28. static int copy_axi_data(void *src, void *dst, enum axi_size_t size)
  29. {
  30. switch (size) {
  31. case AXI_SIZE_8:
  32. *((u8 *)dst) = *((u8 *)src);
  33. return 0;
  34. case AXI_SIZE_16:
  35. *((u16 *)dst) = be16_to_cpu(*((u16 *)src));
  36. return 0;
  37. case AXI_SIZE_32:
  38. *((u32 *)dst) = be32_to_cpu(*((u32 *)src));
  39. return 0;
  40. default:
  41. debug("%s: Unknown AXI transfer size '%d'\n", __func__, size);
  42. return -EINVAL;
  43. }
  44. }
  45. static int sandbox_store_read(struct udevice *dev, ulong address, void *data,
  46. enum axi_size_t size)
  47. {
  48. struct sandbox_store_priv *priv = dev_get_priv(dev);
  49. return copy_axi_data(priv->store + address, data, size);
  50. }
  51. static int sandbox_store_write(struct udevice *dev, ulong address, void *data,
  52. enum axi_size_t size)
  53. {
  54. struct sandbox_store_priv *priv = dev_get_priv(dev);
  55. return copy_axi_data(data, priv->store + address, size);
  56. }
  57. static int sandbox_store_get_store(struct udevice *dev, u8 **store)
  58. {
  59. struct sandbox_store_priv *priv = dev_get_priv(dev);
  60. *store = priv->store;
  61. return 0;
  62. }
  63. static const struct udevice_id sandbox_store_ids[] = {
  64. { .compatible = "sandbox,sandbox_store" },
  65. { /* sentinel */ }
  66. };
  67. static const struct axi_emul_ops sandbox_store_ops = {
  68. .read = sandbox_store_read,
  69. .write = sandbox_store_write,
  70. .get_store = sandbox_store_get_store,
  71. };
  72. static int sandbox_store_probe(struct udevice *dev)
  73. {
  74. struct sandbox_store_priv *priv = dev_get_priv(dev);
  75. u32 reg[2];
  76. int ret;
  77. ret = dev_read_u32_array(dev, "reg", reg, ARRAY_SIZE(reg));
  78. if (ret) {
  79. debug("%s: Could not read 'reg' property\n", dev->name);
  80. return -EINVAL;
  81. }
  82. /*
  83. * Allocate the device's internal storage that will be read
  84. * from/written to
  85. */
  86. priv->store = calloc(reg[1], 1);
  87. if (!priv->store)
  88. return -ENOMEM;
  89. return 0;
  90. }
  91. static int sandbox_store_remove(struct udevice *dev)
  92. {
  93. struct sandbox_store_priv *priv = dev_get_priv(dev);
  94. free(priv->store);
  95. return 0;
  96. }
  97. U_BOOT_DRIVER(sandbox_axi_store) = {
  98. .name = "sandbox_axi_store",
  99. .id = UCLASS_AXI_EMUL,
  100. .of_match = sandbox_store_ids,
  101. .ops = &sandbox_store_ops,
  102. .priv_auto = sizeof(struct sandbox_store_priv),
  103. .probe = sandbox_store_probe,
  104. .remove = sandbox_store_remove,
  105. };