105-uart-add-en7523-support.patch 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. --- /dev/null
  2. +++ b/drivers/tty/serial/8250/8250_en7523.c
  3. @@ -0,0 +1,94 @@
  4. +// SPDX-License-Identifier: GPL-2.0+
  5. +/*
  6. + * Airoha EN7523 driver.
  7. + *
  8. + * Copyright (c) 2022 Genexis Sweden AB
  9. + * Author: Benjamin Larsson <[email protected]>
  10. + */
  11. +#include <linux/clk.h>
  12. +#include <linux/io.h>
  13. +#include <linux/module.h>
  14. +#include <linux/of_irq.h>
  15. +#include <linux/of_platform.h>
  16. +#include <linux/pinctrl/consumer.h>
  17. +#include <linux/platform_device.h>
  18. +#include <linux/pm_runtime.h>
  19. +#include <linux/serial_8250.h>
  20. +#include <linux/serial_reg.h>
  21. +#include <linux/console.h>
  22. +#include <linux/dma-mapping.h>
  23. +#include <linux/tty.h>
  24. +#include <linux/tty_flip.h>
  25. +
  26. +#include "8250.h"
  27. +
  28. +
  29. +/* The Airoha UART is 16550-compatible except for the baud rate calculation.
  30. + *
  31. + * crystal_clock = 20 MHz
  32. + * xindiv_clock = crystal_clock / clock_div
  33. + * (x/y) = XYD, 32 bit register with 16 bits of x and and then 16 bits of y
  34. + * clock_div = XINCLK_DIVCNT (default set to 10 (0x4)),
  35. + * - 3 bit register [ 1, 2, 4, 8, 10, 12, 16, 20 ]
  36. + *
  37. + * baud_rate = ((xindiv_clock) * (x/y)) / ([BRDH,BRDL] * 16)
  38. + *
  39. + * XYD_y seems to need to be larger then XYD_x for things to work.
  40. + * Setting [BRDH,BRDL] to [0,1] and XYD_y to 65000 give even values
  41. + * for usual baud rates.
  42. + *
  43. + * Selecting divider needs to fulfill
  44. + * 1.8432 MHz <= xindiv_clk <= APB clock / 2
  45. + * The clocks are unknown but a divider of value 1 did not work.
  46. + *
  47. + * Optimally the XYD, BRD and XINCLK_DIVCNT registers could be searched to
  48. + * find values that gives the least error for every baud rate. But searching
  49. + * the space takes time and in practise only a few rates are of interest.
  50. + * With some value combinations not working a tested subset is used giving
  51. + * a usable range from 110 to 460800 baud.
  52. + */
  53. +
  54. +#define CLOCK_DIV_TAB_ELEMS 3
  55. +#define XYD_Y 65000
  56. +#define XINDIV_CLOCK 20000000
  57. +#define UART_BRDL_20M 0x01
  58. +#define UART_BRDH_20M 0x00
  59. +
  60. +static int clock_div_tab[] = { 10, 4, 2};
  61. +static int clock_div_reg[] = { 4, 2, 1};
  62. +
  63. +
  64. +int en7523_set_uart_baud_rate (struct uart_port *port, unsigned int baud)
  65. +{
  66. + struct uart_8250_port *up = up_to_u8250p(port);
  67. + unsigned int xyd_x, nom, denom;
  68. + int i;
  69. +
  70. + /* set DLAB to access the baud rate divider registers (BRDH, BRDL) */
  71. + serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
  72. +
  73. + /* set baud rate calculation defaults */
  74. +
  75. + /* set BRDIV ([BRDH,BRDL]) to 1 */
  76. + serial_port_out(port, UART_BRDL, UART_BRDL_20M);
  77. + serial_port_out(port, UART_BRDH, UART_BRDH_20M);
  78. +
  79. + /* calculate XYD_x and XINCLKDR register */
  80. +
  81. + for (i = 0 ; i < CLOCK_DIV_TAB_ELEMS ; i++) {
  82. + denom = (XINDIV_CLOCK/40) / clock_div_tab[i];
  83. + nom = (baud * (XYD_Y/40));
  84. + xyd_x = ((nom/denom) << 4);
  85. + if (xyd_x < XYD_Y) break;
  86. + }
  87. +
  88. + serial_port_out(port, UART_XINCLKDR, clock_div_reg[i]);
  89. + serial_port_out(port, UART_XYD, (xyd_x<<16) | XYD_Y);
  90. +
  91. + /* unset DLAB */
  92. + serial_port_out(port, UART_LCR, up->lcr);
  93. +
  94. + return 0;
  95. +}
  96. +
  97. +EXPORT_SYMBOL_GPL(en7523_set_uart_baud_rate);
  98. --- a/drivers/tty/serial/8250/8250_of.c
  99. +++ b/drivers/tty/serial/8250/8250_of.c
  100. @@ -338,6 +338,7 @@ static const struct of_device_id of_plat
  101. { .compatible = "ti,da830-uart", .data = (void *)PORT_DA830, },
  102. { .compatible = "nuvoton,wpcm450-uart", .data = (void *)PORT_NPCM, },
  103. { .compatible = "nuvoton,npcm750-uart", .data = (void *)PORT_NPCM, },
  104. + { .compatible = "airoha,en7523-uart", .data = (void *)PORT_AIROHA, },
  105. { /* end of list */ },
  106. };
  107. MODULE_DEVICE_TABLE(of, of_platform_serial_table);
  108. --- a/drivers/tty/serial/8250/8250_port.c
  109. +++ b/drivers/tty/serial/8250/8250_port.c
  110. @@ -330,6 +330,14 @@ static const struct serial8250_config ua
  111. .rxtrig_bytes = {1, 8, 16, 30},
  112. .flags = UART_CAP_FIFO | UART_CAP_AFE,
  113. },
  114. + [PORT_AIROHA] = {
  115. + .name = "Airoha 16550",
  116. + .fifo_size = 8,
  117. + .tx_loadsz = 1,
  118. + .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01,
  119. + .rxtrig_bytes = {1, 4},
  120. + .flags = UART_CAP_FIFO,
  121. + },
  122. };
  123. /* Uart divisor latch read */
  124. @@ -2889,6 +2897,12 @@ serial8250_do_set_termios(struct uart_po
  125. serial8250_set_divisor(port, baud, quot, frac);
  126. +#ifdef CONFIG_SERIAL_8250_AIROHA
  127. + /* Airoha SoCs have custom registers for baud rate settings */
  128. + if (port->type == PORT_AIROHA)
  129. + en7523_set_uart_baud_rate(port, baud);
  130. +#endif
  131. +
  132. /*
  133. * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
  134. * is written without DLAB set, this mode will be disabled.
  135. --- a/drivers/tty/serial/8250/Makefile
  136. +++ b/drivers/tty/serial/8250/Makefile
  137. @@ -46,6 +46,7 @@ obj-$(CONFIG_SERIAL_8250_PERICOM) += 825
  138. obj-$(CONFIG_SERIAL_8250_PXA) += 8250_pxa.o
  139. obj-$(CONFIG_SERIAL_8250_TEGRA) += 8250_tegra.o
  140. obj-$(CONFIG_SERIAL_8250_BCM7271) += 8250_bcm7271.o
  141. +obj-$(CONFIG_SERIAL_8250_AIROHA) += 8250_en7523.o
  142. obj-$(CONFIG_SERIAL_OF_PLATFORM) += 8250_of.o
  143. CFLAGS_8250_ingenic.o += -I$(srctree)/scripts/dtc/libfdt
  144. --- a/include/uapi/linux/serial_reg.h
  145. +++ b/include/uapi/linux/serial_reg.h
  146. @@ -382,5 +382,17 @@
  147. #define UART_ALTR_EN_TXFIFO_LW 0x01 /* Enable the TX FIFO Low Watermark */
  148. #define UART_ALTR_TX_LOW 0x41 /* Tx FIFO Low Watermark */
  149. +/*
  150. + * These are definitions for the Airoha EN75XX uart registers
  151. + * Normalized because of 32 bits registers.
  152. + */
  153. +#define UART_BRDL 0
  154. +#define UART_BRDH 1
  155. +#define UART_XINCLKDR 10
  156. +#define UART_XYD 11
  157. +#define UART_TXLVLCNT 12
  158. +#define UART_RXLVLCNT 13
  159. +#define UART_FINTLVL 14
  160. +
  161. #endif /* _LINUX_SERIAL_REG_H */
  162. --- a/include/uapi/linux/serial_core.h
  163. +++ b/include/uapi/linux/serial_core.h
  164. @@ -45,6 +45,7 @@
  165. #define PORT_ALTR_16550_F128 28 /* Altera 16550 UART with 128 FIFOs */
  166. #define PORT_RT2880 29 /* Ralink RT2880 internal UART */
  167. #define PORT_16550A_FSL64 30 /* Freescale 16550 UART with 64 FIFOs */
  168. +#define PORT_AIROHA 31 /* Airoha 16550 UART */
  169. /*
  170. * ARM specific type numbers. These are not currently guaranteed
  171. --- a/include/linux/serial_8250.h
  172. +++ b/include/linux/serial_8250.h
  173. @@ -195,6 +195,7 @@ void serial8250_do_set_mctrl(struct uart
  174. void serial8250_do_set_divisor(struct uart_port *port, unsigned int baud,
  175. unsigned int quot, unsigned int quot_frac);
  176. int fsl8250_handle_irq(struct uart_port *port);
  177. +int en7523_set_uart_baud_rate(struct uart_port *port, unsigned int baud);
  178. int serial8250_handle_irq(struct uart_port *port, unsigned int iir);
  179. u16 serial8250_rx_chars(struct uart_8250_port *up, u16 lsr);
  180. void serial8250_read_char(struct uart_8250_port *up, u16 lsr);