8250_moxa.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * 8250_moxa.c - MOXA Smartio/Industio MUE multiport serial driver.
  3. *
  4. * Author: Mathieu OTHACEHE <[email protected]>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/pci.h>
  12. #include "8250.h"
  13. #define PCI_DEVICE_ID_MOXA_CP102E 0x1024
  14. #define PCI_DEVICE_ID_MOXA_CP102EL 0x1025
  15. #define PCI_DEVICE_ID_MOXA_CP104EL_A 0x1045
  16. #define PCI_DEVICE_ID_MOXA_CP114EL 0x1144
  17. #define PCI_DEVICE_ID_MOXA_CP116E_A_A 0x1160
  18. #define PCI_DEVICE_ID_MOXA_CP116E_A_B 0x1161
  19. #define PCI_DEVICE_ID_MOXA_CP118EL_A 0x1182
  20. #define PCI_DEVICE_ID_MOXA_CP118E_A_I 0x1183
  21. #define PCI_DEVICE_ID_MOXA_CP132EL 0x1322
  22. #define PCI_DEVICE_ID_MOXA_CP134EL_A 0x1342
  23. #define PCI_DEVICE_ID_MOXA_CP138E_A 0x1381
  24. #define PCI_DEVICE_ID_MOXA_CP168EL_A 0x1683
  25. #define MOXA_BASE_BAUD 921600
  26. #define MOXA_UART_OFFSET 0x200
  27. #define MOXA_BASE_BAR 1
  28. struct moxa8250_board {
  29. unsigned int num_ports;
  30. int line[0];
  31. };
  32. enum {
  33. moxa8250_2p = 0,
  34. moxa8250_4p,
  35. moxa8250_8p
  36. };
  37. static struct moxa8250_board moxa8250_boards[] = {
  38. [moxa8250_2p] = { .num_ports = 2},
  39. [moxa8250_4p] = { .num_ports = 4},
  40. [moxa8250_8p] = { .num_ports = 8},
  41. };
  42. static int moxa8250_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  43. {
  44. struct uart_8250_port uart;
  45. struct moxa8250_board *brd;
  46. void __iomem *ioaddr;
  47. resource_size_t baseaddr;
  48. unsigned int i, nr_ports;
  49. unsigned int offset;
  50. int ret;
  51. brd = &moxa8250_boards[id->driver_data];
  52. nr_ports = brd->num_ports;
  53. ret = pcim_enable_device(pdev);
  54. if (ret)
  55. return ret;
  56. brd = devm_kzalloc(&pdev->dev, sizeof(struct moxa8250_board) +
  57. sizeof(unsigned int) * nr_ports, GFP_KERNEL);
  58. if (!brd)
  59. return -ENOMEM;
  60. brd->num_ports = nr_ports;
  61. memset(&uart, 0, sizeof(struct uart_8250_port));
  62. uart.port.dev = &pdev->dev;
  63. uart.port.irq = pdev->irq;
  64. uart.port.uartclk = MOXA_BASE_BAUD * 16;
  65. uart.port.flags = UPF_SKIP_TEST | UPF_BOOT_AUTOCONF | UPF_SHARE_IRQ;
  66. baseaddr = pci_resource_start(pdev, MOXA_BASE_BAR);
  67. ioaddr = pcim_iomap(pdev, MOXA_BASE_BAR, 0);
  68. if (!ioaddr)
  69. return -ENOMEM;
  70. for (i = 0; i < nr_ports; i++) {
  71. /*
  72. * MOXA Smartio MUE boards with 4 ports have
  73. * a different offset for port #3
  74. */
  75. if (nr_ports == 4 && i == 3)
  76. offset = 7 * MOXA_UART_OFFSET;
  77. else
  78. offset = i * MOXA_UART_OFFSET;
  79. uart.port.iotype = UPIO_MEM;
  80. uart.port.iobase = 0;
  81. uart.port.mapbase = baseaddr + offset;
  82. uart.port.membase = ioaddr + offset;
  83. uart.port.regshift = 0;
  84. dev_dbg(&pdev->dev, "Setup PCI port: port %lx, irq %d, type %d\n",
  85. uart.port.iobase, uart.port.irq, uart.port.iotype);
  86. brd->line[i] = serial8250_register_8250_port(&uart);
  87. if (brd->line[i] < 0) {
  88. dev_err(&pdev->dev,
  89. "Couldn't register serial port %lx, irq %d, type %d, error %d\n",
  90. uart.port.iobase, uart.port.irq,
  91. uart.port.iotype, brd->line[i]);
  92. break;
  93. }
  94. }
  95. pci_set_drvdata(pdev, brd);
  96. return 0;
  97. }
  98. static void moxa8250_remove(struct pci_dev *pdev)
  99. {
  100. struct moxa8250_board *brd = pci_get_drvdata(pdev);
  101. unsigned int i;
  102. for (i = 0; i < brd->num_ports; i++)
  103. serial8250_unregister_port(brd->line[i]);
  104. }
  105. #define MOXA_DEVICE(id, data) { PCI_VDEVICE(MOXA, id), (kernel_ulong_t)data }
  106. static const struct pci_device_id pci_ids[] = {
  107. MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP102E, moxa8250_2p),
  108. MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP102EL, moxa8250_2p),
  109. MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP104EL_A, moxa8250_4p),
  110. MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP114EL, moxa8250_4p),
  111. MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP116E_A_A, moxa8250_8p),
  112. MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP116E_A_B, moxa8250_8p),
  113. MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP118EL_A, moxa8250_8p),
  114. MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP118E_A_I, moxa8250_8p),
  115. MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP132EL, moxa8250_2p),
  116. MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP134EL_A, moxa8250_4p),
  117. MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP138E_A, moxa8250_8p),
  118. MOXA_DEVICE(PCI_DEVICE_ID_MOXA_CP168EL_A, moxa8250_8p),
  119. {0}
  120. };
  121. MODULE_DEVICE_TABLE(pci, pci_ids);
  122. static struct pci_driver moxa8250_pci_driver = {
  123. .name = "8250_moxa",
  124. .id_table = pci_ids,
  125. .probe = moxa8250_probe,
  126. .remove = moxa8250_remove,
  127. };
  128. module_pci_driver(moxa8250_pci_driver);
  129. MODULE_AUTHOR("Mathieu OTHACEHE");
  130. MODULE_DESCRIPTION("MOXA SmartIO MUE driver");
  131. MODULE_LICENSE("GPL v2");