test-fdt.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2013 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <fdtdec.h>
  10. #include <malloc.h>
  11. #include <asm/io.h>
  12. #include <dm/test.h>
  13. #include <dm/root.h>
  14. #include <dm/ut.h>
  15. #include <dm/uclass-internal.h>
  16. #include <dm/util.h>
  17. DECLARE_GLOBAL_DATA_PTR;
  18. static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret)
  19. {
  20. const struct dm_test_pdata *pdata = dev->platdata;
  21. struct dm_test_priv *priv = dev_get_priv(dev);
  22. *pingret = pingval + pdata->ping_add;
  23. priv->ping_total += *pingret;
  24. return 0;
  25. }
  26. static const struct test_ops test_ops = {
  27. .ping = testfdt_drv_ping,
  28. };
  29. static int testfdt_ofdata_to_platdata(struct udevice *dev)
  30. {
  31. struct dm_test_pdata *pdata = dev_get_platdata(dev);
  32. pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  33. "ping-add", -1);
  34. pdata->base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset,
  35. "ping-expect");
  36. return 0;
  37. }
  38. static int testfdt_drv_probe(struct udevice *dev)
  39. {
  40. struct dm_test_priv *priv = dev_get_priv(dev);
  41. priv->ping_total += DM_TEST_START_TOTAL;
  42. return 0;
  43. }
  44. static const struct udevice_id testfdt_ids[] = {
  45. {
  46. .compatible = "denx,u-boot-fdt-test",
  47. .data = DM_TEST_TYPE_FIRST },
  48. {
  49. .compatible = "google,another-fdt-test",
  50. .data = DM_TEST_TYPE_SECOND },
  51. { }
  52. };
  53. U_BOOT_DRIVER(testfdt_drv) = {
  54. .name = "testfdt_drv",
  55. .of_match = testfdt_ids,
  56. .id = UCLASS_TEST_FDT,
  57. .ofdata_to_platdata = testfdt_ofdata_to_platdata,
  58. .probe = testfdt_drv_probe,
  59. .ops = &test_ops,
  60. .priv_auto_alloc_size = sizeof(struct dm_test_priv),
  61. .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
  62. };
  63. /* From here is the testfdt uclass code */
  64. int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
  65. {
  66. const struct test_ops *ops = device_get_ops(dev);
  67. if (!ops->ping)
  68. return -ENOSYS;
  69. return ops->ping(dev, pingval, pingret);
  70. }
  71. UCLASS_DRIVER(testfdt) = {
  72. .name = "testfdt",
  73. .id = UCLASS_TEST_FDT,
  74. };
  75. /* Test that FDT-based binding works correctly */
  76. static int dm_test_fdt(struct dm_test_state *dms)
  77. {
  78. const int num_drivers = 3;
  79. struct udevice *dev;
  80. struct uclass *uc;
  81. int ret;
  82. int i;
  83. ret = dm_scan_fdt(gd->fdt_blob);
  84. ut_assert(!ret);
  85. ret = uclass_get(UCLASS_TEST_FDT, &uc);
  86. ut_assert(!ret);
  87. /* These are num_drivers compatible root-level device tree nodes */
  88. ut_asserteq(num_drivers, list_count_items(&uc->dev_head));
  89. /* Each should have no platdata / priv */
  90. for (i = 0; i < num_drivers; i++) {
  91. ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
  92. ut_assert(!ret);
  93. ut_assert(!dev_get_priv(dev));
  94. ut_assert(!dev->platdata);
  95. }
  96. /*
  97. * Now check that the ping adds are what we expect. This is using the
  98. * ping-add property in each node.
  99. */
  100. for (i = 0; i < num_drivers; i++) {
  101. uint32_t base;
  102. ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
  103. ut_assert(!ret);
  104. /*
  105. * Get the 'ping-expect' property, which tells us what the
  106. * ping add should be. We don't use the platdata because we
  107. * want to test the code that sets that up
  108. * (testfdt_drv_probe()).
  109. */
  110. base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset,
  111. "ping-expect");
  112. debug("dev=%d, base=%d: %s\n", i, base,
  113. fdt_get_name(gd->fdt_blob, dev->of_offset, NULL));
  114. ut_assert(!dm_check_operations(dms, dev, base,
  115. dev_get_priv(dev)));
  116. }
  117. return 0;
  118. }
  119. DM_TEST(dm_test_fdt, 0);