devices.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * (C) Copyright 2000
  3. * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
  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 <config.h>
  24. #include <common.h>
  25. #include <stdarg.h>
  26. #include <malloc.h>
  27. #include <devices.h>
  28. #ifdef CONFIG_LOGBUFFER
  29. #include <logbuff.h>
  30. #endif
  31. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
  32. #include <i2c.h>
  33. #endif
  34. list_t devlist = 0;
  35. device_t *stdio_devices[] = { NULL, NULL, NULL };
  36. char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
  37. #if defined(CONFIG_SPLASH_SCREEN) && !defined(CFG_DEVICE_NULLDEV)
  38. #define CFG_DEVICE_NULLDEV 1
  39. #endif
  40. #ifdef CFG_DEVICE_NULLDEV
  41. void nulldev_putc(const char c)
  42. {
  43. /* nulldev is empty! */
  44. }
  45. void nulldev_puts(const char *s)
  46. {
  47. /* nulldev is empty! */
  48. }
  49. int nulldev_input(void)
  50. {
  51. /* nulldev is empty! */
  52. return 0;
  53. }
  54. #endif
  55. /**************************************************************************
  56. * SYSTEM DRIVERS
  57. **************************************************************************
  58. */
  59. static void drv_system_init (void)
  60. {
  61. device_t dev;
  62. memset (&dev, 0, sizeof (dev));
  63. strcpy (dev.name, "serial");
  64. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  65. #if CONFIG_SERIAL_SOFTWARE_FIFO
  66. dev.putc = serial_buffered_putc;
  67. dev.puts = serial_buffered_puts;
  68. dev.getc = serial_buffered_getc;
  69. dev.tstc = serial_buffered_tstc;
  70. #else
  71. dev.putc = serial_putc;
  72. dev.puts = serial_puts;
  73. dev.getc = serial_getc;
  74. dev.tstc = serial_tstc;
  75. #endif
  76. device_register (&dev);
  77. #ifdef CFG_DEVICE_NULLDEV
  78. memset (&dev, 0, sizeof (dev));
  79. strcpy (dev.name, "nulldev");
  80. dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_SYSTEM;
  81. dev.putc = nulldev_putc;
  82. dev.puts = nulldev_puts;
  83. dev.getc = nulldev_input;
  84. dev.tstc = nulldev_input;
  85. device_register (&dev);
  86. #endif
  87. }
  88. /**************************************************************************
  89. * DEVICES
  90. **************************************************************************
  91. */
  92. int device_register (device_t * dev)
  93. {
  94. ListInsertItem (devlist, dev, LIST_END);
  95. return 0;
  96. }
  97. /* deregister the device "devname".
  98. * returns 0 if success, -1 if device is assigned and 1 if devname not found
  99. */
  100. #ifdef CFG_DEVICE_DEREGISTER
  101. int device_deregister(char *devname)
  102. {
  103. int i,l,dev_index;
  104. device_t *dev = NULL;
  105. char temp_names[3][8];
  106. dev_index=-1;
  107. for (i=1; i<=ListNumItems(devlist); i++) {
  108. dev = ListGetPtrToItem (devlist, i);
  109. if(strcmp(dev->name,devname)==0) {
  110. dev_index=i;
  111. break;
  112. }
  113. }
  114. if(dev_index<0) /* device not found */
  115. return 0;
  116. /* get stdio devices (ListRemoveItem changes the dev list) */
  117. for (l=0 ; l< MAX_FILES; l++) {
  118. if (stdio_devices[l] == dev) {
  119. /* Device is assigned -> report error */
  120. return -1;
  121. }
  122. memcpy (&temp_names[l][0],
  123. stdio_devices[l]->name,
  124. sizeof(stdio_devices[l]->name));
  125. }
  126. ListRemoveItem(devlist,NULL,dev_index);
  127. /* reassign Device list */
  128. for (i=1; i<=ListNumItems(devlist); i++) {
  129. dev = ListGetPtrToItem (devlist, i);
  130. for (l=0 ; l< MAX_FILES; l++) {
  131. if(strcmp(dev->name,temp_names[l])==0) {
  132. stdio_devices[l] = dev;
  133. }
  134. }
  135. }
  136. return 0;
  137. }
  138. #endif /* CFG_DEVICE_DEREGISTER */
  139. int devices_init (void)
  140. {
  141. DECLARE_GLOBAL_DATA_PTR;
  142. #ifndef CONFIG_ARM /* already relocated for current ARM implementation */
  143. ulong relocation_offset = gd->reloc_off;
  144. int i;
  145. /* relocate device name pointers */
  146. for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
  147. stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
  148. relocation_offset);
  149. }
  150. #endif
  151. /* Initialize the list */
  152. devlist = ListCreate (sizeof (device_t));
  153. if (devlist == NULL) {
  154. eputs ("Cannot initialize the list of devices!\n");
  155. return -1;
  156. }
  157. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C)
  158. i2c_init (CFG_I2C_SPEED, CFG_I2C_SLAVE);
  159. #endif
  160. #ifdef CONFIG_LCD
  161. drv_lcd_init ();
  162. #endif
  163. #if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
  164. drv_video_init ();
  165. #endif
  166. #ifdef CONFIG_KEYBOARD
  167. drv_keyboard_init ();
  168. #endif
  169. #ifdef CONFIG_LOGBUFFER
  170. drv_logbuff_init ();
  171. #endif
  172. drv_system_init ();
  173. gd-> flags |= GD_FLG_DEVINIT; /* device initialization done */
  174. return (0);
  175. }
  176. int devices_done (void)
  177. {
  178. ListDispose (devlist);
  179. return 0;
  180. }