blk.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <part.h>
  8. #include <usb.h>
  9. #include <asm/state.h>
  10. #include <dm/test.h>
  11. #include <test/test.h>
  12. #include <test/ut.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. /* Test that block devices can be created */
  15. static int dm_test_blk_base(struct unit_test_state *uts)
  16. {
  17. struct udevice *blk1, *blk3, *dev;
  18. /* Make sure there are no block devices */
  19. ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_BLK, 0, &dev));
  20. /* Create two, one the parent of the other */
  21. ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
  22. IF_TYPE_HOST, 1, 512, 2, &blk1));
  23. ut_assertok(blk_create_device(blk1, "sandbox_host_blk", "test",
  24. IF_TYPE_HOST, 3, 512, 2, &blk3));
  25. /* Check we can find them */
  26. ut_asserteq(-ENODEV, blk_get_device(IF_TYPE_HOST, 0, &dev));
  27. ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev));
  28. ut_asserteq_ptr(blk1, dev);
  29. ut_assertok(blk_get_device(IF_TYPE_HOST, 3, &dev));
  30. ut_asserteq_ptr(blk3, dev);
  31. /* Check we can iterate */
  32. ut_assertok(blk_first_device(IF_TYPE_HOST, &dev));
  33. ut_asserteq_ptr(blk1, dev);
  34. ut_assertok(blk_next_device(&dev));
  35. ut_asserteq_ptr(blk3, dev);
  36. return 0;
  37. }
  38. DM_TEST(dm_test_blk_base, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  39. static int count_blk_devices(void)
  40. {
  41. struct udevice *blk;
  42. struct uclass *uc;
  43. int count = 0;
  44. int ret;
  45. ret = uclass_get(UCLASS_BLK, &uc);
  46. if (ret)
  47. return ret;
  48. uclass_foreach_dev(blk, uc)
  49. count++;
  50. return count;
  51. }
  52. /* Test that block devices work correctly with USB */
  53. static int dm_test_blk_usb(struct unit_test_state *uts)
  54. {
  55. struct udevice *usb_dev, *dev;
  56. struct blk_desc *dev_desc;
  57. /* Get a flash device */
  58. state_set_skip_delays(true);
  59. ut_assertok(usb_init());
  60. ut_assertok(uclass_get_device(UCLASS_MASS_STORAGE, 0, &usb_dev));
  61. ut_assertok(blk_get_device_by_str("usb", "0", &dev_desc));
  62. /* The parent should be a block device */
  63. ut_assertok(blk_get_device(IF_TYPE_USB, 0, &dev));
  64. ut_asserteq_ptr(usb_dev, dev_get_parent(dev));
  65. /* Check we have one block device for each mass storage device */
  66. ut_asserteq(6, count_blk_devices());
  67. /* Now go around again, making sure the old devices were unbound */
  68. ut_assertok(usb_stop());
  69. ut_assertok(usb_init());
  70. ut_asserteq(6, count_blk_devices());
  71. ut_assertok(usb_stop());
  72. return 0;
  73. }
  74. DM_TEST(dm_test_blk_usb, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  75. /* Test that we can find block devices without probing them */
  76. static int dm_test_blk_find(struct unit_test_state *uts)
  77. {
  78. struct udevice *blk, *dev;
  79. ut_assertok(blk_create_device(gd->dm_root, "sandbox_host_blk", "test",
  80. IF_TYPE_HOST, 1, 512, 2, &blk));
  81. ut_asserteq(-ENODEV, blk_find_device(IF_TYPE_HOST, 0, &dev));
  82. ut_assertok(blk_find_device(IF_TYPE_HOST, 1, &dev));
  83. ut_asserteq_ptr(blk, dev);
  84. ut_asserteq(false, device_active(dev));
  85. /* Now activate it */
  86. ut_assertok(blk_get_device(IF_TYPE_HOST, 1, &dev));
  87. ut_asserteq_ptr(blk, dev);
  88. ut_asserteq(true, device_active(dev));
  89. return 0;
  90. }
  91. DM_TEST(dm_test_blk_find, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  92. /* Test that block device numbering works as expected */
  93. static int dm_test_blk_devnum(struct unit_test_state *uts)
  94. {
  95. struct udevice *dev, *mmc_dev, *parent;
  96. int i;
  97. /*
  98. * Probe the devices, with the first one being probed last. This is the
  99. * one with no alias / sequence numnber.
  100. */
  101. ut_assertok(uclass_get_device(UCLASS_MMC, 1, &dev));
  102. ut_assertok(uclass_get_device(UCLASS_MMC, 2, &dev));
  103. ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
  104. for (i = 0; i < 3; i++) {
  105. struct blk_desc *desc;
  106. /* Check that the bblock device is attached */
  107. ut_assertok(uclass_get_device_by_seq(UCLASS_MMC, i, &mmc_dev));
  108. ut_assertok(blk_find_device(IF_TYPE_MMC, i, &dev));
  109. parent = dev_get_parent(dev);
  110. ut_asserteq_ptr(parent, mmc_dev);
  111. ut_asserteq(trailing_strtol(mmc_dev->name), i);
  112. /*
  113. * Check that the block device devnum matches its parent's
  114. * sequence number
  115. */
  116. desc = dev_get_uclass_platdata(dev);
  117. ut_asserteq(desc->devnum, i);
  118. }
  119. return 0;
  120. }
  121. DM_TEST(dm_test_blk_devnum, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
  122. /* Test that we can get a block from its parent */
  123. static int dm_test_blk_get_from_parent(struct unit_test_state *uts)
  124. {
  125. struct udevice *dev, *blk;
  126. ut_assertok(uclass_get_device(UCLASS_MMC, 0, &dev));
  127. ut_assertok(blk_get_from_parent(dev, &blk));
  128. ut_assertok(uclass_get_device(UCLASS_I2C, 0, &dev));
  129. ut_asserteq(-ENOTBLK, blk_get_from_parent(dev, &blk));
  130. ut_assertok(uclass_get_device(UCLASS_GPIO, 0, &dev));
  131. ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
  132. return 0;
  133. }
  134. DM_TEST(dm_test_blk_get_from_parent, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);