blk.c 4.6 KB

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