blk.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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(4, 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(4, 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);