mantis_uart.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "dmxdev.h"
  23. #include "dvbdev.h"
  24. #include "dvb_demux.h"
  25. #include "dvb_frontend.h"
  26. #include "dvb_net.h"
  27. #include "mantis_common.h"
  28. #include "mantis_reg.h"
  29. #include "mantis_uart.h"
  30. struct mantis_uart_params {
  31. enum mantis_baud baud_rate;
  32. enum mantis_parity parity;
  33. };
  34. static struct {
  35. char string[7];
  36. } rates[5] = {
  37. { "9600" },
  38. { "19200" },
  39. { "38400" },
  40. { "57600" },
  41. { "115200" }
  42. };
  43. static struct {
  44. char string[5];
  45. } parity[3] = {
  46. { "NONE" },
  47. { "ODD" },
  48. { "EVEN" }
  49. };
  50. #define UART_MAX_BUF 16
  51. int mantis_uart_read(struct mantis_pci *mantis, u8 *data)
  52. {
  53. struct mantis_hwconfig *config = mantis->hwconfig;
  54. u32 stat = 0, i;
  55. /* get data */
  56. for (i = 0; i < (config->bytes + 1); i++) {
  57. stat = mmread(MANTIS_UART_STAT);
  58. if (stat & MANTIS_UART_RXFIFO_FULL) {
  59. dprintk(MANTIS_ERROR, 1, "RX Fifo FULL");
  60. }
  61. data[i] = mmread(MANTIS_UART_RXD) & 0x3f;
  62. dprintk(MANTIS_DEBUG, 1, "Reading ... <%02x>", data[i] & 0x3f);
  63. if (data[i] & (1 << 7)) {
  64. dprintk(MANTIS_ERROR, 1, "UART framing error");
  65. return -EINVAL;
  66. }
  67. if (data[i] & (1 << 6)) {
  68. dprintk(MANTIS_ERROR, 1, "UART parity error");
  69. return -EINVAL;
  70. }
  71. }
  72. return 0;
  73. }
  74. static void mantis_uart_work(struct work_struct *work)
  75. {
  76. struct mantis_pci *mantis = container_of(work, struct mantis_pci, uart_work);
  77. struct mantis_hwconfig *config = mantis->hwconfig;
  78. u8 buf[16];
  79. int i;
  80. mantis_uart_read(mantis, buf);
  81. for (i = 0; i < (config->bytes + 1); i++)
  82. dprintk(MANTIS_INFO, 1, "UART BUF:%d <%02x> ", i, buf[i]);
  83. dprintk(MANTIS_DEBUG, 0, "\n");
  84. }
  85. static int mantis_uart_setup(struct mantis_pci *mantis,
  86. struct mantis_uart_params *params)
  87. {
  88. u32 reg;
  89. mmwrite((mmread(MANTIS_UART_CTL) | (params->parity & 0x3)), MANTIS_UART_CTL);
  90. reg = mmread(MANTIS_UART_BAUD);
  91. switch (params->baud_rate) {
  92. case MANTIS_BAUD_9600:
  93. reg |= 0xd8;
  94. break;
  95. case MANTIS_BAUD_19200:
  96. reg |= 0x6c;
  97. break;
  98. case MANTIS_BAUD_38400:
  99. reg |= 0x36;
  100. break;
  101. case MANTIS_BAUD_57600:
  102. reg |= 0x23;
  103. break;
  104. case MANTIS_BAUD_115200:
  105. reg |= 0x11;
  106. break;
  107. default:
  108. return -EINVAL;
  109. }
  110. mmwrite(reg, MANTIS_UART_BAUD);
  111. return 0;
  112. }
  113. int mantis_uart_init(struct mantis_pci *mantis)
  114. {
  115. struct mantis_hwconfig *config = mantis->hwconfig;
  116. struct mantis_uart_params params;
  117. /* default parity: */
  118. params.baud_rate = config->baud_rate;
  119. params.parity = config->parity;
  120. dprintk(MANTIS_INFO, 1, "Initializing UART @ %sbps parity:%s",
  121. rates[params.baud_rate].string,
  122. parity[params.parity].string);
  123. init_waitqueue_head(&mantis->uart_wq);
  124. spin_lock_init(&mantis->uart_lock);
  125. INIT_WORK(&mantis->uart_work, mantis_uart_work);
  126. /* disable interrupt */
  127. mmwrite(mmread(MANTIS_UART_CTL) & 0xffef, MANTIS_UART_CTL);
  128. mantis_uart_setup(mantis, &params);
  129. /* default 1 byte */
  130. mmwrite((mmread(MANTIS_UART_BAUD) | (config->bytes << 8)), MANTIS_UART_BAUD);
  131. /* flush buffer */
  132. mmwrite((mmread(MANTIS_UART_CTL) | MANTIS_UART_RXFLUSH), MANTIS_UART_CTL);
  133. /* enable interrupt */
  134. mmwrite(mmread(MANTIS_INT_MASK) | 0x800, MANTIS_INT_MASK);
  135. mmwrite(mmread(MANTIS_UART_CTL) | MANTIS_UART_RXINT, MANTIS_UART_CTL);
  136. schedule_work(&mantis->uart_work);
  137. dprintk(MANTIS_DEBUG, 1, "UART successfully initialized");
  138. return 0;
  139. }
  140. EXPORT_SYMBOL_GPL(mantis_uart_init);
  141. void mantis_uart_exit(struct mantis_pci *mantis)
  142. {
  143. /* disable interrupt */
  144. mmwrite(mmread(MANTIS_UART_CTL) & 0xffef, MANTIS_UART_CTL);
  145. flush_work_sync(&mantis->uart_work);
  146. }
  147. EXPORT_SYMBOL_GPL(mantis_uart_exit);