8250_men_mcb.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/device.h>
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/io.h>
  6. #include <linux/mcb.h>
  7. #include <linux/serial.h>
  8. #include <linux/serial_core.h>
  9. #include <linux/serial_8250.h>
  10. #include <uapi/linux/serial_core.h>
  11. #define MEN_UART_ID_Z025 0x19
  12. #define MEN_UART_ID_Z057 0x39
  13. #define MEN_UART_ID_Z125 0x7d
  14. #define MEN_UART_MEM_SIZE 0x10
  15. struct serial_8250_men_mcb_data {
  16. struct uart_8250_port uart;
  17. int line;
  18. };
  19. /*
  20. * The Z125 16550-compatible UART has no fixed base clock assigned
  21. * So, depending on the board we're on, we need to adjust the
  22. * parameter in order to really set the correct baudrate, and
  23. * do so if possible without user interaction
  24. */
  25. static u32 men_lookup_uartclk(struct mcb_device *mdev)
  26. {
  27. /* use default value if board is not available below */
  28. u32 clkval = 1041666;
  29. dev_info(&mdev->dev, "%s on board %s\n",
  30. dev_name(&mdev->dev),
  31. mdev->bus->name);
  32. if (strncmp(mdev->bus->name, "F075", 4) == 0)
  33. clkval = 1041666;
  34. else if (strncmp(mdev->bus->name, "F216", 4) == 0)
  35. clkval = 1843200;
  36. else if (strncmp(mdev->bus->name, "G215", 4) == 0)
  37. clkval = 1843200;
  38. else if (strncmp(mdev->bus->name, "F210", 4) == 0)
  39. clkval = 115200;
  40. else
  41. dev_info(&mdev->dev,
  42. "board not detected, using default uartclk\n");
  43. clkval = clkval << 4;
  44. return clkval;
  45. }
  46. static unsigned int get_num_ports(struct mcb_device *mdev,
  47. void __iomem *membase)
  48. {
  49. switch (mdev->id) {
  50. case MEN_UART_ID_Z125:
  51. return 1U;
  52. case MEN_UART_ID_Z025:
  53. return readb(membase) >> 4;
  54. case MEN_UART_ID_Z057:
  55. return 4U;
  56. default:
  57. dev_err(&mdev->dev, "no supported device!\n");
  58. return -ENODEV;
  59. }
  60. }
  61. static int serial_8250_men_mcb_probe(struct mcb_device *mdev,
  62. const struct mcb_device_id *id)
  63. {
  64. struct serial_8250_men_mcb_data *data;
  65. struct resource *mem;
  66. int num_ports;
  67. int i;
  68. void __iomem *membase;
  69. mem = mcb_get_resource(mdev, IORESOURCE_MEM);
  70. if (mem == NULL)
  71. return -ENXIO;
  72. membase = devm_ioremap_resource(&mdev->dev, mem);
  73. if (IS_ERR(membase))
  74. return PTR_ERR_OR_ZERO(membase);
  75. num_ports = get_num_ports(mdev, membase);
  76. dev_dbg(&mdev->dev, "found a 16z%03u with %u ports\n",
  77. mdev->id, num_ports);
  78. if (num_ports <= 0 || num_ports > 4) {
  79. dev_err(&mdev->dev, "unexpected number of ports: %u\n",
  80. num_ports);
  81. return -ENODEV;
  82. }
  83. data = devm_kcalloc(&mdev->dev, num_ports,
  84. sizeof(struct serial_8250_men_mcb_data),
  85. GFP_KERNEL);
  86. if (!data)
  87. return -ENOMEM;
  88. mcb_set_drvdata(mdev, data);
  89. for (i = 0; i < num_ports; i++) {
  90. data[i].uart.port.dev = mdev->dma_dev;
  91. spin_lock_init(&data[i].uart.port.lock);
  92. data[i].uart.port.type = PORT_16550;
  93. data[i].uart.port.flags = UPF_SKIP_TEST | UPF_SHARE_IRQ
  94. | UPF_FIXED_TYPE;
  95. data[i].uart.port.iotype = UPIO_MEM;
  96. data[i].uart.port.uartclk = men_lookup_uartclk(mdev);
  97. data[i].uart.port.regshift = 0;
  98. data[i].uart.port.irq = mcb_get_irq(mdev);
  99. data[i].uart.port.membase = membase;
  100. data[i].uart.port.fifosize = 60;
  101. data[i].uart.port.mapbase = (unsigned long) mem->start
  102. + i * MEN_UART_MEM_SIZE;
  103. data[i].uart.port.iobase = data[i].uart.port.mapbase;
  104. /* ok, register the port */
  105. data[i].line = serial8250_register_8250_port(&data[i].uart);
  106. if (data[i].line < 0) {
  107. dev_err(&mdev->dev, "unable to register UART port\n");
  108. return data[i].line;
  109. }
  110. dev_info(&mdev->dev, "found MCB UART: ttyS%d\n", data[i].line);
  111. }
  112. return 0;
  113. }
  114. static void serial_8250_men_mcb_remove(struct mcb_device *mdev)
  115. {
  116. int num_ports, i;
  117. struct serial_8250_men_mcb_data *data = mcb_get_drvdata(mdev);
  118. if (!data)
  119. return;
  120. num_ports = get_num_ports(mdev, data[0].uart.port.membase);
  121. if (num_ports < 0 || num_ports > 4) {
  122. dev_err(&mdev->dev, "error retrieving number of ports!\n");
  123. return;
  124. }
  125. for (i = 0; i < num_ports; i++)
  126. serial8250_unregister_port(data[i].line);
  127. }
  128. static const struct mcb_device_id serial_8250_men_mcb_ids[] = {
  129. { .device = MEN_UART_ID_Z025 },
  130. { .device = MEN_UART_ID_Z057 },
  131. { .device = MEN_UART_ID_Z125 },
  132. { }
  133. };
  134. MODULE_DEVICE_TABLE(mcb, serial_8250_men_mcb_ids);
  135. static struct mcb_driver mcb_driver = {
  136. .driver = {
  137. .name = "8250_men_mcb",
  138. .owner = THIS_MODULE,
  139. },
  140. .probe = serial_8250_men_mcb_probe,
  141. .remove = serial_8250_men_mcb_remove,
  142. .id_table = serial_8250_men_mcb_ids,
  143. };
  144. module_mcb_driver(mcb_driver);
  145. MODULE_LICENSE("GPL v2");
  146. MODULE_DESCRIPTION("MEN 8250 UART driver");
  147. MODULE_AUTHOR("Michael Moese <michael.moese@men.de");
  148. MODULE_ALIAS("mcb:16z125");
  149. MODULE_ALIAS("mcb:16z025");
  150. MODULE_ALIAS("mcb:16z057");