8250.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for 8250/16550-type serial ports
  4. *
  5. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  6. *
  7. * Copyright (C) 2001 Russell King.
  8. */
  9. #include <linux/serial_8250.h>
  10. #include <linux/serial_reg.h>
  11. #include <linux/dmaengine.h>
  12. struct uart_8250_dma {
  13. int (*tx_dma)(struct uart_8250_port *p);
  14. int (*rx_dma)(struct uart_8250_port *p);
  15. /* Filter function */
  16. dma_filter_fn fn;
  17. /* Parameter to the filter function */
  18. void *rx_param;
  19. void *tx_param;
  20. struct dma_slave_config rxconf;
  21. struct dma_slave_config txconf;
  22. struct dma_chan *rxchan;
  23. struct dma_chan *txchan;
  24. /* Device address base for DMA operations */
  25. phys_addr_t rx_dma_addr;
  26. phys_addr_t tx_dma_addr;
  27. /* DMA address of the buffer in memory */
  28. dma_addr_t rx_addr;
  29. dma_addr_t tx_addr;
  30. dma_cookie_t rx_cookie;
  31. dma_cookie_t tx_cookie;
  32. void *rx_buf;
  33. size_t rx_size;
  34. size_t tx_size;
  35. unsigned char tx_running;
  36. unsigned char tx_err;
  37. unsigned char rx_running;
  38. };
  39. struct old_serial_port {
  40. unsigned int uart;
  41. unsigned int baud_base;
  42. unsigned int port;
  43. unsigned int irq;
  44. upf_t flags;
  45. unsigned char io_type;
  46. unsigned char __iomem *iomem_base;
  47. unsigned short iomem_reg_shift;
  48. };
  49. struct serial8250_config {
  50. const char *name;
  51. unsigned short fifo_size;
  52. unsigned short tx_loadsz;
  53. unsigned char fcr;
  54. unsigned char rxtrig_bytes[UART_FCR_R_TRIG_MAX_STATE];
  55. unsigned int flags;
  56. };
  57. #define UART_CAP_FIFO (1 << 8) /* UART has FIFO */
  58. #define UART_CAP_EFR (1 << 9) /* UART has EFR */
  59. #define UART_CAP_SLEEP (1 << 10) /* UART has IER sleep */
  60. #define UART_CAP_AFE (1 << 11) /* MCR-based hw flow control */
  61. #define UART_CAP_UUE (1 << 12) /* UART needs IER bit 6 set (Xscale) */
  62. #define UART_CAP_RTOIE (1 << 13) /* UART needs IER bit 4 set (Xscale, Tegra) */
  63. #define UART_CAP_HFIFO (1 << 14) /* UART has a "hidden" FIFO */
  64. #define UART_CAP_RPM (1 << 15) /* Runtime PM is active while idle */
  65. #define UART_CAP_IRDA (1 << 16) /* UART supports IrDA line discipline */
  66. #define UART_CAP_MINI (1 << 17) /* Mini UART on BCM283X family lacks:
  67. * STOP PARITY EPAR SPAR WLEN5 WLEN6
  68. */
  69. #define UART_BUG_QUOT (1 << 0) /* UART has buggy quot LSB */
  70. #define UART_BUG_TXEN (1 << 1) /* UART has buggy TX IIR status */
  71. #define UART_BUG_NOMSR (1 << 2) /* UART has buggy MSR status bits (Au1x00) */
  72. #define UART_BUG_THRE (1 << 3) /* UART has buggy THRE reassertion */
  73. #define UART_BUG_PARITY (1 << 4) /* UART mishandles parity if FIFO enabled */
  74. #ifdef CONFIG_SERIAL_8250_SHARE_IRQ
  75. #define SERIAL8250_SHARE_IRQS 1
  76. #else
  77. #define SERIAL8250_SHARE_IRQS 0
  78. #endif
  79. #define SERIAL8250_PORT_FLAGS(_base, _irq, _flags) \
  80. { \
  81. .iobase = _base, \
  82. .irq = _irq, \
  83. .uartclk = 1843200, \
  84. .iotype = UPIO_PORT, \
  85. .flags = UPF_BOOT_AUTOCONF | (_flags), \
  86. }
  87. #define SERIAL8250_PORT(_base, _irq) SERIAL8250_PORT_FLAGS(_base, _irq, 0)
  88. static inline int serial_in(struct uart_8250_port *up, int offset)
  89. {
  90. return up->port.serial_in(&up->port, offset);
  91. }
  92. static inline void serial_out(struct uart_8250_port *up, int offset, int value)
  93. {
  94. up->port.serial_out(&up->port, offset, value);
  95. }
  96. void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p);
  97. static inline int serial_dl_read(struct uart_8250_port *up)
  98. {
  99. return up->dl_read(up);
  100. }
  101. static inline void serial_dl_write(struct uart_8250_port *up, int value)
  102. {
  103. up->dl_write(up, value);
  104. }
  105. struct uart_8250_port *serial8250_get_port(int line);
  106. void serial8250_rpm_get(struct uart_8250_port *p);
  107. void serial8250_rpm_put(struct uart_8250_port *p);
  108. void serial8250_rpm_get_tx(struct uart_8250_port *p);
  109. void serial8250_rpm_put_tx(struct uart_8250_port *p);
  110. int serial8250_em485_init(struct uart_8250_port *p);
  111. void serial8250_em485_destroy(struct uart_8250_port *p);
  112. static inline void serial8250_out_MCR(struct uart_8250_port *up, int value)
  113. {
  114. serial_out(up, UART_MCR, value);
  115. }
  116. static inline int serial8250_in_MCR(struct uart_8250_port *up)
  117. {
  118. return serial_in(up, UART_MCR);
  119. }
  120. #if defined(__alpha__) && !defined(CONFIG_PCI)
  121. /*
  122. * Digital did something really horribly wrong with the OUT1 and OUT2
  123. * lines on at least some ALPHA's. The failure mode is that if either
  124. * is cleared, the machine locks up with endless interrupts.
  125. */
  126. #define ALPHA_KLUDGE_MCR (UART_MCR_OUT2 | UART_MCR_OUT1)
  127. #else
  128. #define ALPHA_KLUDGE_MCR 0
  129. #endif
  130. #ifdef CONFIG_SERIAL_8250_PNP
  131. int serial8250_pnp_init(void);
  132. void serial8250_pnp_exit(void);
  133. #else
  134. static inline int serial8250_pnp_init(void) { return 0; }
  135. static inline void serial8250_pnp_exit(void) { }
  136. #endif
  137. #ifdef CONFIG_SERIAL_8250_FINTEK
  138. int fintek_8250_probe(struct uart_8250_port *uart);
  139. #else
  140. static inline int fintek_8250_probe(struct uart_8250_port *uart) { return 0; }
  141. #endif
  142. #ifdef CONFIG_ARCH_OMAP1
  143. static inline int is_omap1_8250(struct uart_8250_port *pt)
  144. {
  145. int res;
  146. switch (pt->port.mapbase) {
  147. case OMAP1_UART1_BASE:
  148. case OMAP1_UART2_BASE:
  149. case OMAP1_UART3_BASE:
  150. res = 1;
  151. break;
  152. default:
  153. res = 0;
  154. break;
  155. }
  156. return res;
  157. }
  158. static inline int is_omap1510_8250(struct uart_8250_port *pt)
  159. {
  160. if (!cpu_is_omap1510())
  161. return 0;
  162. return is_omap1_8250(pt);
  163. }
  164. #else
  165. static inline int is_omap1_8250(struct uart_8250_port *pt)
  166. {
  167. return 0;
  168. }
  169. static inline int is_omap1510_8250(struct uart_8250_port *pt)
  170. {
  171. return 0;
  172. }
  173. #endif
  174. #ifdef CONFIG_SERIAL_8250_DMA
  175. extern int serial8250_tx_dma(struct uart_8250_port *);
  176. extern int serial8250_rx_dma(struct uart_8250_port *);
  177. extern void serial8250_rx_dma_flush(struct uart_8250_port *);
  178. extern int serial8250_request_dma(struct uart_8250_port *);
  179. extern void serial8250_release_dma(struct uart_8250_port *);
  180. #else
  181. static inline int serial8250_tx_dma(struct uart_8250_port *p)
  182. {
  183. return -1;
  184. }
  185. static inline int serial8250_rx_dma(struct uart_8250_port *p)
  186. {
  187. return -1;
  188. }
  189. static inline void serial8250_rx_dma_flush(struct uart_8250_port *p) { }
  190. static inline int serial8250_request_dma(struct uart_8250_port *p)
  191. {
  192. return -1;
  193. }
  194. static inline void serial8250_release_dma(struct uart_8250_port *p) { }
  195. #endif
  196. static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
  197. {
  198. unsigned char status;
  199. status = serial_in(up, 0x04); /* EXCR2 */
  200. #define PRESL(x) ((x) & 0x30)
  201. if (PRESL(status) == 0x10) {
  202. /* already in high speed mode */
  203. return 0;
  204. } else {
  205. status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
  206. status |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
  207. serial_out(up, 0x04, status);
  208. }
  209. return 1;
  210. }
  211. static inline int serial_index(struct uart_port *port)
  212. {
  213. return port->minor - 64;
  214. }