serial.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #ifndef __SERIAL_H__
  2. #define __SERIAL_H__
  3. #include <post.h>
  4. struct serial_device {
  5. /* enough bytes to match alignment of following func pointer */
  6. char name[16];
  7. int (*start)(void);
  8. int (*stop)(void);
  9. void (*setbrg)(void);
  10. int (*getc)(void);
  11. int (*tstc)(void);
  12. void (*putc)(const char c);
  13. void (*puts)(const char *s);
  14. #if CONFIG_POST & CONFIG_SYS_POST_UART
  15. void (*loop)(int);
  16. #endif
  17. struct serial_device *next;
  18. };
  19. void default_serial_puts(const char *s);
  20. extern struct serial_device serial_smc_device;
  21. extern struct serial_device serial_scc_device;
  22. extern struct serial_device *default_serial_console(void);
  23. #if defined(CONFIG_MPC83xx) || defined(CONFIG_MPC85xx) || \
  24. defined(CONFIG_MPC86xx) || \
  25. defined(CONFIG_TEGRA) || defined(CONFIG_SYS_COREBOOT) || \
  26. defined(CONFIG_MICROBLAZE)
  27. extern struct serial_device serial0_device;
  28. extern struct serial_device serial1_device;
  29. #endif
  30. extern struct serial_device eserial1_device;
  31. extern struct serial_device eserial2_device;
  32. extern struct serial_device eserial3_device;
  33. extern struct serial_device eserial4_device;
  34. extern struct serial_device eserial5_device;
  35. extern struct serial_device eserial6_device;
  36. extern void serial_register(struct serial_device *);
  37. extern void serial_initialize(void);
  38. extern void serial_stdio_init(void);
  39. extern int serial_assign(const char *name);
  40. extern void serial_reinit_all(void);
  41. /* For usbtty */
  42. #ifdef CONFIG_USB_TTY
  43. extern int usbtty_getc(void);
  44. extern void usbtty_putc(const char c);
  45. extern void usbtty_puts(const char *str);
  46. extern int usbtty_tstc(void);
  47. #else
  48. /* stubs */
  49. #define usbtty_getc() 0
  50. #define usbtty_putc(a)
  51. #define usbtty_puts(a)
  52. #define usbtty_tstc() 0
  53. #endif /* CONFIG_USB_TTY */
  54. struct udevice;
  55. enum serial_par {
  56. SERIAL_PAR_NONE,
  57. SERIAL_PAR_ODD,
  58. SERIAL_PAR_EVEN
  59. };
  60. #define SERIAL_PAR_SHIFT 0
  61. #define SERIAL_PAR_MASK (0x03 << SERIAL_PAR_SHIFT)
  62. #define SERIAL_GET_PARITY(config) \
  63. ((config & SERIAL_PAR_MASK) >> SERIAL_PAR_SHIFT)
  64. enum serial_bits {
  65. SERIAL_5_BITS,
  66. SERIAL_6_BITS,
  67. SERIAL_7_BITS,
  68. SERIAL_8_BITS
  69. };
  70. #define SERIAL_BITS_SHIFT 2
  71. #define SERIAL_BITS_MASK (0x3 << SERIAL_BITS_SHIFT)
  72. #define SERIAL_GET_BITS(config) \
  73. ((config & SERIAL_BITS_MASK) >> SERIAL_BITS_SHIFT)
  74. enum serial_stop {
  75. SERIAL_HALF_STOP, /* 0.5 stop bit */
  76. SERIAL_ONE_STOP, /* 1 stop bit */
  77. SERIAL_ONE_HALF_STOP, /* 1.5 stop bit */
  78. SERIAL_TWO_STOP /* 2 stop bit */
  79. };
  80. #define SERIAL_STOP_SHIFT 4
  81. #define SERIAL_STOP_MASK (0x3 << SERIAL_STOP_SHIFT)
  82. #define SERIAL_GET_STOP(config) \
  83. ((config & SERIAL_STOP_MASK) >> SERIAL_STOP_SHIFT)
  84. #define SERIAL_CONFIG(par, bits, stop) \
  85. (par << SERIAL_PAR_SHIFT | \
  86. bits << SERIAL_BITS_SHIFT | \
  87. stop << SERIAL_STOP_SHIFT)
  88. #define SERIAL_DEFAULT_CONFIG SERIAL_PAR_NONE << SERIAL_PAR_SHIFT | \
  89. SERIAL_8_BITS << SERIAL_BITS_SHIFT | \
  90. SERIAL_ONE_STOP << SERIAL_STOP_SHIFT
  91. /**
  92. * struct struct dm_serial_ops - Driver model serial operations
  93. *
  94. * The uclass interface is implemented by all serial devices which use
  95. * driver model.
  96. */
  97. struct dm_serial_ops {
  98. /**
  99. * setbrg() - Set up the baud rate generator
  100. *
  101. * Adjust baud rate divisors to set up a new baud rate for this
  102. * device. Not all devices will support all rates. If the rate
  103. * cannot be supported, the driver is free to select the nearest
  104. * available rate. or return -EINVAL if this is not possible.
  105. *
  106. * @dev: Device pointer
  107. * @baudrate: New baud rate to use
  108. * @return 0 if OK, -ve on error
  109. */
  110. int (*setbrg)(struct udevice *dev, int baudrate);
  111. /**
  112. * getc() - Read a character and return it
  113. *
  114. * If no character is available, this should return -EAGAIN without
  115. * waiting.
  116. *
  117. * @dev: Device pointer
  118. * @return character (0..255), -ve on error
  119. */
  120. int (*getc)(struct udevice *dev);
  121. /**
  122. * putc() - Write a character
  123. *
  124. * @dev: Device pointer
  125. * @ch: character to write
  126. * @return 0 if OK, -ve on error
  127. */
  128. int (*putc)(struct udevice *dev, const char ch);
  129. /**
  130. * pending() - Check if input/output characters are waiting
  131. *
  132. * This can be used to return an indication of the number of waiting
  133. * characters if the driver knows this (e.g. by looking at the FIFO
  134. * level). It is acceptable to return 1 if an indeterminant number
  135. * of characters is waiting.
  136. *
  137. * This method is optional.
  138. *
  139. * @dev: Device pointer
  140. * @input: true to check input characters, false for output
  141. * @return number of waiting characters, 0 for none, -ve on error
  142. */
  143. int (*pending)(struct udevice *dev, bool input);
  144. /**
  145. * clear() - Clear the serial FIFOs/holding registers
  146. *
  147. * This method is optional.
  148. *
  149. * This quickly clears any input/output characters from the UART.
  150. * If this is not possible, but characters still exist, then it
  151. * is acceptable to return -EAGAIN (try again) or -EINVAL (not
  152. * supported).
  153. *
  154. * @dev: Device pointer
  155. * @return 0 if OK, -ve on error
  156. */
  157. int (*clear)(struct udevice *dev);
  158. #if CONFIG_POST & CONFIG_SYS_POST_UART
  159. /**
  160. * loop() - Control serial device loopback mode
  161. *
  162. * @dev: Device pointer
  163. * @on: 1 to turn loopback on, 0 to turn if off
  164. */
  165. int (*loop)(struct udevice *dev, int on);
  166. #endif
  167. /**
  168. * setconfig() - Set up the uart configuration
  169. * (parity, 5/6/7/8 bits word length, stop bits)
  170. *
  171. * Set up a new config for this device.
  172. *
  173. * @dev: Device pointer
  174. * @serial_config: number of bits, parity and number of stopbits to use
  175. * @return 0 if OK, -ve on error
  176. */
  177. int (*setconfig)(struct udevice *dev, uint serial_config);
  178. };
  179. /**
  180. * struct serial_dev_priv - information about a device used by the uclass
  181. *
  182. * @sdev: stdio device attached to this uart
  183. *
  184. * @buf: Pointer to the RX buffer
  185. * @rd_ptr: Read pointer in the RX buffer
  186. * @wr_ptr: Write pointer in the RX buffer
  187. */
  188. struct serial_dev_priv {
  189. struct stdio_dev *sdev;
  190. char *buf;
  191. int rd_ptr;
  192. int wr_ptr;
  193. };
  194. /* Access the serial operations for a device */
  195. #define serial_get_ops(dev) ((struct dm_serial_ops *)(dev)->driver->ops)
  196. void atmel_serial_initialize(void);
  197. void mcf_serial_initialize(void);
  198. void mpc85xx_serial_initialize(void);
  199. void mpc8xx_serial_initialize(void);
  200. void mxc_serial_initialize(void);
  201. void ns16550_serial_initialize(void);
  202. void pl01x_serial_initialize(void);
  203. void pxa_serial_initialize(void);
  204. void sh_serial_initialize(void);
  205. #endif