8250_keba.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2025 KEBA Industrial Automation GmbH
  4. *
  5. * Driver for KEBA UART FPGA IP core
  6. */
  7. #include <linux/auxiliary_bus.h>
  8. #include <linux/bits.h>
  9. #include <linux/container_of.h>
  10. #include <linux/dev_printk.h>
  11. #include <linux/device/devres.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/misc/keba.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/module.h>
  17. #include <linux/serial_core.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/types.h>
  20. #include "8250.h"
  21. #define KUART "kuart"
  22. /* flags */
  23. #define KUART_RS485 BIT(0)
  24. #define KUART_USE_CAPABILITY BIT(1)
  25. /* registers */
  26. #define KUART_VERSION 0x0000
  27. #define KUART_REVISION 0x0001
  28. #define KUART_CAPABILITY 0x0002
  29. #define KUART_CONTROL 0x0004
  30. #define KUART_BASE 0x000C
  31. #define KUART_REGSHIFT 2
  32. #define KUART_CLK 1843200
  33. /* mode flags */
  34. enum kuart_mode {
  35. KUART_MODE_NONE = 0,
  36. KUART_MODE_RS485,
  37. KUART_MODE_RS422,
  38. KUART_MODE_RS232
  39. };
  40. /* capability flags */
  41. #define KUART_CAPABILITY_NONE BIT(KUART_MODE_NONE)
  42. #define KUART_CAPABILITY_RS485 BIT(KUART_MODE_RS485)
  43. #define KUART_CAPABILITY_RS422 BIT(KUART_MODE_RS422)
  44. #define KUART_CAPABILITY_RS232 BIT(KUART_MODE_RS232)
  45. #define KUART_CAPABILITY_MASK GENMASK(3, 0)
  46. /* registers for Indexed Control Register access in enhanced mode */
  47. #define KUART_EMODE_ICR_OFFSET UART_SCR
  48. #define KUART_EMODE_ICR_VALUE UART_LSR
  49. /* Additional Control Register DTR line configuration */
  50. #define UART_ACR_DTRLC_MASK 0x18
  51. #define UART_ACR_DTRLC_COMPAT 0x00
  52. #define UART_ACR_DTRLC_ENABLE_LOW 0x10
  53. struct kuart {
  54. struct keba_uart_auxdev *auxdev;
  55. void __iomem *base;
  56. unsigned int line;
  57. unsigned int flags;
  58. u8 capability;
  59. enum kuart_mode mode;
  60. };
  61. static void kuart_set_phy_mode(struct kuart *kuart, enum kuart_mode mode)
  62. {
  63. iowrite8(mode, kuart->base + KUART_CONTROL);
  64. }
  65. static void kuart_enhanced_mode(struct uart_8250_port *up, bool enable)
  66. {
  67. u8 lcr, efr;
  68. /* backup LCR register */
  69. lcr = serial_in(up, UART_LCR);
  70. /* enable 650 compatible register set (EFR, ...) */
  71. serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
  72. /* enable/disable enhanced mode with indexed control registers */
  73. efr = serial_in(up, UART_EFR);
  74. if (enable)
  75. efr |= UART_EFR_ECB;
  76. else
  77. efr &= ~UART_EFR_ECB;
  78. serial_out(up, UART_EFR, efr);
  79. /* disable 650 compatible register set, restore LCR */
  80. serial_out(up, UART_LCR, lcr);
  81. }
  82. static void kuart_dtr_line_config(struct uart_8250_port *up, u8 dtrlc)
  83. {
  84. u8 acr;
  85. /* set index register to 0 to access ACR register */
  86. serial_out(up, KUART_EMODE_ICR_OFFSET, UART_ACR);
  87. /* set value register to 0x10 writing DTR mode (1,0) */
  88. acr = serial_in(up, KUART_EMODE_ICR_VALUE);
  89. acr &= ~UART_ACR_DTRLC_MASK;
  90. acr |= dtrlc;
  91. serial_out(up, KUART_EMODE_ICR_VALUE, acr);
  92. }
  93. static int kuart_rs485_config(struct uart_port *port, struct ktermios *termios,
  94. struct serial_rs485 *rs485)
  95. {
  96. struct uart_8250_port *up = up_to_u8250p(port);
  97. struct kuart *kuart = port->private_data;
  98. enum kuart_mode mode;
  99. u8 dtrlc;
  100. if (rs485->flags & SER_RS485_ENABLED) {
  101. if (rs485->flags & SER_RS485_MODE_RS422)
  102. mode = KUART_MODE_RS422;
  103. else
  104. mode = KUART_MODE_RS485;
  105. } else {
  106. mode = KUART_MODE_RS232;
  107. }
  108. if (mode == kuart->mode)
  109. return 0;
  110. if (kuart->flags & KUART_USE_CAPABILITY) {
  111. /* deactivate physical interface, break before make */
  112. kuart_set_phy_mode(kuart, KUART_MODE_NONE);
  113. }
  114. if (mode == KUART_MODE_RS485) {
  115. /*
  116. * Set DTR line configuration of 95x UART to DTR mode (1,0).
  117. * In this mode the DTR pin drives the active-low enable pin of
  118. * an external RS485 buffer. The DTR pin will be forced low
  119. * whenever the transmitter is not empty, otherwise DTR pin is
  120. * high.
  121. */
  122. dtrlc = UART_ACR_DTRLC_ENABLE_LOW;
  123. } else {
  124. /*
  125. * Set DTR line configuration of 95x UART to DTR mode (0,0).
  126. * In this mode the DTR pin is compatible with 16C450, 16C550,
  127. * 16C650 and 16c670 (i.e. normal).
  128. */
  129. dtrlc = UART_ACR_DTRLC_COMPAT;
  130. }
  131. kuart_enhanced_mode(up, true);
  132. kuart_dtr_line_config(up, dtrlc);
  133. kuart_enhanced_mode(up, false);
  134. if (kuart->flags & KUART_USE_CAPABILITY) {
  135. /* activate selected physical interface */
  136. kuart_set_phy_mode(kuart, mode);
  137. }
  138. kuart->mode = mode;
  139. return 0;
  140. }
  141. static int kuart_probe(struct auxiliary_device *auxdev,
  142. const struct auxiliary_device_id *id)
  143. {
  144. struct device *dev = &auxdev->dev;
  145. struct uart_8250_port uart = {};
  146. struct resource res;
  147. struct kuart *kuart;
  148. int retval;
  149. kuart = devm_kzalloc(dev, sizeof(*kuart), GFP_KERNEL);
  150. if (!kuart)
  151. return -ENOMEM;
  152. kuart->auxdev = container_of(auxdev, struct keba_uart_auxdev, auxdev);
  153. kuart->flags = id->driver_data;
  154. auxiliary_set_drvdata(auxdev, kuart);
  155. /*
  156. * map only memory in front of UART registers, UART registers will be
  157. * mapped by serial port
  158. */
  159. res = kuart->auxdev->io;
  160. res.end = res.start + KUART_BASE - 1;
  161. kuart->base = devm_ioremap_resource(dev, &res);
  162. if (IS_ERR(kuart->base))
  163. return PTR_ERR(kuart->base);
  164. if (kuart->flags & KUART_USE_CAPABILITY) {
  165. /*
  166. * supported modes are read from capability register, at least
  167. * one mode other than none must be supported
  168. */
  169. kuart->capability = ioread8(kuart->base + KUART_CAPABILITY) &
  170. KUART_CAPABILITY_MASK;
  171. if ((kuart->capability & ~KUART_CAPABILITY_NONE) == 0)
  172. return -EIO;
  173. }
  174. spin_lock_init(&uart.port.lock);
  175. uart.port.dev = dev;
  176. uart.port.mapbase = kuart->auxdev->io.start + KUART_BASE;
  177. uart.port.irq = kuart->auxdev->irq;
  178. uart.port.uartclk = KUART_CLK;
  179. uart.port.private_data = kuart;
  180. /* 8 bit registers are 32 bit aligned => shift register offset */
  181. uart.port.iotype = UPIO_MEM32;
  182. uart.port.regshift = KUART_REGSHIFT;
  183. /*
  184. * UART mixes 16550, 16750 and 16C950 (for RS485) standard => auto
  185. * configuration works best
  186. */
  187. uart.port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF | UPF_IOREMAP;
  188. /*
  189. * UART supports RS485, RS422 and RS232 with switching of physical
  190. * interface
  191. */
  192. uart.port.rs485_config = kuart_rs485_config;
  193. if (kuart->flags & KUART_RS485) {
  194. uart.port.rs485_supported.flags = SER_RS485_ENABLED |
  195. SER_RS485_RTS_ON_SEND;
  196. uart.port.rs485.flags = SER_RS485_ENABLED |
  197. SER_RS485_RTS_ON_SEND;
  198. }
  199. if (kuart->flags & KUART_USE_CAPABILITY) {
  200. /* default mode priority is RS485 > RS422 > RS232 */
  201. if (kuart->capability & KUART_CAPABILITY_RS422) {
  202. uart.port.rs485_supported.flags |= SER_RS485_ENABLED |
  203. SER_RS485_RTS_ON_SEND |
  204. SER_RS485_MODE_RS422;
  205. uart.port.rs485.flags = SER_RS485_ENABLED |
  206. SER_RS485_RTS_ON_SEND |
  207. SER_RS485_MODE_RS422;
  208. }
  209. if (kuart->capability & KUART_CAPABILITY_RS485) {
  210. uart.port.rs485_supported.flags |= SER_RS485_ENABLED |
  211. SER_RS485_RTS_ON_SEND;
  212. uart.port.rs485.flags = SER_RS485_ENABLED |
  213. SER_RS485_RTS_ON_SEND;
  214. }
  215. }
  216. retval = serial8250_register_8250_port(&uart);
  217. if (retval < 0)
  218. return dev_err_probe(&auxdev->dev, retval,
  219. "UART registration failed!\n");
  220. kuart->line = retval;
  221. return 0;
  222. }
  223. static void kuart_remove(struct auxiliary_device *auxdev)
  224. {
  225. struct kuart *kuart = auxiliary_get_drvdata(auxdev);
  226. if (kuart->flags & KUART_USE_CAPABILITY)
  227. kuart_set_phy_mode(kuart, KUART_MODE_NONE);
  228. serial8250_unregister_port(kuart->line);
  229. }
  230. static const struct auxiliary_device_id kuart_devtype_aux[] = {
  231. { .name = "keba.rs485-uart", .driver_data = KUART_RS485 },
  232. { .name = "keba.rs232-uart", .driver_data = 0 },
  233. { .name = "keba.uart", .driver_data = KUART_USE_CAPABILITY },
  234. {}
  235. };
  236. MODULE_DEVICE_TABLE(auxiliary, kuart_devtype_aux);
  237. static struct auxiliary_driver kuart_driver_aux = {
  238. .name = KUART,
  239. .id_table = kuart_devtype_aux,
  240. .probe = kuart_probe,
  241. .remove = kuart_remove,
  242. };
  243. module_auxiliary_driver(kuart_driver_aux);
  244. MODULE_AUTHOR("Gerhard Engleder <eg@keba.com>");
  245. MODULE_DESCRIPTION("KEBA 8250 serial port driver");
  246. MODULE_LICENSE("GPL");