mantis_uart.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <media/dmxdev.h>
  24. #include <media/dvbdev.h>
  25. #include <media/dvb_demux.h>
  26. #include <media/dvb_frontend.h>
  27. #include <media/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. unsigned long timeout;
  77. stat = mmread(MANTIS_UART_STAT);
  78. if (stat & MANTIS_UART_RXFIFO_FULL)
  79. dprintk(MANTIS_ERROR, 1, "RX Fifo FULL");
  80. /*
  81. * MANTIS_UART_RXFIFO_DATA is only set if at least
  82. * config->bytes + 1 bytes are in the FIFO.
  83. */
  84. /* FIXME: is 10ms good enough ? */
  85. timeout = jiffies + msecs_to_jiffies(10);
  86. while (stat & MANTIS_UART_RXFIFO_DATA) {
  87. mantis_uart_read(mantis);
  88. stat = mmread(MANTIS_UART_STAT);
  89. if (!time_is_after_jiffies(timeout))
  90. break;
  91. }
  92. /* re-enable UART (RX) interrupt */
  93. mantis_unmask_ints(mantis, MANTIS_INT_IRQ1);
  94. }
  95. static int mantis_uart_setup(struct mantis_pci *mantis,
  96. struct mantis_uart_params *params)
  97. {
  98. u32 reg;
  99. mmwrite((mmread(MANTIS_UART_CTL) | (params->parity & 0x3)), MANTIS_UART_CTL);
  100. reg = mmread(MANTIS_UART_BAUD);
  101. switch (params->baud_rate) {
  102. case MANTIS_BAUD_9600:
  103. reg |= 0xd8;
  104. break;
  105. case MANTIS_BAUD_19200:
  106. reg |= 0x6c;
  107. break;
  108. case MANTIS_BAUD_38400:
  109. reg |= 0x36;
  110. break;
  111. case MANTIS_BAUD_57600:
  112. reg |= 0x23;
  113. break;
  114. case MANTIS_BAUD_115200:
  115. reg |= 0x11;
  116. break;
  117. default:
  118. return -EINVAL;
  119. }
  120. mmwrite(reg, MANTIS_UART_BAUD);
  121. return 0;
  122. }
  123. int mantis_uart_init(struct mantis_pci *mantis)
  124. {
  125. struct mantis_hwconfig *config = mantis->hwconfig;
  126. struct mantis_uart_params params;
  127. /* default parity: */
  128. params.baud_rate = config->baud_rate;
  129. params.parity = config->parity;
  130. dprintk(MANTIS_INFO, 1, "Initializing UART @ %sbps parity:%s",
  131. rates[params.baud_rate].string,
  132. parity[params.parity].string);
  133. INIT_WORK(&mantis->uart_work, mantis_uart_work);
  134. /* disable interrupt */
  135. mmwrite(mmread(MANTIS_UART_CTL) & 0xffef, MANTIS_UART_CTL);
  136. mantis_uart_setup(mantis, &params);
  137. /* default 1 byte */
  138. mmwrite((mmread(MANTIS_UART_BAUD) | (config->bytes << 8)), MANTIS_UART_BAUD);
  139. /* flush buffer */
  140. mmwrite((mmread(MANTIS_UART_CTL) | MANTIS_UART_RXFLUSH), MANTIS_UART_CTL);
  141. /* enable interrupt */
  142. mmwrite(mmread(MANTIS_UART_CTL) | MANTIS_UART_RXINT, MANTIS_UART_CTL);
  143. mantis_unmask_ints(mantis, MANTIS_INT_IRQ1);
  144. schedule_work(&mantis->uart_work);
  145. dprintk(MANTIS_DEBUG, 1, "UART successfully initialized");
  146. return 0;
  147. }
  148. EXPORT_SYMBOL_GPL(mantis_uart_init);
  149. void mantis_uart_exit(struct mantis_pci *mantis)
  150. {
  151. /* disable interrupt */
  152. mantis_mask_ints(mantis, MANTIS_INT_IRQ1);
  153. mmwrite(mmread(MANTIS_UART_CTL) & 0xffef, MANTIS_UART_CTL);
  154. flush_work(&mantis->uart_work);
  155. }
  156. EXPORT_SYMBOL_GPL(mantis_uart_exit);