PM / AVS: rockchip-io: add io selectors and supplies for rk3399
[linux-2.6-block.git] / drivers / staging / speakup / serialio.c
CommitLineData
c6e3fd22
WH
1#include <linux/interrupt.h>
2#include <linux/ioport.h>
3
4#include "spk_types.h"
5#include "speakup.h"
6#include "spk_priv.h"
7#include "serialio.h"
8
327b882d
ST
9#include <linux/serial_core.h>
10/* WARNING: Do not change this to <linux/serial.h> without testing that
11 * SERIAL_PORT_DFNS does get defined to the appropriate value. */
12#include <asm/serial.h>
13
5e6dc548
CG
14#ifndef SERIAL_PORT_DFNS
15#define SERIAL_PORT_DFNS
16#endif
17
c6e3fd22
WH
18static void start_serial_interrupt(int irq);
19
3ee0017e 20static const struct old_serial_port rs_table[] = {
c6e3fd22
WH
21 SERIAL_PORT_DFNS
22};
3ee0017e 23static const struct old_serial_port *serstate;
c6e3fd22
WH
24static int timeouts;
25
3ee0017e 26const struct old_serial_port *spk_serial_init(int index)
c6e3fd22
WH
27{
28 int baud = 9600, quot = 0;
29 unsigned int cval = 0;
30 int cflag = CREAD | HUPCL | CLOCAL | B9600 | CS8;
327b882d 31 const struct old_serial_port *ser;
c6e3fd22
WH
32 int err;
33
327b882d
ST
34 if (index >= ARRAY_SIZE(rs_table)) {
35 pr_info("no port info for ttyS%d\n", index);
36 return NULL;
37 }
38 ser = rs_table + index;
39
c6e3fd22
WH
40 /* Divisor, bytesize and parity */
41 quot = ser->baud_base / baud;
42 cval = cflag & (CSIZE | CSTOPB);
43#if defined(__powerpc__) || defined(__alpha__)
44 cval >>= 8;
45#else /* !__powerpc__ && !__alpha__ */
46 cval >>= 4;
47#endif /* !__powerpc__ && !__alpha__ */
48 if (cflag & PARENB)
49 cval |= UART_LCR_PARITY;
50 if (!(cflag & PARODD))
51 cval |= UART_LCR_EPAR;
52 if (synth_request_region(ser->port, 8)) {
53 /* try to take it back. */
3a046c19 54 pr_info("Ports not available, trying to steal them\n");
c6e3fd22
WH
55 __release_region(&ioport_resource, ser->port, 8);
56 err = synth_request_region(ser->port, 8);
57 if (err) {
3ee0017e 58 pr_warn("Unable to allocate port at %x, errno %i",
baf9ac9f 59 ser->port, err);
c6e3fd22
WH
60 return NULL;
61 }
62 }
63
64 /* Disable UART interrupts, set DTR and RTS high
13d825ed
AF
65 * and set speed.
66 */
c6e3fd22
WH
67 outb(cval | UART_LCR_DLAB, ser->port + UART_LCR); /* set DLAB */
68 outb(quot & 0xff, ser->port + UART_DLL); /* LS of divisor */
69 outb(quot >> 8, ser->port + UART_DLM); /* MS of divisor */
70 outb(cval, ser->port + UART_LCR); /* reset DLAB */
71
72 /* Turn off Interrupts */
73 outb(0, ser->port + UART_IER);
74 outb(UART_MCR_DTR | UART_MCR_RTS, ser->port + UART_MCR);
75
76 /* If we read 0xff from the LSR, there is no UART here. */
77 if (inb(ser->port + UART_LSR) == 0xff) {
78 synth_release_region(ser->port, 8);
79 serstate = NULL;
80 return NULL;
81 }
82
83 mdelay(1);
84 speakup_info.port_tts = ser->port;
85 serstate = ser;
86
87 start_serial_interrupt(ser->irq);
88
89 return ser;
90}
91
92static irqreturn_t synth_readbuf_handler(int irq, void *dev_id)
93{
94 unsigned long flags;
95/*printk(KERN_ERR "in irq\n"); */
96/*pr_warn("in IRQ\n"); */
97 int c;
8e69a811 98
9fb31b1a 99 spin_lock_irqsave(&speakup_info.spinlock, flags);
c6e3fd22
WH
100 while (inb_p(speakup_info.port_tts + UART_LSR) & UART_LSR_DR) {
101
102 c = inb_p(speakup_info.port_tts+UART_RX);
103 synth->read_buff_add((u_char) c);
104/*printk(KERN_ERR "c = %d\n", c); */
105/*pr_warn("C = %d\n", c); */
106 }
9fb31b1a 107 spin_unlock_irqrestore(&speakup_info.spinlock, flags);
c6e3fd22
WH
108 return IRQ_HANDLED;
109}
110
111static void start_serial_interrupt(int irq)
112{
113 int rv;
114
114885e0 115 if (!synth->read_buff_add)
c6e3fd22
WH
116 return;
117
118 rv = request_irq(irq, synth_readbuf_handler, IRQF_SHARED,
119 "serial", (void *) synth_readbuf_handler);
120
121 if (rv)
3a046c19 122 pr_err("Unable to request Speakup serial I R Q\n");
c6e3fd22
WH
123 /* Set MCR */
124 outb(UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2,
125 speakup_info.port_tts + UART_MCR);
126 /* Turn on Interrupts */
127 outb(UART_IER_MSI|UART_IER_RLSI|UART_IER_RDI,
128 speakup_info.port_tts + UART_IER);
129 inb(speakup_info.port_tts+UART_LSR);
130 inb(speakup_info.port_tts+UART_RX);
131 inb(speakup_info.port_tts+UART_IIR);
132 inb(speakup_info.port_tts+UART_MSR);
133 outb(1, speakup_info.port_tts + UART_FCR); /* Turn FIFO On */
134}
135
ca2beaf8 136void spk_stop_serial_interrupt(void)
c6e3fd22
WH
137{
138 if (speakup_info.port_tts == 0)
139 return;
140
114885e0 141 if (!synth->read_buff_add)
c6e3fd22
WH
142 return;
143
144 /* Turn off interrupts */
145 outb(0, speakup_info.port_tts+UART_IER);
146 /* Free IRQ */
147 free_irq(serstate->irq, (void *) synth_readbuf_handler);
148}
149
ca2beaf8 150int spk_wait_for_xmitr(void)
c6e3fd22
WH
151{
152 int tmout = SPK_XMITR_TIMEOUT;
8e69a811 153
c6e3fd22 154 if ((synth->alive) && (timeouts >= NUM_DISABLE_TIMEOUTS)) {
baf9ac9f
WH
155 pr_warn("%s: too many timeouts, deactivating speakup\n",
156 synth->long_name);
c6e3fd22
WH
157 synth->alive = 0;
158 /* No synth any more, so nobody will restart TTYs, and we thus
159 * need to do it ourselves. Now that there is no synth we can
13d825ed
AF
160 * let application flood anyway
161 */
c6e3fd22
WH
162 speakup_start_ttys();
163 timeouts = 0;
164 return 0;
165 }
166 while (spk_serial_tx_busy()) {
167 if (--tmout == 0) {
168 pr_warn("%s: timed out (tx busy)\n", synth->long_name);
169 timeouts++;
170 return 0;
171 }
172 udelay(1);
173 }
174 tmout = SPK_CTS_TIMEOUT;
175 while (!((inb_p(speakup_info.port_tts + UART_MSR)) & UART_MSR_CTS)) {
176 /* CTS */
177 if (--tmout == 0) {
baf9ac9f 178 /* pr_warn("%s: timed out (cts)\n",
13d825ed
AF
179 * synth->long_name);
180 */
c6e3fd22
WH
181 timeouts++;
182 return 0;
183 }
184 udelay(1);
185 }
186 timeouts = 0;
187 return 1;
188}
189
190unsigned char spk_serial_in(void)
191{
192 int tmout = SPK_SERIAL_TIMEOUT;
193
194 while (!(inb_p(speakup_info.port_tts + UART_LSR) & UART_LSR_DR)) {
195 if (--tmout == 0) {
196 pr_warn("time out while waiting for input.\n");
197 return 0xff;
198 }
199 udelay(1);
200 }
201 return inb_p(speakup_info.port_tts + UART_RX);
202}
203EXPORT_SYMBOL_GPL(spk_serial_in);
204
205unsigned char spk_serial_in_nowait(void)
206{
207 unsigned char lsr;
208
209 lsr = inb_p(speakup_info.port_tts + UART_LSR);
210 if (!(lsr & UART_LSR_DR))
211 return 0;
212 return inb_p(speakup_info.port_tts + UART_RX);
213}
214EXPORT_SYMBOL_GPL(spk_serial_in_nowait);
215
216int spk_serial_out(const char ch)
217{
ca2beaf8 218 if (synth->alive && spk_wait_for_xmitr()) {
c6e3fd22
WH
219 outb_p(ch, speakup_info.port_tts);
220 return 1;
221 }
222 return 0;
223}
224EXPORT_SYMBOL_GPL(spk_serial_out);
225
226void spk_serial_release(void)
227{
228 if (speakup_info.port_tts == 0)
229 return;
230 synth_release_region(speakup_info.port_tts, 8);
231 speakup_info.port_tts = 0;
232}
233EXPORT_SYMBOL_GPL(spk_serial_release);