mantis_uart.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. Mantis PCI bridge driver
  3. Copyright (C) Manu Abraham (abraham.manu@gmail.com)
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/spinlock.h>
  18. #include <asm/io.h>
  19. #include <linux/signal.h>
  20. #include <linux/sched.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/pci.h>
  23. #include "dmxdev.h"
  24. #include "dvbdev.h"
  25. #include "dvb_demux.h"
  26. #include "dvb_frontend.h"
  27. #include "dvb_net.h"
  28. #include "mantis_common.h"
  29. #include "mantis_reg.h"
  30. #include "mantis_uart.h"
  31. #include "mantis_input.h"
  32. struct mantis_uart_params {
  33. enum mantis_baud baud_rate;
  34. enum mantis_parity parity;
  35. };
  36. static struct {
  37. char string[7];
  38. } rates[5] = {
  39. { "9600" },
  40. { "19200" },
  41. { "38400" },
  42. { "57600" },
  43. { "115200" }
  44. };
  45. static struct {
  46. char string[5];
  47. } parity[3] = {
  48. { "NONE" },
  49. { "ODD" },
  50. { "EVEN" }
  51. };
  52. static void mantis_uart_read(struct mantis_pci *mantis)
  53. {
  54. struct mantis_hwconfig *config = mantis->hwconfig;
  55. int i, scancode = 0, err = 0;
  56. /* get data */
  57. dprintk(MANTIS_DEBUG, 1, "UART Reading ...");
  58. for (i = 0; i < (config->bytes + 1); i++) {
  59. int data = mmread(MANTIS_UART_RXD);
  60. dprintk(MANTIS_DEBUG, 0, " <%02x>", data);
  61. scancode = (scancode << 8) | (data & 0x3f);
  62. err |= data;
  63. if (data & (1 << 7))
  64. dprintk(MANTIS_ERROR, 1, "UART framing error");
  65. if (data & (1 << 6))
  66. dprintk(MANTIS_ERROR, 1, "UART parity error");
  67. }
  68. dprintk(MANTIS_DEBUG, 0, "\n");
  69. if ((err & 0xC0) == 0)
  70. mantis_input_process(mantis, scancode);
  71. }
  72. static void mantis_uart_work(struct work_struct *work)
  73. {
  74. struct mantis_pci *mantis = container_of(work, struct mantis_pci, uart_work);
  75. u32 stat;
  76. stat = mmread(MANTIS_UART_STAT);
  77. if (stat & MANTIS_UART_RXFIFO_FULL)
  78. dprintk(MANTIS_ERROR, 1, "RX Fifo FULL");
  79. /*
  80. * MANTIS_UART_RXFIFO_DATA is only set if at least
  81. * config->bytes + 1 bytes are in the FIFO.
  82. */
  83. while (stat & MANTIS_UART_RXFIFO_DATA) {
  84. mantis_uart_read(mantis);
  85. stat = mmread(MANTIS_UART_STAT);
  86. }
  87. /* re-enable UART (RX) interrupt */
  88. mantis_unmask_ints(mantis, MANTIS_INT_IRQ1);
  89. }
  90. static int mantis_uart_setup(struct mantis_pci *mantis,
  91. struct mantis_uart_params *params)
  92. {
  93. u32 reg;
  94. mmwrite((mmread(MANTIS_UART_CTL) | (params->parity & 0x3)), MANTIS_UART_CTL);
  95. reg = mmread(MANTIS_UART_BAUD);
  96. switch (params->baud_rate) {
  97. case MANTIS_BAUD_9600:
  98. reg |= 0xd8;
  99. break;
  100. case MANTIS_BAUD_19200:
  101. reg |= 0x6c;
  102. break;
  103. case MANTIS_BAUD_38400:
  104. reg |= 0x36;
  105. break;
  106. case MANTIS_BAUD_57600:
  107. reg |= 0x23;
  108. break;
  109. case MANTIS_BAUD_115200:
  110. reg |= 0x11;
  111. break;
  112. default:
  113. return -EINVAL;
  114. }
  115. mmwrite(reg, MANTIS_UART_BAUD);
  116. return 0;
  117. }
  118. int mantis_uart_init(struct mantis_pci *mantis)
  119. {
  120. struct mantis_hwconfig *config = mantis->hwconfig;
  121. struct mantis_uart_params params;
  122. /* default parity: */
  123. params.baud_rate = config->baud_rate;
  124. params.parity = config->parity;
  125. dprintk(MANTIS_INFO, 1, "Initializing UART @ %sbps parity:%s",
  126. rates[params.baud_rate].string,
  127. parity[params.parity].string);
  128. INIT_WORK(&mantis->uart_work, mantis_uart_work);
  129. /* disable interrupt */
  130. mmwrite(mmread(MANTIS_UART_CTL) & 0xffef, MANTIS_UART_CTL);
  131. mantis_uart_setup(mantis, &params);
  132. /* default 1 byte */
  133. mmwrite((mmread(MANTIS_UART_BAUD) | (config->bytes << 8)), MANTIS_UART_BAUD);
  134. /* flush buffer */
  135. mmwrite((mmread(MANTIS_UART_CTL) | MANTIS_UART_RXFLUSH), MANTIS_UART_CTL);
  136. /* enable interrupt */
  137. mmwrite(mmread(MANTIS_UART_CTL) | MANTIS_UART_RXINT, MANTIS_UART_CTL);
  138. mantis_unmask_ints(mantis, MANTIS_INT_IRQ1);
  139. schedule_work(&mantis->uart_work);
  140. dprintk(MANTIS_DEBUG, 1, "UART successfully initialized");
  141. return 0;
  142. }
  143. EXPORT_SYMBOL_GPL(mantis_uart_init);
  144. void mantis_uart_exit(struct mantis_pci *mantis)
  145. {
  146. /* disable interrupt */
  147. mantis_mask_ints(mantis, MANTIS_INT_IRQ1);
  148. mmwrite(mmread(MANTIS_UART_CTL) & 0xffef, MANTIS_UART_CTL);
  149. flush_work(&mantis->uart_work);
  150. }
  151. EXPORT_SYMBOL_GPL(mantis_uart_exit);