serial.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 CFG_POST & CFG_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_smh_device;
  22. extern struct serial_device serial_scc_device;
  23. extern struct serial_device *default_serial_console(void);
  24. #if defined(CONFIG_MPC83xx) || defined(CONFIG_MPC85xx) || \
  25. defined(CONFIG_MPC86xx) || \
  26. defined(CONFIG_ARCH_TEGRA) || defined(CONFIG_SYS_COREBOOT) || \
  27. defined(CONFIG_MICROBLAZE)
  28. extern struct serial_device serial0_device;
  29. extern struct serial_device serial1_device;
  30. #endif
  31. extern struct serial_device eserial1_device;
  32. extern struct serial_device eserial2_device;
  33. extern struct serial_device eserial3_device;
  34. extern struct serial_device eserial4_device;
  35. extern struct serial_device eserial5_device;
  36. extern struct serial_device eserial6_device;
  37. extern void serial_register(struct serial_device *);
  38. extern void serial_stdio_init(void);
  39. extern int serial_assign(const char *name);
  40. extern void serial_reinit_all(void);
  41. int serial_initialize(void);
  42. /* For usbtty */
  43. #ifdef CONFIG_USB_TTY
  44. struct stdio_dev;
  45. int usbtty_getc(struct stdio_dev *dev);
  46. void usbtty_putc(struct stdio_dev *dev, const char c);
  47. void usbtty_puts(struct stdio_dev *dev, const char *str);
  48. int usbtty_tstc(struct stdio_dev *dev);
  49. #else
  50. /* stubs */
  51. #define usbtty_getc(dev) 0
  52. #define usbtty_putc(dev, a)
  53. #define usbtty_puts(dev, a)
  54. #define usbtty_tstc(dev) 0
  55. #endif /* CONFIG_USB_TTY */
  56. struct udevice;
  57. enum serial_par {
  58. SERIAL_PAR_NONE,
  59. SERIAL_PAR_ODD,
  60. SERIAL_PAR_EVEN
  61. };
  62. #define SERIAL_PAR_SHIFT 0
  63. #define SERIAL_PAR_MASK (0x03 << SERIAL_PAR_SHIFT)
  64. #define SERIAL_SET_PARITY(parity) \
  65. ((parity << SERIAL_PAR_SHIFT) & SERIAL_PAR_MASK)
  66. #define SERIAL_GET_PARITY(config) \
  67. ((config & SERIAL_PAR_MASK) >> SERIAL_PAR_SHIFT)
  68. enum serial_bits {
  69. SERIAL_5_BITS,
  70. SERIAL_6_BITS,
  71. SERIAL_7_BITS,
  72. SERIAL_8_BITS
  73. };
  74. #define SERIAL_BITS_SHIFT 2
  75. #define SERIAL_BITS_MASK (0x3 << SERIAL_BITS_SHIFT)
  76. #define SERIAL_SET_BITS(bits) \
  77. ((bits << SERIAL_BITS_SHIFT) & SERIAL_BITS_MASK)
  78. #define SERIAL_GET_BITS(config) \
  79. ((config & SERIAL_BITS_MASK) >> SERIAL_BITS_SHIFT)
  80. enum serial_stop {
  81. SERIAL_HALF_STOP, /* 0.5 stop bit */
  82. SERIAL_ONE_STOP, /* 1 stop bit */
  83. SERIAL_ONE_HALF_STOP, /* 1.5 stop bit */
  84. SERIAL_TWO_STOP /* 2 stop bit */
  85. };
  86. #define SERIAL_STOP_SHIFT 4
  87. #define SERIAL_STOP_MASK (0x3 << SERIAL_STOP_SHIFT)
  88. #define SERIAL_SET_STOP(stop) \
  89. ((stop << SERIAL_STOP_SHIFT) & SERIAL_STOP_MASK)
  90. #define SERIAL_GET_STOP(config) \
  91. ((config & SERIAL_STOP_MASK) >> SERIAL_STOP_SHIFT)
  92. #define SERIAL_CONFIG(par, bits, stop) \
  93. (par << SERIAL_PAR_SHIFT | \
  94. bits << SERIAL_BITS_SHIFT | \
  95. stop << SERIAL_STOP_SHIFT)
  96. #define SERIAL_DEFAULT_CONFIG \
  97. (SERIAL_PAR_NONE << SERIAL_PAR_SHIFT | \
  98. SERIAL_8_BITS << SERIAL_BITS_SHIFT | \
  99. SERIAL_ONE_STOP << SERIAL_STOP_SHIFT)
  100. enum serial_chip_type {
  101. SERIAL_CHIP_UNKNOWN = -1,
  102. SERIAL_CHIP_16550_COMPATIBLE,
  103. };
  104. enum adr_space_type {
  105. SERIAL_ADDRESS_SPACE_MEMORY = 0,
  106. SERIAL_ADDRESS_SPACE_IO,
  107. };
  108. /**
  109. * struct serial_device_info - structure to hold serial device info
  110. *
  111. * @type: type of the UART chip
  112. * @addr_space: address space to access the registers
  113. * @addr: physical address of the registers
  114. * @reg_width: size (in bytes) of the IO accesses to the registers
  115. * @reg_offset: offset to apply to the @addr from the start of the registers
  116. * @reg_shift: quantity to shift the register offsets by
  117. * @clock: UART base clock speed in Hz
  118. * @baudrate: baud rate
  119. */
  120. struct serial_device_info {
  121. enum serial_chip_type type;
  122. enum adr_space_type addr_space;
  123. ulong addr;
  124. u8 reg_width;
  125. u8 reg_offset;
  126. u8 reg_shift;
  127. unsigned int clock;
  128. unsigned int baudrate;
  129. };
  130. #define SERIAL_DEFAULT_ADDRESS 0xBADACCE5
  131. #define SERIAL_DEFAULT_CLOCK (16 * 115200)
  132. /**
  133. * struct struct dm_serial_ops - Driver model serial operations
  134. *
  135. * The uclass interface is implemented by all serial devices which use
  136. * driver model.
  137. */
  138. struct dm_serial_ops {
  139. /**
  140. * setbrg() - Set up the baud rate generator
  141. *
  142. * Adjust baud rate divisors to set up a new baud rate for this
  143. * device. Not all devices will support all rates. If the rate
  144. * cannot be supported, the driver is free to select the nearest
  145. * available rate. or return -EINVAL if this is not possible.
  146. *
  147. * @dev: Device pointer
  148. * @baudrate: New baud rate to use
  149. * @return 0 if OK, -ve on error
  150. */
  151. int (*setbrg)(struct udevice *dev, int baudrate);
  152. /**
  153. * getc() - Read a character and return it
  154. *
  155. * If no character is available, this should return -EAGAIN without
  156. * waiting.
  157. *
  158. * @dev: Device pointer
  159. * @return character (0..255), -ve on error
  160. */
  161. int (*getc)(struct udevice *dev);
  162. /**
  163. * putc() - Write a character
  164. *
  165. * @dev: Device pointer
  166. * @ch: character to write
  167. * @return 0 if OK, -ve on error
  168. */
  169. int (*putc)(struct udevice *dev, const char ch);
  170. /**
  171. * puts() - Write a string
  172. *
  173. * This writes a string. This function should be implemented only if
  174. * writing multiple characters at once is more performant than just
  175. * calling putc() in a loop.
  176. *
  177. * If the whole string cannot be written at once, then this function
  178. * should return the number of characters written. Returning a negative
  179. * error code implies that no characters were written. If this function
  180. * returns 0, then it will be called again with the same arguments.
  181. *
  182. * @dev: Device pointer
  183. * @s: The string to write
  184. * @len: The length of the string to write.
  185. * @return The number of characters written on success, or -ve on error
  186. */
  187. ssize_t (*puts)(struct udevice *dev, const char *s, size_t len);
  188. /**
  189. * pending() - Check if input/output characters are waiting
  190. *
  191. * This can be used to return an indication of the number of waiting
  192. * characters if the driver knows this (e.g. by looking at the FIFO
  193. * level). It is acceptable to return 1 if an indeterminant number
  194. * of characters is waiting.
  195. *
  196. * This method is optional.
  197. *
  198. * @dev: Device pointer
  199. * @input: true to check input characters, false for output
  200. * @return number of waiting characters, 0 for none, -ve on error
  201. */
  202. int (*pending)(struct udevice *dev, bool input);
  203. /**
  204. * clear() - Clear the serial FIFOs/holding registers
  205. *
  206. * This method is optional.
  207. *
  208. * This quickly clears any input/output characters from the UART.
  209. * If this is not possible, but characters still exist, then it
  210. * is acceptable to return -EAGAIN (try again) or -EINVAL (not
  211. * supported).
  212. *
  213. * @dev: Device pointer
  214. * @return 0 if OK, -ve on error
  215. */
  216. int (*clear)(struct udevice *dev);
  217. #if CFG_POST & CFG_SYS_POST_UART
  218. /**
  219. * loop() - Control serial device loopback mode
  220. *
  221. * @dev: Device pointer
  222. * @on: 1 to turn loopback on, 0 to turn if off
  223. */
  224. int (*loop)(struct udevice *dev, int on);
  225. #endif
  226. /**
  227. * getconfig() - Get the uart configuration
  228. * (parity, 5/6/7/8 bits word length, stop bits)
  229. *
  230. * Get a current config for this device.
  231. *
  232. * @dev: Device pointer
  233. * @serial_config: Returns config information (see SERIAL_... above)
  234. * @return 0 if OK, -ve on error
  235. */
  236. int (*getconfig)(struct udevice *dev, uint *serial_config);
  237. /**
  238. * setconfig() - Set up the uart configuration
  239. * (parity, 5/6/7/8 bits word length, stop bits)
  240. *
  241. * Set up a new config for this device.
  242. *
  243. * @dev: Device pointer
  244. * @serial_config: number of bits, parity and number of stopbits to use
  245. * @return 0 if OK, -ve on error
  246. */
  247. int (*setconfig)(struct udevice *dev, uint serial_config);
  248. /**
  249. * getinfo() - Get serial device information
  250. *
  251. * @dev: Device pointer
  252. * @info: struct serial_device_info to fill
  253. * @return 0 if OK, -ve on error
  254. */
  255. int (*getinfo)(struct udevice *dev, struct serial_device_info *info);
  256. };
  257. /**
  258. * struct serial_dev_priv - information about a device used by the uclass
  259. *
  260. * @sdev: stdio device attached to this uart
  261. *
  262. * @buf: Pointer to the RX buffer
  263. * @rd_ptr: Read pointer in the RX buffer
  264. * @wr_ptr: Write pointer in the RX buffer
  265. */
  266. struct serial_dev_priv {
  267. struct stdio_dev *sdev;
  268. char *buf;
  269. int rd_ptr;
  270. int wr_ptr;
  271. };
  272. /* Access the serial operations for a device */
  273. #define serial_get_ops(dev) ((struct dm_serial_ops *)(dev)->driver->ops)
  274. /**
  275. * serial_getconfig() - Get the uart configuration
  276. * (parity, 5/6/7/8 bits word length, stop bits)
  277. *
  278. * Get a current config for this device.
  279. *
  280. * @dev: Device pointer
  281. * @serial_config: Returns config information (see SERIAL_... above)
  282. * Return: 0 if OK, -ve on error
  283. */
  284. int serial_getconfig(struct udevice *dev, uint *config);
  285. /**
  286. * serial_setconfig() - Set up the uart configuration
  287. * (parity, 5/6/7/8 bits word length, stop bits)
  288. *
  289. * Set up a new config for this device.
  290. *
  291. * @dev: Device pointer
  292. * @serial_config: number of bits, parity and number of stopbits to use
  293. * Return: 0 if OK, -ve on error
  294. */
  295. int serial_setconfig(struct udevice *dev, uint config);
  296. /**
  297. * serial_getinfo() - Get serial device information
  298. *
  299. * @dev: Device pointer
  300. * @info: struct serial_device_info to fill
  301. * Return: 0 if OK, -ve on error
  302. */
  303. int serial_getinfo(struct udevice *dev, struct serial_device_info *info);
  304. void atmel_serial_initialize(void);
  305. void mcf_serial_initialize(void);
  306. void mpc85xx_serial_initialize(void);
  307. void mxc_serial_initialize(void);
  308. void ns16550_serial_initialize(void);
  309. void pl01x_serial_initialize(void);
  310. void pxa_serial_initialize(void);
  311. void sh_serial_initialize(void);
  312. /**
  313. * serial_printf() - Write a formatted string to the serial console
  314. *
  315. * The total size of the output must be less than CONFIG_SYS_PBSIZE.
  316. *
  317. * @fmt: Printf format string, followed by format arguments
  318. * Return: number of characters written
  319. */
  320. int serial_printf(const char *fmt, ...)
  321. __attribute__ ((format (__printf__, 1, 2)));
  322. int serial_init(void);
  323. void serial_setbrg(void);
  324. void serial_putc(const char ch);
  325. void serial_putc_raw(const char ch);
  326. void serial_puts(const char *str);
  327. #if defined(CONFIG_CONSOLE_FLUSH_SUPPORT) && CONFIG_IS_ENABLED(DM_SERIAL)
  328. void serial_flush(void);
  329. #else
  330. static inline void serial_flush(void) {}
  331. #endif
  332. int serial_getc(void);
  333. int serial_tstc(void);
  334. #endif