Manu Abraham | a149735 | 2009-12-11 20:41:07 -0300 | [diff] [blame] | 1 | /* |
| 2 | Mantis PCI bridge driver |
| 3 | |
| 4 | Copyright (C) Manu Abraham (abraham.manu@gmail.com) |
| 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 as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame] | 21 | #include <linux/kernel.h> |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 22 | #include <linux/spinlock.h> |
Alexey Dobriyan | b7f080c | 2011-06-16 11:01:34 +0000 | [diff] [blame] | 23 | #include <asm/io.h> |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame] | 24 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame] | 25 | #include <linux/signal.h> |
| 26 | #include <linux/sched.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | |
| 29 | #include "dmxdev.h" |
| 30 | #include "dvbdev.h" |
| 31 | #include "dvb_demux.h" |
| 32 | #include "dvb_frontend.h" |
| 33 | #include "dvb_net.h" |
| 34 | |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 35 | #include "mantis_common.h" |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame] | 36 | #include "mantis_reg.h" |
| 37 | #include "mantis_uart.h" |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 38 | |
| 39 | struct mantis_uart_params { |
| 40 | enum mantis_baud baud_rate; |
| 41 | enum mantis_parity parity; |
| 42 | }; |
| 43 | |
Manu Abraham | a149735 | 2009-12-11 20:41:07 -0300 | [diff] [blame] | 44 | static struct { |
| 45 | char string[7]; |
| 46 | } rates[5] = { |
| 47 | { "9600" }, |
| 48 | { "19200" }, |
| 49 | { "38400" }, |
| 50 | { "57600" }, |
| 51 | { "115200" } |
| 52 | }; |
| 53 | |
| 54 | static struct { |
| 55 | char string[5]; |
| 56 | } parity[3] = { |
| 57 | { "NONE" }, |
| 58 | { "ODD" }, |
| 59 | { "EVEN" } |
| 60 | }; |
| 61 | |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 62 | #define UART_MAX_BUF 16 |
| 63 | |
Mauro Carvalho Chehab | 6860f9c | 2012-10-27 13:00:58 -0300 | [diff] [blame] | 64 | static int mantis_uart_read(struct mantis_pci *mantis, u8 *data) |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 65 | { |
| 66 | struct mantis_hwconfig *config = mantis->hwconfig; |
Manu Abraham | 0bdc799 | 2009-12-15 06:17:54 -0300 | [diff] [blame] | 67 | u32 stat = 0, i; |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 68 | |
| 69 | /* get data */ |
| 70 | for (i = 0; i < (config->bytes + 1); i++) { |
| 71 | |
Manu Abraham | 0bdc799 | 2009-12-15 06:17:54 -0300 | [diff] [blame] | 72 | stat = mmread(MANTIS_UART_STAT); |
| 73 | |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 74 | if (stat & MANTIS_UART_RXFIFO_FULL) { |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame] | 75 | dprintk(MANTIS_ERROR, 1, "RX Fifo FULL"); |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 76 | } |
| 77 | data[i] = mmread(MANTIS_UART_RXD) & 0x3f; |
| 78 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame] | 79 | dprintk(MANTIS_DEBUG, 1, "Reading ... <%02x>", data[i] & 0x3f); |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 80 | |
| 81 | if (data[i] & (1 << 7)) { |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame] | 82 | dprintk(MANTIS_ERROR, 1, "UART framing error"); |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 83 | return -EINVAL; |
| 84 | } |
| 85 | if (data[i] & (1 << 6)) { |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame] | 86 | dprintk(MANTIS_ERROR, 1, "UART parity error"); |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 87 | return -EINVAL; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static void mantis_uart_work(struct work_struct *work) |
| 95 | { |
| 96 | struct mantis_pci *mantis = container_of(work, struct mantis_pci, uart_work); |
| 97 | struct mantis_hwconfig *config = mantis->hwconfig; |
| 98 | u8 buf[16]; |
| 99 | int i; |
| 100 | |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 101 | mantis_uart_read(mantis, buf); |
| 102 | |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 103 | for (i = 0; i < (config->bytes + 1); i++) |
Manu Abraham | a149735 | 2009-12-11 20:41:07 -0300 | [diff] [blame] | 104 | dprintk(MANTIS_INFO, 1, "UART BUF:%d <%02x> ", i, buf[i]); |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 105 | |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame] | 106 | dprintk(MANTIS_DEBUG, 0, "\n"); |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static int mantis_uart_setup(struct mantis_pci *mantis, |
| 110 | struct mantis_uart_params *params) |
| 111 | { |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 112 | u32 reg; |
| 113 | |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 114 | mmwrite((mmread(MANTIS_UART_CTL) | (params->parity & 0x3)), MANTIS_UART_CTL); |
| 115 | |
| 116 | reg = mmread(MANTIS_UART_BAUD); |
| 117 | |
| 118 | switch (params->baud_rate) { |
| 119 | case MANTIS_BAUD_9600: |
| 120 | reg |= 0xd8; |
| 121 | break; |
| 122 | case MANTIS_BAUD_19200: |
| 123 | reg |= 0x6c; |
| 124 | break; |
| 125 | case MANTIS_BAUD_38400: |
| 126 | reg |= 0x36; |
| 127 | break; |
| 128 | case MANTIS_BAUD_57600: |
| 129 | reg |= 0x23; |
| 130 | break; |
| 131 | case MANTIS_BAUD_115200: |
| 132 | reg |= 0x11; |
| 133 | break; |
| 134 | default: |
| 135 | return -EINVAL; |
| 136 | } |
| 137 | |
| 138 | mmwrite(reg, MANTIS_UART_BAUD); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | int mantis_uart_init(struct mantis_pci *mantis) |
| 144 | { |
| 145 | struct mantis_hwconfig *config = mantis->hwconfig; |
| 146 | struct mantis_uart_params params; |
| 147 | |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 148 | /* default parity: */ |
| 149 | params.baud_rate = config->baud_rate; |
| 150 | params.parity = config->parity; |
Manu Abraham | a149735 | 2009-12-11 20:41:07 -0300 | [diff] [blame] | 151 | dprintk(MANTIS_INFO, 1, "Initializing UART @ %sbps parity:%s", |
| 152 | rates[params.baud_rate].string, |
| 153 | parity[params.parity].string); |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 154 | |
| 155 | init_waitqueue_head(&mantis->uart_wq); |
| 156 | spin_lock_init(&mantis->uart_lock); |
| 157 | |
| 158 | INIT_WORK(&mantis->uart_work, mantis_uart_work); |
| 159 | |
| 160 | /* disable interrupt */ |
| 161 | mmwrite(mmread(MANTIS_UART_CTL) & 0xffef, MANTIS_UART_CTL); |
| 162 | |
| 163 | mantis_uart_setup(mantis, ¶ms); |
| 164 | |
| 165 | /* default 1 byte */ |
| 166 | mmwrite((mmread(MANTIS_UART_BAUD) | (config->bytes << 8)), MANTIS_UART_BAUD); |
| 167 | |
| 168 | /* flush buffer */ |
| 169 | mmwrite((mmread(MANTIS_UART_CTL) | MANTIS_UART_RXFLUSH), MANTIS_UART_CTL); |
| 170 | |
| 171 | /* enable interrupt */ |
| 172 | mmwrite(mmread(MANTIS_INT_MASK) | 0x800, MANTIS_INT_MASK); |
| 173 | mmwrite(mmread(MANTIS_UART_CTL) | MANTIS_UART_RXINT, MANTIS_UART_CTL); |
| 174 | |
| 175 | schedule_work(&mantis->uart_work); |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 176 | dprintk(MANTIS_DEBUG, 1, "UART successfully initialized"); |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 177 | |
| 178 | return 0; |
| 179 | } |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame] | 180 | EXPORT_SYMBOL_GPL(mantis_uart_init); |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 181 | |
| 182 | void mantis_uart_exit(struct mantis_pci *mantis) |
| 183 | { |
| 184 | /* disable interrupt */ |
| 185 | mmwrite(mmread(MANTIS_UART_CTL) & 0xffef, MANTIS_UART_CTL); |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 186 | flush_work(&mantis->uart_work); |
Manu Abraham | add2063 | 2009-12-04 05:39:57 -0300 | [diff] [blame] | 187 | } |
Manu Abraham | b3b9614 | 2009-12-04 05:41:11 -0300 | [diff] [blame] | 188 | EXPORT_SYMBOL_GPL(mantis_uart_exit); |