8250_fintek.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Probe for F81216A LPC to 4 UART
  3. *
  4. * Copyright (C) 2014-2016 Ricardo Ribalda, Qtechnology A/S
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/pci.h>
  13. #include <linux/pnp.h>
  14. #include <linux/kernel.h>
  15. #include <linux/serial_core.h>
  16. #include <linux/irq.h>
  17. #include "8250.h"
  18. #define ADDR_PORT 0
  19. #define DATA_PORT 1
  20. #define EXIT_KEY 0xAA
  21. #define CHIP_ID1 0x20
  22. #define CHIP_ID2 0x21
  23. #define CHIP_ID_0 0x1602
  24. #define CHIP_ID_1 0x0501
  25. #define VENDOR_ID1 0x23
  26. #define VENDOR_ID1_VAL 0x19
  27. #define VENDOR_ID2 0x24
  28. #define VENDOR_ID2_VAL 0x34
  29. #define IO_ADDR1 0x61
  30. #define IO_ADDR2 0x60
  31. #define LDN 0x7
  32. #define FINTEK_IRQ_MODE 0x70
  33. #define IRQ_SHARE BIT(4)
  34. #define IRQ_MODE_MASK (BIT(6) | BIT(5))
  35. #define IRQ_LEVEL_LOW 0
  36. #define IRQ_EDGE_HIGH BIT(5)
  37. #define RS485 0xF0
  38. #define RTS_INVERT BIT(5)
  39. #define RS485_URA BIT(4)
  40. #define RXW4C_IRA BIT(3)
  41. #define TXW4C_IRA BIT(2)
  42. struct fintek_8250 {
  43. u16 base_port;
  44. u8 index;
  45. u8 key;
  46. };
  47. static int fintek_8250_enter_key(u16 base_port, u8 key)
  48. {
  49. if (!request_muxed_region(base_port, 2, "8250_fintek"))
  50. return -EBUSY;
  51. /* Force to deactive all SuperIO in this base_port */
  52. outb(EXIT_KEY, base_port + ADDR_PORT);
  53. outb(key, base_port + ADDR_PORT);
  54. outb(key, base_port + ADDR_PORT);
  55. return 0;
  56. }
  57. static void fintek_8250_exit_key(u16 base_port)
  58. {
  59. outb(EXIT_KEY, base_port + ADDR_PORT);
  60. release_region(base_port + ADDR_PORT, 2);
  61. }
  62. static int fintek_8250_check_id(u16 base_port)
  63. {
  64. u16 chip;
  65. outb(VENDOR_ID1, base_port + ADDR_PORT);
  66. if (inb(base_port + DATA_PORT) != VENDOR_ID1_VAL)
  67. return -ENODEV;
  68. outb(VENDOR_ID2, base_port + ADDR_PORT);
  69. if (inb(base_port + DATA_PORT) != VENDOR_ID2_VAL)
  70. return -ENODEV;
  71. outb(CHIP_ID1, base_port + ADDR_PORT);
  72. chip = inb(base_port + DATA_PORT);
  73. outb(CHIP_ID2, base_port + ADDR_PORT);
  74. chip |= inb(base_port + DATA_PORT) << 8;
  75. if (chip != CHIP_ID_0 && chip != CHIP_ID_1)
  76. return -ENODEV;
  77. return 0;
  78. }
  79. static int fintek_8250_rs485_config(struct uart_port *port,
  80. struct serial_rs485 *rs485)
  81. {
  82. uint8_t config = 0;
  83. struct fintek_8250 *pdata = port->private_data;
  84. if (!pdata)
  85. return -EINVAL;
  86. if (rs485->flags & SER_RS485_ENABLED)
  87. memset(rs485->padding, 0, sizeof(rs485->padding));
  88. else
  89. memset(rs485, 0, sizeof(*rs485));
  90. rs485->flags &= SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND |
  91. SER_RS485_RTS_AFTER_SEND;
  92. if (rs485->delay_rts_before_send) {
  93. rs485->delay_rts_before_send = 1;
  94. config |= TXW4C_IRA;
  95. }
  96. if (rs485->delay_rts_after_send) {
  97. rs485->delay_rts_after_send = 1;
  98. config |= RXW4C_IRA;
  99. }
  100. if ((!!(rs485->flags & SER_RS485_RTS_ON_SEND)) ==
  101. (!!(rs485->flags & SER_RS485_RTS_AFTER_SEND)))
  102. rs485->flags &= ~SER_RS485_ENABLED;
  103. else
  104. config |= RS485_URA;
  105. if (rs485->flags & SER_RS485_RTS_ON_SEND)
  106. config |= RTS_INVERT;
  107. if (fintek_8250_enter_key(pdata->base_port, pdata->key))
  108. return -EBUSY;
  109. outb(LDN, pdata->base_port + ADDR_PORT);
  110. outb(pdata->index, pdata->base_port + DATA_PORT);
  111. outb(RS485, pdata->base_port + ADDR_PORT);
  112. outb(config, pdata->base_port + DATA_PORT);
  113. fintek_8250_exit_key(pdata->base_port);
  114. port->rs485 = *rs485;
  115. return 0;
  116. }
  117. static int find_base_port(struct fintek_8250 *pdata, u16 io_address)
  118. {
  119. static const u16 addr[] = {0x4e, 0x2e};
  120. static const u8 keys[] = {0x77, 0xa0, 0x87, 0x67};
  121. int i, j, k;
  122. for (i = 0; i < ARRAY_SIZE(addr); i++) {
  123. for (j = 0; j < ARRAY_SIZE(keys); j++) {
  124. if (fintek_8250_enter_key(addr[i], keys[j]))
  125. continue;
  126. if (fintek_8250_check_id(addr[i])) {
  127. fintek_8250_exit_key(addr[i]);
  128. continue;
  129. }
  130. for (k = 0; k < 4; k++) {
  131. u16 aux;
  132. outb(LDN, addr[i] + ADDR_PORT);
  133. outb(k, addr[i] + DATA_PORT);
  134. outb(IO_ADDR1, addr[i] + ADDR_PORT);
  135. aux = inb(addr[i] + DATA_PORT);
  136. outb(IO_ADDR2, addr[i] + ADDR_PORT);
  137. aux |= inb(addr[i] + DATA_PORT) << 8;
  138. if (aux != io_address)
  139. continue;
  140. fintek_8250_exit_key(addr[i]);
  141. pdata->key = keys[j];
  142. pdata->base_port = addr[i];
  143. pdata->index = k;
  144. return 0;
  145. }
  146. fintek_8250_exit_key(addr[i]);
  147. }
  148. }
  149. return -ENODEV;
  150. }
  151. static int fintek_8250_set_irq_mode(struct fintek_8250 *pdata, bool level_mode)
  152. {
  153. int status;
  154. u8 tmp;
  155. status = fintek_8250_enter_key(pdata->base_port, pdata->key);
  156. if (status)
  157. return status;
  158. outb(LDN, pdata->base_port + ADDR_PORT);
  159. outb(pdata->index, pdata->base_port + DATA_PORT);
  160. outb(FINTEK_IRQ_MODE, pdata->base_port + ADDR_PORT);
  161. tmp = inb(pdata->base_port + DATA_PORT);
  162. tmp &= ~IRQ_MODE_MASK;
  163. tmp |= IRQ_SHARE;
  164. if (!level_mode)
  165. tmp |= IRQ_EDGE_HIGH;
  166. outb(tmp, pdata->base_port + DATA_PORT);
  167. fintek_8250_exit_key(pdata->base_port);
  168. return 0;
  169. }
  170. int fintek_8250_probe(struct uart_8250_port *uart)
  171. {
  172. struct fintek_8250 *pdata;
  173. struct fintek_8250 probe_data;
  174. struct irq_data *irq_data = irq_get_irq_data(uart->port.irq);
  175. bool level_mode = irqd_is_level_type(irq_data);
  176. if (find_base_port(&probe_data, uart->port.iobase))
  177. return -ENODEV;
  178. pdata = devm_kzalloc(uart->port.dev, sizeof(*pdata), GFP_KERNEL);
  179. if (!pdata)
  180. return -ENOMEM;
  181. memcpy(pdata, &probe_data, sizeof(probe_data));
  182. uart->port.rs485_config = fintek_8250_rs485_config;
  183. uart->port.private_data = pdata;
  184. return fintek_8250_set_irq_mode(pdata, level_mode);
  185. }