fpga_manager.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. /*
  3. * Copyright (C) 2012 Altera Corporation <www.altera.com>
  4. * All rights reserved.
  5. *
  6. * This file contains only support functions used also by the SoCFPGA
  7. * platform code, the real meat is located in drivers/fpga/socfpga.c .
  8. */
  9. #include <common.h>
  10. #include <asm/io.h>
  11. #include <linux/errno.h>
  12. #include <asm/arch/fpga_manager.h>
  13. #include <asm/arch/reset_manager.h>
  14. #include <asm/arch/system_manager.h>
  15. /* Timeout count */
  16. #define FPGA_TIMEOUT_CNT 0x1000000
  17. static struct socfpga_fpga_manager *fpgamgr_regs =
  18. (struct socfpga_fpga_manager *)SOCFPGA_FPGAMGRREGS_ADDRESS;
  19. /* Check whether FPGA Init_Done signal is high */
  20. static int is_fpgamgr_initdone_high(void)
  21. {
  22. unsigned long val;
  23. val = readl(&fpgamgr_regs->gpio_ext_porta);
  24. return val & FPGAMGRREGS_MON_GPIO_EXT_PORTA_ID_MASK;
  25. }
  26. /* Get the FPGA mode */
  27. int fpgamgr_get_mode(void)
  28. {
  29. unsigned long val;
  30. val = readl(&fpgamgr_regs->stat);
  31. return val & FPGAMGRREGS_STAT_MODE_MASK;
  32. }
  33. /* Check whether FPGA is ready to be accessed */
  34. int fpgamgr_test_fpga_ready(void)
  35. {
  36. /* Check for init done signal */
  37. if (!is_fpgamgr_initdone_high())
  38. return 0;
  39. /* Check again to avoid false glitches */
  40. if (!is_fpgamgr_initdone_high())
  41. return 0;
  42. if (fpgamgr_get_mode() != FPGAMGRREGS_MODE_USERMODE)
  43. return 0;
  44. return 1;
  45. }
  46. /* Poll until FPGA is ready to be accessed or timeout occurred */
  47. int fpgamgr_poll_fpga_ready(void)
  48. {
  49. unsigned long i;
  50. /* If FPGA is blank, wait till WD invoke warm reset */
  51. for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
  52. /* check for init done signal */
  53. if (!is_fpgamgr_initdone_high())
  54. continue;
  55. /* check again to avoid false glitches */
  56. if (!is_fpgamgr_initdone_high())
  57. continue;
  58. return 1;
  59. }
  60. return 0;
  61. }