blk.c 4.7 KB

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