qfw.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2015 Miao Yan <yanmiaobest@gmail.com>
  4. * (C) Copyright 2021 Asherah Connor <ashe@kivikakk.ee>
  5. */
  6. #include <dm.h>
  7. #include <dm/uclass.h>
  8. #include <qfw.h>
  9. #include <stdlib.h>
  10. int qfw_get_dev(struct udevice **devp)
  11. {
  12. return uclass_first_device_err(UCLASS_QFW, devp);
  13. }
  14. int qfw_online_cpus(struct udevice *dev)
  15. {
  16. u16 nb_cpus;
  17. qfw_read_entry(dev, FW_CFG_NB_CPUS, 2, &nb_cpus);
  18. return le16_to_cpu(nb_cpus);
  19. }
  20. int qfw_read_firmware_list(struct udevice *dev)
  21. {
  22. int i;
  23. u32 count;
  24. struct fw_file *file;
  25. struct list_head *entry;
  26. struct qfw_dev *qdev = dev_get_uclass_priv(dev);
  27. /* don't read it twice */
  28. if (!list_empty(&qdev->fw_list))
  29. return 0;
  30. qfw_read_entry(dev, FW_CFG_FILE_DIR, 4, &count);
  31. if (!count)
  32. return 0;
  33. count = be32_to_cpu(count);
  34. for (i = 0; i < count; i++) {
  35. file = malloc(sizeof(*file));
  36. if (!file) {
  37. printf("error: allocating resource\n");
  38. goto err;
  39. }
  40. qfw_read_entry(dev, FW_CFG_INVALID,
  41. sizeof(struct fw_cfg_file), &file->cfg);
  42. file->addr = 0;
  43. list_add_tail(&file->list, &qdev->fw_list);
  44. }
  45. return 0;
  46. err:
  47. list_for_each(entry, &qdev->fw_list) {
  48. file = list_entry(entry, struct fw_file, list);
  49. free(file);
  50. }
  51. return -ENOMEM;
  52. }
  53. struct fw_file *qfw_find_file(struct udevice *dev, const char *name)
  54. {
  55. struct list_head *entry;
  56. struct fw_file *file;
  57. struct qfw_dev *qdev = dev_get_uclass_priv(dev);
  58. list_for_each(entry, &qdev->fw_list) {
  59. file = list_entry(entry, struct fw_file, list);
  60. if (!strcmp(file->cfg.name, name))
  61. return file;
  62. }
  63. return NULL;
  64. }
  65. struct fw_file *qfw_file_iter_init(struct udevice *dev,
  66. struct fw_cfg_file_iter *iter)
  67. {
  68. struct qfw_dev *qdev = dev_get_uclass_priv(dev);
  69. iter->entry = qdev->fw_list.next;
  70. iter->end = &qdev->fw_list;
  71. return list_entry((struct list_head *)iter->entry,
  72. struct fw_file, list);
  73. }
  74. struct fw_file *qfw_file_iter_next(struct fw_cfg_file_iter *iter)
  75. {
  76. iter->entry = ((struct list_head *)iter->entry)->next;
  77. return list_entry((struct list_head *)iter->entry,
  78. struct fw_file, list);
  79. }
  80. bool qfw_file_iter_end(struct fw_cfg_file_iter *iter)
  81. {
  82. return iter->entry == iter->end;
  83. }