8250.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-11-16 GuEe-GUI first version
  9. */
  10. #ifndef __SERIAL_8250_H__
  11. #define __SERIAL_8250_H__
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. #include <rtdevice.h>
  15. #include <drivers/serial_dm.h>
  16. #include <ioremap.h>
  17. #include "regs.h"
  18. enum
  19. {
  20. PORT_UNKNOWN,
  21. PORT_IO,
  22. PORT_MMIO,
  23. PORT_MMIO16,
  24. PORT_MMIO32,
  25. PORT_MMIO32BE,
  26. };
  27. struct serial8250
  28. {
  29. struct rt_serial_device parent;
  30. struct rt_clk *clk;
  31. int irq;
  32. void *base;
  33. rt_size_t size;
  34. rt_uint32_t freq; /* frequency */
  35. rt_uint32_t regshift; /* reg offset shift */
  36. rt_uint8_t iotype; /* io access style */
  37. struct rt_spinlock spinlock;
  38. rt_err_t (*serial_ios)(struct serial8250 *, struct serial_configure *ios);
  39. rt_uint32_t (*serial_in)(struct serial8250 *, int offset);
  40. void (*serial_out)(struct serial8250 *, int offset, int value);
  41. rt_err_t (*serial_dma_enable)(struct serial8250 *, rt_bool_t enabled);
  42. rt_ssize_t (*serial_dma_tx)(struct serial8250 *, const rt_uint8_t *buf, rt_size_t size);
  43. rt_ssize_t (*serial_dma_rx)(struct serial8250 *, rt_uint8_t *buf, rt_size_t size);
  44. rt_err_t (*handle_irq)(struct serial8250 *, int irq);
  45. /* Free all resource (and parent) by child */
  46. void (*remove)(struct serial8250 *);
  47. void *data;
  48. };
  49. #define serial8250_alloc(obj) rt_calloc(1, sizeof(typeof(*obj)))
  50. #define raw_to_serial8250(raw_serial) rt_container_of(raw_serial, struct serial8250, parent)
  51. rt_err_t serial8250_config(struct serial8250 *serial, const char *options);
  52. rt_err_t serial8250_setup(struct serial8250 *serial);
  53. rt_err_t serial8250_remove(struct serial8250 *serial);
  54. rt_uint32_t serial8250_in(struct serial8250 *serial, int offset);
  55. void serial8250_out(struct serial8250 *serial, int offset, int value);
  56. void serial8250_dma_tx_done(struct serial8250 *serial);
  57. void serial8250_dma_rx_done(struct serial8250 *serial, int recv_len);
  58. rt_err_t serial8250_ios(struct serial8250 *serial, struct serial_configure *cfg);
  59. rt_err_t serial8250_uart_configure(struct rt_serial_device *raw_serial, struct serial_configure *cfg);
  60. rt_err_t serial8250_uart_control(struct rt_serial_device *raw_serial, int cmd, void *arg);
  61. int serial8250_uart_putc(struct rt_serial_device *raw_serial, char c);
  62. int serial8250_uart_getc(struct rt_serial_device *raw_serial);
  63. rt_ssize_t serial8250_uart_dma_transmit(struct rt_serial_device *raw_serial,
  64. rt_uint8_t *buf, rt_size_t size, int direction);
  65. int serial8250_early_putc(struct rt_serial_device *raw_serial, char c);
  66. rt_err_t serial8250_early_fdt_setup(struct serial8250 *serial, struct rt_fdt_earlycon *con, const char *options);
  67. extern struct serial8250 early_serial8250;
  68. extern const struct rt_uart_ops serial8250_uart_ops;
  69. #endif /* __SERIAL_8250_H__ */