blk.c 4.2 KB

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