sandbox_store.c 3.0 KB

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