1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/interrupt.h>
3 #include <linux/ioport.h>
10 #include <linux/serial_core.h>
11 /* WARNING: Do not change this to <linux/serial.h> without testing that
12 * SERIAL_PORT_DFNS does get defined to the appropriate value.
14 #include <asm/serial.h>
16 #ifndef SERIAL_PORT_DFNS
17 #define SERIAL_PORT_DFNS
20 static void start_serial_interrupt(int irq
);
22 static const struct old_serial_port rs_table
[] = {
26 static const struct old_serial_port
*serstate
;
29 static int spk_serial_out(struct spk_synth
*in_synth
, const char ch
);
30 static void spk_serial_send_xchar(char ch
);
31 static void spk_serial_tiocmset(unsigned int set
, unsigned int clear
);
32 static unsigned char spk_serial_in(void);
33 static unsigned char spk_serial_in_nowait(void);
34 static void spk_serial_flush_buffer(void);
36 struct spk_io_ops spk_serial_io_ops
= {
37 .synth_out
= spk_serial_out
,
38 .send_xchar
= spk_serial_send_xchar
,
39 .tiocmset
= spk_serial_tiocmset
,
40 .synth_in
= spk_serial_in
,
41 .synth_in_nowait
= spk_serial_in_nowait
,
42 .flush_buffer
= spk_serial_flush_buffer
,
44 EXPORT_SYMBOL_GPL(spk_serial_io_ops
);
46 const struct old_serial_port
*spk_serial_init(int index
)
48 int baud
= 9600, quot
= 0;
49 unsigned int cval
= 0;
50 int cflag
= CREAD
| HUPCL
| CLOCAL
| B9600
| CS8
;
51 const struct old_serial_port
*ser
;
54 if (index
>= ARRAY_SIZE(rs_table
)) {
55 pr_info("no port info for ttyS%d\n", index
);
58 ser
= rs_table
+ index
;
60 /* Divisor, bytesize and parity */
61 quot
= ser
->baud_base
/ baud
;
62 cval
= cflag
& (CSIZE
| CSTOPB
);
63 #if defined(__powerpc__) || defined(__alpha__)
65 #else /* !__powerpc__ && !__alpha__ */
67 #endif /* !__powerpc__ && !__alpha__ */
69 cval
|= UART_LCR_PARITY
;
70 if (!(cflag
& PARODD
))
71 cval
|= UART_LCR_EPAR
;
72 if (synth_request_region(ser
->port
, 8)) {
73 /* try to take it back. */
74 pr_info("Ports not available, trying to steal them\n");
75 __release_region(&ioport_resource
, ser
->port
, 8);
76 err
= synth_request_region(ser
->port
, 8);
78 pr_warn("Unable to allocate port at %x, errno %i",
84 /* Disable UART interrupts, set DTR and RTS high
87 outb(cval
| UART_LCR_DLAB
, ser
->port
+ UART_LCR
); /* set DLAB */
88 outb(quot
& 0xff, ser
->port
+ UART_DLL
); /* LS of divisor */
89 outb(quot
>> 8, ser
->port
+ UART_DLM
); /* MS of divisor */
90 outb(cval
, ser
->port
+ UART_LCR
); /* reset DLAB */
92 /* Turn off Interrupts */
93 outb(0, ser
->port
+ UART_IER
);
94 outb(UART_MCR_DTR
| UART_MCR_RTS
, ser
->port
+ UART_MCR
);
96 /* If we read 0xff from the LSR, there is no UART here. */
97 if (inb(ser
->port
+ UART_LSR
) == 0xff) {
98 synth_release_region(ser
->port
, 8);
104 speakup_info
.port_tts
= ser
->port
;
107 start_serial_interrupt(ser
->irq
);
112 static irqreturn_t
synth_readbuf_handler(int irq
, void *dev_id
)
117 spin_lock_irqsave(&speakup_info
.spinlock
, flags
);
118 while (inb_p(speakup_info
.port_tts
+ UART_LSR
) & UART_LSR_DR
) {
119 c
= inb_p(speakup_info
.port_tts
+ UART_RX
);
120 synth
->read_buff_add((u_char
)c
);
122 spin_unlock_irqrestore(&speakup_info
.spinlock
, flags
);
126 static void start_serial_interrupt(int irq
)
130 if (!synth
->read_buff_add
)
133 rv
= request_irq(irq
, synth_readbuf_handler
, IRQF_SHARED
,
134 "serial", (void *)synth_readbuf_handler
);
137 pr_err("Unable to request Speakup serial I R Q\n");
139 outb(UART_MCR_DTR
| UART_MCR_RTS
| UART_MCR_OUT2
,
140 speakup_info
.port_tts
+ UART_MCR
);
141 /* Turn on Interrupts */
142 outb(UART_IER_MSI
| UART_IER_RLSI
| UART_IER_RDI
,
143 speakup_info
.port_tts
+ UART_IER
);
144 inb(speakup_info
.port_tts
+ UART_LSR
);
145 inb(speakup_info
.port_tts
+ UART_RX
);
146 inb(speakup_info
.port_tts
+ UART_IIR
);
147 inb(speakup_info
.port_tts
+ UART_MSR
);
148 outb(1, speakup_info
.port_tts
+ UART_FCR
); /* Turn FIFO On */
151 static void spk_serial_send_xchar(char ch
)
153 int timeout
= SPK_XMITR_TIMEOUT
;
155 while (spk_serial_tx_busy()) {
160 outb(ch
, speakup_info
.port_tts
);
163 static void spk_serial_tiocmset(unsigned int set
, unsigned int clear
)
165 int old
= inb(speakup_info
.port_tts
+ UART_MCR
);
167 outb((old
& ~clear
) | set
, speakup_info
.port_tts
+ UART_MCR
);
170 int spk_serial_synth_probe(struct spk_synth
*synth
)
172 const struct old_serial_port
*ser
;
175 if ((synth
->ser
>= SPK_LO_TTY
) && (synth
->ser
<= SPK_HI_TTY
)) {
176 ser
= spk_serial_init(synth
->ser
);
180 outb_p(0, ser
->port
);
182 outb_p('\r', ser
->port
);
186 pr_warn("ttyS%i is an invalid port\n", synth
->ser
);
189 pr_info("%s: not found\n", synth
->long_name
);
192 pr_info("%s: ttyS%i, Driver Version %s\n",
193 synth
->long_name
, synth
->ser
, synth
->version
);
197 EXPORT_SYMBOL_GPL(spk_serial_synth_probe
);
199 void spk_stop_serial_interrupt(void)
201 if (speakup_info
.port_tts
== 0)
204 if (!synth
->read_buff_add
)
207 /* Turn off interrupts */
208 outb(0, speakup_info
.port_tts
+ UART_IER
);
210 free_irq(serstate
->irq
, (void *)synth_readbuf_handler
);
212 EXPORT_SYMBOL_GPL(spk_stop_serial_interrupt
);
214 int spk_wait_for_xmitr(struct spk_synth
*in_synth
)
216 int tmout
= SPK_XMITR_TIMEOUT
;
218 if ((in_synth
->alive
) && (timeouts
>= NUM_DISABLE_TIMEOUTS
)) {
219 pr_warn("%s: too many timeouts, deactivating speakup\n",
220 in_synth
->long_name
);
222 /* No synth any more, so nobody will restart TTYs, and we thus
223 * need to do it ourselves. Now that there is no synth we can
224 * let application flood anyway
226 speakup_start_ttys();
230 while (spk_serial_tx_busy()) {
232 pr_warn("%s: timed out (tx busy)\n",
233 in_synth
->long_name
);
239 tmout
= SPK_CTS_TIMEOUT
;
240 while (!((inb_p(speakup_info
.port_tts
+ UART_MSR
)) & UART_MSR_CTS
)) {
252 static unsigned char spk_serial_in(void)
254 int tmout
= SPK_SERIAL_TIMEOUT
;
256 while (!(inb_p(speakup_info
.port_tts
+ UART_LSR
) & UART_LSR_DR
)) {
258 pr_warn("time out while waiting for input.\n");
263 return inb_p(speakup_info
.port_tts
+ UART_RX
);
266 static unsigned char spk_serial_in_nowait(void)
270 lsr
= inb_p(speakup_info
.port_tts
+ UART_LSR
);
271 if (!(lsr
& UART_LSR_DR
))
273 return inb_p(speakup_info
.port_tts
+ UART_RX
);
276 static void spk_serial_flush_buffer(void)
278 /* TODO: flush the UART 16550 buffer */
281 static int spk_serial_out(struct spk_synth
*in_synth
, const char ch
)
283 if (in_synth
->alive
&& spk_wait_for_xmitr(in_synth
)) {
284 outb_p(ch
, speakup_info
.port_tts
);
290 const char *spk_serial_synth_immediate(struct spk_synth
*synth
,
295 while ((ch
= *buff
)) {
297 ch
= synth
->procspeech
;
298 if (spk_wait_for_xmitr(synth
))
299 outb(ch
, speakup_info
.port_tts
);
306 EXPORT_SYMBOL_GPL(spk_serial_synth_immediate
);
308 void spk_serial_release(void)
310 spk_stop_serial_interrupt();
311 if (speakup_info
.port_tts
== 0)
313 synth_release_region(speakup_info
.port_tts
, 8);
314 speakup_info
.port_tts
= 0;
316 EXPORT_SYMBOL_GPL(spk_serial_release
);