sandbox_store.c 2.9 KB

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