status_led.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  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 <status_led.h>
  25. /*
  26. * The purpose of this code is to signal the operational status of a
  27. * target which usually boots over the network; while running in
  28. * PCBoot, a status LED is blinking. As soon as a valid BOOTP reply
  29. * message has been received, the LED is turned off. The Linux
  30. * kernel, once it is running, will start blinking the LED again,
  31. * with another frequency.
  32. */
  33. /* ------------------------------------------------------------------------- */
  34. #ifdef CONFIG_STATUS_LED
  35. typedef struct {
  36. ulong mask;
  37. int state;
  38. int period;
  39. int cnt;
  40. } led_dev_t;
  41. led_dev_t led_dev[] = {
  42. { STATUS_LED_BIT,
  43. STATUS_LED_STATE,
  44. STATUS_LED_PERIOD,
  45. 0,
  46. },
  47. #if defined(STATUS_LED_BIT1)
  48. { STATUS_LED_BIT1,
  49. STATUS_LED_STATE1,
  50. STATUS_LED_PERIOD1,
  51. 0,
  52. },
  53. #endif
  54. #if defined(STATUS_LED_BIT2)
  55. { STATUS_LED_BIT2,
  56. STATUS_LED_STATE2,
  57. STATUS_LED_PERIOD2,
  58. 0,
  59. },
  60. #endif
  61. };
  62. #define MAX_LED_DEV (sizeof(led_dev)/sizeof(led_dev_t))
  63. static int status_led_init_done = 0;
  64. static void status_led_init (void)
  65. {
  66. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  67. int i;
  68. for (i=0; i<MAX_LED_DEV; ++i) {
  69. led_dev_t *ld = &led_dev[i];
  70. immr->STATUS_LED_PAR &= ~(ld->mask);
  71. #ifdef STATUS_LED_ODR
  72. immr->STATUS_LED_ODR &= ~(ld->mask);
  73. #endif
  74. #if (STATUS_LED_ACTIVE == 0)
  75. if (ld->state == STATUS_LED_ON)
  76. immr->STATUS_LED_DAT &= ~(ld->mask);
  77. else
  78. immr->STATUS_LED_DAT |= ld->mask ;
  79. #else
  80. if (ld->state == STATUS_LED_ON)
  81. immr->STATUS_LED_DAT |= ld->mask ;
  82. else
  83. immr->STATUS_LED_DAT &= ~(ld->mask);
  84. #endif
  85. immr->STATUS_LED_DIR |= ld->mask ;
  86. }
  87. status_led_init_done = 1;
  88. }
  89. void status_led_tick (ulong timestamp)
  90. {
  91. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  92. int i;
  93. if (!status_led_init_done)
  94. status_led_init();
  95. for (i=0; i<MAX_LED_DEV; ++i) {
  96. led_dev_t *ld = &led_dev[i];
  97. if (ld->state != STATUS_LED_BLINKING)
  98. continue;
  99. if (++(ld->cnt) >= ld->period) {
  100. immr->STATUS_LED_DAT ^= ld->mask;
  101. ld->cnt -= ld->period;
  102. }
  103. }
  104. }
  105. void status_led_set (int led, int state)
  106. {
  107. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  108. led_dev_t *ld;
  109. if (led < 0 || led >= MAX_LED_DEV)
  110. return;
  111. if (!status_led_init_done)
  112. status_led_init();
  113. ld = &led_dev[led];
  114. switch (state) {
  115. default:
  116. return;
  117. case STATUS_LED_BLINKING:
  118. ld->cnt = 0; /* always start with full period */
  119. /* fall through */ /* always start with LED _ON_ */
  120. case STATUS_LED_ON:
  121. #if (STATUS_LED_ACTIVE == 0)
  122. immr->STATUS_LED_DAT &= ~(ld->mask);
  123. #else
  124. immr->STATUS_LED_DAT |= ld->mask ;
  125. #endif
  126. break;
  127. case STATUS_LED_OFF:
  128. #if (STATUS_LED_ACTIVE == 0)
  129. immr->STATUS_LED_DAT |= ld->mask ;
  130. #else
  131. immr->STATUS_LED_DAT &= ~(ld->mask);
  132. #endif
  133. break;
  134. }
  135. ld->state = state;
  136. }
  137. #endif /* CONFIG_STATUS_LED */