8250.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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/bits.h>
  10. #include <linux/serial_8250.h>
  11. #include <linux/serial_core.h>
  12. #include <linux/dmaengine.h>
  13. #include "../serial_mctrl_gpio.h"
  14. struct uart_8250_dma {
  15. int (*tx_dma)(struct uart_8250_port *p);
  16. int (*rx_dma)(struct uart_8250_port *p);
  17. void (*prepare_tx_dma)(struct uart_8250_port *p);
  18. void (*prepare_rx_dma)(struct uart_8250_port *p);
  19. /* Filter function */
  20. dma_filter_fn fn;
  21. /* Parameter to the filter function */
  22. void *rx_param;
  23. void *tx_param;
  24. struct dma_slave_config rxconf;
  25. struct dma_slave_config txconf;
  26. struct dma_chan *rxchan;
  27. struct dma_chan *txchan;
  28. /* Device address base for DMA operations */
  29. phys_addr_t rx_dma_addr;
  30. phys_addr_t tx_dma_addr;
  31. /* DMA address of the buffer in memory */
  32. dma_addr_t rx_addr;
  33. dma_addr_t tx_addr;
  34. dma_cookie_t rx_cookie;
  35. dma_cookie_t tx_cookie;
  36. void *rx_buf;
  37. size_t rx_size;
  38. size_t tx_size;
  39. unsigned char tx_running;
  40. unsigned char tx_err;
  41. unsigned char rx_running;
  42. };
  43. struct old_serial_port {
  44. unsigned int uart;
  45. unsigned int baud_base;
  46. unsigned int port;
  47. unsigned int irq;
  48. upf_t flags;
  49. unsigned char io_type;
  50. unsigned char __iomem *iomem_base;
  51. unsigned short iomem_reg_shift;
  52. };
  53. struct serial8250_config {
  54. const char *name;
  55. unsigned short fifo_size;
  56. unsigned short tx_loadsz;
  57. unsigned char fcr;
  58. unsigned char rxtrig_bytes[UART_FCR_R_TRIG_MAX_STATE];
  59. unsigned int flags;
  60. };
  61. #define UART_CAP_FIFO BIT(8) /* UART has FIFO */
  62. #define UART_CAP_EFR BIT(9) /* UART has EFR */
  63. #define UART_CAP_SLEEP BIT(10) /* UART has IER sleep */
  64. #define UART_CAP_AFE BIT(11) /* MCR-based hw flow control */
  65. #define UART_CAP_UUE BIT(12) /* UART needs IER bit 6 set (Xscale) */
  66. #define UART_CAP_RTOIE BIT(13) /* UART needs IER bit 4 set (Xscale, Tegra) */
  67. #define UART_CAP_HFIFO BIT(14) /* UART has a "hidden" FIFO */
  68. #define UART_CAP_RPM BIT(15) /* Runtime PM is active while idle */
  69. #define UART_CAP_IRDA BIT(16) /* UART supports IrDA line discipline */
  70. #define UART_CAP_MINI BIT(17) /* Mini UART on BCM283X family lacks:
  71. * STOP PARITY EPAR SPAR WLEN5 WLEN6
  72. */
  73. #define UART_CAP_NOTEMT BIT(18) /* UART without interrupt on TEMT available */
  74. #define UART_BUG_QUOT BIT(0) /* UART has buggy quot LSB */
  75. #define UART_BUG_TXEN BIT(1) /* UART has buggy TX IIR status */
  76. #define UART_BUG_NOMSR BIT(2) /* UART has buggy MSR status bits (Au1x00) */
  77. #define UART_BUG_THRE BIT(3) /* UART has buggy THRE reassertion */
  78. #define UART_BUG_TXRACE BIT(5) /* UART Tx fails to set remote DR */
  79. /* Module parameters */
  80. #define UART_NR CONFIG_SERIAL_8250_NR_UARTS
  81. extern unsigned int nr_uarts;
  82. #define SERIAL8250_PORT_FLAGS(_base, _irq, _flags) \
  83. { \
  84. .iobase = _base, \
  85. .irq = _irq, \
  86. .uartclk = 1843200, \
  87. .iotype = UPIO_PORT, \
  88. .flags = UPF_BOOT_AUTOCONF | (_flags), \
  89. }
  90. #define SERIAL8250_PORT(_base, _irq) SERIAL8250_PORT_FLAGS(_base, _irq, 0)
  91. extern struct uart_driver serial8250_reg;
  92. void serial8250_register_ports(struct uart_driver *drv, struct device *dev);
  93. /* Legacy ISA bus related APIs */
  94. typedef void (*serial8250_isa_config_fn)(int, struct uart_port *, u32 *);
  95. extern serial8250_isa_config_fn serial8250_isa_config;
  96. void serial8250_isa_init_ports(void);
  97. extern struct platform_device *serial8250_isa_devs;
  98. extern const struct uart_ops *univ8250_port_base_ops;
  99. extern struct uart_ops univ8250_port_ops;
  100. static inline int serial_in(struct uart_8250_port *up, int offset)
  101. {
  102. return up->port.serial_in(&up->port, offset);
  103. }
  104. static inline void serial_out(struct uart_8250_port *up, int offset, int value)
  105. {
  106. up->port.serial_out(&up->port, offset, value);
  107. }
  108. /**
  109. * serial_lsr_in - Read LSR register and preserve flags across reads
  110. * @up: uart 8250 port
  111. *
  112. * Read LSR register and handle saving non-preserved flags across reads.
  113. * The flags that are not preserved across reads are stored into
  114. * up->lsr_saved_flags.
  115. *
  116. * Returns LSR value or'ed with the preserved flags (if any).
  117. */
  118. static inline u16 serial_lsr_in(struct uart_8250_port *up)
  119. {
  120. u16 lsr = up->lsr_saved_flags;
  121. lsr |= serial_in(up, UART_LSR);
  122. up->lsr_saved_flags = lsr & up->lsr_save_mask;
  123. return lsr;
  124. }
  125. /*
  126. * For the 16C950
  127. */
  128. static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
  129. {
  130. serial_out(up, UART_SCR, offset);
  131. serial_out(up, UART_ICR, value);
  132. }
  133. static unsigned int __maybe_unused serial_icr_read(struct uart_8250_port *up,
  134. int offset)
  135. {
  136. unsigned int value;
  137. serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
  138. serial_out(up, UART_SCR, offset);
  139. value = serial_in(up, UART_ICR);
  140. serial_icr_write(up, UART_ACR, up->acr);
  141. return value;
  142. }
  143. void serial8250_clear_fifos(struct uart_8250_port *p);
  144. void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p);
  145. void serial8250_fifo_wait_for_lsr_thre(struct uart_8250_port *up, unsigned int count);
  146. void serial8250_rpm_get(struct uart_8250_port *p);
  147. void serial8250_rpm_put(struct uart_8250_port *p);
  148. DEFINE_GUARD(serial8250_rpm, struct uart_8250_port *,
  149. serial8250_rpm_get(_T), serial8250_rpm_put(_T));
  150. static inline u32 serial_dl_read(struct uart_8250_port *up)
  151. {
  152. return up->dl_read(up);
  153. }
  154. static inline void serial_dl_write(struct uart_8250_port *up, u32 value)
  155. {
  156. up->dl_write(up, value);
  157. }
  158. static inline bool serial8250_set_THRI(struct uart_8250_port *up)
  159. {
  160. /* Port locked to synchronize UART_IER access against the console. */
  161. lockdep_assert_held_once(&up->port.lock);
  162. if (up->ier & UART_IER_THRI)
  163. return false;
  164. up->ier |= UART_IER_THRI;
  165. serial_out(up, UART_IER, up->ier);
  166. return true;
  167. }
  168. static inline bool serial8250_clear_THRI(struct uart_8250_port *up)
  169. {
  170. /* Port locked to synchronize UART_IER access against the console. */
  171. lockdep_assert_held_once(&up->port.lock);
  172. if (!(up->ier & UART_IER_THRI))
  173. return false;
  174. up->ier &= ~UART_IER_THRI;
  175. serial_out(up, UART_IER, up->ier);
  176. return true;
  177. }
  178. struct uart_8250_port *serial8250_setup_port(int index);
  179. struct uart_8250_port *serial8250_get_port(int line);
  180. int serial8250_em485_config(struct uart_port *port, struct ktermios *termios,
  181. struct serial_rs485 *rs485);
  182. void serial8250_em485_start_tx(struct uart_8250_port *p, bool toggle_ier);
  183. void serial8250_em485_stop_tx(struct uart_8250_port *p, bool toggle_ier);
  184. void serial8250_em485_destroy(struct uart_8250_port *p);
  185. extern struct serial_rs485 serial8250_em485_supported;
  186. /* MCR <-> TIOCM conversion */
  187. static inline int serial8250_TIOCM_to_MCR(int tiocm)
  188. {
  189. int mcr = 0;
  190. if (tiocm & TIOCM_RTS)
  191. mcr |= UART_MCR_RTS;
  192. if (tiocm & TIOCM_DTR)
  193. mcr |= UART_MCR_DTR;
  194. if (tiocm & TIOCM_OUT1)
  195. mcr |= UART_MCR_OUT1;
  196. if (tiocm & TIOCM_OUT2)
  197. mcr |= UART_MCR_OUT2;
  198. if (tiocm & TIOCM_LOOP)
  199. mcr |= UART_MCR_LOOP;
  200. return mcr;
  201. }
  202. static inline int serial8250_MCR_to_TIOCM(int mcr)
  203. {
  204. int tiocm = 0;
  205. if (mcr & UART_MCR_RTS)
  206. tiocm |= TIOCM_RTS;
  207. if (mcr & UART_MCR_DTR)
  208. tiocm |= TIOCM_DTR;
  209. if (mcr & UART_MCR_OUT1)
  210. tiocm |= TIOCM_OUT1;
  211. if (mcr & UART_MCR_OUT2)
  212. tiocm |= TIOCM_OUT2;
  213. if (mcr & UART_MCR_LOOP)
  214. tiocm |= TIOCM_LOOP;
  215. return tiocm;
  216. }
  217. /* MSR <-> TIOCM conversion */
  218. static inline int serial8250_MSR_to_TIOCM(int msr)
  219. {
  220. int tiocm = 0;
  221. if (msr & UART_MSR_DCD)
  222. tiocm |= TIOCM_CAR;
  223. if (msr & UART_MSR_RI)
  224. tiocm |= TIOCM_RNG;
  225. if (msr & UART_MSR_DSR)
  226. tiocm |= TIOCM_DSR;
  227. if (msr & UART_MSR_CTS)
  228. tiocm |= TIOCM_CTS;
  229. return tiocm;
  230. }
  231. static inline void serial8250_out_MCR(struct uart_8250_port *up, int value)
  232. {
  233. serial_out(up, UART_MCR, value);
  234. if (up->gpios)
  235. mctrl_gpio_set(up->gpios, serial8250_MCR_to_TIOCM(value));
  236. }
  237. static inline int serial8250_in_MCR(struct uart_8250_port *up)
  238. {
  239. int mctrl;
  240. mctrl = serial_in(up, UART_MCR);
  241. if (up->gpios) {
  242. unsigned int mctrl_gpio = 0;
  243. mctrl_gpio = mctrl_gpio_get_outputs(up->gpios, &mctrl_gpio);
  244. mctrl |= serial8250_TIOCM_to_MCR(mctrl_gpio);
  245. }
  246. return mctrl;
  247. }
  248. #ifdef CONFIG_SERIAL_8250_PNP
  249. int serial8250_pnp_init(void);
  250. void serial8250_pnp_exit(void);
  251. #else
  252. static inline int serial8250_pnp_init(void) { return 0; }
  253. static inline void serial8250_pnp_exit(void) { }
  254. #endif
  255. #ifdef CONFIG_SERIAL_8250_RSA
  256. void univ8250_rsa_support(struct uart_ops *ops, const struct uart_ops *core_ops);
  257. void rsa_enable(struct uart_8250_port *up);
  258. void rsa_disable(struct uart_8250_port *up);
  259. void rsa_autoconfig(struct uart_8250_port *up);
  260. void rsa_reset(struct uart_8250_port *up);
  261. #else
  262. static inline void univ8250_rsa_support(struct uart_ops *ops, const struct uart_ops *core_ops) { }
  263. static inline void rsa_enable(struct uart_8250_port *up) {}
  264. static inline void rsa_disable(struct uart_8250_port *up) {}
  265. static inline void rsa_autoconfig(struct uart_8250_port *up) {}
  266. static inline void rsa_reset(struct uart_8250_port *up) {}
  267. #endif
  268. #ifdef CONFIG_SERIAL_8250_FINTEK
  269. int fintek_8250_probe(struct uart_8250_port *uart);
  270. #else
  271. static inline int fintek_8250_probe(struct uart_8250_port *uart) { return 0; }
  272. #endif
  273. #ifdef CONFIG_ARCH_OMAP1
  274. #include <linux/soc/ti/omap1-soc.h>
  275. static inline int is_omap1_8250(struct uart_8250_port *pt)
  276. {
  277. int res;
  278. switch (pt->port.mapbase) {
  279. case OMAP1_UART1_BASE:
  280. case OMAP1_UART2_BASE:
  281. case OMAP1_UART3_BASE:
  282. res = 1;
  283. break;
  284. default:
  285. res = 0;
  286. break;
  287. }
  288. return res;
  289. }
  290. static inline int is_omap1510_8250(struct uart_8250_port *pt)
  291. {
  292. if (!cpu_is_omap1510())
  293. return 0;
  294. return is_omap1_8250(pt);
  295. }
  296. #else
  297. static inline int is_omap1_8250(struct uart_8250_port *pt)
  298. {
  299. return 0;
  300. }
  301. static inline int is_omap1510_8250(struct uart_8250_port *pt)
  302. {
  303. return 0;
  304. }
  305. #endif
  306. #ifdef CONFIG_SERIAL_8250_DMA
  307. extern int serial8250_tx_dma(struct uart_8250_port *);
  308. extern void serial8250_tx_dma_flush(struct uart_8250_port *);
  309. extern int serial8250_rx_dma(struct uart_8250_port *);
  310. extern void serial8250_rx_dma_flush(struct uart_8250_port *);
  311. extern int serial8250_request_dma(struct uart_8250_port *);
  312. extern void serial8250_release_dma(struct uart_8250_port *);
  313. static inline void serial8250_do_prepare_tx_dma(struct uart_8250_port *p)
  314. {
  315. struct uart_8250_dma *dma = p->dma;
  316. if (dma->prepare_tx_dma)
  317. dma->prepare_tx_dma(p);
  318. }
  319. static inline void serial8250_do_prepare_rx_dma(struct uart_8250_port *p)
  320. {
  321. struct uart_8250_dma *dma = p->dma;
  322. if (dma->prepare_rx_dma)
  323. dma->prepare_rx_dma(p);
  324. }
  325. static inline bool serial8250_tx_dma_running(struct uart_8250_port *p)
  326. {
  327. struct uart_8250_dma *dma = p->dma;
  328. return dma && dma->tx_running;
  329. }
  330. static inline void serial8250_tx_dma_pause(struct uart_8250_port *p)
  331. {
  332. struct uart_8250_dma *dma = p->dma;
  333. if (!dma->tx_running)
  334. return;
  335. dmaengine_pause(dma->txchan);
  336. }
  337. static inline void serial8250_tx_dma_resume(struct uart_8250_port *p)
  338. {
  339. struct uart_8250_dma *dma = p->dma;
  340. if (!dma->tx_running)
  341. return;
  342. dmaengine_resume(dma->txchan);
  343. }
  344. #else
  345. static inline int serial8250_tx_dma(struct uart_8250_port *p)
  346. {
  347. return -1;
  348. }
  349. static inline void serial8250_tx_dma_flush(struct uart_8250_port *p) { }
  350. static inline int serial8250_rx_dma(struct uart_8250_port *p)
  351. {
  352. return -1;
  353. }
  354. static inline void serial8250_rx_dma_flush(struct uart_8250_port *p) { }
  355. static inline int serial8250_request_dma(struct uart_8250_port *p)
  356. {
  357. return -1;
  358. }
  359. static inline void serial8250_release_dma(struct uart_8250_port *p) { }
  360. static inline bool serial8250_tx_dma_running(struct uart_8250_port *p)
  361. {
  362. return false;
  363. }
  364. static inline void serial8250_tx_dma_pause(struct uart_8250_port *p) { }
  365. static inline void serial8250_tx_dma_resume(struct uart_8250_port *p) { }
  366. #endif
  367. static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
  368. {
  369. unsigned char status;
  370. status = serial_in(up, 0x04); /* EXCR2 */
  371. #define PRESL(x) ((x) & 0x30)
  372. if (PRESL(status) == 0x10) {
  373. /* already in high speed mode */
  374. return 0;
  375. } else {
  376. status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
  377. status |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
  378. serial_out(up, 0x04, status);
  379. }
  380. return 1;
  381. }
  382. static inline int serial_index(struct uart_port *port)
  383. {
  384. return port->minor - 64;
  385. }