blk.c 4.6 KB

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