wd_pio.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * (C) Copyright 2004, Li-Pro.Net <www.li-pro.net>
  3. * Stephan Linz <linz@li-pro.net>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <nios.h>
  26. #include <nios-io.h>
  27. #if defined(CONFIG_HW_WATCHDOG)
  28. #if !defined(CONFIG_HW_WDENA_BASE)
  29. #error "*** CONFIG_HW_WDENA_BASE not defined ***"
  30. #if !defined(CONFIG_HW_WDENA_BIT)
  31. #error "*** CONFIG_HW_WDENA_BIT not defined ***"
  32. #endif
  33. #endif
  34. #if !defined(CONFIG_HW_WDTOG_BASE)
  35. #error "*** CONFIG_HW_WDTOG_BASE not defined ***"
  36. #if !defined(CONFIG_HW_WDTOG_BIT)
  37. #error "*** CONFIG_HW_WDTOG_BIT not defined ***"
  38. #endif
  39. #endif
  40. #ifdef CONFIG_HW_WDPORT_WRONLY /* emulate read access */
  41. static unsigned __wd_ena_pio_portval = 0;
  42. #endif
  43. #define WD_PIO_INIT_DONE(V) ((V) & (1 << CONFIG_HW_WDENA_BIT))
  44. void ssv_wd_pio_init(void)
  45. {
  46. nios_pio_t *ena_piop = (nios_pio_t*)CONFIG_HW_WDENA_BASE;
  47. nios_pio_t *trg_piop = (nios_pio_t*)CONFIG_HW_WDTOG_BASE;
  48. trg_piop->data &= ~(1 << CONFIG_HW_WDTOG_BIT);
  49. #ifdef CONFIG_HW_WDPORT_WRONLY /* emulate read access */
  50. __wd_ena_pio_portval |= (1 << CONFIG_HW_WDENA_BIT);
  51. ena_piop->data = __wd_ena_pio_portval;
  52. #else /* !CONFIG_HW_WDPORT_WRONLY */
  53. trg_piop->direction |= (1 << CONFIG_HW_WDTOG_BIT);
  54. ena_piop->data |= (1 << CONFIG_HW_WDENA_BIT);
  55. ena_piop->direction |= (1 << CONFIG_HW_WDENA_BIT);
  56. #endif /* CONFIG_HW_WDPORT_WRONLY */
  57. }
  58. void ssv_wd_pio_done(void)
  59. {
  60. nios_pio_t *piop = (nios_pio_t*)CONFIG_HW_WDENA_BASE;
  61. #ifdef CONFIG_HW_WDPORT_WRONLY /* emulate read access */
  62. __wd_ena_pio_portval &= ~(1 << CONFIG_HW_WDENA_BIT);
  63. piop->data = __wd_ena_pio_portval;
  64. #else /* !CONFIG_HW_WDPORT_WRONLY */
  65. piop->data &= ~(1 << CONFIG_HW_WDENA_BIT);
  66. #endif /* CONFIG_HW_WDPORT_WRONLY */
  67. }
  68. void ssv_wd_pio_reset(void)
  69. {
  70. nios_pio_t *trg_piop = (nios_pio_t*)CONFIG_HW_WDTOG_BASE;
  71. #ifdef CONFIG_HW_WDPORT_WRONLY
  72. if (WD_PIO_INIT_DONE(__wd_ena_pio_portval))
  73. #else
  74. nios_pio_t *ena_piop = (nios_pio_t*)CONFIG_HW_WDENA_BASE;
  75. if (WD_PIO_INIT_DONE(ena_piop->data))
  76. #endif
  77. {
  78. trg_piop->data |= (1 << CONFIG_HW_WDTOG_BIT);
  79. trg_piop->data &= ~(1 << CONFIG_HW_WDTOG_BIT);
  80. }
  81. }
  82. void hw_watchdog_reset(void)
  83. {
  84. int re_enable = disable_interrupts ();
  85. ssv_wd_pio_reset();
  86. if (re_enable)
  87. enable_interrupts ();
  88. }
  89. #if defined(CONFIG_CMD_BSP)
  90. int do_wd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  91. {
  92. nios_pio_t *ena_piop = (nios_pio_t*)CONFIG_HW_WDENA_BASE;
  93. switch (argc)
  94. {
  95. case 1:
  96. printf ("Watchdog timer status is %s\n",
  97. #ifdef CONFIG_HW_WDPORT_WRONLY
  98. WD_PIO_INIT_DONE(__wd_ena_pio_portval)
  99. #else
  100. WD_PIO_INIT_DONE(ena_piop->data)
  101. #endif
  102. ? "on" : "off");
  103. return 0;
  104. case 2:
  105. if (!strcmp(argv[1],"on"))
  106. {
  107. ssv_wd_pio_init();
  108. printf("Watchdog timer now is on\n");
  109. return 0;
  110. }
  111. else if (!strcmp(argv[1],"off"))
  112. {
  113. ssv_wd_pio_done();
  114. printf("Watchdog timer now is off\n");
  115. return 0;
  116. }
  117. break;
  118. default:
  119. break;
  120. }
  121. cmd_usage(cmdtp);
  122. return 1;
  123. }
  124. U_BOOT_CMD(
  125. wd, 2, 1, do_wd,
  126. "check and set watchdog",
  127. "on - switch watchDog on\n"
  128. "wd off - switch watchdog off\n"
  129. "wd - print current status\n"
  130. );
  131. #endif
  132. #endif /* CONFIG_HW_WATCHDOG */